@parcha/agentrun-serve 0.1.0-beta.38 → 0.1.0-beta.40

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 (423) hide show
  1. package/CHANGELOG.md +455 -0
  2. package/README.md +376 -125
  3. package/dist/adapters.d.ts +4 -1
  4. package/dist/adapters.js +4 -2
  5. package/dist/agent-rows.d.ts +0 -2
  6. package/dist/agents-store.d.ts +5 -5
  7. package/dist/agents-store.js +14 -14
  8. package/dist/bash-read-only.js +2 -1
  9. package/dist/cap-short.d.ts +12 -0
  10. package/dist/cap-short.js +60 -0
  11. package/dist/cap.d.ts +3 -10
  12. package/dist/cap.js +2 -5
  13. package/dist/catalog.d.ts +0 -5
  14. package/dist/catalog.js +1 -5
  15. package/dist/chatgpt-plan.d.ts +163 -0
  16. package/dist/chatgpt-plan.js +549 -0
  17. package/dist/cli.js +26 -6
  18. package/dist/composio-preset.d.ts +8 -0
  19. package/dist/composio-preset.js +8 -7
  20. package/dist/composio-triggers.d.ts +113 -0
  21. package/dist/composio-triggers.js +411 -0
  22. package/dist/computer-calls.js +2 -2
  23. package/dist/computer-commands.d.ts +2 -0
  24. package/dist/computer-commands.js +7 -3
  25. package/dist/computer-live.d.ts +7 -2
  26. package/dist/computer-live.js +9 -5
  27. package/dist/computer-routes.d.ts +2 -2
  28. package/dist/computer-routes.js +2 -2
  29. package/dist/computer-setting.d.ts +3 -3
  30. package/dist/computer-setting.js +5 -5
  31. package/dist/computer-view.d.ts +15 -0
  32. package/dist/computer-view.js +16 -2
  33. package/dist/computer-viewer.js +14 -1
  34. package/dist/computers.d.ts +5 -2
  35. package/dist/computers.js +9 -5
  36. package/dist/console/assets/index-B8Zn1Iqn.css +1 -0
  37. package/dist/console/assets/index-Jbxjcmdp.js +705 -0
  38. package/dist/console/index.html +2 -2
  39. package/dist/console-links.js +3 -3
  40. package/dist/decision-cap.d.ts +2 -5
  41. package/dist/decision-cap.js +3 -6
  42. package/dist/decision-rows.js +11 -11
  43. package/dist/email/account.d.ts +83 -0
  44. package/dist/email/account.js +159 -0
  45. package/dist/email/answer.d.ts +15 -0
  46. package/dist/email/answer.js +217 -0
  47. package/dist/email/email-faux.d.ts +95 -0
  48. package/dist/email/email-faux.js +849 -0
  49. package/dist/email/labels.d.ts +1 -0
  50. package/dist/email/labels.js +84 -0
  51. package/dist/email/mail.d.ts +186 -0
  52. package/dist/email/mail.js +373 -0
  53. package/dist/email/names.d.ts +50 -0
  54. package/dist/email/names.js +74 -0
  55. package/dist/email/routes.d.ts +10 -0
  56. package/dist/email/routes.js +50 -0
  57. package/dist/email/tools.d.ts +27 -0
  58. package/dist/email/tools.js +562 -0
  59. package/dist/environment.d.ts +17 -2
  60. package/dist/environment.js +36 -8
  61. package/dist/errors.d.ts +6 -0
  62. package/dist/errors.js +12 -0
  63. package/dist/eval-mailbox.d.ts +103 -0
  64. package/dist/eval-mailbox.js +236 -0
  65. package/dist/graph-view.d.ts +9 -2
  66. package/dist/graph-view.js +6 -2
  67. package/dist/harness-install.d.ts +28 -2
  68. package/dist/harness-install.js +147 -59
  69. package/dist/harness-opencode/adapter.js +2 -26
  70. package/dist/harness-opencode/facts.js +5 -2
  71. package/dist/harness-opencode/host/policy.d.ts +2 -0
  72. package/dist/harness-opencode/host/policy.js +10 -5
  73. package/dist/harness-opencode/index.d.ts +1 -1
  74. package/dist/harness-opencode/index.js +1 -1
  75. package/dist/harness-opencode/versions.d.ts +6 -0
  76. package/dist/harness-opencode/versions.js +6 -0
  77. package/dist/harness-table.js +8 -5
  78. package/dist/harness.d.ts +12 -0
  79. package/dist/harness.js +15 -3
  80. package/dist/hosted-search.d.ts +3 -1
  81. package/dist/hosted-search.js +51 -11
  82. package/dist/import-sources.d.ts +1 -1
  83. package/dist/import-sources.js +3 -3
  84. package/dist/import.d.ts +21 -0
  85. package/dist/import.js +186 -1
  86. package/dist/key-check.d.ts +0 -2
  87. package/dist/key-check.js +3 -4
  88. package/dist/key-refusal.d.ts +8 -2
  89. package/dist/key-refusal.js +16 -13
  90. package/dist/learn/check.d.ts +18 -3
  91. package/dist/learn/check.js +328 -3
  92. package/dist/learn/compile.d.ts +12 -4
  93. package/dist/learn/compile.js +57 -20
  94. package/dist/learn/grade.d.ts +4 -0
  95. package/dist/learn/grade.js +4 -1
  96. package/dist/learn/promote.d.ts +130 -64
  97. package/dist/learn/promote.js +113 -202
  98. package/dist/learn/refit.d.ts +21 -12
  99. package/dist/learn/refit.js +51 -23
  100. package/dist/learn/replay-calls.js +1 -1
  101. package/dist/learn/skills.d.ts +2 -7
  102. package/dist/learn/skills.js +28 -14
  103. package/dist/learned-nodes.d.ts +15 -7
  104. package/dist/learned-nodes.js +29 -5
  105. package/dist/learned-workflow.d.ts +41 -31
  106. package/dist/learned-workflow.js +122 -63
  107. package/dist/learning-batch.js +3 -3
  108. package/dist/learning-cost.d.ts +35 -8
  109. package/dist/learning-cost.js +27 -9
  110. package/dist/learning-routes.d.ts +2 -4
  111. package/dist/learning-routes.js +20 -3
  112. package/dist/learning-words.d.ts +1 -1
  113. package/dist/learning-words.js +4 -1
  114. package/dist/learning.d.ts +5 -5
  115. package/dist/link-check.d.ts +27 -0
  116. package/dist/link-check.js +164 -0
  117. package/dist/local-models.d.ts +39 -2
  118. package/dist/local-models.js +49 -5
  119. package/dist/mcp-pin-first.js +6 -0
  120. package/dist/mcp-secret-writes.js +3 -0
  121. package/dist/mcp-servers-store.d.ts +25 -16
  122. package/dist/mcp-servers-store.js +23 -3
  123. package/dist/model-form.d.ts +11 -0
  124. package/dist/model-form.js +17 -1
  125. package/dist/model.d.ts +26 -6
  126. package/dist/model.js +126 -18
  127. package/dist/monid.d.ts +26 -31
  128. package/dist/monid.js +84 -79
  129. package/dist/native/binding.d.ts +13 -0
  130. package/dist/native/binding.js +57 -0
  131. package/dist/native/cli.d.ts +2 -0
  132. package/dist/native/cli.js +48 -0
  133. package/dist/native/connection.d.ts +8 -0
  134. package/dist/native/connection.js +94 -0
  135. package/dist/native/context.d.ts +6 -0
  136. package/dist/native/context.js +22 -0
  137. package/dist/native/discovery.d.ts +27 -0
  138. package/dist/native/discovery.js +404 -0
  139. package/dist/native/environment.d.ts +4 -0
  140. package/dist/native/environment.js +18 -0
  141. package/dist/native/history.d.ts +20 -0
  142. package/dist/native/history.js +147 -0
  143. package/dist/native/instructions.d.ts +21 -0
  144. package/dist/native/instructions.js +173 -0
  145. package/dist/native/remote-worker-bridge.d.ts +3 -0
  146. package/dist/native/remote-worker-bridge.js +278 -0
  147. package/dist/native/remote.d.ts +187 -0
  148. package/dist/native/remote.js +492 -0
  149. package/dist/native/runtime.d.ts +17 -0
  150. package/dist/native/runtime.js +316 -0
  151. package/dist/native/runtimes.d.ts +66 -0
  152. package/dist/native/runtimes.js +41 -0
  153. package/dist/native/schema.d.ts +46 -0
  154. package/dist/native/schema.js +41 -0
  155. package/dist/native/types.d.ts +87 -0
  156. package/dist/native/types.js +1 -0
  157. package/dist/observation/http.d.ts +4 -0
  158. package/dist/observation/http.js +140 -0
  159. package/dist/observation/observer.d.ts +301 -0
  160. package/dist/observation/observer.js +919 -0
  161. package/dist/observation/schema.d.ts +52 -0
  162. package/dist/observation/schema.js +154 -0
  163. package/dist/openapi.d.ts +2 -1
  164. package/dist/openapi.js +404 -93
  165. package/dist/paid-calls.d.ts +8 -3
  166. package/dist/paid-calls.js +6 -2
  167. package/dist/place-offers.d.ts +4 -8
  168. package/dist/place-offers.js +4 -9
  169. package/dist/port-models.d.ts +10 -2
  170. package/dist/port-models.js +6 -2
  171. package/dist/print.d.ts +4 -2
  172. package/dist/print.js +4 -3
  173. package/dist/propose.d.ts +1 -2
  174. package/dist/propose.js +10 -1
  175. package/dist/provider.d.ts +5 -1
  176. package/dist/provider.js +8 -0
  177. package/dist/push-handler.d.ts +3 -2
  178. package/dist/push-handler.js +20 -6
  179. package/dist/refusal-words.d.ts +14 -0
  180. package/dist/refusal-words.js +16 -2
  181. package/dist/run-launch.d.ts +12 -1
  182. package/dist/run-launch.js +13 -4
  183. package/dist/run-learning-served.d.ts +75 -2
  184. package/dist/run-manifest.d.ts +3 -0
  185. package/dist/run-manifest.js +13 -1
  186. package/dist/run-reads-over-store.d.ts +3 -3
  187. package/dist/run-reads-over-store.js +13 -10
  188. package/dist/run-timeline-served.d.ts +3 -2
  189. package/dist/run-timeline-served.js +8 -9
  190. package/dist/run-view.d.ts +44 -38
  191. package/dist/run-view.js +93 -56
  192. package/dist/runnable.d.ts +13 -5
  193. package/dist/runnable.js +69 -60
  194. package/dist/runner/adapter-loop.d.ts +48 -13
  195. package/dist/runner/adapter-loop.js +691 -254
  196. package/dist/runner/adapter-run-spec.d.ts +38 -12
  197. package/dist/runner/adapter-run-spec.js +175 -106
  198. package/dist/runner/adapters/pi.d.ts +24 -0
  199. package/dist/runner/adapters/pi.js +59 -9
  200. package/dist/runner/beside.d.ts +3 -1
  201. package/dist/runner/beside.js +24 -8
  202. package/dist/runner/claim-secrets.d.ts +3 -1
  203. package/dist/runner/claim-secrets.js +28 -4
  204. package/dist/runner/cli-run-spec.d.ts +30 -0
  205. package/dist/runner/cli-run-spec.js +95 -50
  206. package/dist/runner/computer-channel.d.ts +5 -2
  207. package/dist/runner/computer-channel.js +3 -2
  208. package/dist/runner/computer-journal.d.ts +6 -0
  209. package/dist/runner/computer-journal.js +8 -10
  210. package/dist/runner/conformance/harness.js +6 -3
  211. package/dist/runner/end-of-run.d.ts +2 -0
  212. package/dist/runner/end-of-run.js +3 -1
  213. package/dist/runner/feedback-round.d.ts +40 -13
  214. package/dist/runner/feedback-round.js +64 -16
  215. package/dist/runner/harness-loops.js +17 -0
  216. package/dist/runner/journal-kinds.d.ts +59 -13
  217. package/dist/runner/journal-kinds.js +48 -5
  218. package/dist/runner/learning-dispatch.d.ts +3 -2
  219. package/dist/runner/learning-dispatch.js +6 -6
  220. package/dist/runner/learning-end.d.ts +4 -0
  221. package/dist/runner/learning-end.js +64 -36
  222. package/dist/runner/learning-factory.d.ts +3 -0
  223. package/dist/runner/learning-factory.js +8 -2
  224. package/dist/runner/learning-loop.d.ts +2 -0
  225. package/dist/runner/learning-loop.js +19 -4
  226. package/dist/runner/loop.d.ts +18 -0
  227. package/dist/runner/loop.js +34 -0
  228. package/dist/runner/main.d.ts +6 -1
  229. package/dist/runner/main.js +26 -4
  230. package/dist/runner/mcp-run.d.ts +4 -1
  231. package/dist/runner/mcp-run.js +11 -5
  232. package/dist/runner/meta-tools.d.ts +10 -3
  233. package/dist/runner/meta-tools.js +112 -36
  234. package/dist/runner/metrics.d.ts +80 -21
  235. package/dist/runner/metrics.js +114 -39
  236. package/dist/runner/model-route.d.ts +14 -3
  237. package/dist/runner/model-route.js +155 -19
  238. package/dist/runner/monid-run.d.ts +8 -6
  239. package/dist/runner/monid-run.js +117 -43
  240. package/dist/runner/native-loop.d.ts +11 -0
  241. package/dist/runner/native-loop.js +438 -0
  242. package/dist/runner/promotion.d.ts +76 -10
  243. package/dist/runner/promotion.js +476 -154
  244. package/dist/runner/redact.js +3 -0
  245. package/dist/runner/reflect-hook.js +9 -0
  246. package/dist/runner/runner.d.ts +7 -1
  247. package/dist/runner/runner.js +8 -4
  248. package/dist/runner/sdk-loop.d.ts +7 -0
  249. package/dist/runner/sdk-loop.js +470 -0
  250. package/dist/runner/spec-holds.d.ts +16 -2
  251. package/dist/runner/spec-holds.js +42 -14
  252. package/dist/runner/store-secrets.js +17 -2
  253. package/dist/runner/workflow-first.d.ts +30 -20
  254. package/dist/runner/workflow-first.js +22 -32
  255. package/dist/runner/workflow-loop.d.ts +1 -1
  256. package/dist/runner/workflow-loop.js +18 -12
  257. package/dist/runner/workflow-spec.d.ts +4 -1
  258. package/dist/runner/workflow-spec.js +56 -45
  259. package/dist/runs-over-store.d.ts +30 -2
  260. package/dist/runs-over-store.js +46 -14
  261. package/dist/sdk/runs.d.ts +115 -0
  262. package/dist/sdk/runs.js +422 -0
  263. package/dist/sdk/schema.d.ts +175 -0
  264. package/dist/sdk/schema.js +138 -0
  265. package/dist/sdk/workflows.d.ts +22 -0
  266. package/dist/sdk/workflows.js +39 -0
  267. package/dist/secrets.d.ts +2 -0
  268. package/dist/secrets.js +19 -1
  269. package/dist/server.d.ts +48 -32
  270. package/dist/server.js +568 -137
  271. package/dist/skill-route.d.ts +3 -0
  272. package/dist/skill-route.js +16 -0
  273. package/dist/skills/agentrun-serve/SKILL.md +368 -0
  274. package/dist/stats.d.ts +10 -10
  275. package/dist/store/agent-body.d.ts +12 -8
  276. package/dist/store/agent-body.js +9 -4
  277. package/dist/store/dialect.d.ts +3 -0
  278. package/dist/store/dialect.js +1 -0
  279. package/dist/store/index.d.ts +1 -1
  280. package/dist/store/native-command-owner.d.ts +10 -0
  281. package/dist/store/native-command-owner.js +47 -0
  282. package/dist/store/postgres/connection.d.ts +11 -5
  283. package/dist/store/postgres/connection.js +44 -17
  284. package/dist/store/postgres/dialect.js +5 -0
  285. package/dist/store/postgres/migrations/0012_revision_monid_pin.sql +4 -0
  286. package/dist/store/postgres/migrations/0013_native_command_owner.sql +6 -0
  287. package/dist/store/postgres/migrations/0014_run_cost_upper_bound.sql +6 -0
  288. package/dist/store/postgres/store.d.ts +7 -3
  289. package/dist/store/postgres/store.js +4 -0
  290. package/dist/store/schema.d.ts +7 -0
  291. package/dist/store/schema.js +20 -1
  292. package/dist/store/sqlite/connection.js +72 -8
  293. package/dist/store/sqlite/run-launch.d.ts +11 -0
  294. package/dist/store/sqlite/run-launch.js +8 -0
  295. package/dist/store/sqlite/run-patch.d.ts +8 -1
  296. package/dist/store/sqlite/run-patch.js +14 -3
  297. package/dist/store/sqlite/store.js +170 -34
  298. package/dist/store/sqlite/worker.d.ts +2 -0
  299. package/dist/store/sqlite/worker.js +13 -10
  300. package/dist/store/store.d.ts +67 -7
  301. package/dist/store-reads/analytics.d.ts +7 -7
  302. package/dist/store-reads/analytics.js +23 -5
  303. package/dist/store-reads/cap.js +8 -6
  304. package/dist/store-reads/journal.d.ts +5 -0
  305. package/dist/store-reads/journal.js +85 -41
  306. package/dist/store-reads/learning.d.ts +115 -18
  307. package/dist/store-reads/learning.js +170 -68
  308. package/dist/store-reads/runs.d.ts +5 -0
  309. package/dist/store-reads/runs.js +3 -2
  310. package/dist/store-view.js +75 -21
  311. package/dist/template-eval.d.ts +73 -11
  312. package/dist/template-eval.js +535 -75
  313. package/dist/templates.d.ts +84 -21
  314. package/dist/templates.js +254 -47
  315. package/dist/text-urls.d.ts +38 -0
  316. package/dist/text-urls.js +170 -0
  317. package/dist/timeline/index.d.ts +3 -2
  318. package/dist/timeline/index.js +3 -2
  319. package/dist/timeline/labels.d.ts +5 -2
  320. package/dist/timeline/labels.js +29 -6
  321. package/dist/timeline/origin.d.ts +1 -1
  322. package/dist/timeline/origin.js +2 -2
  323. package/dist/timeline/render-text.js +1 -1
  324. package/dist/timeline/timeline.d.ts +66 -4
  325. package/dist/timeline/timeline.js +160 -30
  326. package/dist/timeline/util.d.ts +5 -0
  327. package/dist/timeline/util.js +44 -8
  328. package/dist/timeline/web-calls.d.ts +17 -0
  329. package/dist/timeline/web-calls.js +106 -0
  330. package/dist/tool-gate.d.ts +46 -44
  331. package/dist/tool-gate.js +101 -92
  332. package/dist/tool-list.d.ts +4 -1
  333. package/dist/tool-list.js +8 -1
  334. package/dist/tool-names.d.ts +18 -13
  335. package/dist/tool-names.js +19 -17
  336. package/dist/tool-plane.d.ts +17 -13
  337. package/dist/tool-plane.js +12 -5
  338. package/dist/tools.d.ts +57 -16
  339. package/dist/tools.js +33 -28
  340. package/dist/trigger-adapter.d.ts +56 -5
  341. package/dist/trigger-adapter.js +6 -0
  342. package/dist/trigger-adapters.js +3 -1
  343. package/dist/trigger-composio.d.ts +10 -0
  344. package/dist/trigger-composio.js +122 -0
  345. package/dist/trigger-email.d.ts +4 -0
  346. package/dist/trigger-email.js +181 -0
  347. package/dist/trigger-signatures.d.ts +6 -10
  348. package/dist/trigger-signatures.js +16 -16
  349. package/dist/trigger-targets.d.ts +1 -1
  350. package/dist/trigger-targets.js +4 -3
  351. package/dist/trigger-webhook.js +6 -4
  352. package/dist/triggers.d.ts +105 -8
  353. package/dist/triggers.js +286 -62
  354. package/dist/uploads.d.ts +9 -0
  355. package/dist/uploads.js +27 -0
  356. package/dist/validate.js +1 -7
  357. package/dist/web-fake.d.ts +56 -0
  358. package/dist/web-fake.js +102 -0
  359. package/dist/web-fetch-direct.d.ts +3 -0
  360. package/dist/web-fetch-direct.js +4 -1
  361. package/dist/web-fetch.d.ts +5 -1
  362. package/dist/web-fetch.js +8 -31
  363. package/dist/web-search.d.ts +51 -2
  364. package/dist/web-search.js +116 -18
  365. package/dist/webhooks.d.ts +2 -1
  366. package/dist/webhooks.js +5 -1
  367. package/dist/workflow/decision-recovery.js +5 -2
  368. package/dist/workflow/decisions.d.ts +7 -0
  369. package/dist/workflow/node-runner.js +10 -2
  370. package/dist/workspace/archil-claim.d.ts +3 -0
  371. package/dist/workspace/archil-claim.js +9 -2
  372. package/dist/workspace/computer-deps.d.ts +22 -9
  373. package/dist/workspace/computer-deps.js +78 -37
  374. package/dist/workspace/computer-for-claim.d.ts +54 -4
  375. package/dist/workspace/computer-for-claim.js +179 -39
  376. package/dist/workspace/computer-janitor.d.ts +10 -3
  377. package/dist/workspace/computer-janitor.js +96 -29
  378. package/dist/workspace/computer-kept.d.ts +82 -0
  379. package/dist/workspace/computer-kept.js +147 -0
  380. package/dist/workspace/computer-mcp.js +4 -3
  381. package/dist/workspace/computer-ports.d.ts +23 -3
  382. package/dist/workspace/computer-ports.js +115 -5
  383. package/dist/workspace/index.d.ts +1 -2
  384. package/dist/workspace/index.js +1 -2
  385. package/dist/workspace/kind-skills/desktop/SKILL.md +25 -18
  386. package/dist/workspace/kind-skills/phone/SKILL.md +28 -17
  387. package/dist/workspace/resolve-computer.d.ts +15 -3
  388. package/dist/workspace/resolve-computer.js +10 -7
  389. package/dist/workspace/revision-skills.d.ts +6 -2
  390. package/dist/workspace/revision-skills.js +12 -5
  391. package/dist/workspace/run-end-computers.d.ts +30 -0
  392. package/dist/workspace/run-end-computers.js +137 -0
  393. package/dist/workspace/run-tools.d.ts +28 -29
  394. package/dist/workspace/run-tools.js +29 -30
  395. package/dist/wrap-up.d.ts +19 -5
  396. package/dist/wrap-up.js +31 -4
  397. package/package.json +13 -9
  398. package/skills/agentrun-serve/SKILL.md +248 -10
  399. package/templates/README.md +117 -23
  400. package/templates/TOOLS.md +168 -0
  401. package/templates/code-review/agent.md +18 -4
  402. package/templates/code-review/evals/mentions-defused/case.yaml +1 -1
  403. package/templates/code-review/evals/off-by-one/graders/no-repo-reads.md +2 -1
  404. package/templates/code-review/evals/other-repo/case.yaml +1 -1
  405. package/templates/code-review/evals/pasted-by-reference/case.yaml +5 -0
  406. package/templates/code-review/evals/pasted-by-reference/graders/by-reference.md +15 -0
  407. package/templates/code-review/evals/pasted-by-reference/graders/head-sha.md +6 -0
  408. package/templates/code-review/evals/pasted-by-reference/prompt.md +31 -0
  409. package/templates/code-review/skills/review/DEEP.md +2 -2
  410. package/templates/code-review/skills/review/REVIEWER.md +2 -2
  411. package/templates/code-review/skills/review/SKILL.md +7 -3
  412. package/templates/release-notes/evals/draft-not-publish/case.yaml +1 -1
  413. package/templates/release-notes/evals/issue-write-refused/case.yaml +1 -1
  414. package/templates/release-notes/evals/late-arrival/case.yaml +1 -1
  415. package/templates/release-notes/evals/other-repo/case.yaml +1 -1
  416. package/dist/console/assets/index-CjqQirbo.css +0 -1
  417. package/dist/console/assets/index-b6-XY5A9.js +0 -576
  418. package/dist/host-calls.d.ts +0 -20
  419. package/dist/host-calls.js +0 -12
  420. package/dist/runner/adapters/pi-computer.d.ts +0 -9
  421. package/dist/runner/adapters/pi-computer.js +0 -51
  422. package/dist/workspace/computer-script.d.ts +0 -53
  423. package/dist/workspace/computer-script.js +0 -172
@@ -11,7 +11,11 @@ export const REFIT_EVERY = 20;
11
11
  /** How many grades, and how many promotion records, an agent keeps (newest kept). */
12
12
  export const GRADES_KEPT = 500;
13
13
  export const PROMOTIONS_KEPT = 200;
14
+ /** The most characters a grade's words (`label`) may hold, and the most of them and of a wrong answer a revise ask shows. */
15
+ export const LABEL_MAX = 4_000;
14
16
  export const GRADE_SOURCES = ["feedback", "eval"];
17
+ /** The ReplayScore fields of `x` alone (a scoreRows or refitGuards result, or a record's `replay`): the one pick of them. */
18
+ export const scoreOf = (x) => ({ n: x.n, agree: x.agree, agent_acc: x.agent_acc, wf_acc: x.wf_acc, delta: x.delta, handed_back: x.handed_back });
15
19
  /** Whether a replay or refit answered alone on at least one of its inputs: one that hands back every input would never
16
20
  * serve. The one spelling of it (clears, settle's hold, the compile's held line); the count is n - handed_back. */
17
21
  export const answersAlone = (score) => score.handed_back < score.n;
@@ -58,47 +62,78 @@ export const putGrade = async (settings, agentId, grade) => {
58
62
  });
59
63
  return kept;
60
64
  };
61
- /** The agent's promotion records, oldest first. */
62
- export const promotionsOf = async (settings, agentId) => itemsOf(await settings.get(promotionsKey(agentId)));
65
+ /** Marks the kept grade `grade` (its run, given at its `at`, kept at its `seq`) with the feedback round it opened and
66
+ * that round's steer, in place: its `seq` stays, so a serving digest's check never counts it twice. A newer grade of the
67
+ * run is left as it is, one given in the same millisecond too (its `seq` is another). The grade, marked. */
68
+ export const markGradeRound = async (settings, agentId, grade, opened) => {
69
+ const mark = { feedback: opened.round, feedbackCommand: opened.command };
70
+ await updateKept(settings, gradesKey(agentId), (items) => items.map((one) => (one.run === grade.run && one.at === grade.at && one.seq === grade.seq ? { ...one, ...mark } : one)));
71
+ return { ...grade, ...mark };
72
+ };
73
+ /** The agent's promotion records, oldest first. A beta.39 store may hold `decided: "superseded"` (a replay a newer draft
74
+ * cancelled; beta.40 writes `skipped` for an older draft instead): read as `skipped`, `skipped_by` the draft that
75
+ * superseded it, with a reason that says when. Only the read maps (readAs, also what a guarded write compares): the stored
76
+ * record is never rewritten. */
77
+ export const readAs = (one) => {
78
+ if (one.decided !== "superseded")
79
+ return one;
80
+ const { superseded_by: by, ...rest } = one;
81
+ return { ...rest, decided: "skipped", reason: "a newer draft arrived while it replayed (before beta.40)", ...(by ? { skipped_by: by } : {}) };
82
+ };
83
+ export const promotionsOf = async (settings, agentId) => itemsOf(await settings.get(promotionsKey(agentId))).map(readAs);
63
84
  /** The newest record of `workflow` at `digest`, or undefined. */
64
85
  export const promotionOf = (records, workflow, digest) => records.findLast((one) => one.workflow === workflow && one.digest === digest);
65
- /** The champion of `workflow`: the newest record of the digest promoted last (a digest's place is its first promote
66
- * record's), while that digest's newest record still says promote. A newer revision replaying or held never stops it; a
86
+ /** The champion of `workflow`: the newest record of the digest promoted last (a digest's place is its latest win's,
87
+ * winsOf), while that digest's newest record still says promote. A newer revision replaying or held never stops it; a
67
88
  * wrong served grade asks for a revision and never stops it; it stops when a newer revision's replay promotes that one
68
- * (then the champion), or its own graded runs hold it (source graded: no champion until a revision is promoted). The
69
- * money rule stops it on its own (runner/workflow-first.ts). Undefined: none. */
89
+ * (then the champion), or its own graded runs hold it (source graded: no champion until a revision is promoted). A held
90
+ * revision its owner replays (runner/promotion.ts askReplay) and the replay promotes wins again: the champion from then.
91
+ * The money rule stops it on its own (runner/workflow-first.ts). Undefined: none. */
70
92
  export function championOf(records, workflow) {
71
93
  const last = lastPromotedOf(records, workflow);
72
94
  const now = last === undefined ? undefined : promotionOf(records, workflow, last);
73
95
  return now?.decided === "promote" ? now : undefined;
74
96
  }
75
- /** The digest of `workflow` promoted last: the one whose first promote record is the newest of every digest's first. */
97
+ /** Each promote record's win, by index: the index of the promote record that began its digest's run of promote records
98
+ * (its first, or the first after a record of that digest that was not promote: a replay that promoted a held revision).
99
+ * A graded check's mark (a promote after a promote, runner/promotion.ts stillServing) is never a win of its own. */
100
+ function winsOf(records) {
101
+ const wins = new Map();
102
+ const streak = new Map();
103
+ records.forEach((one, i) => {
104
+ const at = JSON.stringify([one.workflow, one.digest]);
105
+ if (one.decided !== "promote")
106
+ return void streak.delete(at);
107
+ if (!streak.has(at))
108
+ streak.set(at, i);
109
+ wins.set(i, streak.get(at));
110
+ });
111
+ return wins;
112
+ }
113
+ /** The digest of `workflow` promoted last: the one whose latest win (winsOf) is the newest of every digest's. */
76
114
  function lastPromotedOf(records, workflow) {
77
- const seen = new Set();
78
115
  let last;
79
- for (const one of records) {
80
- if (one.workflow !== workflow || one.decided !== "promote" || seen.has(one.digest))
81
- continue;
82
- seen.add(one.digest);
83
- last = one.digest;
84
- }
116
+ for (const [i, win] of winsOf(records))
117
+ if (i === win && records[i].workflow === workflow)
118
+ last = records[i].digest;
85
119
  return last;
86
120
  }
87
- /** The revisions of `workflow` other than `digest` whose newest record still says replaying, newest first: at most one
88
- * once each new revision supersedes the one replaying (runner/promotion.ts). */
89
- export function replayingOf(records, workflow, digest) {
121
+ /** The revisions of `workflow` other than `digest` whose newest record still says `decided` (replaying, else queued),
122
+ * newest first: at most one replaying (one replay at a time, the reservation's guarded write), and at most one queued
123
+ * once each newer one skips the one queued before it (runner/promotion.ts). */
124
+ export function replayingOf(records, workflow, digest, decided = "replaying") {
90
125
  const out = [];
91
126
  for (const one of [...records].reverse()) {
92
127
  if (one.workflow !== workflow || one.digest === digest || out.some((seen) => seen.digest === one.digest))
93
128
  continue;
94
- if (promotionOf(records, workflow, one.digest) === one && one.decided === "replaying")
129
+ if (promotionOf(records, workflow, one.digest) === one && one.decided === decided)
95
130
  out.push(one);
96
131
  }
97
132
  return out;
98
133
  }
99
134
  /** What each learned workflow with a record serves, by name: `digest`, its champion's (championOf; null: none serves,
100
- * the agent takes its runs), and `replaying`, the revision other than it whose replay is at work (null: none; a
101
- * superseded one's replay was cancelled, never at work). What the promotions list says beside the records
135
+ * the agent takes its runs), and `replaying`, the revision other than it whose replay is at work (null: none; a queued
136
+ * one is not at work yet). What the promotions list says beside the records
102
137
  * (GET /agents/{id}/learning/promotions). */
103
138
  export function servingOf(records) {
104
139
  const names = [...new Set(records.map((one) => one.workflow))].sort((a, b) => (a < b ? -1 : a > b ? 1 : 0));
@@ -110,52 +145,60 @@ export function servingOf(records) {
110
145
  /** The promotions list as it leaves (GET /agents/{id}/learning/promotions): the records and what serves (servingOf), every
111
146
  * text in them cut by `shown` (learning.ts secretsShown, as the other learning reads cut theirs), at any depth and under
112
147
  * any field, so a field added later is cut too: a workflow's name whole (`shown.name`), any other text of its part
113
- * (`shown.text`: a reason, a refit guard's label, a digest, a run id). Numbers and the rest as they are. */
148
+ * (`shown.text`: a reason, a refit guard's label, a digest, a run id). Numbers and the rest as they are. A replaying
149
+ * record's `launch` (a run launch: its notes, model and plane) never leaves: aggregates only. */
114
150
  export function promotionsShown(records, shown) {
115
151
  const cut = (value, key) => typeof value === "string" ? (key === "workflow" ? shown.name(value) : shown.text(value))
116
152
  : Array.isArray(value) ? value.map((one) => cut(one))
117
153
  : value !== null && typeof value === "object" ? Object.fromEntries(Object.entries(value).map(([field, one]) => [field, cut(one, field)]))
118
154
  : value;
119
- return { promotions: records.map((one) => cut(one)), serving: servingOf(records).map((one) => cut(one)) };
155
+ return { promotions: records.map(({ launch: _, ...one }) => cut(one)), serving: servingOf(records).map((one) => cut(one)) };
120
156
  }
121
157
  /** Keeps `record` as the newest of its workflow at its digest, when `still` holds of the one there now (none: undefined)
122
158
  * and of every record kept (`items`: a check across revisions, one challenger at a time, in the same guarded write): so
123
- * of two claims deciding at once only the first decision is kept, and only its claim acts on it (Recorded.won). */
124
- export async function recordPromotion(settings, agentId, record, still) {
159
+ * of two claims deciding at once only the first decision is kept, and only its claim acts on it (Recorded.won). `also`:
160
+ * records kept after it in the same write, all or none (a queue change and the skips it makes, runner/promotion.ts). */
161
+ export async function recordPromotion(settings, agentId, record, still, also = []) {
125
162
  let out = { record, won: true };
126
163
  await updateKept(settings, promotionsKey(agentId), (items) => {
127
- const now = promotionOf(items, record.workflow, record.digest);
128
- if (!still(now, items)) {
164
+ // `still` compares what a read shows (readAs); the write keeps the stored items as they are.
165
+ const read = items.map(readAs);
166
+ const now = promotionOf(read, record.workflow, record.digest);
167
+ if (!still(now, read)) {
129
168
  out = { record: now, won: false };
130
169
  return items;
131
170
  }
132
171
  out = { record, won: true };
133
- return trimPromotions([...items, record]);
172
+ return trimPromotions([...items, record, ...also]);
134
173
  });
135
174
  return out;
136
175
  }
137
176
  /** The newest records, at most PROMOTIONS_KEPT, in order, with each workflow's champion (championOf) the same after as
138
177
  * before, whatever was dropped. championOf reads two things, and the trim keeps both: the place of every digest it can
139
- * still see promoted (a promote record kept keeps its digest's first promote record beside it, so an older revision's
140
- * late graded check never reads as a newer promotion), and the digest promoted last, its first promote record and its
141
- * newest record (so a held last revision is never trimmed into handing the champion back). */
178
+ * still see promoted (a promote record kept keeps its win, winsOf, beside it, so an older revision's late graded check
179
+ * never reads as a newer promotion), and the digest promoted last, its latest win and its newest record (so a held last
180
+ * revision is never trimmed into handing the champion back). The kept records are a run of the newest plus those wins,
181
+ * and a win's own run reaches no further back than it: a record that ended an older run of the same digest is newer
182
+ * than every record of that run, so a run is never joined to an older one by the trim. The queue's state is kept too,
183
+ * whatever its age: a replaying revision's newest record (what its settle, its runs' claims and the one-replay-at-a-time
184
+ * check read: dropped, a newer revision would start a second replay while its runs run, and its verdict would never
185
+ * land) and a queued one's (what replays next). Neither is a promote record, so championOf reads the same. */
142
186
  export function trimPromotions(items) {
143
187
  if (items.length <= PROMOTIONS_KEPT)
144
188
  return [...items];
145
- const firstPromote = new Map();
146
- items.forEach((one, i) => {
147
- const at = JSON.stringify([one.workflow, one.digest]);
148
- if (one.decided === "promote" && !firstPromote.has(at))
149
- firstPromote.set(at, i);
150
- });
151
- const placeOf = (i) => (items[i].decided === "promote" ? firstPromote.get(JSON.stringify([items[i].workflow, items[i].digest])) : undefined);
189
+ const wins = winsOf(items);
190
+ const placeOf = (i) => wins.get(i);
152
191
  const keep = new Set();
153
192
  for (const workflow of new Set(items.map((one) => one.workflow))) {
193
+ for (const one of [...replayingOf(items, workflow, null), ...replayingOf(items, workflow, null, "queued")])
194
+ keep.add(items.lastIndexOf(one));
154
195
  const last = lastPromotedOf(items, workflow);
155
196
  if (last === undefined)
156
197
  continue;
157
- keep.add(firstPromote.get(JSON.stringify([workflow, last])));
158
- keep.add(items.findLastIndex((one) => one.workflow === workflow && one.digest === last));
198
+ const newest = items.findLastIndex((one) => one.workflow === workflow && one.digest === last);
199
+ // Its latest win: the newest win of that digest.
200
+ keep.add(Math.max(...[...wins].filter(([i, win]) => i === win && items[i].workflow === workflow && items[i].digest === last).map(([i]) => i)));
201
+ keep.add(newest);
159
202
  }
160
203
  for (let i = items.length - 1, room = PROMOTIONS_KEPT - keep.size; i >= 0 && room > 0; i--) {
161
204
  if (keep.has(i))
@@ -171,11 +214,15 @@ export function trimPromotions(items) {
171
214
  }
172
215
  return items.filter((_, i) => keep.has(i));
173
216
  }
174
- /** An answer's value: its text as JSON when it is JSON, else its last fenced json block, else its trimmed text. */
217
+ /** The text inside an answer that is one unlabelled fence and nothing else, or undefined. */
218
+ const WHOLE_FENCE = /^ {0,3}(`{3,}|~{3,})[ \t]*\r?\n([\s\S]*?)\r?\n {0,3}\1[`~]*[ \t]*$/;
219
+ /** An answer's value: its text as JSON when it is JSON (bare, or the whole of one unlabelled fence), else its last fenced
220
+ * json block, else its trimmed text. */
175
221
  function valueOf(answer) {
176
222
  const text = answer.trim();
223
+ const inner = WHOLE_FENCE.exec(text)?.[2];
177
224
  try {
178
- return JSON.parse(text);
225
+ return JSON.parse(inner ?? text);
179
226
  }
180
227
  catch {
181
228
  const block = answerJson(text);
@@ -197,25 +244,36 @@ function same(a, b) {
197
244
  }
198
245
  /** Whether two answers say the same: equal as JSON values, else as trimmed text. */
199
246
  export const sameAnswer = (a, b) => a !== null && b !== null && same(valueOf(a), valueOf(b));
200
- /** Whether the workflow's `answer` to a graded input is right: with the grade's expected fields, when its JSON holds each
201
- * of them (or, one field expected, when the answer is that field's text: a workflow whose output is one text field
202
- * answers with that text, learned-workflow.ts learnedOutput); else when it says what the agent's graded answer said,
203
- * that answer's grade. An answer that differs from the agent's when the agent's was wrong is counted wrong: nothing says
204
- * it is right (a replay never flatters the workflow). */
247
+ /** Whether the workflow's `answer` to a graded input is right by its grade: with the grade's expected fields, when its JSON
248
+ * holds each of them (or, one field expected, when the answer is that field's value: a workflow whose output is one
249
+ * field answers with its text, or its JSON text when it is no text, learned-workflow.ts learnedOutput). Else, graded
250
+ * right, when it says what the graded run said. Graded wrong: on a run a learned workflow answered alone (`workflow`),
251
+ * when it says anything else (the bar on a wrong grade is the grade: saying what was graded wrong again is never a tie),
252
+ * and wrong when what it said could not be read (nothing says it differs); on a run the agent answered, wrong whatever it
253
+ * says (another answer proves nothing: a neutral tie with the agent's, agentRight). */
205
254
  export function answerCorrect(answer, recorded) {
206
255
  if (answer === null)
207
256
  return false;
208
257
  if (recorded.expected) {
209
258
  const value = valueOf(answer);
210
259
  const fields = Object.values(recorded.expected);
211
- if (fields.length === 1 && typeof fields[0] === "string" && answer.trim() === fields[0].trim())
260
+ if (fields.length === 1 && (typeof fields[0] === "string" ? answer.trim() === fields[0].trim() : same(value, fields[0])))
212
261
  return true;
213
262
  if (!value || typeof value !== "object" || Array.isArray(value))
214
263
  return false;
215
264
  return Object.entries(recorded.expected).every(([key, want]) => same(value[key], want));
216
265
  }
217
- return sameAnswer(answer, recorded.answer) ? recorded.correct : false;
266
+ if (recorded.correct)
267
+ return sameAnswer(answer, recorded.answer);
268
+ return recorded.path === "workflow" && recorded.answer !== null && !sameAnswer(answer, recorded.answer);
218
269
  }
270
+ /** Whether the agent's answer to a replayed input is right, the agent's side of a replay's score: the graded run's grade
271
+ * when the answer graded is the agent's (path `agent`, or `handed_back`: the agent finished it). On a run a learned
272
+ * workflow answered alone (`workflow`) the store holds no answer of the agent's on that input, so a right grade is the
273
+ * agent's too and a wrong one counts the agent right: the grade's expected answer is the agent's when it gave one, and
274
+ * with none the agent is the default. So a revision held on runs it served wrong never ties its own wrong answers; it
275
+ * serves again only by answering them right (answerCorrect) or handing them back. */
276
+ export const agentRight = (recorded) => recorded.correct || recorded.path === "workflow";
219
277
  /** The thresholds a workflow's guards serve at, by step label (the guard rows' `node`): every step of the workflow
220
278
  * with a `guard` and a `label` (walkGuards). A label names one step: a learned draft that repeats one never runs
221
279
  * (skills.ts draftProblems, repeatedLabels), so no two steps' rows share a threshold here or in a refit. */
@@ -247,127 +305,12 @@ export function walkGuards(doc, visit) {
247
305
  /** The threshold a soft escalate's row (GuardRow signal `escalate`) is read at: its p is 0 when it fired, else 1, so it
248
306
  * hands back at its serving setting exactly when it fired. */
249
307
  export const SOFT_ESCALATE_THRESHOLD = 0.5;
250
- const JUDGE_WROTE = new Set(["judge"]);
251
- const OTHER_WROTE = new Set(["other"]);
252
- const writersOf = (state, key) => state.get(key) ?? OTHER_WROTE;
253
- /** The writers after any of `states`: each key's writers on every one of them. */
254
- function joinWriters(states) {
255
- const out = new Map();
256
- for (const key of new Set(states.flatMap((state) => [...state.keys()]))) {
257
- const each = states.map((state) => writersOf(state, key));
258
- out.set(key, each.every((writers) => writers === each[0]) ? each[0] : new Set(each.flatMap((writers) => [...writers])));
259
- }
260
- return out;
261
- }
262
- const sameWriters = (a, b) => a.size === b.size && [...a].every(([key, writers]) => { const other = b.get(key); return other !== undefined && other.size === writers.size && [...writers].every((w) => other.has(w)); });
263
- /** Past this nesting, or this many passes over a loop's body, softEscalatesOf reads every escalate as hard. */
264
- const SOFT_WALK_DEPTH = 256;
265
- const SOFT_WALK_PASSES = 16;
266
- class SoftWalkTooDeep extends Error {
267
- }
268
- /** The writers after `value` (a workflow node) runs from `state`, as the DSL runs it (workflow.ts runNodeBody): a chain's
269
- * steps in order; one route branch, any of them, after its `as` (its choice); a parallel's branches each from the same
270
- * state, a key one wrote taking that branch's writer; a loop's body once or more, each pass from the last's; a map's
271
- * body per item on its own copy (with `item`, `item_index`), only its `as` (and `failedAs`) left after; a child
272
- * workflow on its own state, only its `as` left after. A judge writes its `as`; a code node with no `as` may write any
273
- * key. Each escalate's verdict (whether its path's head was last written by a judge on every path to it) lands in
274
- * `verdicts`, false on any pass where it is not. */
275
- function walkWriters(value, state, prefix, verdicts, depth) {
276
- const node = value && typeof value === "object" && !Array.isArray(value) ? value : undefined;
277
- if (!node)
278
- return state;
279
- if (depth > SOFT_WALK_DEPTH)
280
- throw new SoftWalkTooDeep();
281
- const wrote = (key, writers, from = state) => (typeof key === "string" && key !== "" ? new Map(from).set(key, writers) : from);
282
- const asNode = (inner) => (inner && typeof inner === "object" && !Array.isArray(inner) ? inner : undefined);
283
- switch (node.node) {
284
- case "chain":
285
- return (Array.isArray(node.steps) ? node.steps : []).reduce((at, step) => walkWriters(step, at, prefix, verdicts, depth + 1), state);
286
- case "route": {
287
- const routed = wrote(node.as, OTHER_WROTE);
288
- const bodies = asNode(node.branches) ? Object.values(node.branches).map((branch) => asNode(branch)?.body) : [];
289
- return bodies.length ? joinWriters(bodies.map((body) => walkWriters(body, routed, prefix, verdicts, depth + 1))) : routed;
290
- }
291
- case "parallel": {
292
- const outs = (Array.isArray(node.branches) ? node.branches : []).map((branch) => walkWriters(branch, state, prefix, verdicts, depth + 1));
293
- const merged = new Map(state);
294
- for (const key of new Set(outs.flatMap((out) => [...out.keys()]))) {
295
- const writers = outs.filter((out) => out.get(key) !== state.get(key));
296
- if (writers.length)
297
- merged.set(key, joinWriters(writers).get(key));
298
- }
299
- return merged;
300
- }
301
- case "loop": {
302
- let entry = state;
303
- for (let pass = 0; pass < SOFT_WALK_PASSES; pass++) {
304
- const out = walkWriters(node.body, entry, prefix, verdicts, depth + 1);
305
- const next = joinWriters([state, out]);
306
- if (sameWriters(next, entry))
307
- return out;
308
- entry = next;
309
- }
310
- throw new SoftWalkTooDeep();
311
- }
312
- case "map": {
313
- walkWriters(node.body, wrote("item_index", OTHER_WROTE, wrote("item", OTHER_WROTE)), prefix, verdicts, depth + 1);
314
- return wrote(node.failedAs, OTHER_WROTE, wrote(node.as, OTHER_WROTE));
315
- }
316
- case "workflow":
317
- walkWriters(asNode(node.workflow)?.root, new Map(), typeof node.label === "string" ? `${prefix}${node.label}/` : prefix, verdicts, depth + 1);
318
- return wrote(node.as, OTHER_WROTE);
319
- case "judge":
320
- return wrote(node.as, JUDGE_WROTE);
321
- case "escalate": {
322
- if (typeof node.label !== "string")
323
- return state;
324
- let when = node.when;
325
- while (when?.predicate === "not")
326
- when = when.of;
327
- const writers = typeof when?.path === "string" ? writersOf(state, when.path.split(".")[0]) : OTHER_WROTE;
328
- const name = `${prefix}${node.label}`;
329
- verdicts.set(name, (verdicts.get(name) ?? true) && writers.size === 1 && writers.has("judge"));
330
- return state;
331
- }
332
- case "code":
333
- if (typeof node.as === "string" && node.as !== "")
334
- return wrote(node.as, OTHER_WROTE);
335
- return new Map([...state].map(([key, writers]) => [key, writers.has("other") ? writers : new Set([...writers, "other"])]));
336
- case "report":
337
- return wrote("report_markdown", OTHER_WROTE);
338
- case "artifact":
339
- return wrote("artifact", OTHER_WROTE);
340
- default:
341
- // agent, decide, extract (`as`, else the label), pick, sift, call (`as`).
342
- return wrote(typeof node.as === "string" && node.as !== "" ? node.as : node.label, OTHER_WROTE);
343
- }
344
- }
345
- /** The escalate nodes of a workflow document a replay records instead of obeying, by label as the DSL names its step (a
346
- * child's `parent/child`): each whose `when` (under any number of `not`s) reads a field a judge node wrote, the judge
347
- * being the step that last wrote that path's head on every path the run can take to the escalate (walkWriters: a judge
348
- * an extract overwrites, one in only some of a route's branches, one a code node with no `as` may have overwritten, are
349
- * not). A judge's field is one model's opinion of a draft the workflow already wrote: the replay goes on to that draft
350
- * and records whether the escalate fired (WorkflowDeps.softEscalate), so a refit can weigh the check against the drafts
351
- * it stopped (learn/refit.ts). Every other escalate (on a call's result, a route's, the input) stays hard in a replay: it
352
- * stops the run, as serving does. A document too deep to walk has none. */
353
- export function softEscalatesOf(doc) {
354
- const verdicts = new Map();
355
- try {
356
- walkWriters(doc && typeof doc === "object" && !Array.isArray(doc) ? doc.root : undefined, new Map(), "", verdicts, 0);
357
- }
358
- catch (error) {
359
- if (error instanceof SoftWalkTooDeep)
360
- return new Set();
361
- throw error;
362
- }
363
- return new Set([...verdicts].flatMap(([name, soft]) => (soft ? [name] : [])));
364
- }
365
308
  /** The threshold a replay's guards read at: the least the DSL takes (a threshold is in (0, 1), exclusive), so a guard
366
309
  * hands back only when p is null (a signal it reads is missing) or 0. */
367
310
  export const RECORD_ONLY_THRESHOLD = 1e-9;
368
311
  /** A workflow's text with every guard's threshold at RECORD_ONLY_THRESHOLD: each guard still reads its signals and
369
312
  * records its row. What a replay runs, so every step's p is known for the refit and the serving thresholds are applied
370
- * after (replayScore). The text unchanged when it has no guard. */
313
+ * after (learn/refit.ts scoreRows). The text unchanged when it has no guard. */
371
314
  export function recordOnly(workflowText) {
372
315
  let doc;
373
316
  try {
@@ -383,46 +326,14 @@ export function recordOnly(workflowText) {
383
326
  });
384
327
  return any ? JSON.stringify(doc) : workflowText;
385
328
  }
386
- /** Whether a replayed input would have been handed back serving at `thresholds` (by step; a step not named keeps its
387
- * row's): it did not answer alone, or one of its guards read no p, or a p under its step's threshold (a soft escalate
388
- * that fired among them: its row's p 0). */
389
- export function escalates(row, thresholds) {
390
- if (row.outcome !== "answered")
391
- return true;
392
- return row.guards.some((guard) => guard.p === null || guard.p < (thresholds.get(guard.node) ?? guard.threshold));
393
- }
394
- const round = (value) => Math.round(value * 1e4) / 1e4;
395
- /** The replayed inputs a replay scores: those its workflow answered or handed back, never one it failed on or skipped. */
396
- export const scoredRows = (rows) => rows.filter((row) => row.outcome === "answered" || row.outcome === "handed_back");
397
- /** What a replay says at the serving `thresholds`, over the inputs it answered or handed back (scoredRows: a failed or
398
- * skipped one is no evidence): each the workflow would have answered alone graded by answerCorrect, each it would have handed back the
399
- * agent's recorded answer and grade. */
400
- export function replayScore(all, thresholds) {
401
- const rows = scoredRows(all);
402
- let [agentRight, wfRight, agree, handed] = [0, 0, 0, 0];
403
- for (const row of rows) {
404
- if (row.recorded.correct)
405
- agentRight += 1;
406
- if (escalates(row, thresholds)) {
407
- handed += 1;
408
- if (row.recorded.correct)
409
- wfRight += 1;
410
- continue;
411
- }
412
- if (answerCorrect(row.answer, row.recorded))
413
- wfRight += 1;
414
- if (sameAnswer(row.answer, row.recorded.answer))
415
- agree += 1;
416
- }
417
- const n = rows.length;
418
- const [agent_acc, wf_acc] = n ? [round(agentRight / n), round(wfRight / n)] : [0, 0];
419
- return { n, agree, agent_acc, wf_acc, delta: round(wf_acc - agent_acc), handed_back: handed };
420
- }
421
329
  /** Whether a score clears the bar: the workflow's accuracy at most PROMOTE_BAR under the agent path's, and it answered
422
330
  * alone on at least one input (one that hands back every input scores the agent's own accuracy, and would never serve). */
423
- export const clears = (score) => score.delta >= -PROMOTE_BAR - 1e-9 && answersAlone(score);
424
- /** A grade as the API answers it (PUT /runs/{id}/grade). */
331
+ export const clears = (score) => withinBar(score) && answersAlone(score);
332
+ /** Whether a score's workflow is at most PROMOTE_BAR under the agent: clears' bar, and what a replay of a held revision
333
+ * meets on the runs it was held on alone (runner/promotion.ts settle), where a hand-back on each is fine. */
334
+ export const withinBar = (score) => score.delta >= -PROMOTE_BAR - 1e-9;
335
+ /** A grade as the API answers it (PUT /runs/{id}/grade; GET /runs/{id}/learning's `graded`, without `expected`). */
425
336
  export const gradeView = (grade) => ({
426
337
  run_id: grade.run, correct: grade.correct, ...(grade.expected ? { expected: grade.expected } : {}), source: grade.source, graded_at: grade.at,
427
- path: grade.path, workflow: grade.workflow ?? null, workflow_digest: grade.digest ?? null,
338
+ path: grade.path, workflow: grade.workflow ?? null, workflow_digest: grade.digest ?? null, feedback: grade.feedback ?? null,
428
339
  });
@@ -1,5 +1,5 @@
1
1
  import type { GuardRow } from "../learned-nodes.js";
2
- import { type RefitGuard } from "./promote.js";
2
+ import { type RefitGuard, type ReplayRow, type ReplayScore } from "./promote.js";
3
3
  /** A switch set off: the guard or the escalate never hands back. Never a threshold the DSL takes (one is in (0, 1)). */
4
4
  export declare const OFF = 0;
5
5
  export declare const FIT_MIN_ROWS = 30;
@@ -7,32 +7,41 @@ export declare const FIT_MIN_ROWS = 30;
7
7
  export declare const THRESHOLD_MIN = 0.001;
8
8
  export declare const THRESHOLD_MAX = 0.999;
9
9
  export declare const FIT_MIN_WRONG = 5;
10
- /** One row a refit reads: the guard rows of one input (each step's p known up to where it stopped, `escalated` on the one
11
- * that handed it back), whether the workflow answered it alone with every guard passing record-only (`answered`), and if
12
- * so whether that answer was right (`wfCorrect`; null: unknown); `agentCorrect`, the chance the agent's answer to it is
13
- * right (its grade, 1 or 0, when known; the agent's mean accuracy when not). */
10
+ /** One row the scorer (scoreRows) reads: the guard rows of one input (each step's p known up to where it stopped,
11
+ * `escalated` on the one that handed it back), whether the workflow answered it alone with every guard passing
12
+ * record-only (`answered`), and if so whether that answer was right (`wfCorrect`; null: unknown) and said what the
13
+ * agent's recorded answer said (`agrees`); `agentCorrect`, the chance the agent's answer to it is right (1 or 0 when
14
+ * known: a replayed row's agentRight; the agent's mean accuracy when not); `of`, the graded run a replayed row replays (refitRowsOf; a served
15
+ * run's row has none). */
14
16
  export interface RefitRow {
15
17
  readonly guards: readonly GuardRow[];
16
18
  readonly answered: boolean;
17
19
  readonly wfCorrect: boolean | null;
20
+ readonly agrees?: boolean;
18
21
  readonly agentCorrect: number;
22
+ readonly of?: string;
19
23
  }
20
24
  /** What a refit chose: the workflow's new text (undefined when nothing changed), each guard it changed, and the rows'
21
- * accuracies at the new thresholds (`agent_acc`, `wf_acc`, `delta`, `handed_back` of `n`). */
22
- export interface Refit {
25
+ * score at the new thresholds (scoreRows). */
26
+ export interface Refit extends ReplayScore {
23
27
  readonly text?: string;
24
28
  readonly guards: RefitGuard[];
25
- readonly n: number;
26
- readonly agent_acc: number;
27
- readonly wf_acc: number;
28
- readonly delta: number;
29
- readonly handed_back: number;
30
29
  }
30
+ /** A replay's rows as the scorer reads them: those its workflow answered or handed back, never one it failed on or skipped
31
+ * (no evidence either way: the replay ran without what the graded run had, or stopped at a write it never runs). Answered
32
+ * alone only when every guard read a p (record-only, none handed back). */
33
+ export declare const refitRowsOf: (rows: readonly ReplayRow[]) => RefitRow[];
31
34
  /** A logistic fit of y over x (Newton's method with a small ridge, so separable rows stay finite): weights and bias. */
32
35
  export declare function fitLogistic(xs: readonly (readonly number[])[], ys: readonly boolean[], ridge?: number): {
33
36
  w: number[];
34
37
  b: number;
35
38
  };
39
+ /** The one score of a workflow over rows (a replay's, a refit's, a serving check's), each step held to `thresholds` (a step
40
+ * not named, its row's own threshold) with p read through `pOf` (the row's p): `n` rows; `agree`, how many it answered
41
+ * alone with the agent's recorded answer; `agent_acc`, the agent's accuracy; `wf_acc`, the workflow's, a hand-back
42
+ * counting as the agent's answer (scoreAt); `delta` = wf_acc - agent_acc; `handed_back` (it answered n - handed_back
43
+ * alone: answersAlone). Accuracies to 1e-4, as the promotion record keeps them. */
44
+ export declare function scoreRows(rows: readonly RefitRow[], thresholds: ReadonlyMap<string, number>, pOf?: (row: GuardRow) => number | null): ReplayScore;
36
45
  /** The workflow `text`'s guards, and the escalates its replay recorded, refit over `rows` (`now`: the fit's fitted_at). A
37
46
  * guard whose step no row reached keeps what it has, and so does an escalate no row recorded. Nothing changes (no
38
47
  * `text`) when every guard keeps its threshold and fit and every escalate stays. */