@parcha/agentrun-serve 0.1.0-beta.58 → 0.1.0-beta.59

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 (375) hide show
  1. package/CHANGELOG.md +325 -1
  2. package/README.md +332 -88
  3. package/dist/acp-package.d.ts +5 -1
  4. package/dist/acp-package.js +19 -2
  5. package/dist/adapters.d.ts +23 -2
  6. package/dist/adapters.js +106 -33
  7. package/dist/agents-store.js +1 -1
  8. package/dist/answer-text.d.ts +3 -0
  9. package/dist/answer-text.js +22 -0
  10. package/dist/attach-conformance.js +5 -4
  11. package/dist/box-paths.d.ts +3 -2
  12. package/dist/box-paths.js +5 -4
  13. package/dist/browser-computer.d.ts +3 -8
  14. package/dist/browser-computer.js +12 -35
  15. package/dist/browser-cost.js +94 -20
  16. package/dist/capacity.d.ts +1 -1
  17. package/dist/capacity.js +3 -3
  18. package/dist/chatgpt-plan.d.ts +5 -0
  19. package/dist/chatgpt-plan.js +25 -11
  20. package/dist/claude-login-token.d.ts +15 -11
  21. package/dist/claude-login-token.js +167 -107
  22. package/dist/claude-login.d.ts +76 -31
  23. package/dist/claude-login.js +426 -79
  24. package/dist/claude-sign-in.d.ts +3 -0
  25. package/dist/claude-sign-in.js +10 -1
  26. package/dist/cli.js +323 -292
  27. package/dist/composio-made.d.ts +1 -6
  28. package/dist/composio-made.js +1 -6
  29. package/dist/composio-owners.d.ts +26 -0
  30. package/dist/composio-owners.js +36 -0
  31. package/dist/composio-preset.d.ts +56 -23
  32. package/dist/composio-preset.js +173 -77
  33. package/dist/composio-triggers.d.ts +4 -4
  34. package/dist/composio-triggers.js +6 -7
  35. package/dist/compute-cost.js +11 -2
  36. package/dist/computer-attach.d.ts +6 -3
  37. package/dist/computer-attach.js +41 -29
  38. package/dist/computer-held-routes.d.ts +1 -1
  39. package/dist/computer-held-routes.js +9 -4
  40. package/dist/computer-keys.d.ts +11 -1
  41. package/dist/computer-keys.js +26 -1
  42. package/dist/computer-record.js +3 -1
  43. package/dist/computer-view.d.ts +5 -3
  44. package/dist/computer-view.js +10 -6
  45. package/dist/computer-viewer.js +36 -7
  46. package/dist/computers.d.ts +2 -1
  47. package/dist/computers.js +6 -3
  48. package/dist/console/THIRD-PARTY-NOTICES.txt +50 -0
  49. package/dist/console/assets/backend-BztxEAvb.js +166 -0
  50. package/dist/console/assets/index-LwNL4L9q.js +619 -0
  51. package/dist/console/assets/share-L84tlUDW.js +6 -0
  52. package/dist/console/assets/styles-CaRKlHke.js +291 -0
  53. package/dist/console/assets/styles-DQo8rzY-.css +1 -0
  54. package/dist/console/index.html +4 -3
  55. package/dist/console/share.html +17 -0
  56. package/dist/decisions.d.ts +17 -7
  57. package/dist/decisions.js +41 -15
  58. package/dist/dial-relay.js +10 -4
  59. package/dist/docker-daemon.js +4 -5
  60. package/dist/docker-host-addresses.js +29 -11
  61. package/dist/email/account.d.ts +6 -0
  62. package/dist/email/account.js +6 -0
  63. package/dist/email/authentication.d.ts +21 -0
  64. package/dist/email/authentication.js +106 -0
  65. package/dist/email/email-faux.d.ts +11 -2
  66. package/dist/email/email-faux.js +21 -5
  67. package/dist/email/mail.d.ts +25 -0
  68. package/dist/email/mail.js +58 -2
  69. package/dist/email/names.js +2 -2
  70. package/dist/email/tools.d.ts +9 -4
  71. package/dist/email/tools.js +95 -13
  72. package/dist/env-switch.d.ts +12 -2
  73. package/dist/env-switch.js +23 -3
  74. package/dist/errors.d.ts +1 -0
  75. package/dist/errors.js +2 -0
  76. package/dist/fal-price.d.ts +11 -1
  77. package/dist/fal-price.js +33 -0
  78. package/dist/faux-computers.js +1 -1
  79. package/dist/filed-answer.d.ts +2 -2
  80. package/dist/filed-answer.js +2 -2
  81. package/dist/graph-served.d.ts +10 -5
  82. package/dist/graph-served.js +40 -10
  83. package/dist/handed-values.js +1 -2
  84. package/dist/harness-check.d.ts +2 -1
  85. package/dist/harness-check.js +45 -12
  86. package/dist/harness-keys.js +9 -6
  87. package/dist/harness-opencode/facts.js +2 -0
  88. package/dist/harness-recipe-entry.d.ts +9 -3
  89. package/dist/harness-recipe-entry.js +18 -9
  90. package/dist/harness-recipe.d.ts +21 -0
  91. package/dist/harness-recipe.js +25 -3
  92. package/dist/harness-table.d.ts +8 -0
  93. package/dist/harness-table.js +17 -6
  94. package/dist/helper-env.d.ts +4 -1
  95. package/dist/helper-env.js +4 -1
  96. package/dist/host-check.d.ts +2 -3
  97. package/dist/host-check.js +13 -7
  98. package/dist/hosted-search.js +1 -1
  99. package/dist/import.d.ts +11 -8
  100. package/dist/import.js +437 -186
  101. package/dist/key-refusal.d.ts +9 -2
  102. package/dist/key-refusal.js +86 -32
  103. package/dist/learn/audit.d.ts +8 -5
  104. package/dist/learn/audit.js +15 -7
  105. package/dist/learn/compile.d.ts +5 -2
  106. package/dist/learn/compile.js +45 -21
  107. package/dist/learn/field-check.d.ts +111 -0
  108. package/dist/learn/field-check.js +174 -0
  109. package/dist/learn/ledger.d.ts +32 -29
  110. package/dist/learn/ledger.js +26 -19
  111. package/dist/learn/promote.d.ts +79 -36
  112. package/dist/learn/promote.js +60 -65
  113. package/dist/learn/secrets.d.ts +4 -0
  114. package/dist/learn/secrets.js +12 -0
  115. package/dist/learned-nodes.d.ts +8 -5
  116. package/dist/learned-nodes.js +8 -5
  117. package/dist/learned-workflow.d.ts +2 -1
  118. package/dist/learned-workflow.js +13 -10
  119. package/dist/learning-cost.d.ts +25 -8
  120. package/dist/learning-cost.js +23 -8
  121. package/dist/learning-setting.d.ts +6 -0
  122. package/dist/learning-setting.js +11 -1
  123. package/dist/listen-first.d.ts +5 -4
  124. package/dist/listen-first.js +4 -5
  125. package/dist/live-reach.d.ts +7 -0
  126. package/dist/live-reach.js +7 -0
  127. package/dist/mcp-api-only-origins.d.ts +15 -0
  128. package/dist/mcp-api-only-origins.js +40 -0
  129. package/dist/mcp-gateway.d.ts +16 -2
  130. package/dist/mcp-gateway.js +41 -3
  131. package/dist/mcp-oauth.d.ts +1 -1
  132. package/dist/mcp-oauth.js +3 -2
  133. package/dist/mcp-probe.d.ts +3 -0
  134. package/dist/mcp-probe.js +3 -0
  135. package/dist/mcp-secret-writes.d.ts +24 -2
  136. package/dist/mcp-secret-writes.js +30 -2
  137. package/dist/mcp-servers-store.d.ts +52 -14
  138. package/dist/mcp-servers-store.js +165 -61
  139. package/dist/mcp-sources.d.ts +1 -2
  140. package/dist/mcp-sources.js +1 -2
  141. package/dist/mcp-store.d.ts +5 -5
  142. package/dist/mcp-store.js +3 -3
  143. package/dist/mcp.js +1 -1
  144. package/dist/model-gateway.d.ts +1 -1
  145. package/dist/model-gateway.js +20 -36
  146. package/dist/ollama-provider.d.ts +23 -9
  147. package/dist/ollama-provider.js +49 -20
  148. package/dist/openapi.js +202 -51
  149. package/dist/otel/genai.d.ts +83 -7
  150. package/dist/otel/genai.js +69 -9
  151. package/dist/otel/tracer.d.ts +42 -13
  152. package/dist/otel/tracer.js +241 -56
  153. package/dist/page.d.ts +7 -1
  154. package/dist/page.js +76 -22
  155. package/dist/paid-calls.d.ts +6 -4
  156. package/dist/paid-calls.js +15 -7
  157. package/dist/place-offers.d.ts +55 -34
  158. package/dist/place-offers.js +93 -54
  159. package/dist/place.d.ts +13 -14
  160. package/dist/place.js +56 -41
  161. package/dist/print.js +1 -1
  162. package/dist/provider.d.ts +3 -2
  163. package/dist/provider.js +17 -8
  164. package/dist/redact/match.d.ts +27 -9
  165. package/dist/redact/match.js +266 -57
  166. package/dist/run-census.d.ts +1 -1
  167. package/dist/run-census.js +4 -5
  168. package/dist/run-cut.js +4 -5
  169. package/dist/run-export-store.d.ts +15 -0
  170. package/dist/run-export-store.js +30 -0
  171. package/dist/run-export.d.ts +139 -0
  172. package/dist/run-export.js +379 -0
  173. package/dist/run-file-cut.d.ts +5 -0
  174. package/dist/run-file-cut.js +13 -0
  175. package/dist/run-launch.js +16 -3
  176. package/dist/run-model-key.d.ts +15 -6
  177. package/dist/run-model-key.js +30 -31
  178. package/dist/run-reads-over-store.js +4 -4
  179. package/dist/run-secrets.js +3 -5
  180. package/dist/run-timeline-served.js +3 -1
  181. package/dist/runnable.js +2 -2
  182. package/dist/runner/adapter-loop.d.ts +35 -18
  183. package/dist/runner/adapter-loop.js +94 -47
  184. package/dist/runner/adapter-run-spec.d.ts +14 -1
  185. package/dist/runner/adapter-run-spec.js +64 -28
  186. package/dist/runner/adapters/pi.js +4 -2
  187. package/dist/runner/audit.d.ts +8 -5
  188. package/dist/runner/audit.js +37 -17
  189. package/dist/runner/beside.js +5 -6
  190. package/dist/runner/claim-secrets.d.ts +10 -3
  191. package/dist/runner/claim-secrets.js +29 -13
  192. package/dist/runner/cli-run-spec.d.ts +18 -0
  193. package/dist/runner/cli-run-spec.js +81 -46
  194. package/dist/runner/compare-end.d.ts +2 -1
  195. package/dist/runner/compare-end.js +25 -10
  196. package/dist/runner/conformance/harness.js +2 -3
  197. package/dist/runner/conformance/subject.d.ts +4 -1
  198. package/dist/runner/harness-loops.js +3 -3
  199. package/dist/runner/held.d.ts +5 -2
  200. package/dist/runner/journal-kinds.d.ts +4 -1
  201. package/dist/runner/journal-kinds.js +22 -4
  202. package/dist/runner/learned-first.js +20 -4
  203. package/dist/runner/learning-end.d.ts +2 -8
  204. package/dist/runner/learning-end.js +11 -19
  205. package/dist/runner/live-ticker.js +7 -5
  206. package/dist/runner/loop-contract.d.ts +0 -2
  207. package/dist/runner/loop.d.ts +7 -0
  208. package/dist/runner/loop.js +1 -0
  209. package/dist/runner/main.js +12 -11
  210. package/dist/runner/mcp-claim.js +21 -5
  211. package/dist/runner/meta-tools.js +3 -2
  212. package/dist/runner/metrics.d.ts +7 -0
  213. package/dist/runner/metrics.js +19 -0
  214. package/dist/runner/model-route.d.ts +10 -7
  215. package/dist/runner/model-route.js +79 -15
  216. package/dist/runner/native-loop.js +10 -8
  217. package/dist/runner/presence.d.ts +1 -4
  218. package/dist/runner/presence.js +6 -8
  219. package/dist/runner/promotion.d.ts +25 -13
  220. package/dist/runner/promotion.js +125 -50
  221. package/dist/runner/redact.d.ts +3 -1
  222. package/dist/runner/redact.js +5 -3
  223. package/dist/runner/responses-fetch.d.ts +36 -0
  224. package/dist/runner/responses-fetch.js +511 -0
  225. package/dist/runner/run-child-host.d.ts +20 -5
  226. package/dist/runner/run-child-host.js +179 -77
  227. package/dist/runner/run-child.js +11 -4
  228. package/dist/runner/runner-check.js +5 -1
  229. package/dist/runner/runner.d.ts +2 -3
  230. package/dist/runner/runner.js +13 -9
  231. package/dist/runner/sdk-loop.js +8 -8
  232. package/dist/runner/spec-holds.d.ts +8 -1
  233. package/dist/runner/spec-holds.js +9 -4
  234. package/dist/runner/workflow-calls.d.ts +9 -0
  235. package/dist/runner/workflow-calls.js +150 -0
  236. package/dist/runner/workflow-first.d.ts +120 -32
  237. package/dist/runner/workflow-first.js +218 -94
  238. package/dist/runner/workflow-spec.js +1 -1
  239. package/dist/runs-over-store.js +8 -7
  240. package/dist/save-once.d.ts +14 -0
  241. package/dist/save-once.js +31 -0
  242. package/dist/scrub-command.d.ts +34 -31
  243. package/dist/scrub-command.js +116 -147
  244. package/dist/sdk/runs.d.ts +6 -0
  245. package/dist/sdk/runs.js +89 -10
  246. package/dist/seal-key.js +4 -4
  247. package/dist/secret-cut.d.ts +3 -0
  248. package/dist/secret-cut.js +7 -1
  249. package/dist/secret-rules.d.ts +8 -1
  250. package/dist/secret-rules.js +482 -53
  251. package/dist/secrets.d.ts +51 -9
  252. package/dist/secrets.js +311 -21
  253. package/dist/server-keys.d.ts +4 -3
  254. package/dist/server-keys.js +4 -3
  255. package/dist/server-secrets-store.js +2 -4
  256. package/dist/server.d.ts +15 -3
  257. package/dist/server.js +312 -80
  258. package/dist/share-context.d.ts +22 -0
  259. package/dist/share-context.js +76 -0
  260. package/dist/shell-lex.d.ts +105 -0
  261. package/dist/shell-lex.js +646 -0
  262. package/dist/sign-ins-store.d.ts +5 -7
  263. package/dist/sign-ins-store.js +12 -16
  264. package/dist/sign-ins.d.ts +7 -11
  265. package/dist/sign-ins.js +40 -41
  266. package/dist/skills/agentrun-serve/SKILL.md +5 -2
  267. package/dist/start-switches.d.ts +6 -5
  268. package/dist/start-switches.js +8 -4
  269. package/dist/stats.d.ts +5 -1
  270. package/dist/stats.js +1 -1
  271. package/dist/store/agent-body.js +2 -2
  272. package/dist/store/dialect.d.ts +4 -0
  273. package/dist/store/dialect.js +1 -0
  274. package/dist/store/index.d.ts +2 -2
  275. package/dist/store/index.js +1 -1
  276. package/dist/store/mcp-record-keys.d.ts +10 -0
  277. package/dist/store/mcp-record-keys.js +13 -0
  278. package/dist/store/postgres/dialect.js +2 -0
  279. package/dist/store/postgres/migrations/0029_run_launch_census_settle.sql +28 -0
  280. package/dist/store/postgres/migrations/0030_learning_ledger_epoch.sql +40 -0
  281. package/dist/store/postgres/migrations/0031_learning_ledger_calls.sql +12 -0
  282. package/dist/store/postgres/store.js +4 -0
  283. package/dist/store/schema.d.ts +33 -10
  284. package/dist/store/schema.js +70 -16
  285. package/dist/store/seal.d.ts +4 -1
  286. package/dist/store/sqlite/run-launch.d.ts +3 -10
  287. package/dist/store/sqlite/run-launch.js +12 -23
  288. package/dist/store/sqlite/run-patch.d.ts +17 -10
  289. package/dist/store/sqlite/run-patch.js +50 -13
  290. package/dist/store/sqlite/store.d.ts +3 -0
  291. package/dist/store/sqlite/store.js +116 -34
  292. package/dist/store/store-schema.js +3 -4
  293. package/dist/store/store.d.ts +67 -32
  294. package/dist/store/store.js +14 -11
  295. package/dist/store-reads/analytics.d.ts +2 -1
  296. package/dist/store-reads/analytics.js +7 -5
  297. package/dist/store-reads/learning.d.ts +35 -26
  298. package/dist/store-reads/learning.js +120 -92
  299. package/dist/store-reads/stats.d.ts +2 -2
  300. package/dist/store-reads/stats.js +13 -7
  301. package/dist/store-view.js +10 -7
  302. package/dist/suggestions.d.ts +1 -1
  303. package/dist/suggestions.js +29 -13
  304. package/dist/template-eval.d.ts +72 -9
  305. package/dist/template-eval.js +358 -64
  306. package/dist/timeline/labels.d.ts +1 -1
  307. package/dist/timeline/labels.js +4 -3
  308. package/dist/timeline/timeline.d.ts +23 -11
  309. package/dist/timeline/timeline.js +115 -31
  310. package/dist/timeline/web-calls.d.ts +8 -6
  311. package/dist/timeline/web-calls.js +10 -6
  312. package/dist/tool-gate.d.ts +13 -1
  313. package/dist/tool-gate.js +43 -3
  314. package/dist/tool-keys-store.js +2 -4
  315. package/dist/tool-keys.d.ts +11 -0
  316. package/dist/tool-keys.js +16 -0
  317. package/dist/tool-plane.d.ts +50 -11
  318. package/dist/tool-plane.js +94 -30
  319. package/dist/tools.d.ts +5 -1
  320. package/dist/tools.js +7 -7
  321. package/dist/trigger-email.js +12 -6
  322. package/dist/triggers.d.ts +15 -34
  323. package/dist/triggers.js +119 -126
  324. package/dist/validate.d.ts +8 -1
  325. package/dist/validate.js +163 -23
  326. package/dist/web-fake.d.ts +2 -1
  327. package/dist/web-fake.js +8 -6
  328. package/dist/web-fetch.d.ts +8 -6
  329. package/dist/web-fetch.js +13 -11
  330. package/dist/web-holds.d.ts +32 -0
  331. package/dist/web-holds.js +86 -0
  332. package/dist/web-run.d.ts +32 -5
  333. package/dist/web-run.js +75 -28
  334. package/dist/web-save.js +23 -12
  335. package/dist/web-search.js +14 -25
  336. package/dist/wires.d.ts +4 -3
  337. package/dist/wires.js +5 -4
  338. package/dist/workspace/archil-claim.d.ts +12 -5
  339. package/dist/workspace/archil-claim.js +45 -5
  340. package/dist/workspace/attach.js +52 -19
  341. package/dist/workspace/attached-computers.d.ts +6 -2
  342. package/dist/workspace/attached-computers.js +5 -4
  343. package/dist/workspace/computer-deps.d.ts +23 -6
  344. package/dist/workspace/computer-deps.js +35 -17
  345. package/dist/workspace/computer-for-claim.d.ts +21 -4
  346. package/dist/workspace/computer-for-claim.js +160 -54
  347. package/dist/workspace/computer-intervals.d.ts +2 -0
  348. package/dist/workspace/computer-intervals.js +1 -1
  349. package/dist/workspace/computer-janitor.d.ts +48 -14
  350. package/dist/workspace/computer-janitor.js +153 -44
  351. package/dist/workspace/computer-kept.d.ts +158 -32
  352. package/dist/workspace/computer-kept.js +288 -63
  353. package/dist/workspace/computer-providers.d.ts +8 -3
  354. package/dist/workspace/computer-providers.js +12 -5
  355. package/dist/workspace/index.d.ts +1 -1
  356. package/dist/workspace/index.js +1 -1
  357. package/dist/workspace/local.js +41 -1
  358. package/dist/workspace/mount-revoke-left.d.ts +31 -0
  359. package/dist/workspace/mount-revoke-left.js +72 -0
  360. package/dist/workspace/paired-route.js +4 -5
  361. package/dist/workspace/resolve-computer.d.ts +5 -2
  362. package/dist/workspace/resolve-computer.js +9 -3
  363. package/dist/workspace/run-end-computers.d.ts +11 -5
  364. package/dist/workspace/run-end-computers.js +153 -28
  365. package/dist/workspace/run-tools.d.ts +22 -3
  366. package/dist/workspace/run-tools.js +17 -5
  367. package/dist/workspace/run-workspace.d.ts +14 -1
  368. package/dist/workspace/run-workspace.js +34 -1
  369. package/dist/workspace/workspace.d.ts +6 -0
  370. package/package.json +12 -13
  371. package/skills/agentrun-serve/SKILL.md +5 -2
  372. package/templates/README.md +22 -6
  373. package/dist/console/assets/backend-CSAWUqtZ.js +0 -166
  374. package/dist/console/assets/index-Bwn1oTem.css +0 -1
  375. package/dist/console/assets/index-DPh29cyz.js +0 -747
@@ -30,14 +30,8 @@ export function tombstoneMetrics(metrics, kept) {
30
30
  out.purged = kept;
31
31
  return out;
32
32
  }
33
- /** The census a launch takes when sealed bytes are first written on it (RunLaunchRow.census): it holds secrets its caller
34
- * handed it, and no end has counted where they sit. An end erases them and writes its census in its place; an end that
35
- * wrote none (a runner from before the census, during a rolling deploy) leaves it, and an ended run's files under it are
36
- * withheld from a cut read, as under CENSUS_UNSCANNED. */
37
- export const CENSUS_PENDING = JSON.stringify("pending");
38
- /** The census an end that erased a launch's sealed bytes writes when it has none made under them (RunLaunchRow.census), and
39
- * a message's seal writes over CENSUS_PENDING left by such an end: what the run was handed may be in its files, and
40
- * nothing says where. */
33
+ /** The census an end that erased a launch's sealed bytes writes when it has none made under them (RunLaunchRow.census): what
34
+ * the run was handed may be in its files, and nothing says where. */
41
35
  export const CENSUS_UNSCANNED = JSON.stringify("unscanned");
42
36
  /** A firing a trigger has no room for: `due` firings are DUE_KEPT already. Nothing was written. */
43
37
  export class TriggerFull extends Error {
@@ -128,12 +122,21 @@ export const MCP_REFRESH_LEASE_MS = MCP_REFRESH_CALL_MS + STORE_WRITE_DEADLINE_M
128
122
  /** How long a runner's row is kept once it stopped saying it is there (a runner that died without its close): its next
129
123
  * put removes every row older than this. */
130
124
  export const RUNNER_ROWS_KEPT_MS = 24 * 60 * 60_000;
131
- /** How old a runner's row can be and still count as there, on the store's clock (runner/presence.ts PRESENCE_STALE_MS:
132
- * three renewals missed). The claim rule reads it for a kept computer's pin. */
125
+ /** How old a runner's row can be and still count as there, on the store's clock: three of its renewals missed
126
+ * (runner/presence.ts PRESENCE_RENEW_MS). Every reader of who is there uses it, the claim rule's kept computer pin
127
+ * included. */
133
128
  export const RUNNER_STALE_MS = 30_000;
134
129
  /** The settings key prefix of the computers kept at a run's park, one row per run (workspace/computer-kept.ts). A row
135
130
  * whose computer is pinned to the runner that kept it (its `runner` and `incarnation`) holds the run for that runner in
136
- * the claim rule, while that runner is there under that incarnation and the keep lasts. */
131
+ * the claim rule, while that runner is there under that incarnation and the keep lasts.
132
+ *
133
+ * "There", for a pinned row's runner, means one thing everywhere: its runners row seen within RUNNER_STALE_MS on the
134
+ * store's clock, under the incarnation the row names, refusing claims or not. That runner holds the machine: no other
135
+ * runner's janitor ends its row (computer-kept.ts heldElsewhere, in sweepKept), and a claim that took the row and could
136
+ * not reach the machine puts the row back (computer-for-claim.ts reattach), so that runner's own end finds it.
137
+ * The claim rule (sqlite/store.ts keptElsewhere) alone asks one thing more, that it is claiming (`refused` null): a
138
+ * refusing runner would make the answer wait the whole keep for nothing, and its row is given back marked ending, so it
139
+ * still ends its machine. */
137
140
  export const KEPT_PREFIX = "computer_kept:";
138
141
  /** The id prefix of a package uploaded whole (POST /v2/templates/import {files}): `upload:<name>`. */
139
142
  export const UPLOAD_PREFIX = "upload:";
@@ -1,5 +1,6 @@
1
+ import type { Source } from "@parcha/agentrun-import";
1
2
  import type { Store } from "../store/store.js";
2
- export type ImportSource = "claude-code" | "codex" | "pi";
3
+ export type ImportSource = Source;
3
4
  /** The states a run's end can have (AnalyticsFilter.pending). */
4
5
  export type PendingEnd = "completed" | "escalated" | "failed" | "stopped";
5
6
  export type AnalyticsFilter = {
@@ -9,6 +9,7 @@ import { NATIVE_RUNTIME_FACTS } from "../native/runtimes.js";
9
9
  import { DEFAULT_HARNESS } from "../harness.js";
10
10
  import { costOfRun, runReadState } from "./runs.js";
11
11
  import { isShadowRun, shadowRunId } from "../learn/audit.js";
12
+ import { shadowOfRun, shadowUsdOf } from "../learn/ledger.js";
12
13
  const PENDING_ENDS = new Set(["completed", "escalated", "failed", "stopped"]);
13
14
  const num = (value) => (typeof value === "number" && Number.isFinite(value) ? value : 0);
14
15
  const metricsOf = (run) => (run.metrics !== null && typeof run.metrics === "object" && !Array.isArray(run.metrics) ? run.metrics : null);
@@ -60,7 +61,7 @@ export async function analytics(store, filter = {}) {
60
61
  const metered = !running && metricsOf(run) !== null;
61
62
  const metrics = metered ? metricsOf(run) : null;
62
63
  const shadow = shadows.get(shadowRunId(run.id));
63
- const shadowUsd = shadow && runReadState(shadow, "agentic") !== "running" && shadow.costUsd !== null ? shadow.costUsd : 0;
64
+ const shadowUsd = shadowUsdOf(shadow && shadowOfRun(shadow));
64
65
  facts.push({
65
66
  startedMs: run.startedAt,
66
67
  endedMs: run.endedAt,
@@ -78,10 +79,10 @@ export async function analytics(store, filter = {}) {
78
79
  });
79
80
  }
80
81
  const counted = (group) => {
81
- let runsN = 0, completed = 0, needsReview = 0, failed = 0, metered = 0, tokenMetered = 0, tokens = 0, costUsd = 0, known = 0, pend = 0;
82
+ let runsN = 0, completed = 0, needsReview = 0, failed = 0, metered = 0, tokenMetered = 0, tokens = 0, costUsd = 0, priced = 0, known = 0, pend = 0;
82
83
  let shadowUsd = 0;
83
84
  for (const f of group) {
84
- shadowUsd += f.shadowUsd;
85
+ shadowUsd += f.shadowUsd ?? 0;
85
86
  runsN += 1;
86
87
  if (f.state === "completed" || f.state === "escalated")
87
88
  completed += 1;
@@ -97,12 +98,13 @@ export async function analytics(store, filter = {}) {
97
98
  tokens += reported;
98
99
  }
99
100
  }
101
+ // Its $ is spent either way; the run is known only with its shadow's.
100
102
  if (!f.running && !f.pending && f.costUsd !== null)
101
- (costUsd += f.costUsd, (known += 1));
103
+ (costUsd += f.costUsd, (priced += 1), (known += f.shadowUsd === null ? 0 : 1));
102
104
  if (f.pending)
103
105
  pend += 1;
104
106
  }
105
- return { runs: runsN, completed, needsReview, failed, runsWithMetrics: metered, runsWithTokenMetrics: tokenMetered, tokens: tokenMetered > 0 || runsN === 0 ? tokens : null, costUsd: known > 0 || shadowUsd > 0 ? costUsd + shadowUsd : null, unknownCostRuns: runsN - known - pend };
107
+ return { runs: runsN, completed, needsReview, failed, runsWithMetrics: metered, runsWithTokenMetrics: tokenMetered, tokens: tokenMetered > 0 || runsN === 0 ? tokens : null, costUsd: priced > 0 || shadowUsd > 0 ? costUsd + shadowUsd : null, unknownCostRuns: runsN - known - pend };
106
108
  };
107
109
  // Totals over the whole scope: the group counts, the token sums, and the time figures.
108
110
  let input = 0, cacheRead = 0, cacheWrite = 0, output = 0, requests = 0, unmetered = 0;
@@ -1,13 +1,13 @@
1
1
  import { type ItemStatus, type Learning, type Shown } from "../learning.js";
2
2
  import { type RunLaunch } from "../run-launch.js";
3
3
  import { type Impact, type ImpactLearning } from "../learning-run.js";
4
- import { type Bill, type CostReplay, type CostRun, type CostSummary, type RunPath } from "../learning-cost.js";
5
- import { type Answered, type LedgerRow, type RevisionAt, type RunFigures, type Summarized } from "../learn/ledger.js";
6
- import { type PastRuns, workflowCostsOf } from "../runner/workflow-first.js";
4
+ import { type Bill, type CostReplay, type CostRun, type CostSummary, type RunPath, type WorkflowCall } from "../learning-cost.js";
5
+ import { type Answered, type RevisionAt, type RunFigures, type Summarized } from "../learn/ledger.js";
6
+ import { type FirstReason, type PastRuns, pathMetrics, workflowCostsOf } from "../runner/workflow-first.js";
7
7
  import { type Reflect } from "../reflect-range.js";
8
8
  import type { EntryLike, TimelineEvent } from "../timeline/index.js";
9
9
  import type { AnalyticsFilter } from "./analytics.js";
10
- import { type PromotionRecord, type Refused, type ResetRevision } from "../learn/promote.js";
10
+ import { type CostStop, type PromotionRecord, type Refused, type ResetRevision } from "../learn/promote.js";
11
11
  import type { LearningProposal, Store } from "../store/store.js";
12
12
  /** A run's learning end: the Learning fields its end sets. */
13
13
  export type RunLearningEnd = Learning;
@@ -97,16 +97,6 @@ export declare function agentLedgerOverStore(store: Store, agentId: string, refu
97
97
  skip: Refused | undefined;
98
98
  listed: RunRecord[];
99
99
  }>;
100
- /** Whether run `run`'s ledger row stands for it as it is now: written for the end the run has (`epoch`, the run's epoch
101
- * after the end that wrote it: run-patch.ts recordLedger), with no cost healed since. A runner or server of the build
102
- * before the ledger writes none: its end of a run again (a follow-up's, or a grade's round that keeps the end it had, at
103
- * the same ended_at) moves the run's epoch, as every end and every claim does, and its heal of a cost (Runs.healCost:
104
- * only over an unknown cost, at the same epoch and end) makes the cost known. Either leaves the row for the run as it
105
- * was, and its reader records it again from the run's row (record), as for a run with no row. A cancel that wins an
106
- * end moves its ended_at alone (run-patch.ts refuseLeftovers): the row stands. */
107
- export declare function ledgerRowStands(row: Pick<LedgerRow, "cost"> & {
108
- readonly epoch: number;
109
- }, run: Pick<RunRecord, "epoch" | "costUsd" | "costAtMostUsd">): boolean;
110
100
  /** Each revision of the agent's learned workflows its promotion records name (one per workflow and digest, in the order
111
101
  * they were first recorded), with the served runs its spend is spread over (K, its check's compile_after) and `state`
112
102
  * from its newest record: `checking` while its check replays, `serving` for the champion (championOf, skipping what
@@ -134,9 +124,11 @@ export type RunAllIn = {
134
124
  export declare function runAllInOverStore(store: Store, runId: string, rowUsd?: number | null, refused?: RefusedFor): Promise<RunAllIn | undefined>;
135
125
  /** The check the serving promotion was decided on, as the cost summary says it: the champion (championOf) of the learned
136
126
  * workflow the newest run a learned workflow ran first on ran (`workflow` or `handed_back`), its record's check numbers
137
- * (the share of the inputs it ran on that said what the agent said, against the agent's own 100%). `cleared`: it passed
138
- * (a champion is promoted), with the agreement check it passed (`agreed` of `alone`, its bar `agree_min`). Null when no run was served, nothing serves now (held, or never promoted), or it was promoted
139
- * on no input (a dry run). The champion skips what `refused` names (the purge gate's). */
127
+ * (the share of the inputs it answered alone that were right on every field, and the agent's share on the same inputs
128
+ * against the same known answers: `agent_agreed`). `cleared`: the joint
129
+ * floor's verdict as the check decided it (`check.joint`, learn/field-check.ts), which every promotion passed. Null when
130
+ * no run was served, nothing serves now (held, or never promoted), or it was promoted on no input (a dry run). The
131
+ * champion skips what `refused` names (the purge gate's). */
140
132
  export declare function servingReplayOf(records: readonly PromotionRecord[], runs: readonly CostRun[], refused?: Refused): CostReplay | null;
141
133
  /** Every round run `runId` kept (one a follow-up opened again is a round past its first), oldest first: none when it kept
142
134
  * no learning. The store's proposal(runId) is the newest round alone; a read of what the run's learning did or spent
@@ -147,7 +139,8 @@ type RunRecord = NonNullable<Awaited<ReturnType<Store["runs"]["get"]>>>;
147
139
  * summarize over `listed`, its runs as runs.list read them, newest first): of its newest PAST_RUNS runs on no named disk
148
140
  * (learn/learning-of.ts: one on a named disk learns nothing), those since its learning's reset (`since`,
149
141
  * learn/compile.ts sinceReset: a workflow before it is not the one it learned after, whatever its name), each series
150
- * run among them with its all-in cost as the series reads it (RunFigures.series_total_usd), and the by-hand side over
142
+ * run among them with its all-in cost as the series reads it (RunFigures.series_total_usd; a served run cut at its cap,
143
+ * at its bound), and the by-hand side over
151
144
  * those same runs: the mean job's own $ (RunFigures.usd: no compile turn, follow-up or shadow) of the ones on the
152
145
  * `agent` path, with the ones its mean left out (cost unknown or only a bound). The served side and the by-hand side
153
146
  * read one set of runs: the cost page and a run's launch read the same window (pastRunsOverStore). */
@@ -159,31 +152,45 @@ export declare function pastRunsOf(out: Summarized, listed: readonly RunRecord[]
159
152
  * share. The cost page reads the same window of the same figures (agentCostOverStore). */
160
153
  export declare function pastRunsOverStore(store: Store, agentId: string): Promise<PastRuns>;
161
154
  /** The money rule's decision for agent `agentId` (runner/workflow-first.ts costStopOf) over the runs a run's launch reads
162
- * (pastRunsOverStore), as the launch reads them: runs that can't be read now stop nothing (workflowFirstOf serves). What
163
- * the promotions read and the learning view fold into each workflow's state (learn/promote.ts servingStateOf). */
164
- export declare const costStopOverStore: (store: Store, agentId: string) => Promise<import("../learn/promote.js").CostStop>;
155
+ * (pastRunsOverStore), as the launch reads them. Runs it can't read stop every workflow, as a launch then leaves the run
156
+ * to the agent (firstChoice past_unread: it fails closed): the view never says serving when it can't tell. What the
157
+ * promotions read and the learning view fold into each workflow's state (learn/promote.ts servingStateOf). */
158
+ export declare const costStopOverStore: (store: Store, agentId: string) => Promise<CostStop>;
165
159
  /** The path run `runId` took and the learned workflow it ran, the one classifier its run record keeps (metrics.path,
166
160
  * metrics.workflow, metrics.workflow_digest: workflow-first.ts pathMetrics, written at the run's end) and its cost per run
167
161
  * reads (pathOfRow). A run its learned workflow served first (its WORKFLOW_FIRST entry, servedOf): `workflow` when it
168
162
  * answered and its answer is still the run's last (servedAnswerStands), `handed_back` when it handed back or a follow-up's
169
163
  * new answer is the agent's, with its digest. Else, for a run its launch offered a learned workflow, its timeline
170
164
  * (runner/learning-end.ts runPathOf, the agent calling the tool itself): `workflow` when its job did only that,
171
- * `handed_back` when the agent went on, with the workflow its job called last. Any other run's path is `agent`. Both
172
- * read the run's rows less its feedback rounds' (withoutFeedbackRounds): a grade's round never moves the path. `cut` on a
173
- * hand-back a restart cut (servedOf: the workflow's call ended `interrupted`), never one the workflow made itself. */
174
- export declare function runPathOverStore(store: Store, runId: string, started: RunLaunch["learning"], redact?: (text: string) => string): Promise<{
165
+ * `handed_back` when the agent went on, with the workflow it called last that ran (its call's record,
166
+ * runner/workflow-calls.ts workflowCallsOf). Any other run's path is `agent`. Both read the run's rows less its feedback rounds' (withoutFeedbackRounds): a grade's
167
+ * round never moves the path. `cut` on a hand-back a restart cut (servedOf: the workflow's call ended `interrupted`),
168
+ * never one the workflow made itself. `entries`: the run's (runEntries), when the caller read them already. */
169
+ export declare function runPathOverStore(store: Store, runId: string, started: RunLaunch["learning"], redact?: (text: string) => string, entries?: readonly EntryLike[]): Promise<{
175
170
  path: RunPath;
176
171
  workflow: string | null;
177
172
  digest?: string;
178
173
  promotion?: number;
179
174
  cut?: true;
180
175
  }>;
176
+ /** The run-record fields run `runId`'s end writes for its path (runner/workflow-first.ts pathMetrics), the one writer
177
+ * every harness's end calls: the run's entries read once (runEntries, every conversation: a call's booking is read),
178
+ * classified (runPathOverStore, for a run that learns) and kept with its calls (`workflow_calls`) and why its ending
179
+ * claim's first choice did or did not serve (`choice`, firstChoice's reason). `redact`: the run's cut, as its
180
+ * follow-up showed a served answer. */
181
+ export declare function runPathMetricsOverStore(store: Store, runId: string, launch: RunLaunch, { redact, choice }?: {
182
+ readonly redact?: (text: string) => string;
183
+ readonly choice?: FirstReason;
184
+ }): Promise<ReturnType<typeof pathMetrics>>;
181
185
  /** The path a run's views say it took (GET /runs/{id} and each runs list row: `path`, and `workflow`), read off its record
182
186
  * as the cost per run reads it (pathOfRow, revisionOfRow: the ledger read's own, so the run views and GET
183
187
  * .../learning/cost never disagree). `path` is null for a run not ended, a workflow revision's run, and a run that learns
184
188
  * whose record names none; `workflow` names the learned workflow and its revision only on a run it served (path
185
189
  * `workflow` or `handed_back`; the name only when its launch offered one, the digest null when its record names none,
186
- * and whether its answer re-picked, repickOfRow), else null. */
190
+ * and whether its answer re-picked, repickOfRow), else null. `workflow_calls`: every learned workflow call its record
191
+ * keeps (learning-cost.ts workflowCallsIn: the platform's and the agent's, each with its digest, caller, outcome and
192
+ * booked $), none when it made none, null where `path` is; `first_choice`: why its ending claim's first choice served
193
+ * or did not (runner/workflow-first.ts FIRST_REASONS), null where its record names none. */
187
194
  export declare function pathViewOf(run: Pick<RunRecord, "metrics" | "state">, kind: string, started: RunLaunch["learning"]): {
188
195
  path: RunPath | null;
189
196
  workflow: {
@@ -191,6 +198,8 @@ export declare function pathViewOf(run: Pick<RunRecord, "metrics" | "state">, ki
191
198
  digest: string | null;
192
199
  repick: Repick | null;
193
200
  } | null;
201
+ workflow_calls: WorkflowCall[] | null;
202
+ first_choice: FirstReason | null;
194
203
  };
195
204
  /** How a served run's answer re-picked (learn/check.ts: a quote miss re-picks it once before it hands back): `found` when
196
205
  * its second pass found its quotes, `missed` when it still missed them. */
@@ -10,12 +10,13 @@ import { runLearningBody } from "../learning-routes.js";
10
10
  import { learningStartOf, learningStartOfStored, readLaunchBody, seedFailed } from "../run-launch.js";
11
11
  import { said, seedStopped } from "../learning-words.js";
12
12
  import { learningImpact } from "../learning-run.js";
13
- import { JOB_PATHS, RUN_PATHS, SERVED_PATHS } from "../learning-cost.js";
14
- import { answeredOf, figuresOf, record, roundsLooked, roundsSpentIn, shadowCostOf, shadowSpend, spendByRun, summarize } from "../learn/ledger.js";
13
+ import { COST_WINDOW, JOB_PATHS, noiseOff, RUN_PATHS, SERVED_PATHS, workflowCallsIn } from "../learning-cost.js";
14
+ import { answeredOf, auditOfLaunch, figuresOf, record, roundsLooked, roundsSpentIn, shadowCostOf, spendByRun, summarize } from "../learn/ledger.js";
15
15
  import { COMPILE_AFTER_DEFAULT } from "../learning-setting.js";
16
- import { modelsOwn, runPathOf, workflowNameOf } from "../runner/learning-end.js";
16
+ import { modelsOwn, runPathOf } from "../runner/learning-end.js";
17
17
  import { feedbackStatus } from "../runner/feedback-round.js";
18
- import { costStopOf, NO_PAST_RUNS, PAST_RUNS, servedAnswerOf, servedAnswerStands, servedOf, workflowCostsOf } from "../runner/workflow-first.js";
18
+ import { costStopOf, FIRST_REASONS, meanOf, PAST_RUNS, pathMetrics, servedAnswerOf, servedAnswerStands, servedOf, workflowCostsOf } from "../runner/workflow-first.js";
19
+ import { workflowCallsOf } from "../runner/workflow-calls.js";
19
20
  import { reflects } from "../reflect-range.js";
20
21
  import { correctionCount } from "../learn/corrections.js";
21
22
  import { runEntries, runReflect, runTimeline, withoutFeedbackRounds } from "./journal.js";
@@ -206,7 +207,7 @@ export async function agentCostOverStore(store, agentId, limit = Infinity, refus
206
207
  const { runs } = out;
207
208
  // Each learned workflow that ran first, where it stands against the money rule, over the figures this series shows: a
208
209
  // workflow over the line shows `needs_revision` with its two numbers, so the page and the launch's decision agree.
209
- const workflows = workflowCostsOf(pastRunsOf(out, listed, await sinceReset(store, agentId)));
210
+ const workflows = workflowCostsOf(await pastRunsRead(store, agentId, out, listed));
210
211
  const summary = { ...out.summary, replay: servingReplayOf(promotions, runs, skip) };
211
212
  const bill = { ...out.bill, holds: holdsOf(promotions, runs) };
212
213
  return { runs: runs.slice(Math.max(0, runs.length - limit)), summary, total_runs: runs.length, unknown_cost_runs: runs.filter((run) => run.total_usd === null).length, unknown_path_runs: out.unknown_path_runs, workflows, bill };
@@ -236,74 +237,54 @@ async function inBatches(items, each) {
236
237
  const zipWith = (keys, values) => keys.map((key, at) => [key, values[at]]);
237
238
  /** What summarize reads of agent `agentId` (LedgerRead, its revisions aside), over `listed` (its runs, as runs.list read
238
239
  * them): the ledger rows of its ended runs that do a job or check one (neither imported nor archived, no workflow
239
- * revision's run, no shadow), a run that does not learn on the `agent` path, each with the workflow name its launch
240
+ * revision's run, no shadow), each on the path its end wrote, each with the workflow name its launch
240
241
  * offered; every spend row; each run's audit shadow (open or ended); and the runs on no named disk (learn/learning-of.ts:
241
- * one on a named disk learns nothing and serves nothing). A run that ended with no row (it ended before the ledger, or
242
- * under a runner of an earlier build), or whose row no longer stands for it (ledgerRowStands), is recorded here from its
243
- * row and its rounds (learn/ledger.ts record), as its end would have; its next end writes it. `runIds`: the runs whose
242
+ * one on a named disk learns nothing and serves nothing). Each ended run has its row: its end wrote it, or the server's
243
+ * open (run-patch.ts backfillLedger) for one an earlier build ended; none is recorded here. `runIds`: the runs whose
244
244
  * ledger is read (Learning.ledger), the agent's every run when absent. `named` false: no workflow name is read for the
245
- * series' rows (a launch's learning start is read only for a row whose end named no path). */
245
+ * series' rows (no launch is read). */
246
246
  async function ledgerReadOf(store, agentId, listed, records, { runIds, named = true } = {}) {
247
- const ledger = await store.learning.ledger(agentId, runIds);
248
247
  const byId = new Map(listed.map((run) => [run.id, run]));
249
- // A row that no longer stands for its run (ledgerRowStands: an earlier build ended it again or healed its cost) is
250
- // recorded again below, with its spend rows, as one with no row is.
251
- const stale = new Set(ledger.rows.flatMap((row) => (byId.has(row.runId) && !ledgerRowStands(row, byId.get(row.runId)) ? [row.runId] : [])));
252
- const recorded = new Map(ledger.rows.flatMap((row) => (stale.has(row.runId) ? [] : [[row.runId, row]])));
253
- const spend = ledger.spend.filter((one) => !stale.has(one.runId));
254
- // The rest are read a few at a time (READS_AT_ONCE), each run's reads in one batch: a claim reads up to PAST_RUNS runs.
255
- const unrecorded = listed.filter((run) => !run.importedFrom && FINAL_STATES.includes(run.state) && !recorded.has(run.id));
256
- const made = await inBatches(unrecorded, async (run) => record(await runEndOf(records, run)));
257
- unrecorded.forEach((run, at) => {
258
- recorded.set(run.id, made[at].row);
259
- spend.push(...made[at].spend);
260
- });
261
- const ended = listed.filter((run) => FINAL_STATES.includes(run.state) && !run.importedFrom && run.archivedAt === null && !isShadowRun(run.id));
248
+ const { recorded, spend } = await ledgerRowsOf(store, agentId, runIds);
249
+ const ended = listed.filter((run) => FINAL_STATES.includes(run.state) && recorded.has(run.id) && !run.importedFrom && run.archivedAt === null && !isShadowRun(run.id));
262
250
  const revisionIds = [...new Set(ended.map((run) => run.revisionId))];
263
251
  const kinds = new Map(zipWith(revisionIds, await inBatches(revisionIds, async (id) => (await store.agents.revision(id))?.kind)));
264
252
  const jobs = ended.filter((run) => kinds.get(run.revisionId) !== "workflow");
265
- // Its launch's learning start: the workflow names it offered, and, for a row whose end named no path, whether it learns.
266
- const starts = await inBatches(jobs, async (run) => (named || recorded.get(run.id).path === null ? learningStartOfRun(records, run.id) : undefined));
253
+ // Its launch's learning start: the workflow names it offered. A row's path is as its end wrote it (learn/ledger.ts
254
+ // record: a run that does not learn and names none is the agent's), the one by-hand rule Learning.byHandBefore reads too.
255
+ const starts = await inBatches(jobs, async (run) => (named ? learningStartOfRun(records, run.id) : undefined));
267
256
  const offered = new Map();
268
257
  const rows = [];
269
258
  const shadows = new Map();
270
259
  const learning = new Set();
271
- const audited = new Set(spend.flatMap((one) => (one.kind === "audit" && one.ofRun !== null ? [one.ofRun] : [])));
272
260
  for (const [run, started] of zipWith(jobs, starts)) {
273
- const row = recorded.get(run.id);
274
- // A run that does not learn names no path: it is the agent's. One that learns and names none is unknown.
275
- rows.push(row.path === null && !started ? { ...row, path: "agent" } : row);
261
+ rows.push(recorded.get(run.id));
276
262
  if (named)
277
263
  offered.set(run.id, started?.workflows);
278
264
  if (run.disk === null)
279
265
  learning.add(run.id);
280
- const shadow = byId.get(shadowRunId(run.id));
281
- if (!shadow)
282
- continue;
283
- const over = FINAL_STATES.includes(shadow.state);
284
- shadows.set(run.id, over ? "ended" : "open");
285
- // A shadow that ended with no audit row (its end named no audit: it failed before its end wrote its metrics) is the
286
- // run's by its id.
287
- if (over && !audited.has(run.id))
288
- spend.push(shadowSpend(shadow, run.id));
266
+ const shadow = shadowOf(byId, run.id);
267
+ if (shadow)
268
+ shadows.set(run.id, shadow);
289
269
  }
290
270
  // Only a name the launch offered is shown.
291
271
  return { rows, spend, shadows, learning, compileAfter: COMPILE_AFTER_DEFAULT, ...(named ? { named: (row) => offeredName(row.workflow, offered.get(row.runId)) } : {}) };
292
272
  }
293
- /** Whether run `run`'s ledger row stands for it as it is now: written for the end the run has (`epoch`, the run's epoch
294
- * after the end that wrote it: run-patch.ts recordLedger), with no cost healed since. A runner or server of the build
295
- * before the ledger writes none: its end of a run again (a follow-up's, or a grade's round that keeps the end it had, at
296
- * the same ended_at) moves the run's epoch, as every end and every claim does, and its heal of a cost (Runs.healCost:
297
- * only over an unknown cost, at the same epoch and end) makes the cost known. Either leaves the row for the run as it
298
- * was, and its reader records it again from the run's row (record), as for a run with no row. A cancel that wins an
299
- * end moves its ended_at alone (run-patch.ts refuseLeftovers): the row stands. */
300
- export function ledgerRowStands(row, run) {
301
- return row.epoch === run.epoch && (row.cost !== "unknown" || (run.costUsd === null && run.costAtMostUsd === null));
302
- }
303
- /** What record() reads of an ended run: its row and every round its learning kept. */
273
+ /** Run `runId`'s audit shadow among `byId` (open while queued or running, ended once final), undefined with none. */
274
+ const shadowOf = (byId, runId) => {
275
+ const shadow = byId.get(shadowRunId(runId));
276
+ return shadow ? (FINAL_STATES.includes(shadow.state) ? "ended" : "open") : undefined;
277
+ };
278
+ /** The ledger rows (by run) and spend rows of `runIds` (Learning.ledger; the agent's every run when absent). */
279
+ async function ledgerRowsOf(store, agentId, runIds) {
280
+ const ledger = await store.learning.ledger(agentId, runIds);
281
+ return { recorded: new Map(ledger.rows.map((row) => [row.runId, row])), spend: ledger.spend };
282
+ }
283
+ /** What record() reads of an ended run: its row, every round its learning kept and, for a shadow, the run it audits. */
304
284
  async function runEndOf(records, run) {
305
285
  const rounds = (await roundsOf(records, run.id)).map((one) => ({ round: one.round, proposal: one.proposal }));
306
- return { id: run.id, agentId: run.agentId, startedAt: run.startedAt, costUsd: run.costUsd, costAtMostUsd: run.costAtMostUsd, metrics: run.metrics, rounds };
286
+ const auditOf = auditOfLaunch(run.id, isShadowRun(run.id) ? (await records.runs.launch(run.id).catch(() => undefined))?.body : undefined);
287
+ return { id: run.id, agentId: run.agentId, startedAt: run.startedAt, costUsd: run.costUsd, costAtMostUsd: run.costAtMostUsd, metrics: run.metrics, rounds, ...(auditOf === undefined ? {} : { auditOf }) };
307
288
  }
308
289
  /** Each hold of the agent's learned workflows (its promotion records that say hold), placed on the series: at the first
309
290
  * run that does a job from it on, by its number among them (the points' `n`), null when none has run since. */
@@ -376,43 +357,40 @@ export async function runAllInOverStore(store, runId, rowUsd, refused) {
376
357
  * recorded it at that cost, with `figures`' compile, shadow and share. */
377
358
  async function pricedAt(store, run, usd, figures) {
378
359
  const row = record(await runEndOf(store, { ...run, costUsd: usd, costAtMostUsd: null })).row;
379
- return figuresOf(isShadowRun(run.id) ? { ...row, path: "audit" } : row, { compile: figures.compile_usd, shadow: figures.shadow_usd, share: figures.share_usd });
360
+ return figuresOf(row, { compile: figures.compile_usd, shadow: figures.shadow_usd, share: figures.share_usd });
380
361
  }
381
- /** One run's figures (learn/ledger.ts figuresOf) from its own ledger row and spend, with no share of its revision's: a
382
- * run that carries none (not served, or out of the series: archived). Its row, or what its end would have recorded
383
- * (record) for one that has none or whose row no longer stands for it (ledgerRowStands); its audit's shadow read as
384
- * summarize reads it (shadowCostOf). */
362
+ /** One run's figures (learn/ledger.ts figuresOf) with no share of its revision's (a run that carries none: not served, or
363
+ * out of the series), over the ledger of it and its audit's shadow (ledgerRowsOf), as summarize reads them. */
385
364
  async function runFiguresOf(store, run) {
386
- const ledger = await store.learning.ledger(run.agentId, [run.id]);
387
- const mine = ledger.rows.find((row) => row.runId === run.id);
388
- const own = mine && ledgerRowStands(mine, run) ? { row: mine, spend: ledger.spend.filter((one) => one.runId === run.id) } : record(await runEndOf(store, run));
389
- const spend = [...own.spend, ...ledger.spend.filter((one) => one.kind === "audit" && one.ofRun === run.id)];
390
- const shadows = new Map();
391
365
  const shadow = isShadowRun(run.id) ? undefined : await store.runs.get(shadowRunId(run.id));
392
- if (shadow) {
393
- const ended = FINAL_STATES.includes(shadow.state);
394
- shadows.set(run.id, ended ? "ended" : "open");
395
- // A shadow that ended with no audit row (its end named no audit, or it has no ledger row yet) is the run's by its id.
396
- if (ended && !spend.some((one) => one.kind === "audit"))
397
- spend.push(shadowSpend(shadow, run.id));
398
- }
366
+ const listed = shadow ? [run, shadow] : [run];
367
+ const { recorded, spend } = await ledgerRowsOf(store, run.agentId, listed.map((one) => one.id));
368
+ // Every ended run has its row (its end's, or backfillLedger's at the server's open); one read without it reads as its
369
+ // end records it, never as nothing.
370
+ const row = recorded.get(run.id) ?? record(await runEndOf(store, run)).row;
399
371
  const { compile, audit } = spendByRun(spend);
400
- // A shadow run is an audit's whatever its end named: its spend is on the run it audits.
401
- const row = isShadowRun(run.id) ? { ...own.row, path: "audit" } : own.row;
372
+ const shadows = new Map();
373
+ const state = shadowOf(new Map(listed.map((one) => [one.id, one])), run.id);
374
+ if (state)
375
+ shadows.set(run.id, state);
402
376
  return figuresOf(row, { compile: compile.has(run.id) ? compile.get(run.id) : 0, shadow: shadowCostOf(run.id, audit, shadows), share: 0 });
403
377
  }
404
378
  /** The check the serving promotion was decided on, as the cost summary says it: the champion (championOf) of the learned
405
379
  * workflow the newest run a learned workflow ran first on ran (`workflow` or `handed_back`), its record's check numbers
406
- * (the share of the inputs it ran on that said what the agent said, against the agent's own 100%). `cleared`: it passed
407
- * (a champion is promoted), with the agreement check it passed (`agreed` of `alone`, its bar `agree_min`). Null when no run was served, nothing serves now (held, or never promoted), or it was promoted
408
- * on no input (a dry run). The champion skips what `refused` names (the purge gate's). */
380
+ * (the share of the inputs it answered alone that were right on every field, and the agent's share on the same inputs
381
+ * against the same known answers: `agent_agreed`). `cleared`: the joint
382
+ * floor's verdict as the check decided it (`check.joint`, learn/field-check.ts), which every promotion passed. Null when
383
+ * no run was served, nothing serves now (held, or never promoted), or it was promoted on no input (a dry run). The
384
+ * champion skips what `refused` names (the purge gate's). */
409
385
  export function servingReplayOf(records, runs, refused) {
410
386
  const name = runs.findLast((run) => (run.path === "workflow" || run.path === "handed_back") && run.workflow !== null)?.workflow;
411
387
  const check = name ? championOf(records, name, refused)?.check : undefined;
412
388
  if (!check?.n)
413
389
  return null;
414
390
  const agreed = check.agreed ?? 0, alone = check.alone ?? 0;
415
- return { n: check.n, workflow_pct: Math.round(checkShares({ n: check.n, agreed, alone }).agreement * 1000) / 10, agent_pct: 100, cleared: true, agreed, alone, agree_min: check.agree_min };
391
+ const agreement = checkShares({ n: check.n, agreed, alone }).agreement;
392
+ const agent = alone ? (check.agent_agreed ?? 0) / alone : 1;
393
+ return { n: check.n, workflow_pct: Math.round(agreement * 1000) / 10, agent_pct: Math.round(agent * 1000) / 10, cleared: check.joint === true, agreed, alone, agree_min: check.agree_min };
416
394
  }
417
395
  /** Every round run `runId` kept (one a follow-up opened again is a round past its first), oldest first: none when it kept
418
396
  * no learning. The store's proposal(runId) is the newest round alone; a read of what the run's learning did or spent
@@ -434,7 +412,8 @@ export async function roundsOf(store, runId) {
434
412
  * summarize over `listed`, its runs as runs.list read them, newest first): of its newest PAST_RUNS runs on no named disk
435
413
  * (learn/learning-of.ts: one on a named disk learns nothing), those since its learning's reset (`since`,
436
414
  * learn/compile.ts sinceReset: a workflow before it is not the one it learned after, whatever its name), each series
437
- * run among them with its all-in cost as the series reads it (RunFigures.series_total_usd), and the by-hand side over
415
+ * run among them with its all-in cost as the series reads it (RunFigures.series_total_usd; a served run cut at its cap,
416
+ * at its bound), and the by-hand side over
438
417
  * those same runs: the mean job's own $ (RunFigures.usd: no compile turn, follow-up or shadow) of the ones on the
439
418
  * `agent` path, with the ones its mean left out (cost unknown or only a bound). The served side and the by-hand side
440
419
  * read one set of runs: the cost page and a run's launch read the same window (pastRunsOverStore). */
@@ -442,13 +421,30 @@ export function pastRunsOf(out, listed, since) {
442
421
  const scope = listed.filter((run) => run.disk === null).slice(0, PAST_RUNS).filter(since);
443
422
  const runs = scope.flatMap((run) => {
444
423
  const figures = out.figures.get(run.id);
445
- return figures ? [{ metrics: run.metrics, startedAt: run.startedAt, allInUsd: figures.series_total_usd }] : [];
424
+ if (!figures)
425
+ return [];
426
+ // A served run cut at its cap reads at its bound with its shadow: it can only overstate the side that stops.
427
+ const served = SERVED_PATHS.includes(run.metrics?.path);
428
+ const bound = served && typeof run.costAtMostUsd === "number" && figures.shadow_usd !== null ? noiseOff(run.costAtMostUsd + figures.shadow_usd) : null;
429
+ return [{ metrics: run.metrics, startedAt: run.startedAt, allInUsd: figures.series_total_usd ?? bound }];
446
430
  });
447
431
  const ids = new Set(scope.map((run) => run.id));
448
432
  const byHand = out.runs.filter((run) => ids.has(run.run_id) && run.path === "agent");
449
433
  const known = byHand.flatMap((run) => (run.cost_usd !== null && !run.cost_upper_bound ? [run.cost_usd] : []));
450
- const by_hand = known.length ? { runs: known.length, mean_usd: Math.round((known.reduce((a, b) => a + b, 0) / known.length) * 1e6) / 1e6 } : null;
451
- return { runs, by_hand, by_hand_unknown: byHand.length - known.length };
434
+ return { runs, by_hand: meanOf(known), by_hand_unknown: byHand.length - known.length };
435
+ }
436
+ /** pastRunsOf over the store: when the window is full and holds no run by hand of known cost (a workflow that serves well
437
+ * serves every run, so none is left in it), the by-hand side is the job's own $ of the newest COST_WINDOW runs by hand
438
+ * of known cost before it, since the reset (Learning.byHandBefore, one bounded read); null only when there are none. */
439
+ async function pastRunsRead(store, agentId, out, listed) {
440
+ const since = await sinceReset(store, agentId);
441
+ const past = pastRunsOf(out, listed, since);
442
+ const window = listed.filter((run) => run.disk === null).slice(0, PAST_RUNS);
443
+ const oldest = window.at(-1);
444
+ if (past.by_hand !== null || window.length < PAST_RUNS || !oldest || !since(oldest))
445
+ return past;
446
+ const before = (await store.learning.byHandBefore(agentId, oldest, COST_WINDOW)).filter(since);
447
+ return { ...past, by_hand: meanOf(before.map((row) => row.jobUsd)) };
452
448
  }
453
449
  /** What the money rule reads for agent `agentId` at a run's launch (pastRunsOf), bounded whatever the agent's history:
454
450
  * its newest PAST_RUNS runs on no named disk (one runs.list page), the ledger of those runs alone (Learning.ledger with
@@ -458,24 +454,29 @@ export function pastRunsOf(out, listed, since) {
458
454
  export async function pastRunsOverStore(store, agentId) {
459
455
  const listed = (await store.runs.list({ agentId, disk: null, limit: PAST_RUNS })).filter((run) => run.agentId === agentId);
460
456
  const read = await ledgerReadOf(store, agentId, listed, store, { runIds: listed.map((run) => run.id), named: false });
461
- return pastRunsOf(summarize({ ...read, revisions: [] }), listed, await sinceReset(store, agentId));
457
+ return pastRunsRead(store, agentId, summarize({ ...read, revisions: [] }), listed);
462
458
  }
463
459
  /** The money rule's decision for agent `agentId` (runner/workflow-first.ts costStopOf) over the runs a run's launch reads
464
- * (pastRunsOverStore), as the launch reads them: runs that can't be read now stop nothing (workflowFirstOf serves). What
465
- * the promotions read and the learning view fold into each workflow's state (learn/promote.ts servingStateOf). */
466
- export const costStopOverStore = async (store, agentId) => costStopOf(await pastRunsOverStore(store, agentId).catch(() => NO_PAST_RUNS));
460
+ * (pastRunsOverStore), as the launch reads them. Runs it can't read stop every workflow, as a launch then leaves the run
461
+ * to the agent (firstChoice past_unread: it fails closed): the view never says serving when it can't tell. What the
462
+ * promotions read and the learning view fold into each workflow's state (learn/promote.ts servingStateOf). */
463
+ export const costStopOverStore = async (store, agentId) => pastRunsOverStore(store, agentId).then(costStopOf, () => () => true);
467
464
  /** The path run `runId` took and the learned workflow it ran, the one classifier its run record keeps (metrics.path,
468
465
  * metrics.workflow, metrics.workflow_digest: workflow-first.ts pathMetrics, written at the run's end) and its cost per run
469
466
  * reads (pathOfRow). A run its learned workflow served first (its WORKFLOW_FIRST entry, servedOf): `workflow` when it
470
467
  * answered and its answer is still the run's last (servedAnswerStands), `handed_back` when it handed back or a follow-up's
471
468
  * new answer is the agent's, with its digest. Else, for a run its launch offered a learned workflow, its timeline
472
469
  * (runner/learning-end.ts runPathOf, the agent calling the tool itself): `workflow` when its job did only that,
473
- * `handed_back` when the agent went on, with the workflow its job called last. Any other run's path is `agent`. Both
474
- * read the run's rows less its feedback rounds' (withoutFeedbackRounds): a grade's round never moves the path. `cut` on a
475
- * hand-back a restart cut (servedOf: the workflow's call ended `interrupted`), never one the workflow made itself. */
476
- export async function runPathOverStore(store, runId, started, redact) {
477
- const entries = await runEntries(store, runId, BACKGROUND_CONTEXT);
478
- const served = servedOf(entries);
470
+ * `handed_back` when the agent went on, with the workflow it called last that ran (its call's record,
471
+ * runner/workflow-calls.ts workflowCallsOf). Any other run's path is `agent`. Both read the run's rows less its feedback rounds' (withoutFeedbackRounds): a grade's
472
+ * round never moves the path. `cut` on a hand-back a restart cut (servedOf: the workflow's call ended `interrupted`),
473
+ * never one the workflow made itself. `entries`: the run's (runEntries), when the caller read them already. */
474
+ export async function runPathOverStore(store, runId, started, redact, entries) {
475
+ entries ??= await runEntries(store, runId, BACKGROUND_CONTEXT);
476
+ // A platform call refused before it ran anything (at its gate, or by the tool: its record's `refused`) served nothing:
477
+ // the run is classified as the agent's own calls say.
478
+ const calls = workflowCallsOf(entries);
479
+ const served = calls.some((call) => call.caller === "platform" && call.outcome === "refused") ? undefined : servedOf(entries);
479
480
  if (!served && !started?.workflows?.length)
480
481
  return { path: "agent", workflow: null };
481
482
  // The path is who wrote the run's final answer, read on the job's own rows: a grade's feedback round asks the agent no
@@ -491,8 +492,26 @@ export async function runPathOverStore(store, runId, started, redact) {
491
492
  return { path: alone ? "workflow" : "handed_back", workflow: served.workflow, ...(served.digest ? { digest: served.digest } : {}), ...(served.promotion !== undefined ? { promotion: served.promotion } : {}), ...(served.cut ? { cut: true } : {}) };
492
493
  }
493
494
  const path = runPathOf(rows);
494
- // Only a name the launch offered: a call's raw `name` (a failed call's included) is the model's text, never shown.
495
- return { path, workflow: path === "agent" ? null : offeredName(workflowNameOf([...callArgsOf(entries).values()]), started?.workflows) };
495
+ if (path === "agent")
496
+ return { path, workflow: null };
497
+ // The workflow its agent called last that ran (its call's record), and only a name the launch offered (offeredName),
498
+ // as every view of the run names it; none for a call that ran no workflow (a name it does not have, a gate's refusal).
499
+ // A run that keeps no call record at all (a connected agent's: its SDK runs the workflow, the server keeps its events)
500
+ // names the one its last learned_workflow call asked for by name (callArgsOf).
501
+ const called = calls.length ? calls.filter((call) => call.caller === "agent" && call.outcome !== "refused").at(-1)?.workflow : [...callArgsOf(entries).values()].findLast((call) => WORKFLOW_CALLS.has(call.tool) && typeof call.args.name === "string")?.args.name;
502
+ return { path, workflow: offeredName(typeof called === "string" ? called : null, started?.workflows) };
503
+ }
504
+ /** The run-record fields run `runId`'s end writes for its path (runner/workflow-first.ts pathMetrics), the one writer
505
+ * every harness's end calls: the run's entries read once (runEntries, every conversation: a call's booking is read),
506
+ * classified (runPathOverStore, for a run that learns) and kept with its calls (`workflow_calls`) and why its ending
507
+ * claim's first choice did or did not serve (`choice`, firstChoice's reason). `redact`: the run's cut, as its
508
+ * follow-up showed a served answer. */
509
+ export async function runPathMetricsOverStore(store, runId, launch, { redact, choice } = {}) {
510
+ if (!launch.learning && !launch.audit)
511
+ return {};
512
+ const entries = await runEntries(store, runId, BACKGROUND_CONTEXT);
513
+ const classified = launch.learning ? await runPathOverStore(store, runId, launch.learning, redact, entries) : undefined;
514
+ return pathMetrics(launch, classified, entries, choice);
496
515
  }
497
516
  /** A run's path and learned workflow as its record keeps them (`metrics`: runPathOverStore at its end); `agent` for a run
498
517
  * that does not learn (its launch has no learning start: its record names none). Undefined for a run that learns whose
@@ -511,11 +530,20 @@ function pathOfRow(metrics, started) {
511
530
  * .../learning/cost never disagree). `path` is null for a run not ended, a workflow revision's run, and a run that learns
512
531
  * whose record names none; `workflow` names the learned workflow and its revision only on a run it served (path
513
532
  * `workflow` or `handed_back`; the name only when its launch offered one, the digest null when its record names none,
514
- * and whether its answer re-picked, repickOfRow), else null. */
533
+ * and whether its answer re-picked, repickOfRow), else null. `workflow_calls`: every learned workflow call its record
534
+ * keeps (learning-cost.ts workflowCallsIn: the platform's and the agent's, each with its digest, caller, outcome and
535
+ * booked $), none when it made none, null where `path` is; `first_choice`: why its ending claim's first choice served
536
+ * or did not (runner/workflow-first.ts FIRST_REASONS), null where its record names none. */
515
537
  export function pathViewOf(run, kind, started) {
516
538
  const placed = kind === "workflow" || !FINAL_STATES.includes(run.state) ? undefined : pathOfRow(run.metrics, started);
517
539
  const served = placed && SERVED_PATHS.includes(placed.path);
518
- return { path: placed?.path ?? null, workflow: served ? { name: placed.workflow, digest: revisionOfRow(run)?.digest ?? null, repick: repickOfRow(run) } : null };
540
+ const chose = run.metrics?.first_choice;
541
+ return {
542
+ path: placed?.path ?? null,
543
+ workflow: served ? { name: placed.workflow, digest: revisionOfRow(run)?.digest ?? null, repick: repickOfRow(run) } : null,
544
+ workflow_calls: placed ? workflowCallsIn(run.metrics) : null,
545
+ first_choice: placed && FIRST_REASONS.includes(chose) ? chose : null,
546
+ };
519
547
  }
520
548
  /** Whether the workflow a run's record names re-picked its answer, from its metrics' loop rows (workflow-first.ts
521
549
  * pathMetrics `loops`, LoopRow): its last re-pick loop (marked `repick`, learn/check.ts repickLabels) of that workflow
@@ -6,8 +6,8 @@ export type RunState = RunReadState;
6
6
  export type RunCounts = Record<RunState, number> & {
7
7
  total: number;
8
8
  };
9
- /** `usd` sums the `known` runs' costs (those that ended with one); null when there are none, never 0. A run still running,
10
- * or one that ended with no cost, is `unknown`. `basis`: how the summed costs were reached (metrics.ts joinedBasis over
9
+ /** `usd` sums the costs of the runs that ended with one and their shadows' known costs; null when there are none, never
10
+ * 0. A run still running, one that ended with no cost, or one whose shadow is at work or unpriced, is `unknown`. `basis`: how the summed costs were reached (metrics.ts joinedBasis over
11
11
  * each one's stored basis, storedBasisOf). */
12
12
  export type CostSum = {
13
13
  usd: number | null;