@desplega.ai/agent-swarm 2.8.0 → 2.8.2

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 (216) hide show
  1. package/dist/{acp-adapter-t949vbbp.js → acp-adapter-fqzkxyra.js} +3 -3
  2. package/dist/{actions-7e9czc7s.js → actions-kxek041g.js} +18 -18
  3. package/dist/{amp-adapter-gv6cbb7d.js → amp-adapter-ce7jjke4.js} +1 -1
  4. package/dist/{app-b9fbkp9v.js → app-v6zz8yh0.js} +3 -3
  5. package/dist/{assistant-wjb7v4c7.js → assistant-m0sw9xx2.js} +19 -19
  6. package/dist/{boot-args-schema-2t37e0fe.js → boot-args-schema-66bk37wv.js} +1 -1
  7. package/dist/{boot-reembed-gka3zas7.js → boot-reembed-edjpwsc1.js} +1 -1
  8. package/dist/{boot-reembed-a35nkyr1.js → boot-reembed-y27n09tz.js} +1 -1
  9. package/dist/{boot-scrub-sweep-65sdnp6c.js → boot-scrub-sweep-1xc35965.js} +3 -3
  10. package/dist/{boot-seeds-x42tzytw.js → boot-seeds-7hjj91qj.js} +4 -4
  11. package/dist/{boxd-qbkxy6ap.js → boxd-ent8ch04.js} +1 -1
  12. package/dist/{claude-adapter-v221b2fd.js → claude-adapter-stw7jdcf.js} +3 -3
  13. package/dist/{claude-managed-adapter-29eq09wd.js → claude-managed-adapter-jh6c523j.js} +2 -2
  14. package/dist/{claude-managed-setup-9m6450zk.js → claude-managed-setup-y6z2perp.js} +5 -5
  15. package/dist/{claude-sdk-session-0q0ptaba.js → claude-sdk-session-vvya8wqk.js} +3 -3
  16. package/dist/{cli-wk18cm92.js → cli-0cq8y6m7.js} +1 -1
  17. package/dist/{cli-p1w18yhx.js → cli-1pz0330a.js} +1 -1
  18. package/dist/{cli-y97qxcfx.js → cli-1qkz151v.js} +3 -3
  19. package/dist/{cli-qwca7s44.js → cli-26ctbcdp.js} +18 -6
  20. package/dist/{cli-8fknbnhw.js → cli-2kvhc7x8.js} +7 -7
  21. package/dist/{cli-rfywm85n.js → cli-3e875j5p.js} +136 -49
  22. package/dist/{cli-6wxjceza.js → cli-3sy2xags.js} +10 -10
  23. package/dist/{cli-a7wvp2km.js → cli-3vmb0n58.js} +1 -1
  24. package/dist/{cli-p1bq50aa.js → cli-4vzgeta4.js} +13 -13
  25. package/dist/{cli-m28atx8q.js → cli-5y340nzn.js} +4 -4
  26. package/dist/{cli-7p1kvkck.js → cli-6rc5frdm.js} +1 -1
  27. package/dist/{cli-tph5wt3y.js → cli-6wj7p986.js} +1 -1
  28. package/dist/{cli-t73cas91.js → cli-7349va39.js} +8100 -7620
  29. package/dist/{cli-ee0b971j.js → cli-837vfs2x.js} +2 -2
  30. package/dist/{cli-abejgen1.js → cli-959kp5h2.js} +12 -11
  31. package/dist/{cli-8kzasymh.js → cli-97ae65rz.js} +4 -4
  32. package/dist/{cli-m4dqmnbp.js → cli-9mb2q6pb.js} +1 -1
  33. package/dist/{cli-p2gfst4m.js → cli-a0z912j7.js} +123 -35
  34. package/dist/{cli-4txa0ter.js → cli-b98a65ed.js} +3 -3
  35. package/dist/{cli-k0wwfqbg.js → cli-dns0y538.js} +4 -0
  36. package/dist/{cli-tk7cjj8h.js → cli-dzpc6en0.js} +1 -1
  37. package/dist/{cli-qa4wdgsy.js → cli-e1p3n5gq.js} +4 -4
  38. package/dist/{cli-gxkx0zzp.js → cli-f4e8r23x.js} +1 -1
  39. package/dist/{cli-qmqk2yc2.js → cli-f4r3g076.js} +1 -1
  40. package/dist/{cli-wcg8x116.js → cli-f6bahpn5.js} +1 -1
  41. package/dist/{cli-qsnnd04k.js → cli-fpjmegdb.js} +1 -1
  42. package/dist/{cli-j790gp2w.js → cli-fwy8kfd6.js} +271 -37
  43. package/dist/{cli-rmmpt0wm.js → cli-g5mtmf6t.js} +6 -6
  44. package/dist/{cli-rzsy6nc6.js → cli-gyt3zydq.js} +3 -3
  45. package/dist/{cli-pasjcnmj.js → cli-j24zbska.js} +3 -3
  46. package/dist/{cli-qfedvmcr.js → cli-jdknh18m.js} +10 -10
  47. package/dist/{cli-7b8sbmag.js → cli-kjtwrhea.js} +1 -1
  48. package/dist/{cli-gpwhbgx0.js → cli-m9s2qff2.js} +57 -25
  49. package/dist/{cli-2wnq8jkz.js → cli-mxn1qvbn.js} +3 -2
  50. package/dist/{cli-s7t6p9my.js → cli-n6vtrcht.js} +1 -1
  51. package/dist/{cli-2zp0et3v.js → cli-pchypa1s.js} +55 -19
  52. package/dist/{cli-yq3zak69.js → cli-pfs8adn4.js} +1 -1
  53. package/dist/{cli-c55z5chy.js → cli-q24n86d2.js} +2 -2
  54. package/dist/{cli-h38x262p.js → cli-rcecc3yq.js} +167 -43
  55. package/dist/{cli-xae4g51r.js → cli-rh15cakt.js} +1 -1
  56. package/dist/{cli-sdtt3men.js → cli-rmzsadkf.js} +1 -1
  57. package/dist/{cli-f3vq58d2.js → cli-t2tma8gb.js} +1 -1
  58. package/dist/{cli-m23zjcy2.js → cli-tekytdq9.js} +2 -2
  59. package/dist/{cli-tqegb8eq.js → cli-vdektqgf.js} +1 -1
  60. package/dist/{cli-ffhtssbp.js → cli-vj75tyaw.js} +1 -1
  61. package/dist/{cli-deefvewm.js → cli-w401b9tq.js} +1 -1
  62. package/dist/{cli-n9ftpys4.js → cli-x6t50dmw.js} +7 -7
  63. package/dist/{cli-6t09sdhy.js → cli-xf3qw745.js} +3 -3
  64. package/dist/{cli-hxpyc9sn.js → cli-z1w943va.js} +2 -2
  65. package/dist/{cli-334z18y3.js → cli-zh9rsbqf.js} +3 -3
  66. package/dist/cli.js +17 -17
  67. package/dist/{codex-adapter-30j9bpzd.js → codex-adapter-zerf5qsp.js} +1 -1
  68. package/dist/{codex-hook-h3vk6xzr.js → codex-hook-30gyhzxe.js} +1 -1
  69. package/dist/{codex-session-runner-8vp57v8w.js → codex-session-runner-89s304py.js} +1 -1
  70. package/dist/{commands-bngnz9g7.js → commands-jeh1b51j.js} +3 -3
  71. package/dist/{controller-hxx8wwy1.js → controller-mctfbrse.js} +3 -3
  72. package/dist/{controllers-connect-e2b4gyxc.js → controllers-connect-nxa1ddwn.js} +19 -19
  73. package/dist/{cursor-adapter-n5nc3mfe.js → cursor-adapter-qj8vehv6.js} +1 -1
  74. package/dist/{db-bgm7vkc0.js → db-2dvay0es.js} +3 -3
  75. package/dist/{db-c6np41ca.js → db-b36qn6k3.js} +2 -2
  76. package/dist/{dispatcher-jnybck15.js → dispatcher-gndbta01.js} +17 -17
  77. package/dist/{dsh-adapter-nrhbrjz8.js → dsh-adapter-f75sz821.js} +1 -1
  78. package/dist/{e2b-jkwfhbcv.js → e2b-3gpc2n3w.js} +1 -1
  79. package/dist/{e2b-s16txnpx.js → e2b-jbm7mj93.js} +1 -1
  80. package/dist/{embedding-space-crsm2zp0.js → embedding-space-4p05vb2x.js} +6 -6
  81. package/dist/{embeddings-e7z3f27f.js → embeddings-m6768mgh.js} +1 -1
  82. package/dist/{external-channel-m77wf5z3.js → external-channel-gjv3jne3.js} +2 -2
  83. package/dist/{fx-adapter-b6w0kf56.js → fx-adapter-rpsgb1tw.js} +4 -4
  84. package/dist/{grok-adapter-4sqtebdk.js → grok-adapter-n2m3zjg1.js} +4 -4
  85. package/dist/handlers-nv8edn19.js +32 -0
  86. package/dist/{heartbeat-xgb036sx.js → heartbeat-a1tv60gt.js} +20 -20
  87. package/dist/{hook-xsk1jbha.js → hook-8h5dzcgq.js} +2 -2
  88. package/dist/{hook-5h02c9ga.js → hook-cyaps9ba.js} +4 -4
  89. package/dist/{http-txe7qhvn.js → http-t15mhbd9.js} +1733 -79
  90. package/dist/{index-0eny15vj.js → index-2skqtd76.js} +2 -2
  91. package/dist/{index-w7em4ktq.js → index-d6wkh8ar.js} +2 -2
  92. package/dist/{index-rvccek1w.js → index-gjwaqaq8.js} +5 -5
  93. package/dist/{index-e5pfbsys.js → index-hjhfk3ra.js} +3 -3
  94. package/dist/{index-1qw6f2dc.js → index-khe3gp0k.js} +4 -4
  95. package/dist/index-ksmyd6kc.js +37 -0
  96. package/dist/{index-vk63emp7.js → index-qav6455f.js} +14 -12
  97. package/dist/index-rjnrje98.js +32 -0
  98. package/dist/index-tv3413wy.js +33 -0
  99. package/dist/{index-ysvbhg5m.js → index-w1zqf680.js} +1 -1
  100. package/dist/{keepalive-4zsrce78.js → keepalive-91s7hzrb.js} +3 -3
  101. package/dist/lead-3hmddvrm.js +70 -0
  102. package/dist/{maintenance-86jvartk.js → maintenance-7pjmy5xy.js} +1 -1
  103. package/dist/{node-je82wjet.js → node-4c5jqxr3.js} +1 -1
  104. package/dist/{oauth-refresh-sweep-j52kg9pr.js → oauth-refresh-sweep-rzqfm94j.js} +2 -2
  105. package/dist/{onboard-c2aj5p8q.js → onboard-74st7pag.js} +3 -3
  106. package/dist/{opencode-adapter-dsagkhtw.js → opencode-adapter-arn7gm8q.js} +2 -2
  107. package/dist/{otel-impl-yyy203z8.js → otel-impl-rnzey8cb.js} +1 -1
  108. package/dist/{pi-mono-adapter-546cd1cc.js → pi-mono-adapter-6mkwq64n.js} +3 -2
  109. package/dist/{pricing-refresh-1rgzqyzz.js → pricing-refresh-ha21jhk7.js} +2 -2
  110. package/dist/{rbac-roles-e70xfeyj.js → rbac-roles-59dxachk.js} +2 -2
  111. package/dist/{rbac-roles-q89qe8er.js → rbac-roles-wqhma4dr.js} +3 -3
  112. package/dist/{render-v2-k6tt107q.js → render-v2-14kpzx12.js} +3 -3
  113. package/dist/{resume-cnnrjfxa.js → resume-gsa5fb5f.js} +6 -6
  114. package/dist/{session-access-xkxkjw3n.js → session-access-skm7bnp6.js} +4 -4
  115. package/dist/{session-source-backfill-1d5pdwww.js → session-source-backfill-pbqzbncv.js} +2 -2
  116. package/dist/{setup-se8q7gkc.js → setup-5yermsem.js} +1 -1
  117. package/dist/{work-objects-1kxnbzy5.js → work-objects-xj0ck0w4.js} +20 -20
  118. package/dist/{worker-follow-up-zqm8apt4.js → worker-follow-up-911xc0b8.js} +19 -17
  119. package/dist/worker-qgg0m2gw.js +70 -0
  120. package/openapi.json +1741 -290
  121. package/package.json +3 -3
  122. package/src/apps/catalog.generated.json +120 -1
  123. package/src/apps/definition.ts +38 -11
  124. package/src/apps/page-validator.ts +88 -0
  125. package/src/be/db/model-profiles.ts +110 -11
  126. package/src/be/db/tasks/read.ts +20 -0
  127. package/src/be/db/workflow-run-definitions.ts +19 -8
  128. package/src/be/db-client.ts +43 -5
  129. package/src/be/db.ts +9 -3
  130. package/src/be/migrations/284_model_profile_fallbacks.sql +9 -0
  131. package/src/be/model-profile-check.ts +43 -18
  132. package/src/be/runtime-pools/routing.ts +1 -0
  133. package/src/be/savings/calibrate.ts +468 -0
  134. package/src/be/savings/compute.ts +182 -0
  135. package/src/be/savings/model.ts +406 -0
  136. package/src/be/scripts/typecheck.ts +5 -0
  137. package/src/be/seed/agent-fs-provision.ts +48 -11
  138. package/src/be/seed/workflows-seeder.ts +67 -23
  139. package/src/be/skill-parser.ts +19 -7
  140. package/src/commands/onboard/agent-fs-version.ts +1 -1
  141. package/src/commands/provider-error-class.ts +97 -0
  142. package/src/commands/runner.ts +99 -17
  143. package/src/extensions/contract-types.generated.ts +1 -1
  144. package/src/http/all-routes.ts +2 -0
  145. package/src/http/fs.ts +8 -2
  146. package/src/http/index.ts +4 -0
  147. package/src/http/model-profiles.ts +43 -10
  148. package/src/http/poll.ts +1 -0
  149. package/src/http/savings-calibration.ts +277 -0
  150. package/src/http/savings.ts +363 -0
  151. package/src/http/tasks.ts +46 -4
  152. package/src/http/trackers/authorize-gate.ts +41 -0
  153. package/src/http/trackers/jira.ts +26 -4
  154. package/src/http/trackers/linear.ts +26 -4
  155. package/src/integrations/chat-sdk/limits.ts +6 -1
  156. package/src/jira/oauth.ts +7 -1
  157. package/src/linear/README.md +2 -1
  158. package/src/linear/oauth.ts +7 -1
  159. package/src/providers/amp-adapter.ts +1 -1
  160. package/src/providers/codex-adapter.ts +3 -0
  161. package/src/providers/pi-mono-adapter.ts +6 -5
  162. package/src/providers/types.ts +7 -0
  163. package/src/scripts-runtime/eval-harness.ts +14 -1
  164. package/src/scripts-runtime/executors/types.ts +3 -0
  165. package/src/scripts-runtime/sdk-allowlist.ts +11 -1
  166. package/src/scripts-runtime/swarm-sdk.ts +6 -0
  167. package/src/scripts-runtime/types/stdlib.d.ts +9 -0
  168. package/src/scripts-runtime/types/swarm-extension.d.ts +34 -0
  169. package/src/scripts-runtime/types/swarm-sdk.d.ts +9 -0
  170. package/src/tasks/worker-follow-up.ts +142 -3
  171. package/src/tests/agent-fs-provision-seeder.test.ts +81 -0
  172. package/src/tests/apps-chart-shell.test.ts +170 -0
  173. package/src/tests/apps-icon.test.ts +156 -2
  174. package/src/tests/chat-sdk-relay.test.ts +12 -0
  175. package/src/tests/codex-adapter.test.ts +11 -0
  176. package/src/tests/comb-default.test.ts +6 -0
  177. package/src/tests/core-auth.test.ts +7 -6
  178. package/src/tests/db-client-busy-retry.test.ts +58 -6
  179. package/src/tests/deploy-guard.test.ts +154 -0
  180. package/src/tests/docs-index-footprint.test.js +3 -3
  181. package/src/tests/fixtures/app-shell-charts-definition.json.txt +180 -0
  182. package/src/tests/mcp-tools.test.ts +1 -1
  183. package/src/tests/model-profile-fallbacks.test.ts +482 -0
  184. package/src/tests/model-profiles-api.test.ts +1 -0
  185. package/src/tests/pi-mono-session-cost-on-abort.test.ts +115 -0
  186. package/src/tests/provider-error-class.test.ts +174 -0
  187. package/src/tests/runner-provider-fallback.test.ts +197 -0
  188. package/src/tests/savings-calibration-api.test.ts +264 -0
  189. package/src/tests/savings-calibration.test.ts +201 -0
  190. package/src/tests/savings-compute.test.ts +406 -0
  191. package/src/tests/savings-model.test.ts +287 -0
  192. package/src/tests/script-connections.test.ts +2 -0
  193. package/src/tests/script-native-unawaited-drain.test.ts +167 -0
  194. package/src/tests/sdk-allowlist.test.ts +40 -0
  195. package/src/tests/seed-workflows.test.ts +167 -11
  196. package/src/tests/skill-parser.test.ts +45 -0
  197. package/src/tests/tracker-oauth-authorize-auth.test.ts +262 -0
  198. package/src/tests/workflow-definition-pinning.test.ts +128 -3
  199. package/src/tests/workflow-script-principal.test.ts +94 -4
  200. package/src/tools/app-upsert.ts +1 -1
  201. package/src/tools/model-profiles/list-model-profiles.ts +4 -2
  202. package/src/tools/script-run.ts +1 -1
  203. package/src/tools/update-profile.ts +1 -1
  204. package/src/types.ts +115 -33
  205. package/src/utils/constants.ts +2 -0
  206. package/templates/skills/apps/SKILL.md +62 -3
  207. package/templates/skills/apps/content.md +62 -3
  208. package/templates/skills/swarm-docs/content.md +2 -0
  209. package/templates/skills/swarm-scripts/SKILL.md +1 -1
  210. package/templates/skills/swarm-scripts/content.md +1 -1
  211. package/dist/handlers-kdjcyzdj.js +0 -32
  212. package/dist/index-835xdgnm.js +0 -33
  213. package/dist/index-d469300x.js +0 -37
  214. package/dist/index-rzqwp11g.js +0 -32
  215. package/dist/lead-yqt47dkr.js +0 -70
  216. package/dist/worker-j6xxtq7p.js +0 -70
@@ -0,0 +1,287 @@
1
+ import { describe, expect, test } from "bun:test";
2
+ import {
3
+ CURVE,
4
+ curve,
5
+ DEFAULT_OPS,
6
+ F_FOLLOWUP,
7
+ jobKey,
8
+ nodeOpsOf,
9
+ parseCalibration,
10
+ priceWorkflow,
11
+ SAVINGS_TIERS,
12
+ splitByDay,
13
+ type WorkflowFacts,
14
+ } from "../be/savings/model";
15
+
16
+ const facts = (over: Partial<WorkflowFacts> = {}): WorkflowFacts => ({
17
+ runs: 1,
18
+ countedRuns: 1,
19
+ schedRuns: 0,
20
+ schedEffectRuns: 0,
21
+ opsSum: 0,
22
+ agentTasks: 0,
23
+ agentUsd: 0,
24
+ agentMs: 0,
25
+ followupUsd: 0,
26
+ recordedLlmUsd: 0,
27
+ estimatedLlmUsd: 0,
28
+ computeSecs: 0,
29
+ ...over,
30
+ });
31
+
32
+ const perRun = (ops: number) =>
33
+ SAVINGS_TIERS.map(
34
+ (tier) =>
35
+ priceWorkflow(facts({ opsSum: ops }), { tier, days: 30, band: "job" }).agentOnlyUsdPerRun,
36
+ );
37
+
38
+ // The report rounds its cells by hand (ultra pr-fix-loop: 3.03 vs 3.044), so allow 1.5 cents.
39
+ function expectReportRow(ops: number, row: number[]) {
40
+ perRun(ops).forEach((value, i) => {
41
+ expect(Math.abs(value - (row[i] as number))).toBeLessThan(0.015);
42
+ });
43
+ }
44
+
45
+ describe("savings model", () => {
46
+ test("agent-only cost of one run matches the cost report table", () => {
47
+ // Report table "Agent-only cost of one run", smol/regular/smart/ultra.
48
+ expectReportRow(11, [0.12, 0.26, 0.41, 0.82]); // claude-code-changelog-watch
49
+ expectReportRow(100, [0.15, 0.78, 1.46, 3.03]); // pr-fix-loop
50
+ });
51
+
52
+ test("every tier curve has one point per turn up to TAU_MAX", () => {
53
+ for (const tier of SAVINGS_TIERS) {
54
+ expect(CURVE[tier].usd).toHaveLength(61);
55
+ expect(CURVE[tier].hrs).toHaveLength(61);
56
+ }
57
+ // Interpolates and clamps.
58
+ expect(curve(1.5, "smart", "usd")).toBeCloseTo((0.1746 + 0.2138) / 2, 6);
59
+ expect(curve(0, "smart", "usd")).toBe(0.1746);
60
+ expect(curve(500, "smart", "usd")).toBe(2.8086);
61
+ });
62
+
63
+ test("schedule runs are capped at 24 a day, but effect runs always count", () => {
64
+ const priced = priceWorkflow(
65
+ facts({ runs: 1100, countedRuns: 1050, schedRuns: 1000, schedEffectRuns: 800, opsSum: 0 }),
66
+ { tier: "smart", days: 30, band: "job" },
67
+ );
68
+ // max(min(1000, 720), 800) + (1050 - 1000) non-schedule runs
69
+ expect(priced.substitutedRuns).toBe(850);
70
+ expect(priced.effectRuns).toBe(80);
71
+ expect(priced.enabledOnlyRuns).toBe(250);
72
+ expect(priced.capApplied).toBe(true);
73
+
74
+ const under = priceWorkflow(facts({ runs: 10, countedRuns: 10, schedRuns: 10 }), {
75
+ tier: "smart",
76
+ days: 30,
77
+ band: "job",
78
+ });
79
+ expect(under.substitutedRuns).toBe(10);
80
+ expect(under.capApplied).toBe(false);
81
+ });
82
+
83
+ test("the band is wider for one job than for a fleet sum, and p50 is the point", () => {
84
+ const f = facts({ runs: 100, countedRuns: 100, opsSum: 100 * 40 });
85
+ const job = priceWorkflow(f, { tier: "smart", days: 30, band: "job" });
86
+ const fleet = priceWorkflow(f, { tier: "smart", days: 30, band: "fleet" });
87
+ expect(job.savingUsd.p50).toBeCloseTo(fleet.savingUsd.p50, 9);
88
+ expect(job.savingUsd.p50).toBeCloseTo(100 * job.agentOnlyUsdPerRun, 9);
89
+ expect(job.savingUsd.p10).toBeLessThan(fleet.savingUsd.p10);
90
+ expect(job.savingUsd.p90).toBeGreaterThan(fleet.savingUsd.p90);
91
+ expect(job.hoursSaved.p10).toBeLessThan(job.hoursSaved.p50);
92
+ });
93
+
94
+ test("kept agent spend appears on both sides; actual spend is per substituted run", () => {
95
+ const priced = priceWorkflow(
96
+ facts({
97
+ runs: 4,
98
+ countedRuns: 4,
99
+ opsSum: 40,
100
+ agentTasks: 2,
101
+ agentUsd: 8,
102
+ followupUsd: 2,
103
+ estimatedLlmUsd: 1,
104
+ agentMs: 7_200_000,
105
+ computeSecs: 3600,
106
+ }),
107
+ { tier: "smart", days: 30, band: "job" },
108
+ );
109
+ expect(priced.actualUsd).toBeCloseTo(11, 9);
110
+ // The $1 LLM estimate is not measured spend.
111
+ expect(priced.measuredUsd).toBeCloseTo(10, 9);
112
+ expect(priced.estimatedLlmUsd).toBeCloseTo(1, 9);
113
+ expect(priced.agentOnlyUsd.p50 - 10).toBeCloseTo(4 * priced.agentOnlyUsdPerRun, 9);
114
+ expect(priced.actualHours).toBeCloseTo(3, 9);
115
+ });
116
+
117
+ test("measured spend is reported unscaled; the scaled estimate keeps the saving", () => {
118
+ // One $100 manual run and 999 skipped runs on one day.
119
+ const window = facts({ runs: 1000, countedRuns: 1, opsSum: 10, agentTasks: 1, agentUsd: 100 });
120
+ const priced = priceWorkflow(window, { tier: "smart", days: 30, band: "job" });
121
+ expect(priced.substitutedRuns).toBe(1);
122
+ expect(priced.measuredUsd).toBe(100);
123
+ // The estimator's actual side is the scaled estimate, not the $100 measured.
124
+ expect(priced.actualUsd).toBeCloseTo(0.1, 9);
125
+ // Kept agent spend cancels: the saving is one substituted run at the unit cost.
126
+ expect(priced.savingUsd.p50).toBeCloseTo(priced.agentOnlyUsdPerRun, 9);
127
+ const [point] = splitByDay(window, [{ day: "2026-10-01", ...window }], {
128
+ tier: "smart",
129
+ days: 30,
130
+ });
131
+ expect(point?.measuredUsd).toBe(100);
132
+ expect(point?.actualUsd).toBeCloseTo(0.1, 9);
133
+ expect(point?.savingUsd).toBeCloseTo(priced.savingUsd.p50, 9);
134
+ });
135
+
136
+ test("estimated LLM spend is reported apart from measured spend", () => {
137
+ // No recorded LLM cost: $5 is estimated from tokens and output size.
138
+ const window = facts({ runs: 1, countedRuns: 1, opsSum: 10, estimatedLlmUsd: 5 });
139
+ const priced = priceWorkflow(window, { tier: "smart", days: 30, band: "job" });
140
+ expect(priced.measuredUsd).toBe(0);
141
+ expect(priced.estimatedLlmUsd).toBe(5);
142
+ // The saving still subtracts the estimate.
143
+ expect(priced.actualUsd).toBe(5);
144
+ expect(priced.savingUsd.p50).toBeCloseTo(priced.agentOnlyUsdPerRun - 5, 9);
145
+ const [point] = splitByDay(window, [{ day: "2026-10-01", ...window }], {
146
+ tier: "smart",
147
+ days: 30,
148
+ });
149
+ expect(point?.measuredUsd).toBe(0);
150
+ expect(point?.estimatedLlmUsd).toBe(5);
151
+ expect(point?.actualUsd).toBe(5);
152
+ expect(point?.savingUsd).toBeCloseTo(priced.savingUsd.p50, 9);
153
+ });
154
+
155
+ test("recorded LLM cost replaces the token estimate", () => {
156
+ const priced = priceWorkflow(facts({ recordedLlmUsd: 2, estimatedLlmUsd: 5 }), {
157
+ tier: "smart",
158
+ days: 30,
159
+ band: "job",
160
+ });
161
+ expect(priced.actualUsd).toBe(2);
162
+ expect(priced.measuredUsd).toBe(2);
163
+ expect(priced.estimatedLlmUsd).toBe(0);
164
+ });
165
+
166
+ test("a pure agent dispatcher substitutes nothing and pays no review", () => {
167
+ const priced = priceWorkflow(facts({ runs: 2, countedRuns: 2, agentTasks: 2, agentUsd: 4 }), {
168
+ tier: "smart",
169
+ days: 30,
170
+ band: "job",
171
+ });
172
+ expect(priced.agentOnlyUsdPerRun).toBe(0);
173
+ expect(priced.savingUsd.p50).toBe(0);
174
+ });
175
+
176
+ test("node ops and job keys", () => {
177
+ const ops = nodeOpsOf(
178
+ {
179
+ nodes: [
180
+ { id: "s", type: "script", config: { script: "await a(); await b()" } },
181
+ { id: "bare", type: "script", config: { script: "echo hi" } },
182
+ { id: "known", type: "swarm-script", config: { scriptName: "known" } },
183
+ { id: "unknown", type: "swarm-script", config: { scriptName: "nope" } },
184
+ { id: "n", type: "notify" },
185
+ { id: "a", type: "agent-task" },
186
+ ],
187
+ },
188
+ new Map([["known", 42]]),
189
+ );
190
+ expect(Object.fromEntries(ops)).toEqual({
191
+ s: 2,
192
+ bare: 1,
193
+ known: 42,
194
+ unknown: DEFAULT_OPS,
195
+ n: 1,
196
+ a: 0,
197
+ });
198
+ expect(jobKey("pr-fix-loop-retired-2026-10-28")).toBe("pr-fix-loop");
199
+ expect(jobKey("pr-fix-loop")).toBe("pr-fix-loop");
200
+ expect(F_FOLLOWUP).toBe(0.107);
201
+ });
202
+
203
+ test("daily split sums to the window, and capped schedule runs spread pro rata", () => {
204
+ // 2 days of polling every 5 minutes (288 a day) against a 1-day cap of 24,
205
+ // plus one manual run with an agent task and a recorded LLM cost.
206
+ const day1 = facts({
207
+ runs: 288,
208
+ countedRuns: 288,
209
+ schedRuns: 288,
210
+ schedEffectRuns: 3,
211
+ opsSum: 288 * 5,
212
+ recordedLlmUsd: 0.4,
213
+ });
214
+ const day2 = facts({
215
+ runs: 145,
216
+ countedRuns: 145,
217
+ schedRuns: 144,
218
+ schedEffectRuns: 0,
219
+ opsSum: 145 * 5,
220
+ agentTasks: 1,
221
+ agentUsd: 2,
222
+ followupUsd: 0.1,
223
+ recordedLlmUsd: 0.2,
224
+ });
225
+ const window = facts({});
226
+ for (const k of Object.keys(window) as (keyof WorkflowFacts)[]) window[k] = day1[k] + day2[k];
227
+ const priced = priceWorkflow(window, { tier: "smart", days: 1, band: "job" });
228
+ expect(priced.capApplied).toBe(true);
229
+ const points = splitByDay(
230
+ window,
231
+ [
232
+ { day: "2026-10-01", ...day1 },
233
+ { day: "2026-10-02", ...day2 },
234
+ ],
235
+ { tier: "smart", days: 1 },
236
+ );
237
+ const sum = (key: "savingUsd" | "actualUsd" | "measuredUsd" | "estimatedLlmUsd") =>
238
+ points.reduce((s, p) => s + p[key], 0);
239
+ expect(sum("savingUsd")).toBeCloseTo(priced.savingUsd.p50, 9);
240
+ expect(sum("actualUsd")).toBeCloseTo(priced.actualUsd, 9);
241
+ expect(sum("measuredUsd")).toBeCloseTo(priced.measuredUsd, 9);
242
+ expect(sum("estimatedLlmUsd")).toBeCloseTo(priced.estimatedLlmUsd, 9);
243
+ // The 24 counted schedule runs split 288:144; the manual run stays on day 2.
244
+ const unit = priced.agentOnlyUsdPerRun;
245
+ const share = priced.substitutedRuns / window.runs;
246
+ expect(points[0]?.savingUsd).toBeCloseTo(16 * unit - 0.4 * share, 9);
247
+ expect(points[1]?.savingUsd).toBeCloseTo((8 + 1) * unit - 0.2 * share, 9);
248
+ });
249
+ });
250
+
251
+ describe("calibration", () => {
252
+ const stored = (over: Record<string, unknown> = {}) =>
253
+ JSON.stringify({ calibratedAt: "2026-12-01", model: "claude-opus-5-5", curve: CURVE, ...over });
254
+
255
+ test("a stored curve parses as a deployment calibration", () => {
256
+ expect(parseCalibration(stored())).toMatchObject({
257
+ source: "deployment",
258
+ calibratedAt: "2026-12-01",
259
+ model: "claude-opus-5-5",
260
+ });
261
+ });
262
+
263
+ test("a missing, malformed or wrongly shaped value is null, so callers use the shipped curve", () => {
264
+ expect(parseCalibration(null)).toBeNull();
265
+ expect(parseCalibration("not json")).toBeNull();
266
+ expect(parseCalibration(stored({ calibratedAt: "yesterday" }))).toBeNull();
267
+ const short = { ...CURVE, smart: { usd: CURVE.smart.usd.slice(1), hrs: CURVE.smart.hrs } };
268
+ expect(parseCalibration(stored({ curve: short }))).toBeNull();
269
+ const zero = { ...CURVE, smol: { usd: CURVE.smol.usd.map(() => 0), hrs: CURVE.smol.hrs } };
270
+ expect(parseCalibration(stored({ curve: zero }))).toBeNull();
271
+ });
272
+
273
+ test("priceWorkflow uses the given curve and the shipped one by default", () => {
274
+ const doubled = Object.fromEntries(
275
+ SAVINGS_TIERS.map((t) => [t, { usd: CURVE[t].usd.map((v) => v * 2), hrs: CURVE[t].hrs }]),
276
+ ) as typeof CURVE;
277
+ const f = facts({ opsSum: 10 });
278
+ const base = priceWorkflow(f, { tier: "smart", days: 30, band: "job" });
279
+ const same = priceWorkflow(f, { tier: "smart", days: 30, band: "job", curve: CURVE });
280
+ const twice = priceWorkflow(f, { tier: "smart", days: 30, band: "job", curve: doubled });
281
+ expect(same.agentOnlyUsdPerRun).toBe(base.agentOnlyUsdPerRun);
282
+ expect(twice.agentOnlyUsdPerRun - F_FOLLOWUP).toBeCloseTo(
283
+ 2 * (base.agentOnlyUsdPerRun - F_FOLLOWUP),
284
+ 6,
285
+ );
286
+ });
287
+ });
@@ -691,6 +691,8 @@ describe("script connections", () => {
691
691
  markMigrationApplied(database, "281_model_profiles_pr2.sql");
692
692
  // 283 alters workflow_runs, which this fixture does not create.
693
693
  markMigrationApplied(database, "283_workflow_run_definitions.sql");
694
+ // 284 alters model_profiles (made by the skipped 220) and agent_tasks.
695
+ markMigrationApplied(database, "284_model_profile_fallbacks.sql");
694
696
 
695
697
  const id = crypto.randomUUID();
696
698
  const now = new Date().toISOString();
@@ -0,0 +1,167 @@
1
+ import { afterAll, beforeAll, beforeEach, describe, expect, test } from "bun:test";
2
+ import { NativeScriptExecutor } from "../scripts-runtime/executors/native";
3
+ import {
4
+ DEFAULT_SCRIPT_RESOURCES,
5
+ type ExecutorInput,
6
+ UNAWAITED_WORK_DRAIN_MS,
7
+ } from "../scripts-runtime/executors/types";
8
+ import { SKIP_SANDBOX_SPAWN_TESTS } from "./sandbox-spawn-test-helpers";
9
+
10
+ // A native run used to stay open until every call the script started had
11
+ // settled, even after its default export resolved. A parent that raced a slow
12
+ // nested `ctx.swarm.script_run` against a 100 ms deadline therefore still ran
13
+ // to the 30 s wall, was classified `timeout`, and lost its result. These tests
14
+ // drive the real swarm-sdk against a stand-in API: the nested script_run
15
+ // request is held open for 40 s, like a child that sleeps 40 s.
16
+
17
+ const CHILD_HOLD_MS = 40_000;
18
+ // The production wall. Keeping it here makes the old behaviour fail this file
19
+ // the same way the measured runs failed: at 30 s, with error "timeout".
20
+ const WALL_CLOCK_MS = DEFAULT_SCRIPT_RESOURCES.wallClockMs;
21
+ const TEST_BUDGET_MS = WALL_CLOCK_MS + 15_000;
22
+
23
+ let server: ReturnType<typeof Bun.serve>;
24
+ const held = new Set<() => void>();
25
+ let scriptRunRequests = 0;
26
+ let kvWrites: Array<{ path: string; body: unknown }> = [];
27
+
28
+ beforeAll(() => {
29
+ server = Bun.serve({
30
+ port: 0,
31
+ async fetch(req) {
32
+ const url = new URL(req.url);
33
+ if (req.method === "POST" && url.pathname === "/api/scripts/run") {
34
+ scriptRunRequests++;
35
+ await new Promise<void>((resolve) => {
36
+ const release = () => {
37
+ clearTimeout(timer);
38
+ held.delete(release);
39
+ resolve();
40
+ };
41
+ const timer = setTimeout(release, CHILD_HOLD_MS);
42
+ held.add(release);
43
+ });
44
+ return Response.json({ result: "child-finished" });
45
+ }
46
+ if (req.method === "PUT" && url.pathname.startsWith("/api/kv/")) {
47
+ const body = await req.json();
48
+ // Answer slowly so the write is still in flight when the script returns.
49
+ await Bun.sleep(300);
50
+ kvWrites.push({ path: url.pathname, body });
51
+ return Response.json({ ok: true });
52
+ }
53
+ return new Response("not found", { status: 404 });
54
+ },
55
+ });
56
+ });
57
+
58
+ afterAll(() => {
59
+ for (const release of [...held]) release();
60
+ server.stop(true);
61
+ });
62
+
63
+ beforeEach(() => {
64
+ scriptRunRequests = 0;
65
+ kvWrites = [];
66
+ });
67
+
68
+ function input(source: string): ExecutorInput {
69
+ return {
70
+ source,
71
+ args: {},
72
+ configPayload: {
73
+ system: {
74
+ apiKey: { value: "drain-test-secret", isSecret: true },
75
+ agentId: { value: "00000000-0000-4000-8000-000000000001", isSecret: false },
76
+ mcpBaseUrl: { value: `http://127.0.0.1:${server.port}`, isSecret: false },
77
+ },
78
+ user: {},
79
+ },
80
+ resources: { ...DEFAULT_SCRIPT_RESOURCES, memoryMb: 2048, wallClockMs: WALL_CLOCK_MS },
81
+ fsMode: "none",
82
+ network: "open",
83
+ };
84
+ }
85
+
86
+ const spawnTest = test.skipIf(SKIP_SANDBOX_SPAWN_TESTS);
87
+
88
+ describe("native run settles when the entry function resolves", () => {
89
+ spawnTest(
90
+ "a Promise.race deadline bounds a run whose nested script_run is still pending",
91
+ async () => {
92
+ const output = await new NativeScriptExecutor().run(
93
+ input(`
94
+ export default async (_args, ctx) => {
95
+ const child = ctx.swarm
96
+ .script_run({ source: "export default async () => { await Bun.sleep(40000); return 1; }", args: {} })
97
+ .then(() => "child");
98
+ const deadline = new Promise((resolve) => setTimeout(() => resolve("deadline"), 100));
99
+ return { winner: await Promise.race([child, deadline]) };
100
+ };
101
+ `),
102
+ );
103
+
104
+ expect(scriptRunRequests).toBe(1);
105
+ expect(output.error).toBeUndefined();
106
+ expect(output.exitCode).toBe(0);
107
+ expect(output.result).toEqual({ winner: "deadline" });
108
+ expect(output.durationMs).toBeLessThan(5_000);
109
+ expect(output.stderr).toContain("un-awaited calls were still pending");
110
+ },
111
+ TEST_BUDGET_MS,
112
+ );
113
+
114
+ spawnTest(
115
+ "an un-awaited kv_set still lands, and a run with nothing pending exits without waiting",
116
+ async () => {
117
+ const output = await new NativeScriptExecutor().run(
118
+ input(`
119
+ export default async (_args, ctx) => {
120
+ // Fired without await: the request is in flight when the script returns.
121
+ void ctx.swarm.kv_set({ key: "fired-at-return", value: "a" });
122
+ // Started later, still inside the drain window.
123
+ void (async () => {
124
+ await Bun.sleep(500);
125
+ await ctx.swarm.kv_set({ key: "fired-after-500ms", value: "b" });
126
+ })();
127
+ return "returned";
128
+ };
129
+ `),
130
+ );
131
+
132
+ expect(output.error).toBeUndefined();
133
+ expect(output.result).toBe("returned");
134
+ expect(kvWrites.map((w) => w.path).sort()).toEqual([
135
+ "/api/kv/fired-after-500ms",
136
+ "/api/kv/fired-at-return",
137
+ ]);
138
+ // Both writes settled on their own, so the drain timer never fired.
139
+ expect(output.stderr).not.toContain("un-awaited calls were still pending");
140
+ expect(output.durationMs).toBeLessThan(UNAWAITED_WORK_DRAIN_MS + 2_000);
141
+ },
142
+ TEST_BUDGET_MS,
143
+ );
144
+
145
+ spawnTest(
146
+ "an un-awaited call that starts after the drain window is stopped and reported",
147
+ async () => {
148
+ const output = await new NativeScriptExecutor().run(
149
+ input(`
150
+ export default async (_args, ctx) => {
151
+ void (async () => {
152
+ await Bun.sleep(${UNAWAITED_WORK_DRAIN_MS + 1_500});
153
+ await ctx.swarm.kv_set({ key: "too-late", value: "c" });
154
+ })();
155
+ return "returned";
156
+ };
157
+ `),
158
+ );
159
+
160
+ expect(output.error).toBeUndefined();
161
+ expect(output.result).toBe("returned");
162
+ expect(kvWrites).toEqual([]);
163
+ expect(output.stderr).toContain("un-awaited calls were still pending");
164
+ },
165
+ TEST_BUDGET_MS,
166
+ );
167
+ });
@@ -65,6 +65,46 @@ describe("script SDK allowlist", () => {
65
65
  );
66
66
  });
67
67
 
68
+ test("savings calibration is REST-only: reachable from scripts, not an MCP tool or bridge entry", async () => {
69
+ const requests: { method: string; path: string; body: unknown }[] = [];
70
+ const httpServer = Bun.serve({
71
+ port: 0,
72
+ async fetch(req) {
73
+ requests.push({
74
+ method: req.method,
75
+ path: new URL(req.url).pathname,
76
+ body: req.method === "POST" ? await req.json() : undefined,
77
+ });
78
+ return Response.json({ ok: true });
79
+ },
80
+ });
81
+ const config = new SwarmConfig({
82
+ system: {
83
+ apiKey: { value: "sdk-test-key", isSecret: true },
84
+ agentId: { value: "sdk-test-agent", isSecret: false },
85
+ mcpBaseUrl: { value: `http://127.0.0.1:${httpServer.port}`, isSecret: false },
86
+ },
87
+ user: {},
88
+ });
89
+ const sdk = createSwarmSdk(config);
90
+ try {
91
+ await sdk.savings_calibrationGet();
92
+ await sdk.savings_calibrationRun({ days: 7, apply: true });
93
+ expect(requests).toEqual([
94
+ { method: "GET", path: "/api/savings/calibration", body: undefined },
95
+ { method: "POST", path: "/api/savings/calibration", body: { days: 7, apply: true } },
96
+ ]);
97
+ } finally {
98
+ httpServer.stop(true);
99
+ }
100
+ // Neither an MCP tool nor an allowlist entry, so the workflow capability list and the bridge skip them.
101
+ for (const name of ["savings_calibrationGet", "savings_calibrationRun"]) {
102
+ expect((SDK_ALLOWLIST as readonly string[]).includes(name)).toBe(false);
103
+ expect(isMcpToolAllowedForScripts(name)).toBe(false);
104
+ expect(name in registeredTools).toBe(false);
105
+ }
106
+ });
107
+
68
108
  test("workflow_listRuns sends bounded pagination defaults and rejects an excessive limit", async () => {
69
109
  let requestUrl: URL | undefined;
70
110
  const httpServer = Bun.serve({