@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.
- package/dist/{acp-adapter-t949vbbp.js → acp-adapter-fqzkxyra.js} +3 -3
- package/dist/{actions-7e9czc7s.js → actions-kxek041g.js} +18 -18
- package/dist/{amp-adapter-gv6cbb7d.js → amp-adapter-ce7jjke4.js} +1 -1
- package/dist/{app-b9fbkp9v.js → app-v6zz8yh0.js} +3 -3
- package/dist/{assistant-wjb7v4c7.js → assistant-m0sw9xx2.js} +19 -19
- package/dist/{boot-args-schema-2t37e0fe.js → boot-args-schema-66bk37wv.js} +1 -1
- package/dist/{boot-reembed-gka3zas7.js → boot-reembed-edjpwsc1.js} +1 -1
- package/dist/{boot-reembed-a35nkyr1.js → boot-reembed-y27n09tz.js} +1 -1
- package/dist/{boot-scrub-sweep-65sdnp6c.js → boot-scrub-sweep-1xc35965.js} +3 -3
- package/dist/{boot-seeds-x42tzytw.js → boot-seeds-7hjj91qj.js} +4 -4
- package/dist/{boxd-qbkxy6ap.js → boxd-ent8ch04.js} +1 -1
- package/dist/{claude-adapter-v221b2fd.js → claude-adapter-stw7jdcf.js} +3 -3
- package/dist/{claude-managed-adapter-29eq09wd.js → claude-managed-adapter-jh6c523j.js} +2 -2
- package/dist/{claude-managed-setup-9m6450zk.js → claude-managed-setup-y6z2perp.js} +5 -5
- package/dist/{claude-sdk-session-0q0ptaba.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-y97qxcfx.js → cli-1qkz151v.js} +3 -3
- package/dist/{cli-qwca7s44.js → cli-26ctbcdp.js} +18 -6
- package/dist/{cli-8fknbnhw.js → cli-2kvhc7x8.js} +7 -7
- package/dist/{cli-rfywm85n.js → cli-3e875j5p.js} +136 -49
- package/dist/{cli-6wxjceza.js → cli-3sy2xags.js} +10 -10
- package/dist/{cli-a7wvp2km.js → cli-3vmb0n58.js} +1 -1
- package/dist/{cli-p1bq50aa.js → cli-4vzgeta4.js} +13 -13
- package/dist/{cli-m28atx8q.js → cli-5y340nzn.js} +4 -4
- package/dist/{cli-7p1kvkck.js → cli-6rc5frdm.js} +1 -1
- package/dist/{cli-tph5wt3y.js → cli-6wj7p986.js} +1 -1
- package/dist/{cli-t73cas91.js → cli-7349va39.js} +8100 -7620
- package/dist/{cli-ee0b971j.js → cli-837vfs2x.js} +2 -2
- package/dist/{cli-abejgen1.js → cli-959kp5h2.js} +12 -11
- package/dist/{cli-8kzasymh.js → cli-97ae65rz.js} +4 -4
- package/dist/{cli-m4dqmnbp.js → cli-9mb2q6pb.js} +1 -1
- package/dist/{cli-p2gfst4m.js → cli-a0z912j7.js} +123 -35
- package/dist/{cli-4txa0ter.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-qa4wdgsy.js → cli-e1p3n5gq.js} +4 -4
- package/dist/{cli-gxkx0zzp.js → cli-f4e8r23x.js} +1 -1
- package/dist/{cli-qmqk2yc2.js → cli-f4r3g076.js} +1 -1
- package/dist/{cli-wcg8x116.js → cli-f6bahpn5.js} +1 -1
- package/dist/{cli-qsnnd04k.js → cli-fpjmegdb.js} +1 -1
- package/dist/{cli-j790gp2w.js → cli-fwy8kfd6.js} +271 -37
- package/dist/{cli-rmmpt0wm.js → cli-g5mtmf6t.js} +6 -6
- package/dist/{cli-rzsy6nc6.js → cli-gyt3zydq.js} +3 -3
- package/dist/{cli-pasjcnmj.js → cli-j24zbska.js} +3 -3
- package/dist/{cli-qfedvmcr.js → cli-jdknh18m.js} +10 -10
- package/dist/{cli-7b8sbmag.js → cli-kjtwrhea.js} +1 -1
- package/dist/{cli-gpwhbgx0.js → cli-m9s2qff2.js} +57 -25
- package/dist/{cli-2wnq8jkz.js → cli-mxn1qvbn.js} +3 -2
- package/dist/{cli-s7t6p9my.js → cli-n6vtrcht.js} +1 -1
- package/dist/{cli-2zp0et3v.js → cli-pchypa1s.js} +55 -19
- package/dist/{cli-yq3zak69.js → cli-pfs8adn4.js} +1 -1
- package/dist/{cli-c55z5chy.js → cli-q24n86d2.js} +2 -2
- package/dist/{cli-h38x262p.js → cli-rcecc3yq.js} +167 -43
- package/dist/{cli-xae4g51r.js → cli-rh15cakt.js} +1 -1
- package/dist/{cli-sdtt3men.js → cli-rmzsadkf.js} +1 -1
- package/dist/{cli-f3vq58d2.js → cli-t2tma8gb.js} +1 -1
- package/dist/{cli-m23zjcy2.js → cli-tekytdq9.js} +2 -2
- package/dist/{cli-tqegb8eq.js → cli-vdektqgf.js} +1 -1
- package/dist/{cli-ffhtssbp.js → cli-vj75tyaw.js} +1 -1
- package/dist/{cli-deefvewm.js → cli-w401b9tq.js} +1 -1
- package/dist/{cli-n9ftpys4.js → cli-x6t50dmw.js} +7 -7
- package/dist/{cli-6t09sdhy.js → cli-xf3qw745.js} +3 -3
- package/dist/{cli-hxpyc9sn.js → cli-z1w943va.js} +2 -2
- package/dist/{cli-334z18y3.js → cli-zh9rsbqf.js} +3 -3
- package/dist/cli.js +17 -17
- package/dist/{codex-adapter-30j9bpzd.js → codex-adapter-zerf5qsp.js} +1 -1
- package/dist/{codex-hook-h3vk6xzr.js → codex-hook-30gyhzxe.js} +1 -1
- package/dist/{codex-session-runner-8vp57v8w.js → codex-session-runner-89s304py.js} +1 -1
- package/dist/{commands-bngnz9g7.js → commands-jeh1b51j.js} +3 -3
- package/dist/{controller-hxx8wwy1.js → controller-mctfbrse.js} +3 -3
- package/dist/{controllers-connect-e2b4gyxc.js → controllers-connect-nxa1ddwn.js} +19 -19
- package/dist/{cursor-adapter-n5nc3mfe.js → cursor-adapter-qj8vehv6.js} +1 -1
- package/dist/{db-bgm7vkc0.js → db-2dvay0es.js} +3 -3
- package/dist/{db-c6np41ca.js → db-b36qn6k3.js} +2 -2
- package/dist/{dispatcher-jnybck15.js → dispatcher-gndbta01.js} +17 -17
- 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-crsm2zp0.js → embedding-space-4p05vb2x.js} +6 -6
- package/dist/{embeddings-e7z3f27f.js → embeddings-m6768mgh.js} +1 -1
- package/dist/{external-channel-m77wf5z3.js → external-channel-gjv3jne3.js} +2 -2
- package/dist/{fx-adapter-b6w0kf56.js → fx-adapter-rpsgb1tw.js} +4 -4
- package/dist/{grok-adapter-4sqtebdk.js → grok-adapter-n2m3zjg1.js} +4 -4
- package/dist/handlers-nv8edn19.js +32 -0
- package/dist/{heartbeat-xgb036sx.js → heartbeat-a1tv60gt.js} +20 -20
- package/dist/{hook-xsk1jbha.js → hook-8h5dzcgq.js} +2 -2
- package/dist/{hook-5h02c9ga.js → hook-cyaps9ba.js} +4 -4
- package/dist/{http-txe7qhvn.js → http-t15mhbd9.js} +1733 -79
- package/dist/{index-0eny15vj.js → index-2skqtd76.js} +2 -2
- package/dist/{index-w7em4ktq.js → index-d6wkh8ar.js} +2 -2
- package/dist/{index-rvccek1w.js → index-gjwaqaq8.js} +5 -5
- package/dist/{index-e5pfbsys.js → index-hjhfk3ra.js} +3 -3
- package/dist/{index-1qw6f2dc.js → index-khe3gp0k.js} +4 -4
- package/dist/index-ksmyd6kc.js +37 -0
- package/dist/{index-vk63emp7.js → index-qav6455f.js} +14 -12
- package/dist/index-rjnrje98.js +32 -0
- package/dist/index-tv3413wy.js +33 -0
- package/dist/{index-ysvbhg5m.js → index-w1zqf680.js} +1 -1
- package/dist/{keepalive-4zsrce78.js → keepalive-91s7hzrb.js} +3 -3
- package/dist/lead-3hmddvrm.js +70 -0
- package/dist/{maintenance-86jvartk.js → maintenance-7pjmy5xy.js} +1 -1
- package/dist/{node-je82wjet.js → node-4c5jqxr3.js} +1 -1
- package/dist/{oauth-refresh-sweep-j52kg9pr.js → oauth-refresh-sweep-rzqfm94j.js} +2 -2
- package/dist/{onboard-c2aj5p8q.js → onboard-74st7pag.js} +3 -3
- package/dist/{opencode-adapter-dsagkhtw.js → opencode-adapter-arn7gm8q.js} +2 -2
- package/dist/{otel-impl-yyy203z8.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-1rgzqyzz.js → pricing-refresh-ha21jhk7.js} +2 -2
- package/dist/{rbac-roles-e70xfeyj.js → rbac-roles-59dxachk.js} +2 -2
- package/dist/{rbac-roles-q89qe8er.js → rbac-roles-wqhma4dr.js} +3 -3
- package/dist/{render-v2-k6tt107q.js → render-v2-14kpzx12.js} +3 -3
- package/dist/{resume-cnnrjfxa.js → resume-gsa5fb5f.js} +6 -6
- package/dist/{session-access-xkxkjw3n.js → session-access-skm7bnp6.js} +4 -4
- package/dist/{session-source-backfill-1d5pdwww.js → session-source-backfill-pbqzbncv.js} +2 -2
- package/dist/{setup-se8q7gkc.js → setup-5yermsem.js} +1 -1
- package/dist/{work-objects-1kxnbzy5.js → work-objects-xj0ck0w4.js} +20 -20
- package/dist/{worker-follow-up-zqm8apt4.js → worker-follow-up-911xc0b8.js} +19 -17
- package/dist/worker-qgg0m2gw.js +70 -0
- package/openapi.json +1741 -290
- package/package.json +3 -3
- 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/eval-harness.ts +14 -1
- package/src/scripts-runtime/executors/types.ts +3 -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/mcp-tools.test.ts +1 -1
- 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/script-native-unawaited-drain.test.ts +167 -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/script-run.ts +1 -1
- 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/templates/skills/swarm-scripts/SKILL.md +1 -1
- package/templates/skills/swarm-scripts/content.md +1 -1
- package/dist/handlers-kdjcyzdj.js +0 -32
- package/dist/index-835xdgnm.js +0 -33
- package/dist/index-d469300x.js +0 -37
- package/dist/index-rzqwp11g.js +0 -32
- package/dist/lead-yqt47dkr.js +0 -70
- package/dist/worker-j6xxtq7p.js +0 -70
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import { classifyProviderError, classifyProviderErrorOf } from "../commands/provider-error-class";
|
|
3
|
+
|
|
4
|
+
const NOW = Date.parse("2026-11-05T10:00:00Z");
|
|
5
|
+
|
|
6
|
+
function classOf(
|
|
7
|
+
failureReason: string | undefined,
|
|
8
|
+
extra: Record<string, unknown> = {},
|
|
9
|
+
model = "claude-opus-5-5",
|
|
10
|
+
) {
|
|
11
|
+
return classifyProviderErrorOf({ exitCode: 1, ...extra }, failureReason, model, undefined, NOW);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
describe("classifyProviderErrorOf: provider errors per harness", () => {
|
|
15
|
+
const cases: [string, string | undefined, Record<string, unknown>, string, string][] = [
|
|
16
|
+
// claude: structured rate_limit_event, then the error tracker's labels.
|
|
17
|
+
[
|
|
18
|
+
"claude key-wide rate limit event",
|
|
19
|
+
"Rate limit hit",
|
|
20
|
+
{ rateLimitResetAt: "2026-11-05T12:00:00Z" },
|
|
21
|
+
"claude-opus-5-5",
|
|
22
|
+
"rate_limit",
|
|
23
|
+
],
|
|
24
|
+
["claude usage limit text", "Claude usage limit reached", {}, "claude-opus-5-5", "rate_limit"],
|
|
25
|
+
[
|
|
26
|
+
"claude weekly model window event",
|
|
27
|
+
"Session error (exit code 1)",
|
|
28
|
+
{
|
|
29
|
+
modelRateLimit: {
|
|
30
|
+
window: "seven_day_opus",
|
|
31
|
+
model: "opus",
|
|
32
|
+
resetAt: "2026-11-08T00:00:00Z",
|
|
33
|
+
},
|
|
34
|
+
},
|
|
35
|
+
"claude-opus-5-5",
|
|
36
|
+
"model_window",
|
|
37
|
+
],
|
|
38
|
+
[
|
|
39
|
+
"claude seat mismatch event",
|
|
40
|
+
"Session error (exit code 1)",
|
|
41
|
+
{ creditsRequired: { observedAt: "2026-11-05T09:59:00Z" } },
|
|
42
|
+
"claude-fable-5-1",
|
|
43
|
+
"seat",
|
|
44
|
+
],
|
|
45
|
+
[
|
|
46
|
+
"claude 5xx",
|
|
47
|
+
"Server error (API overloaded): Service unavailable",
|
|
48
|
+
{},
|
|
49
|
+
"claude-opus-5-5",
|
|
50
|
+
"overloaded",
|
|
51
|
+
],
|
|
52
|
+
["claude 529", "API Error: 529 overloaded_error", {}, "claude-opus-5-5", "overloaded"],
|
|
53
|
+
["claude auth", "Authentication failed: invalid token", {}, "claude-opus-5-5", "auth"],
|
|
54
|
+
// codex
|
|
55
|
+
[
|
|
56
|
+
"codex credits exhausted",
|
|
57
|
+
"Your workspace is out of credits. Ask your workspace owner to refill in order to continue.",
|
|
58
|
+
{},
|
|
59
|
+
"gpt-6.1-sol",
|
|
60
|
+
"credits",
|
|
61
|
+
],
|
|
62
|
+
[
|
|
63
|
+
"codex auth",
|
|
64
|
+
"[auth-error] Codex authentication failed — check OPENAI_API_KEY or ChatGPT login.",
|
|
65
|
+
{},
|
|
66
|
+
"gpt-6.1-sol",
|
|
67
|
+
"auth",
|
|
68
|
+
],
|
|
69
|
+
["codex 429", "429 Too Many Requests", {}, "gpt-6.1-sol", "rate_limit"],
|
|
70
|
+
// pi / opencode / gateways
|
|
71
|
+
[
|
|
72
|
+
"opencode overloaded",
|
|
73
|
+
"system text: opencode error: provider overloaded",
|
|
74
|
+
{},
|
|
75
|
+
"openrouter/deepseek/deepseek-v4.1-flash",
|
|
76
|
+
"overloaded",
|
|
77
|
+
],
|
|
78
|
+
[
|
|
79
|
+
"gateway 503",
|
|
80
|
+
"provider returned error: 503 service unavailable",
|
|
81
|
+
{},
|
|
82
|
+
"openrouter/deepseek/deepseek-v4.1-flash",
|
|
83
|
+
"server_error",
|
|
84
|
+
],
|
|
85
|
+
[
|
|
86
|
+
"gateway 502",
|
|
87
|
+
"upstream error: HTTP 502 Bad Gateway",
|
|
88
|
+
{},
|
|
89
|
+
"openrouter/deepseek/deepseek-v4.1-flash",
|
|
90
|
+
"server_error",
|
|
91
|
+
],
|
|
92
|
+
// Every provider 5xx, not just 500-504.
|
|
93
|
+
[
|
|
94
|
+
"gateway 521",
|
|
95
|
+
"provider returned error: 521 web server is down",
|
|
96
|
+
{},
|
|
97
|
+
"openrouter/deepseek/deepseek-v4.1-flash",
|
|
98
|
+
"server_error",
|
|
99
|
+
],
|
|
100
|
+
[
|
|
101
|
+
"gateway 507",
|
|
102
|
+
"upstream error: HTTP 507 Insufficient Storage",
|
|
103
|
+
{},
|
|
104
|
+
"openrouter/deepseek/deepseek-v4.1-flash",
|
|
105
|
+
"server_error",
|
|
106
|
+
],
|
|
107
|
+
["api 599", "API Error: status 599", {}, "claude-opus-5-5", "server_error"],
|
|
108
|
+
[
|
|
109
|
+
"auth error wrapper",
|
|
110
|
+
"Authentication error: Authentication failed: invalid key",
|
|
111
|
+
{},
|
|
112
|
+
"openrouter/deepseek/deepseek-v4.1-flash",
|
|
113
|
+
"auth",
|
|
114
|
+
],
|
|
115
|
+
];
|
|
116
|
+
for (const [name, reason, extra, model, expected] of cases) {
|
|
117
|
+
test(name, () => {
|
|
118
|
+
expect(classOf(reason, extra, model)).toBe(expected as never);
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
describe("never a fallback", () => {
|
|
124
|
+
const cases: [string, string | undefined, Record<string, unknown>][] = [
|
|
125
|
+
["task failure", "Error during execution", {}],
|
|
126
|
+
["generic exit", "Claude process exited with code 1", {}],
|
|
127
|
+
["tool error text", "Bash tool failed: command not found", {}],
|
|
128
|
+
["context overflow", "prompt is too long: 210000 tokens > 200000 maximum", {}],
|
|
129
|
+
["context overflow (codex)", "context_length_exceeded", {}],
|
|
130
|
+
["budget refusal", "Budget limit exceeded", {}],
|
|
131
|
+
["max turns", "Max turns exceeded", {}],
|
|
132
|
+
["cancellation", "Rate limit hit", { errorCategory: "cancelled" }],
|
|
133
|
+
["transient codex refresh lock", "[auth-error] waiting for the refresh lock", {}],
|
|
134
|
+
["no reason", undefined, {}],
|
|
135
|
+
// A 5xx-looking number that is not a status.
|
|
136
|
+
["5xx-looking count", "Error: wrote 521 lines, then the test failed", {}],
|
|
137
|
+
// Harness-side exceptions (MCP / transport): their text never decides.
|
|
138
|
+
[
|
|
139
|
+
"MCP transport 503",
|
|
140
|
+
"MCP error -32000: HTTP 503 Service Unavailable",
|
|
141
|
+
{ errorOrigin: "harness" },
|
|
142
|
+
],
|
|
143
|
+
[
|
|
144
|
+
"MCP tool 429",
|
|
145
|
+
"429 Too Many Requests from the GitHub MCP server",
|
|
146
|
+
{ errorOrigin: "harness" },
|
|
147
|
+
],
|
|
148
|
+
["MCP tool 401", "HTTP 401 Unauthorized", { errorOrigin: "harness" }],
|
|
149
|
+
];
|
|
150
|
+
for (const [name, reason, extra] of cases) {
|
|
151
|
+
test(name, () => {
|
|
152
|
+
expect(classOf(reason, extra)).toBeNull();
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
test("a clean exit is never a fallback, even with a rate-limit event", () => {
|
|
157
|
+
expect(
|
|
158
|
+
classifyProviderErrorOf(
|
|
159
|
+
{ exitCode: 0, rateLimitResetAt: "2026-11-05T12:00:00Z" },
|
|
160
|
+
undefined,
|
|
161
|
+
"claude-opus-5-5",
|
|
162
|
+
),
|
|
163
|
+
).toBeNull();
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
test("a context-overflow text wins over a rate-limit outcome", () => {
|
|
167
|
+
expect(
|
|
168
|
+
classifyProviderError({ exitCode: 1 }, "maximum context length exceeded", {
|
|
169
|
+
kind: "key",
|
|
170
|
+
rateLimitedUntil: "2026-11-05T12:00:00Z",
|
|
171
|
+
}),
|
|
172
|
+
).toBeNull();
|
|
173
|
+
});
|
|
174
|
+
});
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
// The runner side of provider-error fallbacks: a profile task that ends on a
|
|
2
|
+
// provider error asks the API to supersede it with `provider_fallback`
|
|
3
|
+
// instead of finishing it; anything else finishes as before.
|
|
4
|
+
|
|
5
|
+
import { afterAll, beforeAll, beforeEach, expect, spyOn, test } from "bun:test";
|
|
6
|
+
import { createServer, type Server } from "node:http";
|
|
7
|
+
import {
|
|
8
|
+
type ApiConfig,
|
|
9
|
+
checkCompletedProcesses,
|
|
10
|
+
type RunnerState,
|
|
11
|
+
type RunningTask,
|
|
12
|
+
requestProviderFallback,
|
|
13
|
+
} from "../commands/runner";
|
|
14
|
+
import type { ProviderResult } from "../providers/types";
|
|
15
|
+
import { listenOnFreePort } from "./test-net";
|
|
16
|
+
|
|
17
|
+
let server: Server;
|
|
18
|
+
let config: ApiConfig;
|
|
19
|
+
let requests: Array<{ path: string; body: Record<string, unknown> }>;
|
|
20
|
+
let supersedeStatus: number;
|
|
21
|
+
let supersedeBody: Record<string, unknown>;
|
|
22
|
+
|
|
23
|
+
beforeAll(async () => {
|
|
24
|
+
server = createServer(async (req, res) => {
|
|
25
|
+
const path = req.url?.split("?")[0]?.split("/").at(-1) ?? "";
|
|
26
|
+
let text = "";
|
|
27
|
+
for await (const chunk of req) text += chunk;
|
|
28
|
+
requests.push({ path, body: text ? JSON.parse(text) : {} });
|
|
29
|
+
if (path === "supersede") {
|
|
30
|
+
res
|
|
31
|
+
.writeHead(supersedeStatus, { "Content-Type": "application/json" })
|
|
32
|
+
.end(JSON.stringify(supersedeBody));
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
res.writeHead(200, { "Content-Type": "application/json" }).end("{}");
|
|
36
|
+
});
|
|
37
|
+
config = {
|
|
38
|
+
apiUrl: `http://127.0.0.1:${await listenOnFreePort(server)}`,
|
|
39
|
+
apiKey: "provider-fallback-test-credential",
|
|
40
|
+
agentId: crypto.randomUUID(),
|
|
41
|
+
};
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
beforeEach(() => {
|
|
45
|
+
requests = [];
|
|
46
|
+
supersedeStatus = 200;
|
|
47
|
+
supersedeBody = { success: true, kind: "resumed", resumeTaskId: crypto.randomUUID() };
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
afterAll(async () => {
|
|
51
|
+
server.closeAllConnections();
|
|
52
|
+
await new Promise<void>((resolve) => server.close(() => resolve()));
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
function completedState(taskId: string, result: Partial<ProviderResult>, resolvedProfile?: string) {
|
|
56
|
+
const state: RunnerState = {
|
|
57
|
+
activeTasks: new Map([
|
|
58
|
+
[
|
|
59
|
+
taskId,
|
|
60
|
+
{
|
|
61
|
+
taskId,
|
|
62
|
+
result: { isError: true, ...result } as ProviderResult,
|
|
63
|
+
triggerType: "task_assigned",
|
|
64
|
+
startTime: new Date(),
|
|
65
|
+
harnessProvider: "claude",
|
|
66
|
+
hasLocalEnvironment: false,
|
|
67
|
+
model: "claude-opus-5-5",
|
|
68
|
+
resolvedProfile,
|
|
69
|
+
} as RunningTask,
|
|
70
|
+
],
|
|
71
|
+
]),
|
|
72
|
+
maxConcurrent: 1,
|
|
73
|
+
startedAt: Date.now(),
|
|
74
|
+
tasksProcessed: 0,
|
|
75
|
+
harnessProvider: "claude",
|
|
76
|
+
codexCreditsExhaustedCooldownMs: 0,
|
|
77
|
+
modelWindowBlocks: new Map(),
|
|
78
|
+
};
|
|
79
|
+
return state;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const paths = () => requests.map((r) => r.path);
|
|
83
|
+
|
|
84
|
+
test("a rate-limited profile task is superseded with provider_fallback, not finished", async () => {
|
|
85
|
+
const taskId = crypto.randomUUID();
|
|
86
|
+
const log = spyOn(console, "log").mockImplementation(() => {});
|
|
87
|
+
try {
|
|
88
|
+
await checkCompletedProcesses(
|
|
89
|
+
completedState(
|
|
90
|
+
taskId,
|
|
91
|
+
{ exitCode: 1, failureReason: "Rate limit hit", rateLimitResetAt: "2099-01-01T00:00:00Z" },
|
|
92
|
+
"claude-glm",
|
|
93
|
+
),
|
|
94
|
+
"worker",
|
|
95
|
+
config,
|
|
96
|
+
);
|
|
97
|
+
} finally {
|
|
98
|
+
log.mockRestore();
|
|
99
|
+
}
|
|
100
|
+
const supersede = requests.find((r) => r.path === "supersede");
|
|
101
|
+
expect(supersede?.body).toEqual({ reason: "provider_fallback", errorClass: "rate_limit" });
|
|
102
|
+
expect(paths()).not.toContain("finish");
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
test("no fallback left: the task fails, led by the API's chain message", async () => {
|
|
106
|
+
supersedeBody = {
|
|
107
|
+
success: true,
|
|
108
|
+
kind: "noFallback",
|
|
109
|
+
resumeTaskId: null,
|
|
110
|
+
failureReason: "overloaded on claude-c; no fallback left (tried: claude-a → claude-c)",
|
|
111
|
+
};
|
|
112
|
+
const taskId = crypto.randomUUID();
|
|
113
|
+
const log = spyOn(console, "log").mockImplementation(() => {});
|
|
114
|
+
try {
|
|
115
|
+
await checkCompletedProcesses(
|
|
116
|
+
completedState(
|
|
117
|
+
taskId,
|
|
118
|
+
{ exitCode: 1, failureReason: "Server error (API overloaded): Service unavailable" },
|
|
119
|
+
"claude-c",
|
|
120
|
+
),
|
|
121
|
+
"worker",
|
|
122
|
+
config,
|
|
123
|
+
);
|
|
124
|
+
} finally {
|
|
125
|
+
log.mockRestore();
|
|
126
|
+
}
|
|
127
|
+
const finish = requests.find((r) => r.path === "finish");
|
|
128
|
+
expect(finish?.body.status).toBe("failed");
|
|
129
|
+
expect(finish?.body.failureReason).toBe(
|
|
130
|
+
"overloaded on claude-c; no fallback left (tried: claude-a → claude-c)\n\nServer error (API overloaded): Service unavailable",
|
|
131
|
+
);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
test("controls: no profile, a task failure, or an old API all finish as before", async () => {
|
|
135
|
+
const log = spyOn(console, "log").mockImplementation(() => {});
|
|
136
|
+
const warn = spyOn(console, "warn").mockImplementation(() => {});
|
|
137
|
+
try {
|
|
138
|
+
// No profile at claim: no supersede call at all.
|
|
139
|
+
await checkCompletedProcesses(
|
|
140
|
+
completedState(crypto.randomUUID(), { exitCode: 1, failureReason: "Rate limit hit" }),
|
|
141
|
+
"worker",
|
|
142
|
+
config,
|
|
143
|
+
);
|
|
144
|
+
expect(paths()).not.toContain("supersede");
|
|
145
|
+
expect(paths()).toContain("finish");
|
|
146
|
+
|
|
147
|
+
// A task failure on a profile task: no supersede call.
|
|
148
|
+
requests = [];
|
|
149
|
+
await checkCompletedProcesses(
|
|
150
|
+
completedState(
|
|
151
|
+
crypto.randomUUID(),
|
|
152
|
+
{ exitCode: 1, failureReason: "Error during execution" },
|
|
153
|
+
"claude-a",
|
|
154
|
+
),
|
|
155
|
+
"worker",
|
|
156
|
+
config,
|
|
157
|
+
);
|
|
158
|
+
expect(paths()).not.toContain("supersede");
|
|
159
|
+
expect(requests.find((r) => r.path === "finish")?.body.failureReason).toBe(
|
|
160
|
+
"Error during execution",
|
|
161
|
+
);
|
|
162
|
+
|
|
163
|
+
// An API without the reason rejects it (400): the task fails with its own text.
|
|
164
|
+
requests = [];
|
|
165
|
+
supersedeStatus = 400;
|
|
166
|
+
supersedeBody = { error: "Invalid enum value" };
|
|
167
|
+
await checkCompletedProcesses(
|
|
168
|
+
completedState(
|
|
169
|
+
crypto.randomUUID(),
|
|
170
|
+
{ exitCode: 1, failureReason: "Rate limit hit" },
|
|
171
|
+
"claude-a",
|
|
172
|
+
),
|
|
173
|
+
"worker",
|
|
174
|
+
config,
|
|
175
|
+
);
|
|
176
|
+
expect(paths()).toContain("supersede");
|
|
177
|
+
expect(requests.find((r) => r.path === "finish")?.body.failureReason).toBe("Rate limit hit");
|
|
178
|
+
} finally {
|
|
179
|
+
log.mockRestore();
|
|
180
|
+
warn.mockRestore();
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
test("requestProviderFallback sends nothing without a profile or a class", async () => {
|
|
185
|
+
expect(
|
|
186
|
+
await requestProviderFallback(config, "worker", crypto.randomUUID(), {
|
|
187
|
+
errorClass: "pool_empty",
|
|
188
|
+
}),
|
|
189
|
+
).toEqual({ superseded: false });
|
|
190
|
+
expect(
|
|
191
|
+
await requestProviderFallback(config, "worker", crypto.randomUUID(), {
|
|
192
|
+
resolvedProfile: "claude-a",
|
|
193
|
+
errorClass: null,
|
|
194
|
+
}),
|
|
195
|
+
).toEqual({ superseded: false });
|
|
196
|
+
expect(requests).toEqual([]);
|
|
197
|
+
});
|
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
import { Database } from "bun:sqlite";
|
|
2
|
+
import { afterAll, beforeAll, describe, expect, test } from "bun:test";
|
|
3
|
+
import { unlink } from "node:fs/promises";
|
|
4
|
+
import { createServer as createHttpServer, type Server } from "node:http";
|
|
5
|
+
import { closeDb, createAgent, createWorkflow, getDbClient, initDb } from "../be/db";
|
|
6
|
+
import { TIER_MODEL } from "../be/savings/calibrate";
|
|
7
|
+
import { SAVINGS_CALIBRATION_KEY, SAVINGS_TIERS } from "../be/savings/model";
|
|
8
|
+
import { handleSavings } from "../http/savings";
|
|
9
|
+
import { handleSavingsCalibration, setCalibrationDeadlineMs } from "../http/savings-calibration";
|
|
10
|
+
import { getPathSegments, parseQueryParams } from "../http/utils";
|
|
11
|
+
import { setRequestAuth } from "../utils/request-auth-context";
|
|
12
|
+
import { listenOnFreePort } from "./test-net";
|
|
13
|
+
|
|
14
|
+
const TEST_DB_PATH = "./test-savings-calibration-api.sqlite";
|
|
15
|
+
const PER_TURN = 6; // sessions per turn count; MIN_SESSIONS_PER_TURN is 5
|
|
16
|
+
|
|
17
|
+
// The preload swaps file DBs for an in-memory template copy; the route's
|
|
18
|
+
// bounded executor reads the DB file from a child process, so opt out here.
|
|
19
|
+
const testGlobals = globalThis as typeof globalThis & {
|
|
20
|
+
__testMigrationTemplate?: Uint8Array;
|
|
21
|
+
__savedCalibrationTemplate?: Uint8Array;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
async function removeDbFiles(path: string): Promise<void> {
|
|
25
|
+
for (const suffix of ["", "-wal", "-shm"]) {
|
|
26
|
+
try {
|
|
27
|
+
await unlink(path + suffix);
|
|
28
|
+
} catch (error) {
|
|
29
|
+
if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
describe("savings calibration API", () => {
|
|
35
|
+
let agentId: string;
|
|
36
|
+
let workflowId: string;
|
|
37
|
+
let server: Server;
|
|
38
|
+
let port: number;
|
|
39
|
+
|
|
40
|
+
/** Opus sessions of 1..61 turns, `PER_TURN` each, whose tokens (so cost) scale with `k` and the turn count. */
|
|
41
|
+
let currentK = 1;
|
|
42
|
+
async function seed(k: number) {
|
|
43
|
+
currentK = k;
|
|
44
|
+
await getDbClient().run("DELETE FROM session_costs");
|
|
45
|
+
await getDbClient().run(
|
|
46
|
+
`INSERT INTO session_costs (id, sessionId, agentId, totalCostUsd, durationMs, numTurns, model, createdAt, modelBreakdown)
|
|
47
|
+
WITH RECURSIVE n(i) AS (SELECT 0 UNION ALL SELECT i + 1 FROM n WHERE i < ?)
|
|
48
|
+
SELECT 'cal-' || i, 'cal-' || i, ?, 1, 3000 * (i / ? + 1), i / ? + 1, 'claude-opus-5-5',
|
|
49
|
+
strftime('%Y-%m-%dT%H:%M:%fZ','now','-1 day'),
|
|
50
|
+
json_array(json_object('model','claude-opus-5-5','inputTokens',10,
|
|
51
|
+
'outputTokens', CAST(? * 300 * (i / ? + 1) AS INTEGER),
|
|
52
|
+
'cacheReadTokens', CAST(? * 20000 * (i / ? + 1) AS INTEGER),
|
|
53
|
+
'cacheWriteTokens', CAST(? * (5000 + 100 * (i / ? + 1)) AS INTEGER), 'costUsd', 1))
|
|
54
|
+
FROM n`,
|
|
55
|
+
[61 * PER_TURN - 1, agentId, PER_TURN, PER_TURN, k, PER_TURN, k, PER_TURN, k, PER_TURN],
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** Reseed so the derived smart mean cost is `target` times the shipped one. */
|
|
60
|
+
async function scaleTo(target: number) {
|
|
61
|
+
const ratio = (await post({})).body.meanRatio as number;
|
|
62
|
+
await seed((currentK * target) / ratio);
|
|
63
|
+
}
|
|
64
|
+
const url = (path: string) => `http://127.0.0.1:${port}${path}`;
|
|
65
|
+
const post = async (body: unknown, headers: Record<string, string> = {}) => {
|
|
66
|
+
const res = await fetch(url("/api/savings/calibration"), {
|
|
67
|
+
method: "POST",
|
|
68
|
+
headers,
|
|
69
|
+
body: JSON.stringify(body),
|
|
70
|
+
});
|
|
71
|
+
return { status: res.status, body: (await res.json()) as any };
|
|
72
|
+
};
|
|
73
|
+
const get = async () => (await (await fetch(url("/api/savings/calibration"))).json()) as any;
|
|
74
|
+
const storedRows = async () =>
|
|
75
|
+
getDbClient().query("SELECT value FROM swarm_config WHERE key = ?", [SAVINGS_CALIBRATION_KEY]);
|
|
76
|
+
|
|
77
|
+
beforeAll(async () => {
|
|
78
|
+
testGlobals.__savedCalibrationTemplate = testGlobals.__testMigrationTemplate;
|
|
79
|
+
testGlobals.__testMigrationTemplate = undefined;
|
|
80
|
+
await removeDbFiles(TEST_DB_PATH);
|
|
81
|
+
initDb(TEST_DB_PATH);
|
|
82
|
+
agentId = (await createAgent({ name: "calibration-agent", isLead: false, status: "idle" })).id;
|
|
83
|
+
workflowId = (await createWorkflow({ name: "calibration-wf", definition: { nodes: [] } })).id;
|
|
84
|
+
server = createHttpServer(async (req, res) => {
|
|
85
|
+
// The real server authenticates before routing; a guest header stands in for a denied caller.
|
|
86
|
+
setRequestAuth(
|
|
87
|
+
req,
|
|
88
|
+
req.headers["x-test-guest"]
|
|
89
|
+
? { kind: "guest" }
|
|
90
|
+
: { kind: "operator", fingerprint: "test-key" },
|
|
91
|
+
);
|
|
92
|
+
const segs = getPathSegments(req.url || "");
|
|
93
|
+
const query = parseQueryParams(req.url || "");
|
|
94
|
+
const handled =
|
|
95
|
+
(await handleSavingsCalibration(req, res, segs, query)) ||
|
|
96
|
+
(await handleSavings(req, res, segs, query));
|
|
97
|
+
if (!handled) {
|
|
98
|
+
res.writeHead(404);
|
|
99
|
+
res.end();
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
port = await listenOnFreePort(server);
|
|
103
|
+
for (const model of Object.values(TIER_MODEL)) {
|
|
104
|
+
for (const [tokenClass, price] of [
|
|
105
|
+
["input", 4],
|
|
106
|
+
["output", 20],
|
|
107
|
+
["cached_input", 0.2],
|
|
108
|
+
["cache_write_1h", 8],
|
|
109
|
+
] as const) {
|
|
110
|
+
await getDbClient().run(
|
|
111
|
+
`INSERT INTO pricing (provider, model, token_class, effective_from, price_per_million_usd, createdAt, lastUpdatedAt)
|
|
112
|
+
VALUES ('claude', ?, ?, 0, ?, 0, 0)`,
|
|
113
|
+
[model, tokenClass, price],
|
|
114
|
+
);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
// Tokens scale linearly with k, so one dry run gives the k that matches the shipped mean cost.
|
|
118
|
+
await seed(1);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
afterAll(async () => {
|
|
122
|
+
server?.close();
|
|
123
|
+
closeDb();
|
|
124
|
+
testGlobals.__testMigrationTemplate = testGlobals.__savedCalibrationTemplate;
|
|
125
|
+
testGlobals.__savedCalibrationTemplate = undefined;
|
|
126
|
+
await removeDbFiles(TEST_DB_PATH);
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
test("a dry run derives and compares but stores nothing", async () => {
|
|
130
|
+
const { status, body } = await post({});
|
|
131
|
+
expect(status).toBe(200);
|
|
132
|
+
expect(body).toMatchObject({
|
|
133
|
+
applied: false,
|
|
134
|
+
refusals: [],
|
|
135
|
+
sessions: 61 * PER_TURN,
|
|
136
|
+
});
|
|
137
|
+
expect(body.window.days).toBe(30);
|
|
138
|
+
expect(body.calibration.curve.smart.usd).toHaveLength(61);
|
|
139
|
+
expect(body.gaps).toHaveLength(SAVINGS_TIERS.length * 2);
|
|
140
|
+
expect(body.meanRatio).toBeGreaterThan(0);
|
|
141
|
+
expect(await storedRows()).toHaveLength(0);
|
|
142
|
+
expect(await get()).toMatchObject({ source: "shipped", stored: null });
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
test("apply stores the curve, GET reports it, and the savings endpoints see it at once", async () => {
|
|
146
|
+
await scaleTo(1);
|
|
147
|
+
const jobs = async () => {
|
|
148
|
+
const res = await fetch(url(`/api/savings/jobs?workflowId=${workflowId}`));
|
|
149
|
+
return (await res.json()) as any;
|
|
150
|
+
};
|
|
151
|
+
expect((await jobs()).calibrationSource).toBe("shipped"); // fills the 60 s curve cache
|
|
152
|
+
|
|
153
|
+
const { status, body } = await post({ apply: true });
|
|
154
|
+
expect(status).toBe(200);
|
|
155
|
+
expect(body).toMatchObject({ applied: true, refusals: [] });
|
|
156
|
+
const row = JSON.parse(((await storedRows())[0] as { value: string }).value);
|
|
157
|
+
expect(row.curve.smart.usd).toEqual(body.calibration.curve.smart.usd);
|
|
158
|
+
|
|
159
|
+
expect(await get()).toMatchObject({
|
|
160
|
+
source: "deployment",
|
|
161
|
+
stored: { valid: true, window: { sessions: 366 } },
|
|
162
|
+
});
|
|
163
|
+
expect((await jobs()).calibrationSource).toBe("deployment");
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
test("a derived curve far from the shipped one is returned but never applied", async () => {
|
|
167
|
+
await getDbClient().run("DELETE FROM swarm_config WHERE key = ?", [SAVINGS_CALIBRATION_KEY]);
|
|
168
|
+
await scaleTo(4); // 4x the shipped mean
|
|
169
|
+
const { body } = await post({ apply: true });
|
|
170
|
+
expect(body.calibration).not.toBeNull();
|
|
171
|
+
expect(body.applied).toBe(false);
|
|
172
|
+
expect(body.refusals[0]).toMatch(/not applied: derived smart curve is [\d.]+x the shipped one/);
|
|
173
|
+
expect(await storedRows()).toHaveLength(0);
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
test("too few sessions: refused, nothing derived, nothing stored", async () => {
|
|
177
|
+
await getDbClient().run("DELETE FROM session_costs");
|
|
178
|
+
const { status, body } = await post({ apply: true });
|
|
179
|
+
expect(status).toBe(200);
|
|
180
|
+
expect(body).toMatchObject({ applied: false, calibration: null, gaps: [], sessions: 0 });
|
|
181
|
+
expect(body.refusals[0]).toMatch(/only 0 opus sessions/);
|
|
182
|
+
expect(await storedRows()).toHaveLength(0);
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
test("a malformed stored row is reported invalid, served as shipped, and replaced by apply", async () => {
|
|
186
|
+
await seed(1);
|
|
187
|
+
await scaleTo(1);
|
|
188
|
+
await getDbClient().run("DELETE FROM swarm_config WHERE key = ?", [SAVINGS_CALIBRATION_KEY]);
|
|
189
|
+
await getDbClient().run(
|
|
190
|
+
"INSERT INTO swarm_config (id, scope, scopeId, key, value, isSecret, createdAt, lastUpdatedAt) VALUES (?, 'global', NULL, ?, '{not json', 0, 'now', 'now')",
|
|
191
|
+
[crypto.randomUUID(), SAVINGS_CALIBRATION_KEY],
|
|
192
|
+
);
|
|
193
|
+
expect(await get()).toMatchObject({ source: "shipped", stored: { valid: false } });
|
|
194
|
+
expect((await post({ apply: true })).body.applied).toBe(true);
|
|
195
|
+
expect(await get()).toMatchObject({ source: "deployment", stored: { valid: true } });
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
test("a second concurrent run gets 409", async () => {
|
|
199
|
+
const statuses = (await Promise.all([post({}), post({})])).map((r) => r.status).sort();
|
|
200
|
+
expect(statuses).toEqual([200, 409]);
|
|
201
|
+
expect((await post({})).status).toBe(200); // the lock is released afterwards
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
test("a run past the deadline is a 504 and releases the lock", async () => {
|
|
205
|
+
setCalibrationDeadlineMs(1);
|
|
206
|
+
try {
|
|
207
|
+
const { status, body } = await post({});
|
|
208
|
+
expect(status).toBe(504);
|
|
209
|
+
expect(body.error).toMatch(/deadline/);
|
|
210
|
+
} finally {
|
|
211
|
+
setCalibrationDeadlineMs(null);
|
|
212
|
+
}
|
|
213
|
+
expect((await post({})).status).toBe(200);
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
test("a contended write past the deadline is a 504 and is never committed late", async () => {
|
|
217
|
+
await getDbClient().run("DELETE FROM swarm_config WHERE key = ?", [SAVINGS_CALIBRATION_KEY]);
|
|
218
|
+
await seed(1);
|
|
219
|
+
await scaleTo(1);
|
|
220
|
+
// A second connection plays an external writer (litestream checkpoint, CLI) for longer than the deadline.
|
|
221
|
+
const locker = new Database(TEST_DB_PATH);
|
|
222
|
+
locker.exec("PRAGMA busy_timeout = 25");
|
|
223
|
+
locker.run("BEGIN IMMEDIATE");
|
|
224
|
+
const holdMs = 2500;
|
|
225
|
+
const releaseAt = Date.now() + holdMs;
|
|
226
|
+
const release = setTimeout(() => locker.run("ROLLBACK"), holdMs);
|
|
227
|
+
setCalibrationDeadlineMs(1000);
|
|
228
|
+
try {
|
|
229
|
+
const startedAt = Date.now();
|
|
230
|
+
const { status, body } = await post({ apply: true });
|
|
231
|
+
const elapsed = Date.now() - startedAt;
|
|
232
|
+
expect(status).toBe(504);
|
|
233
|
+
expect(body.error).toMatch(/deadline/);
|
|
234
|
+
// Answered at the deadline, not after the holder let go.
|
|
235
|
+
expect(Date.now()).toBeLessThan(releaseAt);
|
|
236
|
+
expect(elapsed).toBeLessThan(holdMs);
|
|
237
|
+
// Outwait the holder: a late retry would commit now.
|
|
238
|
+
await new Promise((r) => setTimeout(r, releaseAt - Date.now() + 700));
|
|
239
|
+
expect(await storedRows()).toHaveLength(0);
|
|
240
|
+
} finally {
|
|
241
|
+
clearTimeout(release);
|
|
242
|
+
if (locker.inTransaction) locker.run("ROLLBACK");
|
|
243
|
+
locker.close();
|
|
244
|
+
setCalibrationDeadlineMs(null);
|
|
245
|
+
}
|
|
246
|
+
// The lock is released and an uncontended apply works again.
|
|
247
|
+
expect((await post({ apply: true })).body.applied).toBe(true);
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
test("apply needs config write access; a dry run does not", async () => {
|
|
251
|
+
await getDbClient().run("DELETE FROM swarm_config WHERE key = ?", [SAVINGS_CALIBRATION_KEY]);
|
|
252
|
+
const guest = { "x-test-guest": "1" };
|
|
253
|
+
expect((await post({ apply: true }, guest)).status).toBe(403);
|
|
254
|
+
expect(await storedRows()).toHaveLength(0);
|
|
255
|
+
expect((await post({}, guest)).status).toBe(200);
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
test("invalid input is a 400", async () => {
|
|
259
|
+
expect((await post({ days: 0 })).status).toBe(400);
|
|
260
|
+
expect((await post({ days: 91 })).status).toBe(400);
|
|
261
|
+
expect((await post({ end: "yesterday" })).status).toBe(400);
|
|
262
|
+
expect((await post({ apply: "yes" })).status).toBe(400);
|
|
263
|
+
});
|
|
264
|
+
});
|