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

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 (410) hide show
  1. package/CHANGELOG.md +418 -0
  2. package/README.md +325 -124
  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-BNhZs-jp.css +1 -0
  37. package/dist/console/assets/index-BQv5aOGB.js +700 -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 +84 -0
  44. package/dist/email/account.js +159 -0
  45. package/dist/email/answer.d.ts +10 -0
  46. package/dist/email/answer.js +182 -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 +380 -0
  53. package/dist/email/names.d.ts +20 -0
  54. package/dist/email/names.js +40 -0
  55. package/dist/email/routes.d.ts +9 -0
  56. package/dist/email/routes.js +50 -0
  57. package/dist/email/tools.d.ts +20 -0
  58. package/dist/email/tools.js +517 -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 +9 -2
  93. package/dist/learn/compile.js +52 -16
  94. package/dist/learn/grade.d.ts +4 -0
  95. package/dist/learn/grade.js +4 -1
  96. package/dist/learn/promote.d.ts +106 -51
  97. package/dist/learn/promote.js +82 -189
  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 +26 -8
  109. package/dist/learning-cost.js +17 -7
  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 +33 -2
  118. package/dist/local-models.js +43 -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 +1 -0
  164. package/dist/openapi.js +394 -85
  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 +7 -2
  170. package/dist/port-models.js +5 -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 +11 -0
  180. package/dist/refusal-words.js +13 -2
  181. package/dist/run-launch.d.ts +10 -1
  182. package/dist/run-launch.js +11 -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 +42 -35
  191. package/dist/run-view.js +79 -44
  192. package/dist/runnable.d.ts +13 -5
  193. package/dist/runnable.js +69 -60
  194. package/dist/runner/adapter-loop.d.ts +42 -11
  195. package/dist/runner/adapter-loop.js +651 -247
  196. package/dist/runner/adapter-run-spec.d.ts +37 -12
  197. package/dist/runner/adapter-run-spec.js +139 -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 +94 -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 +37 -13
  217. package/dist/runner/journal-kinds.js +9 -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 +12 -3
  237. package/dist/runner/model-route.js +139 -18
  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 +74 -8
  243. package/dist/runner/promotion.js +365 -124
  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 +554 -130
  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/index.d.ts +1 -1
  278. package/dist/store/native-command-owner.d.ts +10 -0
  279. package/dist/store/native-command-owner.js +47 -0
  280. package/dist/store/postgres/migrations/0012_revision_monid_pin.sql +4 -0
  281. package/dist/store/postgres/migrations/0013_native_command_owner.sql +6 -0
  282. package/dist/store/postgres/migrations/0014_run_cost_upper_bound.sql +6 -0
  283. package/dist/store/postgres/store.js +3 -0
  284. package/dist/store/schema.d.ts +7 -0
  285. package/dist/store/schema.js +20 -1
  286. package/dist/store/sqlite/run-patch.d.ts +8 -1
  287. package/dist/store/sqlite/run-patch.js +14 -3
  288. package/dist/store/sqlite/store.js +133 -18
  289. package/dist/store/store.d.ts +52 -7
  290. package/dist/store-reads/analytics.d.ts +7 -7
  291. package/dist/store-reads/analytics.js +23 -5
  292. package/dist/store-reads/cap.js +8 -6
  293. package/dist/store-reads/journal.d.ts +5 -0
  294. package/dist/store-reads/journal.js +85 -41
  295. package/dist/store-reads/learning.d.ts +85 -5
  296. package/dist/store-reads/learning.js +45 -17
  297. package/dist/store-reads/runs.d.ts +5 -0
  298. package/dist/store-reads/runs.js +3 -2
  299. package/dist/store-view.js +75 -21
  300. package/dist/template-eval.d.ts +73 -11
  301. package/dist/template-eval.js +535 -75
  302. package/dist/templates.d.ts +70 -20
  303. package/dist/templates.js +191 -34
  304. package/dist/text-urls.d.ts +38 -0
  305. package/dist/text-urls.js +170 -0
  306. package/dist/timeline/index.d.ts +3 -2
  307. package/dist/timeline/index.js +3 -2
  308. package/dist/timeline/labels.d.ts +5 -2
  309. package/dist/timeline/labels.js +29 -6
  310. package/dist/timeline/origin.d.ts +1 -1
  311. package/dist/timeline/origin.js +2 -2
  312. package/dist/timeline/render-text.js +1 -1
  313. package/dist/timeline/timeline.d.ts +66 -4
  314. package/dist/timeline/timeline.js +160 -30
  315. package/dist/timeline/util.d.ts +5 -0
  316. package/dist/timeline/util.js +44 -8
  317. package/dist/timeline/web-calls.d.ts +17 -0
  318. package/dist/timeline/web-calls.js +106 -0
  319. package/dist/tool-gate.d.ts +42 -42
  320. package/dist/tool-gate.js +97 -91
  321. package/dist/tool-list.d.ts +4 -1
  322. package/dist/tool-list.js +8 -1
  323. package/dist/tool-names.d.ts +18 -13
  324. package/dist/tool-names.js +19 -17
  325. package/dist/tool-plane.d.ts +17 -13
  326. package/dist/tool-plane.js +12 -5
  327. package/dist/tools.d.ts +44 -16
  328. package/dist/tools.js +28 -28
  329. package/dist/trigger-adapter.d.ts +85 -5
  330. package/dist/trigger-adapter.js +6 -0
  331. package/dist/trigger-adapters.js +3 -1
  332. package/dist/trigger-composio.d.ts +10 -0
  333. package/dist/trigger-composio.js +122 -0
  334. package/dist/trigger-email.d.ts +4 -0
  335. package/dist/trigger-email.js +228 -0
  336. package/dist/trigger-signatures.d.ts +6 -10
  337. package/dist/trigger-signatures.js +16 -16
  338. package/dist/trigger-targets.d.ts +1 -1
  339. package/dist/trigger-targets.js +4 -3
  340. package/dist/trigger-webhook.js +6 -4
  341. package/dist/triggers.d.ts +94 -9
  342. package/dist/triggers.js +263 -62
  343. package/dist/uploads.d.ts +9 -0
  344. package/dist/uploads.js +27 -0
  345. package/dist/validate.js +1 -7
  346. package/dist/web-fake.d.ts +56 -0
  347. package/dist/web-fake.js +102 -0
  348. package/dist/web-fetch-direct.d.ts +3 -0
  349. package/dist/web-fetch-direct.js +4 -1
  350. package/dist/web-fetch.d.ts +5 -1
  351. package/dist/web-fetch.js +8 -31
  352. package/dist/web-search.d.ts +51 -2
  353. package/dist/web-search.js +116 -18
  354. package/dist/webhooks.d.ts +2 -1
  355. package/dist/webhooks.js +5 -1
  356. package/dist/workflow/node-runner.js +10 -2
  357. package/dist/workspace/archil-claim.d.ts +3 -0
  358. package/dist/workspace/archil-claim.js +9 -2
  359. package/dist/workspace/computer-deps.d.ts +22 -9
  360. package/dist/workspace/computer-deps.js +78 -37
  361. package/dist/workspace/computer-for-claim.d.ts +54 -4
  362. package/dist/workspace/computer-for-claim.js +179 -39
  363. package/dist/workspace/computer-janitor.d.ts +10 -3
  364. package/dist/workspace/computer-janitor.js +96 -29
  365. package/dist/workspace/computer-kept.d.ts +82 -0
  366. package/dist/workspace/computer-kept.js +147 -0
  367. package/dist/workspace/computer-mcp.js +4 -3
  368. package/dist/workspace/computer-ports.d.ts +23 -3
  369. package/dist/workspace/computer-ports.js +115 -5
  370. package/dist/workspace/index.d.ts +1 -2
  371. package/dist/workspace/index.js +1 -2
  372. package/dist/workspace/kind-skills/desktop/SKILL.md +25 -18
  373. package/dist/workspace/kind-skills/phone/SKILL.md +28 -17
  374. package/dist/workspace/resolve-computer.d.ts +15 -3
  375. package/dist/workspace/resolve-computer.js +10 -7
  376. package/dist/workspace/revision-skills.d.ts +6 -2
  377. package/dist/workspace/revision-skills.js +12 -5
  378. package/dist/workspace/run-end-computers.d.ts +30 -0
  379. package/dist/workspace/run-end-computers.js +137 -0
  380. package/dist/workspace/run-tools.d.ts +28 -29
  381. package/dist/workspace/run-tools.js +29 -30
  382. package/dist/wrap-up.d.ts +19 -5
  383. package/dist/wrap-up.js +31 -4
  384. package/package.json +13 -9
  385. package/skills/agentrun-serve/SKILL.md +248 -10
  386. package/templates/README.md +117 -23
  387. package/templates/TOOLS.md +168 -0
  388. package/templates/code-review/agent.md +18 -4
  389. package/templates/code-review/evals/mentions-defused/case.yaml +1 -1
  390. package/templates/code-review/evals/off-by-one/graders/no-repo-reads.md +2 -1
  391. package/templates/code-review/evals/other-repo/case.yaml +1 -1
  392. package/templates/code-review/evals/pasted-by-reference/case.yaml +5 -0
  393. package/templates/code-review/evals/pasted-by-reference/graders/by-reference.md +15 -0
  394. package/templates/code-review/evals/pasted-by-reference/graders/head-sha.md +6 -0
  395. package/templates/code-review/evals/pasted-by-reference/prompt.md +31 -0
  396. package/templates/code-review/skills/review/DEEP.md +2 -2
  397. package/templates/code-review/skills/review/REVIEWER.md +2 -2
  398. package/templates/code-review/skills/review/SKILL.md +7 -3
  399. package/templates/release-notes/evals/draft-not-publish/case.yaml +1 -1
  400. package/templates/release-notes/evals/issue-write-refused/case.yaml +1 -1
  401. package/templates/release-notes/evals/late-arrival/case.yaml +1 -1
  402. package/templates/release-notes/evals/other-repo/case.yaml +1 -1
  403. package/dist/console/assets/index-CjqQirbo.css +0 -1
  404. package/dist/console/assets/index-b6-XY5A9.js +0 -576
  405. package/dist/host-calls.d.ts +0 -20
  406. package/dist/host-calls.js +0 -12
  407. package/dist/runner/adapters/pi-computer.d.ts +0 -9
  408. package/dist/runner/adapters/pi-computer.js +0 -51
  409. package/dist/workspace/computer-script.d.ts +0 -53
  410. 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,30 +62,51 @@ export const putGrade = async (settings, agentId, grade) => {
58
62
  });
59
63
  return kept;
60
64
  };
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
+ };
61
73
  /** The agent's promotion records, oldest first. */
62
74
  export const promotionsOf = async (settings, agentId) => itemsOf(await settings.get(promotionsKey(agentId)));
63
75
  /** The newest record of `workflow` at `digest`, or undefined. */
64
76
  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
77
+ /** The champion of `workflow`: the newest record of the digest promoted last (a digest's place is its latest win's,
78
+ * winsOf), while that digest's newest record still says promote. A newer revision replaying or held never stops it; a
67
79
  * 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. */
80
+ * (then the champion), or its own graded runs hold it (source graded: no champion until a revision is promoted). A held
81
+ * revision its owner replays (runner/promotion.ts askReplay) and the replay promotes wins again: the champion from then.
82
+ * The money rule stops it on its own (runner/workflow-first.ts). Undefined: none. */
70
83
  export function championOf(records, workflow) {
71
84
  const last = lastPromotedOf(records, workflow);
72
85
  const now = last === undefined ? undefined : promotionOf(records, workflow, last);
73
86
  return now?.decided === "promote" ? now : undefined;
74
87
  }
75
- /** The digest of `workflow` promoted last: the one whose first promote record is the newest of every digest's first. */
88
+ /** Each promote record's win, by index: the index of the promote record that began its digest's run of promote records
89
+ * (its first, or the first after a record of that digest that was not promote: a replay that promoted a held revision).
90
+ * A graded check's mark (a promote after a promote, runner/promotion.ts stillServing) is never a win of its own. */
91
+ function winsOf(records) {
92
+ const wins = new Map();
93
+ const streak = new Map();
94
+ records.forEach((one, i) => {
95
+ const at = JSON.stringify([one.workflow, one.digest]);
96
+ if (one.decided !== "promote")
97
+ return void streak.delete(at);
98
+ if (!streak.has(at))
99
+ streak.set(at, i);
100
+ wins.set(i, streak.get(at));
101
+ });
102
+ return wins;
103
+ }
104
+ /** The digest of `workflow` promoted last: the one whose latest win (winsOf) is the newest of every digest's. */
76
105
  function lastPromotedOf(records, workflow) {
77
- const seen = new Set();
78
106
  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
- }
107
+ for (const [i, win] of winsOf(records))
108
+ if (i === win && records[i].workflow === workflow)
109
+ last = records[i].digest;
85
110
  return last;
86
111
  }
87
112
  /** The revisions of `workflow` other than `digest` whose newest record still says replaying, newest first: at most one
@@ -110,13 +135,14 @@ export function servingOf(records) {
110
135
  /** The promotions list as it leaves (GET /agents/{id}/learning/promotions): the records and what serves (servingOf), every
111
136
  * text in them cut by `shown` (learning.ts secretsShown, as the other learning reads cut theirs), at any depth and under
112
137
  * 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. */
138
+ * (`shown.text`: a reason, a refit guard's label, a digest, a run id). Numbers and the rest as they are. A replaying
139
+ * record's `launch` (a run launch: its notes, model and plane) never leaves: aggregates only. */
114
140
  export function promotionsShown(records, shown) {
115
141
  const cut = (value, key) => typeof value === "string" ? (key === "workflow" ? shown.name(value) : shown.text(value))
116
142
  : Array.isArray(value) ? value.map((one) => cut(one))
117
143
  : value !== null && typeof value === "object" ? Object.fromEntries(Object.entries(value).map(([field, one]) => [field, cut(one, field)]))
118
144
  : value;
119
- return { promotions: records.map((one) => cut(one)), serving: servingOf(records).map((one) => cut(one)) };
145
+ return { promotions: records.map(({ launch: _, ...one }) => cut(one)), serving: servingOf(records).map((one) => cut(one)) };
120
146
  }
121
147
  /** Keeps `record` as the newest of its workflow at its digest, when `still` holds of the one there now (none: undefined)
122
148
  * and of every record kept (`items`: a check across revisions, one challenger at a time, in the same guarded write): so
@@ -136,26 +162,25 @@ export async function recordPromotion(settings, agentId, record, still) {
136
162
  }
137
163
  /** The newest records, at most PROMOTIONS_KEPT, in order, with each workflow's champion (championOf) the same after as
138
164
  * 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). */
165
+ * still see promoted (a promote record kept keeps its win, winsOf, beside it, so an older revision's late graded check
166
+ * never reads as a newer promotion), and the digest promoted last, its latest win and its newest record (so a held last
167
+ * revision is never trimmed into handing the champion back). The kept records are a run of the newest plus those wins,
168
+ * 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
169
+ * than every record of that run, so a run is never joined to an older one by the trim. */
142
170
  export function trimPromotions(items) {
143
171
  if (items.length <= PROMOTIONS_KEPT)
144
172
  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);
173
+ const wins = winsOf(items);
174
+ const placeOf = (i) => wins.get(i);
152
175
  const keep = new Set();
153
176
  for (const workflow of new Set(items.map((one) => one.workflow))) {
154
177
  const last = lastPromotedOf(items, workflow);
155
178
  if (last === undefined)
156
179
  continue;
157
- keep.add(firstPromote.get(JSON.stringify([workflow, last])));
158
- keep.add(items.findLastIndex((one) => one.workflow === workflow && one.digest === last));
180
+ const newest = items.findLastIndex((one) => one.workflow === workflow && one.digest === last);
181
+ // Its latest win: the newest win of that digest.
182
+ keep.add(Math.max(...[...wins].filter(([i, win]) => i === win && items[i].workflow === workflow && items[i].digest === last).map(([i]) => i)));
183
+ keep.add(newest);
159
184
  }
160
185
  for (let i = items.length - 1, room = PROMOTIONS_KEPT - keep.size; i >= 0 && room > 0; i--) {
161
186
  if (keep.has(i))
@@ -171,11 +196,15 @@ export function trimPromotions(items) {
171
196
  }
172
197
  return items.filter((_, i) => keep.has(i));
173
198
  }
174
- /** An answer's value: its text as JSON when it is JSON, else its last fenced json block, else its trimmed text. */
199
+ /** The text inside an answer that is one unlabelled fence and nothing else, or undefined. */
200
+ const WHOLE_FENCE = /^ {0,3}(`{3,}|~{3,})[ \t]*\r?\n([\s\S]*?)\r?\n {0,3}\1[`~]*[ \t]*$/;
201
+ /** An answer's value: its text as JSON when it is JSON (bare, or the whole of one unlabelled fence), else its last fenced
202
+ * json block, else its trimmed text. */
175
203
  function valueOf(answer) {
176
204
  const text = answer.trim();
205
+ const inner = WHOLE_FENCE.exec(text)?.[2];
177
206
  try {
178
- return JSON.parse(text);
207
+ return JSON.parse(inner ?? text);
179
208
  }
180
209
  catch {
181
210
  const block = answerJson(text);
@@ -197,25 +226,36 @@ function same(a, b) {
197
226
  }
198
227
  /** Whether two answers say the same: equal as JSON values, else as trimmed text. */
199
228
  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). */
229
+ /** Whether the workflow's `answer` to a graded input is right by its grade: with the grade's expected fields, when its JSON
230
+ * holds each of them (or, one field expected, when the answer is that field's value: a workflow whose output is one
231
+ * field answers with its text, or its JSON text when it is no text, learned-workflow.ts learnedOutput). Else, graded
232
+ * right, when it says what the graded run said. Graded wrong: on a run a learned workflow answered alone (`workflow`),
233
+ * when it says anything else (the bar on a wrong grade is the grade: saying what was graded wrong again is never a tie),
234
+ * and wrong when what it said could not be read (nothing says it differs); on a run the agent answered, wrong whatever it
235
+ * says (another answer proves nothing: a neutral tie with the agent's, agentRight). */
205
236
  export function answerCorrect(answer, recorded) {
206
237
  if (answer === null)
207
238
  return false;
208
239
  if (recorded.expected) {
209
240
  const value = valueOf(answer);
210
241
  const fields = Object.values(recorded.expected);
211
- if (fields.length === 1 && typeof fields[0] === "string" && answer.trim() === fields[0].trim())
242
+ if (fields.length === 1 && (typeof fields[0] === "string" ? answer.trim() === fields[0].trim() : same(value, fields[0])))
212
243
  return true;
213
244
  if (!value || typeof value !== "object" || Array.isArray(value))
214
245
  return false;
215
246
  return Object.entries(recorded.expected).every(([key, want]) => same(value[key], want));
216
247
  }
217
- return sameAnswer(answer, recorded.answer) ? recorded.correct : false;
248
+ if (recorded.correct)
249
+ return sameAnswer(answer, recorded.answer);
250
+ return recorded.path === "workflow" && recorded.answer !== null && !sameAnswer(answer, recorded.answer);
218
251
  }
252
+ /** 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
253
+ * when the answer graded is the agent's (path `agent`, or `handed_back`: the agent finished it). On a run a learned
254
+ * workflow answered alone (`workflow`) the store holds no answer of the agent's on that input, so a right grade is the
255
+ * 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
256
+ * with none the agent is the default. So a revision held on runs it served wrong never ties its own wrong answers; it
257
+ * serves again only by answering them right (answerCorrect) or handing them back. */
258
+ export const agentRight = (recorded) => recorded.correct || recorded.path === "workflow";
219
259
  /** The thresholds a workflow's guards serve at, by step label (the guard rows' `node`): every step of the workflow
220
260
  * with a `guard` and a `label` (walkGuards). A label names one step: a learned draft that repeats one never runs
221
261
  * (skills.ts draftProblems, repeatedLabels), so no two steps' rows share a threshold here or in a refit. */
@@ -247,127 +287,12 @@ export function walkGuards(doc, visit) {
247
287
  /** 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
288
  * hands back at its serving setting exactly when it fired. */
249
289
  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
290
  /** The threshold a replay's guards read at: the least the DSL takes (a threshold is in (0, 1), exclusive), so a guard
366
291
  * hands back only when p is null (a signal it reads is missing) or 0. */
367
292
  export const RECORD_ONLY_THRESHOLD = 1e-9;
368
293
  /** A workflow's text with every guard's threshold at RECORD_ONLY_THRESHOLD: each guard still reads its signals and
369
294
  * 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. */
295
+ * after (learn/refit.ts scoreRows). The text unchanged when it has no guard. */
371
296
  export function recordOnly(workflowText) {
372
297
  let doc;
373
298
  try {
@@ -383,46 +308,14 @@ export function recordOnly(workflowText) {
383
308
  });
384
309
  return any ? JSON.stringify(doc) : workflowText;
385
310
  }
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
311
  /** Whether a score clears the bar: the workflow's accuracy at most PROMOTE_BAR under the agent path's, and it answered
422
312
  * 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). */
313
+ export const clears = (score) => withinBar(score) && answersAlone(score);
314
+ /** Whether a score's workflow is at most PROMOTE_BAR under the agent: clears' bar, and what a replay of a held revision
315
+ * meets on the runs it was held on alone (runner/promotion.ts settle), where a hand-back on each is fine. */
316
+ export const withinBar = (score) => score.delta >= -PROMOTE_BAR - 1e-9;
317
+ /** A grade as the API answers it (PUT /runs/{id}/grade; GET /runs/{id}/learning's `graded`, without `expected`). */
425
318
  export const gradeView = (grade) => ({
426
319
  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,
320
+ path: grade.path, workflow: grade.workflow ?? null, workflow_digest: grade.digest ?? null, feedback: grade.feedback ?? null,
428
321
  });
@@ -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. */
@@ -1,4 +1,15 @@
1
- import { PROMOTE_BAR, SOFT_ESCALATE_THRESHOLD, softEscalatesOf, walkGuards } from "./promote.js";
1
+ // Threshold refit: a learned workflow's step guards (runner/step-guard.ts) re-chosen from graded rows, so a guard set too
2
+ // high (October's receipt hold: 46 of 54 revisions held) or too low is moved to where the rows say it belongs. Each guarded
3
+ // step's threshold becomes the cheapest one (the fewest hand-backs) that keeps the workflow's accuracy over the rows within
4
+ // PROMOTE_BAR of the agent's; a `both` guard's fit (logistic over [self, jev], Platt-style) is refit first when the rows
5
+ // hold at least FIT_MIN_ROWS with at least FIT_MIN_WRONG wrong, else kept. A guard may also be switched off (OFF: it never
6
+ // hands back, and the revision drops it): the cheapest setting of all, so a guard whose p says nothing of right and wrong
7
+ // goes. So may an escalate node a replay recorded instead of obeying (the DSL's softEscalates), on the same rule:
8
+ // one that stops right drafts is dropped from its chain, one that stops wrong ones stays. Pure: the rows in, the new
9
+ // workflow text and what changed out; runner/promotion.ts reads the rows (replays and graded served runs) and writes the
10
+ // revision.
11
+ import { softEscalates } from "@parcha/agentrun-dsl";
12
+ import { agentRight, answerCorrect, PROMOTE_BAR, sameAnswer, SOFT_ESCALATE_THRESHOLD, walkGuards } from "./promote.js";
2
13
  import { workflowNodes } from "./workflow-nodes.js";
3
14
  /** A switch set off: the guard or the escalate never hands back. Never a threshold the DSL takes (one is in (0, 1)). */
4
15
  export const OFF = 0;
@@ -7,6 +18,18 @@ export const FIT_MIN_ROWS = 30;
7
18
  export const THRESHOLD_MIN = 0.001;
8
19
  export const THRESHOLD_MAX = 0.999;
9
20
  export const FIT_MIN_WRONG = 5;
21
+ /** A replay's rows as the scorer reads them: those its workflow answered or handed back, never one it failed on or skipped
22
+ * (no evidence either way: the replay ran without what the graded run had, or stopped at a write it never runs). Answered
23
+ * alone only when every guard read a p (record-only, none handed back). */
24
+ export const refitRowsOf = (rows) => rows.flatMap((row) => {
25
+ if (row.outcome !== "answered" && row.outcome !== "handed_back")
26
+ return [];
27
+ const answered = row.outcome === "answered" && row.guards.every((guard) => guard.p !== null);
28
+ return [{
29
+ of: row.of, guards: row.guards, answered, agentCorrect: agentRight(row.recorded) ? 1 : 0,
30
+ wfCorrect: answered ? answerCorrect(row.answer, row.recorded) : null, agrees: answered && sameAnswer(row.answer, row.recorded.answer),
31
+ }];
32
+ });
10
33
  const sigmoid = (z) => 1 / (1 + Math.exp(-z));
11
34
  const round = (value, places = 4) => Math.round(value * 10 ** places) / 10 ** places;
12
35
  /** A logistic fit of y over x (Newton's method with a small ridge, so separable rows stay finite): weights and bias. */
@@ -61,14 +84,15 @@ function solve(a, b) {
61
84
  }
62
85
  return m.map((row, i) => row[n] / row[i]);
63
86
  }
64
- /** The rows' accuracies with p read through `pOf` and each step held to `thresholds`: a row any guard of hands back on
87
+ /** The rows' score, unrounded (scoreRows rounds it; the refit's search compares it as it is), with p read through `pOf`
88
+ * and each step held to `thresholds`: a row any guard of hands back on
65
89
  * takes the agent's answer; one that answered alone, its grade. A row that did not answer alone because a guard handed
66
90
  * it back (one of its guards escalated) and that no guard would hand back at `thresholds` would have answered with an
67
91
  * answer nobody read: counted wrong, so a threshold never comes down past hand-backs with no evidence of what the
68
92
  * workflow says there. One that did not answer for another reason (a step failed, an escalate node) takes the agent's
69
93
  * answer at every threshold. A switch OFF never hands back, a missing p or not. */
70
- function scoreAt(rows, pOf, thresholds) {
71
- let [wf, agent, handed] = [0, 0, 0];
94
+ function scoreAt(rows, thresholds, pOf) {
95
+ let [wf, agent, handed, agree] = [0, 0, 0, 0];
72
96
  for (const row of rows) {
73
97
  agent += row.agentCorrect;
74
98
  const under = row.guards.some((guard) => {
@@ -82,12 +106,25 @@ function scoreAt(rows, pOf, thresholds) {
82
106
  if (under || (!known && !row.guards.some((guard) => guard.escalated))) {
83
107
  handed += 1;
84
108
  wf += row.agentCorrect;
109
+ continue;
85
110
  }
86
- else if (known && row.wfCorrect)
111
+ if (known && row.wfCorrect)
87
112
  wf += 1;
113
+ if (row.agrees)
114
+ agree += 1;
88
115
  }
89
116
  const n = rows.length || 1;
90
- return { wf_acc: wf / n, agent_acc: agent / n, handed_back: handed };
117
+ return { n: rows.length, agree, wf_acc: wf / n, agent_acc: agent / n, handed_back: handed };
118
+ }
119
+ /** 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
120
+ * not named, its row's own threshold) with p read through `pOf` (the row's p): `n` rows; `agree`, how many it answered
121
+ * alone with the agent's recorded answer; `agent_acc`, the agent's accuracy; `wf_acc`, the workflow's, a hand-back
122
+ * counting as the agent's answer (scoreAt); `delta` = wf_acc - agent_acc; `handed_back` (it answered n - handed_back
123
+ * alone: answersAlone). Accuracies to 1e-4, as the promotion record keeps them. */
124
+ export function scoreRows(rows, thresholds, pOf = (row) => row.p) {
125
+ const score = scoreAt(rows, thresholds, pOf);
126
+ const [agent_acc, wf_acc] = [round(score.agent_acc), round(score.wf_acc)];
127
+ return { n: score.n, agree: score.agree, agent_acc, wf_acc, delta: round(wf_acc - agent_acc), handed_back: score.handed_back };
91
128
  }
92
129
  /** How good one raise is, compared first to last: any accuracy gained before none; then accuracy per input handed back
93
130
  * (a gain that hands back nothing first); then the fewest handed back. A raise that gains nothing ranks by its loss. */
@@ -98,10 +135,10 @@ const rankAbove = (a, b) => {
98
135
  return a[i] > b[i];
99
136
  return false;
100
137
  };
101
- /** The escalates of `doc` a refit may switch off: each a replay records (softEscalatesOf) that is a step of a chain with
138
+ /** The escalates of `doc` a refit may switch off: each a replay records (softEscalates) that is a step of a chain with
102
139
  * a step that is no escalate, so the chain keeps a step once they are dropped. */
103
140
  function switchableEscalates(doc) {
104
- const soft = softEscalatesOf(doc);
141
+ const soft = softEscalates(doc);
105
142
  const out = new Set();
106
143
  for (const { node, prefix } of workflowNodes(doc)) {
107
144
  if (node.node !== "chain" || !Array.isArray(node.steps))
@@ -124,7 +161,7 @@ export function refitGuards(text, rows, now) {
124
161
  doc = JSON.parse(text);
125
162
  }
126
163
  catch {
127
- return { guards: [], n: rows.length, agent_acc: 0, wf_acc: 0, delta: 0, handed_back: 0 };
164
+ return { guards: [], n: rows.length, agree: 0, agent_acc: 0, wf_acc: 0, delta: 0, handed_back: 0 };
128
165
  }
129
166
  const guards = [];
130
167
  walkGuards(doc, (label, guard, node) => guards.push({ label, guard, node }));
@@ -153,8 +190,8 @@ export function refitGuards(text, rows, now) {
153
190
  };
154
191
  const was = new Map(guards.map(({ label, guard }) => [label, typeof guard.threshold === "number" ? guard.threshold : OFF]));
155
192
  const thresholds = new Map([...was, ...escalates.map((label) => [label, SOFT_ESCALATE_THRESHOLD])]);
156
- const agentAcc = scoreAt(rows, pOf, thresholds).agent_acc;
157
- const meets = (at) => scoreAt(rows, pOf, at).wf_acc >= agentAcc - PROMOTE_BAR - 1e-9;
193
+ const agentAcc = scoreAt(rows, thresholds, pOf).agent_acc;
194
+ const meets = (at) => scoreAt(rows, at, pOf).wf_acc >= agentAcc - PROMOTE_BAR - 1e-9;
158
195
  // Each guard's candidates: OFF (hands back nothing), THRESHOLD_MIN (nothing but a missing p or one under it), each p its
159
196
  // rows read within the DSL's range, its own threshold, and THRESHOLD_MAX; a step no row reached keeps its own. Each
160
197
  // recorded escalate's: off or on.
@@ -171,11 +208,11 @@ export function refitGuards(text, rows, now) {
171
208
  // handing back every input, and the step whose guard tells right from wrong is then never raised.
172
209
  for (const [label, candidates] of candidatesOf)
173
210
  thresholds.set(label, candidates[0]);
174
- for (let now = scoreAt(rows, pOf, thresholds); !meets(thresholds);) {
211
+ for (let now = scoreAt(rows, thresholds, pOf); !meets(thresholds);) {
175
212
  let best;
176
213
  for (const [label, candidates] of candidatesOf) {
177
214
  for (const at of candidates.filter((c) => c > thresholds.get(label))) {
178
- const next = scoreAt(rows, pOf, new Map([...thresholds, [label, at]]));
215
+ const next = scoreAt(rows, new Map([...thresholds, [label, at]]), pOf);
179
216
  const rank = raiseRank(next.wf_acc - now.wf_acc, next.handed_back - now.handed_back);
180
217
  if (!best || rankAbove(rank, best.rank))
181
218
  best = { label, at, rank, next };
@@ -223,14 +260,5 @@ export function refitGuards(text, rows, now) {
223
260
  for (const label of off)
224
261
  changed.push({ node: label, was: null, threshold: null, escalate: true });
225
262
  }
226
- const score = scoreAt(rows, pOf, thresholds);
227
- return {
228
- ...(changed.length ? { text: JSON.stringify(doc) } : {}),
229
- guards: changed,
230
- n: rows.length,
231
- agent_acc: round(score.agent_acc),
232
- wf_acc: round(score.wf_acc),
233
- delta: round(score.wf_acc - score.agent_acc),
234
- handed_back: score.handed_back,
235
- };
263
+ return { ...(changed.length ? { text: JSON.stringify(doc) } : {}), guards: changed, ...scoreRows(rows, thresholds, pOf) };
236
264
  }
@@ -7,7 +7,7 @@
7
7
  // hit, recorded or already run in this replay, costs nothing and is never sent again.
8
8
  // - A write is NEVER run in a replay: it is answered from the graded run's recording when a call there has the same
9
9
  // tool and canonical args (its answer, an error included), else the replay of that input stops there and is skipped
10
- // (SKIPPED_WRITE): no evidence either way, never scored wrong or failed (learn/promote.ts scoredRows).
10
+ // (SKIPPED_WRITE): no evidence either way, never scored wrong or failed (learn/refit.ts refitRowsOf).
11
11
  // The recording is the graded run's own journal (a CLI's record shaped as pi's), each tool call by the wire name the
12
12
  // harness called it by (catalog.ts wireName): a call node names its tool by catalog name, matched through the same map.
13
13
  // Prior art: MCP's tool annotations (readOnlyHint, destructiveHint, 2025-03-26 spec) for the read/write line; VCR-style
@@ -274,11 +274,6 @@ export declare const LEARNED_MAX_TURNS = 8;
274
274
  * learned runner cannot run (LEARNED_NODES: a judge's on a run with no `decisions`; with them, one whose provider the run
275
275
  * lacks or its gate holds to ask or deny). With no list it
276
276
  * fails closed: a draft the DSL refuses only once given an empty list names a tool. */
277
- /** The MCP tools set to ask a learned call would ask its approver about (`asked`, learnedDraftTools), and whether a card
278
- * names them all (`fits`: learned-workflow.ts learnedAskFits, the gate's own rule, handed in so it is never copied). */
279
- export type DraftAsk = {
280
- readonly asked: readonly string[];
281
- readonly fits: (asks: readonly string[]) => boolean;
282
- };
283
- export declare function draftProblems(dsl: DraftReader, text: string, tools?: readonly string[], gated?: Readonly<Record<string, string>>, deniedBy?: Readonly<Record<string, SettingFrom>>, ask?: DraftAsk, decisions?: DraftDecisions): DraftProblem[];
277
+ /** `decisions` is the managed host's registry; `adapters` is an explicit backend callback contract, when supplied. */
278
+ export declare function draftProblems(dsl: DraftReader, text: string, tools?: readonly string[], gated?: Readonly<Record<string, string>>, deniedBy?: Readonly<Record<string, SettingFrom>>, decisions?: DraftDecisions, adapters?: readonly ("runNode" | "runJudge")[]): DraftProblem[];
284
279
  export {};