@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
|
@@ -9,11 +9,14 @@ import type { IncomingMessage, ServerResponse } from "node:http";
|
|
|
9
9
|
import { isAlias } from "@desplega/model-catalog";
|
|
10
10
|
import { z } from "zod";
|
|
11
11
|
import { resolveHttpAuditUserId } from "../be/audit-user";
|
|
12
|
+
import { getDbClient } from "../be/db";
|
|
12
13
|
import {
|
|
13
14
|
createModelProfile,
|
|
14
15
|
deleteModelProfile,
|
|
15
16
|
getModelProfileByName,
|
|
16
17
|
listModelProfiles,
|
|
18
|
+
MAX_MODEL_PROFILE_FALLBACKS,
|
|
19
|
+
modelProfileFallbacksError,
|
|
17
20
|
modelProfileReferences,
|
|
18
21
|
updateModelProfile,
|
|
19
22
|
} from "../be/db/model-profiles";
|
|
@@ -71,7 +74,7 @@ const createProfile = route({
|
|
|
71
74
|
pattern: ["api", "model-profiles"],
|
|
72
75
|
summary: "Create a model profile",
|
|
73
76
|
description:
|
|
74
|
-
"A profile runs on one harness. `model` must run on that harness and be in the model catalog unless `allowCustomModel` is true (a gateway id, a fresh launch); a `latest:` alias is accepted and resolves at claim. `effort` must be a level the harness and model support. `credentials` pins which pooled keys the profile uses: per credential var of the harness, the last 5 characters of each allowed key (`CODEX_OAUTH` is not supported). Operator only (the shared API key).",
|
|
77
|
+
"A profile runs on one harness. `model` must run on that harness and be in the model catalog unless `allowCustomModel` is true (a gateway id, a fresh launch); a `latest:` alias is accepted and resolves at claim. `effort` must be a level the harness and model support. `credentials` pins which pooled keys the profile uses: per credential var of the harness, the last 5 characters of each allowed key (`CODEX_OAUTH` is not supported). `fallbacks` lists profiles on the same harness, in order, that a task on this profile hops to when a provider error (rate limit, seat, overload, auth, empty key pool) ends it; no repeats, no self, no cycle. Operator only (the shared API key).",
|
|
75
78
|
tags: ["Models"],
|
|
76
79
|
rbac: { permission: "models.profiles.write" },
|
|
77
80
|
body: z.object({
|
|
@@ -82,6 +85,7 @@ const createProfile = route({
|
|
|
82
85
|
allowCustomModel: z.boolean().optional().default(false),
|
|
83
86
|
description: z.string().trim().max(500).nullable().optional(),
|
|
84
87
|
credentials: ModelProfileCredentialsSchema.nullable().optional(),
|
|
88
|
+
fallbacks: z.array(ModelProfileNameSchema).max(MAX_MODEL_PROFILE_FALLBACKS).optional(),
|
|
85
89
|
}),
|
|
86
90
|
responses: {
|
|
87
91
|
201: { description: "Profile created", schema: ModelProfileSchema },
|
|
@@ -97,7 +101,7 @@ const updateProfile = route({
|
|
|
97
101
|
pattern: ["api", "model-profiles", null],
|
|
98
102
|
summary: "Update a model profile",
|
|
99
103
|
description:
|
|
100
|
-
"Changes the model, effort, custom-model flag, description
|
|
104
|
+
"Changes the model, effort, custom-model flag, description, credential allowlist or fallbacks; the name and harness are fixed. Omit a field to keep it; `effort: null` and `credentials: null` clear it, and `fallbacks: []` empties the list. Applies to tasks claimed after the change. Operator only (the shared API key).",
|
|
101
105
|
tags: ["Models"],
|
|
102
106
|
rbac: { permission: "models.profiles.write" },
|
|
103
107
|
params: z.object({ name: z.string() }),
|
|
@@ -107,6 +111,7 @@ const updateProfile = route({
|
|
|
107
111
|
allowCustomModel: z.boolean().optional(),
|
|
108
112
|
description: z.string().trim().max(500).nullable().optional(),
|
|
109
113
|
credentials: ModelProfileCredentialsSchema.nullable().optional(),
|
|
114
|
+
fallbacks: z.array(ModelProfileNameSchema).max(MAX_MODEL_PROFILE_FALLBACKS).optional(),
|
|
110
115
|
}),
|
|
111
116
|
responses: {
|
|
112
117
|
200: { description: "Profile updated", schema: ModelProfileSchema },
|
|
@@ -122,7 +127,7 @@ const deleteProfile = route({
|
|
|
122
127
|
pattern: ["api", "model-profiles", null],
|
|
123
128
|
summary: "Delete a model profile",
|
|
124
129
|
description:
|
|
125
|
-
"Refused (409) for a shipped `<harness>-<tier>` seed (edit it instead; it would come back at the next boot), and while an agent has the profile as its default, a schedule names it, or a task holds it (unfinished, or superseded and still owed its resume). Operator only (the shared API key).",
|
|
130
|
+
"Refused (409) for a shipped `<harness>-<tier>` seed (edit it instead; it would come back at the next boot), and while an agent has the profile as its default, a schedule names it, another profile lists it as a fallback, or a task holds it (unfinished, or superseded and still owed its resume). Operator only (the shared API key).",
|
|
126
131
|
tags: ["Models"],
|
|
127
132
|
rbac: { permission: "models.profiles.write" },
|
|
128
133
|
params: z.object({ name: z.string() }),
|
|
@@ -258,9 +263,21 @@ export async function handleModelProfiles(
|
|
|
258
263
|
jsonError(res, error, 400);
|
|
259
264
|
return true;
|
|
260
265
|
}
|
|
261
|
-
const
|
|
262
|
-
|
|
266
|
+
const userId = await resolveHttpAuditUserId(req, myAgentId);
|
|
267
|
+
// The fallback graph check and the write share one transaction, so two
|
|
268
|
+
// concurrent saves cannot each pass and together form a cycle.
|
|
269
|
+
const outcome = await getDbClient().transaction(async () => {
|
|
270
|
+
const fallbackError = body.fallbacks
|
|
271
|
+
? await modelProfileFallbacksError(body.name, body.harness, body.fallbacks)
|
|
272
|
+
: null;
|
|
273
|
+
if (fallbackError) return { error: fallbackError };
|
|
274
|
+
return { created: await createModelProfile(body, { userId }) };
|
|
263
275
|
});
|
|
276
|
+
if (typeof outcome.error === "string") {
|
|
277
|
+
jsonError(res, outcome.error, 400);
|
|
278
|
+
return true;
|
|
279
|
+
}
|
|
280
|
+
const created = outcome.created;
|
|
264
281
|
if (!created) {
|
|
265
282
|
jsonError(res, `Model profile "${body.name}" already exists`, 409);
|
|
266
283
|
return true;
|
|
@@ -290,9 +307,19 @@ export async function handleModelProfiles(
|
|
|
290
307
|
jsonError(res, error, 400);
|
|
291
308
|
return true;
|
|
292
309
|
}
|
|
293
|
-
const
|
|
294
|
-
|
|
310
|
+
const userId = await resolveHttpAuditUserId(req, myAgentId);
|
|
311
|
+
const outcome = await getDbClient().transaction(async () => {
|
|
312
|
+
const fallbackError = body.fallbacks
|
|
313
|
+
? await modelProfileFallbacksError(current.name, current.harness, body.fallbacks)
|
|
314
|
+
: null;
|
|
315
|
+
if (fallbackError) return { error: fallbackError };
|
|
316
|
+
return { updated: await updateModelProfile(current.name, body, { userId }) };
|
|
295
317
|
});
|
|
318
|
+
if (typeof outcome.error === "string") {
|
|
319
|
+
jsonError(res, outcome.error, 400);
|
|
320
|
+
return true;
|
|
321
|
+
}
|
|
322
|
+
const updated = outcome.updated;
|
|
296
323
|
if (!updated) {
|
|
297
324
|
jsonError(res, "Model profile not found", 404);
|
|
298
325
|
return true;
|
|
@@ -319,11 +346,17 @@ export async function handleModelProfiles(
|
|
|
319
346
|
return true;
|
|
320
347
|
}
|
|
321
348
|
const refs = await modelProfileReferences(current.name);
|
|
322
|
-
if (
|
|
349
|
+
if (
|
|
350
|
+
refs.agentIds.length > 0 ||
|
|
351
|
+
refs.openTaskCount > 0 ||
|
|
352
|
+
refs.scheduleIds.length > 0 ||
|
|
353
|
+
refs.fallbackOf.length > 0
|
|
354
|
+
) {
|
|
323
355
|
const schedules = refs.scheduleIds.length > 0 ? ` (${refs.scheduleIds.join(", ")})` : "";
|
|
356
|
+
const fallbackOf = refs.fallbackOf.length > 0 ? ` (${refs.fallbackOf.join(", ")})` : "";
|
|
324
357
|
jsonError(
|
|
325
358
|
res,
|
|
326
|
-
`"${current.name}" is in use: default of ${refs.agentIds.length} agent(s), held by ${refs.openTaskCount} unfinished or awaiting-resume task(s), named by ${refs.scheduleIds.length} schedule(s)${schedules}.`,
|
|
359
|
+
`"${current.name}" is in use: default of ${refs.agentIds.length} agent(s), held by ${refs.openTaskCount} unfinished or awaiting-resume task(s), named by ${refs.scheduleIds.length} schedule(s)${schedules}, a fallback of ${refs.fallbackOf.length} profile(s)${fallbackOf}.`,
|
|
327
360
|
409,
|
|
328
361
|
);
|
|
329
362
|
return true;
|
|
@@ -334,7 +367,7 @@ export async function handleModelProfiles(
|
|
|
334
367
|
if (outcome === "in-use") {
|
|
335
368
|
jsonError(
|
|
336
369
|
res,
|
|
337
|
-
`"${current.name}" is in use by a task, a schedule
|
|
370
|
+
`"${current.name}" is in use by a task, a schedule, an agent default or a fallback list written just now.`,
|
|
338
371
|
409,
|
|
339
372
|
);
|
|
340
373
|
return true;
|
package/src/http/poll.ts
CHANGED
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
import type { IncomingMessage, ServerResponse } from "node:http";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import { getDbClient, getSwarmConfigs, upsertSwarmConfig } from "../be/db";
|
|
4
|
+
import { type DbParam, DbTransactionDeadlineError } from "../be/db-client";
|
|
5
|
+
import { calibrate, type DerivedCurveSet } from "../be/savings/calibrate";
|
|
6
|
+
import type { SavingsSql } from "../be/savings/compute";
|
|
7
|
+
import {
|
|
8
|
+
type CurveSet,
|
|
9
|
+
SAVINGS_CALIBRATION_KEY,
|
|
10
|
+
SAVINGS_TIERS,
|
|
11
|
+
SHIPPED_CALIBRATION,
|
|
12
|
+
StoredCalibrationSchema,
|
|
13
|
+
} from "../be/savings/model";
|
|
14
|
+
import { ensureConfigAdmin } from "./config";
|
|
15
|
+
import { DbQueryConcurrencyCapError, DbQueryTimeoutError } from "./db-query-shared";
|
|
16
|
+
import { route } from "./route-def";
|
|
17
|
+
import { boundedSql, clearCalibrationCache } from "./savings";
|
|
18
|
+
import { json, jsonError } from "./utils";
|
|
19
|
+
|
|
20
|
+
/** A run that has not finished by now is abandoned with a 504. */
|
|
21
|
+
const DEADLINE_MS = 120_000;
|
|
22
|
+
let deadlineMs = DEADLINE_MS;
|
|
23
|
+
let running = false;
|
|
24
|
+
|
|
25
|
+
/** Test hook: shrink the deadline. */
|
|
26
|
+
export function setCalibrationDeadlineMs(ms: number | null): void {
|
|
27
|
+
deadlineMs = ms ?? DEADLINE_MS;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
class DeadlineError extends Error {}
|
|
31
|
+
|
|
32
|
+
const plain = (c: CurveSet): DerivedCurveSet =>
|
|
33
|
+
Object.fromEntries(
|
|
34
|
+
SAVINGS_TIERS.map((t) => [t, { usd: [...c[t].usd], hrs: [...c[t].hrs] }]),
|
|
35
|
+
) as DerivedCurveSet;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* `boundedSql` (child process, per-query budget, shared concurrency cap) under one
|
|
39
|
+
* wall-clock deadline for the whole run. A full concurrency cap is waited out, not read as empty.
|
|
40
|
+
*/
|
|
41
|
+
function deadlineSql(deadlineAt: number): SavingsSql {
|
|
42
|
+
return async <T>(sql: string, params: DbParam[]) => {
|
|
43
|
+
for (let attempt = 0; ; attempt++) {
|
|
44
|
+
const left = deadlineAt - Date.now();
|
|
45
|
+
if (left <= 0) throw new DeadlineError();
|
|
46
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
47
|
+
const query = boundedSql<T>(sql, params);
|
|
48
|
+
query.catch(() => {}); // an abandoned query's late failure is not an unhandled rejection
|
|
49
|
+
try {
|
|
50
|
+
return await Promise.race([
|
|
51
|
+
query,
|
|
52
|
+
new Promise<never>((_, reject) => {
|
|
53
|
+
timer = setTimeout(() => reject(new DeadlineError()), left);
|
|
54
|
+
}),
|
|
55
|
+
]);
|
|
56
|
+
} catch (err) {
|
|
57
|
+
if (!(err instanceof DbQueryConcurrencyCapError)) throw err;
|
|
58
|
+
// Never sleep past the deadline: the next turn then fails on time.
|
|
59
|
+
const nap = Math.min(250 * 2 ** attempt, 2000, Math.max(0, deadlineAt - Date.now()));
|
|
60
|
+
await new Promise((r) => setTimeout(r, nap));
|
|
61
|
+
} finally {
|
|
62
|
+
clearTimeout(timer);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const TierCurvesSchema = z.object({ usd: z.array(z.number()), hrs: z.array(z.number()) });
|
|
69
|
+
const CurveSetSchema = z.object(
|
|
70
|
+
Object.fromEntries(SAVINGS_TIERS.map((t) => [t, TierCurvesSchema])) as Record<
|
|
71
|
+
(typeof SAVINGS_TIERS)[number],
|
|
72
|
+
typeof TierCurvesSchema
|
|
73
|
+
>,
|
|
74
|
+
);
|
|
75
|
+
const WindowSchema = z.object({ start: z.string(), end: z.string(), sessions: z.number().int() });
|
|
76
|
+
|
|
77
|
+
const StoredSchema = z.object({
|
|
78
|
+
calibratedAt: z.string(),
|
|
79
|
+
model: z.string(),
|
|
80
|
+
curve: CurveSetSchema,
|
|
81
|
+
window: WindowSchema.optional(),
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
const GapSchema = z.object({
|
|
85
|
+
tier: z.enum(SAVINGS_TIERS),
|
|
86
|
+
key: z.enum(["usd", "hrs"]),
|
|
87
|
+
median: z.number().describe("Median absolute deviation from the shipped curve, as a fraction."),
|
|
88
|
+
max: z.number(),
|
|
89
|
+
over5pct: z.number().int().describe("Turn counts more than 5% from the shipped curve."),
|
|
90
|
+
points: z.number().int(),
|
|
91
|
+
weighted: z
|
|
92
|
+
.number()
|
|
93
|
+
.describe("Session-weighted mean of derived over shipped; 1 means the same price."),
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
const getRoute = route({
|
|
97
|
+
method: "get",
|
|
98
|
+
path: "/api/savings/calibration",
|
|
99
|
+
pattern: ["api", "savings", "calibration"],
|
|
100
|
+
summary: "The savings curve in use",
|
|
101
|
+
description:
|
|
102
|
+
"Which curve prices the savings endpoints: the one shipped with the release, or one derived from this deployment's sessions and stored by `POST /api/savings/calibration`. Reports the stored row's metadata (a malformed row is reported as invalid and ignored by the savings endpoints).",
|
|
103
|
+
tags: ["Savings"],
|
|
104
|
+
responses: {
|
|
105
|
+
200: {
|
|
106
|
+
description: "The active calibration",
|
|
107
|
+
schema: z.object({
|
|
108
|
+
source: z.enum(["shipped", "deployment"]),
|
|
109
|
+
active: StoredSchema.omit({ window: true }),
|
|
110
|
+
stored: z
|
|
111
|
+
.object({
|
|
112
|
+
valid: z.boolean(),
|
|
113
|
+
updatedAt: z.string(),
|
|
114
|
+
calibratedAt: z.string().optional(),
|
|
115
|
+
model: z.string().optional(),
|
|
116
|
+
window: WindowSchema.optional(),
|
|
117
|
+
})
|
|
118
|
+
.nullable(),
|
|
119
|
+
}),
|
|
120
|
+
},
|
|
121
|
+
},
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
const postRoute = route({
|
|
125
|
+
method: "post",
|
|
126
|
+
path: "/api/savings/calibration",
|
|
127
|
+
pattern: ["api", "savings", "calibration"],
|
|
128
|
+
summary: "Derive the savings curve from this deployment's sessions",
|
|
129
|
+
description:
|
|
130
|
+
"Reads the window's opus sessions through the bounded query executor, derives the per-tier cost and hours curve, and compares it to the shipped one. Read-only unless `apply` is true, which also requires `config.write.any`: then the curve is stored in the global `SAVINGS_CURVE_CALIBRATION` config row and the savings endpoints use it within seconds. Nothing is stored when the data cannot support a curve or the derived mean cost is more than 3x or less than a third of the shipped one. One run at a time; a run past 120 s is abandoned with 504.",
|
|
131
|
+
tags: ["Savings"],
|
|
132
|
+
body: z.object({
|
|
133
|
+
days: z.number().int().min(1).max(90).default(30),
|
|
134
|
+
end: z.iso.datetime().optional().describe("End of the window (ISO 8601); default now."),
|
|
135
|
+
apply: z.boolean().default(false),
|
|
136
|
+
}),
|
|
137
|
+
responses: {
|
|
138
|
+
200: {
|
|
139
|
+
description: "The derived curve, or why there is none",
|
|
140
|
+
schema: z.object({
|
|
141
|
+
window: z.object({ start: z.string(), end: z.string(), days: z.number().int() }),
|
|
142
|
+
sessions: z.number().int().describe("Opus sessions of 1 to 61 turns used."),
|
|
143
|
+
refusals: z
|
|
144
|
+
.array(z.string())
|
|
145
|
+
.describe("Why no curve was derived, or why a derived one was not applied."),
|
|
146
|
+
calibration: StoredSchema.nullable().describe("The derived curve; null when refused."),
|
|
147
|
+
speed: z.record(z.string(), z.number()).nullable(),
|
|
148
|
+
repriceMatch: z.number().nullable(),
|
|
149
|
+
meanRatio: z
|
|
150
|
+
.number()
|
|
151
|
+
.nullable()
|
|
152
|
+
.describe("Derived smart-tier mean cost over the shipped one."),
|
|
153
|
+
gaps: z.array(GapSchema),
|
|
154
|
+
applied: z.boolean(),
|
|
155
|
+
}),
|
|
156
|
+
},
|
|
157
|
+
400: { description: "Invalid days, end or apply" },
|
|
158
|
+
403: { description: "`apply` without permission to write config" },
|
|
159
|
+
408: { description: "A session query ran past its time budget" },
|
|
160
|
+
409: { description: "Another calibration run is in progress" },
|
|
161
|
+
504: { description: "The run passed its 120 s deadline" },
|
|
162
|
+
},
|
|
163
|
+
rbac: {
|
|
164
|
+
ungated:
|
|
165
|
+
"a dry run only reads sessions; apply:true is gated by config.write.any in the handler",
|
|
166
|
+
},
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
export async function handleSavingsCalibration(
|
|
170
|
+
req: IncomingMessage,
|
|
171
|
+
res: ServerResponse,
|
|
172
|
+
pathSegments: string[],
|
|
173
|
+
queryParams: URLSearchParams,
|
|
174
|
+
): Promise<boolean> {
|
|
175
|
+
if (getRoute.match(req.method, pathSegments)) {
|
|
176
|
+
const [row] = await getSwarmConfigs({ scope: "global", key: SAVINGS_CALIBRATION_KEY });
|
|
177
|
+
let parsed: z.infer<typeof StoredCalibrationSchema> | null = null;
|
|
178
|
+
try {
|
|
179
|
+
const result = StoredCalibrationSchema.safeParse(JSON.parse(row?.value ?? "null"));
|
|
180
|
+
parsed = result.success ? result.data : null;
|
|
181
|
+
} catch {
|
|
182
|
+
// malformed JSON: reported below as an invalid row
|
|
183
|
+
}
|
|
184
|
+
const active = parsed ?? { ...SHIPPED_CALIBRATION, curve: plain(SHIPPED_CALIBRATION.curve) };
|
|
185
|
+
getRoute.respond(res, 200, {
|
|
186
|
+
source: parsed ? "deployment" : "shipped",
|
|
187
|
+
active: { calibratedAt: active.calibratedAt, model: active.model, curve: active.curve },
|
|
188
|
+
stored: row
|
|
189
|
+
? {
|
|
190
|
+
valid: parsed !== null,
|
|
191
|
+
updatedAt: row.lastUpdatedAt,
|
|
192
|
+
...(parsed
|
|
193
|
+
? {
|
|
194
|
+
calibratedAt: parsed.calibratedAt,
|
|
195
|
+
model: parsed.model,
|
|
196
|
+
...(parsed.window ? { window: parsed.window } : {}),
|
|
197
|
+
}
|
|
198
|
+
: {}),
|
|
199
|
+
}
|
|
200
|
+
: null,
|
|
201
|
+
});
|
|
202
|
+
return true;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
if (!postRoute.match(req.method, pathSegments)) return false;
|
|
206
|
+
const parsed = await postRoute.parse(req, res, pathSegments, queryParams);
|
|
207
|
+
if (!parsed) return true;
|
|
208
|
+
const { days, end, apply } = parsed.body;
|
|
209
|
+
// One clock for the whole request: the permission check, every query, the derivation and the write.
|
|
210
|
+
const deadlineAt = Date.now() + deadlineMs;
|
|
211
|
+
if (apply && !(await ensureConfigAdmin(req, res, "config.write.any"))) return true;
|
|
212
|
+
if (running) {
|
|
213
|
+
jsonError(res, "A calibration run is already in progress", 409);
|
|
214
|
+
return true;
|
|
215
|
+
}
|
|
216
|
+
running = true;
|
|
217
|
+
try {
|
|
218
|
+
const run = await calibrate(deadlineSql(deadlineAt), {
|
|
219
|
+
days,
|
|
220
|
+
end: end ? new Date(end) : new Date(),
|
|
221
|
+
});
|
|
222
|
+
// The derivation between queries is CPU work no query race can cut short.
|
|
223
|
+
if (Date.now() >= deadlineAt) throw new DeadlineError();
|
|
224
|
+
const refusals = run.refusal ? [run.refusal] : [];
|
|
225
|
+
let applied = false;
|
|
226
|
+
if (apply && run.calibration) {
|
|
227
|
+
if (run.applyBlocked) {
|
|
228
|
+
refusals.push(`not applied: ${run.applyBlocked}`);
|
|
229
|
+
} else {
|
|
230
|
+
StoredCalibrationSchema.parse(run.calibration);
|
|
231
|
+
// The outer transaction owns the deadline (upsertSwarmConfig's own one becomes a
|
|
232
|
+
// savepoint inside it): BEGIN stops retrying and COMMIT is skipped once it has
|
|
233
|
+
// passed, so a 504 is never followed by a late write.
|
|
234
|
+
await getDbClient().transaction(
|
|
235
|
+
() =>
|
|
236
|
+
upsertSwarmConfig({
|
|
237
|
+
scope: "global",
|
|
238
|
+
key: SAVINGS_CALIBRATION_KEY,
|
|
239
|
+
value: JSON.stringify(run.calibration),
|
|
240
|
+
// Read by the API only; keep the ~9 KB blob out of every agent's environment.
|
|
241
|
+
exportToEnv: false,
|
|
242
|
+
description: "Savings session curve derived from this deployment's sessions",
|
|
243
|
+
}),
|
|
244
|
+
{ deadlineAt },
|
|
245
|
+
);
|
|
246
|
+
clearCalibrationCache();
|
|
247
|
+
applied = true;
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
postRoute.respond(res, 200, {
|
|
251
|
+
window: run.window,
|
|
252
|
+
sessions: run.sessions,
|
|
253
|
+
refusals,
|
|
254
|
+
calibration: run.calibration,
|
|
255
|
+
speed: run.speed,
|
|
256
|
+
repriceMatch: run.repriceMatch,
|
|
257
|
+
meanRatio: run.meanRatio,
|
|
258
|
+
gaps: run.gaps,
|
|
259
|
+
applied,
|
|
260
|
+
});
|
|
261
|
+
} catch (err) {
|
|
262
|
+
if (err instanceof DeadlineError || err instanceof DbTransactionDeadlineError) {
|
|
263
|
+
jsonError(
|
|
264
|
+
res,
|
|
265
|
+
`Calibration passed its ${deadlineMs / 1000} s deadline; use a shorter window`,
|
|
266
|
+
504,
|
|
267
|
+
);
|
|
268
|
+
} else if (err instanceof DbQueryTimeoutError) {
|
|
269
|
+
json(res, { error: "db_query_timeout", message: err.message }, 408);
|
|
270
|
+
} else {
|
|
271
|
+
throw err;
|
|
272
|
+
}
|
|
273
|
+
} finally {
|
|
274
|
+
running = false;
|
|
275
|
+
}
|
|
276
|
+
return true;
|
|
277
|
+
}
|