@parcha/agentrun-serve 0.1.0-beta.32 → 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 (444) hide show
  1. package/CHANGELOG.md +270 -0
  2. package/README.md +343 -246
  3. package/dist/adapters.d.ts +0 -7
  4. package/dist/adapters.js +11 -22
  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 +14 -0
  14. package/dist/api-only.js +33 -0
  15. package/dist/archil-routes.d.ts +1 -1
  16. package/dist/bash-read-only.d.ts +22 -0
  17. package/dist/bash-read-only.js +701 -0
  18. package/dist/box-egress-vm.d.ts +0 -8
  19. package/dist/box-egress-vm.js +0 -10
  20. package/dist/box-egress.d.ts +0 -4
  21. package/dist/box-egress.js +1 -16
  22. package/dist/box-paths.d.ts +25 -2
  23. package/dist/box-paths.js +79 -11
  24. package/dist/box-relay.d.ts +0 -11
  25. package/dist/box-relay.js +0 -20
  26. package/dist/box-settings.d.ts +26 -0
  27. package/dist/box-settings.js +63 -0
  28. package/dist/boxes.d.ts +21 -1
  29. package/dist/boxes.js +27 -20
  30. package/dist/browser-commands.d.ts +5 -2
  31. package/dist/browser-commands.js +4 -4
  32. package/dist/browser-computer.d.ts +5 -0
  33. package/dist/browser-computer.js +21 -0
  34. package/dist/browser-cost.d.ts +9 -6
  35. package/dist/browser-cost.js +94 -51
  36. package/dist/browser-live.d.ts +7 -20
  37. package/dist/browser-live.js +15 -93
  38. package/dist/browser-person.d.ts +18 -53
  39. package/dist/browser-person.js +94 -279
  40. package/dist/budget.d.ts +0 -4
  41. package/dist/budget.js +0 -7
  42. package/dist/cap.d.ts +8 -12
  43. package/dist/cap.js +12 -27
  44. package/dist/cli.js +61 -20
  45. package/dist/closed-pipes.d.ts +2 -0
  46. package/dist/closed-pipes.js +13 -0
  47. package/dist/computer-commands.d.ts +2 -0
  48. package/dist/computer-commands.js +2 -0
  49. package/dist/computer-keys.js +5 -2
  50. package/dist/computer-setting.d.ts +20 -4
  51. package/dist/computer-setting.js +25 -5
  52. package/dist/computers.d.ts +31 -3
  53. package/dist/computers.js +70 -11
  54. package/dist/confine.d.ts +120 -0
  55. package/dist/confine.js +436 -0
  56. package/dist/console/assets/index-RPHGlXmM.js +540 -0
  57. package/dist/console/assets/index-opTN5Naq.css +1 -0
  58. package/dist/console/index.html +2 -2
  59. package/dist/daytona-api.d.ts +0 -11
  60. package/dist/daytona-api.js +1 -82
  61. package/dist/decision-rows.d.ts +1 -1
  62. package/dist/decision-rows.js +1 -1
  63. package/dist/docker-daemon.d.ts +31 -0
  64. package/dist/docker-daemon.js +56 -0
  65. package/dist/docker-host-addresses.d.ts +37 -0
  66. package/dist/docker-host-addresses.js +110 -0
  67. package/dist/effort-guidance.js +4 -3
  68. package/dist/environment.d.ts +0 -3
  69. package/dist/environment.js +3 -5
  70. package/dist/errors.d.ts +0 -2
  71. package/dist/errors.js +6 -9
  72. package/dist/filed-answer.d.ts +32 -0
  73. package/dist/filed-answer.js +160 -0
  74. package/dist/files.d.ts +1 -11
  75. package/dist/files.js +2 -21
  76. package/dist/github-connection.d.ts +2 -2
  77. package/dist/github-connection.js +1 -1
  78. package/dist/graph-view.d.ts +3 -5
  79. package/dist/graph-view.js +4 -14
  80. package/dist/handed-values.d.ts +9 -0
  81. package/dist/handed-values.js +26 -0
  82. package/dist/harness-install.d.ts +22 -5
  83. package/dist/harness-install.js +98 -20
  84. package/dist/harness-opencode/adapter.d.ts +7 -0
  85. package/dist/harness-opencode/adapter.js +23 -6
  86. package/dist/harness-opencode/index.d.ts +1 -1
  87. package/dist/harness-opencode/index.js +1 -1
  88. package/dist/harness-opencode/verify.d.ts +17 -0
  89. package/dist/harness-opencode/verify.js +30 -10
  90. package/dist/harness-table.js +6 -1
  91. package/dist/harness.d.ts +4 -3
  92. package/dist/harness.js +16 -1
  93. package/dist/heard.js +1 -1
  94. package/dist/held-fd.d.ts +5 -0
  95. package/dist/held-fd.js +14 -0
  96. package/dist/host-calls.d.ts +20 -0
  97. package/dist/host-calls.js +12 -0
  98. package/dist/host-tools.d.ts +4 -21
  99. package/dist/host-tools.js +4 -61
  100. package/dist/host.d.ts +2 -4
  101. package/dist/host.js +2 -7
  102. package/dist/import-summary.d.ts +1 -5
  103. package/dist/import-summary.js +0 -21
  104. package/dist/import.d.ts +6 -6
  105. package/dist/import.js +62 -24
  106. package/dist/key-refusal.d.ts +5 -1
  107. package/dist/key-refusal.js +19 -4
  108. package/dist/learn/check.d.ts +6 -2
  109. package/dist/learn/check.js +83 -29
  110. package/dist/learn/folder.d.ts +5 -1
  111. package/dist/learn/folder.js +10 -3
  112. package/dist/learn/grade.d.ts +14 -0
  113. package/dist/learn/grade.js +68 -12
  114. package/dist/learn/index.d.ts +4 -4
  115. package/dist/learn/index.js +4 -4
  116. package/dist/learn/reflect.d.ts +31 -2
  117. package/dist/learn/reflect.js +79 -4
  118. package/dist/learn/relevance.d.ts +24 -0
  119. package/dist/learn/relevance.js +91 -0
  120. package/dist/learn/secrets.d.ts +8 -1
  121. package/dist/learn/secrets.js +95 -15
  122. package/dist/learn/skills.d.ts +112 -14
  123. package/dist/learn/skills.js +556 -52
  124. package/dist/learned-workflow.d.ts +37 -5
  125. package/dist/learned-workflow.js +106 -12
  126. package/dist/learning-routes.d.ts +16 -49
  127. package/dist/learning-routes.js +46 -36
  128. package/dist/learning-run.d.ts +6 -250
  129. package/dist/learning-run.js +11 -665
  130. package/dist/learning-words.d.ts +12 -5
  131. package/dist/learning-words.js +18 -9
  132. package/dist/learning.d.ts +93 -77
  133. package/dist/learning.js +176 -304
  134. package/dist/live/stream.js +1 -1
  135. package/dist/live/translate.js +13 -3
  136. package/dist/local-models.d.ts +11 -5
  137. package/dist/local-models.js +13 -8
  138. package/dist/mcp-gateway.d.ts +11 -52
  139. package/dist/mcp-gateway.js +44 -148
  140. package/dist/mcp-oauth.d.ts +10 -14
  141. package/dist/mcp-oauth.js +14 -32
  142. package/dist/mcp-probe.d.ts +0 -7
  143. package/dist/mcp-probe.js +4 -10
  144. package/dist/mcp-servers-store.d.ts +6 -6
  145. package/dist/mcp-servers-store.js +13 -15
  146. package/dist/mcp-store.js +3 -4
  147. package/dist/mcp.js +4 -3
  148. package/dist/model.d.ts +6 -7
  149. package/dist/model.js +43 -27
  150. package/dist/monid.d.ts +0 -2
  151. package/dist/monid.js +3 -6
  152. package/dist/openapi.d.ts +8 -1
  153. package/dist/openapi.js +124 -50
  154. package/dist/page.d.ts +1 -3
  155. package/dist/page.js +9 -19
  156. package/dist/paid-calls.d.ts +21 -27
  157. package/dist/paid-calls.js +28 -62
  158. package/dist/pause.d.ts +0 -2
  159. package/dist/pause.js +1 -8
  160. package/dist/place.d.ts +0 -5
  161. package/dist/place.js +0 -10
  162. package/dist/print.d.ts +2 -1
  163. package/dist/print.js +4 -2
  164. package/dist/propose.js +83 -39
  165. package/dist/push-handler.d.ts +1 -1
  166. package/dist/reflect-range.d.ts +25 -0
  167. package/dist/reflect-range.js +30 -0
  168. package/dist/refusal-words.d.ts +5 -1
  169. package/dist/refusal-words.js +5 -1
  170. package/dist/rows.d.ts +1 -1
  171. package/dist/run-cut.d.ts +0 -6
  172. package/dist/run-cut.js +0 -6
  173. package/dist/run-db.d.ts +2 -13
  174. package/dist/run-db.js +2 -41
  175. package/dist/run-launch.d.ts +19 -2
  176. package/dist/run-launch.js +13 -5
  177. package/dist/run-learning-served.d.ts +46 -2
  178. package/dist/run-learning-served.js +26 -7
  179. package/dist/run-reads-over-store.d.ts +1 -1
  180. package/dist/run-reads-over-store.js +15 -6
  181. package/dist/run-secrets.d.ts +33 -0
  182. package/dist/run-secrets.js +53 -0
  183. package/dist/run-timeline-served.d.ts +5 -1
  184. package/dist/run-timeline-served.js +19 -5
  185. package/dist/run-view.d.ts +12 -17
  186. package/dist/run-view.js +29 -28
  187. package/dist/runnable.d.ts +18 -10
  188. package/dist/runnable.js +21 -10
  189. package/dist/runner/adapter-loop.d.ts +14 -3
  190. package/dist/runner/adapter-loop.js +464 -62
  191. package/dist/runner/adapter-run-spec.d.ts +29 -8
  192. package/dist/runner/adapter-run-spec.js +96 -37
  193. package/dist/runner/adapters/index.js +11 -1
  194. package/dist/runner/adapters/pi.d.ts +15 -1
  195. package/dist/runner/adapters/pi.js +19 -5
  196. package/dist/runner/beside.d.ts +30 -3
  197. package/dist/runner/beside.js +54 -12
  198. package/dist/runner/claim-secrets.d.ts +10 -1
  199. package/dist/runner/claim-secrets.js +49 -15
  200. package/dist/runner/cli-run-spec.d.ts +2 -0
  201. package/dist/runner/cli-run-spec.js +60 -17
  202. package/dist/runner/commands.js +1 -1
  203. package/dist/runner/computer-journal.d.ts +15 -2
  204. package/dist/runner/computer-journal.js +41 -5
  205. package/dist/runner/conformance/harness.js +118 -8
  206. package/dist/runner/conformance/subject.d.ts +4 -1
  207. package/dist/runner/dispatch.d.ts +8 -1
  208. package/dist/runner/dispatch.js +81 -7
  209. package/dist/runner/end-of-run.d.ts +8 -2
  210. package/dist/runner/end-of-run.js +10 -4
  211. package/dist/runner/github-app.js +1 -1
  212. package/dist/runner/harness-loops.d.ts +9 -1
  213. package/dist/runner/harness-loops.js +15 -2
  214. package/dist/runner/journal-kinds.d.ts +30 -6
  215. package/dist/runner/journal-kinds.js +20 -5
  216. package/dist/runner/journal-reader.d.ts +1 -1
  217. package/dist/runner/journal-reader.js +2 -2
  218. package/dist/runner/learning-ask.d.ts +4 -0
  219. package/dist/runner/learning-ask.js +15 -1
  220. package/dist/runner/learning-end.d.ts +18 -3
  221. package/dist/runner/learning-end.js +113 -11
  222. package/dist/runner/learning-loop.d.ts +5 -0
  223. package/dist/runner/learning-loop.js +26 -6
  224. package/dist/runner/live-ticker.d.ts +1 -1
  225. package/dist/runner/live-ticker.js +28 -4
  226. package/dist/runner/loop-contract.d.ts +3 -2
  227. package/dist/runner/main.d.ts +64 -10
  228. package/dist/runner/main.js +165 -27
  229. package/dist/runner/mcp-claim.d.ts +1 -1
  230. package/dist/runner/mcp-claim.js +2 -3
  231. package/dist/runner/mcp-run.d.ts +2 -0
  232. package/dist/runner/mcp-run.js +15 -5
  233. package/dist/runner/memory-claim.d.ts +9 -0
  234. package/dist/runner/memory-claim.js +25 -0
  235. package/dist/runner/metrics.d.ts +33 -1
  236. package/dist/runner/metrics.js +85 -1
  237. package/dist/runner/model-route.d.ts +13 -0
  238. package/dist/runner/model-route.js +154 -11
  239. package/dist/runner/redact.d.ts +8 -2
  240. package/dist/runner/redact.js +11 -2
  241. package/dist/runner/reflect-hook.d.ts +12 -1
  242. package/dist/runner/reflect-hook.js +33 -3
  243. package/dist/runner/runner.d.ts +20 -0
  244. package/dist/runner/runner.js +56 -10
  245. package/dist/runner/store-port.d.ts +1 -1
  246. package/dist/runner/store-secrets.d.ts +3 -3
  247. package/dist/runner/store-secrets.js +8 -12
  248. package/dist/runner/text-tools.d.ts +44 -0
  249. package/dist/runner/text-tools.js +237 -0
  250. package/dist/runner/workflow-loop.d.ts +6 -0
  251. package/dist/runner/workflow-loop.js +6 -4
  252. package/dist/runner/workflow-spec.d.ts +18 -0
  253. package/dist/runner/workflow-spec.js +68 -26
  254. package/dist/runs-over-store.d.ts +8 -3
  255. package/dist/runs-over-store.js +76 -19
  256. package/dist/seal-key.d.ts +54 -15
  257. package/dist/seal-key.js +232 -82
  258. package/dist/sealed-settings-store.d.ts +19 -4
  259. package/dist/sealed-settings-store.js +9 -9
  260. package/dist/sealing.d.ts +4 -0
  261. package/dist/sealing.js +20 -0
  262. package/dist/secret-cut.d.ts +1 -1
  263. package/dist/secret-cut.js +1 -1
  264. package/dist/secret-name.d.ts +9 -0
  265. package/dist/secret-name.js +15 -0
  266. package/dist/secret-writes.d.ts +0 -1
  267. package/dist/secret-writes.js +0 -1
  268. package/dist/secrets.d.ts +2 -10
  269. package/dist/secrets.js +21 -17
  270. package/dist/server-fs.d.ts +4 -0
  271. package/dist/server-fs.js +8 -2
  272. package/dist/server-lock.d.ts +0 -24
  273. package/dist/server-lock.js +1 -80
  274. package/dist/server-secrets-store.d.ts +4 -4
  275. package/dist/server-secrets-store.js +10 -32
  276. package/dist/server-secrets.d.ts +4 -9
  277. package/dist/server-secrets.js +12 -33
  278. package/dist/server.d.ts +26 -44
  279. package/dist/server.js +229 -142
  280. package/dist/sign-ins-store.d.ts +34 -36
  281. package/dist/sign-ins-store.js +27 -38
  282. package/dist/sign-ins.d.ts +2 -119
  283. package/dist/sign-ins.js +5 -311
  284. package/dist/standard-webhooks.d.ts +1 -1
  285. package/dist/standard-webhooks.js +1 -1
  286. package/dist/stats.d.ts +3 -2
  287. package/dist/stats.js +1 -1
  288. package/dist/store/agent-body.d.ts +8 -2
  289. package/dist/store/agent-body.js +16 -4
  290. package/dist/store/index.d.ts +3 -3
  291. package/dist/store/index.js +3 -3
  292. package/dist/store/open.d.ts +18 -5
  293. package/dist/store/open.js +23 -3
  294. package/dist/store/postgres/connection.d.ts +18 -2
  295. package/dist/store/postgres/connection.js +156 -32
  296. package/dist/store/postgres/migrate.d.ts +4 -0
  297. package/dist/store/postgres/migrate.js +27 -3
  298. package/dist/store/postgres/migrations/0003_commands_one_answer.check.sql +19 -0
  299. package/dist/store/postgres/migrations/0003_commands_one_answer.sql +13 -0
  300. package/dist/store/postgres/store.d.ts +9 -2
  301. package/dist/store/postgres/store.js +9 -0
  302. package/dist/store/schema.js +1 -1
  303. package/dist/store/seal.js +2 -2
  304. package/dist/store/sqlite/connection.js +49 -5
  305. package/dist/store/sqlite/run-patch.d.ts +3 -0
  306. package/dist/store/sqlite/run-patch.js +3 -0
  307. package/dist/store/sqlite/store.js +64 -20
  308. package/dist/store/store.d.ts +38 -6
  309. package/dist/store/store.js +12 -2
  310. package/dist/store-reads/cap.d.ts +1 -1
  311. package/dist/store-reads/cap.js +22 -4
  312. package/dist/store-reads/journal.d.ts +25 -6
  313. package/dist/store-reads/journal.js +173 -21
  314. package/dist/store-reads/learning.d.ts +26 -8
  315. package/dist/store-reads/learning.js +12 -4
  316. package/dist/store-reads/workflow.d.ts +1 -1
  317. package/dist/store-reads/workflow.js +1 -1
  318. package/dist/store-view.d.ts +5 -1
  319. package/dist/store-view.js +91 -23
  320. package/dist/template-eval.d.ts +60 -4
  321. package/dist/template-eval.js +120 -54
  322. package/dist/templates.d.ts +24 -10
  323. package/dist/templates.js +69 -38
  324. package/dist/timeline/group.d.ts +33 -0
  325. package/dist/timeline/group.js +77 -0
  326. package/dist/timeline/index.d.ts +7 -0
  327. package/dist/timeline/index.js +7 -0
  328. package/dist/timeline/labels.d.ts +79 -0
  329. package/dist/timeline/labels.js +150 -0
  330. package/dist/timeline/origin.d.ts +56 -0
  331. package/dist/timeline/origin.js +92 -0
  332. package/dist/timeline/render-text.d.ts +11 -0
  333. package/dist/timeline/render-text.js +62 -0
  334. package/dist/timeline/timeline.d.ts +274 -0
  335. package/dist/timeline/timeline.js +1308 -0
  336. package/dist/timeline/util.d.ts +17 -0
  337. package/dist/timeline/util.js +49 -0
  338. package/dist/tool-gate.d.ts +41 -216
  339. package/dist/tool-gate.js +79 -540
  340. package/dist/tool-keys-store.d.ts +11 -12
  341. package/dist/tool-keys-store.js +24 -31
  342. package/dist/tool-keys.d.ts +9 -104
  343. package/dist/tool-keys.js +15 -271
  344. package/dist/tool-plane.d.ts +14 -42
  345. package/dist/tool-plane.js +23 -81
  346. package/dist/tool-settings-store.d.ts +1 -2
  347. package/dist/tool-settings-store.js +5 -8
  348. package/dist/tools.d.ts +31 -19
  349. package/dist/tools.js +101 -108
  350. package/dist/trigger-adapter.d.ts +2 -2
  351. package/dist/trigger-schedule.js +2 -1
  352. package/dist/trigger-secrets-shown.js +5 -1
  353. package/dist/trigger-targets.d.ts +13 -1
  354. package/dist/trigger-targets.js +31 -1
  355. package/dist/triggers.d.ts +2 -2
  356. package/dist/triggers.js +3 -3
  357. package/dist/turn-end.d.ts +1 -1
  358. package/dist/turn-end.js +1 -1
  359. package/dist/turns.d.ts +0 -6
  360. package/dist/turns.js +0 -19
  361. package/dist/uploads.js +1 -1
  362. package/dist/usage.d.ts +0 -2
  363. package/dist/usage.js +0 -2
  364. package/dist/validate.d.ts +2 -0
  365. package/dist/validate.js +23 -14
  366. package/dist/webhooks.d.ts +102 -76
  367. package/dist/webhooks.js +266 -233
  368. package/dist/workflow-run.d.ts +114 -0
  369. package/dist/workflow-run.js +144 -0
  370. package/dist/workspace/beneath.d.ts +19 -0
  371. package/dist/workspace/beneath.js +106 -0
  372. package/dist/workspace/computer-deps.d.ts +52 -5
  373. package/dist/workspace/computer-deps.js +154 -19
  374. package/dist/workspace/computer-for-claim.d.ts +19 -14
  375. package/dist/workspace/computer-for-claim.js +77 -16
  376. package/dist/workspace/computer-hold.d.ts +6 -1
  377. package/dist/workspace/computer-hold.js +6 -1
  378. package/dist/workspace/computer-janitor.d.ts +47 -0
  379. package/dist/workspace/computer-janitor.js +182 -0
  380. package/dist/workspace/computer-providers.d.ts +6 -2
  381. package/dist/workspace/computer-providers.js +1 -1
  382. package/dist/workspace/grep-worker.d.ts +5 -0
  383. package/dist/workspace/grep-worker.js +9 -6
  384. package/dist/workspace/index.d.ts +4 -3
  385. package/dist/workspace/index.js +4 -4
  386. package/dist/workspace/local.d.ts +11 -2
  387. package/dist/workspace/local.js +161 -43
  388. package/dist/workspace/resolve-computer.d.ts +50 -10
  389. package/dist/workspace/resolve-computer.js +46 -11
  390. package/dist/workspace/run-tools.d.ts +13 -31
  391. package/dist/workspace/run-tools.js +10 -27
  392. package/dist/workspace-paths.d.ts +0 -17
  393. package/dist/workspace-paths.js +1 -41
  394. package/dist/workspace.d.ts +0 -17
  395. package/dist/workspace.js +3 -99
  396. package/launcher/agentrun-serve +6 -33
  397. package/package.json +14 -14
  398. package/templates/README.md +9 -3
  399. package/templates/inbound-lead/agent.md +1 -1
  400. package/templates/kyb/agent.md +1 -1
  401. package/templates/market-monitor/agent.md +2 -2
  402. package/templates/market-monitor/skills/monitor/SKILL.md +6 -4
  403. package/dist/app.d.ts +0 -5
  404. package/dist/app.js +0 -514
  405. package/dist/bound-files.d.ts +0 -4
  406. package/dist/bound-files.js +0 -164
  407. package/dist/bounds.d.ts +0 -26
  408. package/dist/bounds.js +0 -137
  409. package/dist/coding-tools.d.ts +0 -47
  410. package/dist/coding-tools.js +0 -189
  411. package/dist/computer-adb.d.ts +0 -12
  412. package/dist/computer-adb.js +0 -14
  413. package/dist/console/assets/index-35-7aeIZ.js +0 -519
  414. package/dist/console/assets/index-Cejbov-Y.css +0 -1
  415. package/dist/content-filter.d.ts +0 -7
  416. package/dist/content-filter.js +0 -23
  417. package/dist/disk.d.ts +0 -33
  418. package/dist/disk.js +0 -85
  419. package/dist/door-line.d.ts +0 -23
  420. package/dist/door-line.js +0 -83
  421. package/dist/grade.d.ts +0 -63
  422. package/dist/grade.js +0 -150
  423. package/dist/harness-app.d.ts +0 -69
  424. package/dist/harness-app.js +0 -1162
  425. package/dist/mcp-servers.d.ts +0 -269
  426. package/dist/mcp-servers.js +0 -456
  427. package/dist/packs.d.ts +0 -71
  428. package/dist/packs.js +0 -180
  429. package/dist/request-start.d.ts +0 -27
  430. package/dist/request-start.js +0 -381
  431. package/dist/run-box.d.ts +0 -205
  432. package/dist/run-box.js +0 -661
  433. package/dist/run-holder.d.ts +0 -52
  434. package/dist/run-holder.js +0 -139
  435. package/dist/runs.d.ts +0 -1200
  436. package/dist/runs.js +0 -4103
  437. package/dist/sealed-settings.d.ts +0 -60
  438. package/dist/sealed-settings.js +0 -178
  439. package/dist/served-only.d.ts +0 -5
  440. package/dist/served-only.js +0 -12
  441. package/dist/sessions.d.ts +0 -6
  442. package/dist/sessions.js +0 -109
  443. package/dist/workspace/computer-serves.d.ts +0 -12
  444. package/dist/workspace/computer-serves.js +0 -15
@@ -1,72 +1,11 @@
1
- // A run's learning, a step the kernel calls. runs.ts owns a run's process, its door, its rows and its serve.json; it
2
- // builds one LearningRuns and calls it where a run's life meets learning:
3
- // onStart the run about to be made: its seed read first, then pinned in it once it is (learning.ts)
4
- // onSaved its serve.json written: the learning ledger's line, once its learning ended (usage.ts)
5
- // onLoad a run read at a start, before the kernel holds it: a seed copy a server went down during taken back
6
- // beforeFinish its job ended: the end-of-run turn asked first, and the run let go once the turn ends
7
- // onAdopt a running run read at a start: one a server left in its end-of-run turn finishes, its turn asked again
8
- // onLaunch its process launched: the saved keys it was handed, kept for the check of what its turn writes
9
- // beforeSettle its end being written: the turn's rows closed, and what the turn answered kept aside
10
- // afterSettle its end written: the notes the turn answered with checked (propose.ts) and kept as its proposal; the
11
- // runner's built-in learning run grades and merges it (runner/learning-loop.ts)
12
- // onReopen, afterReopen a follow-up: its copy laid again where it is gone, and a turn its process went in aborted
13
- // recover the server's start: what a server left between steps, picked up once
14
- // close, idle the server closing: nothing new started, then every check in flight waited for
15
- // It holds the runs' learning in flight; a run's learning itself is in its serve.json (`learning`, `reflect`), which
16
- // the kernel writes.
17
- import { join, posix } from "node:path";
18
- import { ANSWER_MAX, INDEX_FILE } from "./learn/index.js";
19
- import { runDirIn } from "@parcha/pi-durable-agent";
20
- import { CAP_REFUSAL_PREFIX, SPEND_CAP_REACHED } from "./cap.js";
21
- import { readJson, RUN_META, runDoorToken } from "./files.js";
22
- import { activeOf, currentLearning, detailShown, dropSeed, holdsForOk, keepTurn, KEPT_LEARNING, layLearned, layPinnedSkills, LEARNED_DIR, LEARNING_BUDGET, LEARNING_LABEL, LEARNING_REASONS, learningFolderOf, learningTestHooks, learningView, pinSeed, readPin, readTurn, secretsShown, seedAt, seedOf, shortLearning, skillsAt } from "./learning.js";
23
- import { statusesOf } from "./learning-routes.js";
24
- import { cutStrings, redacted } from "./key-refusal.js";
25
- import { servedOnly } from "./served-only.js";
26
- import { secret, SECRET_NAMES } from "./secrets.js";
27
- import { said, STOPPED } from "./learning-words.js";
28
- import { ended, lineNotesOf, notesLineOf, notesOf, outcomeOf, outcomes, record } from "./usage.js";
29
- import { isObject } from "./validate.js";
30
- import { REFLECT_REQUEST } from "./turns.js";
31
- import { isWorkspaceError, localWorkspace } from "./workspace/index.js";
32
- /** Who sent the end-of-run turn: the server, never a person, and no request field sets it. The one value it is sent
33
- * with, at its one call (reflectTurn); the tool gate reads it there and refuses that turn's writes under work/.learning/ (tool-gate.ts NOTE_ONLY_TEXT). */
34
- const REFLECT_ORIGIN = "reflect";
35
- /** The run asked its end-of-run turn and has not ended since. */
36
- export const reflecting = (run) => run.meta.reflect !== undefined && run.meta.reflect.until === undefined;
37
- /** The run ended after its end-of-run turn did, and what the turn wrote is neither checked nor dropped yet. */
38
- const awaitsPropose = (meta) => meta?.learning?.state === "reflecting" && meta.reflect?.ended === true && meta.reflect.until !== undefined;
39
- /** The end-of-run turn's request id: the door and pi take one id once, so a turn asked again after a restart is not
40
- * asked twice. */
41
- const reflectId = (runId) => `${REFLECT_REQUEST}${runId}`;
42
- /** How long a run's end-of-run turn may take before it is aborted and the run finishes without it. */
43
- const REFLECT_TIMEOUT_MS = 5 * 60_000;
44
- /** How often the run's door is asked whether its end-of-run turn has ended. */
45
- const REFLECT_POLL_MS = 250;
46
- /** A row's store entry, from its id (`<conversation>:<entry>[:...]`); NaN for a row no entry made (a live one, usage). */
47
- const entryOf = (id) => Number(id.split(":")[1]);
48
- /** Whether entry `id` (a number), or the row `id`, is the end-of-run turn's. Before the run has ended every later entry
49
- * is, and so is a live row. */
50
- export function reflects(reflect, id) {
51
- if (!reflect)
52
- return false;
53
- const entry = typeof id === "number" ? id : entryOf(id);
54
- if (Number.isInteger(entry))
55
- return entry > reflect.after && (reflect.until === undefined || entry <= reflect.until);
56
- return reflect.until === undefined && typeof id === "string" && id.includes(":live:");
57
- }
58
- /** `rows` with the end-of-run turn's labelled: never the run's answer (meaningful skips the label), its answer shown as
59
- * a text row, and its message as the host's, in a person's words (learning-words.ts): what the model is asked stays in
60
- * the run's store, and no row shows it. */
61
- export function labelled(reflect, rows) {
62
- if (!reflect)
63
- return rows;
64
- return rows.map((row) => (!reflects(reflect, row.id) ? row : {
65
- ...row, agent_label: LEARNING_LABEL,
66
- ...(row.kind === "final_answer" ? { kind: "text_block" } : {}),
67
- ...(row.kind === "user_message" ? { summary: said.asked, text: said.asked } : {}),
68
- }));
69
- }
1
+ // A run's learning, as the server reads it from a run's rows and the runs' rows: the notes it read (notesRead), its
2
+ // learning row once its learning ended (withLearning), how the runs that tried to learn fared (learningStats), and an
3
+ // agent's impact (noteRuns, learningImpact). Which rows its end-of-run turn made is reflect-range.ts's (reflects,
4
+ // labelled). The runner runs the end-of-run turn itself (runner/reflect-hook.ts).
5
+ import { posix } from "node:path";
6
+ import { INDEX_FILE } from "./learn/index.js";
7
+ import { detailShown, LEARNED_DIR, LEARNING_LABEL, LEARNING_REASONS } from "./learning.js";
8
+ import { reflects } from "./reflect-range.js";
70
9
  /** A path that names a file at the top of a run's copy of the folder: `.learning/<file>` relative to its work, as a
71
10
  * harness's file tools take it, or the same at the end of an absolute path (claude-agent's CLI joins its working
72
11
  * directory; a box names its own). */
@@ -101,9 +40,9 @@ export function withLearning(rows, learning, shown) {
101
40
  tool_name: null, tool_use_id: null, tool_status: null, tool_input_preview: null, tool_result_preview: null, tool_result_size_bytes: null, text: detail,
102
41
  learning: { state: learning.state, reason: learning.reason, detail },
103
42
  };
104
- // Only the served view makes it (servedOnly): learning that starts over (seeded, reflecting) drops it, even while a
105
- // live process feeds the timeline.
106
- return [...rows, servedOnly(row)];
43
+ // No store entry makes it: the served view adds it at each read, and learning that starts over (seeded, reflecting)
44
+ // has none.
45
+ return [...rows, row];
107
46
  }
108
47
  /** How the runs that tried to learn fared, over the runs the analytics' `from`, `to` and `agent` pick (by when they
109
48
  * started, unarchived): `runs` with a learning block, `merged`, `dropped` by reason, and `in_progress` (seeded,
@@ -131,18 +70,6 @@ export function learningStats(rows, filter, learningOf) {
131
70
  }
132
71
  return out;
133
72
  }
134
- /** What run `run`'s end-of-run turn answered, and what the user said in the run, as beforeSettle keeps them: the whole
135
- * text of each of the turn's assistant messages, in order (its notes may come in more than one), cut once past
136
- * ANSWER_MAX, which the parser refuses; and each message the person sent the run before the turn, whole, in order. */
137
- function turnOf(run) {
138
- const answer = [...(run.turnText ?? new Map())].sort(([a], [b]) => a - b).map(([, text]) => text).filter(Boolean).join("\n\n");
139
- // From the run's store, whole (heard.ts): a timeline row cuts a message at 2,000 characters. A trigger's input that
140
- // its owner did not write is never what the user said: it goes apart, by its source.
141
- const heard = [...(run.hearing?.heard ?? new Map())].filter(([id]) => !reflects(run.meta.reflect, id)).sort(([a], [b]) => a - b).map(([, said]) => said);
142
- const asked = heard.filter((said) => said.source === undefined).map(({ text }) => text);
143
- const triggered = heard.flatMap(({ text, source }) => (source === undefined ? [] : [{ source, text }]));
144
- return { answer: answer.length > ANSWER_MAX ? answer.slice(0, ANSWER_MAX + 1) : answer, asked, ...(triggered.length ? { triggered } : {}) };
145
- }
146
73
  /** Each note's count of runs that were given it and that read it, by its file, over runs' note files (usage.ts
147
74
  * notesOf, lineNotesOf): a held run's from its learning, an ended one's from its ledger line. A run counts a note
148
75
  * once each way. */
@@ -214,584 +141,3 @@ export function learningImpact(rows, learningOf) {
214
141
  const [before, later] = [counted(done.filter((row) => Date.parse(row.startedAt) < first)), counted(after)];
215
142
  return before.length >= 2 && later.length >= 2 ? { before: sideOf(before), after: sideOf(later) } : null;
216
143
  }
217
- const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
218
- /** The files a run was given and read (usage.ts notesOf, lineNotesOf), as one key. */
219
- const filesKey = (files) => JSON.stringify(files);
220
- const warn = (runId, text) => void process.stderr.write(`agentrun-serve: run ${runId}: ${text}\n`);
221
- /** A row of the run's that a person sees. Its rows never hold the reducer's quiet ones (runs.ts take). */
222
- const shows = (row) => row.kind !== "usage" && row.kind !== "phase";
223
- /** `work` for `key`, one at a time: the one in flight, else a new one, forgotten once it settles. */
224
- function once(running, key, work) {
225
- let current = running.get(key);
226
- if (!current)
227
- running.set(key, (current = work().finally(() => running.delete(key))));
228
- return current;
229
- }
230
- /** The learning of every run the kernel holds, from the end-of-run turn to the merge. */
231
- export class LearningRuns {
232
- options;
233
- kernel;
234
- /** The secrets each launch of a run that learns handed its process (runs.ts launch()), by run id, so a key a person
235
- * replaced or deleted while the run lived is still refused in what its end-of-run turn wrote. In memory only, never on
236
- * disk: a server that restarts loses them, and the run's next launch hands it the keys as they are then, which the
237
- * check reads too. Dropped once the run's learning ends, and when the server closes. */
238
- handed = new Map();
239
- /** Aborted as the server closes: no check starts after it. */
240
- stopping = new AbortController();
241
- /** The runs whose learning's end is in the learning ledger (usage.ts), each with the files its lines say it was given
242
- * and read (filesKey): read at the start, and each one written since. */
243
- recorded = new Map();
244
- /** By run: what its end-of-run turn wrote, being checked (proposeOnce). */
245
- proposing = new Map();
246
- /** The host's secrets as shown() last cut them, by their values, and the saved keys it read. */
247
- cut;
248
- saved;
249
- constructor(options, kernel) {
250
- this.options = options;
251
- this.kernel = kernel;
252
- }
253
- get closing() {
254
- return this.stopping.signal.aborted;
255
- }
256
- // ---- the kernel's calls ---------------------------------------------------------------------------------------------
257
- /** A run of `revision` about to be made for agent `agentId`. It learns when the server's switch (`--learning`) is on and
258
- * its agent's definition says `learning: true`; then its seed is read now, before the run is made, so the folder is
259
- * read once whatever the run's setup does, and what comes back pins it in the run once it is made and its input
260
- * saved (a server that goes down during the copy leaves no learning saved: onLoad), at `at`: its
261
- * learning, `seeded`, or `dropped` as `seed_failed` (a learning fault never refuses a run). Undefined when it does
262
- * not learn. What the run reads under `# Learned` is its notes within the server's learning budget, told of
263
- * `.learning/` only when its agent has the read tool, and cut of the host's secrets as they are then. */
264
- async onStart(agentId, revision) {
265
- const learns = this.options.learning !== "off" && revision.kind === "agentic" && revision.body.learning === true;
266
- if (!learns)
267
- return undefined;
268
- const read = revision.kind === "agentic" && revision.body.tools.includes("read");
269
- const seeded = await this.seedFor(agentId);
270
- return async (started, at) => {
271
- let failed = "failed" in seeded ? seeded.failed : undefined;
272
- // The notes its # Learned section names (learning.ts givenOf, as its pin keeps them): what it started with.
273
- let given = [];
274
- if ("seed" in seeded) {
275
- try {
276
- const own = revision.kind === "agentic" ? Object.keys(revision.body.skills) : [];
277
- const pin = await pinSeed(started.dir, localWorkspace(started.workspace), seeded.seed, { budget: this.options.learningBudget ?? LEARNING_BUDGET, read, shown: this.shown(), own });
278
- given = pin.given ?? [];
279
- for (const path of seeded.seed.skipped)
280
- warn(started.runId, `its learning folder's ${path} was not read`);
281
- for (const line of seeded.warned)
282
- warn(started.runId, line);
283
- for (const name of pin.skills_left_out ?? [])
284
- warn(started.runId, `starts without the skill ${name} its agent learned: it is over what is left of the learned skills' budget, or holds a secret`);
285
- for (const name of pin.workflows_left_out ?? [])
286
- warn(started.runId, `starts without the workflow ${name} its agent learned: its line is over what is left of the learned_workflow tool's description budget, or it holds a secret`);
287
- }
288
- catch (error) {
289
- warn(started.runId, `starts with nothing learned: ${error.message}`);
290
- failed = said.stopped(STOPPED.notCopied);
291
- }
292
- }
293
- await learningTestHooks.afterSeed?.(started.dir);
294
- const notes = "seed" in seeded ? Object.keys(seeded.seed.notes).length : 0;
295
- return failed === undefined ? { state: "seeded", reason: null, detail: said.seeded(notes), at, seed_notes: notes, ...(given.length ? { given } : {}) } : { state: "dropped", reason: "seed_failed", detail: failed, at };
296
- };
297
- }
298
- /** A start whose seed copy threw out of its pin (runs.ts start): its learning, in its own words, as a copy that failed
299
- * says it; the copy taken back. */
300
- async copyThrew(started, at, error) {
301
- warn(started.runId, `starts with nothing learned: ${error instanceof Error ? error.message : String(error)}`);
302
- await dropSeed(started.dir, localWorkspace(started.workspace)).catch(() => undefined);
303
- return { state: "dropped", reason: "seed_failed", detail: said.stopped(STOPPED.notCopied), at };
304
- }
305
- /** The run's serve.json holds `learning` now: the learning ledger's line for the run (usage.ts), once, when that
306
- * learning has ended; after it, a `notes` line each time the notes it was given or read are no longer what the
307
- * ledger says (a follow-up read another note), which counts neither the run nor its costs again. Never rejects: a
308
- * line not written is written by the next save, or by the next start's pass. */
309
- onSaved(run, learning) {
310
- if (ended(learning))
311
- this.handed.delete(run.id);
312
- if (!ended(learning))
313
- return Promise.resolve();
314
- const was = this.recorded.get(run.id);
315
- const files = filesKey(notesOf(learning));
316
- if (was === files)
317
- return Promise.resolve();
318
- this.recorded.set(run.id, files);
319
- return record(this.options.agents.dir, was === undefined ? outcomeOf(run.id, run.agentId, learning) : notesLineOf(run.id, run.agentId, learning)).catch((error) => {
320
- if (was === undefined)
321
- this.recorded.delete(run.id);
322
- else
323
- this.recorded.set(run.id, was);
324
- warn(run.id, `${was === undefined ? "its learning's end is" : "the notes it was given and read are"} not in the ledger yet: ${error.message}`);
325
- });
326
- }
327
- /** The run's job ended with `end`. True when its end-of-run turn is asked first: the run is finishing with that
328
- * outcome, and is let go once the turn ends. False when it is let go now, its learning dropped when it skips the turn. */
329
- beforeFinish(run, end) {
330
- const skip = this.reflectSkip(run, end);
331
- if (skip === undefined) {
332
- this.reflect(run, end.outcome);
333
- return true;
334
- }
335
- if (skip)
336
- this.dropLearning(run, ...skip);
337
- return false;
338
- }
339
- /** A run read at a start, before the kernel holds it, `save` writing its serve.json: one a server went down while it
340
- * copied the run's seed (running, its input saved and no learning, and a pin or a `work/.learning/` there) has both
341
- * taken back, so it starts again with nothing learned, and its learning dropped as `seed_failed`, saved before the
342
- * ledger's line (onSaved). Called before anything reads the run: its relaunch, a settle, a follow-up. A seed_failed
343
- * run asks no end-of-run turn (reflectSkip). A copy that never began leaves nothing, and the learning stays null. */
344
- async onLoad(row, dir, meta, save) {
345
- if (row.state !== "running" || meta.learning || !(await dropSeed(dir, localWorkspace(join(dir, "work")))))
346
- return;
347
- meta.learning = { state: "dropped", reason: "seed_failed", detail: said.stopped(STOPPED.serverStopped), at: new Date().toISOString() };
348
- await save();
349
- warn(row.id, "its server stopped while it copied what its agent learned; it starts again with nothing learned");
350
- await this.onSaved({ id: row.id, agentId: row.agent }, meta.learning);
351
- }
352
- /** A run's process launched, handed the secrets named `names`: what each held then, as handover read it, kept beside
353
- * the run while its learning is under way (a key replaced since stays refused in its notes). */
354
- onLaunch(run, names, also = []) {
355
- if (!run.meta.learning || ended(run.meta.learning))
356
- return;
357
- const values = [...names.flatMap((name) => secret(name, this.options.storedKeys) ?? []), ...also];
358
- if (!values.length)
359
- return;
360
- const kept = this.handed.get(run.id) ?? new Set();
361
- for (const value of values)
362
- kept.add(value);
363
- this.handed.set(run.id, kept);
364
- }
365
- /** A running run read at a start, `live` when its process serves it still. One a server left while its end-of-run turn
366
- * ran is finishing with its job's outcome, and nothing opens it again; when its process lives, the turn is asked again
367
- * under the same request id, which it takes once, and waited for. */
368
- onAdopt(run, live) {
369
- // A run its server hid (its browser sign-in unreadable, runs.ts) ends failed, saying why: its turn is never asked
370
- // again, and its job's outcome never stands in for that end.
371
- if (!reflecting(run) || run.meta.unshown)
372
- return;
373
- run.finishing = run.meta.reflect.outcome;
374
- if (run.meta.error)
375
- run.error = { ...run.meta.error, retryable: false };
376
- if (live)
377
- void this.reflectTurn(run);
378
- }
379
- /** The run's end being written: its end-of-run turn's rows closed, and its learning settled from how the turn went;
380
- * then what the turn answered, and what the user said in the run, kept in its directory before the run reads ended,
381
- * so a follow-up that opens it again never changes what is checked. */
382
- async beforeSettle(run, outcome) {
383
- this.closeLearning(run, outcome);
384
- const keeping = awaitsPropose(run.meta);
385
- if (keeping) {
386
- await keepTurn(run.dir, turnOf(run)).catch((error) => {
387
- warn(run.id, `what its end-of-run turn answered could not be kept: ${error.message}`);
388
- this.setLearning(run, "dropped", "propose_failed", said.stopped(STOPPED.notSetAside));
389
- });
390
- }
391
- // Kept now, or never to be: the person's words, whole, are let go (a finished run is held for the server's life).
392
- if (keeping || (run.meta.learning?.state !== "seeded" && run.meta.learning?.state !== "reflecting"))
393
- run.hearing = undefined;
394
- await this.noteRead(run);
395
- }
396
- /** The run's end written, as it would be without learning: what its end-of-run turn wrote checked, and kept as its
397
- * proposal or dropped. Called only then, so a slow or failing check never holds the job. A proposal is graded and
398
- * merged by the runner's built-in learning run (runner/learning-loop.ts), never here. */
399
- afterSettle(run) {
400
- return this.proposeOnce(run);
401
- }
402
- /** A run that learns, opened again for a follow-up, before its process starts: `work/.learning/` laid again from its
403
- * seed where the run took it away, so the follow-up finds the notes its instructions name where they say, and the
404
- * learned skills its pin names laid again, fresh, so its process reads each where it was. A fault leaves it without. */
405
- async onReopen(run) {
406
- await learningTestHooks.beforeReopen?.(run.dir);
407
- const work = localWorkspace(join(run.dir, "work"));
408
- if (!run.meta.reflect)
409
- return;
410
- try {
411
- const pin = await readPin(run.dir);
412
- const copied = await work.list(LEARNED_DIR).then(() => true, (error) => !isWorkspaceError(error, "NOT_FOUND"));
413
- if (pin && !copied)
414
- await layLearned(work, await seedAt(this.options.agents.dir, run.agentId, pin.seed), this.shown());
415
- if (pin)
416
- await layPinnedSkills(work, pin, () => skillsAt(this.options.agents.dir, run.agentId, pin.seed));
417
- }
418
- catch (error) {
419
- warn(run.id, `opens again without its copy of what its agent learned: ${error.message}`);
420
- }
421
- }
422
- /** Its process launched again for the follow-up: an end-of-run turn its process went in would resume with the run, so
423
- * it is aborted before the follow-up is sent. One that cannot be aborted runs out ahead of it, and its rows are never
424
- * the follow-up's answer, which comes after them. */
425
- async afterReopen(run) {
426
- if (run.meta.reflect && run.meta.learning?.reason === "crashed")
427
- await this.kernel.toDoor(run, "/abort", {}).catch(() => undefined);
428
- }
429
- /** The server's start: the learning ledger read from now, beside the running runs' pass, never before it (a line
430
- * written twice meanwhile is read once). What comes back takes every run once that pass is done: each ended run whose
431
- * learning a server left between steps, what its end-of-run turn wrote not checked yet, is checked now, once; a
432
- * learning end saved and not in the ledger (a server that went between the two) is written there. */
433
- recover() {
434
- const inLedger = outcomes(this.options.agents.dir).catch(() => new Map());
435
- return async (rows) => {
436
- for (const [run, line] of await inLedger)
437
- this.recorded.set(run, filesKey(lineNotesOf(line)));
438
- for (const row of rows) {
439
- if (this.closing)
440
- return;
441
- if (row.state === "running" || row.importedFrom || row.kind !== "agentic")
442
- continue;
443
- const meta = (await readJson(join(runDirIn(this.options.agents.dir, row.agent, row.id), RUN_META)));
444
- const learning = isObject(meta?.learning) ? meta.learning : undefined;
445
- await this.onSaved({ id: row.id, agentId: row.agent }, learning);
446
- if (!awaitsPropose(meta))
447
- continue;
448
- const run = await this.kernel.get(row.id).catch((error) => (warn(row.id, `not read: ${error.message}; the next start tries again`), null));
449
- if (run && awaitsPropose(run.meta))
450
- await this.proposeOnce(run);
451
- }
452
- };
453
- }
454
- /** The server closing: nothing new starts. */
455
- close() {
456
- this.stopping.abort();
457
- this.handed.clear();
458
- }
459
- /** Once every run has settled: each check in flight ended. */
460
- async idle() {
461
- await Promise.all(this.proposing.values());
462
- }
463
- /** A held run's learning, in short, from memory (null when it does not learn); undefined for a run the kernel does not
464
- * hold. For a count, which reads the rest from the learning ledger and never a run's files. */
465
- heldLearning(runId) {
466
- const held = this.kernel.held(runId);
467
- if (!held)
468
- return undefined;
469
- const learning = held.meta.learning;
470
- return learning ? { state: learning.state, reason: learning.reason } : null;
471
- }
472
- /** The secrets this server never shows as they are now (serverSecrets), as a reader cuts them (secretsShown), as the
473
- * learning routes cut them. Built again only once they change. Every run's view and every row of a list asks it, so
474
- * the saved keys, which are read from their file and opened one by one, are read once and kept until a person saves
475
- * or deletes one (keysChanged); the rest is read each time. */
476
- shown() {
477
- const saved = (this.saved ??= new Map(SECRET_NAMES.map((name) => [name, this.options.storedKeys?.(name)])));
478
- const values = this.options.serverSecrets((name) => saved.get(name));
479
- const key = values.join("\0");
480
- if (this.cut?.key !== key)
481
- this.cut = { key, shown: secretsShown(values) };
482
- return this.cut.shown;
483
- }
484
- /** A key was saved on this server or deleted from it: the next view reads them again. */
485
- keysChanged() {
486
- this.saved = undefined;
487
- }
488
- /** Agent `agentId`'s run's `learning` as it reads now (learning.ts currentLearning): what it held for its owner's OK
489
- * checked against the folder's main (learning-routes.ts statusesOf, one read per commit of main). Its record stays as
490
- * the merge wrote it; one that held nothing is read as it is, with no read of the folder. */
491
- async current(agentId, learning) {
492
- if (!holdsForOk(learning))
493
- return learning;
494
- return currentLearning(learning, await statusesOf(this.options.agents.dir, agentId));
495
- }
496
- /** `rows` with their learning row as it reads now (current): its words from what it held, checked against main. */
497
- async rowsNow(agentId, learning, rows) {
498
- const now = await this.current(agentId, learning);
499
- if (now === learning || !now)
500
- return [...rows];
501
- const [row] = withLearning([], now, this.shown());
502
- return rows.map((one) => (one.kind === "learning" && row ? { ...one, summary: row.summary, text: row.text, learning: row.learning } : one));
503
- }
504
- /** A run's learning as its view shows it (learning.ts learningView), cut of the host's secrets now; `settled`, once
505
- * the run has ended and settled, shows the notes it read. */
506
- view(learning, settled) {
507
- return learning ? learningView(learning, this.shown(), settled) : null;
508
- }
509
- /** A run's learning as a list shows it (learning.ts shortLearning): from serve.json for a run the kernel does not hold. */
510
- short(learning, settled) {
511
- return shortLearning(learning, this.shown(), settled);
512
- }
513
- /** How the runs `rows` holds that tried to learn fared, over `filter` (learningStats): a held run's learning from
514
- * memory, any other's end from the learning ledger, never a run's files. */
515
- async counts(rows, filter) {
516
- const ledger = await outcomes(this.options.agents.dir);
517
- return learningStats(rows, filter, (row) => this.heldLearning(row.id) ?? ledger.get(row.id) ?? null);
518
- }
519
- /** Agent `agentId`'s runs (`rows`, its own) as its learning view counts them, the ledger read once: how they fared
520
- * (counts, less `runs`), and what learning changed (learningImpact), each run's learning from memory when the kernel
521
- * holds it, else its end from its ledger line; never a run's files. */
522
- async ofAgent(rows, agentId) {
523
- const ledger = await outcomes(this.options.agents.dir);
524
- const learningOf = (row) => {
525
- const held = this.kernel.held(row.id);
526
- if (!held) {
527
- const line = ledger.get(row.id);
528
- return line ? { ...line, grade_usd: line.cost } : null;
529
- }
530
- const learning = held.meta.learning;
531
- if (!learning)
532
- return null;
533
- // As its ledger line will say it (usage.ts outcomeOf): its grade's cost and its look-back's once it ended.
534
- const over = ended(learning);
535
- const { look_back_usd, ...rest } = learning;
536
- return { ...rest, ...(over ? { grade_usd: learning.cost_usd ? learning.cost_usd.grade : 0 } : {}), ...(learning.answered_at !== undefined ? { look_back_usd } : over ? { look_back_usd: 0 } : {}) };
537
- };
538
- const { runs: _, ...counts } = learningStats(rows, { agent: agentId }, learningOf);
539
- // Each note's runs: given it and read it, a held run's from memory, an ended one's from its ledger line.
540
- const notes = noteRuns(rows.map((row) => {
541
- const held = this.kernel.held(row.id);
542
- if (held)
543
- return held.meta.learning ? notesOf(held.meta.learning) : null;
544
- const line = ledger.get(row.id);
545
- return line ? lineNotesOf(line) : null;
546
- }));
547
- return { counts, impact: learningImpact(rows, learningOf), notes };
548
- }
549
- // ---- the run's learning ---------------------------------------------------------------------------------------------
550
- /** What a run of `agentId` starts from: its learning folder's main (learning.ts seedOf, through the folder's script
551
- * and file API), the folder made first when it is absent, with the lines its read said (a skill it left out); else
552
- * why it starts with nothing learned. A learning fault never refuses a run: a folder that cannot be made or read is
553
- * said once per run. */
554
- async seedFor(agentId) {
555
- const warned = [];
556
- return seedOf(this.options.agents.dir, agentId, (line) => warned.push(line)).then((seed) => ({ seed, warned }), (error) => {
557
- process.stderr.write(`agentrun-serve: the agent ${agentId}'s learning folder could not be read, so its run starts with nothing learned: ${error.message}\n`);
558
- return { failed: said.stopped(STOPPED.unreadable) };
559
- });
560
- }
561
- /** Moves the run's learning on, and shows its row. What it keeps whatever it does after (learning.ts KEPT_LEARNING:
562
- * the notes the run read, how many it started from, its answer's time and costs) stays with it. Saved by the caller,
563
- * or with the run's end. */
564
- setLearning(run, state, reason = null, detail = null, more = {}) {
565
- const before = run.meta.learning;
566
- const kept = Object.fromEntries(KEPT_LEARNING.flatMap((key) => (before?.[key] === undefined ? [] : [[key, before[key]]])));
567
- run.meta.learning = { state, reason, detail, at: new Date().toISOString(), ...kept, ...more };
568
- run.updatedAt = run.meta.learning.at;
569
- this.kernel.show(run);
570
- }
571
- /** As the run settles, before its end is written: the notes its job read from its copy of the folder (notesRead), by
572
- * their names in its seed, kept with its learning (`read`), so a view or a list reads them from serve.json and never
573
- * from its rows again. Only a note the run started from counts: MEMORY.md, a file the run wrote there, and a note
574
- * not in its seed are not notes it read. Read again at each settle, a follow-up's reads with the rest. A seed that
575
- * cannot be read leaves what was there, and says why. */
576
- async noteRead(run) {
577
- if (!run.meta.learning)
578
- return;
579
- const files = notesRead(run.meta.reflect, run.rows);
580
- let read = [];
581
- if (files.length) {
582
- try {
583
- const pin = await readPin(run.dir);
584
- const names = new Map(pin ? activeOf(await seedAt(this.options.agents.dir, run.agentId, pin.seed)).map((note) => [note.file, note.name]) : []);
585
- read = files.flatMap((file) => (names.has(file) ? [{ file, name: names.get(file) }] : []));
586
- }
587
- catch (error) {
588
- return warn(run.id, `which notes it read is not known: ${error.message}`);
589
- }
590
- }
591
- // As the learning is now: nothing else changed it meanwhile, but its object may be a new one.
592
- const learning = run.meta.learning;
593
- if (!learning)
594
- return;
595
- const { read: _, ...rest } = learning;
596
- run.meta.learning = read.length ? { ...rest, read } : rest;
597
- }
598
- dropLearning(run, reason, detail) {
599
- this.setLearning(run, "dropped", reason, detail);
600
- void this.kernel.save(run).catch((error) => warn(run.id, `its learning's drop is not saved: ${error.message}`));
601
- }
602
- /** Why a run that just ended asks no end-of-run turn: a drop's reason and detail; null when it does not learn, or asked
603
- * one already (one per run, ever); undefined when it asks one. The cap keeps no reserve: a run its cap stopped has no
604
- * room left, and one whose spend is unknown cannot be kept within its cap. */
605
- reflectSkip(run, end) {
606
- if (run.meta.learning?.state !== "seeded" || run.meta.reflect)
607
- return null;
608
- if (run.cancel)
609
- return ["cancelled", said.stopped(STOPPED.cancelled)];
610
- if (end.abort || end.error?.code === SPEND_CAP_REACHED)
611
- return ["no_budget", said.noBudget];
612
- if (run.meta.max_cost_usd !== undefined && this.kernel.costOf(run.rows) === null)
613
- return ["unknown_cost", said.unknownCost];
614
- return undefined;
615
- }
616
- /** The run answered or failed: it is finishing with that outcome, and its harness is asked one end-of-run turn first.
617
- * The turn is saved before it is asked, so a restart asks it again under the same id, never twice. */
618
- reflect(run, outcome) {
619
- run.finishing = outcome;
620
- run.endedAt = new Date().toISOString();
621
- const after = run.lastEntry ?? Math.max(0, ...run.rows.map((row) => entryOf(row.id)).filter(Number.isInteger));
622
- run.meta.reflect = { outcome, after, deadline: new Date(Date.now() + (this.options.reflectTimeoutMs ?? REFLECT_TIMEOUT_MS)).toISOString() };
623
- // The job's error, kept for a restart: the turn's is never the run's.
624
- if (run.error)
625
- run.meta.error = { code: run.error.code, message: redacted(run.error.message, this.refused(run)), ...(run.error.details ? { details: cutStrings(run.error.details, (text) => redacted(text, this.refused(run))) } : {}) };
626
- // Its answer is final now: when, and what its job cost, before the end-of-run turn spends any more.
627
- this.setLearning(run, "reflecting", null, said.reflecting, { answered_at: run.endedAt, job_cost_usd: this.kernel.costOf(run.rows) });
628
- this.kernel.mark(run);
629
- void this.reflectTurn(run);
630
- }
631
- /** Asks the end-of-run turn through the run's door and waits for its end, its deadline (the turn is aborted) or the
632
- * process to go; then lets the run go. The turn answers with its notes: it writes nothing, so nothing the run left in
633
- * its copy of the folder counts. */
634
- async reflectTurn(run) {
635
- const reflect = run.meta.reflect;
636
- let end = "gone";
637
- try {
638
- const pin = await readPin(run.dir);
639
- if (!pin)
640
- throw new Error("the run has no learning pin");
641
- const seed = await seedAt(this.options.agents.dir, run.agentId, pin.seed);
642
- await this.kernel.save(run);
643
- const { reflectPrompt } = await import("./learn/index.js");
644
- const { tools } = await this.draftTools(run);
645
- // The one call that sends the end-of-run turn, with its origin. Only while the run is finishing with no outcome yet,
646
- // read in the same tick the origin's write starts in (toLiveDoor writes it before its first await): a message
647
- // waits while the run is finishing (runs.ts message), so a write starts only before that wait ends. One whose
648
- // process went while this turn was made ready has its outcome already: it is asked nothing.
649
- if (!run.finishing || run.outcome)
650
- return;
651
- await this.kernel.toLiveDoor(run, "/submit", { requestId: reflectId(run.id), content: reflectPrompt({ dir: LEARNED_DIR, active: Object.keys(seed.notes).length, tools }), whenBusy: "followUp" }, REFLECT_ORIGIN);
652
- end = await this.reflectEnd(run, Date.parse(reflect.deadline));
653
- }
654
- catch (error) {
655
- if (!this.kernel.live(run) || run.cancel)
656
- return;
657
- warn(run.id, `its end-of-run turn was not asked: ${error.message}`);
658
- this.dropLearning(run, "reflect_failed", said.stopped(STOPPED.notAsked));
659
- return this.kernel.terminate(run);
660
- }
661
- if (end === "gone")
662
- return;
663
- if (end === "ended")
664
- reflect.ended = true;
665
- else {
666
- reflect.timedOut = true;
667
- await this.kernel.abortTurn(run);
668
- }
669
- await this.kernel.save(run).catch((error) => warn(run.id, `its end-of-run turn's end is not saved: ${error.message}`));
670
- this.kernel.terminate(run);
671
- }
672
- /** How the end-of-run turn ends: `ended` once the run's door says it is idle (the turn was taken before the submit was
673
- * answered, so idle is after it), `timeout` at `deadline`, `gone` when the process went or the run was cancelled. */
674
- async reflectEnd(run, deadline) {
675
- for (;;) {
676
- const status = await this.kernel.toLiveDoor(run, "/status").catch(() => null);
677
- if (!this.kernel.live(run) || run.cancel)
678
- return "gone";
679
- if (isObject(status?.busy) && status.busy.kind === "idle")
680
- return "ended";
681
- if (Date.now() >= deadline)
682
- return "timeout";
683
- await sleep(REFLECT_POLL_MS);
684
- }
685
- }
686
- /** At the run's end: the end-of-run turn's rows closed, and its learning settled from how the turn went. A run that
687
- * never left `seeded` went before it could reflect. */
688
- closeLearning(run, outcome) {
689
- const { learning, reflect } = run.meta;
690
- if (learning?.state === "seeded")
691
- return this.setLearning(run, "dropped", ...(run.cancel || outcome === "cancelled" ? ["cancelled", said.stopped(STOPPED.cancelled)] : ["crashed", said.stopped(STOPPED.crashed)]));
692
- if (!reflect || reflect.until !== undefined)
693
- return;
694
- reflect.until = run.lastEntry ?? reflect.after;
695
- // The run ends now, when its end-of-run turn closed: `answered_at` keeps when its job answered, and the end the run's
696
- // settle writes once (runs.ts carryBack) is this one, not the answer's.
697
- run.endedAt = new Date().toISOString();
698
- // What the turn cost: the run's cost now, its job's set apart; unknown when either is.
699
- if (learning) {
700
- const total = this.kernel.costOf(run.rows);
701
- const job = learning.job_cost_usd;
702
- run.meta.learning = { ...learning, look_back_usd: total === null || job === null || job === undefined ? null : Math.max(0, Math.round((total - job) * 1e9) / 1e9) };
703
- }
704
- if (learning?.state !== "reflecting")
705
- return;
706
- const last = run.rows.findLast((row) => row.agent_label === LEARNING_LABEL && shows(row));
707
- if (run.cancel)
708
- this.setLearning(run, "dropped", "cancelled", said.stopped(STOPPED.cancelledLooking));
709
- else if (reflect.timedOut)
710
- this.setLearning(run, "dropped", "reflect_timeout", said.stopped(STOPPED.timedOut));
711
- else if (last?.kind === "error" && CAP_REFUSAL_PREFIX.test(last.text ?? ""))
712
- this.setLearning(run, "dropped", "no_budget", said.noBudget);
713
- else if (last?.kind === "error")
714
- this.setLearning(run, "dropped", "reflect_failed", said.stopped(STOPPED.failed));
715
- else if (!reflect.ended)
716
- this.setLearning(run, "dropped", "crashed", said.stopped(STOPPED.crashedLooking));
717
- else
718
- this.setLearning(run, "reflecting", null, said.reflecting);
719
- }
720
- /** What the run's end-of-run turn wrote, checked and kept as its proposal or dropped (propose.ts), once at a time, after
721
- * the run has ended. Never rejects: `proposed` or `dropped`, `propose_failed` for a fault. A server that goes
722
- * meanwhile leaves it `reflecting` in serve.json, and the next start checks it again. */
723
- proposeOnce(run) {
724
- // A run hidden while this server can't read the browser sign-in it was handed (meta.unshown, runs.ts load): the pointer
725
- // is not among the values refused, so a note that holds it would pass, be graded and reach every later run. Its
726
- // learning is left as it is, unsaved (an ended run's hidden mark is this server's alone), for a server that reads the
727
- // sign-in to check it.
728
- if (!awaitsPropose(run.meta) || run.meta.unshown)
729
- return Promise.resolve();
730
- return once(this.proposing, run.id, () => this.propose(run));
731
- }
732
- /** The tools a run's workflow draft may name: its revision's granted tools a workflow's call node runs here that the
733
- * gate allows, under the server's tool settings and the agent's own (tools.ts learnedDraftTools); `gated`, those set to
734
- * ask or deny, which a draft that calls one is refused for. None for a revision not found, so a draft that names one is
735
- * refused. */
736
- async draftTools(run) {
737
- const known = await this.options.agents.revision(run.agentId, run.revision);
738
- if (known?.revision.kind !== "agentic")
739
- return { tools: [], gated: {} };
740
- const [{ learnedDraftTools }, { permissionFrom }, { readToolSettings }] = await Promise.all([import("./tools.js"), import("./tool-gate.js"), import("./tool-plane.js")]);
741
- // The server's settings, beside the agent store, as the run's gate reads them, and the agent's own.
742
- const server = readToolSettings(this.options.agents.dir).settings;
743
- const agent = (known.revision.body.toolSettings ?? {});
744
- // Unattended (approvals "never"): a tool set to ask is allowed at its run's gate, so a draft may name it.
745
- const approvals = known.revision.body.approvals === "never" ? "never" : "ask";
746
- return learnedDraftTools(known.revision.body.tools, (name) => permissionFrom(name, server, agent, approvals).permission, undefined, (name) => permissionFrom(name, server, agent, approvals).from);
747
- }
748
- async propose(run) {
749
- try {
750
- await learningTestHooks.beforePropose?.(run.dir);
751
- const pin = await readPin(run.dir);
752
- if (!pin)
753
- throw new Error("the run has no learning pin");
754
- // A pin of an empty seed (no main when the run started) is the folder's read's to propose from; this proposal
755
- // reads a commit of main.
756
- if (pin.seed === null)
757
- throw new Error("the run's pin names no commit of main to propose against");
758
- const { proposeRun } = await import("./propose.js");
759
- // The secrets this server holds, wherever its launcher put them, the keys saved on it, what each launch handed the
760
- // run, and its own tokens. How the job ended, before its end-of-run turn: only a completed run keeps a skill.
761
- const turn = await readTurn(run.dir);
762
- const drafts = await this.draftTools(run);
763
- const folder = learningFolderOf(this.options.agents.dir, run.agentId);
764
- let proposed;
765
- try {
766
- proposed = await proposeRun({ folder: folder.workspace, runDir: run.dir, seed: pin.seed, turn, refuse: this.refused(run), ...(run.meta.reflect ? { outcome: run.meta.reflect.outcome } : {}), ...drafts });
767
- }
768
- finally {
769
- await folder.end();
770
- }
771
- // What the checks left out goes on with it (`problems`), to be said with what it learned.
772
- if (proposed.state === "proposed")
773
- this.setLearning(run, "proposed", null, said.proposed(proposed.notes, proposed.skills, proposed.workflows), proposed.left ? { problems: proposed.left } : {});
774
- else if (proposed.reason === "empty")
775
- this.setLearning(run, "dropped", "empty", said.empty);
776
- else if (proposed.reason === "unparsed") {
777
- warn(run.id, `its end-of-run turn's answer was not read: ${proposed.detail}`);
778
- this.setLearning(run, "dropped", "unparsed", said.unparsed);
779
- }
780
- else
781
- this.setLearning(run, "dropped", "checker", said.checker(proposed.problems), { problems: proposed.problems });
782
- }
783
- catch (error) {
784
- warn(run.id, `what its end-of-run turn answered could not be checked: ${error.message}`);
785
- this.setLearning(run, "dropped", "propose_failed", said.stopped(STOPPED.notChecked));
786
- }
787
- await this.kernel.save(run).catch((error) => warn(run.id, `its learning is not saved: ${error.message}`));
788
- }
789
- /** What a run's learning refuses, and cuts from what it says: every secret this server never shows (serverSecrets),
790
- * what each launch handed the run, and every value its errors and answers never show (runs.ts' `handed`: its browser
791
- * sign-in's pointer among them, read back after a restart). */
792
- refused(run) {
793
- // The run's own door token too: its process holds it, so its notes may.
794
- const own = run.doorToken ?? runDoorToken(run.dir);
795
- return [...this.options.serverSecrets(), ...(this.handed.get(run.id) ?? []), ...(run.handed ?? []), ...(own ? [own] : [])];
796
- }
797
- }