@parcha/agentrun-serve 0.1.0-beta.43 → 0.1.0-beta.45

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 (388) hide show
  1. package/CHANGELOG.md +231 -0
  2. package/README.md +580 -320
  3. package/dist/acp-package.d.ts +28 -0
  4. package/dist/acp-package.js +23 -0
  5. package/dist/adapter-facts.d.ts +8 -1
  6. package/dist/adapter-facts.js +9 -1
  7. package/dist/adapters.d.ts +5 -0
  8. package/dist/adapters.js +36 -3
  9. package/dist/agent-delete.d.ts +50 -0
  10. package/dist/agent-delete.js +125 -0
  11. package/dist/agents-store.d.ts +9 -3
  12. package/dist/agents-store.js +22 -8
  13. package/dist/agents.d.ts +5 -1
  14. package/dist/agents.js +8 -4
  15. package/dist/api-only.d.ts +8 -3
  16. package/dist/api-only.js +10 -4
  17. package/dist/archil-routes.d.ts +5 -1
  18. package/dist/archil-routes.js +57 -2
  19. package/dist/browser-wait-ended.d.ts +2 -0
  20. package/dist/browser-wait-ended.js +2 -0
  21. package/dist/calendar/ics-faux.d.ts +3 -1
  22. package/dist/calendar/ics-faux.js +12 -2
  23. package/dist/calendar/tools.js +5 -2
  24. package/dist/chatgpt-plan.js +3 -1
  25. package/dist/claude-login.d.ts +135 -0
  26. package/dist/claude-login.js +244 -0
  27. package/dist/cli.js +92 -22
  28. package/dist/composio-preset.d.ts +4 -3
  29. package/dist/composio-preset.js +81 -11
  30. package/dist/composio-triggers.d.ts +34 -2
  31. package/dist/composio-triggers.js +261 -31
  32. package/dist/computer-attach.d.ts +40 -5
  33. package/dist/computer-attach.js +130 -15
  34. package/dist/computer-keys.d.ts +3 -0
  35. package/dist/computer-keys.js +7 -1
  36. package/dist/computer-live.js +3 -2
  37. package/dist/computer-lost.d.ts +11 -0
  38. package/dist/computer-lost.js +19 -0
  39. package/dist/computer-record.d.ts +10 -2
  40. package/dist/computer-record.js +50 -18
  41. package/dist/computer-setting.d.ts +1 -0
  42. package/dist/computer-setting.js +2 -2
  43. package/dist/computers.d.ts +21 -10
  44. package/dist/computers.js +39 -28
  45. package/dist/console/assets/index-3O2Yl6hZ.js +701 -0
  46. package/dist/console/assets/index-DaFsZuzQ.css +1 -0
  47. package/dist/console/index.html +2 -2
  48. package/dist/decisions.d.ts +1 -1
  49. package/dist/dial-relay.d.ts +29 -0
  50. package/dist/dial-relay.js +94 -0
  51. package/dist/email/tools.js +10 -5
  52. package/dist/env-proxy.d.ts +2 -0
  53. package/dist/env-proxy.js +35 -0
  54. package/dist/errors.d.ts +4 -1
  55. package/dist/errors.js +10 -4
  56. package/dist/faux-computers.d.ts +8 -2
  57. package/dist/faux-computers.js +83 -6
  58. package/dist/harness-archive.d.ts +67 -0
  59. package/dist/harness-archive.js +177 -0
  60. package/dist/harness-goose.d.ts +34 -0
  61. package/dist/harness-goose.js +87 -0
  62. package/dist/harness-install.d.ts +21 -0
  63. package/dist/harness-install.js +24 -11
  64. package/dist/harness-opencode/adapter.js +1 -1
  65. package/dist/harness-table.d.ts +59 -3
  66. package/dist/harness-table.js +119 -14
  67. package/dist/harness.d.ts +17 -1
  68. package/dist/harness.js +22 -11
  69. package/dist/host.d.ts +12 -10
  70. package/dist/host.js +19 -16
  71. package/dist/import-worker.d.ts +3 -0
  72. package/dist/import-worker.js +7 -5
  73. package/dist/import.d.ts +22 -1
  74. package/dist/import.js +141 -28
  75. package/dist/key-check.js +3 -0
  76. package/dist/learn/audit.d.ts +6 -6
  77. package/dist/learn/audit.js +3 -7
  78. package/dist/learn/check.d.ts +26 -89
  79. package/dist/learn/check.js +58 -336
  80. package/dist/learn/commit.d.ts +63 -90
  81. package/dist/learn/commit.js +124 -316
  82. package/dist/learn/compile.d.ts +53 -50
  83. package/dist/learn/compile.js +88 -103
  84. package/dist/learn/corrections.d.ts +13 -0
  85. package/dist/learn/corrections.js +23 -0
  86. package/dist/learn/delete.d.ts +17 -0
  87. package/dist/learn/delete.js +42 -0
  88. package/dist/learn/folder.d.ts +35 -47
  89. package/dist/learn/folder.js +118 -134
  90. package/dist/learn/index.d.ts +3 -5
  91. package/dist/learn/index.js +6 -9
  92. package/dist/learn/learning.sh +45 -15
  93. package/dist/learn/promote.d.ts +20 -2
  94. package/dist/learn/promote.js +17 -4
  95. package/dist/learn/{skills.d.ts → workflow-draft.d.ts} +34 -38
  96. package/dist/learn/{skills.js → workflow-draft.js} +65 -59
  97. package/dist/learn/workflow-nodes.d.ts +22 -1
  98. package/dist/learn/workflow-nodes.js +29 -10
  99. package/dist/learned-workflow.js +2 -2
  100. package/dist/learning-cost.d.ts +127 -13
  101. package/dist/learning-cost.js +142 -10
  102. package/dist/learning-routes.d.ts +99 -170
  103. package/dist/learning-routes.js +199 -420
  104. package/dist/learning-run.d.ts +19 -40
  105. package/dist/learning-run.js +23 -93
  106. package/dist/learning-setting.d.ts +4 -4
  107. package/dist/learning-setting.js +6 -6
  108. package/dist/learning-words.d.ts +28 -74
  109. package/dist/learning-words.js +64 -162
  110. package/dist/learning.d.ts +62 -393
  111. package/dist/learning.js +101 -651
  112. package/dist/listen-first.d.ts +30 -0
  113. package/dist/listen-first.js +115 -0
  114. package/dist/live-reach.d.ts +25 -2
  115. package/dist/live-reach.js +46 -4
  116. package/dist/llms.txt +2 -1
  117. package/dist/local-models.d.ts +3 -11
  118. package/dist/local-models.js +5 -6
  119. package/dist/mcp-servers-store.d.ts +16 -16
  120. package/dist/model-form.js +6 -5
  121. package/dist/model.d.ts +12 -4
  122. package/dist/model.js +41 -16
  123. package/dist/native/binding.js +1 -1
  124. package/dist/native/connection.js +1 -1
  125. package/dist/native/discovery.js +1 -1
  126. package/dist/native/instructions.d.ts +3 -0
  127. package/dist/native/instructions.js +5 -3
  128. package/dist/native/remote-worker-bridge.js +9 -43
  129. package/dist/native/remote.d.ts +8 -15
  130. package/dist/native/remote.js +3 -3
  131. package/dist/native/schema.js +1 -1
  132. package/dist/native/types.d.ts +2 -2
  133. package/dist/openapi.js +375 -347
  134. package/dist/owner.d.ts +8 -0
  135. package/dist/owner.js +11 -0
  136. package/dist/paid-calls.d.ts +14 -0
  137. package/dist/paid-calls.js +31 -1
  138. package/dist/particle-provider.d.ts +11 -0
  139. package/dist/particle-provider.js +135 -0
  140. package/dist/place.d.ts +8 -3
  141. package/dist/place.js +13 -9
  142. package/dist/port-models.d.ts +12 -4
  143. package/dist/port-models.js +28 -11
  144. package/dist/print.d.ts +27 -0
  145. package/dist/print.js +21 -0
  146. package/dist/provider.d.ts +7 -0
  147. package/dist/provider.js +30 -2
  148. package/dist/reflect-range.js +2 -3
  149. package/dist/run-launch.d.ts +14 -28
  150. package/dist/run-launch.js +9 -12
  151. package/dist/run-learning-served.d.ts +16 -90
  152. package/dist/run-listing.d.ts +34 -0
  153. package/dist/run-listing.js +128 -0
  154. package/dist/run-model-key.d.ts +64 -0
  155. package/dist/run-model-key.js +147 -0
  156. package/dist/run-timeline-served.js +5 -3
  157. package/dist/run-trace.d.ts +97 -0
  158. package/dist/run-trace.js +253 -0
  159. package/dist/run-view.d.ts +15 -0
  160. package/dist/run-view.js +9 -0
  161. package/dist/runnable.js +2 -1
  162. package/dist/runner/adapter-loop.d.ts +14 -6
  163. package/dist/runner/adapter-loop.js +25 -14
  164. package/dist/runner/adapter-run-spec.d.ts +22 -7
  165. package/dist/runner/adapter-run-spec.js +130 -77
  166. package/dist/runner/adapters/index.d.ts +4 -2
  167. package/dist/runner/adapters/index.js +35 -4
  168. package/dist/runner/adapters/pi.d.ts +4 -12
  169. package/dist/runner/adapters/pi.js +2 -2
  170. package/dist/runner/audit.d.ts +5 -1
  171. package/dist/runner/audit.js +31 -12
  172. package/dist/runner/beside.d.ts +7 -2
  173. package/dist/runner/beside.js +41 -9
  174. package/dist/runner/claim-secrets.d.ts +66 -1
  175. package/dist/runner/claim-secrets.js +136 -3
  176. package/dist/runner/cli-run-spec.d.ts +2 -1
  177. package/dist/runner/cli-run-spec.js +82 -59
  178. package/dist/runner/commands.d.ts +3 -6
  179. package/dist/runner/conformance/harness.d.ts +6 -0
  180. package/dist/runner/conformance/harness.js +16 -5
  181. package/dist/runner/dispatch.d.ts +2 -2
  182. package/dist/runner/end-of-run.d.ts +6 -14
  183. package/dist/runner/end-of-run.js +8 -33
  184. package/dist/runner/feedback-round.d.ts +5 -4
  185. package/dist/runner/feedback-round.js +14 -10
  186. package/dist/runner/github-app.js +3 -1
  187. package/dist/runner/harness-loops.d.ts +0 -8
  188. package/dist/runner/harness-loops.js +30 -56
  189. package/dist/runner/held.d.ts +170 -0
  190. package/dist/runner/held.js +57 -0
  191. package/dist/runner/journal-kinds.d.ts +7 -3
  192. package/dist/runner/journal-kinds.js +2 -3
  193. package/dist/runner/learned-first.d.ts +40 -0
  194. package/dist/runner/learned-first.js +32 -0
  195. package/dist/runner/learning-end.d.ts +77 -66
  196. package/dist/runner/learning-end.js +250 -299
  197. package/dist/runner/learning-seed.d.ts +15 -33
  198. package/dist/runner/learning-seed.js +24 -45
  199. package/dist/runner/loop-contract.d.ts +8 -3
  200. package/dist/runner/loop-contract.js +4 -0
  201. package/dist/runner/loop.js +6 -2
  202. package/dist/runner/main.d.ts +18 -9
  203. package/dist/runner/main.js +82 -22
  204. package/dist/runner/mcp-run.d.ts +11 -0
  205. package/dist/runner/mcp-run.js +67 -0
  206. package/dist/runner/memory-claim.d.ts +14 -6
  207. package/dist/runner/memory-claim.js +18 -9
  208. package/dist/runner/model-route.d.ts +7 -4
  209. package/dist/runner/model-route.js +22 -10
  210. package/dist/runner/native-loop.d.ts +0 -7
  211. package/dist/runner/native-loop.js +9 -101
  212. package/dist/runner/promotion.d.ts +13 -6
  213. package/dist/runner/promotion.js +63 -17
  214. package/dist/runner/reflect-hook.d.ts +64 -91
  215. package/dist/runner/reflect-hook.js +112 -206
  216. package/dist/runner/run-channel.d.ts +43 -0
  217. package/dist/runner/run-channel.js +467 -0
  218. package/dist/runner/run-child-host.d.ts +169 -0
  219. package/dist/runner/run-child-host.js +1222 -0
  220. package/dist/runner/run-child.d.ts +1 -0
  221. package/dist/runner/run-child.js +254 -0
  222. package/dist/runner/run-errors.d.ts +2 -0
  223. package/dist/runner/run-errors.js +20 -0
  224. package/dist/runner/runner.js +58 -9
  225. package/dist/runner/sdk-ask.d.ts +78 -0
  226. package/dist/runner/sdk-ask.js +158 -0
  227. package/dist/runner/sdk-loop.d.ts +2 -4
  228. package/dist/runner/sdk-loop.js +29 -49
  229. package/dist/runner/store-secrets.d.ts +5 -3
  230. package/dist/runner/store-secrets.js +14 -6
  231. package/dist/runner/workflow-first.d.ts +5 -0
  232. package/dist/runner/workflow-first.js +4 -0
  233. package/dist/runner/workflow-spec.js +17 -4
  234. package/dist/runs-over-store.d.ts +21 -32
  235. package/dist/runs-over-store.js +44 -41
  236. package/dist/sdk/runs.d.ts +6 -11
  237. package/dist/sdk/runs.js +20 -41
  238. package/dist/sdk/schema.js +3 -4
  239. package/dist/sdk/workflows.d.ts +1 -1
  240. package/dist/seal-key.d.ts +5 -0
  241. package/dist/seal-key.js +7 -0
  242. package/dist/secrets.d.ts +3 -0
  243. package/dist/secrets.js +16 -3
  244. package/dist/server.d.ts +119 -83
  245. package/dist/server.js +1121 -419
  246. package/dist/sign-ins-store.d.ts +8 -0
  247. package/dist/sign-ins-store.js +36 -1
  248. package/dist/skills.js +1 -1
  249. package/dist/store/index.d.ts +3 -3
  250. package/dist/store/index.js +2 -2
  251. package/dist/store/journal/storage.js +3 -0
  252. package/dist/store/postgres/migrations/0017_run_listing.sql +83 -0
  253. package/dist/store/postgres/migrations/0018_trigger_template.sql +6 -0
  254. package/dist/store/postgres/migrations/0019_runner_relays.sql +28 -0
  255. package/dist/store/schema.d.ts +53 -4
  256. package/dist/store/schema.js +137 -6
  257. package/dist/store/seal.d.ts +20 -2
  258. package/dist/store/seal.js +24 -8
  259. package/dist/store/sqlite/connection.d.ts +2 -0
  260. package/dist/store/sqlite/connection.js +3 -1
  261. package/dist/store/sqlite/run-launch.d.ts +4 -1
  262. package/dist/store/sqlite/run-launch.js +12 -0
  263. package/dist/store/sqlite/run-patch.d.ts +0 -17
  264. package/dist/store/sqlite/run-patch.js +4 -33
  265. package/dist/store/sqlite/store.d.ts +3 -2
  266. package/dist/store/sqlite/store.js +360 -75
  267. package/dist/store/sqlite/worker.js +25 -0
  268. package/dist/store/store.d.ts +231 -39
  269. package/dist/store/store.js +25 -3
  270. package/dist/store-reads/analytics.js +11 -1
  271. package/dist/store-reads/learning.d.ts +131 -194
  272. package/dist/store-reads/learning.js +292 -301
  273. package/dist/store-reads/loop.d.ts +0 -1
  274. package/dist/store-reads/loop.js +1 -1
  275. package/dist/store-reads/stats.js +13 -3
  276. package/dist/store-view.d.ts +34 -2
  277. package/dist/store-view.js +108 -41
  278. package/dist/telemetry.d.ts +59 -0
  279. package/dist/telemetry.js +153 -0
  280. package/dist/template-eval.d.ts +3 -6
  281. package/dist/template-eval.js +22 -36
  282. package/dist/templates.d.ts +2 -1
  283. package/dist/templates.js +6 -3
  284. package/dist/timeline/index.d.ts +1 -1
  285. package/dist/timeline/index.js +1 -1
  286. package/dist/timeline/timeline.d.ts +5 -1
  287. package/dist/timeline/timeline.js +16 -0
  288. package/dist/tool-gate.d.ts +0 -7
  289. package/dist/tool-gate.js +1 -15
  290. package/dist/tool-keys-store.d.ts +1 -1
  291. package/dist/tool-keys.d.ts +6 -4
  292. package/dist/tool-keys.js +12 -6
  293. package/dist/tool-plane.d.ts +12 -12
  294. package/dist/tools.d.ts +13 -13
  295. package/dist/tools.js +29 -9
  296. package/dist/trigger-adapter.d.ts +3 -0
  297. package/dist/trigger-adapters.d.ts +4 -0
  298. package/dist/trigger-adapters.js +13 -0
  299. package/dist/trigger-composio.d.ts +58 -1
  300. package/dist/trigger-composio.js +160 -6
  301. package/dist/triggers.d.ts +64 -5
  302. package/dist/triggers.js +141 -17
  303. package/dist/validate.d.ts +11 -7
  304. package/dist/validate.js +9 -8
  305. package/dist/webhooks.d.ts +4 -1
  306. package/dist/webhooks.js +11 -4
  307. package/dist/workspace/agent-computer.d.ts +2 -0
  308. package/dist/workspace/agent-computer.js +2 -0
  309. package/dist/workspace/archil-claim.d.ts +7 -0
  310. package/dist/workspace/archil-claim.js +29 -0
  311. package/dist/workspace/archil.d.ts +9 -1
  312. package/dist/workspace/archil.js +165 -3
  313. package/dist/workspace/attach.d.ts +3 -1
  314. package/dist/workspace/attach.js +2 -0
  315. package/dist/workspace/attached-computers.d.ts +16 -5
  316. package/dist/workspace/attached-computers.js +25 -9
  317. package/dist/workspace/computer-deps.js +1 -1
  318. package/dist/workspace/computer-for-claim.d.ts +22 -8
  319. package/dist/workspace/computer-for-claim.js +339 -46
  320. package/dist/workspace/computer-hold.js +2 -1
  321. package/dist/workspace/computer-kept.d.ts +11 -1
  322. package/dist/workspace/computer-kept.js +24 -3
  323. package/dist/workspace/resolve-computer.d.ts +41 -21
  324. package/dist/workspace/resolve-computer.js +172 -64
  325. package/dist/workspace/revision-skills.d.ts +1 -2
  326. package/dist/workspace/revision-skills.js +3 -4
  327. package/dist/workspace/run-workspace.d.ts +1 -1
  328. package/dist/workspace/run-workspace.js +5 -0
  329. package/dist/workspace/synced-folder.d.ts +85 -0
  330. package/dist/workspace/synced-folder.js +230 -0
  331. package/dist/workspace/workspace.d.ts +4 -0
  332. package/launcher/agentrun-serve +1 -1
  333. package/package.json +10 -9
  334. package/templates/README.md +7 -7
  335. package/templates/inbound-lead/agent.md +3 -3
  336. package/templates/inbound-lead/evals/learned-correction/case.yaml +3 -3
  337. package/templates/inbound-lead/evals/learned-correction/fixtures/corrections.md +1 -0
  338. package/templates/inbound-lead/evals/learned-correction/graders/applies-correction.md +22 -0
  339. package/templates/inbound-lead/evals/learned-correction/graders/learned-origin.md +1 -1
  340. package/templates/inbound-lead/evals/learned-correction/prompt.md +1 -1
  341. package/templates/inbound-lead/lead.schema.json +6 -5
  342. package/templates/inbound-lead/rules/RUBRIC.md +4 -4
  343. package/templates/inbound-lead/skills/lead/SKILL.md +4 -4
  344. package/templates/market-monitor/evals/learned-preference/case.yaml +4 -4
  345. package/templates/market-monitor/evals/learned-preference/fixtures/corrections.md +1 -0
  346. package/templates/market-monitor/evals/learned-preference/graders/omits-junior-post.md +3 -3
  347. package/templates/market-monitor/evals/learned-preference/prompt.md +1 -1
  348. package/templates/release-notes/evals/learned-style/case.yaml +4 -4
  349. package/templates/release-notes/evals/learned-style/fixtures/corrections.md +1 -0
  350. package/templates/release-notes/evals/learned-style/graders/same-changes.md +2 -2
  351. package/templates/release-notes/evals/learned-style/prompt.md +1 -1
  352. package/dist/console/assets/index-BrcDSBD8.css +0 -1
  353. package/dist/console/assets/index-Bz6IWncl.js +0 -711
  354. package/dist/learn/grade.d.ts +0 -138
  355. package/dist/learn/grade.js +0 -682
  356. package/dist/learn/notes.d.ts +0 -81
  357. package/dist/learn/notes.js +0 -178
  358. package/dist/learn/reflect.d.ts +0 -77
  359. package/dist/learn/reflect.js +0 -228
  360. package/dist/learn/relevance.d.ts +0 -24
  361. package/dist/learn/relevance.js +0 -91
  362. package/dist/learn/seed-check.d.ts +0 -74
  363. package/dist/learn/seed-check.js +0 -103
  364. package/dist/learning-batch.d.ts +0 -291
  365. package/dist/learning-batch.js +0 -600
  366. package/dist/learning-evals.d.ts +0 -33
  367. package/dist/learning-evals.js +0 -98
  368. package/dist/native/context.d.ts +0 -6
  369. package/dist/native/context.js +0 -22
  370. package/dist/propose.d.ts +0 -52
  371. package/dist/propose.js +0 -224
  372. package/dist/runner/learning-ask.d.ts +0 -47
  373. package/dist/runner/learning-ask.js +0 -128
  374. package/dist/runner/learning-dispatch.d.ts +0 -45
  375. package/dist/runner/learning-dispatch.js +0 -99
  376. package/dist/runner/learning-factory.d.ts +0 -16
  377. package/dist/runner/learning-factory.js +0 -50
  378. package/dist/runner/learning-loop.d.ts +0 -273
  379. package/dist/runner/learning-loop.js +0 -521
  380. package/dist/usage.d.ts +0 -11
  381. package/dist/usage.js +0 -11
  382. package/templates/inbound-lead/evals/learned-correction/fixtures/learning/MEMORY.md +0 -1
  383. package/templates/inbound-lead/evals/learned-correction/fixtures/learning/no-agencies.md +0 -14
  384. package/templates/inbound-lead/evals/learned-correction/graders/applies-note.md +0 -21
  385. package/templates/market-monitor/evals/learned-preference/fixtures/learning/MEMORY.md +0 -1
  386. package/templates/market-monitor/evals/learned-preference/fixtures/learning/skip-junior-job-posts.md +0 -8
  387. package/templates/release-notes/evals/learned-style/fixtures/learning/MEMORY.md +0 -1
  388. package/templates/release-notes/evals/learned-style/fixtures/learning/say-agent-not-bot.md +0 -13
@@ -33,7 +33,7 @@ import { defineTool, ROOT_CONVERSATION_ID } from "@earendil-works/pi-durable";
33
33
  import { registerLabel, registerOutcome } from "./timeline/index.js";
34
34
  import { LEARNED_NODES, LEARNED_NODES_MAX } from "./learned-nodes.js";
35
35
  import { draftProblems, LEARNED_MAX_TURNS } from "./learn/index.js";
36
- import { decisionsAsked, deniedDecisionWords } from "./learn/skills.js";
36
+ import { decisionsAsked, deniedDecisionWords } from "./learn/workflow-draft.js";
37
37
  import { DEFAULT_DECISION_PROVIDER } from "./decisions.js";
38
38
  import { workflowLine } from "./learning.js";
39
39
  import { ledgerOf } from "./paid-calls.js";
@@ -478,7 +478,7 @@ export async function runLearned(run, flow, input, signal, ledger, nodes = [], n
478
478
  * other node shares. Only letters, digits, `_` and `-`, so a `/` of its path never reads as another level of nesting
479
479
  * and its call id passes whole through a URL (an approval's). */
480
480
  const nestedKey = (label, executionPath, item) => `${label.replace(/[^A-Za-z0-9_-]/g, "_").slice(0, 40)}-${createHash("sha256").update(JSON.stringify([label, executionPath ?? "", item?.index ?? null])).digest("hex").slice(0, 16)}`;
481
- /** The draft check's decisions for `run` (learn/skills.ts DraftDecisions): its providers, and the gate's setting of each
481
+ /** The draft check's decisions for `run` (learn/workflow-draft.ts DraftDecisions): its providers, and the gate's setting of each
482
482
  * provider `text` asks (its bare id as the settings key) where it is not allow, with where a deny was set. A run handed
483
483
  * no decisions (its claim was handed no key: none named, or each denied) has no provider, so a decision node is refused
484
484
  * in the gate's words when its provider is denied, else as one the host does not have; never asked. */
@@ -16,31 +16,41 @@ export type CostLooked = {
16
16
  job_cost_usd?: number | null;
17
17
  look_back_usd?: number | null;
18
18
  };
19
+ /** A booked figure with its float noise rounded off, nothing more (runner/metrics.ts floatNoiseOff, the precision a run's
20
+ * own cost is kept at): a decision costs millionths of a dollar, so a figure rounded to the millionth would lose it, and
21
+ * the run's cost here would read other than its usage row (A8). `+ 0` makes a noise-sized negative a plain 0, never -0. */
22
+ export declare const noiseOff: (value: number) => number;
19
23
  /** A run's own cost and what learning added for it, from its row's cost (`rowUsd`: the job, its look-back and any turn a
20
- * person asked of it after), what it kept as it answered (`looked`, none for a run that did not look back) and what its
21
- * grade spent (`gradeUsd`: its learning run's cost once that run ended, stopped or failed, 0 while it runs or when there
22
- * is none, null when unknown):
24
+ * person asked of it after) and what it kept as it answered (`looked`, none for a run that did not look back):
23
25
  * - `cost_usd`: the job's own $: its row's cost less its look-back, never under what it had cost when it answered (a
24
26
  * later turn's spend is the job's too); its row's cost for a run that did not look back;
25
- * - `learning_usd`: what learning cost for it: its look-back and its grade, 0 for a run that does not learn; null when
26
- * either is unknown (a grade that failed or was stopped mid-ask spent what no one knows);
27
+ * - `learning_usd`: what learning cost for it: its look-back (its compile turn) and what was spent beside it
28
+ * (`besideUsd`: its audit's shadow run), 0 for a run that does not learn; null when unknown (a turn stopped mid-ask
29
+ * spent what no one knows, or a shadow still running);
27
30
  * - `total_usd`: their sum, the run's all-in $; null when either is. */
28
- export declare function runCostOf(rowUsd: number | null, looked: CostLooked | null, gradeUsd?: number | null): {
31
+ export declare function runCostOf(rowUsd: number | null, looked: CostLooked | null, besideUsd?: number | null): {
29
32
  cost_usd: number | null;
30
33
  learning_usd: number | null;
31
34
  total_usd: number | null;
32
35
  };
33
- /** One run of the series, in the order the agent's runs started: `n` its number among them (1 the first). */
36
+ /** One run of the series, in the order the agent's runs started: `n` its number among the runs that do a job (JOB_PATHS,
37
+ * 1 the first: the bill's numbering), null for a replay, which checks a revision and does no job. */
34
38
  export type CostRun = {
35
- n: number;
39
+ n: number | null;
36
40
  run_id: string;
37
41
  started_at: string;
38
42
  path: RunPath;
39
43
  /** The learned workflow its job called last, by name; null when it called none. */
40
44
  workflow: string | null;
41
- /** The commit of the agent's learning folder the run started from (its learning start's seed): the learned revision
42
- * that served it. Null for a run that does not learn. */
43
- learned_revision: string | null;
45
+ /** The revision of that workflow that ran it (its record's metrics.workflow_digest: the first 16 hex characters of its
46
+ * workflow.json's sha256); null when no learned workflow ran. One value while one revision serves. */
47
+ digest: string | null;
48
+ /** The first run its revision (`workflow` at `digest`) served, over every run of the series (tookOverOf): where that
49
+ * revision took over, its promotion's flag on the chart. The first of them is the summary's takeover (a workflow that
50
+ * ran first always names its revision); false for a replay and a run no revision ran (`digest` null). */
51
+ took_over: boolean;
52
+ /** An audit's shadow run was started for it (runner/audit.ts): its cost is in `learning_usd`. */
53
+ audited: boolean;
44
54
  cost_usd: number | null;
45
55
  /** Its stored cost is only a bound (the run row's `costAtMostUsd`: a request cut at its cap hold): `cost_usd` and `total_usd` are null, the bound is never shown as a cost. */
46
56
  cost_upper_bound: boolean;
@@ -71,11 +81,17 @@ export type CostReplay = {
71
81
  /** The paths a learned workflow served a run on: it ran first and answered alone (`workflow`) or handed back
72
82
  * (`handed_back`). A `replay` checks a new revision on a graded input and serves no one; an `agent` run is by hand. */
73
83
  export declare const SERVED_PATHS: readonly RunPath[];
84
+ /** Each run of the series (every run, oldest first, before a read's limit) with `took_over`: true on the first run a
85
+ * revision (`workflow` at `digest`) served (SERVED_PATHS), never on a replay or a run no revision ran. A promotion is
86
+ * written as a run is claimed, after the runs queued before it started, so where a revision took over is read from the
87
+ * runs it served, never from start times. */
88
+ export declare function tookOverOf(runs: readonly Omit<CostRun, "took_over">[]): CostRun[];
74
89
  /** Why the after side has a figure or not: `priced`, it has; `no_served_runs`, no learned workflow has taken over (a
75
90
  * replay is no served run); `cost_unknown`, runs were served but none's all-in cost is known exactly. */
76
91
  export declare const AFTER_STATES: readonly ["priced", "no_served_runs", "cost_unknown"];
77
92
  export type AfterState = (typeof AFTER_STATES)[number];
78
- /** What the series shows: `takeover`, its first `workflow` run, null before one; `before`, the mean of the job's own $
93
+ /** What the series shows: `takeover`, the first run a learned workflow served (SERVED_PATHS: it answered alone or handed
94
+ * back; the bill's `promoted`), null before one; `before`, the mean of the job's own $
79
95
  * (learning left out) over its `agent` runs before the takeover (every one when there is none yet), whose cost is known
80
96
  * exactly; `after`, the mean all-in $ (learning included) of the runs a learned workflow served from the takeover on
81
97
  * (SERVED_PATHS: never a replay, which checks a revision and serves no one, nor a run by hand) whose total is known
@@ -112,10 +128,108 @@ export type CostSummary = {
112
128
  replay: CostReplay | null;
113
129
  };
114
130
  export declare function costSummary(runs: readonly CostRun[], replay?: CostReplay | null): CostSummary;
131
+ /** The paths of the runs that do a job: the bill's run index counts these. A replay checks a revision and an audit's
132
+ * shadow audits a run: their spend is learning, booked where it falls (billOf) and carried by the runs they serve. */
133
+ export declare const JOB_PATHS: readonly RunPath[];
134
+ /** One run of the bill: `n` its place among the agent's runs that do a job (JOB_PATHS, 1 the first), `actual_usd` the
135
+ * booked $ to it, every run before it included, and `agent_usd` the agent for all of it: the baseline times the runs
136
+ * the bill shows to it (null with no baseline). */
137
+ export type BillPoint = {
138
+ n: number;
139
+ run_id: string;
140
+ actual_usd: number;
141
+ agent_usd: number | null;
142
+ };
143
+ /** A revision's learning spend and what its served runs carry of it (amortize): `compile_usd` the compile turns that
144
+ * drafted it (a recompile's turn is its recompiled revision's), `check_usd` its check's replay runs, `spend_usd` the
145
+ * two (each null when a part's cost is unknown), `k` the served runs it is spread over (its check's compile_after),
146
+ * `served` every run it served (its first `k` carry its spend), `attributed_usd` what they carry, `unattributed_usd` the rest, and `state`:
147
+ * `checking` (its check is at work), `serving` (it is the champion) or `stopped` (held or replaced: nothing more will
148
+ * carry it). */
149
+ export type RevisionSpend = {
150
+ workflow: string;
151
+ digest: string;
152
+ compile_usd: number | null;
153
+ check_usd: number | null;
154
+ spend_usd: number | null;
155
+ k: number;
156
+ state: "checking" | "serving" | "stopped";
157
+ };
158
+ export type RevisionShare = RevisionSpend & {
159
+ served: number;
160
+ attributed_usd: number | null;
161
+ unattributed_usd: number | null;
162
+ };
163
+ /** A hold of a learned workflow, placed on the series by the server (agentCostOverStore): `n` the first run that does a
164
+ * job from it on (its number among them, as the points have it; null when none has run since), `by` what held it (its
165
+ * audits, a check of a new revision, or this release's workflow DSL refusing the serving revision: a hold with neither
166
+ * `audit` nor `check`, runner/promotion.ts holdRefused), and its record's `reason`. */
167
+ export type BillHold = {
168
+ n: number | null;
169
+ at: string;
170
+ workflow: string;
171
+ by: "audit" | "check" | "dsl";
172
+ reason: string;
173
+ };
174
+ /** The owner's bill over the series (GET /agents/{id}/learning/cost `bill`, billOf), booked $ only: `points`, one a run
175
+ * that does a job; `baseline` the mean own $ (learning left out) of the `agent` runs before the first served run (every
176
+ * one with none yet) known exactly; `promoted` the first run a learned workflow served (or an audit audited); `break_even`
177
+ * the first run from `promoted` on after which `actual_usd` never again exceeds `agent_usd`, the booked total past the
178
+ * last point included (null: not yet, or nothing served), and `gap_usd` the booked total less the line at the last point
179
+ * while there is none; `withheld` the runs (a replay among them) whose cost is unknown or only a bound, on neither line;
180
+ * `booked_usd` every priced run's $, spend booked after the last point included, and `learning_usd` what of it learning
181
+ * booked: every priced job's learning (its compile turn and its audit's shadow run) and every priced replay. With the
182
+ * v2 split (amortize): `revisions`, `unattributed_usd` (the booked total less every run's all-in: what no run carries)
183
+ * and `holds`. */
184
+ export type Bill = {
185
+ baseline: {
186
+ runs: number;
187
+ mean_usd: number;
188
+ } | null;
189
+ promoted: {
190
+ n: number;
191
+ run_id: string;
192
+ at: string;
193
+ } | null;
194
+ points: BillPoint[];
195
+ break_even: {
196
+ n: number;
197
+ run_id: string;
198
+ } | null;
199
+ gap_usd: number | null;
200
+ withheld: number;
201
+ booked_usd: number;
202
+ learning_usd: number;
203
+ revisions: RevisionShare[];
204
+ unattributed_usd: number | null;
205
+ holds: BillHold[];
206
+ };
207
+ /** The bill over `runs` (the cost series, oldest first, replays among them): each run that does a job, all in (its own
208
+ * $, its compile turn and its audit shadow), with each replay's $ joining the run that does a job it followed (the first
209
+ * one when none did), cumulative; against the baseline times the runs shown so far. Spend booked with no point to join
210
+ * (a replay after a job of unknown cost) is carried to the next point, and with none after it is still booked: in the
211
+ * gap, and no break-even while it puts the bill over the line. Every sum and difference has its float noise rounded off
212
+ * (noiseOff), so a tie reads as a tie and a decision's sub-micro-dollar cost is booked as the usage row books it. */
213
+ export declare function billOf(runs: readonly CostRun[]): Omit<Bill, "revisions" | "unattributed_usd" | "holds">;
214
+ /** Each revision's spend split over its first `k` served runs (`served`: the runs a learned workflow served, oldest
215
+ * first, with the workflow and digest that served each: a revision is both, as two workflows of the same text share a
216
+ * digest), in whole micro-dollars: each carries spend/k, the first ones a micro-dollar more, and the k-th the
217
+ * sub-micro-dollar rest too, so the split is exact to what was booked; a run past the k-th carries none of it.
218
+ * `shares`: what each served run carries, by run id (null when its revision's spend is unknown); what a revision's
219
+ * runs have not carried is its `unattributed_usd`, and `unattributed_usd` their sum (null once one is unknown). */
220
+ export declare function amortize(revisions: readonly RevisionSpend[], served: readonly {
221
+ run_id: string;
222
+ workflow: string;
223
+ digest: string;
224
+ }[]): {
225
+ shares: Map<string, number | null>;
226
+ revisions: RevisionShare[];
227
+ unattributed_usd: number | null;
228
+ };
115
229
  /** The series as CSV (RFC 4180, a header line, `\r\n` line ends): one row a run, an unknown figure empty. A text cell that
116
230
  * a spreadsheet would read as a formula (one starting with =, +, -, @, a tab or a carriage return) starts with a `'`, so
117
231
  * it opens as the text it is. */
118
- export declare const COST_CSV_COLUMNS: readonly ["n", "run_id", "started_at", "path", "workflow", "learned_revision", "cost_usd", "cost_upper_bound", "learning_usd", "total_usd"];
232
+ export declare const COST_CSV_COLUMNS: readonly ["n", "run_id", "started_at", "path", "workflow", "digest", "audited", "cost_usd", "cost_upper_bound", "learning_usd", "total_usd"];
119
233
  export declare function costCsv(runs: readonly CostRun[]): string;
120
234
  /** GET /agents/{id}/learning/cost's query: `limit`, an integer from 1 to COST_RUNS_MAX (COST_RUNS_DEFAULT when absent),
121
235
  * and `format`, `json` (the default) or `csv`. Anything else is 400. */
@@ -15,25 +15,29 @@ import { ApiError } from "./errors.js";
15
15
  * learning). One classifier (store-reads/learning.ts runPathOverStore) writes it on the run's
16
16
  * record at its end (metrics.path). */
17
17
  export const RUN_PATHS = ["agent", "handed_back", "workflow", "replay", "audit"];
18
+ /** A mean, rounded to the millionth: a quotient, never a booked figure. */
18
19
  const usd = (value) => Math.round(value * 1e6) / 1e6;
20
+ /** A booked figure with its float noise rounded off, nothing more (runner/metrics.ts floatNoiseOff, the precision a run's
21
+ * own cost is kept at): a decision costs millionths of a dollar, so a figure rounded to the millionth would lose it, and
22
+ * the run's cost here would read other than its usage row (A8). `+ 0` makes a noise-sized negative a plain 0, never -0. */
23
+ export const noiseOff = (value) => Math.round(value * 1e12) / 1e12 + 0;
19
24
  /** A run's own cost and what learning added for it, from its row's cost (`rowUsd`: the job, its look-back and any turn a
20
- * person asked of it after), what it kept as it answered (`looked`, none for a run that did not look back) and what its
21
- * grade spent (`gradeUsd`: its learning run's cost once that run ended, stopped or failed, 0 while it runs or when there
22
- * is none, null when unknown):
25
+ * person asked of it after) and what it kept as it answered (`looked`, none for a run that did not look back):
23
26
  * - `cost_usd`: the job's own $: its row's cost less its look-back, never under what it had cost when it answered (a
24
27
  * later turn's spend is the job's too); its row's cost for a run that did not look back;
25
- * - `learning_usd`: what learning cost for it: its look-back and its grade, 0 for a run that does not learn; null when
26
- * either is unknown (a grade that failed or was stopped mid-ask spent what no one knows);
28
+ * - `learning_usd`: what learning cost for it: its look-back (its compile turn) and what was spent beside it
29
+ * (`besideUsd`: its audit's shadow run), 0 for a run that does not learn; null when unknown (a turn stopped mid-ask
30
+ * spent what no one knows, or a shadow still running);
27
31
  * - `total_usd`: their sum, the run's all-in $; null when either is. */
28
- export function runCostOf(rowUsd, looked, gradeUsd = 0) {
32
+ export function runCostOf(rowUsd, looked, besideUsd = 0) {
29
33
  const answered = looked?.answered_at !== undefined;
30
34
  const asAnswered = answered ? (looked.job_cost_usd ?? null) : rowUsd;
31
35
  const back = answered ? (looked.look_back_usd ?? null) : 0;
32
36
  // What the row holds past the answer and its look-back is a later turn's spend, unknown once the row's cost is; with the
33
37
  // look-back's cost unknown the two can't be told apart, and the job is what it had cost when it answered.
34
38
  const job = !answered || asAnswered === null || back === null ? asAnswered : rowUsd === null ? null : Math.max(asAnswered, rowUsd - back);
35
- const spent = back === null || gradeUsd === null ? null : back + gradeUsd;
36
- return { cost_usd: job === null ? null : usd(job), learning_usd: spent === null ? null : usd(spent), total_usd: job === null || spent === null ? null : usd(job + spent) };
39
+ const spent = back === null || besideUsd === null ? null : back + besideUsd;
40
+ return { cost_usd: job === null ? null : noiseOff(job), learning_usd: spent === null ? null : noiseOff(spent), total_usd: job === null || spent === null ? null : noiseOff(job + spent) };
37
41
  }
38
42
  /** The fewest runs after the takeover a "times cheaper" figure needs: fewer say too little. */
39
43
  export const CHEAPER_AFTER_RUNS = 20;
@@ -46,11 +50,25 @@ export const COST_RUNS_MAX = 10_000;
46
50
  /** The paths a learned workflow served a run on: it ran first and answered alone (`workflow`) or handed back
47
51
  * (`handed_back`). A `replay` checks a new revision on a graded input and serves no one; an `agent` run is by hand. */
48
52
  export const SERVED_PATHS = ["workflow", "handed_back"];
53
+ /** Each run of the series (every run, oldest first, before a read's limit) with `took_over`: true on the first run a
54
+ * revision (`workflow` at `digest`) served (SERVED_PATHS), never on a replay or a run no revision ran. A promotion is
55
+ * written as a run is claimed, after the runs queued before it started, so where a revision took over is read from the
56
+ * runs it served, never from start times. */
57
+ export function tookOverOf(runs) {
58
+ const seen = new Set();
59
+ return runs.map((run) => {
60
+ const revision = run.digest !== null && SERVED_PATHS.includes(run.path) ? JSON.stringify([run.workflow, run.digest]) : null;
61
+ const first = revision !== null && !seen.has(revision);
62
+ if (revision !== null)
63
+ seen.add(revision);
64
+ return { ...run, took_over: first };
65
+ });
66
+ }
49
67
  /** Why the after side has a figure or not: `priced`, it has; `no_served_runs`, no learned workflow has taken over (a
50
68
  * replay is no served run); `cost_unknown`, runs were served but none's all-in cost is known exactly. */
51
69
  export const AFTER_STATES = ["priced", "no_served_runs", "cost_unknown"];
52
70
  export function costSummary(runs, replay = null) {
53
- const first = runs.findIndex((run) => run.path === "workflow");
71
+ const first = runs.findIndex((run) => SERVED_PATHS.includes(run.path));
54
72
  const beforeSide = (first < 0 ? runs : runs.slice(0, first)).filter((run) => run.path === "agent").map((run) => ({ value: run.cost_usd, bound: run.cost_upper_bound }));
55
73
  // Served runs alone: a replay (a check of a new revision, at workflow cost) and a run by hand are never the after side.
56
74
  const served = first < 0 ? [] : runs.slice(first).filter((run) => SERVED_PATHS.includes(run.path));
@@ -71,10 +89,124 @@ export function costSummary(runs, replay = null) {
71
89
  const afterState = after ? "priced" : first < 0 ? "no_served_runs" : "cost_unknown";
72
90
  return { takeover: first < 0 ? null : { n: runs[first].n, run_id: runs[first].run_id }, before, after, after_state: afterState, times_cheaper: times, hand_backs: handBacks, withheld, replay };
73
91
  }
92
+ /** The paths of the runs that do a job: the bill's run index counts these. A replay checks a revision and an audit's
93
+ * shadow audits a run: their spend is learning, booked where it falls (billOf) and carried by the runs they serve. */
94
+ export const JOB_PATHS = ["agent", "workflow", "handed_back"];
95
+ /** The bill over `runs` (the cost series, oldest first, replays among them): each run that does a job, all in (its own
96
+ * $, its compile turn and its audit shadow), with each replay's $ joining the run that does a job it followed (the first
97
+ * one when none did), cumulative; against the baseline times the runs shown so far. Spend booked with no point to join
98
+ * (a replay after a job of unknown cost) is carried to the next point, and with none after it is still booked: in the
99
+ * gap, and no break-even while it puts the bill over the line. Every sum and difference has its float noise rounded off
100
+ * (noiseOff), so a tie reads as a tie and a decision's sub-micro-dollar cost is booked as the usage row books it. */
101
+ export function billOf(runs) {
102
+ const jobs = runs.filter((run) => JOB_PATHS.includes(run.path));
103
+ const priced = (run) => run.total_usd !== null && !run.cost_upper_bound;
104
+ // The first run a learned workflow served (the summary's takeover): by its place in the series, never by a start time,
105
+ // which two runs queued together can share.
106
+ const first = jobs.findIndex((run) => SERVED_PATHS.includes(run.path));
107
+ const before = (first < 0 ? jobs : jobs.slice(0, first)).filter((run) => run.path === "agent" && run.cost_usd !== null && !run.cost_upper_bound);
108
+ const baseline = before.length ? { runs: before.length, mean_usd: usd(before.reduce((sum, run) => sum + run.cost_usd, 0) / before.length) } : null;
109
+ // Each replay's $ joins the job it followed; one before every job joins the first.
110
+ const extra = new Map();
111
+ let withheld = 0;
112
+ let last;
113
+ const early = [];
114
+ for (const run of runs) {
115
+ if (JOB_PATHS.includes(run.path)) {
116
+ last = run.run_id;
117
+ if (early.length)
118
+ extra.set(last, noiseOff((extra.get(last) ?? 0) + early.splice(0).reduce((a, b) => noiseOff(a + b), 0)));
119
+ continue;
120
+ }
121
+ if (run.path !== "replay")
122
+ continue;
123
+ if (!priced(run)) {
124
+ withheld += 1;
125
+ continue;
126
+ }
127
+ if (last === undefined)
128
+ early.push(run.total_usd);
129
+ else
130
+ extra.set(last, noiseOff((extra.get(last) ?? 0) + run.total_usd));
131
+ }
132
+ const points = [];
133
+ let actual = 0;
134
+ let shown = 0;
135
+ const line = baseline ? baseline.mean_usd : null;
136
+ const diffs = [];
137
+ jobs.forEach((run, at) => {
138
+ if (!priced(run)) {
139
+ withheld += 1;
140
+ actual = noiseOff(actual + (extra.get(run.run_id) ?? 0));
141
+ return;
142
+ }
143
+ shown += 1;
144
+ actual = noiseOff(actual + run.total_usd + (extra.get(run.run_id) ?? 0));
145
+ const agent = line === null ? null : noiseOff(line * shown);
146
+ points.push({ n: at + 1, run_id: run.run_id, actual_usd: actual, agent_usd: agent });
147
+ diffs.push(agent === null ? Number.NaN : noiseOff(actual - agent));
148
+ });
149
+ // Replays with no job at all to join (every job run archived, say) are booked all the same.
150
+ actual = noiseOff(early.reduce((sum, one) => sum + one, actual));
151
+ // Of it, learning's: the booked total less the priced jobs' own $ (each priced job's total is its own $ and its learning).
152
+ const learning = noiseOff(jobs.reduce((rest, run) => (priced(run) ? noiseOff(rest - run.cost_usd) : rest), actual));
153
+ const promoted = first < 0 ? null : { n: first + 1, run_id: jobs[first].run_id, at: jobs[first].started_at };
154
+ // The booked total against the line at the last point: spend after it (carried to no point) is in.
155
+ const tail = line === null ? Number.NaN : noiseOff(actual - noiseOff(line * shown));
156
+ let breakEven = null;
157
+ if (promoted && line !== null && !(tail > 0)) {
158
+ const from = points.findIndex((point) => point.n >= promoted.n);
159
+ if (from >= 0) {
160
+ const over = diffs.findLastIndex((diff, at) => at >= from && diff > 0);
161
+ const at = over < 0 ? from : over + 1;
162
+ if (at < points.length)
163
+ breakEven = { n: points[at].n, run_id: points[at].run_id };
164
+ }
165
+ }
166
+ const gap = !breakEven && line !== null && points.length ? tail : null;
167
+ return { baseline, promoted, points, break_even: breakEven, gap_usd: gap, withheld, booked_usd: actual, learning_usd: learning };
168
+ }
169
+ /** Each revision's spend split over its first `k` served runs (`served`: the runs a learned workflow served, oldest
170
+ * first, with the workflow and digest that served each: a revision is both, as two workflows of the same text share a
171
+ * digest), in whole micro-dollars: each carries spend/k, the first ones a micro-dollar more, and the k-th the
172
+ * sub-micro-dollar rest too, so the split is exact to what was booked; a run past the k-th carries none of it.
173
+ * `shares`: what each served run carries, by run id (null when its revision's spend is unknown); what a revision's
174
+ * runs have not carried is its `unattributed_usd`, and `unattributed_usd` their sum (null once one is unknown). */
175
+ export function amortize(revisions, served) {
176
+ const shares = new Map();
177
+ let rest = 0;
178
+ const out = revisions.map((revision) => {
179
+ const all = served.filter((one) => one.workflow === revision.workflow && one.digest === revision.digest);
180
+ const runs = all.slice(0, revision.k);
181
+ if (revision.spend_usd === null) {
182
+ for (const run of runs)
183
+ shares.set(run.run_id, null);
184
+ rest = null;
185
+ return { ...revision, served: all.length, attributed_usd: null, unattributed_usd: null };
186
+ }
187
+ // Its whole micro-dollars (read off its pico-dollars, so float noise never drops one), split in integers; the rest
188
+ // under a micro-dollar is the k-th run's.
189
+ const total = Math.floor(Math.round(revision.spend_usd * 1e12) / 1e6);
190
+ const fraction = noiseOff(revision.spend_usd - total / 1e6);
191
+ const each = Math.floor(total / revision.k);
192
+ const over = total - each * revision.k;
193
+ let carried = 0;
194
+ runs.forEach((run, at) => {
195
+ const share = noiseOff((each + (at < over ? 1 : 0)) / 1e6 + (at === revision.k - 1 ? fraction : 0));
196
+ carried = noiseOff(carried + share);
197
+ shares.set(run.run_id, share);
198
+ });
199
+ const left = noiseOff(revision.spend_usd - carried);
200
+ if (rest !== null)
201
+ rest = noiseOff(rest + left);
202
+ return { ...revision, served: all.length, attributed_usd: carried, unattributed_usd: left };
203
+ });
204
+ return { shares, revisions: out, unattributed_usd: rest };
205
+ }
74
206
  /** The series as CSV (RFC 4180, a header line, `\r\n` line ends): one row a run, an unknown figure empty. A text cell that
75
207
  * a spreadsheet would read as a formula (one starting with =, +, -, @, a tab or a carriage return) starts with a `'`, so
76
208
  * it opens as the text it is. */
77
- export const COST_CSV_COLUMNS = ["n", "run_id", "started_at", "path", "workflow", "learned_revision", "cost_usd", "cost_upper_bound", "learning_usd", "total_usd"];
209
+ export const COST_CSV_COLUMNS = ["n", "run_id", "started_at", "path", "workflow", "digest", "audited", "cost_usd", "cost_upper_bound", "learning_usd", "total_usd"];
78
210
  export function costCsv(runs) {
79
211
  const cell = (value) => {
80
212
  const raw = value === null || value === undefined ? "" : String(value);