@desplega.ai/agent-swarm 2.8.0 → 2.8.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (216) hide show
  1. package/dist/{acp-adapter-t949vbbp.js → acp-adapter-fqzkxyra.js} +3 -3
  2. package/dist/{actions-7e9czc7s.js → actions-kxek041g.js} +18 -18
  3. package/dist/{amp-adapter-gv6cbb7d.js → amp-adapter-ce7jjke4.js} +1 -1
  4. package/dist/{app-b9fbkp9v.js → app-v6zz8yh0.js} +3 -3
  5. package/dist/{assistant-wjb7v4c7.js → assistant-m0sw9xx2.js} +19 -19
  6. package/dist/{boot-args-schema-2t37e0fe.js → boot-args-schema-66bk37wv.js} +1 -1
  7. package/dist/{boot-reembed-gka3zas7.js → boot-reembed-edjpwsc1.js} +1 -1
  8. package/dist/{boot-reembed-a35nkyr1.js → boot-reembed-y27n09tz.js} +1 -1
  9. package/dist/{boot-scrub-sweep-65sdnp6c.js → boot-scrub-sweep-1xc35965.js} +3 -3
  10. package/dist/{boot-seeds-x42tzytw.js → boot-seeds-7hjj91qj.js} +4 -4
  11. package/dist/{boxd-qbkxy6ap.js → boxd-ent8ch04.js} +1 -1
  12. package/dist/{claude-adapter-v221b2fd.js → claude-adapter-stw7jdcf.js} +3 -3
  13. package/dist/{claude-managed-adapter-29eq09wd.js → claude-managed-adapter-jh6c523j.js} +2 -2
  14. package/dist/{claude-managed-setup-9m6450zk.js → claude-managed-setup-y6z2perp.js} +5 -5
  15. package/dist/{claude-sdk-session-0q0ptaba.js → claude-sdk-session-vvya8wqk.js} +3 -3
  16. package/dist/{cli-wk18cm92.js → cli-0cq8y6m7.js} +1 -1
  17. package/dist/{cli-p1w18yhx.js → cli-1pz0330a.js} +1 -1
  18. package/dist/{cli-y97qxcfx.js → cli-1qkz151v.js} +3 -3
  19. package/dist/{cli-qwca7s44.js → cli-26ctbcdp.js} +18 -6
  20. package/dist/{cli-8fknbnhw.js → cli-2kvhc7x8.js} +7 -7
  21. package/dist/{cli-rfywm85n.js → cli-3e875j5p.js} +136 -49
  22. package/dist/{cli-6wxjceza.js → cli-3sy2xags.js} +10 -10
  23. package/dist/{cli-a7wvp2km.js → cli-3vmb0n58.js} +1 -1
  24. package/dist/{cli-p1bq50aa.js → cli-4vzgeta4.js} +13 -13
  25. package/dist/{cli-m28atx8q.js → cli-5y340nzn.js} +4 -4
  26. package/dist/{cli-7p1kvkck.js → cli-6rc5frdm.js} +1 -1
  27. package/dist/{cli-tph5wt3y.js → cli-6wj7p986.js} +1 -1
  28. package/dist/{cli-t73cas91.js → cli-7349va39.js} +8100 -7620
  29. package/dist/{cli-ee0b971j.js → cli-837vfs2x.js} +2 -2
  30. package/dist/{cli-abejgen1.js → cli-959kp5h2.js} +12 -11
  31. package/dist/{cli-8kzasymh.js → cli-97ae65rz.js} +4 -4
  32. package/dist/{cli-m4dqmnbp.js → cli-9mb2q6pb.js} +1 -1
  33. package/dist/{cli-p2gfst4m.js → cli-a0z912j7.js} +123 -35
  34. package/dist/{cli-4txa0ter.js → cli-b98a65ed.js} +3 -3
  35. package/dist/{cli-k0wwfqbg.js → cli-dns0y538.js} +4 -0
  36. package/dist/{cli-tk7cjj8h.js → cli-dzpc6en0.js} +1 -1
  37. package/dist/{cli-qa4wdgsy.js → cli-e1p3n5gq.js} +4 -4
  38. package/dist/{cli-gxkx0zzp.js → cli-f4e8r23x.js} +1 -1
  39. package/dist/{cli-qmqk2yc2.js → cli-f4r3g076.js} +1 -1
  40. package/dist/{cli-wcg8x116.js → cli-f6bahpn5.js} +1 -1
  41. package/dist/{cli-qsnnd04k.js → cli-fpjmegdb.js} +1 -1
  42. package/dist/{cli-j790gp2w.js → cli-fwy8kfd6.js} +271 -37
  43. package/dist/{cli-rmmpt0wm.js → cli-g5mtmf6t.js} +6 -6
  44. package/dist/{cli-rzsy6nc6.js → cli-gyt3zydq.js} +3 -3
  45. package/dist/{cli-pasjcnmj.js → cli-j24zbska.js} +3 -3
  46. package/dist/{cli-qfedvmcr.js → cli-jdknh18m.js} +10 -10
  47. package/dist/{cli-7b8sbmag.js → cli-kjtwrhea.js} +1 -1
  48. package/dist/{cli-gpwhbgx0.js → cli-m9s2qff2.js} +57 -25
  49. package/dist/{cli-2wnq8jkz.js → cli-mxn1qvbn.js} +3 -2
  50. package/dist/{cli-s7t6p9my.js → cli-n6vtrcht.js} +1 -1
  51. package/dist/{cli-2zp0et3v.js → cli-pchypa1s.js} +55 -19
  52. package/dist/{cli-yq3zak69.js → cli-pfs8adn4.js} +1 -1
  53. package/dist/{cli-c55z5chy.js → cli-q24n86d2.js} +2 -2
  54. package/dist/{cli-h38x262p.js → cli-rcecc3yq.js} +167 -43
  55. package/dist/{cli-xae4g51r.js → cli-rh15cakt.js} +1 -1
  56. package/dist/{cli-sdtt3men.js → cli-rmzsadkf.js} +1 -1
  57. package/dist/{cli-f3vq58d2.js → cli-t2tma8gb.js} +1 -1
  58. package/dist/{cli-m23zjcy2.js → cli-tekytdq9.js} +2 -2
  59. package/dist/{cli-tqegb8eq.js → cli-vdektqgf.js} +1 -1
  60. package/dist/{cli-ffhtssbp.js → cli-vj75tyaw.js} +1 -1
  61. package/dist/{cli-deefvewm.js → cli-w401b9tq.js} +1 -1
  62. package/dist/{cli-n9ftpys4.js → cli-x6t50dmw.js} +7 -7
  63. package/dist/{cli-6t09sdhy.js → cli-xf3qw745.js} +3 -3
  64. package/dist/{cli-hxpyc9sn.js → cli-z1w943va.js} +2 -2
  65. package/dist/{cli-334z18y3.js → cli-zh9rsbqf.js} +3 -3
  66. package/dist/cli.js +17 -17
  67. package/dist/{codex-adapter-30j9bpzd.js → codex-adapter-zerf5qsp.js} +1 -1
  68. package/dist/{codex-hook-h3vk6xzr.js → codex-hook-30gyhzxe.js} +1 -1
  69. package/dist/{codex-session-runner-8vp57v8w.js → codex-session-runner-89s304py.js} +1 -1
  70. package/dist/{commands-bngnz9g7.js → commands-jeh1b51j.js} +3 -3
  71. package/dist/{controller-hxx8wwy1.js → controller-mctfbrse.js} +3 -3
  72. package/dist/{controllers-connect-e2b4gyxc.js → controllers-connect-nxa1ddwn.js} +19 -19
  73. package/dist/{cursor-adapter-n5nc3mfe.js → cursor-adapter-qj8vehv6.js} +1 -1
  74. package/dist/{db-bgm7vkc0.js → db-2dvay0es.js} +3 -3
  75. package/dist/{db-c6np41ca.js → db-b36qn6k3.js} +2 -2
  76. package/dist/{dispatcher-jnybck15.js → dispatcher-gndbta01.js} +17 -17
  77. package/dist/{dsh-adapter-nrhbrjz8.js → dsh-adapter-f75sz821.js} +1 -1
  78. package/dist/{e2b-jkwfhbcv.js → e2b-3gpc2n3w.js} +1 -1
  79. package/dist/{e2b-s16txnpx.js → e2b-jbm7mj93.js} +1 -1
  80. package/dist/{embedding-space-crsm2zp0.js → embedding-space-4p05vb2x.js} +6 -6
  81. package/dist/{embeddings-e7z3f27f.js → embeddings-m6768mgh.js} +1 -1
  82. package/dist/{external-channel-m77wf5z3.js → external-channel-gjv3jne3.js} +2 -2
  83. package/dist/{fx-adapter-b6w0kf56.js → fx-adapter-rpsgb1tw.js} +4 -4
  84. package/dist/{grok-adapter-4sqtebdk.js → grok-adapter-n2m3zjg1.js} +4 -4
  85. package/dist/handlers-nv8edn19.js +32 -0
  86. package/dist/{heartbeat-xgb036sx.js → heartbeat-a1tv60gt.js} +20 -20
  87. package/dist/{hook-xsk1jbha.js → hook-8h5dzcgq.js} +2 -2
  88. package/dist/{hook-5h02c9ga.js → hook-cyaps9ba.js} +4 -4
  89. package/dist/{http-txe7qhvn.js → http-t15mhbd9.js} +1733 -79
  90. package/dist/{index-0eny15vj.js → index-2skqtd76.js} +2 -2
  91. package/dist/{index-w7em4ktq.js → index-d6wkh8ar.js} +2 -2
  92. package/dist/{index-rvccek1w.js → index-gjwaqaq8.js} +5 -5
  93. package/dist/{index-e5pfbsys.js → index-hjhfk3ra.js} +3 -3
  94. package/dist/{index-1qw6f2dc.js → index-khe3gp0k.js} +4 -4
  95. package/dist/index-ksmyd6kc.js +37 -0
  96. package/dist/{index-vk63emp7.js → index-qav6455f.js} +14 -12
  97. package/dist/index-rjnrje98.js +32 -0
  98. package/dist/index-tv3413wy.js +33 -0
  99. package/dist/{index-ysvbhg5m.js → index-w1zqf680.js} +1 -1
  100. package/dist/{keepalive-4zsrce78.js → keepalive-91s7hzrb.js} +3 -3
  101. package/dist/lead-3hmddvrm.js +70 -0
  102. package/dist/{maintenance-86jvartk.js → maintenance-7pjmy5xy.js} +1 -1
  103. package/dist/{node-je82wjet.js → node-4c5jqxr3.js} +1 -1
  104. package/dist/{oauth-refresh-sweep-j52kg9pr.js → oauth-refresh-sweep-rzqfm94j.js} +2 -2
  105. package/dist/{onboard-c2aj5p8q.js → onboard-74st7pag.js} +3 -3
  106. package/dist/{opencode-adapter-dsagkhtw.js → opencode-adapter-arn7gm8q.js} +2 -2
  107. package/dist/{otel-impl-yyy203z8.js → otel-impl-rnzey8cb.js} +1 -1
  108. package/dist/{pi-mono-adapter-546cd1cc.js → pi-mono-adapter-6mkwq64n.js} +3 -2
  109. package/dist/{pricing-refresh-1rgzqyzz.js → pricing-refresh-ha21jhk7.js} +2 -2
  110. package/dist/{rbac-roles-e70xfeyj.js → rbac-roles-59dxachk.js} +2 -2
  111. package/dist/{rbac-roles-q89qe8er.js → rbac-roles-wqhma4dr.js} +3 -3
  112. package/dist/{render-v2-k6tt107q.js → render-v2-14kpzx12.js} +3 -3
  113. package/dist/{resume-cnnrjfxa.js → resume-gsa5fb5f.js} +6 -6
  114. package/dist/{session-access-xkxkjw3n.js → session-access-skm7bnp6.js} +4 -4
  115. package/dist/{session-source-backfill-1d5pdwww.js → session-source-backfill-pbqzbncv.js} +2 -2
  116. package/dist/{setup-se8q7gkc.js → setup-5yermsem.js} +1 -1
  117. package/dist/{work-objects-1kxnbzy5.js → work-objects-xj0ck0w4.js} +20 -20
  118. package/dist/{worker-follow-up-zqm8apt4.js → worker-follow-up-911xc0b8.js} +19 -17
  119. package/dist/worker-qgg0m2gw.js +70 -0
  120. package/openapi.json +1741 -290
  121. package/package.json +3 -3
  122. package/src/apps/catalog.generated.json +120 -1
  123. package/src/apps/definition.ts +38 -11
  124. package/src/apps/page-validator.ts +88 -0
  125. package/src/be/db/model-profiles.ts +110 -11
  126. package/src/be/db/tasks/read.ts +20 -0
  127. package/src/be/db/workflow-run-definitions.ts +19 -8
  128. package/src/be/db-client.ts +43 -5
  129. package/src/be/db.ts +9 -3
  130. package/src/be/migrations/284_model_profile_fallbacks.sql +9 -0
  131. package/src/be/model-profile-check.ts +43 -18
  132. package/src/be/runtime-pools/routing.ts +1 -0
  133. package/src/be/savings/calibrate.ts +468 -0
  134. package/src/be/savings/compute.ts +182 -0
  135. package/src/be/savings/model.ts +406 -0
  136. package/src/be/scripts/typecheck.ts +5 -0
  137. package/src/be/seed/agent-fs-provision.ts +48 -11
  138. package/src/be/seed/workflows-seeder.ts +67 -23
  139. package/src/be/skill-parser.ts +19 -7
  140. package/src/commands/onboard/agent-fs-version.ts +1 -1
  141. package/src/commands/provider-error-class.ts +97 -0
  142. package/src/commands/runner.ts +99 -17
  143. package/src/extensions/contract-types.generated.ts +1 -1
  144. package/src/http/all-routes.ts +2 -0
  145. package/src/http/fs.ts +8 -2
  146. package/src/http/index.ts +4 -0
  147. package/src/http/model-profiles.ts +43 -10
  148. package/src/http/poll.ts +1 -0
  149. package/src/http/savings-calibration.ts +277 -0
  150. package/src/http/savings.ts +363 -0
  151. package/src/http/tasks.ts +46 -4
  152. package/src/http/trackers/authorize-gate.ts +41 -0
  153. package/src/http/trackers/jira.ts +26 -4
  154. package/src/http/trackers/linear.ts +26 -4
  155. package/src/integrations/chat-sdk/limits.ts +6 -1
  156. package/src/jira/oauth.ts +7 -1
  157. package/src/linear/README.md +2 -1
  158. package/src/linear/oauth.ts +7 -1
  159. package/src/providers/amp-adapter.ts +1 -1
  160. package/src/providers/codex-adapter.ts +3 -0
  161. package/src/providers/pi-mono-adapter.ts +6 -5
  162. package/src/providers/types.ts +7 -0
  163. package/src/scripts-runtime/eval-harness.ts +14 -1
  164. package/src/scripts-runtime/executors/types.ts +3 -0
  165. package/src/scripts-runtime/sdk-allowlist.ts +11 -1
  166. package/src/scripts-runtime/swarm-sdk.ts +6 -0
  167. package/src/scripts-runtime/types/stdlib.d.ts +9 -0
  168. package/src/scripts-runtime/types/swarm-extension.d.ts +34 -0
  169. package/src/scripts-runtime/types/swarm-sdk.d.ts +9 -0
  170. package/src/tasks/worker-follow-up.ts +142 -3
  171. package/src/tests/agent-fs-provision-seeder.test.ts +81 -0
  172. package/src/tests/apps-chart-shell.test.ts +170 -0
  173. package/src/tests/apps-icon.test.ts +156 -2
  174. package/src/tests/chat-sdk-relay.test.ts +12 -0
  175. package/src/tests/codex-adapter.test.ts +11 -0
  176. package/src/tests/comb-default.test.ts +6 -0
  177. package/src/tests/core-auth.test.ts +7 -6
  178. package/src/tests/db-client-busy-retry.test.ts +58 -6
  179. package/src/tests/deploy-guard.test.ts +154 -0
  180. package/src/tests/docs-index-footprint.test.js +3 -3
  181. package/src/tests/fixtures/app-shell-charts-definition.json.txt +180 -0
  182. package/src/tests/mcp-tools.test.ts +1 -1
  183. package/src/tests/model-profile-fallbacks.test.ts +482 -0
  184. package/src/tests/model-profiles-api.test.ts +1 -0
  185. package/src/tests/pi-mono-session-cost-on-abort.test.ts +115 -0
  186. package/src/tests/provider-error-class.test.ts +174 -0
  187. package/src/tests/runner-provider-fallback.test.ts +197 -0
  188. package/src/tests/savings-calibration-api.test.ts +264 -0
  189. package/src/tests/savings-calibration.test.ts +201 -0
  190. package/src/tests/savings-compute.test.ts +406 -0
  191. package/src/tests/savings-model.test.ts +287 -0
  192. package/src/tests/script-connections.test.ts +2 -0
  193. package/src/tests/script-native-unawaited-drain.test.ts +167 -0
  194. package/src/tests/sdk-allowlist.test.ts +40 -0
  195. package/src/tests/seed-workflows.test.ts +167 -11
  196. package/src/tests/skill-parser.test.ts +45 -0
  197. package/src/tests/tracker-oauth-authorize-auth.test.ts +262 -0
  198. package/src/tests/workflow-definition-pinning.test.ts +128 -3
  199. package/src/tests/workflow-script-principal.test.ts +94 -4
  200. package/src/tools/app-upsert.ts +1 -1
  201. package/src/tools/model-profiles/list-model-profiles.ts +4 -2
  202. package/src/tools/script-run.ts +1 -1
  203. package/src/tools/update-profile.ts +1 -1
  204. package/src/types.ts +115 -33
  205. package/src/utils/constants.ts +2 -0
  206. package/templates/skills/apps/SKILL.md +62 -3
  207. package/templates/skills/apps/content.md +62 -3
  208. package/templates/skills/swarm-docs/content.md +2 -0
  209. package/templates/skills/swarm-scripts/SKILL.md +1 -1
  210. package/templates/skills/swarm-scripts/content.md +1 -1
  211. package/dist/handlers-kdjcyzdj.js +0 -32
  212. package/dist/index-835xdgnm.js +0 -33
  213. package/dist/index-d469300x.js +0 -37
  214. package/dist/index-rzqwp11g.js +0 -32
  215. package/dist/lead-yqt47dkr.js +0 -70
  216. package/dist/worker-j6xxtq7p.js +0 -70
@@ -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 or credential allowlist; the name and harness are fixed. Omit a field to keep it; `effort: null` and `credentials: null` clear it. Applies to tasks claimed after the change. Operator only (the shared API key).",
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 created = await createModelProfile(body, {
262
- userId: await resolveHttpAuditUserId(req, myAgentId),
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 updated = await updateModelProfile(current.name, body, {
294
- userId: await resolveHttpAuditUserId(req, myAgentId),
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 (refs.agentIds.length > 0 || refs.openTaskCount > 0 || refs.scheduleIds.length > 0) {
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 or an agent default created just now.`,
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
@@ -583,6 +583,7 @@ export async function handlePoll(
583
583
  const result = await checkProfileForAgent(myAgentId, profile, {
584
584
  vcsRepo,
585
585
  includeRepoSecrets,
586
+ fallbacks: false,
586
587
  });
587
588
  const ok = result?.ok !== false;
588
589
  profileOk.set(memoKey, ok);
@@ -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
+ }