@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.
- package/dist/acp-adapter-xq6s2z6t.js +14 -0
- package/dist/{actions-pwjpn0j6.js → actions-ewz7bp4g.js} +20 -19
- package/dist/{amp-adapter-cq9jgj6x.js → amp-adapter-f25tqhnf.js} +3 -3
- package/dist/{app-cnfffgwg.js → app-mkptw0k7.js} +4 -4
- package/dist/{assistant-kpn317y1.js → assistant-c47rfe77.js} +23 -22
- package/dist/{boot-args-schema-pbbnhmc3.js → boot-args-schema-4vb0xsat.js} +1 -1
- package/dist/{boot-reembed-9m0ysxma.js → boot-reembed-1ghy2sab.js} +1 -1
- package/dist/{boot-reembed-majbn53m.js → boot-reembed-htr73hbs.js} +1 -1
- package/dist/{boot-scrub-sweep-app8g8ym.js → boot-scrub-sweep-yrytg42z.js} +4 -4
- package/dist/{boot-seeds-pbtmvcjm.js → boot-seeds-n2b3dx4e.js} +4 -4
- package/dist/{boxd-qbkxy6ap.js → boxd-jmfqgkqa.js} +2 -2
- package/dist/{claude-adapter-wn1g3qp5.js → claude-adapter-r33pqnzx.js} +8 -9
- package/dist/{claude-managed-adapter-jh6c523j.js → claude-managed-adapter-hddmxjyj.js} +4 -4
- package/dist/{claude-managed-setup-pd47nzch.js → claude-managed-setup-hfvry1bd.js} +5 -5
- package/dist/{claude-sdk-session-3a2a6589.js → claude-sdk-session-0hamqzc7.js} +10 -11
- package/dist/{cli-v1dv1r37.js → cli-0bcgyj5b.js} +74 -3
- package/dist/{cli-a3b239vq.js → cli-0vksaxvr.js} +2 -2
- package/dist/{cli-mrt48bx7.js → cli-15204js0.js} +1 -1
- package/dist/{cli-8aes17e4.js → cli-1m96dc36.js} +3 -3
- package/dist/{cli-2r0jcc3k.js → cli-2d0cghfn.js} +1 -1
- package/dist/{cli-wk18cm92.js → cli-4ax9mvvg.js} +4 -4
- package/dist/{cli-9wngrawf.js → cli-4dpkjc2e.js} +6 -6
- package/dist/{cli-x1wa213s.js → cli-4e1hfbpq.js} +5 -5
- package/dist/{cli-7349va39.js → cli-500c09j9.js} +921 -431
- package/dist/{cli-m4dqmnbp.js → cli-54qr8mcd.js} +2 -2
- package/dist/{cli-m88rpzkh.js → cli-5a7szzfh.js} +2 -2
- package/dist/{cli-bst1vp7e.js → cli-5y2fvw4z.js} +13 -5
- package/dist/{cli-vj75tyaw.js → cli-6yygkr0w.js} +7 -7
- package/dist/{cli-7b8sbmag.js → cli-7c5tgqhf.js} +5 -5
- package/dist/{cli-2gxeg5gr.js → cli-8476gtwf.js} +1 -1
- package/dist/{cli-yncmerje.js → cli-922z7gwm.js} +4 -4
- package/dist/{cli-jrzkw01m.js → cli-93t5s4zq.js} +2 -2
- package/dist/{cli-sn7rtnj4.js → cli-9zmb7ptc.js} +9 -9
- package/dist/{cli-tk7cjj8h.js → cli-am1ptset.js} +3 -3
- package/dist/{cli-cyhbbd1z.js → cli-awwptga4.js} +1 -1
- package/dist/{cli-89cqnasv.js → cli-bhsfbmk8.js} +19 -7
- package/dist/{cli-9vhebtjz.js → cli-bj6qhgte.js} +15 -32
- package/dist/{cli-g70vxtjh.js → cli-c0xjnr8z.js} +2 -2
- package/dist/{cli-vqptjbr7.js → cli-c3ng0mb4.js} +14 -14
- package/dist/{cli-re4raaj0.js → cli-c71n3bxq.js} +11 -6
- package/dist/{cli-7p1kvkck.js → cli-crpf163n.js} +25 -18
- package/dist/{cli-k0wwfqbg.js → cli-dns0y538.js} +4 -0
- package/dist/{cli-d3p5vwbk.js → cli-dpsfwh7n.js} +13 -21
- package/dist/{cli-xzm9vcfg.js → cli-dtdht77h.js} +2 -2
- package/dist/{cli-bb431xey.js → cli-e2w34h6z.js} +231 -68
- package/dist/{cli-eddnyg3t.js → cli-emekyyhr.js} +1 -1
- package/dist/{cli-d84byzn8.js → cli-f1kwb5fc.js} +109 -33
- package/dist/{cli-0jzra9ad.js → cli-gn9y6fn7.js} +2 -2
- package/dist/{cli-tqegb8eq.js → cli-gpz1d0t2.js} +4 -4
- package/dist/{cli-7w69ne2f.js → cli-h9yy18gy.js} +239 -67
- package/dist/{cli-m5akxq8q.js → cli-j8cjdzan.js} +1 -1
- package/dist/{cli-m9s2qff2.js → cli-jg5txpm6.js} +187 -93
- package/dist/{cli-k26bkr9t.js → cli-k2rw11py.js} +3 -3
- package/dist/{cli-93k32c37.js → cli-k96cecs7.js} +377 -61
- package/dist/{cli-3vjhs18n.js → cli-kd56bmk4.js} +1 -1
- package/dist/{cli-k0615eh8.js → cli-kmemts3w.js} +13 -13
- package/dist/{cli-pcad4cb1.js → cli-knva3nmx.js} +2 -2
- package/dist/{cli-b085njxz.js → cli-kw44j53p.js} +15 -14
- package/dist/{cli-bsywb58a.js → cli-mdpbjnzq.js} +10 -10
- package/dist/{cli-syybb0p1.js → cli-me0r8gzr.js} +35 -76
- package/dist/{cli-4r05hnqa.js → cli-mwwmka77.js} +15 -15
- package/dist/{cli-1xh9a7hn.js → cli-n5t077me.js} +2 -2
- package/dist/{cli-rvhdmjvt.js → cli-nx2n3z6p.js} +1 -1
- package/dist/{cli-28fq781p.js → cli-pe4d96rb.js} +1 -1
- package/dist/{cli-3rkdne1h.js → cli-pxwtcgcp.js} +2 -2
- package/dist/{cli-zcsw143p.js → cli-qmqt520k.js} +2 -2
- package/dist/{cli-2zp0et3v.js → cli-qps8je6j.js} +70 -19
- package/dist/{cli-xae4g51r.js → cli-rh15cakt.js} +1 -1
- package/dist/{cli-k93bq1wq.js → cli-rjvsf9qa.js} +5 -5
- package/dist/{cli-6mma83xa.js → cli-rqfvkx5t.js} +7 -7
- package/dist/{cli-dy32zecz.js → cli-s7zz734a.js} +1 -1
- package/dist/{cli-yn2bbre9.js → cli-s8p8hhty.js} +2 -2
- package/dist/{cli-vbh10fqa.js → cli-sdvcrq5x.js} +5 -5
- package/dist/{cli-b5rs97a6.js → cli-sg27rk2t.js} +5 -5
- package/dist/{cli-d6521kb5.js → cli-skmhsnqh.js} +1 -1
- package/dist/{cli-5mrq3ake.js → cli-t4dv07kr.js} +1 -1
- package/dist/{cli-hxpyc9sn.js → cli-t5ys4ebm.js} +3 -3
- package/dist/{cli-qmqk2yc2.js → cli-tas6ckky.js} +2 -2
- package/dist/{cli-p1w18yhx.js → cli-tf79ryyy.js} +1 -1
- package/dist/{cli-kc7v1nfv.js → cli-tnzebztn.js} +1 -1
- package/dist/{cli-gax7fk3t.js → cli-vx421edw.js} +3 -3
- package/dist/{cli-kchjb7qy.js → cli-x3ex5bqf.js} +1 -1
- package/dist/cli-yss07azk.js +142 -0
- package/dist/{cli-0ntbxyaa.js → cli-z8cvws4c.js} +363 -47
- package/dist/{cli-tacge1w0.js → cli-zdaypkq9.js} +146 -52
- package/dist/{cli-b8e9nb04.js → cli-zeaehgyd.js} +2 -2
- package/dist/{cli-jcpr5smb.js → cli-ztq5z4e1.js} +4 -4
- package/dist/cli.js +21 -21
- package/dist/{codex-adapter-nha71q3n.js → codex-adapter-2brxx5ta.js} +7 -8
- package/dist/{codex-hook-h3vk6xzr.js → codex-hook-c930c3ea.js} +2 -2
- package/dist/{codex-login-bcda78ge.js → codex-login-cbrs1h5w.js} +1 -1
- package/dist/{codex-session-runner-ak9x1k1h.js → codex-session-runner-y9anfj0r.js} +8 -9
- package/dist/{commands-f587qpq8.js → commands-wn9q06qr.js} +3 -3
- package/dist/{controller-hxx8wwy1.js → controller-fw608czd.js} +4 -4
- package/dist/{controllers-connect-k5sg9m9h.js → controllers-connect-63j5rdcm.js} +20 -20
- package/dist/{cursor-adapter-n5nc3mfe.js → cursor-adapter-6mb9jme8.js} +2 -2
- package/dist/{db-nzf8zb5z.js → db-0m6v5tq4.js} +3 -3
- package/dist/{db-amm8w15a.js → db-y4wfrzbm.js} +4 -2
- package/dist/{devin-adapter-xedz98zv.js → devin-adapter-r0yx3gv8.js} +1 -1
- package/dist/{dispatcher-89zrts8f.js → dispatcher-9tq2zzdj.js} +17 -17
- package/dist/{docker-866qpzt9.js → docker-j75atvpt.js} +2 -2
- package/dist/{dsh-adapter-nrhbrjz8.js → dsh-adapter-t09sv4tq.js} +2 -2
- package/dist/{e2b-s16txnpx.js → e2b-7s9dscap.js} +3 -3
- package/dist/{e2b-jkwfhbcv.js → e2b-a7vzs2vj.js} +3 -3
- package/dist/{easter-egg-3d-533knw22.js → easter-egg-3d-fbwvmwhe.js} +1 -1
- package/dist/{embedding-space-ezwenjmj.js → embedding-space-wh8de1t9.js} +6 -6
- package/dist/{embeddings-np74ht6g.js → embeddings-3hpbdtjr.js} +1 -1
- package/dist/{event-bus-e66gh7cc.js → event-bus-tr6h3gv8.js} +1 -1
- package/dist/{execute-b3mrb1mv.js → execute-4bdf8sdy.js} +1 -1
- package/dist/{external-channel-z46yx9d7.js → external-channel-ts2fwqh0.js} +2 -2
- package/dist/fx-adapter-1rnxnf56.js +17 -0
- package/dist/grok-adapter-efhmf1r8.js +17 -0
- package/dist/handlers-anrr3py9.js +32 -0
- package/dist/{heartbeat-dt9qw9c8.js → heartbeat-2ze5fzpc.js} +20 -20
- package/dist/{hook-8k6fbz9b.js → hook-ca54yd4s.js} +5 -5
- package/dist/{hook-jgcgygex.js → hook-gaehe2w5.js} +8 -8
- package/dist/{http-haw4r8ne.js → http-jfg8t2yk.js} +3839 -122
- package/dist/{index-tvfafeyy.js → index-3gv6sc4z.js} +1 -1
- package/dist/{index-tez78qj3.js → index-3yvzbhxd.js} +100 -23
- package/dist/index-8w2aznds.js +33 -0
- package/dist/{index-5gk12my8.js → index-csgrxedz.js} +1 -1
- package/dist/index-qnjvr362.js +37 -0
- package/dist/index-yzxd3sn9.js +32 -0
- package/dist/{keepalive-xn6s7rmg.js → keepalive-k2a3qe73.js} +4 -4
- package/dist/lead-zhxqbfdv.js +69 -0
- package/dist/{maintenance-vdts800b.js → maintenance-gb5sd5nc.js} +1 -1
- package/dist/{node-4c5jqxr3.js → node-7qsx5kvm.js} +1 -1
- package/dist/{oauth-refresh-sweep-094c8wfk.js → oauth-refresh-sweep-y7rm6bkb.js} +3 -3
- package/dist/{onboard-v99eh56m.js → onboard-jmc5qerv.js} +4 -4
- package/dist/{opencode-adapter-dsagkhtw.js → opencode-adapter-7eade9ev.js} +6 -7
- package/dist/{otel-impl-q7yr9yp0.js → otel-impl-8j1xhxqd.js} +2 -2
- package/dist/{pi-mono-adapter-546cd1cc.js → pi-mono-adapter-yqmaf32x.js} +14 -13
- package/dist/{pricing-refresh-7dh2yase.js → pricing-refresh-1nj722h6.js} +2 -2
- package/dist/{rbac-roles-mdmc96p4.js → rbac-roles-b1mtr6dr.js} +3 -3
- package/dist/{rbac-roles-npbmdh77.js → rbac-roles-n1f3frk1.js} +2 -2
- package/dist/{render-v2-twhhwmms.js → render-v2-1gfpd8rj.js} +4 -4
- package/dist/{resume-5gsrjd5e.js → resume-w1s1eh5v.js} +7 -7
- package/dist/{runtime-1hv06jsn.js → runtime-84g5egsc.js} +1 -1
- package/dist/{session-access-m191x30x.js → session-access-y6z1txk4.js} +4 -4
- package/dist/{session-source-backfill-nt923ask.js → session-source-backfill-gxxwfh12.js} +3 -3
- package/dist/{setup-wv467afy.js → setup-r91gstyk.js} +1 -1
- package/dist/{virtual-modules-gazyapte.js → virtual-modules-cgnj6kme.js} +3 -3
- package/dist/{work-objects-8zkrpy13.js → work-objects-qcxzg61c.js} +20 -20
- package/dist/worker-7j2907g6.js +69 -0
- package/dist/{worker-follow-up-cecqszem.js → worker-follow-up-y2d2h7km.js} +19 -17
- package/dist/{x-2fyrt5pp.js → x-qhxq2ama.js} +2 -2
- package/openapi.json +2908 -351
- package/package.json +4 -4
- package/src/apps/catalog.generated.json +120 -1
- package/src/apps/definition.ts +38 -11
- package/src/apps/page-validator.ts +88 -0
- package/src/be/db/model-profiles.ts +110 -11
- package/src/be/db/tasks/read.ts +53 -0
- package/src/be/db/workflow-run-definitions.ts +19 -8
- package/src/be/db-client.ts +43 -5
- package/src/be/db.ts +40 -9
- package/src/be/migrations/284_model_profile_fallbacks.sql +9 -0
- package/src/be/migrations/289_automation_savings_daily.sql +47 -0
- package/src/be/migrations/291_task_slack_participants.sql +4 -0
- package/src/be/migrations/293_savings_component_links.sql +58 -0
- package/src/be/migrations/296_automation_savings_daily_read_index.sql +6 -0
- package/src/be/model-profile-check.ts +53 -20
- package/src/be/runtime-pools/routing.ts +1 -0
- package/src/be/savings/calibrate.ts +468 -0
- package/src/be/savings/compute.ts +677 -0
- package/src/be/savings/model.ts +674 -0
- package/src/be/savings/read.ts +1500 -0
- package/src/be/savings/rollup.ts +571 -0
- package/src/be/scripts/run-credential.ts +15 -3
- package/src/be/scripts/typecheck.ts +91 -4
- package/src/be/seed/agent-fs-provision.ts +48 -11
- package/src/be/seed/workflows-seeder.ts +67 -23
- package/src/be/seed-scripts/catalog/dreaming-learnings.ts +10 -2
- package/src/be/seed-scripts/catalog/dreaming-memtax-create.ts +70 -4
- package/src/be/skill-parser.ts +19 -7
- package/src/be/swarm-config-guard.ts +8 -0
- package/src/be/users.ts +29 -2
- package/src/commands/onboard/agent-fs-version.ts +1 -1
- package/src/commands/provider-error-class.ts +97 -0
- package/src/commands/runner.ts +258 -58
- package/src/extensions/contract-types.generated.ts +1 -1
- package/src/http/all-routes.ts +2 -0
- package/src/http/fs.ts +8 -2
- package/src/http/index.ts +11 -0
- package/src/http/model-profiles.ts +43 -10
- package/src/http/poll.ts +15 -1
- package/src/http/requested-by.ts +97 -1
- package/src/http/savings-calibration.ts +277 -0
- package/src/http/savings.ts +849 -0
- package/src/http/scripts.ts +26 -15
- package/src/http/tasks.ts +89 -9
- package/src/http/trackers/authorize-gate.ts +41 -0
- package/src/http/trackers/jira.ts +26 -4
- package/src/http/trackers/linear.ts +26 -4
- package/src/integrations/chat-sdk/limits.ts +6 -1
- package/src/jira/oauth.ts +7 -1
- package/src/linear/README.md +2 -1
- package/src/linear/oauth.ts +7 -1
- package/src/prompts/session-templates.ts +80 -3
- package/src/providers/acp-targets.ts +1 -1
- package/src/providers/amp-adapter.ts +1 -1
- package/src/providers/claude-adapter.ts +0 -20
- package/src/providers/claude-flag-settings.ts +12 -10
- package/src/providers/codex-adapter.ts +12 -34
- package/src/providers/opencode-adapter.ts +34 -12
- package/src/providers/pi-mono-adapter.ts +6 -5
- package/src/providers/types.ts +7 -0
- package/src/scheduler/scheduler.ts +20 -14
- package/src/scripts-runtime/ctx.ts +1 -1
- package/src/scripts-runtime/executors/quickjs-bundle.ts +1 -0
- package/src/scripts-runtime/executors/quickjs-prelude.ts +162 -2
- package/src/scripts-runtime/executors/quickjs-runner.ts +4 -1
- package/src/scripts-runtime/sdk-allowlist.ts +11 -1
- package/src/scripts-runtime/stdlib/fetch.ts +113 -15
- package/src/scripts-runtime/stdlib/index.ts +3 -1
- package/src/scripts-runtime/stdlib/retry.ts +174 -0
- package/src/scripts-runtime/swarm-sdk.ts +192 -17
- package/src/scripts-runtime/types/stdlib.d.ts +95 -4
- package/src/scripts-runtime/types/swarm-extension.d.ts +60 -0
- package/src/scripts-runtime/types/swarm-sdk.d.ts +69 -2
- package/src/slack/actions.ts +1 -0
- package/src/slack/assistant.ts +2 -0
- package/src/slack/enrich.ts +144 -50
- package/src/slack/handlers.ts +37 -95
- package/src/slack/templates.ts +9 -0
- package/src/slack/thread-buffer.ts +45 -71
- package/src/slack/thread-participants.ts +187 -0
- package/src/tasks/worker-follow-up.ts +142 -3
- package/src/tests/agent-fs-provision-seeder.test.ts +81 -0
- package/src/tests/apps-chart-shell.test.ts +170 -0
- package/src/tests/apps-icon.test.ts +156 -2
- package/src/tests/base-prompt.test.ts +5 -4
- package/src/tests/chat-sdk-relay.test.ts +12 -0
- package/src/tests/claude-adapter-binary.test.ts +7 -1
- package/src/tests/claude-adapter.test.ts +10 -64
- package/src/tests/claude-flag-settings.test.ts +15 -11
- package/src/tests/claude-hook-credentials.test.ts +0 -1
- package/src/tests/claude-sdk-lifecycle.test.ts +5 -1
- package/src/tests/codex-adapter.test.ts +28 -78
- package/src/tests/comb-default.test.ts +6 -0
- package/src/tests/core-auth.test.ts +7 -6
- package/src/tests/create-task-vcs-repo.test.ts +136 -0
- package/src/tests/db-client-busy-retry.test.ts +58 -6
- package/src/tests/db-task-read-contract.test.ts +2 -0
- package/src/tests/db-task-write-contract.test.ts +1 -0
- package/src/tests/deploy-guard.test.ts +154 -0
- package/src/tests/docs-index-footprint.test.js +3 -3
- package/src/tests/dreaming-learnings.test.ts +104 -0
- package/src/tests/extensions-pre-task.test.ts +4 -2
- package/src/tests/fetch-resolved-env.test.ts +47 -1
- package/src/tests/fixtures/app-shell-charts-definition.json.txt +180 -0
- package/src/tests/model-profile-fallbacks.test.ts +482 -0
- package/src/tests/model-profiles-api.test.ts +1 -0
- package/src/tests/opencode-adapter.test.ts +53 -6
- package/src/tests/pi-mono-session-cost-on-abort.test.ts +115 -0
- package/src/tests/prompt-template-session.test.ts +17 -1
- package/src/tests/provider-error-class.test.ts +174 -0
- package/src/tests/runner-provider-fallback.test.ts +197 -0
- package/src/tests/runner-requester-profile.test.ts +139 -1
- package/src/tests/savings-calibration-api.test.ts +264 -0
- package/src/tests/savings-calibration.test.ts +201 -0
- package/src/tests/savings-compound.test.ts +774 -0
- package/src/tests/savings-compute.test.ts +215 -0
- package/src/tests/savings-fleet-latency.test.ts +143 -0
- package/src/tests/savings-fleet-read.test.ts +195 -0
- package/src/tests/savings-fleet-scale.test.ts +222 -0
- package/src/tests/savings-model.test.ts +499 -0
- package/src/tests/savings-read.test.ts +367 -0
- package/src/tests/savings-rollup.test.ts +766 -0
- package/src/tests/savings-scripts.test.ts +611 -0
- package/src/tests/script-connections.test.ts +4 -0
- package/src/tests/script-executor-conformance.test.ts +444 -0
- package/src/tests/scripts-http.test.ts +55 -0
- package/src/tests/scripts-runtime-fetch.test.ts +2 -2
- package/src/tests/sdk-allowlist.test.ts +40 -0
- package/src/tests/secret-rules.test.ts +124 -1
- package/src/tests/secret-scrubber.test.ts +1 -1
- package/src/tests/seed-workflows.test.ts +167 -11
- package/src/tests/skill-parser.test.ts +45 -0
- package/src/tests/slack-dm-implicit-mention.test.ts +20 -1
- package/src/tests/slack-identity-resolution.test.ts +23 -0
- package/src/tests/slack-inbound-files.test.ts +2 -1
- package/src/tests/slack-thread-participants.test.ts +429 -0
- package/src/tests/tool-annotations.test.ts +30 -0
- package/src/tests/tracker-oauth-authorize-auth.test.ts +262 -0
- package/src/tests/workflow-definition-pinning.test.ts +128 -3
- package/src/tests/workflow-script-principal.test.ts +94 -4
- package/src/tools/app-upsert.ts +1 -1
- package/src/tools/model-profiles/list-model-profiles.ts +4 -2
- package/src/tools/update-profile.ts +1 -1
- package/src/types.ts +146 -33
- package/src/utils/constants.ts +2 -0
- package/src/utils/mcp-server-fetcher.test.ts +89 -0
- package/src/utils/mcp-server-fetcher.ts +13 -5
- package/src/utils/request-auth-context.ts +24 -0
- package/src/utils/secret-rules.generated.ts +1 -1
- package/src/utils/secret-scrubber.ts +154 -29
- package/src/workflows/executors/swarm-script.ts +23 -19
- package/templates/skills/apps/SKILL.md +62 -3
- package/templates/skills/apps/content.md +62 -3
- package/templates/skills/swarm-docs/content.md +2 -0
- package/templates/skills/swarm-scripts/SKILL.md +5 -4
- package/templates/skills/swarm-scripts/content.md +5 -4
- package/templates/skills/work-on-task/SKILL.md +1 -1
- package/templates/skills/work-on-task/content.md +1 -1
- package/templates/workflows/dreaming/content.md +1 -1
- package/dist/acp-adapter-pdy3qsvg.js +0 -15
- package/dist/cli-4bd1rck6.js +0 -4
- package/dist/cli-jywyhqws.js +0 -104
- package/dist/fx-adapter-3q12snn6.js +0 -18
- package/dist/grok-adapter-ahb1w033.js +0 -18
- package/dist/handlers-vbww69re.js +0 -32
- package/dist/index-4xwaf5th.js +0 -32
- package/dist/index-f0hcrs8e.js +0 -33
- package/dist/index-s4ewdma4.js +0 -37
- package/dist/lead-6ygbfhbh.js +0 -70
- package/dist/worker-mz5z7kj0.js +0 -70
- package/src/providers/ctx-mode-env.ts +0 -10
|
@@ -0,0 +1,611 @@
|
|
|
1
|
+
import { afterAll, beforeAll, describe, expect, test } from "bun:test";
|
|
2
|
+
import { unlink } from "node:fs/promises";
|
|
3
|
+
import { createServer as createHttpServer, type Server } from "node:http";
|
|
4
|
+
import {
|
|
5
|
+
closeDb,
|
|
6
|
+
createAgent,
|
|
7
|
+
createScheduledTask,
|
|
8
|
+
createTaskExtended,
|
|
9
|
+
getDbClient,
|
|
10
|
+
initDb,
|
|
11
|
+
upsertSwarmConfig,
|
|
12
|
+
} from "../be/db";
|
|
13
|
+
import { computeScriptWindow, LIVE_MAX_RUNS, type SavingsSql } from "../be/savings/compute";
|
|
14
|
+
import {
|
|
15
|
+
CURVE,
|
|
16
|
+
type CurveSet,
|
|
17
|
+
curve,
|
|
18
|
+
DEFAULT_OPS,
|
|
19
|
+
F_FOLLOWUP,
|
|
20
|
+
INLINE_SCRIPT_NAME,
|
|
21
|
+
K,
|
|
22
|
+
R,
|
|
23
|
+
SAVINGS_CALIBRATION_KEY,
|
|
24
|
+
SAVINGS_TIERS,
|
|
25
|
+
T0,
|
|
26
|
+
} from "../be/savings/model";
|
|
27
|
+
import { readScriptSavings, resetSavingsReadCache } from "../be/savings/read";
|
|
28
|
+
import {
|
|
29
|
+
daysToCompute,
|
|
30
|
+
RECOMPUTE_AFTER_MS,
|
|
31
|
+
resetSavingsRollupForTests,
|
|
32
|
+
runSavingsRollupTick,
|
|
33
|
+
} from "../be/savings/rollup";
|
|
34
|
+
import { clearCalibrationCache, handleSavings } from "../http/savings";
|
|
35
|
+
import { getPathSegments, parseQueryParams } from "../http/utils";
|
|
36
|
+
import { listenOnFreePort } from "./test-net";
|
|
37
|
+
|
|
38
|
+
const TEST_DB_PATH = "./test-savings-scripts.sqlite";
|
|
39
|
+
const NOW = Date.parse("2026-10-31T12:00:00.000Z");
|
|
40
|
+
const WEEK = ["25", "26", "27", "28", "29", "30", "31"].map((d) => `2026-10-${d}`);
|
|
41
|
+
|
|
42
|
+
const inProcessSql: SavingsSql = (sql, params) => getDbClient().query(sql, params);
|
|
43
|
+
|
|
44
|
+
async function removeDbFiles(path: string): Promise<void> {
|
|
45
|
+
for (const suffix of ["", "-wal", "-shm"]) {
|
|
46
|
+
try {
|
|
47
|
+
await unlink(path + suffix);
|
|
48
|
+
} catch (error) {
|
|
49
|
+
if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
async function catalogScript(name: string, source: string): Promise<void> {
|
|
55
|
+
await getDbClient().run(
|
|
56
|
+
`INSERT INTO scripts (id, name, scope, source, description, intent, signatureJson, contentHash)
|
|
57
|
+
VALUES (?, ?, 'global', ?, 'savings fixture', 'fixture', '{}', ?)`,
|
|
58
|
+
[crypto.randomUUID(), name, source, crypto.randomUUID()],
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** `count` runs of one script on `day`, one a minute from 00:00, each 30 s long. */
|
|
63
|
+
async function bulkScriptRuns(
|
|
64
|
+
agentId: string,
|
|
65
|
+
name: string | null,
|
|
66
|
+
day: string,
|
|
67
|
+
count: number,
|
|
68
|
+
apiEndpointId: string | null = null,
|
|
69
|
+
) {
|
|
70
|
+
await getDbClient().run(
|
|
71
|
+
`INSERT INTO script_runs (id, agentId, scriptName, source, status, startedAt, finishedAt, apiEndpointId)
|
|
72
|
+
WITH RECURSIVE seq(i) AS (SELECT 0 UNION ALL SELECT i + 1 FROM seq WHERE i < ? - 1)
|
|
73
|
+
SELECT ? || '-' || ? || '-' || i, ?, ?, 'x', 'completed',
|
|
74
|
+
strftime('%Y-%m-%dT%H:%M:%fZ', ?, '+' || i || ' minutes'),
|
|
75
|
+
strftime('%Y-%m-%dT%H:%M:%fZ', ?, '+' || i || ' minutes', '+30 seconds'), ? FROM seq`,
|
|
76
|
+
[
|
|
77
|
+
count,
|
|
78
|
+
name ?? "inline",
|
|
79
|
+
day,
|
|
80
|
+
agentId,
|
|
81
|
+
name,
|
|
82
|
+
`${day}T00:00:00.000Z`,
|
|
83
|
+
`${day}T00:00:00.000Z`,
|
|
84
|
+
apiEndpointId,
|
|
85
|
+
],
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const unit = (ops: number) => curve(T0 + K * ops, "smart", "usd");
|
|
90
|
+
|
|
91
|
+
describe("savings scripts", () => {
|
|
92
|
+
let agentId: string;
|
|
93
|
+
let server: Server;
|
|
94
|
+
let port: number;
|
|
95
|
+
|
|
96
|
+
beforeAll(async () => {
|
|
97
|
+
await removeDbFiles(TEST_DB_PATH);
|
|
98
|
+
initDb(TEST_DB_PATH);
|
|
99
|
+
await resetSavingsRollupForTests();
|
|
100
|
+
agentId = (await createAgent({ name: "savings-scripts-agent", isLead: false, status: "idle" }))
|
|
101
|
+
.id;
|
|
102
|
+
await catalogScript("poll-sched", "await a(); await b(); await c(); await d();");
|
|
103
|
+
await catalogScript("helper", "await a(); await b();".repeat(4));
|
|
104
|
+
await catalogScript("dispatcher-api", "await ctx.swarm.task_send({}); await x();");
|
|
105
|
+
await createScheduledTask({
|
|
106
|
+
name: "poll-sched-hourly",
|
|
107
|
+
intervalMs: 3_600_000,
|
|
108
|
+
targetType: "script",
|
|
109
|
+
scriptName: "poll-sched",
|
|
110
|
+
});
|
|
111
|
+
for (const day of WEEK) {
|
|
112
|
+
// 30 runs a day of a scheduled script: 210 in the week against a cap of 168.
|
|
113
|
+
await bulkScriptRuns(agentId, "poll-sched", day, 30);
|
|
114
|
+
// 5 runs a day of a script agents call.
|
|
115
|
+
await bulkScriptRuns(agentId, "helper", day, 5);
|
|
116
|
+
// 3 API runs a day of a script that can dispatch.
|
|
117
|
+
await bulkScriptRuns(agentId, "dispatcher-api", day, 3, "endpoint-1");
|
|
118
|
+
}
|
|
119
|
+
// One API run created an agent task a minute after it started.
|
|
120
|
+
const task = await createTaskExtended("savings scripts fixture", { agentId });
|
|
121
|
+
const runId = `dispatcher-api-${WEEK[2]}-0`;
|
|
122
|
+
await getDbClient().run("UPDATE agent_tasks SET createdAt=? WHERE id=?", [
|
|
123
|
+
`${WEEK[2]}T00:01:00.000Z`,
|
|
124
|
+
task.id,
|
|
125
|
+
]);
|
|
126
|
+
await getDbClient().run("UPDATE script_runs SET output=? WHERE id=?", [
|
|
127
|
+
JSON.stringify({ taskId: task.id }),
|
|
128
|
+
runId,
|
|
129
|
+
]);
|
|
130
|
+
// An API run at 23:59 whose task lands after midnight, with no run the next day.
|
|
131
|
+
await catalogScript("midnight-api", "await ctx.swarm.task_send({});");
|
|
132
|
+
await getDbClient().run(
|
|
133
|
+
`INSERT INTO script_runs (id, agentId, scriptName, source, status, startedAt, finishedAt, apiEndpointId)
|
|
134
|
+
VALUES ('midnight-run', ?, 'midnight-api', 'x', 'completed', ?, ?, 'endpoint-2')`,
|
|
135
|
+
[agentId, `${WEEK[3]}T23:59:00.000Z`, `${WEEK[3]}T23:59:30.000Z`],
|
|
136
|
+
);
|
|
137
|
+
const late = await createTaskExtended("savings scripts midnight fixture", { agentId });
|
|
138
|
+
await getDbClient().run("UPDATE agent_tasks SET createdAt=? WHERE id=?", [
|
|
139
|
+
`${WEEK[4]}T00:01:00.000Z`,
|
|
140
|
+
late.id,
|
|
141
|
+
]);
|
|
142
|
+
await getDbClient().run("UPDATE script_runs SET output=? WHERE id='midnight-run'", [
|
|
143
|
+
JSON.stringify({ taskId: late.id }),
|
|
144
|
+
]);
|
|
145
|
+
// Runs with no script name and a name missing from the catalog.
|
|
146
|
+
await bulkScriptRuns(agentId, null, WEEK[1] as string, 2);
|
|
147
|
+
await bulkScriptRuns(agentId, "ghost", WEEK[1] as string, 1);
|
|
148
|
+
|
|
149
|
+
server = createHttpServer(async (req, res) => {
|
|
150
|
+
const handled = await handleSavings(
|
|
151
|
+
req,
|
|
152
|
+
res,
|
|
153
|
+
getPathSegments(req.url || ""),
|
|
154
|
+
parseQueryParams(req.url || ""),
|
|
155
|
+
);
|
|
156
|
+
if (!handled) {
|
|
157
|
+
res.writeHead(404);
|
|
158
|
+
res.end();
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
port = await listenOnFreePort(server);
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
afterAll(async () => {
|
|
165
|
+
server?.close();
|
|
166
|
+
await resetSavingsRollupForTests();
|
|
167
|
+
closeDb();
|
|
168
|
+
await removeDbFiles(TEST_DB_PATH);
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
test("compute keys runs by name, counts awaits as ops and finds API effect runs", async () => {
|
|
172
|
+
const out = await computeScriptWindow(
|
|
173
|
+
{ start: `${WEEK[0]}T00:00:00.000Z`, end: "2026-11-01T00:00:00.000Z" },
|
|
174
|
+
inProcessSql,
|
|
175
|
+
);
|
|
176
|
+
const sum = (name: string) => {
|
|
177
|
+
const s = out.find((x) => x.scriptName === name);
|
|
178
|
+
const f = { runs: 0, apiRuns: 0, effectRuns: 0, apiEffectRuns: 0, opsSum: 0, computeSecs: 0 };
|
|
179
|
+
for (const d of s?.byDay ?? []) {
|
|
180
|
+
for (const k of Object.keys(f) as (keyof typeof f)[]) f[k] += d[k];
|
|
181
|
+
}
|
|
182
|
+
return f;
|
|
183
|
+
};
|
|
184
|
+
expect(out.map((s) => s.scriptName)).toEqual([
|
|
185
|
+
INLINE_SCRIPT_NAME,
|
|
186
|
+
"dispatcher-api",
|
|
187
|
+
"ghost",
|
|
188
|
+
"helper",
|
|
189
|
+
"midnight-api",
|
|
190
|
+
"poll-sched",
|
|
191
|
+
]);
|
|
192
|
+
expect(sum("poll-sched")).toMatchObject({ runs: 210, apiRuns: 0, opsSum: 4 * 210 });
|
|
193
|
+
expect(sum("poll-sched").computeSecs).toBeCloseTo(210 * 30, 3);
|
|
194
|
+
expect(sum("helper").opsSum).toBe(8 * 35);
|
|
195
|
+
expect(sum("dispatcher-api")).toMatchObject({ runs: 21, apiRuns: 21, apiEffectRuns: 1 });
|
|
196
|
+
expect(sum("ghost").opsSum).toBe(DEFAULT_OPS);
|
|
197
|
+
// Negative control: a name with no runs.
|
|
198
|
+
expect(
|
|
199
|
+
await computeScriptWindow(
|
|
200
|
+
{ start: `${WEEK[0]}T00:00:00.000Z`, end: "2026-11-01T00:00:00.000Z" },
|
|
201
|
+
inProcessSql,
|
|
202
|
+
{ scriptName: "nope" },
|
|
203
|
+
),
|
|
204
|
+
).toEqual([]);
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
test("before the first tick the script window is not computed", async () => {
|
|
208
|
+
const read = await readScriptSavings({ days: 7, tier: "smart", now: NOW });
|
|
209
|
+
expect(read).toMatchObject({ complete: false, rows: [], total: null });
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
test("the tick fills script buckets for every day", async () => {
|
|
213
|
+
const stats = await runSavingsRollupTick(inProcessSql, { now: NOW });
|
|
214
|
+
expect(stats.lastError).toBeNull();
|
|
215
|
+
expect(stats.daysPending).toBe(0);
|
|
216
|
+
// Today and yesterday are not final: they are due again once the computation ages.
|
|
217
|
+
expect(await daysToCompute("script", NOW)).toEqual([]);
|
|
218
|
+
expect(await daysToCompute("script", NOW + RECOMPUTE_AFTER_MS)).toEqual([
|
|
219
|
+
"2026-10-31",
|
|
220
|
+
"2026-10-30",
|
|
221
|
+
]);
|
|
222
|
+
const api = await getDbClient().get<{ n: number }>(
|
|
223
|
+
"SELECT count(*) n FROM automation_savings_daily WHERE jobKind='script' AND subKind='api'",
|
|
224
|
+
);
|
|
225
|
+
// dispatcher-api on each of the 7 days, midnight-api on its run's day.
|
|
226
|
+
expect(api?.n).toBe(8);
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
test("an API run at 23:59 credits its after-midnight task to the run's day", async () => {
|
|
230
|
+
const buckets = await getDbClient().query<{ day: string; subKind: string; effectRuns: number }>(
|
|
231
|
+
"SELECT day, subKind, effectRuns FROM automation_savings_daily WHERE jobKind='script' AND jobName='midnight-api' ORDER BY day, subKind",
|
|
232
|
+
);
|
|
233
|
+
// No bucket on the task's day: the effect stays on the run's day.
|
|
234
|
+
expect(buckets).toEqual([
|
|
235
|
+
{ day: WEEK[3] as string, subKind: "", effectRuns: 0 },
|
|
236
|
+
{ day: WEEK[3] as string, subKind: "api", effectRuns: 1 },
|
|
237
|
+
]);
|
|
238
|
+
const read = await readScriptSavings({ days: 7, tier: "smart", now: NOW });
|
|
239
|
+
const row = read.rows.find((r) => r.scriptName === "midnight-api");
|
|
240
|
+
expect(row).toMatchObject({ runs: 1, substitutedRuns: 1, effectRuns: 1 });
|
|
241
|
+
expect(row?.savingUsd.p50).toBeCloseTo(R.p50 * unit(1) + F_FOLLOWUP, 6);
|
|
242
|
+
expect(row?.hoursSaved.p50).toBeGreaterThan(0);
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
test("scheduled scripts are capped standalone tasks; agent-invoked ones are marginal turns", async () => {
|
|
246
|
+
const read = await readScriptSavings({ days: 7, tier: "smart", now: NOW });
|
|
247
|
+
expect(read.complete).toBe(true);
|
|
248
|
+
const row = (name: string) => read.rows.find((r) => r.scriptName === name);
|
|
249
|
+
expect(row("poll-sched")).toMatchObject({
|
|
250
|
+
kind: "script-scheduled",
|
|
251
|
+
runs: 210,
|
|
252
|
+
substitutedRuns: 168,
|
|
253
|
+
capApplied: true,
|
|
254
|
+
actualUsd: 0,
|
|
255
|
+
});
|
|
256
|
+
expect(row("poll-sched")?.savingUsd.p50).toBeCloseTo(168 * (R.p50 * unit(4) + F_FOLLOWUP), 6);
|
|
257
|
+
expect(row("helper")).toMatchObject({ kind: "script-agent-invoked", substitutedRuns: 35 });
|
|
258
|
+
expect(row("helper")?.savingUsd.p50).toBeCloseTo(
|
|
259
|
+
35 * R.p50 * (unit(8) - curve(T0, "smart", "usd")),
|
|
260
|
+
6,
|
|
261
|
+
);
|
|
262
|
+
// API runs count only with an effect, as one standalone decision each.
|
|
263
|
+
expect(row("dispatcher-api")).toMatchObject({
|
|
264
|
+
runs: 21,
|
|
265
|
+
substitutedRuns: 1,
|
|
266
|
+
enabledOnlyRuns: 20,
|
|
267
|
+
effectRuns: 1,
|
|
268
|
+
});
|
|
269
|
+
expect(row("dispatcher-api")?.savingUsd.p50).toBeCloseTo(R.p50 * unit(2) + F_FOLLOWUP, 6);
|
|
270
|
+
expect(row(INLINE_SCRIPT_NAME)?.runs).toBe(2);
|
|
271
|
+
const p50 = read.rows.reduce((s, r) => s + r.savingUsd.p50, 0);
|
|
272
|
+
expect(read.total?.savingUsd.p50).toBeCloseTo(p50, 6);
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
test("daily points of one script add up to its window saving", async () => {
|
|
276
|
+
const read = await readScriptSavings({
|
|
277
|
+
days: 30,
|
|
278
|
+
tier: "smart",
|
|
279
|
+
now: NOW,
|
|
280
|
+
scriptName: "poll-sched",
|
|
281
|
+
});
|
|
282
|
+
expect(read.rows).toHaveLength(1);
|
|
283
|
+
// Thirty rolling days touch thirty-one UTC days.
|
|
284
|
+
expect(read.points).toHaveLength(31);
|
|
285
|
+
const saving = read.points.reduce((s, p) => s + p.savingUsd, 0);
|
|
286
|
+
expect(saving).toBeCloseTo(read.rows[0]?.savingUsd.p50 ?? Number.NaN, 6);
|
|
287
|
+
expect(read.points.filter((p) => p.savingUsd > 0).map((p) => p.day)).toEqual(WEEK);
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
test("the window's partial first day counts only the runs inside the window", async () => {
|
|
291
|
+
// Three rolling days from noon: the first day is a fixture day whose runs
|
|
292
|
+
// all start before noon, so none of them are inside the window.
|
|
293
|
+
const count = async (from: string, to = "9999") =>
|
|
294
|
+
Number(
|
|
295
|
+
(
|
|
296
|
+
await getDbClient().get<{ n: number }>(
|
|
297
|
+
"SELECT count(*) n FROM script_runs WHERE scriptName='poll-sched' AND startedAt>=? AND startedAt<?",
|
|
298
|
+
[from, to],
|
|
299
|
+
)
|
|
300
|
+
)?.n,
|
|
301
|
+
);
|
|
302
|
+
const opts = { days: 3, tier: "smart" as const, now: NOW, scriptName: "poll-sched" };
|
|
303
|
+
const live = await readScriptSavings({ ...opts, sql: inProcessSql });
|
|
304
|
+
expect(live.window.start).toBe("2026-10-28T12:00:00.000Z");
|
|
305
|
+
expect(live.boundaryEstimated).toBe(false);
|
|
306
|
+
expect(live.rows[0]?.runs).toBe(await count(live.window.start));
|
|
307
|
+
expect(live.points).toHaveLength(4);
|
|
308
|
+
expect(live.points[0]).toMatchObject({ day: WEEK[3], savingUsd: 0 });
|
|
309
|
+
|
|
310
|
+
// A failed scan scales the first day by the share of it inside the window.
|
|
311
|
+
const failing: SavingsSql = async () => {
|
|
312
|
+
throw new Error("database is busy");
|
|
313
|
+
};
|
|
314
|
+
resetSavingsReadCache();
|
|
315
|
+
const scaled = await readScriptSavings({ ...opts, sql: failing });
|
|
316
|
+
expect(scaled.boundaryEstimated).toBe(true);
|
|
317
|
+
const firstDay = await count(`${WEEK[3]}T00:00:00.000Z`, `${WEEK[4]}T00:00:00.000Z`);
|
|
318
|
+
expect(firstDay).toBeGreaterThan(0);
|
|
319
|
+
const scaledRuns = (await count(`${WEEK[4]}T00:00:00.000Z`)) + Math.round(firstDay / 2);
|
|
320
|
+
expect(scaled.rows[0]?.runs).toBe(scaledRuns);
|
|
321
|
+
// The failed scan is not kept: the next read in the same minute scans again.
|
|
322
|
+
const again = await readScriptSavings({ ...opts, sql: inProcessSql });
|
|
323
|
+
expect(again.boundaryEstimated).toBe(false);
|
|
324
|
+
expect(again.rows).toEqual(live.rows);
|
|
325
|
+
|
|
326
|
+
// Above LIVE_MAX_RUNS runs in the slice the scan is skipped and the day is scaled.
|
|
327
|
+
const scans: string[] = [];
|
|
328
|
+
const busy =
|
|
329
|
+
(runs: number): SavingsSql =>
|
|
330
|
+
async (sql, params) => {
|
|
331
|
+
if (sql.startsWith("SELECT count(*) n FROM script_runs")) return [{ n: runs }] as never;
|
|
332
|
+
scans.push(sql);
|
|
333
|
+
return inProcessSql(sql, params);
|
|
334
|
+
};
|
|
335
|
+
resetSavingsReadCache();
|
|
336
|
+
const capped = await readScriptSavings({ ...opts, sql: busy(LIVE_MAX_RUNS + 1) });
|
|
337
|
+
expect(capped.boundaryEstimated).toBe(true);
|
|
338
|
+
expect(capped.rows[0]?.runs).toBe(scaledRuns);
|
|
339
|
+
expect(scans).toEqual([]);
|
|
340
|
+
// Control: at the cap the slice is scanned exactly.
|
|
341
|
+
resetSavingsReadCache();
|
|
342
|
+
const atCap = await readScriptSavings({ ...opts, sql: busy(LIVE_MAX_RUNS) });
|
|
343
|
+
expect(atCap.boundaryEstimated).toBe(false);
|
|
344
|
+
expect(atCap.rows).toEqual(live.rows);
|
|
345
|
+
expect(scans.length).toBeGreaterThan(0);
|
|
346
|
+
resetSavingsReadCache();
|
|
347
|
+
});
|
|
348
|
+
|
|
349
|
+
test("fleet reads across the minutes of an hour scan the first day once", async () => {
|
|
350
|
+
resetSavingsReadCache();
|
|
351
|
+
const MINUTE = 60_000;
|
|
352
|
+
let scans = 0;
|
|
353
|
+
const counting: SavingsSql = (sql, params) => {
|
|
354
|
+
// Every first-day scan opens with this count.
|
|
355
|
+
if (sql.startsWith("SELECT count(*) n FROM script_runs")) scans += 1;
|
|
356
|
+
return inProcessSql(sql, params);
|
|
357
|
+
};
|
|
358
|
+
const list = (now: number) => readScriptSavings({ days: 3, tier: "smart", now, sql: counting });
|
|
359
|
+
const first = await list(NOW + MINUTE);
|
|
360
|
+
expect(first.boundaryEstimated).toBe(false);
|
|
361
|
+
expect(first.rows.length).toBeGreaterThan(1);
|
|
362
|
+
expect(scans).toBe(1);
|
|
363
|
+
for (const m of [2, 17, 59]) {
|
|
364
|
+
// Within the hour every fleet read prices the same slice, from 12:00.
|
|
365
|
+
expect((await list(NOW + m * MINUTE)).rows).toEqual(first.rows);
|
|
366
|
+
}
|
|
367
|
+
expect(scans).toBe(1);
|
|
368
|
+
// Concurrent reads share one scan.
|
|
369
|
+
resetSavingsReadCache();
|
|
370
|
+
await Promise.all([list(NOW + MINUTE), list(NOW + 2 * MINUTE)]);
|
|
371
|
+
expect(scans).toBe(2);
|
|
372
|
+
// The next hour's window scans again.
|
|
373
|
+
await list(NOW + 61 * MINUTE);
|
|
374
|
+
expect(scans).toBe(3);
|
|
375
|
+
resetSavingsReadCache();
|
|
376
|
+
});
|
|
377
|
+
|
|
378
|
+
test("fleet reads keep only the runs from a mid-hour or midnight window start", async () => {
|
|
379
|
+
const MINUTE = 60_000;
|
|
380
|
+
const day = WEEK[3] as string;
|
|
381
|
+
const times = ["00:15", "00:45", "12:15", "12:45"];
|
|
382
|
+
for (const t of times) {
|
|
383
|
+
await getDbClient().run(
|
|
384
|
+
`INSERT INTO script_runs (id, agentId, scriptName, source, status, startedAt, finishedAt)
|
|
385
|
+
VALUES (?, ?, 'edge-window', 'x', 'completed', ?, ?)`,
|
|
386
|
+
[`edge-window-${t}`, agentId, `${day}T${t}:00.000Z`, `${day}T${t}:30.000Z`],
|
|
387
|
+
);
|
|
388
|
+
}
|
|
389
|
+
const rollUp = async () => {
|
|
390
|
+
await getDbClient().run("DELETE FROM automation_savings_days WHERE day=?", [day]);
|
|
391
|
+
await runSavingsRollupTick(inProcessSql, { now: NOW });
|
|
392
|
+
resetSavingsReadCache();
|
|
393
|
+
};
|
|
394
|
+
await rollUp();
|
|
395
|
+
const read = (now: number, sql: SavingsSql = inProcessSql, scriptName?: string) =>
|
|
396
|
+
readScriptSavings({ days: 3, tier: "smart", now, sql, scriptName });
|
|
397
|
+
const runsOf = (r: { rows: { scriptName: string; runs: number }[] }, name: string) =>
|
|
398
|
+
r.rows.find((row) => row.scriptName === name)?.runs ?? 0;
|
|
399
|
+
|
|
400
|
+
// Mid-hour: a 12:10 start keeps both 12:xx runs.
|
|
401
|
+
expect(runsOf(await read(NOW + 10 * MINUTE), "edge-window")).toBe(2);
|
|
402
|
+
// The same hour's cached slice starts at 12:00; a 12:30 start drops the 12:15 run.
|
|
403
|
+
const mid = await read(NOW + 30 * MINUTE);
|
|
404
|
+
expect(mid.window.start).toBe(`${day}T12:30:00.000Z`);
|
|
405
|
+
expect(mid.boundaryEstimated).toBe(false);
|
|
406
|
+
expect(runsOf(mid, "edge-window")).toBe(1);
|
|
407
|
+
|
|
408
|
+
// Midnight: a 00:30 start (slice floored to the day start) drops the 00:15 run.
|
|
409
|
+
resetSavingsReadCache();
|
|
410
|
+
const midnight = Date.parse("2026-10-31T00:30:00.000Z");
|
|
411
|
+
const night = await read(midnight);
|
|
412
|
+
expect(runsOf(night, "edge-window")).toBe(3);
|
|
413
|
+
// poll-sched's first-day runs all fall from 00:00 to 00:29: the fleet row matches the exact single-script read.
|
|
414
|
+
const single = await read(midnight, inProcessSql, "poll-sched");
|
|
415
|
+
expect(runsOf(night, "poll-sched")).toBe(single.rows[0]?.runs ?? -1);
|
|
416
|
+
// Control: a window starting at midnight keeps the whole first day.
|
|
417
|
+
expect(runsOf(await read(Date.parse("2026-10-31T00:00:00.000Z")), "edge-window")).toBe(4);
|
|
418
|
+
|
|
419
|
+
// A failed scan scales the first day by the share of it from the window start, not the hour.
|
|
420
|
+
const failing: SavingsSql = async () => {
|
|
421
|
+
throw new Error("database is busy");
|
|
422
|
+
};
|
|
423
|
+
resetSavingsReadCache();
|
|
424
|
+
const scaledFleet = await read(NOW + 30 * MINUTE, failing);
|
|
425
|
+
resetSavingsReadCache();
|
|
426
|
+
const scaledSingle = await read(NOW + 30 * MINUTE, failing, "poll-sched");
|
|
427
|
+
expect(scaledFleet.boundaryEstimated).toBe(true);
|
|
428
|
+
// 30 x 11.5/24 rounds to 14; from 12:00 it would be 15.
|
|
429
|
+
expect(runsOf(scaledFleet, "poll-sched")).toBe(scaledSingle.rows[0]?.runs ?? -1);
|
|
430
|
+
|
|
431
|
+
await getDbClient().run("DELETE FROM script_runs WHERE scriptName='edge-window'");
|
|
432
|
+
await rollUp();
|
|
433
|
+
});
|
|
434
|
+
|
|
435
|
+
describe("routes", () => {
|
|
436
|
+
const get = async (path: string) => {
|
|
437
|
+
const res = await fetch(`http://127.0.0.1:${port}${path}`);
|
|
438
|
+
return { status: res.status, body: (await res.json().catch(() => null)) as never };
|
|
439
|
+
};
|
|
440
|
+
|
|
441
|
+
test("scriptName, kind=script and the daily series read the script rollup", async () => {
|
|
442
|
+
// The ticks above used a fixed clock; cover the real current window.
|
|
443
|
+
await runSavingsRollupTick(inProcessSql);
|
|
444
|
+
await bulkScriptRuns(agentId, "helper", new Date().toISOString().slice(0, 10), 2);
|
|
445
|
+
await runSavingsRollupTick(inProcessSql);
|
|
446
|
+
const one = (await get("/api/savings/jobs?scriptName=helper&days=7")).body as {
|
|
447
|
+
valid: boolean;
|
|
448
|
+
boundaryEstimated: boolean;
|
|
449
|
+
window: { start: string };
|
|
450
|
+
rows: { kind: string; scriptName: string; workflowId?: string; runs: number }[];
|
|
451
|
+
};
|
|
452
|
+
expect(one.valid).toBe(true);
|
|
453
|
+
// The real window can also cover fixture days, so count its runs.
|
|
454
|
+
const count = async (from: string, to = "9999") =>
|
|
455
|
+
Number(
|
|
456
|
+
(
|
|
457
|
+
await getDbClient().get<{ n: number }>(
|
|
458
|
+
"SELECT count(*) n FROM script_runs WHERE scriptName='helper' AND startedAt>=? AND startedAt<?",
|
|
459
|
+
[from, to],
|
|
460
|
+
)
|
|
461
|
+
)?.n,
|
|
462
|
+
);
|
|
463
|
+
expect(one.rows).toEqual([
|
|
464
|
+
expect.objectContaining({ kind: "script-agent-invoked", scriptName: "helper" }),
|
|
465
|
+
]);
|
|
466
|
+
const runs = one.rows[0]?.runs ?? -1;
|
|
467
|
+
if (one.boundaryEstimated) {
|
|
468
|
+
// The live scan of the partial first day failed: that day was scaled.
|
|
469
|
+
const firstDay = one.window.start.slice(0, 10);
|
|
470
|
+
const secondDay = new Date(Date.parse(`${firstDay}T00:00:00.000Z`) + 86_400_000)
|
|
471
|
+
.toISOString()
|
|
472
|
+
.slice(0, 10);
|
|
473
|
+
const after = await count(`${secondDay}T00:00:00.000Z`);
|
|
474
|
+
expect(runs).toBeGreaterThanOrEqual(after);
|
|
475
|
+
expect(runs).toBeLessThanOrEqual(
|
|
476
|
+
after + (await count(`${firstDay}T00:00:00.000Z`, `${secondDay}T00:00:00.000Z`)),
|
|
477
|
+
);
|
|
478
|
+
} else {
|
|
479
|
+
expect(runs).toBe(await count(one.window.start));
|
|
480
|
+
}
|
|
481
|
+
expect(one.rows[0]?.workflowId).toBeUndefined();
|
|
482
|
+
const list = (await get("/api/savings/jobs?kind=script&days=7")).body as {
|
|
483
|
+
rows: { scriptName: string }[];
|
|
484
|
+
};
|
|
485
|
+
expect(list.rows.map((r) => r.scriptName)).toContain("helper");
|
|
486
|
+
const daily = (await get("/api/savings/daily?scriptName=helper&days=7")).body as {
|
|
487
|
+
valid: boolean;
|
|
488
|
+
points: { savingUsd: number }[];
|
|
489
|
+
};
|
|
490
|
+
expect(daily.valid).toBe(true);
|
|
491
|
+
expect(daily.points).toHaveLength(8);
|
|
492
|
+
// A catalog script with no runs in the window.
|
|
493
|
+
await catalogScript("idle-script", "await a();");
|
|
494
|
+
expect((await get("/api/savings/jobs?scriptName=idle-script&days=7")).body).toMatchObject({
|
|
495
|
+
valid: false,
|
|
496
|
+
reason: "no-runs",
|
|
497
|
+
rows: [],
|
|
498
|
+
});
|
|
499
|
+
});
|
|
500
|
+
|
|
501
|
+
test("a stored deployment curve prices scheduled and agent-invoked scripts", async () => {
|
|
502
|
+
await catalogScript("calib-sched", "await a(); await b(); await c();");
|
|
503
|
+
await catalogScript("calib-helper", "await a(); await b(); await c(); await d(); await e();");
|
|
504
|
+
await createScheduledTask({
|
|
505
|
+
name: "calib-sched-hourly",
|
|
506
|
+
intervalMs: 3_600_000,
|
|
507
|
+
targetType: "script",
|
|
508
|
+
scriptName: "calib-sched",
|
|
509
|
+
});
|
|
510
|
+
// Fresh runs, so the on-read refresh of today picks them up.
|
|
511
|
+
const recent = async (name: string, count: number) => {
|
|
512
|
+
for (let i = 0; i < count; i++) {
|
|
513
|
+
const at = new Date(Date.now() - (i + 1) * 1000).toISOString();
|
|
514
|
+
await getDbClient().run(
|
|
515
|
+
`INSERT INTO script_runs (id, agentId, scriptName, source, status, startedAt, finishedAt)
|
|
516
|
+
VALUES (?, ?, ?, 'x', 'completed', ?, ?)`,
|
|
517
|
+
[`${name}-${i}`, agentId, name, at, at],
|
|
518
|
+
);
|
|
519
|
+
}
|
|
520
|
+
};
|
|
521
|
+
await recent("calib-sched", 4);
|
|
522
|
+
await recent("calib-helper", 3);
|
|
523
|
+
// Script buckets refresh only on the tick, and the test above computed
|
|
524
|
+
// today moments ago: drop today's marker so the tick rescans it.
|
|
525
|
+
await getDbClient().run(
|
|
526
|
+
"DELETE FROM automation_savings_days WHERE jobKind='script' AND day=?",
|
|
527
|
+
[new Date().toISOString().slice(0, 10)],
|
|
528
|
+
);
|
|
529
|
+
await runSavingsRollupTick(inProcessSql);
|
|
530
|
+
type Read = {
|
|
531
|
+
calibrationSource: string;
|
|
532
|
+
rows: {
|
|
533
|
+
kind: string;
|
|
534
|
+
substitutedRuns: number;
|
|
535
|
+
savingUsd: { p50: number };
|
|
536
|
+
agentOnlyUsdPerRun: number;
|
|
537
|
+
}[];
|
|
538
|
+
};
|
|
539
|
+
const read = async (name: string) =>
|
|
540
|
+
(await get(`/api/savings/jobs?scriptName=${name}&days=7&tier=smart`)).body as Read;
|
|
541
|
+
// Distinct from the shipped curve at every point, including the T0 baseline
|
|
542
|
+
// that agent-invoked runs subtract.
|
|
543
|
+
const deployed = Object.fromEntries(
|
|
544
|
+
SAVINGS_TIERS.map((t) => [
|
|
545
|
+
t,
|
|
546
|
+
{ usd: CURVE[t].usd.map((v, i) => v * 3 + i * 0.01), hrs: CURVE[t].hrs },
|
|
547
|
+
]),
|
|
548
|
+
) as CurveSet;
|
|
549
|
+
const sched = (set?: CurveSet) => R.p50 * curve(T0 + K * 3, "smart", "usd", set) + F_FOLLOWUP;
|
|
550
|
+
const helper = (set?: CurveSet) =>
|
|
551
|
+
R.p50 * (curve(T0 + K * 5, "smart", "usd", set) - curve(T0, "smart", "usd", set));
|
|
552
|
+
const check = async (source: string, set?: CurveSet) => {
|
|
553
|
+
const s = await read("calib-sched");
|
|
554
|
+
const h = await read("calib-helper");
|
|
555
|
+
expect(s.calibrationSource).toBe(source);
|
|
556
|
+
expect(h.calibrationSource).toBe(source);
|
|
557
|
+
expect(s.rows[0]).toMatchObject({ kind: "script-scheduled", substitutedRuns: 4 });
|
|
558
|
+
expect(h.rows[0]).toMatchObject({ kind: "script-agent-invoked", substitutedRuns: 3 });
|
|
559
|
+
expect(s.rows[0]?.agentOnlyUsdPerRun).toBeCloseTo(sched(set), 2);
|
|
560
|
+
expect(s.rows[0]?.savingUsd.p50).toBeCloseTo(4 * sched(set), 1);
|
|
561
|
+
expect(h.rows[0]?.agentOnlyUsdPerRun).toBeCloseTo(helper(set), 2);
|
|
562
|
+
expect(h.rows[0]?.savingUsd.p50).toBeCloseTo(3 * helper(set), 1);
|
|
563
|
+
};
|
|
564
|
+
try {
|
|
565
|
+
clearCalibrationCache();
|
|
566
|
+
await check("shipped");
|
|
567
|
+
await upsertSwarmConfig({
|
|
568
|
+
scope: "global",
|
|
569
|
+
key: SAVINGS_CALIBRATION_KEY,
|
|
570
|
+
value: JSON.stringify({
|
|
571
|
+
calibratedAt: "2026-12-01",
|
|
572
|
+
model: "claude-opus-5-5",
|
|
573
|
+
curve: deployed,
|
|
574
|
+
}),
|
|
575
|
+
});
|
|
576
|
+
clearCalibrationCache();
|
|
577
|
+
await check("deployment", deployed);
|
|
578
|
+
// The script list and the daily series read the same curve.
|
|
579
|
+
const list = (await get("/api/savings/jobs?kind=script&days=7&tier=smart")).body as Read & {
|
|
580
|
+
rows: { scriptName: string }[];
|
|
581
|
+
};
|
|
582
|
+
expect(list.calibrationSource).toBe("deployment");
|
|
583
|
+
expect(list.rows.find((r) => r.scriptName === "calib-helper")?.savingUsd.p50).toBeCloseTo(
|
|
584
|
+
3 * helper(deployed),
|
|
585
|
+
1,
|
|
586
|
+
);
|
|
587
|
+
const daily = (await get("/api/savings/daily?scriptName=calib-sched&days=7&tier=smart"))
|
|
588
|
+
.body as { calibrationSource: string; points: { savingUsd: number }[] };
|
|
589
|
+
expect(daily.calibrationSource).toBe("deployment");
|
|
590
|
+
const cents = daily.points.reduce((sum, p) => sum + Math.round(p.savingUsd * 100), 0);
|
|
591
|
+
expect(cents / 100).toBeCloseTo(4 * sched(deployed), 1);
|
|
592
|
+
// The deployment curve is not the shipped one for either kind.
|
|
593
|
+
expect(sched(deployed)).toBeGreaterThan(sched() * 2);
|
|
594
|
+
expect(helper(deployed)).toBeGreaterThan(helper() * 2);
|
|
595
|
+
} finally {
|
|
596
|
+
await getDbClient().run("DELETE FROM swarm_config WHERE key = ?", [
|
|
597
|
+
SAVINGS_CALIBRATION_KEY,
|
|
598
|
+
]);
|
|
599
|
+
clearCalibrationCache();
|
|
600
|
+
}
|
|
601
|
+
});
|
|
602
|
+
|
|
603
|
+
test("bad input is rejected", async () => {
|
|
604
|
+
expect((await get("/api/savings/jobs?scriptName=nope")).status).toBe(404);
|
|
605
|
+
expect((await get("/api/savings/daily?scriptName=nope")).status).toBe(404);
|
|
606
|
+
expect((await get("/api/savings/jobs?scriptName=helper&kind=script")).status).toBe(400);
|
|
607
|
+
expect((await get("/api/savings/daily?scriptName=helper&workflowId=x")).status).toBe(400);
|
|
608
|
+
expect((await get("/api/savings/jobs?kind=task")).status).toBe(400);
|
|
609
|
+
});
|
|
610
|
+
});
|
|
611
|
+
});
|
|
@@ -691,6 +691,10 @@ 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");
|
|
696
|
+
// 293 alters agent_tasks, workflow_runs, script_runs and session_tokens.
|
|
697
|
+
markMigrationApplied(database, "293_savings_component_links.sql");
|
|
694
698
|
|
|
695
699
|
const id = crypto.randomUUID();
|
|
696
700
|
const now = new Date().toISOString();
|