@desplega.ai/agent-swarm 2.8.1 → 2.8.3

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 (318) hide show
  1. package/dist/acp-adapter-xq6s2z6t.js +14 -0
  2. package/dist/{actions-pwjpn0j6.js → actions-ewz7bp4g.js} +20 -19
  3. package/dist/{amp-adapter-cq9jgj6x.js → amp-adapter-f25tqhnf.js} +3 -3
  4. package/dist/{app-cnfffgwg.js → app-mkptw0k7.js} +4 -4
  5. package/dist/{assistant-kpn317y1.js → assistant-c47rfe77.js} +23 -22
  6. package/dist/{boot-args-schema-pbbnhmc3.js → boot-args-schema-4vb0xsat.js} +1 -1
  7. package/dist/{boot-reembed-9m0ysxma.js → boot-reembed-1ghy2sab.js} +1 -1
  8. package/dist/{boot-reembed-majbn53m.js → boot-reembed-htr73hbs.js} +1 -1
  9. package/dist/{boot-scrub-sweep-app8g8ym.js → boot-scrub-sweep-yrytg42z.js} +4 -4
  10. package/dist/{boot-seeds-pbtmvcjm.js → boot-seeds-n2b3dx4e.js} +4 -4
  11. package/dist/{boxd-qbkxy6ap.js → boxd-jmfqgkqa.js} +2 -2
  12. package/dist/{claude-adapter-wn1g3qp5.js → claude-adapter-r33pqnzx.js} +8 -9
  13. package/dist/{claude-managed-adapter-jh6c523j.js → claude-managed-adapter-hddmxjyj.js} +4 -4
  14. package/dist/{claude-managed-setup-pd47nzch.js → claude-managed-setup-hfvry1bd.js} +5 -5
  15. package/dist/{claude-sdk-session-3a2a6589.js → claude-sdk-session-0hamqzc7.js} +10 -11
  16. package/dist/{cli-v1dv1r37.js → cli-0bcgyj5b.js} +74 -3
  17. package/dist/{cli-a3b239vq.js → cli-0vksaxvr.js} +2 -2
  18. package/dist/{cli-mrt48bx7.js → cli-15204js0.js} +1 -1
  19. package/dist/{cli-8aes17e4.js → cli-1m96dc36.js} +3 -3
  20. package/dist/{cli-2r0jcc3k.js → cli-2d0cghfn.js} +1 -1
  21. package/dist/{cli-wk18cm92.js → cli-4ax9mvvg.js} +4 -4
  22. package/dist/{cli-9wngrawf.js → cli-4dpkjc2e.js} +6 -6
  23. package/dist/{cli-x1wa213s.js → cli-4e1hfbpq.js} +5 -5
  24. package/dist/{cli-7349va39.js → cli-500c09j9.js} +921 -431
  25. package/dist/{cli-m4dqmnbp.js → cli-54qr8mcd.js} +2 -2
  26. package/dist/{cli-m88rpzkh.js → cli-5a7szzfh.js} +2 -2
  27. package/dist/{cli-bst1vp7e.js → cli-5y2fvw4z.js} +13 -5
  28. package/dist/{cli-vj75tyaw.js → cli-6yygkr0w.js} +7 -7
  29. package/dist/{cli-7b8sbmag.js → cli-7c5tgqhf.js} +5 -5
  30. package/dist/{cli-2gxeg5gr.js → cli-8476gtwf.js} +1 -1
  31. package/dist/{cli-yncmerje.js → cli-922z7gwm.js} +4 -4
  32. package/dist/{cli-jrzkw01m.js → cli-93t5s4zq.js} +2 -2
  33. package/dist/{cli-sn7rtnj4.js → cli-9zmb7ptc.js} +9 -9
  34. package/dist/{cli-tk7cjj8h.js → cli-am1ptset.js} +3 -3
  35. package/dist/{cli-cyhbbd1z.js → cli-awwptga4.js} +1 -1
  36. package/dist/{cli-89cqnasv.js → cli-bhsfbmk8.js} +19 -7
  37. package/dist/{cli-9vhebtjz.js → cli-bj6qhgte.js} +15 -32
  38. package/dist/{cli-g70vxtjh.js → cli-c0xjnr8z.js} +2 -2
  39. package/dist/{cli-vqptjbr7.js → cli-c3ng0mb4.js} +14 -14
  40. package/dist/{cli-re4raaj0.js → cli-c71n3bxq.js} +11 -6
  41. package/dist/{cli-7p1kvkck.js → cli-crpf163n.js} +25 -18
  42. package/dist/{cli-k0wwfqbg.js → cli-dns0y538.js} +4 -0
  43. package/dist/{cli-d3p5vwbk.js → cli-dpsfwh7n.js} +13 -21
  44. package/dist/{cli-xzm9vcfg.js → cli-dtdht77h.js} +2 -2
  45. package/dist/{cli-bb431xey.js → cli-e2w34h6z.js} +231 -68
  46. package/dist/{cli-eddnyg3t.js → cli-emekyyhr.js} +1 -1
  47. package/dist/{cli-d84byzn8.js → cli-f1kwb5fc.js} +109 -33
  48. package/dist/{cli-0jzra9ad.js → cli-gn9y6fn7.js} +2 -2
  49. package/dist/{cli-tqegb8eq.js → cli-gpz1d0t2.js} +4 -4
  50. package/dist/{cli-7w69ne2f.js → cli-h9yy18gy.js} +239 -67
  51. package/dist/{cli-m5akxq8q.js → cli-j8cjdzan.js} +1 -1
  52. package/dist/{cli-m9s2qff2.js → cli-jg5txpm6.js} +187 -93
  53. package/dist/{cli-k26bkr9t.js → cli-k2rw11py.js} +3 -3
  54. package/dist/{cli-93k32c37.js → cli-k96cecs7.js} +377 -61
  55. package/dist/{cli-3vjhs18n.js → cli-kd56bmk4.js} +1 -1
  56. package/dist/{cli-k0615eh8.js → cli-kmemts3w.js} +13 -13
  57. package/dist/{cli-pcad4cb1.js → cli-knva3nmx.js} +2 -2
  58. package/dist/{cli-b085njxz.js → cli-kw44j53p.js} +15 -14
  59. package/dist/{cli-bsywb58a.js → cli-mdpbjnzq.js} +10 -10
  60. package/dist/{cli-syybb0p1.js → cli-me0r8gzr.js} +35 -76
  61. package/dist/{cli-4r05hnqa.js → cli-mwwmka77.js} +15 -15
  62. package/dist/{cli-1xh9a7hn.js → cli-n5t077me.js} +2 -2
  63. package/dist/{cli-rvhdmjvt.js → cli-nx2n3z6p.js} +1 -1
  64. package/dist/{cli-28fq781p.js → cli-pe4d96rb.js} +1 -1
  65. package/dist/{cli-3rkdne1h.js → cli-pxwtcgcp.js} +2 -2
  66. package/dist/{cli-zcsw143p.js → cli-qmqt520k.js} +2 -2
  67. package/dist/{cli-2zp0et3v.js → cli-qps8je6j.js} +70 -19
  68. package/dist/{cli-xae4g51r.js → cli-rh15cakt.js} +1 -1
  69. package/dist/{cli-k93bq1wq.js → cli-rjvsf9qa.js} +5 -5
  70. package/dist/{cli-6mma83xa.js → cli-rqfvkx5t.js} +7 -7
  71. package/dist/{cli-dy32zecz.js → cli-s7zz734a.js} +1 -1
  72. package/dist/{cli-yn2bbre9.js → cli-s8p8hhty.js} +2 -2
  73. package/dist/{cli-vbh10fqa.js → cli-sdvcrq5x.js} +5 -5
  74. package/dist/{cli-b5rs97a6.js → cli-sg27rk2t.js} +5 -5
  75. package/dist/{cli-d6521kb5.js → cli-skmhsnqh.js} +1 -1
  76. package/dist/{cli-5mrq3ake.js → cli-t4dv07kr.js} +1 -1
  77. package/dist/{cli-hxpyc9sn.js → cli-t5ys4ebm.js} +3 -3
  78. package/dist/{cli-qmqk2yc2.js → cli-tas6ckky.js} +2 -2
  79. package/dist/{cli-p1w18yhx.js → cli-tf79ryyy.js} +1 -1
  80. package/dist/{cli-kc7v1nfv.js → cli-tnzebztn.js} +1 -1
  81. package/dist/{cli-gax7fk3t.js → cli-vx421edw.js} +3 -3
  82. package/dist/{cli-kchjb7qy.js → cli-x3ex5bqf.js} +1 -1
  83. package/dist/cli-yss07azk.js +142 -0
  84. package/dist/{cli-0ntbxyaa.js → cli-z8cvws4c.js} +363 -47
  85. package/dist/{cli-tacge1w0.js → cli-zdaypkq9.js} +146 -52
  86. package/dist/{cli-b8e9nb04.js → cli-zeaehgyd.js} +2 -2
  87. package/dist/{cli-jcpr5smb.js → cli-ztq5z4e1.js} +4 -4
  88. package/dist/cli.js +21 -21
  89. package/dist/{codex-adapter-nha71q3n.js → codex-adapter-2brxx5ta.js} +7 -8
  90. package/dist/{codex-hook-h3vk6xzr.js → codex-hook-c930c3ea.js} +2 -2
  91. package/dist/{codex-login-bcda78ge.js → codex-login-cbrs1h5w.js} +1 -1
  92. package/dist/{codex-session-runner-ak9x1k1h.js → codex-session-runner-y9anfj0r.js} +8 -9
  93. package/dist/{commands-f587qpq8.js → commands-wn9q06qr.js} +3 -3
  94. package/dist/{controller-hxx8wwy1.js → controller-fw608czd.js} +4 -4
  95. package/dist/{controllers-connect-k5sg9m9h.js → controllers-connect-63j5rdcm.js} +20 -20
  96. package/dist/{cursor-adapter-n5nc3mfe.js → cursor-adapter-6mb9jme8.js} +2 -2
  97. package/dist/{db-nzf8zb5z.js → db-0m6v5tq4.js} +3 -3
  98. package/dist/{db-amm8w15a.js → db-y4wfrzbm.js} +4 -2
  99. package/dist/{devin-adapter-xedz98zv.js → devin-adapter-r0yx3gv8.js} +1 -1
  100. package/dist/{dispatcher-89zrts8f.js → dispatcher-9tq2zzdj.js} +17 -17
  101. package/dist/{docker-866qpzt9.js → docker-j75atvpt.js} +2 -2
  102. package/dist/{dsh-adapter-nrhbrjz8.js → dsh-adapter-t09sv4tq.js} +2 -2
  103. package/dist/{e2b-s16txnpx.js → e2b-7s9dscap.js} +3 -3
  104. package/dist/{e2b-jkwfhbcv.js → e2b-a7vzs2vj.js} +3 -3
  105. package/dist/{easter-egg-3d-533knw22.js → easter-egg-3d-fbwvmwhe.js} +1 -1
  106. package/dist/{embedding-space-ezwenjmj.js → embedding-space-wh8de1t9.js} +6 -6
  107. package/dist/{embeddings-np74ht6g.js → embeddings-3hpbdtjr.js} +1 -1
  108. package/dist/{event-bus-e66gh7cc.js → event-bus-tr6h3gv8.js} +1 -1
  109. package/dist/{execute-b3mrb1mv.js → execute-4bdf8sdy.js} +1 -1
  110. package/dist/{external-channel-z46yx9d7.js → external-channel-ts2fwqh0.js} +2 -2
  111. package/dist/fx-adapter-1rnxnf56.js +17 -0
  112. package/dist/grok-adapter-efhmf1r8.js +17 -0
  113. package/dist/handlers-anrr3py9.js +32 -0
  114. package/dist/{heartbeat-dt9qw9c8.js → heartbeat-2ze5fzpc.js} +20 -20
  115. package/dist/{hook-8k6fbz9b.js → hook-ca54yd4s.js} +5 -5
  116. package/dist/{hook-jgcgygex.js → hook-gaehe2w5.js} +8 -8
  117. package/dist/{http-haw4r8ne.js → http-jfg8t2yk.js} +3839 -122
  118. package/dist/{index-tvfafeyy.js → index-3gv6sc4z.js} +1 -1
  119. package/dist/{index-tez78qj3.js → index-3yvzbhxd.js} +100 -23
  120. package/dist/index-8w2aznds.js +33 -0
  121. package/dist/{index-5gk12my8.js → index-csgrxedz.js} +1 -1
  122. package/dist/index-qnjvr362.js +37 -0
  123. package/dist/index-yzxd3sn9.js +32 -0
  124. package/dist/{keepalive-xn6s7rmg.js → keepalive-k2a3qe73.js} +4 -4
  125. package/dist/lead-zhxqbfdv.js +69 -0
  126. package/dist/{maintenance-vdts800b.js → maintenance-gb5sd5nc.js} +1 -1
  127. package/dist/{node-4c5jqxr3.js → node-7qsx5kvm.js} +1 -1
  128. package/dist/{oauth-refresh-sweep-094c8wfk.js → oauth-refresh-sweep-y7rm6bkb.js} +3 -3
  129. package/dist/{onboard-v99eh56m.js → onboard-jmc5qerv.js} +4 -4
  130. package/dist/{opencode-adapter-dsagkhtw.js → opencode-adapter-7eade9ev.js} +6 -7
  131. package/dist/{otel-impl-q7yr9yp0.js → otel-impl-8j1xhxqd.js} +2 -2
  132. package/dist/{pi-mono-adapter-546cd1cc.js → pi-mono-adapter-yqmaf32x.js} +14 -13
  133. package/dist/{pricing-refresh-7dh2yase.js → pricing-refresh-1nj722h6.js} +2 -2
  134. package/dist/{rbac-roles-mdmc96p4.js → rbac-roles-b1mtr6dr.js} +3 -3
  135. package/dist/{rbac-roles-npbmdh77.js → rbac-roles-n1f3frk1.js} +2 -2
  136. package/dist/{render-v2-twhhwmms.js → render-v2-1gfpd8rj.js} +4 -4
  137. package/dist/{resume-5gsrjd5e.js → resume-w1s1eh5v.js} +7 -7
  138. package/dist/{runtime-1hv06jsn.js → runtime-84g5egsc.js} +1 -1
  139. package/dist/{session-access-m191x30x.js → session-access-y6z1txk4.js} +4 -4
  140. package/dist/{session-source-backfill-nt923ask.js → session-source-backfill-gxxwfh12.js} +3 -3
  141. package/dist/{setup-wv467afy.js → setup-r91gstyk.js} +1 -1
  142. package/dist/{virtual-modules-gazyapte.js → virtual-modules-cgnj6kme.js} +3 -3
  143. package/dist/{work-objects-8zkrpy13.js → work-objects-qcxzg61c.js} +20 -20
  144. package/dist/worker-7j2907g6.js +69 -0
  145. package/dist/{worker-follow-up-cecqszem.js → worker-follow-up-y2d2h7km.js} +19 -17
  146. package/dist/{x-2fyrt5pp.js → x-qhxq2ama.js} +2 -2
  147. package/openapi.json +2908 -351
  148. package/package.json +4 -4
  149. package/src/apps/catalog.generated.json +120 -1
  150. package/src/apps/definition.ts +38 -11
  151. package/src/apps/page-validator.ts +88 -0
  152. package/src/be/db/model-profiles.ts +110 -11
  153. package/src/be/db/tasks/read.ts +53 -0
  154. package/src/be/db/workflow-run-definitions.ts +19 -8
  155. package/src/be/db-client.ts +43 -5
  156. package/src/be/db.ts +40 -9
  157. package/src/be/migrations/284_model_profile_fallbacks.sql +9 -0
  158. package/src/be/migrations/289_automation_savings_daily.sql +47 -0
  159. package/src/be/migrations/291_task_slack_participants.sql +4 -0
  160. package/src/be/migrations/293_savings_component_links.sql +58 -0
  161. package/src/be/migrations/296_automation_savings_daily_read_index.sql +6 -0
  162. package/src/be/model-profile-check.ts +53 -20
  163. package/src/be/runtime-pools/routing.ts +1 -0
  164. package/src/be/savings/calibrate.ts +468 -0
  165. package/src/be/savings/compute.ts +677 -0
  166. package/src/be/savings/model.ts +674 -0
  167. package/src/be/savings/read.ts +1500 -0
  168. package/src/be/savings/rollup.ts +571 -0
  169. package/src/be/scripts/run-credential.ts +15 -3
  170. package/src/be/scripts/typecheck.ts +91 -4
  171. package/src/be/seed/agent-fs-provision.ts +48 -11
  172. package/src/be/seed/workflows-seeder.ts +67 -23
  173. package/src/be/seed-scripts/catalog/dreaming-learnings.ts +10 -2
  174. package/src/be/seed-scripts/catalog/dreaming-memtax-create.ts +70 -4
  175. package/src/be/skill-parser.ts +19 -7
  176. package/src/be/swarm-config-guard.ts +8 -0
  177. package/src/be/users.ts +29 -2
  178. package/src/commands/onboard/agent-fs-version.ts +1 -1
  179. package/src/commands/provider-error-class.ts +97 -0
  180. package/src/commands/runner.ts +258 -58
  181. package/src/extensions/contract-types.generated.ts +1 -1
  182. package/src/http/all-routes.ts +2 -0
  183. package/src/http/fs.ts +8 -2
  184. package/src/http/index.ts +11 -0
  185. package/src/http/model-profiles.ts +43 -10
  186. package/src/http/poll.ts +15 -1
  187. package/src/http/requested-by.ts +97 -1
  188. package/src/http/savings-calibration.ts +277 -0
  189. package/src/http/savings.ts +849 -0
  190. package/src/http/scripts.ts +26 -15
  191. package/src/http/tasks.ts +89 -9
  192. package/src/http/trackers/authorize-gate.ts +41 -0
  193. package/src/http/trackers/jira.ts +26 -4
  194. package/src/http/trackers/linear.ts +26 -4
  195. package/src/integrations/chat-sdk/limits.ts +6 -1
  196. package/src/jira/oauth.ts +7 -1
  197. package/src/linear/README.md +2 -1
  198. package/src/linear/oauth.ts +7 -1
  199. package/src/prompts/session-templates.ts +80 -3
  200. package/src/providers/acp-targets.ts +1 -1
  201. package/src/providers/amp-adapter.ts +1 -1
  202. package/src/providers/claude-adapter.ts +0 -20
  203. package/src/providers/claude-flag-settings.ts +12 -10
  204. package/src/providers/codex-adapter.ts +12 -34
  205. package/src/providers/opencode-adapter.ts +34 -12
  206. package/src/providers/pi-mono-adapter.ts +6 -5
  207. package/src/providers/types.ts +7 -0
  208. package/src/scheduler/scheduler.ts +20 -14
  209. package/src/scripts-runtime/ctx.ts +1 -1
  210. package/src/scripts-runtime/executors/quickjs-bundle.ts +1 -0
  211. package/src/scripts-runtime/executors/quickjs-prelude.ts +162 -2
  212. package/src/scripts-runtime/executors/quickjs-runner.ts +4 -1
  213. package/src/scripts-runtime/sdk-allowlist.ts +11 -1
  214. package/src/scripts-runtime/stdlib/fetch.ts +113 -15
  215. package/src/scripts-runtime/stdlib/index.ts +3 -1
  216. package/src/scripts-runtime/stdlib/retry.ts +174 -0
  217. package/src/scripts-runtime/swarm-sdk.ts +192 -17
  218. package/src/scripts-runtime/types/stdlib.d.ts +95 -4
  219. package/src/scripts-runtime/types/swarm-extension.d.ts +60 -0
  220. package/src/scripts-runtime/types/swarm-sdk.d.ts +69 -2
  221. package/src/slack/actions.ts +1 -0
  222. package/src/slack/assistant.ts +2 -0
  223. package/src/slack/enrich.ts +144 -50
  224. package/src/slack/handlers.ts +37 -95
  225. package/src/slack/templates.ts +9 -0
  226. package/src/slack/thread-buffer.ts +45 -71
  227. package/src/slack/thread-participants.ts +187 -0
  228. package/src/tasks/worker-follow-up.ts +142 -3
  229. package/src/tests/agent-fs-provision-seeder.test.ts +81 -0
  230. package/src/tests/apps-chart-shell.test.ts +170 -0
  231. package/src/tests/apps-icon.test.ts +156 -2
  232. package/src/tests/base-prompt.test.ts +5 -4
  233. package/src/tests/chat-sdk-relay.test.ts +12 -0
  234. package/src/tests/claude-adapter-binary.test.ts +7 -1
  235. package/src/tests/claude-adapter.test.ts +10 -64
  236. package/src/tests/claude-flag-settings.test.ts +15 -11
  237. package/src/tests/claude-hook-credentials.test.ts +0 -1
  238. package/src/tests/claude-sdk-lifecycle.test.ts +5 -1
  239. package/src/tests/codex-adapter.test.ts +28 -78
  240. package/src/tests/comb-default.test.ts +6 -0
  241. package/src/tests/core-auth.test.ts +7 -6
  242. package/src/tests/create-task-vcs-repo.test.ts +136 -0
  243. package/src/tests/db-client-busy-retry.test.ts +58 -6
  244. package/src/tests/db-task-read-contract.test.ts +2 -0
  245. package/src/tests/db-task-write-contract.test.ts +1 -0
  246. package/src/tests/deploy-guard.test.ts +154 -0
  247. package/src/tests/docs-index-footprint.test.js +3 -3
  248. package/src/tests/dreaming-learnings.test.ts +104 -0
  249. package/src/tests/extensions-pre-task.test.ts +4 -2
  250. package/src/tests/fetch-resolved-env.test.ts +47 -1
  251. package/src/tests/fixtures/app-shell-charts-definition.json.txt +180 -0
  252. package/src/tests/model-profile-fallbacks.test.ts +482 -0
  253. package/src/tests/model-profiles-api.test.ts +1 -0
  254. package/src/tests/opencode-adapter.test.ts +53 -6
  255. package/src/tests/pi-mono-session-cost-on-abort.test.ts +115 -0
  256. package/src/tests/prompt-template-session.test.ts +17 -1
  257. package/src/tests/provider-error-class.test.ts +174 -0
  258. package/src/tests/runner-provider-fallback.test.ts +197 -0
  259. package/src/tests/runner-requester-profile.test.ts +139 -1
  260. package/src/tests/savings-calibration-api.test.ts +264 -0
  261. package/src/tests/savings-calibration.test.ts +201 -0
  262. package/src/tests/savings-compound.test.ts +774 -0
  263. package/src/tests/savings-compute.test.ts +215 -0
  264. package/src/tests/savings-fleet-latency.test.ts +143 -0
  265. package/src/tests/savings-fleet-read.test.ts +195 -0
  266. package/src/tests/savings-fleet-scale.test.ts +222 -0
  267. package/src/tests/savings-model.test.ts +499 -0
  268. package/src/tests/savings-read.test.ts +367 -0
  269. package/src/tests/savings-rollup.test.ts +766 -0
  270. package/src/tests/savings-scripts.test.ts +611 -0
  271. package/src/tests/script-connections.test.ts +4 -0
  272. package/src/tests/script-executor-conformance.test.ts +444 -0
  273. package/src/tests/scripts-http.test.ts +55 -0
  274. package/src/tests/scripts-runtime-fetch.test.ts +2 -2
  275. package/src/tests/sdk-allowlist.test.ts +40 -0
  276. package/src/tests/secret-rules.test.ts +124 -1
  277. package/src/tests/secret-scrubber.test.ts +1 -1
  278. package/src/tests/seed-workflows.test.ts +167 -11
  279. package/src/tests/skill-parser.test.ts +45 -0
  280. package/src/tests/slack-dm-implicit-mention.test.ts +20 -1
  281. package/src/tests/slack-identity-resolution.test.ts +23 -0
  282. package/src/tests/slack-inbound-files.test.ts +2 -1
  283. package/src/tests/slack-thread-participants.test.ts +429 -0
  284. package/src/tests/tool-annotations.test.ts +30 -0
  285. package/src/tests/tracker-oauth-authorize-auth.test.ts +262 -0
  286. package/src/tests/workflow-definition-pinning.test.ts +128 -3
  287. package/src/tests/workflow-script-principal.test.ts +94 -4
  288. package/src/tools/app-upsert.ts +1 -1
  289. package/src/tools/model-profiles/list-model-profiles.ts +4 -2
  290. package/src/tools/update-profile.ts +1 -1
  291. package/src/types.ts +146 -33
  292. package/src/utils/constants.ts +2 -0
  293. package/src/utils/mcp-server-fetcher.test.ts +89 -0
  294. package/src/utils/mcp-server-fetcher.ts +13 -5
  295. package/src/utils/request-auth-context.ts +24 -0
  296. package/src/utils/secret-rules.generated.ts +1 -1
  297. package/src/utils/secret-scrubber.ts +154 -29
  298. package/src/workflows/executors/swarm-script.ts +23 -19
  299. package/templates/skills/apps/SKILL.md +62 -3
  300. package/templates/skills/apps/content.md +62 -3
  301. package/templates/skills/swarm-docs/content.md +2 -0
  302. package/templates/skills/swarm-scripts/SKILL.md +5 -4
  303. package/templates/skills/swarm-scripts/content.md +5 -4
  304. package/templates/skills/work-on-task/SKILL.md +1 -1
  305. package/templates/skills/work-on-task/content.md +1 -1
  306. package/templates/workflows/dreaming/content.md +1 -1
  307. package/dist/acp-adapter-pdy3qsvg.js +0 -15
  308. package/dist/cli-4bd1rck6.js +0 -4
  309. package/dist/cli-jywyhqws.js +0 -104
  310. package/dist/fx-adapter-3q12snn6.js +0 -18
  311. package/dist/grok-adapter-ahb1w033.js +0 -18
  312. package/dist/handlers-vbww69re.js +0 -32
  313. package/dist/index-4xwaf5th.js +0 -32
  314. package/dist/index-f0hcrs8e.js +0 -33
  315. package/dist/index-s4ewdma4.js +0 -37
  316. package/dist/lead-6ygbfhbh.js +0 -70
  317. package/dist/worker-mz5z7kj0.js +0 -70
  318. package/src/providers/ctx-mode-env.ts +0 -10
@@ -0,0 +1,1500 @@
1
+ /**
2
+ * Read path of the savings rollup: sums the daily buckets of a window, shares
3
+ * the schedule cap across the versions of a job, applies the enabled-only rule
4
+ * and prices at the requested tier. All of that happens at read time, so a
5
+ * tier or window change needs no recomputation.
6
+ */
7
+ import { getDbClient } from "../db";
8
+ import type { DbParam } from "../db-client";
9
+ import {
10
+ computeScriptWindow,
11
+ computeWorkflowWindow,
12
+ countScriptRuns,
13
+ type DayFacts,
14
+ LIVE_MAX_RUNS,
15
+ type SavingsSql,
16
+ type ScriptWindowFacts,
17
+ } from "./compute";
18
+ import {
19
+ allocateSchedule,
20
+ type CurveSet,
21
+ INLINE_SCRIPT_NAME,
22
+ isEnabledOnlyRouter,
23
+ MODEL_VERSION,
24
+ type PricedWorkflow,
25
+ priceScript,
26
+ priceWorkflow,
27
+ type SavingsBand,
28
+ type SavingsTier,
29
+ type ScheduleAllocation,
30
+ type ScriptFacts,
31
+ type ScriptKind,
32
+ splitByDay,
33
+ splitScriptByDay,
34
+ type WorkflowFacts,
35
+ } from "./model";
36
+ import { dayStartIso, SAVINGS_FACT_COLUMNS, savingsRollupGeneration, utcDay } from "./rollup";
37
+
38
+ const DAY_MS = 86_400_000;
39
+ const MINUTE_MS = 60_000;
40
+
41
+ export type SavingsWindow = { start: string; end: string; days: number };
42
+
43
+ export type WorkflowSavingsRow = PricedWorkflow & { workflowId: string; name: string };
44
+
45
+ export type SavingsTotals = {
46
+ runs: number;
47
+ substitutedRuns: number;
48
+ enabledOnlyRuns: number;
49
+ measuredUsd: number;
50
+ estimatedLlmUsd: number;
51
+ actualUsd: number;
52
+ agentOnlyUsd: SavingsBand;
53
+ savingUsd: SavingsBand;
54
+ actualHours: number;
55
+ hoursSaved: SavingsBand;
56
+ };
57
+
58
+ export type SavingsDailyPoint = {
59
+ day: string;
60
+ savingUsd: number;
61
+ actualUsd: number;
62
+ measuredUsd: number;
63
+ estimatedLlmUsd: number;
64
+ };
65
+
66
+ export type WorkflowSavingsRead = {
67
+ window: SavingsWindow;
68
+ /** Newest rollup time of the window's days; null before the first rollup. */
69
+ computedAt: string | null;
70
+ /** Every day of the window has been computed under the current model version. */
71
+ complete: boolean;
72
+ /**
73
+ * The runs of the window's first, partial day could not be scanned live (more
74
+ * than the live limit, or a busy database): that day's bucket was scaled to
75
+ * the part inside the window instead.
76
+ */
77
+ boundaryEstimated: boolean;
78
+ rows: WorkflowSavingsRow[];
79
+ total: SavingsTotals | null;
80
+ /** p50 per UTC day the window touches (`days + 1`), oldest first, summed over the rows; they add up to the total. */
81
+ points: SavingsDailyPoint[];
82
+ };
83
+
84
+ type BucketRow = WorkflowFacts & { day: string; jobId: string; jobKey: string; jobName: string };
85
+ type Job = {
86
+ workflowId: string;
87
+ jobKey: string;
88
+ name: string;
89
+ facts: WorkflowFacts;
90
+ days: BucketRow[];
91
+ };
92
+
93
+ const zeroFacts = (): WorkflowFacts => ({
94
+ runs: 0,
95
+ countedRuns: 0,
96
+ schedRuns: 0,
97
+ schedEffectRuns: 0,
98
+ eventRuns: 0,
99
+ effectRuns: 0,
100
+ effectOpsSum: 0,
101
+ opsSum: 0,
102
+ agentTasks: 0,
103
+ agentUsd: 0,
104
+ agentMs: 0,
105
+ followupUsd: 0,
106
+ childTaskUsd: 0,
107
+ childTaskMs: 0,
108
+ recordedLlmUsd: 0,
109
+ estimatedLlmUsd: 0,
110
+ computeSecs: 0,
111
+ });
112
+
113
+ function addFacts(into: WorkflowFacts, row: WorkflowFacts): void {
114
+ for (const c of SAVINGS_FACT_COLUMNS) into[c] += Number(row[c]) || 0;
115
+ }
116
+
117
+ /**
118
+ * The rolling window: `days` x 24 hours ending now, the same window the live
119
+ * computation uses. Its start is taken to the minute, so reads within a minute
120
+ * share one scan of the partial first day. It touches `days + 1` UTC days: the
121
+ * first is partial and is scanned live; the others come from the buckets.
122
+ */
123
+ export function savingsWindow(
124
+ days: number,
125
+ now: number,
126
+ ): SavingsWindow & { firstDay: string; lastDay: string } {
127
+ const startMs = Math.floor(now / MINUTE_MS) * MINUTE_MS - days * DAY_MS;
128
+ return {
129
+ start: new Date(startMs).toISOString(),
130
+ end: new Date(now).toISOString(),
131
+ days,
132
+ firstDay: utcDay(startMs),
133
+ lastDay: utcDay(now),
134
+ };
135
+ }
136
+
137
+ /** UTC days from `firstDay` to `lastDay`, both included. */
138
+ function daysBetween(firstDay: string, lastDay: string): string[] {
139
+ const out: string[] = [];
140
+ for (
141
+ let t = Date.parse(dayStartIso(firstDay));
142
+ t <= Date.parse(dayStartIso(lastDay));
143
+ t += DAY_MS
144
+ ) {
145
+ out.push(utcDay(t));
146
+ }
147
+ return out;
148
+ }
149
+
150
+ /** Live facts of one workflow over a first-day slice by UTC minute, or the slice's run count (null when unknown) when they could not be read. */
151
+ type SliceResult = { byMinute: DayFacts[] } | { runs: number | null };
152
+ /** Live facts by script name over a first-day slice by UTC minute, or null when they could not be read. */
153
+ type ScriptSliceResult = ScriptWindowFacts[] | null;
154
+ /** First-day scans by workflow (or script read) and slice start, kept a little past the slice start's minute or hour. */
155
+ const sliceCache = new Map<string, { at: number; ttl: number; result: Promise<unknown> }>();
156
+ const SLICE_TTL_MS = 2 * MINUTE_MS;
157
+ const HOUR_MS = 60 * MINUTE_MS;
158
+ const FLEET_SLICE_TTL_MS = HOUR_MS + 2 * MINUTE_MS;
159
+ /**
160
+ * First-day scans in flight, across every read (the workflow and script reads
161
+ * of a fleet read run together): below the default db-query concurrency cap of 3.
162
+ */
163
+ const FIRST_DAY_SCAN_CONCURRENCY = 2;
164
+ let scansInFlight = 0;
165
+ const scanWaiters: (() => void)[] = [];
166
+
167
+ async function withScanSlot<T>(scan: () => Promise<T>): Promise<T> {
168
+ if (scansInFlight < FIRST_DAY_SCAN_CONCURRENCY) scansInFlight++;
169
+ else await new Promise<void>((resolve) => scanWaiters.push(resolve));
170
+ try {
171
+ return await scan();
172
+ } finally {
173
+ // Hand the slot to the next waiter, or free it.
174
+ const next = scanWaiters.shift();
175
+ if (next) next();
176
+ else scansInFlight--;
177
+ }
178
+ }
179
+
180
+ /** One scan per key at a time, kept for `ttl`; a failed scan reads as `failed` and is not kept. */
181
+ function cachedScan<T>(
182
+ key: string,
183
+ ttl: number,
184
+ scan: () => Promise<T>,
185
+ failed: T,
186
+ label: string,
187
+ ): Promise<T> {
188
+ const hit = sliceCache.get(key);
189
+ if (hit) return hit.result as Promise<T>;
190
+ const now = Date.now();
191
+ // A slice start of an older minute (or hour) is never read again.
192
+ for (const [k, v] of sliceCache) if (now - v.at > v.ttl) sliceCache.delete(k);
193
+ const result: Promise<T> = withScanSlot(scan).catch((err): T => {
194
+ console.warn(
195
+ `[savings-read] first-day scan of ${label} failed: ${String(err instanceof Error ? err.message : err).slice(0, 200)}`,
196
+ );
197
+ // A failed scan is not kept: the next read scans again.
198
+ if (sliceCache.get(key)?.result === result) sliceCache.delete(key);
199
+ return failed;
200
+ });
201
+ sliceCache.set(key, { at: now, ttl, result });
202
+ return result;
203
+ }
204
+
205
+ function scanSlice(
206
+ workflow: { id: string; definition: unknown },
207
+ slice: { start: string; end: string },
208
+ sql: SavingsSql,
209
+ ttl: number,
210
+ ): Promise<SliceResult> {
211
+ return cachedScan<SliceResult>(
212
+ `${workflow.id}|${slice.start}`,
213
+ ttl,
214
+ async () => {
215
+ const out = await computeWorkflowWindow(workflow, slice, sql, { bucket: "minute" });
216
+ return out.status === "ok" ? { byMinute: out.byDay } : { runs: out.runs };
217
+ },
218
+ { runs: null },
219
+ workflow.id,
220
+ );
221
+ }
222
+
223
+ const COUNT_COLUMNS = new Set<keyof WorkflowFacts>([
224
+ "runs",
225
+ "countedRuns",
226
+ "schedRuns",
227
+ "schedEffectRuns",
228
+ "eventRuns",
229
+ "effectRuns",
230
+ "agentTasks",
231
+ ]);
232
+
233
+ /**
234
+ * The part of a first-day bucket inside the window when it could not be
235
+ * scanned: the bucket scaled by the share of its runs in the window (known when
236
+ * the scan refused on run count), else by the share of the day in the window.
237
+ */
238
+ function scaleBucket(bucket: WorkflowFacts, share: number): WorkflowFacts {
239
+ const out = zeroFacts();
240
+ for (const c of SAVINGS_FACT_COLUMNS) {
241
+ const v = (Number(bucket[c]) || 0) * share;
242
+ out[c] = COUNT_COLUMNS.has(c) ? Math.round(v) : v;
243
+ }
244
+ return out;
245
+ }
246
+
247
+ function parseDefinition(definition: unknown): unknown {
248
+ if (typeof definition !== "string") return definition ?? {};
249
+ try {
250
+ return JSON.parse(definition);
251
+ } catch {
252
+ return {};
253
+ }
254
+ }
255
+
256
+ /** Binary order, as SQLite compares text. */
257
+ const byId = (a: string, b: string): number => (a < b ? -1 : a > b ? 1 : 0);
258
+
259
+ const isRetired = (name: string) => /-retired-\d{4}-\d{2}-\d{2}$/.test(name);
260
+
261
+ /** At most about this many bucket rows per synchronous query (a single larger day is read whole). */
262
+ const CHUNK_ROWS = 5_000;
263
+ /** Jobs priced between two event-loop yields. */
264
+ const PRICE_BATCH = 100;
265
+ /** A cached fleet read is reused at most this long, even when nothing bumped. */
266
+ const FLEET_CACHE_TTL_MS = 60_000;
267
+
268
+ /**
269
+ * Yield to the event loop. Turns chain across every read in flight: the
270
+ * workflow and script reads of a fleet read run together, and two of their
271
+ * slices in one loop turn would stall it for both.
272
+ */
273
+ let lastTurn: Promise<void> = Promise.resolve();
274
+ const yieldLoop = (): Promise<void> => {
275
+ lastTurn = lastTurn.then(() => new Promise<void>((resolve) => setImmediate(resolve)));
276
+ return lastTurn;
277
+ };
278
+
279
+ /**
280
+ * Read the buckets matching `where` over the window and fold them with
281
+ * `onRows`, in slices of whole days of at most about CHUNK_ROWS rows, oldest
282
+ * first, yielding to the event loop between them: a fleet window is ~100k
283
+ * rows, and one synchronous read and fold of them stalls every other request.
284
+ *
285
+ * Slices follow the row counts, not the calendar. Every query waits its turn
286
+ * on the shared connection, which takes up to a second on a busy API, so a
287
+ * read pays one wait per slice: fixed 3-day slices made a 90-day read 31
288
+ * waits even when most of its days are empty. The slices cover the window
289
+ * end to end, so a day that gains rows after the count is still read.
290
+ *
291
+ * `oneJob` names the index of a one-job filter (`jobKey` or `jobId`): its few
292
+ * rows are read in one query, through that index. Without ANALYZE statistics
293
+ * (production has none) the planner would pick the kind-and-day index and
294
+ * read every job's buckets of the window.
295
+ */
296
+ async function forEachBucketChunk<T>(
297
+ columns: string,
298
+ where: string,
299
+ params: DbParam[],
300
+ firstDay: string,
301
+ lastDay: string,
302
+ onRows: (rows: T[]) => void | Promise<void>,
303
+ oneJob?: "idx_automation_savings_daily_key" | "idx_automation_savings_daily_job",
304
+ ): Promise<void> {
305
+ const client = getDbClient();
306
+ if (oneJob) {
307
+ await onRows(
308
+ await client.query<T>(
309
+ `SELECT ${columns} FROM automation_savings_daily INDEXED BY ${oneJob} WHERE ${where} AND day>=? AND day<=? ORDER BY day`,
310
+ [...params, firstDay, lastDay],
311
+ ),
312
+ );
313
+ return;
314
+ }
315
+ const counts = await client.query<{ day: string; n: number }>(
316
+ `SELECT day, count(*) n FROM automation_savings_daily WHERE ${where} AND day>=? AND day<=? GROUP BY day ORDER BY day`,
317
+ [...params, firstDay, lastDay],
318
+ );
319
+ // Each slice ends on its last counted day; the next starts the day after.
320
+ const ends: string[] = [];
321
+ let rows = 0;
322
+ for (const c of counts) {
323
+ const n = Number(c.n) || 0;
324
+ if (ends.length > 0 && rows + n <= CHUNK_ROWS) {
325
+ ends[ends.length - 1] = c.day;
326
+ rows += n;
327
+ } else {
328
+ ends.push(c.day);
329
+ rows = n;
330
+ }
331
+ }
332
+ // The first slice starts on the first day and the last one ends on the last day.
333
+ ends[Math.max(0, ends.length - 1)] = lastDay;
334
+ let from = firstDay;
335
+ for (const to of ends) {
336
+ // Also before the first slice: database calls settle as microtasks, so
337
+ // without it a parallel read's first slice would run in this same turn.
338
+ await yieldLoop();
339
+ await onRows(
340
+ await client.query<T>(
341
+ `SELECT ${columns} FROM automation_savings_daily WHERE ${where} AND day>=? AND day<=? ORDER BY day`,
342
+ [...params, from, to],
343
+ ),
344
+ );
345
+ from = utcDay(Date.parse(dayStartIso(to)) + DAY_MS);
346
+ }
347
+ }
348
+
349
+ /** The priced part of a read, shared by every band: the band only picks the total. */
350
+ type PricedCore<R> = {
351
+ computedAt: string | null;
352
+ boundaryEstimated: boolean;
353
+ rows: R[];
354
+ jobTotal: SavingsTotals | null;
355
+ fleetTotal: SavingsTotals | null;
356
+ points: SavingsDailyPoint[];
357
+ };
358
+
359
+ type FleetCacheEntry = { fingerprint: string; at: number; value: Promise<PricedCore<unknown>> };
360
+ const fleetCache = new Map<string, FleetCacheEntry>();
361
+ let fleetLoads = 0;
362
+
363
+ const curveKeys = new WeakMap<CurveSet, string>();
364
+
365
+ /** Cache-key part for a session-cost curve: a deployment calibration prices differently from the shipped one. */
366
+ function curveKey(curve: CurveSet | undefined): string {
367
+ if (!curve) return "shipped";
368
+ let key = curveKeys.get(curve);
369
+ if (key === undefined) {
370
+ key = Bun.hash(JSON.stringify(curve)).toString(36);
371
+ curveKeys.set(curve, key);
372
+ }
373
+ return key;
374
+ }
375
+
376
+ /** Test hook: forget cached fleet reads and first-day scans. */
377
+ export function resetSavingsReadCache(): void {
378
+ sliceCache.clear();
379
+ fleetCache.clear();
380
+ fleetLoads = 0;
381
+ }
382
+
383
+ /** Test hook: how many fleet reads hit the database since the last reset. */
384
+ export const savingsFleetLoads = (): number => fleetLoads;
385
+
386
+ /**
387
+ * One load per window, tier and rollup state: concurrent readers (the fleet
388
+ * page fires four) share the in-flight promise, and later ones reuse the
389
+ * result until a rollup write changes the fingerprint or the TTL runs out.
390
+ */
391
+ function cachedFleet<R>(
392
+ key: string,
393
+ fingerprint: string,
394
+ load: () => Promise<PricedCore<R>>,
395
+ ): Promise<PricedCore<R>> {
396
+ const now = Date.now();
397
+ const hit = fleetCache.get(key);
398
+ if (hit && hit.fingerprint === fingerprint && now - hit.at < FLEET_CACHE_TTL_MS) {
399
+ return hit.value as Promise<PricedCore<R>>;
400
+ }
401
+ for (const [k, e] of fleetCache) if (now - e.at >= FLEET_CACHE_TTL_MS) fleetCache.delete(k);
402
+ fleetLoads++;
403
+ const value = load();
404
+ const entry: FleetCacheEntry = { fingerprint, at: now, value };
405
+ fleetCache.set(key, entry);
406
+ value.catch(() => {
407
+ if (fleetCache.get(key) === entry) fleetCache.delete(key);
408
+ });
409
+ return value;
410
+ }
411
+
412
+ /**
413
+ * Price the workflows of a window from the rollup: every workflow when
414
+ * `workflowId` is absent, else that one (its job's other versions still share
415
+ * the schedule cap). `band: "fleet"` prices the total with the fleet spread
416
+ * even for one row, for a caller that adds it to other totals.
417
+ */
418
+ export async function readWorkflowSavings(opts: {
419
+ days: number;
420
+ tier: SavingsTier;
421
+ now: number;
422
+ workflowId?: string;
423
+ band?: "fleet";
424
+ /** Runs the live scan of the window's partial first day. */
425
+ sql?: SavingsSql;
426
+ /** A deployment calibration of the session-cost curve; the shipped curve when absent. */
427
+ curve?: CurveSet;
428
+ }): Promise<WorkflowSavingsRead> {
429
+ const client = getDbClient();
430
+ const { firstDay, lastDay, ...window } = savingsWindow(opts.days, opts.now);
431
+ const calendarDays = daysBetween(firstDay, lastDay);
432
+ const marker = await client.get<{ n: number; at: string | null }>(
433
+ "SELECT count(*) n, max(computedAt) at FROM automation_savings_days WHERE jobKind='workflow' AND modelVersion=? AND day>=? AND day<=?",
434
+ [MODEL_VERSION, firstDay, lastDay],
435
+ );
436
+ const complete = Number(marker?.n) >= calendarDays.length;
437
+ const markerAt = marker?.at ?? null;
438
+ const scope = {
439
+ days: opts.days,
440
+ tier: opts.tier,
441
+ window,
442
+ firstDay,
443
+ lastDay,
444
+ calendarDays,
445
+ markerAt,
446
+ sql: opts.sql,
447
+ curve: opts.curve,
448
+ };
449
+
450
+ let core: PricedCore<WorkflowSavingsRow>;
451
+ if (opts.workflowId) {
452
+ const own = await client.get<{ jobKey: string }>(
453
+ "SELECT jobKey FROM automation_savings_daily WHERE jobKind='workflow' AND jobId=? AND day>=? AND day<=? LIMIT 1",
454
+ [opts.workflowId, firstDay, lastDay],
455
+ );
456
+ if (!own) {
457
+ return {
458
+ window,
459
+ computedAt: markerAt,
460
+ complete,
461
+ boundaryEstimated: false,
462
+ rows: [],
463
+ total: null,
464
+ points: [],
465
+ };
466
+ }
467
+ core = await priceWorkflowBuckets({
468
+ ...scope,
469
+ jobKey: own.jobKey,
470
+ workflowId: opts.workflowId,
471
+ });
472
+ } else {
473
+ // The window start is part of the key: the first day's live scan depends on it.
474
+ core = await cachedFleet(
475
+ `workflow:${opts.days}:${opts.tier}:${curveKey(opts.curve)}:${window.start}:${lastDay}`,
476
+ `${savingsRollupGeneration()}:${marker?.n}:${markerAt}`,
477
+ () => priceWorkflowBuckets(scope),
478
+ );
479
+ }
480
+ const band = opts.band ?? (core.rows.length === 1 ? "job" : "fleet");
481
+ return {
482
+ window,
483
+ computedAt: core.computedAt,
484
+ complete,
485
+ boundaryEstimated: core.boundaryEstimated,
486
+ rows: [...core.rows],
487
+ total: band === "job" ? core.jobTotal : core.fleetTotal,
488
+ points: [...core.points],
489
+ };
490
+ }
491
+
492
+ async function priceWorkflowBuckets(scope: {
493
+ days: number;
494
+ tier: SavingsTier;
495
+ window: SavingsWindow;
496
+ firstDay: string;
497
+ lastDay: string;
498
+ calendarDays: string[];
499
+ markerAt: string | null;
500
+ sql?: SavingsSql;
501
+ curve?: CurveSet;
502
+ jobKey?: string;
503
+ workflowId?: string;
504
+ }): Promise<PricedCore<WorkflowSavingsRow>> {
505
+ const keyFilter = scope.jobKey !== undefined ? "AND jobKey=?" : "";
506
+ let computedAt = scope.markerAt;
507
+ let boundaryEstimated = false;
508
+ const jobs = new Map<string, Job>();
509
+ const fold = async (buckets: (BucketRow & { computedAt: string })[]) => {
510
+ for (const b of buckets)
511
+ if (!computedAt || b.computedAt > computedAt) computedAt = b.computedAt;
512
+ // Slices arrive oldest first: only the first one holds the partial first day.
513
+ if (buckets.some((b) => b.day === scope.firstDay)) {
514
+ boundaryEstimated = await clipFirstDay(
515
+ buckets,
516
+ scope.window,
517
+ scope.firstDay,
518
+ !scope.workflowId,
519
+ scope.sql,
520
+ );
521
+ }
522
+ for (const b of buckets) {
523
+ if (!(Number(b.runs) > 0)) continue;
524
+ let job = jobs.get(b.jobId);
525
+ if (!job) {
526
+ job = {
527
+ workflowId: b.jobId,
528
+ jobKey: b.jobKey,
529
+ name: b.jobName,
530
+ facts: zeroFacts(),
531
+ days: [],
532
+ };
533
+ jobs.set(b.jobId, job);
534
+ }
535
+ // The newest bucket carries the current name.
536
+ job.name = b.jobName;
537
+ addFacts(job.facts, b);
538
+ job.days.push(b);
539
+ }
540
+ };
541
+ await forEachBucketChunk(
542
+ `day, jobId, jobKey, jobName, computedAt, ${SAVINGS_FACT_COLUMNS.join(", ")}`,
543
+ `jobKind='workflow' AND subKind='' AND modelVersion=? ${keyFilter}`,
544
+ [MODEL_VERSION, ...(scope.jobKey !== undefined ? [scope.jobKey] : [])],
545
+ scope.firstDay,
546
+ scope.lastDay,
547
+ fold,
548
+ scope.jobKey !== undefined ? "idx_automation_savings_daily_key" : undefined,
549
+ );
550
+
551
+ // Versions of one job share the schedule cap: the live version first, then
552
+ // retired copies by name. A version priced by the enabled-only rule takes no
553
+ // share of the cap.
554
+ const allocations = new Map<string, ScheduleAllocation>();
555
+ const byKey = new Map<string, Job[]>();
556
+ for (const job of jobs.values()) {
557
+ if (isEnabledOnlyRouter(job.facts, scope.days)) continue;
558
+ byKey.set(job.jobKey, [...(byKey.get(job.jobKey) ?? []), job]);
559
+ }
560
+ for (const versions of byKey.values()) {
561
+ versions.sort(
562
+ (a, b) =>
563
+ Number(isRetired(a.name)) - Number(isRetired(b.name)) || a.name.localeCompare(b.name),
564
+ );
565
+ const shares = allocateSchedule(
566
+ versions.map((v) => v.facts),
567
+ scope.days,
568
+ );
569
+ versions.forEach((v, i) => {
570
+ allocations.set(v.workflowId, shares[i] as ScheduleAllocation);
571
+ });
572
+ }
573
+
574
+ const selected = [...jobs.values()].filter(
575
+ (j) => !scope.workflowId || j.workflowId === scope.workflowId,
576
+ );
577
+ const rows: WorkflowSavingsRow[] = [];
578
+ const jobParts: PricedWorkflow[] = [];
579
+ const fleetParts: PricedWorkflow[] = [];
580
+ const daily = dayTotals(scope.calendarDays);
581
+ const pricing = { tier: scope.tier, days: scope.days, curve: scope.curve };
582
+ for (const [i, job] of selected.entries()) {
583
+ if (i > 0 && i % PRICE_BATCH === 0) await yieldLoop();
584
+ const allocation = allocations.get(job.workflowId);
585
+ const priced = priceWorkflow(job.facts, { ...pricing, band: "job" }, allocation);
586
+ rows.push({ ...priced, workflowId: job.workflowId, name: job.name });
587
+ jobParts.push(priced);
588
+ fleetParts.push(priceWorkflow(job.facts, { ...pricing, band: "fleet" }, allocation));
589
+ daily.add(splitByDay(job.facts, job.days, pricing, priced));
590
+ }
591
+ // Ties break on the id, so the order does not depend on which index served the read.
592
+ rows.sort((a, b) => b.savingUsd.p50 - a.savingUsd.p50 || byId(a.workflowId, b.workflowId));
593
+
594
+ return {
595
+ computedAt,
596
+ boundaryEstimated,
597
+ rows,
598
+ jobTotal: jobParts.length > 0 ? sumTotals(jobParts) : null,
599
+ fleetTotal: fleetParts.length > 0 ? sumTotals(fleetParts) : null,
600
+ points: rows.length > 0 ? daily.points : [],
601
+ };
602
+ }
603
+
604
+ /**
605
+ * Replace each first-day bucket with the facts of the runs inside the window,
606
+ * scanned live (the window starts mid-day). Returns whether any of them had to
607
+ * be estimated instead.
608
+ *
609
+ * A fleet read (no workflowId) scans from the window start floored to the hour
610
+ * and keeps the scans for that hour, so list reads cost one scan per workflow
611
+ * per hour, not per minute. The scan groups runs by minute, and every read
612
+ * keeps only the minutes from `window.start` on, so both reads stay exact to
613
+ * the minute. An estimate is scaled to the part of the slice inside the window.
614
+ */
615
+ async function clipFirstDay(
616
+ buckets: BucketRow[],
617
+ window: SavingsWindow,
618
+ firstDay: string,
619
+ fleet: boolean,
620
+ sql: SavingsSql = (q, p) => getDbClient().query(q, p),
621
+ ): Promise<boolean> {
622
+ const startMs = Date.parse(window.start);
623
+ // A window starting at midnight keeps the whole first-day bucket.
624
+ if (window.start === dayStartIso(firstDay)) return false;
625
+ const sliceStart = fleet
626
+ ? new Date(Math.floor(startMs / HOUR_MS) * HOUR_MS).toISOString()
627
+ : window.start;
628
+ const first = buckets.filter((b) => b.day === firstDay);
629
+ if (first.length === 0) return false;
630
+ const ids = [...new Set(first.map((b) => b.jobId))];
631
+ const defs = await getDbClient().query<{ id: string; definition: unknown }>(
632
+ `SELECT id, definition FROM workflows WHERE id IN (${ids.map(() => "?").join(",")})`,
633
+ ids,
634
+ );
635
+ const defOf = new Map(defs.map((d) => [d.id, parseDefinition(d.definition)]));
636
+ const slice = { start: sliceStart, end: dayStartIso(utcDay(startMs + DAY_MS)) };
637
+ const ttl = fleet ? FLEET_SLICE_TTL_MS : SLICE_TTL_MS;
638
+ const endMs = Date.parse(slice.end);
639
+ const dayShare = (endMs - startMs) / DAY_MS;
640
+ // The part of the slice (floored to the hour on a fleet read) inside the window.
641
+ const sliceShare = (endMs - startMs) / (endMs - Date.parse(slice.start));
642
+ const startMinute = minuteKey(window.start);
643
+ let estimated = false;
644
+ const clip = async (b: BucketRow) => {
645
+ const result: SliceResult = defOf.has(b.jobId)
646
+ ? await scanSlice({ id: b.jobId, definition: defOf.get(b.jobId) }, slice, sql, ttl)
647
+ : { runs: null };
648
+ let facts: WorkflowFacts;
649
+ if ("byMinute" in result) {
650
+ facts = zeroFacts();
651
+ for (const m of result.byMinute) if (m.day >= startMinute) addFacts(facts, m);
652
+ } else {
653
+ estimated = true;
654
+ const share =
655
+ result.runs !== null && Number(b.runs) > 0
656
+ ? Math.min(1, result.runs / Number(b.runs)) * sliceShare
657
+ : dayShare;
658
+ facts = scaleBucket(b, share);
659
+ }
660
+ for (const c of SAVINGS_FACT_COLUMNS) b[c] = facts[c];
661
+ };
662
+ // A few scans at a time, below the bounded executor's concurrency cap.
663
+ const queue = [...first];
664
+ await Promise.all(
665
+ Array.from({ length: Math.min(FIRST_DAY_SCAN_CONCURRENCY, queue.length) }, async () => {
666
+ for (let b = queue.shift(); b; b = queue.shift()) await clip(b);
667
+ }),
668
+ );
669
+ return estimated;
670
+ }
671
+
672
+ function sumBand(parts: SavingsBand[]): SavingsBand {
673
+ return {
674
+ p10: parts.reduce((s, b) => s + b.p10, 0),
675
+ p50: parts.reduce((s, b) => s + b.p50, 0),
676
+ p90: parts.reduce((s, b) => s + b.p90, 0),
677
+ };
678
+ }
679
+
680
+ function sumTotals(parts: PricedWorkflow[]): SavingsTotals {
681
+ return {
682
+ runs: parts.reduce((s, p) => s + p.runs, 0),
683
+ substitutedRuns: parts.reduce((s, p) => s + p.substitutedRuns, 0),
684
+ enabledOnlyRuns: parts.reduce((s, p) => s + p.enabledOnlyRuns, 0),
685
+ measuredUsd: parts.reduce((s, p) => s + p.measuredUsd, 0),
686
+ estimatedLlmUsd: parts.reduce((s, p) => s + p.estimatedLlmUsd, 0),
687
+ actualUsd: parts.reduce((s, p) => s + p.actualUsd, 0),
688
+ agentOnlyUsd: sumBand(parts.map((p) => p.agentOnlyUsd)),
689
+ savingUsd: sumBand(parts.map((p) => p.savingUsd)),
690
+ actualHours: parts.reduce((s, p) => s + p.actualHours, 0),
691
+ hoursSaved: sumBand(parts.map((p) => p.hoursSaved)),
692
+ };
693
+ }
694
+
695
+ /**
696
+ * One point per UTC day the window touches; `add` adds up the points of each
697
+ * day, and days without runs stay zero. A fleet read adds each job's points
698
+ * as it prices the job, between its event-loop yields: one pass over the
699
+ * whole fleet's points (91k at fleet scale) stalled the loop for ~20ms.
700
+ */
701
+ function dayTotals(calendarDays: string[]) {
702
+ const days = calendarDays.map((day) => ({
703
+ day,
704
+ savingUsd: 0,
705
+ actualUsd: 0,
706
+ measuredUsd: 0,
707
+ estimatedLlmUsd: 0,
708
+ }));
709
+ const byDay = new Map(days.map((p) => [p.day, p]));
710
+ return {
711
+ points: days as SavingsDailyPoint[],
712
+ add(points: SavingsDailyPoint[]): void {
713
+ for (const p of points) {
714
+ const d = byDay.get(p.day);
715
+ if (!d) continue;
716
+ d.savingUsd += p.savingUsd;
717
+ d.actualUsd += p.actualUsd;
718
+ d.measuredUsd += p.measuredUsd;
719
+ d.estimatedLlmUsd += p.estimatedLlmUsd;
720
+ }
721
+ },
722
+ };
723
+ }
724
+
725
+ /** The day totals of `points`, one point per UTC day the window touches. */
726
+ function fillDays(points: SavingsDailyPoint[], calendarDays: string[]): SavingsDailyPoint[] {
727
+ const totals = dayTotals(calendarDays);
728
+ totals.add(points);
729
+ return totals.points;
730
+ }
731
+
732
+ export type ScriptSavingsRow = PricedWorkflow & {
733
+ kind: ScriptKind;
734
+ scriptName: string;
735
+ name: string;
736
+ };
737
+
738
+ export type ScriptSavingsRead = Omit<WorkflowSavingsRead, "rows"> & { rows: ScriptSavingsRow[] };
739
+
740
+ /** A script name the savings route answers for: one in the catalog, or the inline-source bucket. */
741
+ export async function isKnownScriptName(name: string): Promise<boolean> {
742
+ if (name === INLINE_SCRIPT_NAME) return true;
743
+ return !!(await getDbClient().get<{ n: number }>("SELECT 1 n FROM scripts WHERE name=? LIMIT 1", [
744
+ name,
745
+ ]));
746
+ }
747
+
748
+ const zeroScriptFacts = (): ScriptFacts => ({
749
+ runs: 0,
750
+ apiRuns: 0,
751
+ effectRuns: 0,
752
+ apiEffectRuns: 0,
753
+ opsSum: 0,
754
+ computeSecs: 0,
755
+ });
756
+
757
+ type ScriptBucketRow = {
758
+ day: string;
759
+ jobId: string;
760
+ subKind: string;
761
+ computedAt: string;
762
+ runs: number;
763
+ effectRuns: number;
764
+ opsSum: number;
765
+ computeSecs: number;
766
+ };
767
+
768
+ /** Fold a bucket row (subKind '' = non-API runs, 'api' = API runs) into script facts. */
769
+ function addScriptBucket(into: ScriptFacts, b: ScriptBucketRow): void {
770
+ const runs = Number(b.runs) || 0;
771
+ const effect = Number(b.effectRuns) || 0;
772
+ into.runs += runs;
773
+ into.opsSum += Number(b.opsSum) || 0;
774
+ into.computeSecs += Number(b.computeSecs) || 0;
775
+ if (b.subKind === "api") {
776
+ into.apiRuns += runs;
777
+ into.apiEffectRuns += effect;
778
+ } else {
779
+ into.effectRuns += effect;
780
+ }
781
+ }
782
+
783
+ /**
784
+ * Price the scripts of a window from the rollup: every script name when
785
+ * `scriptName` is absent, else that one. A script is "scheduled" when a
786
+ * schedule targets it now (v7 reads the same at run time); scripts are keyed
787
+ * by name, so an agent-scoped and a global script of one name share a row.
788
+ */
789
+ export async function readScriptSavings(opts: {
790
+ days: number;
791
+ tier: SavingsTier;
792
+ now: number;
793
+ scriptName?: string;
794
+ /** As in `readWorkflowSavings`. */
795
+ band?: "fleet";
796
+ /** Runs the live scan of the window's partial first day. */
797
+ sql?: SavingsSql;
798
+ /** A deployment calibration of the session-cost curve; the shipped curve when absent. */
799
+ curve?: CurveSet;
800
+ }): Promise<ScriptSavingsRead> {
801
+ const client = getDbClient();
802
+ const { firstDay, lastDay, ...window } = savingsWindow(opts.days, opts.now);
803
+ const calendarDays = daysBetween(firstDay, lastDay);
804
+ // Issued together: each query waits its turn on the shared connection, and
805
+ // two queued at once wait about once.
806
+ const [marker, scheduledRows] = await Promise.all([
807
+ client.get<{ n: number; at: string | null }>(
808
+ "SELECT count(*) n, max(computedAt) at FROM automation_savings_days WHERE jobKind='script' AND modelVersion=? AND day>=? AND day<=?",
809
+ [MODEL_VERSION, firstDay, lastDay],
810
+ ),
811
+ client.query<{ scriptName: string }>(
812
+ "SELECT DISTINCT scriptName FROM scheduled_tasks WHERE targetType='script' AND scriptName IS NOT NULL ORDER BY scriptName",
813
+ ),
814
+ ]);
815
+ const complete = Number(marker?.n) >= calendarDays.length;
816
+ const scheduled = scheduledRows.map((r) => r.scriptName);
817
+ const scope = {
818
+ days: opts.days,
819
+ tier: opts.tier,
820
+ window,
821
+ firstDay,
822
+ lastDay,
823
+ calendarDays,
824
+ markerAt: marker?.at ?? null,
825
+ scheduled: new Set(scheduled),
826
+ scriptName: opts.scriptName,
827
+ sql: opts.sql,
828
+ curve: opts.curve,
829
+ };
830
+ const core =
831
+ opts.scriptName !== undefined
832
+ ? await priceScriptBuckets(scope)
833
+ : await cachedFleet(
834
+ `script:${opts.days}:${opts.tier}:${curveKey(opts.curve)}:${window.start}:${lastDay}`,
835
+ `${savingsRollupGeneration()}:${marker?.n}:${scope.markerAt}:${JSON.stringify(scheduled)}`,
836
+ () => priceScriptBuckets(scope),
837
+ );
838
+ const band = opts.band ?? (core.rows.length === 1 ? "job" : "fleet");
839
+ return {
840
+ window,
841
+ computedAt: core.computedAt,
842
+ complete,
843
+ boundaryEstimated: core.boundaryEstimated,
844
+ rows: [...core.rows],
845
+ total: band === "job" ? core.jobTotal : core.fleetTotal,
846
+ points: [...core.points],
847
+ };
848
+ }
849
+
850
+ async function priceScriptBuckets(scope: {
851
+ days: number;
852
+ tier: SavingsTier;
853
+ window: SavingsWindow;
854
+ firstDay: string;
855
+ lastDay: string;
856
+ calendarDays: string[];
857
+ markerAt: string | null;
858
+ scheduled: Set<string>;
859
+ scriptName?: string;
860
+ sql?: SavingsSql;
861
+ curve?: CurveSet;
862
+ }): Promise<PricedCore<ScriptSavingsRow>> {
863
+ const byName = scope.scriptName !== undefined;
864
+ let computedAt = scope.markerAt;
865
+ const jobs = new Map<string, { facts: ScriptFacts; days: Map<string, ScriptFacts> }>();
866
+ await forEachBucketChunk<ScriptBucketRow>(
867
+ "day, jobId, subKind, computedAt, runs, effectRuns, opsSum, computeSecs",
868
+ `jobKind='script' AND modelVersion=? ${byName ? "AND jobId=?" : ""}`,
869
+ [MODEL_VERSION, ...(byName ? [scope.scriptName as string] : [])],
870
+ scope.firstDay,
871
+ scope.lastDay,
872
+ (buckets) => {
873
+ for (const b of buckets) {
874
+ if (!computedAt || b.computedAt > computedAt) computedAt = b.computedAt;
875
+ let job = jobs.get(b.jobId);
876
+ if (!job) {
877
+ job = { facts: zeroScriptFacts(), days: new Map() };
878
+ jobs.set(b.jobId, job);
879
+ }
880
+ addScriptBucket(job.facts, b);
881
+ const day = job.days.get(b.day) ?? zeroScriptFacts();
882
+ addScriptBucket(day, b);
883
+ job.days.set(b.day, day);
884
+ }
885
+ },
886
+ byName ? "idx_automation_savings_daily_job" : undefined,
887
+ );
888
+ const boundaryEstimated = await clipScriptFirstDay(jobs, scope.window, scope.firstDay, scope);
889
+
890
+ const rows: ScriptSavingsRow[] = [];
891
+ const jobParts: PricedWorkflow[] = [];
892
+ const fleetParts: PricedWorkflow[] = [];
893
+ const daily = dayTotals(scope.calendarDays);
894
+ let i = 0;
895
+ for (const [scriptName, job] of jobs) {
896
+ if (i > 0 && i % PRICE_BATCH === 0) await yieldLoop();
897
+ i++;
898
+ const kind: ScriptKind = scope.scheduled.has(scriptName)
899
+ ? "script-scheduled"
900
+ : "script-agent-invoked";
901
+ const pricing = { tier: scope.tier, days: scope.days, kind, curve: scope.curve };
902
+ const priced = priceScript(job.facts, { ...pricing, band: "job" });
903
+ rows.push({ ...priced, kind, scriptName, name: scriptName });
904
+ jobParts.push(priced);
905
+ fleetParts.push(priceScript(job.facts, { ...pricing, band: "fleet" }));
906
+ daily.add(
907
+ splitScriptByDay(
908
+ job.facts,
909
+ [...job.days].map(([day, f]) => ({ ...f, day })),
910
+ priced,
911
+ ),
912
+ );
913
+ }
914
+ rows.sort((a, b) => b.savingUsd.p50 - a.savingUsd.p50 || byId(a.scriptName, b.scriptName));
915
+
916
+ return {
917
+ computedAt,
918
+ boundaryEstimated,
919
+ rows,
920
+ jobTotal: jobParts.length > 0 ? sumTotals(jobParts) : null,
921
+ fleetTotal: fleetParts.length > 0 ? sumTotals(fleetParts) : null,
922
+ points: rows.length > 0 ? daily.points : [],
923
+ };
924
+ }
925
+
926
+ /** Deepest run tree the "with components" view follows. */
927
+ export const MAX_COMPONENT_DEPTH = 10;
928
+
929
+ export type SavingsComponent = {
930
+ kind: "workflow" | "script" | "task";
931
+ /** Set for workflow components. */
932
+ workflowId?: string;
933
+ /** Set for script components. */
934
+ scriptName?: string;
935
+ name: string;
936
+ /** How the nearest occurrence below the root is linked. */
937
+ linkKind: SavingsLinkKind;
938
+ /** 1 for a direct child of the root. */
939
+ depth: number;
940
+ /** Runs (tasks for a task component) in this tree. Fractional below the first level. */
941
+ runs: number;
942
+ actualUsd: number;
943
+ agentOnlyUsd: SavingsBand;
944
+ savingUsd: SavingsBand;
945
+ hoursSaved: SavingsBand;
946
+ /**
947
+ * Tasks the root caused without owning them: their spend is already in the
948
+ * root's own row, on both sides, so they add nothing here.
949
+ */
950
+ inOwnRow: boolean;
951
+ };
952
+
953
+ type SavingsLinkKind =
954
+ | "sub-workflow"
955
+ | "step-workflow"
956
+ | "step-script"
957
+ | "step-task"
958
+ | "descendant-task";
959
+
960
+ export type WorkflowComponentsRead = {
961
+ components: SavingsComponent[];
962
+ /** Own runs plus every component's share; null when the root has no row. */
963
+ withComponents: SavingsTotals | null;
964
+ };
965
+
966
+ const scaleBand = (b: SavingsBand, f: number): SavingsBand => ({
967
+ p10: b.p10 * f,
968
+ p50: b.p50 * f,
969
+ p90: b.p90 * f,
970
+ });
971
+
972
+ /** A share `f` of a priced row: its per-run figures times the linked runs. */
973
+ function scaleRow(row: PricedWorkflow, f: number): PricedWorkflow {
974
+ return {
975
+ ...row,
976
+ runs: row.runs * f,
977
+ substitutedRuns: row.substitutedRuns * f,
978
+ enabledOnlyRuns: row.enabledOnlyRuns * f,
979
+ effectRuns: row.effectRuns * f,
980
+ measuredUsd: row.measuredUsd * f,
981
+ estimatedLlmUsd: row.estimatedLlmUsd * f,
982
+ actualUsd: row.actualUsd * f,
983
+ agentOnlyUsd: scaleBand(row.agentOnlyUsd, f),
984
+ savingUsd: scaleBand(row.savingUsd, f),
985
+ actualHours: row.actualHours * f,
986
+ agentOnlyHours: scaleBand(row.agentOnlyHours, f),
987
+ hoursSaved: scaleBand(row.hoursSaved, f),
988
+ };
989
+ }
990
+
991
+ const zeroBand = (): SavingsBand => ({ p10: 0, p50: 0, p90: 0 });
992
+ const addBand = (a: SavingsBand, b: SavingsBand): SavingsBand => ({
993
+ p10: a.p10 + b.p10,
994
+ p50: a.p50 + b.p50,
995
+ p90: a.p90 + b.p90,
996
+ });
997
+
998
+ /**
999
+ * The workflow's run tree for the window: every workflow run, nested script
1000
+ * run and unowned task its runs caused, recursively (depth at most
1001
+ * MAX_COMPONENT_DEPTH, never re-entering a workflow already on the path).
1002
+ * Each component is priced as on its own row: that row's figures per run
1003
+ * times the runs linked into this tree. A child workflow's own links count in
1004
+ * the share of its runs this tree started. The sum is a read-time view: no
1005
+ * list or fleet total adds it, since every component is on its own row.
1006
+ */
1007
+ export async function readWorkflowComponents(opts: {
1008
+ days: number;
1009
+ tier: SavingsTier;
1010
+ now: number;
1011
+ workflowId: string;
1012
+ /** The root's own row, as the single-workflow read priced it. */
1013
+ own: WorkflowSavingsRow;
1014
+ /** The curve the own row was priced with, so every component is priced alike. */
1015
+ curve?: CurveSet;
1016
+ }): Promise<WorkflowComponentsRead> {
1017
+ const { firstDay, lastDay, start } = savingsWindow(opts.days, opts.now);
1018
+ // The window starts mid-way through its first UTC day; links are stored per
1019
+ // day, so that day counts by the share of it inside the window.
1020
+ // shortcut: a day-share estimate, not a live scan; scan the first day's links
1021
+ // like the rows do if component figures near the boundary need to be exact.
1022
+ const firstDayShare =
1023
+ (Date.parse(dayStartIso(utcDay(Date.parse(start) + DAY_MS))) - Date.parse(start)) / DAY_MS;
1024
+ const [workflows, scripts, links] = await Promise.all([
1025
+ readWorkflowSavings({ days: opts.days, tier: opts.tier, now: opts.now, curve: opts.curve }),
1026
+ readScriptSavings({ days: opts.days, tier: opts.tier, now: opts.now, curve: opts.curve }),
1027
+ getDbClient().query<{
1028
+ parentJobId: string;
1029
+ childKind: "workflow" | "script" | "task";
1030
+ childJobId: string;
1031
+ linkKind: SavingsLinkKind;
1032
+ n: number;
1033
+ usd: number;
1034
+ }>(
1035
+ `SELECT parentJobId, childKind, childJobId, linkKind,
1036
+ sum(CASE WHEN day=? THEN n*? ELSE n END) n,
1037
+ sum(CASE WHEN day=? THEN actualUsd*? ELSE actualUsd END) usd
1038
+ FROM automation_savings_links WHERE modelVersion=? AND day>=? AND day<=?
1039
+ GROUP BY 1,2,3,4 ORDER BY 1,2,3,4`,
1040
+ [firstDay, firstDayShare, firstDay, firstDayShare, MODEL_VERSION, firstDay, lastDay],
1041
+ ),
1042
+ ]);
1043
+ const workflowRows = new Map(workflows.rows.map((r) => [r.workflowId, r]));
1044
+ workflowRows.set(opts.workflowId, opts.own);
1045
+ const scriptRows = new Map(scripts.rows.map((r) => [r.scriptName, r]));
1046
+ const children = new Map<string, typeof links>();
1047
+ for (const l of links) children.set(l.parentJobId, [...(children.get(l.parentJobId) ?? []), l]);
1048
+
1049
+ const components = new Map<string, SavingsComponent>();
1050
+ const parts: PricedWorkflow[] = [];
1051
+ const addComponent = (
1052
+ c: Omit<SavingsComponent, "actualUsd" | "agentOnlyUsd" | "savingUsd" | "hoursSaved">,
1053
+ priced: PricedWorkflow | null,
1054
+ taskUsd = 0,
1055
+ ) => {
1056
+ const key = `${c.kind}\u0000${c.workflowId ?? c.scriptName ?? c.linkKind}`;
1057
+ const prev = components.get(key);
1058
+ components.set(key, {
1059
+ ...c,
1060
+ linkKind: prev && prev.depth <= c.depth ? prev.linkKind : c.linkKind,
1061
+ depth: Math.min(prev?.depth ?? c.depth, c.depth),
1062
+ runs: (prev?.runs ?? 0) + c.runs,
1063
+ actualUsd: (prev?.actualUsd ?? 0) + (priced?.actualUsd ?? taskUsd),
1064
+ agentOnlyUsd: addBand(prev?.agentOnlyUsd ?? zeroBand(), priced?.agentOnlyUsd ?? zeroBand()),
1065
+ savingUsd: addBand(prev?.savingUsd ?? zeroBand(), priced?.savingUsd ?? zeroBand()),
1066
+ hoursSaved: addBand(prev?.hoursSaved ?? zeroBand(), priced?.hoursSaved ?? zeroBand()),
1067
+ });
1068
+ if (priced) parts.push(priced);
1069
+ };
1070
+
1071
+ const walk = (parent: string, share: number, depth: number, path: Set<string>) => {
1072
+ for (const l of children.get(parent) ?? []) {
1073
+ const linked = Number(l.n) * share;
1074
+ if (l.childKind === "task") {
1075
+ // Only the root's: a child workflow's unowned tasks are in its row already.
1076
+ if (depth === 1 && linked > 0) {
1077
+ addComponent(
1078
+ {
1079
+ kind: "task",
1080
+ name:
1081
+ l.linkKind === "step-task"
1082
+ ? "Tasks its script steps created"
1083
+ : "Tasks below its agent tasks",
1084
+ linkKind: l.linkKind,
1085
+ depth,
1086
+ runs: linked,
1087
+ inOwnRow: true,
1088
+ },
1089
+ null,
1090
+ Number(l.usd) * share,
1091
+ );
1092
+ }
1093
+ continue;
1094
+ }
1095
+ // A cycle back to a workflow on the path is that workflow's own row: skip it.
1096
+ if (linked <= 0 || (l.childKind === "workflow" && path.has(l.childJobId))) continue;
1097
+ const row =
1098
+ l.childKind === "workflow" ? workflowRows.get(l.childJobId) : scriptRows.get(l.childJobId);
1099
+ const f = row && row.runs > 0 ? linked / row.runs : 0;
1100
+ addComponent(
1101
+ {
1102
+ kind: l.childKind,
1103
+ ...(l.childKind === "workflow"
1104
+ ? { workflowId: l.childJobId }
1105
+ : { scriptName: l.childJobId }),
1106
+ name: row?.name ?? l.childJobId,
1107
+ linkKind: l.linkKind,
1108
+ depth,
1109
+ runs: linked,
1110
+ inOwnRow: false,
1111
+ },
1112
+ row ? scaleRow(row, f) : null,
1113
+ );
1114
+ if (l.childKind === "workflow" && f > 0 && depth < MAX_COMPONENT_DEPTH) {
1115
+ walk(l.childJobId, Math.min(1, f), depth + 1, new Set([...path, l.childJobId]));
1116
+ }
1117
+ }
1118
+ };
1119
+ walk(opts.workflowId, 1, 1, new Set([opts.workflowId]));
1120
+
1121
+ const list = [...components.values()].sort(
1122
+ (a, b) =>
1123
+ Number(a.inOwnRow) - Number(b.inOwnRow) ||
1124
+ a.depth - b.depth ||
1125
+ b.savingUsd.p50 - a.savingUsd.p50,
1126
+ );
1127
+ return { components: list, withComponents: sumTotals([opts.own, ...parts]) };
1128
+ }
1129
+
1130
+ export type FleetKindTotals = {
1131
+ jobs: number;
1132
+ runs: number;
1133
+ substitutedRuns: number;
1134
+ /** p50 */
1135
+ savingUsd: number;
1136
+ /** p50 */
1137
+ hoursSaved: number;
1138
+ };
1139
+
1140
+ export type FleetSavingsRead = Omit<WorkflowSavingsRead, "rows"> & {
1141
+ workflows: WorkflowSavingsRow[];
1142
+ scripts: ScriptSavingsRow[];
1143
+ byKind: {
1144
+ workflows: FleetKindTotals;
1145
+ scheduledScripts: FleetKindTotals;
1146
+ agentInvokedScripts: FleetKindTotals;
1147
+ };
1148
+ };
1149
+
1150
+ function kindTotals(rows: PricedWorkflow[]): FleetKindTotals {
1151
+ return {
1152
+ jobs: rows.length,
1153
+ runs: rows.reduce((s, r) => s + r.runs, 0),
1154
+ substitutedRuns: rows.reduce((s, r) => s + r.substitutedRuns, 0),
1155
+ savingUsd: rows.reduce((s, r) => s + r.savingUsd.p50, 0),
1156
+ hoursSaved: rows.reduce((s, r) => s + r.hoursSaved.p50, 0),
1157
+ };
1158
+ }
1159
+
1160
+ function addTotals(a: SavingsTotals, b: SavingsTotals): SavingsTotals {
1161
+ const band = (x: SavingsBand, y: SavingsBand) => sumBand([x, y]);
1162
+ return {
1163
+ runs: a.runs + b.runs,
1164
+ substitutedRuns: a.substitutedRuns + b.substitutedRuns,
1165
+ enabledOnlyRuns: a.enabledOnlyRuns + b.enabledOnlyRuns,
1166
+ measuredUsd: a.measuredUsd + b.measuredUsd,
1167
+ estimatedLlmUsd: a.estimatedLlmUsd + b.estimatedLlmUsd,
1168
+ actualUsd: a.actualUsd + b.actualUsd,
1169
+ agentOnlyUsd: band(a.agentOnlyUsd, b.agentOnlyUsd),
1170
+ savingUsd: band(a.savingUsd, b.savingUsd),
1171
+ actualHours: a.actualHours + b.actualHours,
1172
+ hoursSaved: band(a.hoursSaved, b.hoursSaved),
1173
+ };
1174
+ }
1175
+
1176
+ /**
1177
+ * The fleet: every workflow and every script of the window. The total uses
1178
+ * the fleet band unless the fleet is one job; the daily points add up to its p50.
1179
+ */
1180
+ export async function readFleetSavings(opts: {
1181
+ days: number;
1182
+ tier: SavingsTier;
1183
+ now: number;
1184
+ /** Runs the live scan of the window's partial first day. */
1185
+ sql?: SavingsSql;
1186
+ /** A deployment calibration of the session-cost curve; the shipped curve when absent. */
1187
+ curve?: CurveSet;
1188
+ }): Promise<FleetSavingsRead> {
1189
+ // Independent reads: run together, so their database waits overlap.
1190
+ const [wf, sc] = await Promise.all([
1191
+ readWorkflowSavings({ ...opts, band: "fleet" }),
1192
+ readScriptSavings({ ...opts, band: "fleet" }),
1193
+ ]);
1194
+ const { firstDay, lastDay } = savingsWindow(opts.days, opts.now);
1195
+ const all: PricedWorkflow[] = [...wf.rows, ...sc.rows];
1196
+ const total =
1197
+ all.length === 1
1198
+ ? sumTotals(all)
1199
+ : wf.total && sc.total
1200
+ ? addTotals(wf.total, sc.total)
1201
+ : (wf.total ?? sc.total);
1202
+ const points =
1203
+ all.length > 0 ? fillDays([...wf.points, ...sc.points], daysBetween(firstDay, lastDay)) : [];
1204
+ const computedAt =
1205
+ wf.computedAt && sc.computedAt
1206
+ ? wf.computedAt > sc.computedAt
1207
+ ? wf.computedAt
1208
+ : sc.computedAt
1209
+ : (wf.computedAt ?? sc.computedAt);
1210
+ return {
1211
+ window: wf.window,
1212
+ computedAt,
1213
+ complete: wf.complete && sc.complete,
1214
+ boundaryEstimated: wf.boundaryEstimated || sc.boundaryEstimated,
1215
+ workflows: wf.rows,
1216
+ scripts: sc.rows,
1217
+ total,
1218
+ points,
1219
+ byKind: {
1220
+ workflows: kindTotals(wf.rows),
1221
+ scheduledScripts: kindTotals(sc.rows.filter((r) => r.kind === "script-scheduled")),
1222
+ agentInvokedScripts: kindTotals(sc.rows.filter((r) => r.kind === "script-agent-invoked")),
1223
+ },
1224
+ };
1225
+ }
1226
+
1227
+ /** Session spend of the whole swarm in the window, the same sum the usage page shows. */
1228
+ export async function readFleetLlmUsd(window: SavingsWindow): Promise<number> {
1229
+ const row = await getDbClient().get<{ usd: number | null }>(
1230
+ "SELECT sum(totalCostUsd) usd FROM session_costs WHERE createdAt>=? AND createdAt<=?",
1231
+ [window.start, window.end],
1232
+ );
1233
+ return Number(row?.usd) || 0;
1234
+ }
1235
+
1236
+ export type ScheduleRef = { scheduleId: string; scheduleName: string; scheduleEnabled: boolean };
1237
+
1238
+ /**
1239
+ * A schedule's saving. `modelled`: the schedule's workflow or script target, whose
1240
+ * row is the target's whole row because runs carry no schedule link (two schedules
1241
+ * of one target show the same row). `measured`: an agent-task schedule, which has
1242
+ * no automation to substitute, so it shows what its tasks cost and no saving.
1243
+ */
1244
+ export type ScheduleSavingsRow =
1245
+ | ((WorkflowSavingsRow | ScriptSavingsRow) & ScheduleRef & { basis: "modelled" })
1246
+ | (ScheduleRef & {
1247
+ basis: "measured";
1248
+ kind: "agent-task-schedule";
1249
+ name: string;
1250
+ /** Agent tasks the schedule created in the window. */
1251
+ runs: number;
1252
+ /** Recorded cost of those tasks only; their review tasks and follow-ups are not linked to the schedule. */
1253
+ actualUsd: number;
1254
+ actualHours: number;
1255
+ });
1256
+
1257
+ export type ScheduleSavingsRead = {
1258
+ window: SavingsWindow;
1259
+ computedAt: string | null;
1260
+ complete: boolean;
1261
+ boundaryEstimated: boolean;
1262
+ rows: ScheduleSavingsRow[];
1263
+ /** Set for one schedule whose workflow or script no longer exists. */
1264
+ targetMissing: boolean;
1265
+ };
1266
+
1267
+ type ScheduleTarget = {
1268
+ id: string;
1269
+ name: string;
1270
+ enabled: number;
1271
+ targetType: "agent-task" | "workflow" | "script";
1272
+ workflowId: string | null;
1273
+ scriptName: string | null;
1274
+ };
1275
+
1276
+ /**
1277
+ * Price the schedules of a window: every schedule when `scheduleId` is absent,
1278
+ * else that one, or null when it does not exist. A disabled schedule is still
1279
+ * priced, from the runs of its target in the window, and flagged. Schedules whose
1280
+ * target has no runs in the window, or no longer exists, have no row in a list.
1281
+ */
1282
+ export async function readScheduleSavings(opts: {
1283
+ days: number;
1284
+ tier: SavingsTier;
1285
+ now: number;
1286
+ scheduleId?: string;
1287
+ sql?: SavingsSql;
1288
+ curve?: CurveSet;
1289
+ }): Promise<ScheduleSavingsRead | null> {
1290
+ const client = getDbClient();
1291
+ const schedules = await client.query<ScheduleTarget>(
1292
+ `SELECT id, name, enabled, targetType, workflowId, scriptName FROM scheduled_tasks ${opts.scheduleId ? "WHERE id=?" : ""}`,
1293
+ opts.scheduleId ? [opts.scheduleId] : [],
1294
+ );
1295
+ if (opts.scheduleId && schedules.length === 0) return null;
1296
+ const single = opts.scheduleId !== undefined;
1297
+ const of = (targetType: ScheduleTarget["targetType"]) =>
1298
+ schedules.filter((s) => s.targetType === targetType);
1299
+ const base = {
1300
+ days: opts.days,
1301
+ tier: opts.tier,
1302
+ now: opts.now,
1303
+ sql: opts.sql,
1304
+ curve: opts.curve,
1305
+ };
1306
+
1307
+ // One rollup read per target type, narrowed to the target when asking for one schedule.
1308
+ const workflowSchedules = of("workflow");
1309
+ const scriptSchedules = of("script");
1310
+ const workflows = workflowSchedules.some((s) => s.workflowId)
1311
+ ? await readWorkflowSavings({
1312
+ ...base,
1313
+ workflowId: single ? (workflowSchedules[0]?.workflowId ?? undefined) : undefined,
1314
+ })
1315
+ : null;
1316
+ const scripts = scriptSchedules.some((s) => s.scriptName)
1317
+ ? await readScriptSavings({
1318
+ ...base,
1319
+ scriptName: single ? (scriptSchedules[0]?.scriptName ?? undefined) : undefined,
1320
+ })
1321
+ : null;
1322
+ const workflowRows = new Map(workflows?.rows.map((r) => [r.workflowId, r]));
1323
+ const scriptRows = new Map(scripts?.rows.map((r) => [r.scriptName, r]));
1324
+
1325
+ const window = savingsWindow(opts.days, opts.now);
1326
+ const measured = of("agent-task").length > 0 ? await readMeasuredSpend(window.start, opts) : null;
1327
+
1328
+ const rows: ScheduleSavingsRow[] = [];
1329
+ let targetMissing = false;
1330
+ for (const s of schedules) {
1331
+ const ref: ScheduleRef = {
1332
+ scheduleId: s.id,
1333
+ scheduleName: s.name,
1334
+ scheduleEnabled: Boolean(s.enabled),
1335
+ };
1336
+ if (s.targetType === "agent-task") {
1337
+ const spend = measured?.get(s.id);
1338
+ if (spend) {
1339
+ rows.push({
1340
+ ...ref,
1341
+ basis: "measured",
1342
+ kind: "agent-task-schedule",
1343
+ name: s.name,
1344
+ runs: spend.runs,
1345
+ actualUsd: spend.usd,
1346
+ actualHours: spend.ms / 3_600_000,
1347
+ });
1348
+ }
1349
+ continue;
1350
+ }
1351
+ const target =
1352
+ s.targetType === "workflow"
1353
+ ? s.workflowId
1354
+ ? workflowRows.get(s.workflowId)
1355
+ : undefined
1356
+ : s.scriptName
1357
+ ? scriptRows.get(s.scriptName)
1358
+ : undefined;
1359
+ if (target) rows.push({ ...target, ...ref, basis: "modelled" });
1360
+ else if (single) targetMissing = !(await targetExists(s));
1361
+ }
1362
+ rows.sort(
1363
+ (a, b) =>
1364
+ Number(b.basis === "modelled") - Number(a.basis === "modelled") ||
1365
+ (a.basis === "modelled" && b.basis === "modelled"
1366
+ ? b.savingUsd.p50 - a.savingUsd.p50
1367
+ : b.basis === "measured" && a.basis === "measured"
1368
+ ? b.actualUsd - a.actualUsd
1369
+ : 0),
1370
+ );
1371
+
1372
+ const reads = [workflows, scripts].filter((r) => r !== null);
1373
+ const newest = reads.map((r) => r.computedAt).filter((t): t is string => t !== null);
1374
+ return {
1375
+ window: { start: window.start, end: window.end, days: window.days },
1376
+ // Measured spend is read live, so a measured-only response is computed now.
1377
+ computedAt: newest.sort().at(-1) ?? (measured ? new Date(opts.now).toISOString() : null),
1378
+ complete: reads.every((r) => r.complete),
1379
+ boundaryEstimated: reads.some((r) => r.boundaryEstimated),
1380
+ rows,
1381
+ targetMissing,
1382
+ };
1383
+ }
1384
+
1385
+ async function targetExists(s: ScheduleTarget): Promise<boolean> {
1386
+ // A schedule with no target configured has nothing that could be gone.
1387
+ if (s.targetType === "workflow") {
1388
+ return (
1389
+ !s.workflowId ||
1390
+ !!(await getDbClient().get("SELECT 1 FROM workflows WHERE id=?", [s.workflowId]))
1391
+ );
1392
+ }
1393
+ return !s.scriptName || (await isKnownScriptName(s.scriptName));
1394
+ }
1395
+
1396
+ /** Tasks created by schedules in the window and their recorded cost: all schedules, or one. */
1397
+ async function readMeasuredSpend(
1398
+ start: string,
1399
+ opts: { scheduleId?: string; now: number },
1400
+ ): Promise<Map<string, { runs: number; usd: number; ms: number }>> {
1401
+ const rows = await getDbClient().query<{
1402
+ scheduleId: string;
1403
+ runs: number;
1404
+ usd: number | null;
1405
+ ms: number | null;
1406
+ }>(
1407
+ `SELECT t.scheduleId, count(DISTINCT t.id) runs, sum(c.totalCostUsd) usd, sum(c.durationMs) ms
1408
+ FROM agent_tasks t LEFT JOIN session_costs c ON c.taskId=t.id
1409
+ WHERE ${opts.scheduleId ? "t.scheduleId=?" : "t.scheduleId IS NOT NULL"} AND t.createdAt>=? AND t.createdAt<=?
1410
+ GROUP BY t.scheduleId`,
1411
+ [...(opts.scheduleId ? [opts.scheduleId] : []), start, new Date(opts.now).toISOString()],
1412
+ );
1413
+ return new Map(
1414
+ rows.map((r) => [
1415
+ r.scheduleId,
1416
+ { runs: Number(r.runs), usd: Number(r.usd) || 0, ms: Number(r.ms) || 0 },
1417
+ ]),
1418
+ );
1419
+ }
1420
+
1421
+ /**
1422
+ * Replace each script's first-day facts with those of its runs inside the
1423
+ * window, scanned live (the window starts mid-day), and drop scripts left with
1424
+ * no runs. When the scan fails or the slice has more than LIVE_MAX_RUNS runs,
1425
+ * the day is scaled by the share of it inside the window instead; returns
1426
+ * whether that happened. Scans are cached like the workflow slices: a fleet
1427
+ * read (no scriptName) floors the slice start to the hour and a single-script
1428
+ * read scans from the window start; both group runs by minute and keep only
1429
+ * the minutes from `window.start` on.
1430
+ */
1431
+ async function clipScriptFirstDay(
1432
+ jobs: Map<string, { facts: ScriptFacts; days: Map<string, ScriptFacts> }>,
1433
+ window: SavingsWindow,
1434
+ firstDay: string,
1435
+ opts: { scriptName?: string; sql?: SavingsSql },
1436
+ ): Promise<boolean> {
1437
+ const fleet = opts.scriptName === undefined;
1438
+ const startMs = Date.parse(window.start);
1439
+ // A window starting at midnight keeps the whole first-day bucket.
1440
+ if (window.start === dayStartIso(firstDay)) return false;
1441
+ const sliceStart = fleet
1442
+ ? new Date(Math.floor(startMs / HOUR_MS) * HOUR_MS).toISOString()
1443
+ : window.start;
1444
+ if (![...jobs.values()].some((j) => j.days.has(firstDay))) return false;
1445
+ const slice = { start: sliceStart, end: dayStartIso(utcDay(startMs + DAY_MS)) };
1446
+ const sql: SavingsSql = opts.sql ?? ((q, p) => getDbClient().query(q, p));
1447
+ const scope = { scriptName: opts.scriptName };
1448
+ const live = await cachedScan<ScriptSliceResult>(
1449
+ `script\u0000${MODEL_VERSION}\u0000${opts.scriptName ?? "*"}\u0000${slice.start}`,
1450
+ fleet ? FLEET_SLICE_TTL_MS : SLICE_TTL_MS,
1451
+ async () => {
1452
+ // Count first, so a busy slice never reaches the effect probe.
1453
+ if ((await countScriptRuns(slice, sql, scope)) > LIVE_MAX_RUNS) return null;
1454
+ return computeScriptWindow(slice, sql, { ...scope, bucket: "minute" });
1455
+ },
1456
+ null,
1457
+ opts.scriptName ?? "scripts",
1458
+ );
1459
+ // The runs from the window start on: the cached slice may start up to 59 minutes earlier.
1460
+ const startMinute = minuteKey(window.start);
1461
+ const inWindow = new Map<string, ScriptFacts>();
1462
+ for (const s of live ?? []) {
1463
+ const facts = zeroScriptFacts();
1464
+ for (const m of s.byDay) {
1465
+ if (m.day < startMinute) continue;
1466
+ for (const k of Object.keys(facts) as (keyof ScriptFacts)[]) facts[k] += m[k];
1467
+ }
1468
+ inWindow.set(s.scriptName, facts);
1469
+ }
1470
+ const dayShare = (Date.parse(slice.end) - startMs) / DAY_MS;
1471
+ for (const [name, job] of jobs) {
1472
+ const old = job.days.get(firstDay);
1473
+ if (!old) continue;
1474
+ const clipped = live
1475
+ ? (inWindow.get(name) ?? zeroScriptFacts())
1476
+ : scaleScriptFacts(old, dayShare);
1477
+ for (const k of Object.keys(old) as (keyof ScriptFacts)[]) {
1478
+ job.facts[k] += clipped[k] - old[k];
1479
+ }
1480
+ job.days.set(firstDay, clipped);
1481
+ if (!(job.facts.runs > 0)) jobs.delete(name);
1482
+ }
1483
+ return live === null;
1484
+ }
1485
+
1486
+ /** The UTC minute (`YYYY-MM-DDTHH:MM`) of an ISO timestamp: the key of a minute-bucketed scan. */
1487
+ function minuteKey(iso: string): string {
1488
+ return iso.slice(0, 16);
1489
+ }
1490
+
1491
+ function scaleScriptFacts(facts: ScriptFacts, share: number): ScriptFacts {
1492
+ return {
1493
+ runs: Math.round(facts.runs * share),
1494
+ apiRuns: Math.round(facts.apiRuns * share),
1495
+ effectRuns: Math.round(facts.effectRuns * share),
1496
+ apiEffectRuns: Math.round(facts.apiEffectRuns * share),
1497
+ opsSum: facts.opsSum * share,
1498
+ computeSecs: facts.computeSecs * share,
1499
+ };
1500
+ }