@desplega.ai/agent-swarm 2.8.1 → 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.
- package/dist/{acp-adapter-pdy3qsvg.js → acp-adapter-fqzkxyra.js} +3 -3
- package/dist/{actions-pwjpn0j6.js → actions-kxek041g.js} +17 -17
- package/dist/{amp-adapter-cq9jgj6x.js → amp-adapter-ce7jjke4.js} +1 -1
- package/dist/{app-cnfffgwg.js → app-v6zz8yh0.js} +3 -3
- package/dist/{assistant-kpn317y1.js → assistant-m0sw9xx2.js} +18 -18
- package/dist/{boot-args-schema-pbbnhmc3.js → boot-args-schema-66bk37wv.js} +1 -1
- package/dist/{boot-reembed-9m0ysxma.js → boot-reembed-edjpwsc1.js} +1 -1
- package/dist/{boot-reembed-majbn53m.js → boot-reembed-y27n09tz.js} +1 -1
- package/dist/{boot-scrub-sweep-app8g8ym.js → boot-scrub-sweep-1xc35965.js} +3 -3
- package/dist/{boot-seeds-pbtmvcjm.js → boot-seeds-7hjj91qj.js} +4 -4
- package/dist/{boxd-qbkxy6ap.js → boxd-ent8ch04.js} +1 -1
- package/dist/{claude-adapter-wn1g3qp5.js → claude-adapter-stw7jdcf.js} +3 -3
- package/dist/{claude-managed-setup-pd47nzch.js → claude-managed-setup-y6z2perp.js} +1 -1
- package/dist/{claude-sdk-session-3a2a6589.js → claude-sdk-session-vvya8wqk.js} +3 -3
- package/dist/{cli-wk18cm92.js → cli-0cq8y6m7.js} +1 -1
- package/dist/{cli-p1w18yhx.js → cli-1pz0330a.js} +1 -1
- package/dist/{cli-d3p5vwbk.js → cli-1qkz151v.js} +3 -3
- package/dist/{cli-89cqnasv.js → cli-26ctbcdp.js} +18 -6
- package/dist/{cli-syybb0p1.js → cli-2kvhc7x8.js} +7 -7
- package/dist/{cli-93k32c37.js → cli-3e875j5p.js} +128 -41
- package/dist/{cli-k0615eh8.js → cli-3sy2xags.js} +10 -10
- package/dist/{cli-dy32zecz.js → cli-3vmb0n58.js} +1 -1
- package/dist/{cli-4r05hnqa.js → cli-4vzgeta4.js} +13 -13
- package/dist/{cli-b5rs97a6.js → cli-5y340nzn.js} +4 -4
- package/dist/{cli-7p1kvkck.js → cli-6rc5frdm.js} +1 -1
- package/dist/{cli-jcpr5smb.js → cli-837vfs2x.js} +2 -2
- package/dist/{cli-b085njxz.js → cli-959kp5h2.js} +12 -11
- package/dist/{cli-k93bq1wq.js → cli-97ae65rz.js} +4 -4
- package/dist/{cli-m4dqmnbp.js → cli-9mb2q6pb.js} +1 -1
- package/dist/{cli-bb431xey.js → cli-a0z912j7.js} +121 -33
- package/dist/{cli-k26bkr9t.js → cli-b98a65ed.js} +3 -3
- package/dist/{cli-k0wwfqbg.js → cli-dns0y538.js} +4 -0
- package/dist/{cli-tk7cjj8h.js → cli-dzpc6en0.js} +1 -1
- package/dist/{cli-6mma83xa.js → cli-e1p3n5gq.js} +4 -4
- package/dist/{cli-re4raaj0.js → cli-f4e8r23x.js} +1 -1
- package/dist/{cli-qmqk2yc2.js → cli-f4r3g076.js} +1 -1
- package/dist/{cli-pcad4cb1.js → cli-f6bahpn5.js} +1 -1
- package/dist/{cli-b8e9nb04.js → cli-fpjmegdb.js} +1 -1
- package/dist/{cli-0ntbxyaa.js → cli-fwy8kfd6.js} +271 -37
- package/dist/{cli-tacge1w0.js → cli-g5mtmf6t.js} +6 -6
- package/dist/{cli-sn7rtnj4.js → cli-gyt3zydq.js} +3 -3
- package/dist/{cli-vbh10fqa.js → cli-j24zbska.js} +3 -3
- package/dist/{cli-vqptjbr7.js → cli-jdknh18m.js} +10 -10
- package/dist/{cli-7b8sbmag.js → cli-kjtwrhea.js} +1 -1
- package/dist/{cli-9vhebtjz.js → cli-mxn1qvbn.js} +3 -2
- package/dist/{cli-zcsw143p.js → cli-n6vtrcht.js} +1 -1
- package/dist/{cli-2zp0et3v.js → cli-pchypa1s.js} +55 -19
- package/dist/{cli-gax7fk3t.js → cli-pfs8adn4.js} +1 -1
- package/dist/{cli-1xh9a7hn.js → cli-q24n86d2.js} +2 -2
- package/dist/{cli-7w69ne2f.js → cli-rcecc3yq.js} +167 -43
- package/dist/{cli-xae4g51r.js → cli-rh15cakt.js} +1 -1
- package/dist/{cli-yn2bbre9.js → cli-rmzsadkf.js} +1 -1
- package/dist/{cli-a3b239vq.js → cli-t2tma8gb.js} +1 -1
- package/dist/{cli-9wngrawf.js → cli-tekytdq9.js} +2 -2
- package/dist/{cli-tqegb8eq.js → cli-vdektqgf.js} +1 -1
- package/dist/{cli-cyhbbd1z.js → cli-w401b9tq.js} +1 -1
- package/dist/{cli-jywyhqws.js → cli-xf3qw745.js} +3 -3
- package/dist/{cli-hxpyc9sn.js → cli-z1w943va.js} +2 -2
- package/dist/{cli-yncmerje.js → cli-zh9rsbqf.js} +2 -2
- package/dist/cli.js +16 -16
- package/dist/{codex-adapter-nha71q3n.js → codex-adapter-zerf5qsp.js} +1 -1
- package/dist/{codex-hook-h3vk6xzr.js → codex-hook-30gyhzxe.js} +1 -1
- package/dist/{codex-session-runner-ak9x1k1h.js → codex-session-runner-89s304py.js} +1 -1
- package/dist/{commands-f587qpq8.js → commands-jeh1b51j.js} +3 -3
- package/dist/{controller-hxx8wwy1.js → controller-mctfbrse.js} +3 -3
- package/dist/{controllers-connect-k5sg9m9h.js → controllers-connect-nxa1ddwn.js} +18 -18
- package/dist/{cursor-adapter-n5nc3mfe.js → cursor-adapter-qj8vehv6.js} +1 -1
- package/dist/{db-nzf8zb5z.js → db-2dvay0es.js} +3 -3
- package/dist/{db-amm8w15a.js → db-b36qn6k3.js} +2 -2
- package/dist/{dispatcher-89zrts8f.js → dispatcher-gndbta01.js} +16 -16
- package/dist/{dsh-adapter-nrhbrjz8.js → dsh-adapter-f75sz821.js} +1 -1
- package/dist/{e2b-jkwfhbcv.js → e2b-3gpc2n3w.js} +1 -1
- package/dist/{e2b-s16txnpx.js → e2b-jbm7mj93.js} +1 -1
- package/dist/{embedding-space-ezwenjmj.js → embedding-space-4p05vb2x.js} +6 -6
- package/dist/{embeddings-np74ht6g.js → embeddings-m6768mgh.js} +1 -1
- package/dist/{external-channel-z46yx9d7.js → external-channel-gjv3jne3.js} +2 -2
- package/dist/{fx-adapter-3q12snn6.js → fx-adapter-rpsgb1tw.js} +4 -4
- package/dist/{grok-adapter-ahb1w033.js → grok-adapter-n2m3zjg1.js} +4 -4
- package/dist/handlers-nv8edn19.js +32 -0
- package/dist/{heartbeat-dt9qw9c8.js → heartbeat-a1tv60gt.js} +19 -19
- package/dist/{hook-8k6fbz9b.js → hook-8h5dzcgq.js} +2 -2
- package/dist/{hook-jgcgygex.js → hook-cyaps9ba.js} +4 -4
- package/dist/{http-haw4r8ne.js → http-t15mhbd9.js} +1731 -77
- package/dist/{index-s4ewdma4.js → index-ksmyd6kc.js} +16 -16
- package/dist/{index-tez78qj3.js → index-qav6455f.js} +13 -11
- package/dist/index-rjnrje98.js +32 -0
- package/dist/index-tv3413wy.js +33 -0
- package/dist/{index-5gk12my8.js → index-w1zqf680.js} +1 -1
- package/dist/{keepalive-xn6s7rmg.js → keepalive-91s7hzrb.js} +3 -3
- package/dist/{lead-6ygbfhbh.js → lead-3hmddvrm.js} +30 -30
- package/dist/{maintenance-vdts800b.js → maintenance-7pjmy5xy.js} +1 -1
- package/dist/{oauth-refresh-sweep-094c8wfk.js → oauth-refresh-sweep-rzqfm94j.js} +2 -2
- package/dist/{onboard-v99eh56m.js → onboard-74st7pag.js} +3 -3
- package/dist/{opencode-adapter-dsagkhtw.js → opencode-adapter-arn7gm8q.js} +2 -2
- package/dist/{otel-impl-q7yr9yp0.js → otel-impl-rnzey8cb.js} +1 -1
- package/dist/{pi-mono-adapter-546cd1cc.js → pi-mono-adapter-6mkwq64n.js} +3 -2
- package/dist/{pricing-refresh-7dh2yase.js → pricing-refresh-ha21jhk7.js} +2 -2
- package/dist/{rbac-roles-npbmdh77.js → rbac-roles-59dxachk.js} +2 -2
- package/dist/{rbac-roles-mdmc96p4.js → rbac-roles-wqhma4dr.js} +3 -3
- package/dist/{render-v2-twhhwmms.js → render-v2-14kpzx12.js} +3 -3
- package/dist/{resume-5gsrjd5e.js → resume-gsa5fb5f.js} +6 -6
- package/dist/{session-access-m191x30x.js → session-access-skm7bnp6.js} +4 -4
- package/dist/{session-source-backfill-nt923ask.js → session-source-backfill-pbqzbncv.js} +2 -2
- package/dist/{setup-wv467afy.js → setup-5yermsem.js} +1 -1
- package/dist/{work-objects-8zkrpy13.js → work-objects-xj0ck0w4.js} +19 -19
- package/dist/{worker-follow-up-cecqszem.js → worker-follow-up-911xc0b8.js} +18 -16
- package/dist/{worker-mz5z7kj0.js → worker-qgg0m2gw.js} +30 -30
- package/openapi.json +1741 -290
- package/package.json +2 -2
- 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 +20 -0
- package/src/be/db/workflow-run-definitions.ts +19 -8
- package/src/be/db-client.ts +43 -5
- package/src/be/db.ts +9 -3
- package/src/be/migrations/284_model_profile_fallbacks.sql +9 -0
- package/src/be/model-profile-check.ts +43 -18
- package/src/be/runtime-pools/routing.ts +1 -0
- package/src/be/savings/calibrate.ts +468 -0
- package/src/be/savings/compute.ts +182 -0
- package/src/be/savings/model.ts +406 -0
- package/src/be/scripts/typecheck.ts +5 -0
- package/src/be/seed/agent-fs-provision.ts +48 -11
- package/src/be/seed/workflows-seeder.ts +67 -23
- package/src/be/skill-parser.ts +19 -7
- package/src/commands/onboard/agent-fs-version.ts +1 -1
- package/src/commands/provider-error-class.ts +97 -0
- package/src/commands/runner.ts +99 -17
- 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 +4 -0
- package/src/http/model-profiles.ts +43 -10
- package/src/http/poll.ts +1 -0
- package/src/http/savings-calibration.ts +277 -0
- package/src/http/savings.ts +363 -0
- package/src/http/tasks.ts +46 -4
- 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/providers/amp-adapter.ts +1 -1
- package/src/providers/codex-adapter.ts +3 -0
- package/src/providers/pi-mono-adapter.ts +6 -5
- package/src/providers/types.ts +7 -0
- package/src/scripts-runtime/sdk-allowlist.ts +11 -1
- package/src/scripts-runtime/swarm-sdk.ts +6 -0
- package/src/scripts-runtime/types/stdlib.d.ts +9 -0
- package/src/scripts-runtime/types/swarm-extension.d.ts +34 -0
- package/src/scripts-runtime/types/swarm-sdk.d.ts +9 -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/chat-sdk-relay.test.ts +12 -0
- package/src/tests/codex-adapter.test.ts +11 -0
- package/src/tests/comb-default.test.ts +6 -0
- package/src/tests/core-auth.test.ts +7 -6
- package/src/tests/db-client-busy-retry.test.ts +58 -6
- package/src/tests/deploy-guard.test.ts +154 -0
- package/src/tests/docs-index-footprint.test.js +3 -3
- 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/pi-mono-session-cost-on-abort.test.ts +115 -0
- package/src/tests/provider-error-class.test.ts +174 -0
- package/src/tests/runner-provider-fallback.test.ts +197 -0
- package/src/tests/savings-calibration-api.test.ts +264 -0
- package/src/tests/savings-calibration.test.ts +201 -0
- package/src/tests/savings-compute.test.ts +406 -0
- package/src/tests/savings-model.test.ts +287 -0
- package/src/tests/script-connections.test.ts +2 -0
- package/src/tests/sdk-allowlist.test.ts +40 -0
- package/src/tests/seed-workflows.test.ts +167 -11
- package/src/tests/skill-parser.test.ts +45 -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 +115 -33
- package/src/utils/constants.ts +2 -0
- 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/dist/handlers-vbww69re.js +0 -32
- package/dist/index-4xwaf5th.js +0 -32
- package/dist/index-f0hcrs8e.js +0 -33
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import {
|
|
3
|
+
compareToShipped,
|
|
4
|
+
type DeriveInput,
|
|
5
|
+
deriveCurves,
|
|
6
|
+
MIN_SESSIONS,
|
|
7
|
+
type OpusSession,
|
|
8
|
+
type TierPrices,
|
|
9
|
+
trimmedMean,
|
|
10
|
+
} from "../be/savings/calibrate";
|
|
11
|
+
import {
|
|
12
|
+
CURVE,
|
|
13
|
+
parseCalibration,
|
|
14
|
+
priceWorkflow,
|
|
15
|
+
SAVINGS_TIERS,
|
|
16
|
+
type SavingsTier,
|
|
17
|
+
type WorkflowFacts,
|
|
18
|
+
} from "../be/savings/model";
|
|
19
|
+
|
|
20
|
+
const PRICES: Record<SavingsTier, TierPrices> = {
|
|
21
|
+
smol: { input: 0.1, output: 0.5, cached_input: 0.01, cache_write_1h: 0.2 },
|
|
22
|
+
regular: { input: 2, output: 10, cached_input: 0.1, cache_write_1h: 4 },
|
|
23
|
+
smart: { input: 4, output: 20, cached_input: 0.2, cache_write_1h: 8 },
|
|
24
|
+
ultra: { input: 10, output: 50, cached_input: 0.25, cache_write_1h: 20 },
|
|
25
|
+
};
|
|
26
|
+
const OTHER = 0.001;
|
|
27
|
+
const costAt = (s: Omit<OpusSession, "costUsd" | "otherUsd">, p: TierPrices) =>
|
|
28
|
+
(s.inputTokens * p.input +
|
|
29
|
+
s.outputTokens * p.output +
|
|
30
|
+
s.cacheReadTokens * p.cached_input +
|
|
31
|
+
s.cacheWriteTokens * p.cache_write_1h) /
|
|
32
|
+
1e6;
|
|
33
|
+
|
|
34
|
+
/** `per` sessions at each of `from`..`to` turns whose tokens grow linearly with the turn count. */
|
|
35
|
+
function sessions(per: number, outlier = true, from = 1, to = 61): OpusSession[] {
|
|
36
|
+
const out: OpusSession[] = [];
|
|
37
|
+
for (let turns = from; turns <= to; turns++) {
|
|
38
|
+
for (let k = 0; k < per; k++) {
|
|
39
|
+
const base = {
|
|
40
|
+
turns,
|
|
41
|
+
durationMs: 3_000 * turns,
|
|
42
|
+
inputTokens: 10,
|
|
43
|
+
outputTokens: 300 * turns,
|
|
44
|
+
cacheReadTokens: 20_000 * turns,
|
|
45
|
+
cacheWriteTokens: 5_000 + 100 * turns,
|
|
46
|
+
};
|
|
47
|
+
// One runaway session per turn count: the trimmed mean must ignore it.
|
|
48
|
+
const blowup = outlier && k === 0 ? 50 : 1;
|
|
49
|
+
const tokens = { ...base, cacheReadTokens: base.cacheReadTokens * blowup };
|
|
50
|
+
out.push({ ...tokens, costUsd: costAt(tokens, PRICES.smart), otherUsd: OTHER });
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return out;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const input = (opus: OpusSession[], tierTurns: DeriveInput["tierTurns"] = {}): DeriveInput => ({
|
|
57
|
+
opus,
|
|
58
|
+
prices: PRICES,
|
|
59
|
+
tierTurns,
|
|
60
|
+
end: new Date("2026-12-01T10:00:00Z"),
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
describe("savings calibration", () => {
|
|
64
|
+
test("recovers a known linear cost curve per tier, ignoring outlier sessions", () => {
|
|
65
|
+
const out = deriveCurves(input(sessions(20)));
|
|
66
|
+
for (const tier of SAVINGS_TIERS) {
|
|
67
|
+
for (let turns = 2; turns <= 61; turns++) {
|
|
68
|
+
const clean = {
|
|
69
|
+
inputTokens: 10,
|
|
70
|
+
outputTokens: 300 * turns,
|
|
71
|
+
cacheReadTokens: 20_000 * turns,
|
|
72
|
+
cacheWriteTokens: 5_000 + 100 * turns,
|
|
73
|
+
};
|
|
74
|
+
const truth = costAt(clean, PRICES[tier]) + OTHER;
|
|
75
|
+
expect(out.curve[tier].usd[turns - 1] as number).toBeCloseTo(truth, 3);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
expect(out.repriceMatch).toBe(1);
|
|
79
|
+
expect(out.calibratedAt).toBe("2026-12-01");
|
|
80
|
+
// The result is what the API stores: it must parse.
|
|
81
|
+
expect(
|
|
82
|
+
parseCalibration(
|
|
83
|
+
JSON.stringify({ calibratedAt: out.calibratedAt, model: out.model, curve: out.curve }),
|
|
84
|
+
),
|
|
85
|
+
).not.toBeNull();
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
test("tier hours are opus hours times the tier's measured speed; a thin tier uses the fallback", () => {
|
|
89
|
+
// Opus runs 3 s a turn; haiku 4.5 s (1.5x), 40 sessions. Sonnet has 2 sessions: fallback.
|
|
90
|
+
const haiku = Array.from({ length: 40 }, () => ({ turns: 10, durationMs: 45_000 }));
|
|
91
|
+
const sonnet = [
|
|
92
|
+
{ turns: 10, durationMs: 100_000 },
|
|
93
|
+
{ turns: 5, durationMs: 100_000 },
|
|
94
|
+
];
|
|
95
|
+
const out = deriveCurves(input(sessions(20), { smol: haiku, regular: sonnet }));
|
|
96
|
+
expect(out.speed).toEqual({ smol: 1.5, regular: 1.29, smart: 1, ultra: 1 });
|
|
97
|
+
expect(out.curve.smol.hrs[19]).toBeCloseTo((out.curve.smart.hrs[19] as number) * 1.5, 4);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
test("a curve is non-decreasing even when a turn count is noisy", () => {
|
|
101
|
+
const noisy = sessions(20, false).map((s) =>
|
|
102
|
+
s.turns === 30 ? { ...s, cacheReadTokens: 1, outputTokens: 1 } : s,
|
|
103
|
+
);
|
|
104
|
+
const { smart } = deriveCurves(input(noisy)).curve;
|
|
105
|
+
smart.usd.forEach((v, i) => {
|
|
106
|
+
if (i > 0) expect(v).toBeGreaterThanOrEqual(smart.usd[i - 1] as number);
|
|
107
|
+
});
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
test("negative control: too few sessions refuses to calibrate", () => {
|
|
111
|
+
expect(() => deriveCurves(input(sessions(20).slice(0, MIN_SESSIONS - 1)))).toThrow(
|
|
112
|
+
/only \d+ opus sessions/,
|
|
113
|
+
);
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
// Dollar figures below are the session curve's own unit, before R, the review cost and the error band.
|
|
117
|
+
test("negative control: 300 five-turn sessions do not become a flat curve", () => {
|
|
118
|
+
// Without a coverage rule this prices a 2-turn job like a 5-turn one (the 5-turn point at every turn).
|
|
119
|
+
const fiveTurn = sessions(300, false, 5, 5);
|
|
120
|
+
expect(fiveTurn).toHaveLength(300);
|
|
121
|
+
expect(() => deriveCurves(input(fiveTurn))).toThrow(/turn coverage too thin/);
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
test("refuses a window that misses the bottom or the top of the turn range", () => {
|
|
125
|
+
expect(() => deriveCurves(input(sessions(20, false, 6, 61)))).toThrow(
|
|
126
|
+
/reaching turn 3 or lower/,
|
|
127
|
+
);
|
|
128
|
+
expect(() => deriveCurves(input(sessions(40, false, 2, 19)))).toThrow(/turn 20 or higher/);
|
|
129
|
+
// 7 supported turn counts spanning 2..30: both ends reached, too few points between them.
|
|
130
|
+
const gappy = sessions(60, false, 2, 30).filter(
|
|
131
|
+
(s) => s.turns === 2 || s.turns === 30 || s.turns % 5 === 0,
|
|
132
|
+
);
|
|
133
|
+
expect(() => deriveCurves(input(gappy))).toThrow(/need at least 15/);
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
test("past the data the shipped shape is used, scaled to the last supported turn", () => {
|
|
137
|
+
const out = deriveCurves(input(sessions(20, false, 2, 30)));
|
|
138
|
+
for (const tier of SAVINGS_TIERS) {
|
|
139
|
+
const usd = out.curve[tier].usd;
|
|
140
|
+
const scale = (usd[29] as number) / CURVE[tier].usd[29];
|
|
141
|
+
for (const turns of [31, 45, 61]) {
|
|
142
|
+
expect(usd[turns - 1] as number).toBeCloseTo(CURVE[tier].usd[turns - 1] * scale, 3);
|
|
143
|
+
}
|
|
144
|
+
// Not flat: the tail keeps climbing.
|
|
145
|
+
expect(usd[60] as number).toBeGreaterThan((usd[29] as number) * 1.5);
|
|
146
|
+
}
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
test("below the data the shipped shape is used, scaled to the first supported turn", () => {
|
|
150
|
+
const out = deriveCurves(input(sessions(20, false, 3, 61)));
|
|
151
|
+
for (const tier of SAVINGS_TIERS) {
|
|
152
|
+
const usd = out.curve[tier].usd;
|
|
153
|
+
const scale = (usd[2] as number) / CURVE[tier].usd[2];
|
|
154
|
+
expect(usd[1] as number).toBeCloseTo(CURVE[tier].usd[1] * scale, 3);
|
|
155
|
+
expect(usd[1] as number).toBeLessThan(usd[2] as number);
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
test("a sparse-tail curve still prices more ops higher, past the data too", () => {
|
|
160
|
+
const out = deriveCurves(input(sessions(20, false, 2, 30)));
|
|
161
|
+
const facts = (opsSum: number): WorkflowFacts => ({
|
|
162
|
+
runs: 100,
|
|
163
|
+
schedRuns: 0,
|
|
164
|
+
schedEffectRuns: 0,
|
|
165
|
+
countedRuns: 100,
|
|
166
|
+
opsSum,
|
|
167
|
+
agentTasks: 0,
|
|
168
|
+
agentUsd: 0,
|
|
169
|
+
followupUsd: 0,
|
|
170
|
+
agentMs: 0,
|
|
171
|
+
recordedLlmUsd: 0,
|
|
172
|
+
estimatedLlmUsd: 0,
|
|
173
|
+
computeSecs: 0,
|
|
174
|
+
});
|
|
175
|
+
const perRun = (ops: number) =>
|
|
176
|
+
priceWorkflow(facts(ops * 100), { tier: "smart", days: 30, band: "job", curve: out.curve })
|
|
177
|
+
.agentOnlyUsdPerRun;
|
|
178
|
+
expect(perRun(1)).toBeLessThan(perRun(60));
|
|
179
|
+
expect(perRun(120)).toBeGreaterThan(perRun(60));
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
test("compareToShipped is 1.0 on the shipped curve and flags a doubled one", () => {
|
|
183
|
+
const weights = Array.from({ length: 61 }, () => 1);
|
|
184
|
+
for (const c of compareToShipped(CURVE, weights)) {
|
|
185
|
+
expect(c.weighted).toBe(1);
|
|
186
|
+
expect(c.over5pct).toBe(0);
|
|
187
|
+
}
|
|
188
|
+
const doubled = Object.fromEntries(
|
|
189
|
+
SAVINGS_TIERS.map((t) => [t, { usd: CURVE[t].usd.map((v) => v * 2), hrs: CURVE[t].hrs }]),
|
|
190
|
+
) as typeof CURVE;
|
|
191
|
+
const usd = compareToShipped(doubled, weights).filter((c) => c.key === "usd");
|
|
192
|
+
for (const c of usd) {
|
|
193
|
+
expect(c.weighted).toBeCloseTo(2, 6);
|
|
194
|
+
expect(c.over5pct).toBe(60);
|
|
195
|
+
}
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
test("trimmedMean drops the extremes", () => {
|
|
199
|
+
expect(trimmedMean([1, 2, 3, 4, 5, 6, 7, 8, 9, 1000])).toBe(5.5);
|
|
200
|
+
});
|
|
201
|
+
});
|
|
@@ -0,0 +1,406 @@
|
|
|
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
|
+
createSessionCost,
|
|
8
|
+
createTaskExtended,
|
|
9
|
+
createWorkflow,
|
|
10
|
+
createWorkflowRun,
|
|
11
|
+
createWorkflowRunStep,
|
|
12
|
+
getDbClient,
|
|
13
|
+
initDb,
|
|
14
|
+
upsertSwarmConfig,
|
|
15
|
+
} from "../be/db";
|
|
16
|
+
import { computeWorkflowWindow, LIVE_MAX_RUNS } from "../be/savings/compute";
|
|
17
|
+
import { CURVE, DEFAULT_OPS, SAVINGS_CALIBRATION_KEY, SAVINGS_TIERS } from "../be/savings/model";
|
|
18
|
+
import { handleSavings, resetSavingsCache } from "../http/savings";
|
|
19
|
+
import { getPathSegments, parseQueryParams } from "../http/utils";
|
|
20
|
+
import { listenOnFreePort } from "./test-net";
|
|
21
|
+
|
|
22
|
+
const TEST_DB_PATH = "./test-savings-compute.sqlite";
|
|
23
|
+
const WINDOW = { start: "2026-10-01T00:00:00.000Z", end: "2026-10-31T00:00:00.000Z" };
|
|
24
|
+
const DEFINITION = {
|
|
25
|
+
nodes: [
|
|
26
|
+
{ id: "fetch", type: "script", config: { script: "await a(); await b(); await c();" } },
|
|
27
|
+
{ id: "ping", type: "notify", config: {} },
|
|
28
|
+
{ id: "sw", type: "swarm-script", config: { scriptName: "not-in-catalog" } },
|
|
29
|
+
{ id: "agent", type: "agent-task", config: {} },
|
|
30
|
+
],
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
// The preload swaps file DBs for an in-memory template copy; the route's
|
|
34
|
+
// bounded executor reads the DB file from a child process, so opt out here.
|
|
35
|
+
const testGlobals = globalThis as typeof globalThis & {
|
|
36
|
+
__testMigrationTemplate?: Uint8Array;
|
|
37
|
+
__savedSavingsTemplate?: Uint8Array;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
async function removeDbFiles(path: string): Promise<void> {
|
|
41
|
+
for (const suffix of ["", "-wal", "-shm"]) {
|
|
42
|
+
try {
|
|
43
|
+
await unlink(path + suffix);
|
|
44
|
+
} catch (error) {
|
|
45
|
+
if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
describe("savings compute", () => {
|
|
51
|
+
let agentId: string;
|
|
52
|
+
let workflowId: string;
|
|
53
|
+
let emptyWorkflowId: string;
|
|
54
|
+
let busyWorkflowId: string;
|
|
55
|
+
let server: Server;
|
|
56
|
+
let port: number;
|
|
57
|
+
|
|
58
|
+
async function run(opts: {
|
|
59
|
+
trigger: "schedule" | "manual";
|
|
60
|
+
status: string;
|
|
61
|
+
startedAt: string;
|
|
62
|
+
steps: string[];
|
|
63
|
+
}): Promise<string> {
|
|
64
|
+
const id = crypto.randomUUID();
|
|
65
|
+
await createWorkflowRun({ id, workflowId, triggerType: opts.trigger });
|
|
66
|
+
await getDbClient().run("UPDATE workflow_runs SET status=?, startedAt=? WHERE id=?", [
|
|
67
|
+
opts.status,
|
|
68
|
+
opts.startedAt,
|
|
69
|
+
id,
|
|
70
|
+
]);
|
|
71
|
+
for (const nodeId of opts.steps) {
|
|
72
|
+
const node = DEFINITION.nodes.find((n) => n.id === nodeId);
|
|
73
|
+
const stepId = crypto.randomUUID();
|
|
74
|
+
await createWorkflowRunStep({ id: stepId, runId: id, nodeId, nodeType: node?.type ?? "" });
|
|
75
|
+
// Every step lasts 2 s, except the agent step (10 s, which is not compute time).
|
|
76
|
+
const secs = nodeId === "agent" ? 10 : 2;
|
|
77
|
+
const finished = new Date(Date.parse(opts.startedAt) + secs * 1000).toISOString();
|
|
78
|
+
await getDbClient().run(
|
|
79
|
+
"UPDATE workflow_run_steps SET status='completed', startedAt=?, finishedAt=? WHERE id=?",
|
|
80
|
+
[opts.startedAt, finished, stepId],
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
return id;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
async function agentTask(runId: string, usd: number, ms: number): Promise<string> {
|
|
87
|
+
const task = await createTaskExtended(`savings fixture ${usd}`, {
|
|
88
|
+
agentId,
|
|
89
|
+
source: "workflow",
|
|
90
|
+
workflowRunId: runId,
|
|
91
|
+
});
|
|
92
|
+
await createSessionCost({
|
|
93
|
+
sessionId: crypto.randomUUID(),
|
|
94
|
+
taskId: task.id,
|
|
95
|
+
agentId,
|
|
96
|
+
totalCostUsd: usd,
|
|
97
|
+
durationMs: ms,
|
|
98
|
+
numTurns: 3,
|
|
99
|
+
model: "claude-opus-5-5",
|
|
100
|
+
});
|
|
101
|
+
return task.id;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
beforeAll(async () => {
|
|
105
|
+
testGlobals.__savedSavingsTemplate = testGlobals.__testMigrationTemplate;
|
|
106
|
+
testGlobals.__testMigrationTemplate = undefined;
|
|
107
|
+
await removeDbFiles(TEST_DB_PATH);
|
|
108
|
+
initDb(TEST_DB_PATH);
|
|
109
|
+
agentId = (await createAgent({ name: "savings-agent", isLead: false, status: "idle" })).id;
|
|
110
|
+
workflowId = (await createWorkflow({ name: "savings-fixture", definition: DEFINITION })).id;
|
|
111
|
+
emptyWorkflowId = (await createWorkflow({ name: "savings-empty", definition: DEFINITION })).id;
|
|
112
|
+
busyWorkflowId = (await createWorkflow({ name: "savings-busy", definition: DEFINITION })).id;
|
|
113
|
+
|
|
114
|
+
// Three scheduled runs; the first created an agent task (an effect run).
|
|
115
|
+
const s1 = await run({
|
|
116
|
+
trigger: "schedule",
|
|
117
|
+
status: "completed",
|
|
118
|
+
startedAt: "2026-10-02T10:00:00.000Z",
|
|
119
|
+
steps: ["fetch", "ping"],
|
|
120
|
+
});
|
|
121
|
+
await agentTask(s1, 1, 1_800_000);
|
|
122
|
+
for (const startedAt of ["2026-10-03T10:00:00.000Z", "2026-10-04T10:00:00.000Z"]) {
|
|
123
|
+
await run({ trigger: "schedule", status: "completed", startedAt, steps: ["fetch", "ping"] });
|
|
124
|
+
}
|
|
125
|
+
// One manual run with an agent node, its session and a lead follow-up review.
|
|
126
|
+
const m = await run({
|
|
127
|
+
trigger: "manual",
|
|
128
|
+
status: "completed",
|
|
129
|
+
startedAt: "2026-10-05T10:00:00.000Z",
|
|
130
|
+
steps: ["fetch", "sw", "agent"],
|
|
131
|
+
});
|
|
132
|
+
const worker = await agentTask(m, 2, 3_600_000);
|
|
133
|
+
const followUp = await createTaskExtended("Worker task completed: savings fixture", {
|
|
134
|
+
agentId,
|
|
135
|
+
parentTaskId: worker,
|
|
136
|
+
});
|
|
137
|
+
await createSessionCost({
|
|
138
|
+
sessionId: crypto.randomUUID(),
|
|
139
|
+
taskId: followUp.id,
|
|
140
|
+
agentId,
|
|
141
|
+
totalCostUsd: 0.5,
|
|
142
|
+
durationMs: 60_000,
|
|
143
|
+
numTurns: 1,
|
|
144
|
+
model: "claude-opus-5-5",
|
|
145
|
+
});
|
|
146
|
+
// A skipped run counts as a run but substitutes nothing.
|
|
147
|
+
await run({
|
|
148
|
+
trigger: "schedule",
|
|
149
|
+
status: "skipped",
|
|
150
|
+
startedAt: "2026-10-06T10:00:00.000Z",
|
|
151
|
+
steps: [],
|
|
152
|
+
});
|
|
153
|
+
// Outside the window: ignored.
|
|
154
|
+
await run({
|
|
155
|
+
trigger: "manual",
|
|
156
|
+
status: "completed",
|
|
157
|
+
startedAt: "2026-09-20T10:00:00.000Z",
|
|
158
|
+
steps: ["fetch"],
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
// More runs than the live path handles.
|
|
162
|
+
await getDbClient().run(
|
|
163
|
+
`INSERT INTO workflow_runs (id, workflowId, triggerType, startedAt)
|
|
164
|
+
WITH RECURSIVE seq(i) AS (SELECT 1 UNION ALL SELECT i + 1 FROM seq WHERE i < ?)
|
|
165
|
+
SELECT 'busy-' || i, ?, 'event', '2026-10-10T00:00:00.000Z' FROM seq`,
|
|
166
|
+
[LIVE_MAX_RUNS + 1, busyWorkflowId],
|
|
167
|
+
);
|
|
168
|
+
|
|
169
|
+
server = createHttpServer(async (req, res) => {
|
|
170
|
+
const handled = await handleSavings(
|
|
171
|
+
req,
|
|
172
|
+
res,
|
|
173
|
+
getPathSegments(req.url || ""),
|
|
174
|
+
parseQueryParams(req.url || ""),
|
|
175
|
+
);
|
|
176
|
+
if (!handled) {
|
|
177
|
+
res.writeHead(404);
|
|
178
|
+
res.end();
|
|
179
|
+
}
|
|
180
|
+
});
|
|
181
|
+
port = await listenOnFreePort(server);
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
afterAll(async () => {
|
|
185
|
+
server?.close();
|
|
186
|
+
closeDb();
|
|
187
|
+
testGlobals.__testMigrationTemplate = testGlobals.__savedSavingsTemplate;
|
|
188
|
+
testGlobals.__savedSavingsTemplate = undefined;
|
|
189
|
+
await removeDbFiles(TEST_DB_PATH);
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
test("facts match the hand-computed fixture", async () => {
|
|
193
|
+
const out = await computeWorkflowWindow({ id: workflowId, definition: DEFINITION }, WINDOW);
|
|
194
|
+
expect(out.status).toBe("ok");
|
|
195
|
+
if (out.status !== "ok") return;
|
|
196
|
+
expect(out.facts).toEqual({
|
|
197
|
+
runs: 5,
|
|
198
|
+
countedRuns: 4,
|
|
199
|
+
schedRuns: 3,
|
|
200
|
+
schedEffectRuns: 1,
|
|
201
|
+
// 3 scheduled runs x (fetch 3 + ping 1) + manual (fetch 3 + sw DEFAULT_OPS + agent 0)
|
|
202
|
+
opsSum: 3 * 4 + 3 + DEFAULT_OPS,
|
|
203
|
+
agentTasks: 2,
|
|
204
|
+
agentUsd: 3,
|
|
205
|
+
agentMs: 5_400_000,
|
|
206
|
+
followupUsd: 0.5,
|
|
207
|
+
recordedLlmUsd: 0,
|
|
208
|
+
estimatedLlmUsd: 0,
|
|
209
|
+
// 6 scheduled steps + fetch + sw, 2 s each; the agent step is excluded.
|
|
210
|
+
computeSecs: expect.closeTo(16, 3) as unknown as number,
|
|
211
|
+
});
|
|
212
|
+
// One bucket per UTC start day; the skipped run still counts as a run.
|
|
213
|
+
expect(out.byDay.map((d) => [d.day, d.runs, d.countedRuns, d.schedEffectRuns])).toEqual([
|
|
214
|
+
["2026-10-02", 1, 1, 1],
|
|
215
|
+
["2026-10-03", 1, 1, 0],
|
|
216
|
+
["2026-10-04", 1, 1, 0],
|
|
217
|
+
["2026-10-05", 1, 1, 0],
|
|
218
|
+
["2026-10-06", 1, 0, 0],
|
|
219
|
+
]);
|
|
220
|
+
expect(out.byDay[0]).toMatchObject({ agentTasks: 1, agentUsd: 1, opsSum: 4 });
|
|
221
|
+
expect(out.byDay[3]).toMatchObject({ agentTasks: 1, agentUsd: 2, followupUsd: 0.5 });
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
test("negative control: an unknown workflow id has no runs", async () => {
|
|
225
|
+
const out = await computeWorkflowWindow({ id: "no-such-workflow", definition: {} }, WINDOW);
|
|
226
|
+
expect(out).toEqual({ status: "ok", facts: expect.objectContaining({ runs: 0 }), byDay: [] });
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
test("a workflow above the live limit is refused before any step scan", async () => {
|
|
230
|
+
const out = await computeWorkflowWindow({ id: busyWorkflowId, definition: DEFINITION }, WINDOW);
|
|
231
|
+
expect(out).toEqual({ status: "too-many-runs", runs: LIVE_MAX_RUNS + 1 });
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
describe("GET /api/savings/jobs and /api/savings/daily", () => {
|
|
235
|
+
const get = async (query: string, path = "jobs") => {
|
|
236
|
+
resetSavingsCache();
|
|
237
|
+
const res = await fetch(`http://127.0.0.1:${port}/api/savings/${path}?${query}`);
|
|
238
|
+
return { status: res.status, body: (await res.json().catch(() => null)) as never };
|
|
239
|
+
};
|
|
240
|
+
|
|
241
|
+
test("a workflow with no runs in the window is not valid", async () => {
|
|
242
|
+
const { status, body } = await get(`workflowId=${emptyWorkflowId}`);
|
|
243
|
+
expect(status).toBe(200);
|
|
244
|
+
expect(body).toMatchObject({
|
|
245
|
+
valid: false,
|
|
246
|
+
reason: "no-runs",
|
|
247
|
+
rows: [],
|
|
248
|
+
tier: "smart",
|
|
249
|
+
calibrationModel: "claude-opus-5-5",
|
|
250
|
+
window: { days: 30 },
|
|
251
|
+
});
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
test("too many runs is reported, never as $0", async () => {
|
|
255
|
+
const { body } = await get(`workflowId=${busyWorkflowId}&days=90`);
|
|
256
|
+
expect(body).toMatchObject({ valid: false, reason: "too-many-runs", rows: [] });
|
|
257
|
+
const daily = await get(`workflowId=${busyWorkflowId}&days=90`, "daily");
|
|
258
|
+
expect(daily.body).toMatchObject({ valid: false, reason: "too-many-runs", points: [] });
|
|
259
|
+
const idle = await get(`workflowId=${emptyWorkflowId}`, "daily");
|
|
260
|
+
expect(idle.body).toMatchObject({ valid: false, reason: "no-runs", points: [] });
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
test("unknown workflow is 404; bad tier, days or a missing id is 400", async () => {
|
|
264
|
+
expect((await get("workflowId=nope")).status).toBe(404);
|
|
265
|
+
expect((await get(`workflowId=${workflowId}&tier=huge`)).status).toBe(400);
|
|
266
|
+
expect((await get(`workflowId=${workflowId}&days=12`)).status).toBe(400);
|
|
267
|
+
expect((await get("")).status).toBe(400);
|
|
268
|
+
expect((await get("workflowId=nope", "daily")).status).toBe(404);
|
|
269
|
+
expect((await get(`workflowId=${workflowId}&tier=huge`, "daily")).status).toBe(400);
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
test("scans run in the bounded executor: a spent budget is a 408, not a blocked API", async () => {
|
|
273
|
+
const saved = process.env.DB_QUERY_HTTP_BUDGET_MS;
|
|
274
|
+
process.env.DB_QUERY_HTTP_BUDGET_MS = "1";
|
|
275
|
+
try {
|
|
276
|
+
expect((await get(`workflowId=${workflowId}`)).status).toBe(408);
|
|
277
|
+
} finally {
|
|
278
|
+
if (saved === undefined) delete process.env.DB_QUERY_HTTP_BUDGET_MS;
|
|
279
|
+
else process.env.DB_QUERY_HTTP_BUDGET_MS = saved;
|
|
280
|
+
}
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
test("a workflow with runs returns one priced row", async () => {
|
|
284
|
+
// The fixture runs are in October 2026; the route window ends now, so
|
|
285
|
+
// shift them into the last 7 days for this check.
|
|
286
|
+
await getDbClient().run(
|
|
287
|
+
"UPDATE workflow_runs SET startedAt=strftime('%Y-%m-%dT%H:%M:%fZ','now','-1 day') WHERE workflowId=? AND startedAt>=?",
|
|
288
|
+
[workflowId, WINDOW.start],
|
|
289
|
+
);
|
|
290
|
+
const { status, body } = await get(`workflowId=${workflowId}&days=7&tier=smol`);
|
|
291
|
+
expect(status).toBe(200);
|
|
292
|
+
const b = body as {
|
|
293
|
+
valid: boolean;
|
|
294
|
+
tier: string;
|
|
295
|
+
rows: { substitutedRuns: number; savingUsd: { p10: number; p50: number; p90: number } }[];
|
|
296
|
+
};
|
|
297
|
+
expect(b.valid).toBe(true);
|
|
298
|
+
expect(b.tier).toBe("smol");
|
|
299
|
+
expect(b.rows).toHaveLength(1);
|
|
300
|
+
expect(b.rows[0]?.substitutedRuns).toBe(4);
|
|
301
|
+
const band = b.rows[0]?.savingUsd;
|
|
302
|
+
expect(band && band.p10 < band.p50 && band.p50 < band.p90).toBe(true);
|
|
303
|
+
expect((body as { calibrationSource: string }).calibrationSource).toBe("shipped");
|
|
304
|
+
});
|
|
305
|
+
|
|
306
|
+
describe("deployment calibration", () => {
|
|
307
|
+
const store = (value: string) =>
|
|
308
|
+
upsertSwarmConfig({ scope: "global", key: SAVINGS_CALIBRATION_KEY, value });
|
|
309
|
+
const p50 = async () =>
|
|
310
|
+
(
|
|
311
|
+
(await get(`workflowId=${workflowId}&days=7&tier=smart`)).body as {
|
|
312
|
+
rows: { agentOnlyUsd: { p50: number } }[];
|
|
313
|
+
}
|
|
314
|
+
).rows[0]?.agentOnlyUsd.p50 as number;
|
|
315
|
+
const doubled = JSON.stringify({
|
|
316
|
+
calibratedAt: "2026-12-01",
|
|
317
|
+
model: "claude-opus-5-5",
|
|
318
|
+
curve: Object.fromEntries(
|
|
319
|
+
SAVINGS_TIERS.map((t) => [t, { usd: CURVE[t].usd.map((v) => v * 2), hrs: CURVE[t].hrs }]),
|
|
320
|
+
),
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
afterAll(async () => {
|
|
324
|
+
await getDbClient().run("DELETE FROM swarm_config WHERE key = ?", [
|
|
325
|
+
SAVINGS_CALIBRATION_KEY,
|
|
326
|
+
]);
|
|
327
|
+
});
|
|
328
|
+
|
|
329
|
+
test("a stored curve prices the response and is named as the source", async () => {
|
|
330
|
+
const shipped = await p50();
|
|
331
|
+
await store(doubled);
|
|
332
|
+
const { body } = await get(`workflowId=${workflowId}&days=7&tier=smart`);
|
|
333
|
+
expect(body).toMatchObject({ calibrationSource: "deployment", calibratedAt: "2026-12-01" });
|
|
334
|
+
expect(await p50()).toBeGreaterThan(shipped * 1.2);
|
|
335
|
+
});
|
|
336
|
+
|
|
337
|
+
test("the daily series uses the same stored curve, so it still sums to the header", async () => {
|
|
338
|
+
await store(doubled);
|
|
339
|
+
const jobs = (await get(`workflowId=${workflowId}&days=7`)).body as {
|
|
340
|
+
rows: { savingUsd: { p50: number } }[];
|
|
341
|
+
};
|
|
342
|
+
const daily = (await get(`workflowId=${workflowId}&days=7`, "daily")).body as {
|
|
343
|
+
calibrationSource: string;
|
|
344
|
+
points: { savingUsd: number }[];
|
|
345
|
+
};
|
|
346
|
+
expect(daily.calibrationSource).toBe("deployment");
|
|
347
|
+
const cents = daily.points.reduce((sum, p) => sum + Math.round(p.savingUsd * 100), 0);
|
|
348
|
+
expect(cents).toBe(Math.round((jobs.rows[0]?.savingUsd.p50 ?? Number.NaN) * 100));
|
|
349
|
+
});
|
|
350
|
+
|
|
351
|
+
test("a malformed stored curve falls back to the shipped one", async () => {
|
|
352
|
+
await store("{not json");
|
|
353
|
+
const { body } = await get(`workflowId=${workflowId}&days=7&tier=smart`);
|
|
354
|
+
expect(body).toMatchObject({ calibrationSource: "shipped", calibratedAt: "2026-11-05" });
|
|
355
|
+
});
|
|
356
|
+
});
|
|
357
|
+
|
|
358
|
+
test("the daily points sum to the header saving", async () => {
|
|
359
|
+
// Runs over several days; the previous test moved them all to yesterday.
|
|
360
|
+
// An hour back keeps every run clear of the window end (`now` at request time).
|
|
361
|
+
await getDbClient().run(
|
|
362
|
+
"UPDATE workflow_runs SET startedAt=strftime('%Y-%m-%dT%H:%M:%fZ','now','-' || (rowid % 6) || ' days','-1 hour') WHERE workflowId=?",
|
|
363
|
+
[workflowId],
|
|
364
|
+
);
|
|
365
|
+
const jobs = (await get(`workflowId=${workflowId}&days=7`)).body as {
|
|
366
|
+
rows: {
|
|
367
|
+
savingUsd: { p50: number };
|
|
368
|
+
actualUsd: number;
|
|
369
|
+
measuredUsd: number;
|
|
370
|
+
estimatedLlmUsd: number;
|
|
371
|
+
}[];
|
|
372
|
+
};
|
|
373
|
+
const { status, body } = await get(`workflowId=${workflowId}&days=7`, "daily");
|
|
374
|
+
expect(status).toBe(200);
|
|
375
|
+
const daily = body as {
|
|
376
|
+
valid: boolean;
|
|
377
|
+
tier: string;
|
|
378
|
+
points: {
|
|
379
|
+
day: string;
|
|
380
|
+
savingUsd: number;
|
|
381
|
+
actualUsd: number;
|
|
382
|
+
measuredUsd: number;
|
|
383
|
+
estimatedLlmUsd: number;
|
|
384
|
+
}[];
|
|
385
|
+
};
|
|
386
|
+
expect(daily.valid).toBe(true);
|
|
387
|
+
expect(daily.tier).toBe("smart");
|
|
388
|
+
// A 7-day window touches 8 UTC days; every one has a point.
|
|
389
|
+
expect(daily.points).toHaveLength(8);
|
|
390
|
+
const cents = (xs: number[]) => Math.round(xs.reduce((s, x) => s + x, 0) * 100);
|
|
391
|
+
expect(cents(daily.points.map((p) => p.savingUsd))).toBe(
|
|
392
|
+
Math.round((jobs.rows[0]?.savingUsd.p50 ?? Number.NaN) * 100),
|
|
393
|
+
);
|
|
394
|
+
expect(cents(daily.points.map((p) => p.actualUsd))).toBe(
|
|
395
|
+
Math.round((jobs.rows[0]?.actualUsd ?? Number.NaN) * 100),
|
|
396
|
+
);
|
|
397
|
+
expect(cents(daily.points.map((p) => p.measuredUsd))).toBe(
|
|
398
|
+
Math.round((jobs.rows[0]?.measuredUsd ?? Number.NaN) * 100),
|
|
399
|
+
);
|
|
400
|
+
expect(cents(daily.points.map((p) => p.estimatedLlmUsd))).toBe(
|
|
401
|
+
Math.round((jobs.rows[0]?.estimatedLlmUsd ?? Number.NaN) * 100),
|
|
402
|
+
);
|
|
403
|
+
expect(daily.points.filter((p) => p.savingUsd !== 0).length).toBeGreaterThan(0);
|
|
404
|
+
});
|
|
405
|
+
});
|
|
406
|
+
});
|