@desplega.ai/agent-swarm 2.13.4 → 2.13.5

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 (307) hide show
  1. package/dist/acp-adapter-tbt61zkm.js +17 -0
  2. package/dist/{acp-targets-zrk9k0n7.js → acp-targets-w8dbs4nr.js} +7 -7
  3. package/dist/{actions-acqqj9jn.js → actions-8zrcbjrx.js} +26 -26
  4. package/dist/{agent-fs-provision-zmaykqnd.js → agent-fs-provision-cbt2ttds.js} +4 -4
  5. package/dist/{amp-adapter-a7ts8tmw.js → amp-adapter-8x6zp8w4.js} +2 -2
  6. package/dist/{anthropic-messages-88b7s3n9.js → anthropic-messages-vdd1zmha.js} +10 -10
  7. package/dist/{app-tmwa442h.js → app-zfa9dwjw.js} +6 -6
  8. package/dist/{approval-notifications-6psaaaye.js → approval-notifications-rv92zmza.js} +7 -7
  9. package/dist/{assistant-564v6emv.js → assistant-q7s7rm1q.js} +24 -24
  10. package/dist/{boot-args-schema-n099e0f2.js → boot-args-schema-g27kpzr1.js} +1 -1
  11. package/dist/{boot-reembed-epvg4fp9.js → boot-reembed-1mjs52nt.js} +1 -1
  12. package/dist/{boot-reembed-chx66qfk.js → boot-reembed-m62dpdkg.js} +1 -1
  13. package/dist/{boot-scrub-sweep-dhkfp0a7.js → boot-scrub-sweep-ss1vcamw.js} +2 -2
  14. package/dist/{boot-seeds-m74fc07r.js → boot-seeds-hwnvgbb3.js} +4 -4
  15. package/dist/{boxd-451q55ys.js → boxd-y0szhp0g.js} +1 -1
  16. package/dist/{claude-adapter-hw63yj3j.js → claude-adapter-0rjmbhjk.js} +7 -7
  17. package/dist/{claude-managed-adapter-z5s7fx6p.js → claude-managed-adapter-6k032agg.js} +3 -3
  18. package/dist/{claude-managed-setup-wee1762m.js → claude-managed-setup-6czvve8q.js} +4 -4
  19. package/dist/{claude-sdk-session-cktyqkpk.js → claude-sdk-session-pr0znb81.js} +12667 -11881
  20. package/dist/{cli-0fgvnb0x.js → cli-0qt3epg4.js} +3 -3
  21. package/dist/{cli-ka48vpev.js → cli-19d6v8er.js} +3 -3
  22. package/dist/{cli-zexwhj3f.js → cli-1jq2em7b.js} +3 -3
  23. package/dist/{cli-5rgm8p39.js → cli-2wzkvfgm.js} +2 -2
  24. package/dist/{cli-x2cem5vy.js → cli-34fygf87.js} +2 -2
  25. package/dist/{cli-tsc3pdfd.js → cli-3860pmrq.js} +3 -3
  26. package/dist/{cli-hhtbandk.js → cli-3vw6dd4g.js} +2 -2
  27. package/dist/{cli-qzz6tz8s.js → cli-4gaf3tx5.js} +1 -1
  28. package/dist/{cli-9ah37a8a.js → cli-55h8m1x3.js} +8 -5
  29. package/dist/{cli-7gbff32z.js → cli-5n9w2xzx.js} +1 -1
  30. package/dist/{cli-p436yrzp.js → cli-5tq15aa7.js} +1 -1
  31. package/dist/{cli-jyhbvdga.js → cli-5web09sb.js} +2 -2
  32. package/dist/{cli-3zrwrnz7.js → cli-69zzpjgh.js} +1 -1
  33. package/dist/{cli-7g3fg4gf.js → cli-6msnpj1z.js} +6 -6
  34. package/dist/{cli-azj9t1f9.js → cli-7r9tvk8v.js} +41 -3
  35. package/dist/{cli-4fpa0zns.js → cli-7ypg8hka.js} +3 -3
  36. package/dist/{cli-8cas2xvp.js → cli-887vhew6.js} +2 -2
  37. package/dist/{cli-t0jmkcah.js → cli-8pd1t17c.js} +7 -7
  38. package/dist/{cli-h9d3sdnc.js → cli-94mxerrp.js} +1 -1
  39. package/dist/{cli-bv4vvnmg.js → cli-9n6g14vs.js} +2 -2
  40. package/dist/{cli-5ashycw3.js → cli-abby987g.js} +2 -2
  41. package/dist/{cli-kj84fqxz.js → cli-avax5mp9.js} +1 -1
  42. package/dist/{cli-ea7nz50q.js → cli-b42yfn17.js} +11 -11
  43. package/dist/{cli-ysv0k1we.js → cli-b9ygp1kz.js} +1 -1
  44. package/dist/{cli-3bxrrh53.js → cli-bvamr9g0.js} +2 -2
  45. package/dist/{cli-gse7bp3z.js → cli-ced4azn1.js} +10 -9
  46. package/dist/{cli-7by7vp6m.js → cli-cmbb2t8a.js} +2 -2
  47. package/dist/{cli-0p76my9q.js → cli-cp163xga.js} +10 -10
  48. package/dist/{cli-jzerrzgc.js → cli-ebpq3c5z.js} +8 -8
  49. package/dist/{cli-p3knvk62.js → cli-ehg90jd1.js} +113 -39
  50. package/dist/{cli-jg5txpm6.js → cli-emdfd8fh.js} +75 -1
  51. package/dist/{cli-d5pc8ep0.js → cli-evy3egte.js} +7 -7
  52. package/dist/{cli-60dwk8tx.js → cli-ewk3sg68.js} +2 -2
  53. package/dist/{cli-nc4pbwmp.js → cli-f7b9nyvk.js} +4 -4
  54. package/dist/{cli-29h5239w.js → cli-f9xw9pr4.js} +4 -4
  55. package/dist/{cli-3tzmh38q.js → cli-ffn0a515.js} +7 -7
  56. package/dist/{cli-dqgnbnxs.js → cli-grbngev5.js} +6 -1
  57. package/dist/{cli-cq4am6vg.js → cli-hd5bm1e1.js} +4 -4
  58. package/dist/{cli-akm6y2a8.js → cli-htb5bpz2.js} +1 -1
  59. package/dist/{cli-tr19nyn9.js → cli-j69pzrrz.js} +6 -6
  60. package/dist/{cli-hdhpdrka.js → cli-jdq3jmny.js} +3 -3
  61. package/dist/{cli-ja450998.js → cli-ks06cd0n.js} +1 -1
  62. package/dist/{cli-pz1dp12c.js → cli-kwj3131p.js} +1 -1
  63. package/dist/{cli-1vmhnncj.js → cli-m8f4379k.js} +3 -3
  64. package/dist/{cli-jbrc98qb.js → cli-n79j1vxc.js} +2 -2
  65. package/dist/{cli-b768ek79.js → cli-nrt048ys.js} +1 -0
  66. package/dist/{cli-93n305hm.js → cli-pedm2kht.js} +1 -1
  67. package/dist/{cli-eds5xnsz.js → cli-phmy14hb.js} +2 -2
  68. package/dist/{cli-n1kjs58m.js → cli-prz1p3ft.js} +3 -3
  69. package/dist/{cli-pz1nq5xm.js → cli-q1h041y8.js} +3 -3
  70. package/dist/{cli-cah298e5.js → cli-q41qar56.js} +104 -119
  71. package/dist/{cli-4pbwg7sn.js → cli-qmx8bnt4.js} +1 -1
  72. package/dist/{cli-944w9wb4.js → cli-qrvk9nka.js} +18 -13
  73. package/dist/{cli-gtj12v95.js → cli-r3dxpmyt.js} +1 -1
  74. package/dist/{cli-phdzn699.js → cli-ryem5cfq.js} +7 -7
  75. package/dist/{cli-49aq3cyn.js → cli-s8dhq7rj.js} +4 -4
  76. package/dist/{cli-fkpnx3vf.js → cli-sejvnjvw.js} +3 -2
  77. package/dist/{cli-m1sns1da.js → cli-srre41gb.js} +2 -2
  78. package/dist/{cli-5rxsb3wq.js → cli-t0n9svkb.js} +3 -3
  79. package/dist/{cli-tzw1jr2t.js → cli-t879ww0y.js} +2 -2
  80. package/dist/{cli-xwsm6xkr.js → cli-tgc1kgg3.js} +1 -1
  81. package/dist/{cli-404am6f3.js → cli-tt7dkwjj.js} +6 -6
  82. package/dist/{cli-zjdhrzm0.js → cli-tvyt8w6n.js} +2 -2
  83. package/dist/{cli-hyqksecw.js → cli-v09eza21.js} +9 -9
  84. package/dist/{cli-rv3y60bt.js → cli-vbge4rsp.js} +4 -4
  85. package/dist/{cli-bzcwwebm.js → cli-vcpyttcw.js} +1 -1
  86. package/dist/{cli-904wk98s.js → cli-wzecestt.js} +338 -98
  87. package/dist/{cli-gkv0r2hq.js → cli-x2adbg4j.js} +70 -8
  88. package/dist/{cli-4q2c8rxw.js → cli-z3x7h79n.js} +12 -6
  89. package/dist/{cli-s33bzmk0.js → cli-z40t85ms.js} +1 -1
  90. package/dist/{cli-rmgvwcmy.js → cli-zbbtp02a.js} +98 -21
  91. package/dist/{cli-3xqad83h.js → cli-zntvmqmm.js} +1 -1
  92. package/dist/{cli-zhwjdhjr.js → cli-zxa1vmx7.js} +1 -1
  93. package/dist/{cli-50wa8k4h.js → cli-zy9bb22b.js} +1 -2
  94. package/dist/{cli-ts5yv7dn.js → cli-zzq96q57.js} +123 -33
  95. package/dist/cli.js +18 -18
  96. package/dist/{codex-adapter-2fs2c0n6.js → codex-adapter-bn7eaxk5.js} +6 -6
  97. package/dist/{codex-hook-jxjv6dy5.js → codex-hook-nnmezjg9.js} +2 -2
  98. package/dist/{codex-session-runner-r55tmx4c.js → codex-session-runner-j1e9hffc.js} +6 -6
  99. package/dist/{commands-0mxdy9x8.js → commands-amhpafrr.js} +3 -3
  100. package/dist/{controller-g3vkfhr4.js → controller-tw7g10j2.js} +3 -3
  101. package/dist/{controllers-connect-4wdmncqt.js → controllers-connect-2ssqz7f4.js} +23 -23
  102. package/dist/{core-gsvj5f3z.js → core-bwp98v4w.js} +21 -21
  103. package/dist/{cursor-adapter-t9qb96q2.js → cursor-adapter-d6knjmwa.js} +1 -1
  104. package/dist/{db-487vvyv9.js → db-fd6xxm77.js} +3 -3
  105. package/dist/{db-76p7w3gr.js → db-sc59379z.js} +2 -2
  106. package/dist/{dispatcher-64rp6w67.js → dispatcher-a2gdqz8c.js} +21 -21
  107. package/dist/{dsh-adapter-8abzqk5c.js → dsh-adapter-43ygwqn7.js} +1 -1
  108. package/dist/{e2b-dc8f6ctq.js → e2b-cn7cvjqy.js} +1 -1
  109. package/dist/{e2b-3ykbe805.js → e2b-qnxyfea6.js} +1 -1
  110. package/dist/{easter-egg-3d-jrrwyf55.js → easter-egg-3d-a0pphy71.js} +1 -1
  111. package/dist/{embedding-space-001hhhpk.js → embedding-space-qepzrach.js} +6 -6
  112. package/dist/{embeddings-esj19cnw.js → embeddings-d7vr881x.js} +1 -1
  113. package/dist/{execute-301hkxqg.js → execute-6afts2bn.js} +1 -1
  114. package/dist/{external-channel-w10e2j05.js → external-channel-881zq1x4.js} +2 -2
  115. package/dist/fx-adapter-cr2hq17y.js +20 -0
  116. package/dist/{grok-adapter-3a0ezfee.js → grok-adapter-ayddnbsc.js} +9 -9
  117. package/dist/handlers-7ghs84x2.js +41 -0
  118. package/dist/{heartbeat-tbrj7vhq.js → heartbeat-4c022m6a.js} +24 -24
  119. package/dist/{hook-3am1sq68.js → hook-4k84x499.js} +5 -5
  120. package/dist/{hook-tx917rqw.js → hook-a7eh6zty.js} +7 -7
  121. package/dist/{http-3rmemtqy.js → http-6aw5wanc.js} +180 -94
  122. package/dist/index-dtt0d634.js +44 -0
  123. package/dist/index-hmszyqkk.js +42 -0
  124. package/dist/index-j6rm1rtn.js +39 -0
  125. package/dist/{index-aetfyx6p.js → index-rwwhsmjg.js} +1 -1
  126. package/dist/{index-s7xtvc0f.js → index-xhad0n8m.js} +1 -1
  127. package/dist/{index-2jw11t8s.js → index-yq9hhhhy.js} +25 -25
  128. package/dist/{index-gfnq66t1.js → index-z2g9469g.js} +202 -17
  129. package/dist/{keepalive-9px3t7qj.js → keepalive-zhbmztqw.js} +4 -4
  130. package/dist/lead-g2j4q39z.js +83 -0
  131. package/dist/{maintenance-vwqjwst6.js → maintenance-rsp0591h.js} +1 -1
  132. package/dist/{node-2wfcey7d.js → node-0as4n1ef.js} +3 -3
  133. package/dist/{node-zd6shky9.js → node-dhxbt01p.js} +3 -3
  134. package/dist/{node-z67yjre7.js → node-rxte5hc6.js} +290 -70
  135. package/dist/{node-hjn5n1yp.js → node-t0ct2zpw.js} +3 -5
  136. package/dist/{oauth-refresh-sweep-yqry3wn4.js → oauth-refresh-sweep-w3cqmxq7.js} +3 -3
  137. package/dist/{object-webhooks-8s1wtths.js → object-webhooks-331hay92.js} +7 -7
  138. package/dist/{onboard-hb8czx0p.js → onboard-n2w80m2n.js} +3 -3
  139. package/dist/{opencode-adapter-djd6m6dk.js → opencode-adapter-z98127cz.js} +6 -6
  140. package/dist/{otel-impl-2ymwyczz.js → otel-impl-xsk35tvf.js} +1 -1
  141. package/dist/{pending-nxavtvx7.js → pending-2e6trkxd.js} +22 -22
  142. package/dist/{pi-mono-adapter-d2cpmcy4.js → pi-mono-adapter-wh6yndtm.js} +8 -8
  143. package/dist/{pricing-refresh-1yz8jkqv.js → pricing-refresh-9355dj1t.js} +2 -2
  144. package/dist/{rbac-roles-dbpx0x9v.js → rbac-roles-58y3m912.js} +3 -3
  145. package/dist/{rbac-roles-7s6m31xw.js → rbac-roles-8eajw4s6.js} +2 -2
  146. package/dist/{render-v2-7y20rjrn.js → render-v2-7gskkmd4.js} +6 -6
  147. package/dist/{resume-wz546x47.js → resume-0drnmpja.js} +9 -9
  148. package/dist/{runtime-cb5bd9tz.js → runtime-d2tcmqte.js} +1 -1
  149. package/dist/{session-access-4vcft8kq.js → session-access-0ztys6bd.js} +5 -5
  150. package/dist/{session-source-backfill-57b50tpw.js → session-source-backfill-cdm5s046.js} +2 -2
  151. package/dist/{setup-e1tevjyj.js → setup-pqwb9tcc.js} +1 -1
  152. package/dist/thread-buffer-epx3san4.js +42 -0
  153. package/dist/{tree-visibility-07wf1nct.js → tree-visibility-rwz67mje.js} +2 -2
  154. package/dist/{user-merge-jobs-n20qpv6s.js → user-merge-jobs-mxrhamwt.js} +3 -3
  155. package/dist/{virtual-modules-6fxmn1yv.js → virtual-modules-71y1gsmg.js} +3 -3
  156. package/dist/{work-objects-eg7y48pe.js → work-objects-3k4ykt9j.js} +24 -24
  157. package/dist/worker-09gqn9xy.js +83 -0
  158. package/dist/{worker-follow-up-jnvnnf2b.js → worker-follow-up-y0h64xpr.js} +21 -21
  159. package/openapi.json +387 -6
  160. package/package.json +4 -4
  161. package/src/apps/sync.ts +20 -5
  162. package/src/azure-devops/boards-sync.ts +1 -1
  163. package/src/be/db/slack-trees.ts +12 -1
  164. package/src/be/db/tasks/read.ts +25 -1
  165. package/src/be/db/tasks/write.ts +47 -12
  166. package/src/be/db/users/avatar.ts +12 -0
  167. package/src/be/db.ts +78 -5
  168. package/src/be/migrations/344_user_avatar.sql +4 -0
  169. package/src/be/migrations/345_task_status_kind.sql +6 -0
  170. package/src/be/primitives/object-embed.ts +2 -2
  171. package/src/be/primitives/objects.ts +2 -2
  172. package/src/be/runtime-pools/supervisor.ts +1 -0
  173. package/src/be/scripts/typecheck.ts +1 -1
  174. package/src/be/swarm-config-guard.ts +4 -0
  175. package/src/bitbucket/handlers.ts +1 -1
  176. package/src/commands/credential-outcome-report.ts +96 -0
  177. package/src/commands/onboard/agent-fs-version.ts +1 -1
  178. package/src/commands/runner.ts +49 -3
  179. package/src/event-waits/github.ts +39 -6
  180. package/src/event-waits/index.ts +4 -2
  181. package/src/event-waits/types.ts +10 -3
  182. package/src/extensions/contract-types.generated.ts +1 -1
  183. package/src/github/app.ts +177 -13
  184. package/src/github/handlers.ts +7 -2
  185. package/src/github/pull-request-state.ts +100 -11
  186. package/src/gitlab/handlers.ts +2 -2
  187. package/src/heartbeat/heartbeat.ts +6 -1
  188. package/src/http/api-keys.ts +26 -0
  189. package/src/http/asset-key-write.ts +45 -0
  190. package/src/http/assets.ts +4 -3
  191. package/src/http/github-token.ts +27 -6
  192. package/src/http/pages.ts +4 -10
  193. package/src/http/schedules.ts +6 -6
  194. package/src/http/sessions.ts +7 -2
  195. package/src/http/tasks.ts +23 -5
  196. package/src/http/users.ts +5 -0
  197. package/src/http/webhooks.ts +19 -1
  198. package/src/http/workflows.ts +5 -10
  199. package/src/integrations/chat-sdk/commands.ts +3 -1
  200. package/src/jira/sync.ts +1 -1
  201. package/src/linear/sync.ts +5 -3
  202. package/src/prompts/session-templates.ts +1 -0
  203. package/src/providers/acp-targets.ts +2 -2
  204. package/src/providers/amp-adapter.ts +1 -1
  205. package/src/providers/claude-adapter.ts +5 -0
  206. package/src/providers/claude-sdk-session.ts +5 -0
  207. package/src/providers/runtime-dependencies.ts +1 -1
  208. package/src/scheduler/deferred-task-waits.ts +74 -13
  209. package/src/scripts-runtime/swarm-sdk.ts +8 -1
  210. package/src/scripts-runtime/types/stdlib.d.ts +8 -1
  211. package/src/scripts-runtime/types/swarm-extension.d.ts +19 -2
  212. package/src/scripts-runtime/types/swarm-sdk.d.ts +8 -1
  213. package/src/slack/actions.ts +1 -1
  214. package/src/slack/render-v2.ts +148 -7
  215. package/src/tests/api-drain-worker-e2e.test.ts +0 -2
  216. package/src/tests/approval-auto-cancellation-migration.test.ts +8 -19
  217. package/src/tests/approval-card-resolution.test.ts +2 -17
  218. package/src/tests/approval-requests-notifications.test.ts +41 -27
  219. package/src/tests/approval-requests.test.ts +137 -369
  220. package/src/tests/apps-sync-engine.test.ts +5 -3
  221. package/src/tests/apps-table-filters-migration.test.ts +44 -25
  222. package/src/tests/bitbucket-handlers.test.ts +2 -0
  223. package/src/tests/chat-sdk-commands.test.ts +1 -0
  224. package/src/tests/claude-adapter.test.ts +35 -0
  225. package/src/tests/codex-adapter.test.ts +3 -92
  226. package/src/tests/codex-gpt6-release.test.ts +0 -9
  227. package/src/tests/codex-oauth-credential-info.test.ts +35 -18
  228. package/src/tests/codex-oauth.test.ts +0 -6
  229. package/src/tests/codex-pool.test.ts +5 -214
  230. package/src/tests/codex-rate-limit-parse.test.ts +0 -7
  231. package/src/tests/codex-swarm-events.test.ts +11 -54
  232. package/src/tests/db-client-busy-retry.test.ts +49 -16
  233. package/src/tests/defer-task.test.ts +1 -0
  234. package/src/tests/e2e-shards.test.ts +9 -9
  235. package/src/tests/enabled-tools.test.ts +0 -2
  236. package/src/tests/fixtures/base-prompt-baseline.json +6 -6
  237. package/src/tests/github-app-worker-auth-helper.test.ts +147 -1
  238. package/src/tests/gitlab-handlers.test.ts +2 -0
  239. package/src/tests/heartbeat-jev-gate.test.ts +9 -241
  240. package/src/tests/heartbeat-reroute-decision.test.ts +2 -0
  241. package/src/tests/heartbeat-supersede-resume.test.ts +2 -0
  242. package/src/tests/heartbeat.test.ts +1 -0
  243. package/src/tests/key-usage-selection.test.ts +302 -0
  244. package/src/tests/memory-e2e.test.ts +34 -103
  245. package/src/tests/memory-key-paths.test.ts +5 -0
  246. package/src/tests/memory-rater-llm.test.ts +33 -149
  247. package/src/tests/memory-store.test.ts +24 -125
  248. package/src/tests/memory.test.ts +31 -100
  249. package/src/tests/merge-gate-detect-changes.test.ts +108 -2
  250. package/src/tests/otel-service-name.test.ts +0 -9
  251. package/src/tests/otel-session-cost-metrics.test.ts +1 -38
  252. package/src/tests/pool-session-logs.test.ts +8 -42
  253. package/src/tests/pre-push-affected-tests.test.ts +106 -0
  254. package/src/tests/prompt-template-session.test.ts +3 -0
  255. package/src/tests/runner-api-drain.test.ts +2 -17
  256. package/src/tests/runner-terminal-window-report.test.ts +132 -0
  257. package/src/tests/runtime-supervisor-failures.test.ts +1 -0
  258. package/src/tests/sandboxed-process.test.ts +26 -21
  259. package/src/tests/schedule-http-triage-tools.test.ts +12 -9
  260. package/src/tests/scheduled-tasks.test.ts +14 -51
  261. package/src/tests/script-executor-conformance.test.ts +4 -48
  262. package/src/tests/script-executor-quickjs-pool.test.ts +9 -7
  263. package/src/tests/script-native-startup.test.ts +21 -25
  264. package/src/tests/secret-rules.test.ts +4 -2
  265. package/src/tests/session-logs.test.ts +1 -260
  266. package/src/tests/slack-render-v2.test.ts +5 -148
  267. package/src/tests/task-cascade-fail.test.ts +1 -0
  268. package/src/tests/task-supersede-resume.test.ts +1 -0
  269. package/src/tests/test-proc.ts +5 -2
  270. package/src/tests/window-stream-reporter.test.ts +122 -0
  271. package/src/tests/workflow-agent-task-retry.test.ts +1 -3
  272. package/src/tests/workflow-cascade-cancel.test.ts +1 -0
  273. package/src/tests/workflow-retry-v2.test.ts +34 -43
  274. package/src/tests/workflow-retry-validation.test.ts +8 -48
  275. package/src/tests/worktree-reaper.test.ts +2 -2
  276. package/src/tools/cancel-task.ts +2 -2
  277. package/src/tools/defer-task.ts +7 -2
  278. package/src/tools/get-task-details.ts +8 -4
  279. package/src/tools/get-tasks.ts +13 -2
  280. package/src/tools/manage-user.ts +9 -0
  281. package/src/tools/store-progress.ts +5 -3
  282. package/src/types.ts +93 -0
  283. package/src/utils/constants.ts +12 -0
  284. package/src/utils/credentials.ts +42 -5
  285. package/src/utils/error-tracker.ts +6 -0
  286. package/src/utils/key-usage.ts +114 -0
  287. package/src/utils/sandboxed-process.ts +1 -0
  288. package/src/workflows/resume.ts +5 -2
  289. package/templates/skills/apps/SKILL.md +2 -0
  290. package/templates/skills/apps/content.md +2 -0
  291. package/templates/skills/boards-and-sheets/content.md +4 -2
  292. package/templates/skills/pages/content.md +7 -0
  293. package/templates/skills/swarm-docs/content.md +1 -0
  294. package/templates/skills/swarm-scripts/SKILL.md +1 -0
  295. package/templates/skills/swarm-scripts/content.md +1 -0
  296. package/dist/acp-adapter-mr4temec.js +0 -17
  297. package/dist/fx-adapter-4sjrbdne.js +0 -20
  298. package/dist/handlers-ef3k11hb.js +0 -41
  299. package/dist/index-e6bq0exk.js +0 -42
  300. package/dist/index-hf2k1vge.js +0 -39
  301. package/dist/index-taxmejj3.js +0 -44
  302. package/dist/lead-5vt6f174.js +0 -83
  303. package/dist/thread-buffer-686pa1p1.js +0 -42
  304. package/dist/worker-4vp616ms.js +0 -83
  305. package/src/tests/apps-table-selection-migration.test.ts +0 -95
  306. package/src/tests/extensions-example-heartbeat-remediate.test.ts +0 -69
  307. package/src/tests/extensions-example-rewrite-and-suppress.test.ts +0 -70
@@ -27,7 +27,6 @@ const MMDD = "10-15";
27
27
  type Fixture = {
28
28
  configs: Record<string, string>;
29
29
  heartbeatMd?: string;
30
- prevStatus?: string;
31
30
  reportedFailures?: unknown;
32
31
  prevOutput?: string | null;
33
32
  prevCreatedAt?: string | null;
@@ -74,36 +73,32 @@ function fakeCtx(f: Fixture) {
74
73
  return table(["status"], f.approvalStatus ? [[f.approvalStatus]] : []);
75
74
  }
76
75
  if (sql.includes("heartbeatMd")) return table(["heartbeatMd"], [[f.heartbeatMd ?? ""]]);
77
- if (sql.includes("'heartbeat-checklist'") || sql.includes("finishedAt <= ?")) {
78
- if (sql.includes("'heartbeat-checklist'")) calls.previousReads++;
76
+ if (sql.includes("'heartbeat-checklist'")) {
77
+ calls.previousReads++;
79
78
  // Execute historical selection against real rows, including its bounds and limit.
80
79
  const db = new Database(":memory:");
81
80
  try {
82
81
  db.run(`CREATE TABLE agent_tasks (
83
- id TEXT, output TEXT, task TEXT, agentId TEXT, taskType TEXT,
84
- status TEXT, createdAt TEXT, finishedAt TEXT
82
+ id TEXT, output TEXT, agentId TEXT, taskType TEXT,
83
+ status TEXT, createdAt TEXT
85
84
  )`);
86
85
  if (f.prevCreatedAt !== null)
87
- db.run("INSERT INTO agent_tasks VALUES (?, ?, ?, ?, ?, ?, ?, ?)", [
86
+ db.run("INSERT INTO agent_tasks VALUES (?, ?, ?, ?, ?, ?)", [
88
87
  "prev-id",
89
88
  f.prevOutput ?? null,
90
- f.prevStatus ?? "",
91
89
  LEAD,
92
90
  "heartbeat-checklist",
93
91
  "completed",
94
92
  f.prevCreatedAt ?? "2026-10-15T13:00:00.000Z",
95
- null,
96
93
  ]);
97
94
  for (const failure of f.failed ?? []) {
98
- db.run("INSERT INTO agent_tasks VALUES (?, ?, ?, ?, ?, ?, ?, ?)", [
95
+ db.run("INSERT INTO agent_tasks VALUES (?, ?, ?, ?, ?, ?)", [
99
96
  failure.id,
100
97
  null,
101
- failure.task,
102
98
  failure.agentId,
103
99
  "test",
104
100
  "failed",
105
101
  "2026-10-15T06:00:00.000Z",
106
- failure.finishedAt,
107
102
  ]);
108
103
  }
109
104
  return { data: { rows: db.query(sql).all(...params) } };
@@ -271,9 +266,6 @@ describe("heartbeat-jev-gate with a provider", () => {
271
266
  "- Blocker: old thing (DELETE 10-12)",
272
267
  "- Future item (DELETE 10-30)",
273
268
  ].join("\n"),
274
- // The previous checklist already listed aaaaaaaa, so only bbbbbbbb is new.
275
- prevStatus:
276
- '## Current System Status [auto-generated]\n## Recent Failures (last 6h) [auto-generated]\n- [aaaaaaaa] "old failure" — agent: 12345678, reason: boom, at: 2026-10-15T12:00:00Z\n',
277
269
  prevOutput: "All clear.\n\nNothing to do.",
278
270
  failed: [
279
271
  {
@@ -337,148 +329,10 @@ describe("heartbeat-jev-gate with a provider", () => {
337
329
  }
338
330
  });
339
331
 
340
- for (const ordersFirst of [true, false]) {
341
- test(`previous failures are not new with standing orders ${ordersFirst ? "before" : "after"} status`, async () => {
342
- const failures =
343
- '## Recent Failures (last 6h) [auto-generated]\n- [aaaaaaaa] "old failure" — agent: 12345678, reason: boom, at: 2026-10-15T12:00:00Z\n';
344
- const orders = "## Your Standing Orders\n- Already triaged.\n";
345
- const status = `## Current System Status [auto-generated]\n${failures}`;
346
- const { ctx } = fakeCtx({
347
- configs: { TYPESAFE_API_KEY: "x" },
348
- reportedFailures: ["aaaaaaaa-0000"],
349
- prevStatus: ordersFirst ? orders + status : status + orders,
350
- failed: [
351
- {
352
- id: "aaaaaaaa-0000",
353
- agentId: "12345678-bbbb",
354
- task: "old failure",
355
- failureReason: "boom",
356
- finishedAt: "2026-10-15T12:00:00Z",
357
- },
358
- {
359
- id: "bbbbbbbb-0000",
360
- agentId: "12345678-bbbb",
361
- task: "new failure",
362
- failureReason: "exit 1",
363
- finishedAt: "2026-10-15T13:01:00.000Z",
364
- },
365
- ],
366
- jev: jevSays(0.9),
367
- });
368
- const verdict = await run(ctx, { dryRun: true });
369
- expect(verdict.state.new_failures_since_previous_heartbeat).toEqual([
370
- "bbbbbbbb: new failure | exit 1",
371
- ]);
372
- expect(verdict.state.failures_last_6h_total).toBe(2);
373
- });
374
- }
375
-
376
- test.each([
377
- "## Recent Failures (last 6h) [auto-generated]\nNone.\n",
378
- "",
379
- ])("failures outside the previous Recent Failures section stay new (%s)", async (section) => {
380
- const { ctx } = fakeCtx({
381
- configs: { TYPESAFE_API_KEY: "x" },
382
- prevStatus: `## Current System Status [auto-generated]\n${section}## Gate Findings [auto-generated]\n- [aaaaaaaa] "quoted failure" — agent: 12345678, reason: boom, at: 2026-10-15T12:00:00Z\n`,
383
- failed: [
384
- {
385
- id: "aaaaaaaa-0000",
386
- agentId: "12345678-bbbb",
387
- task: "quoted failure",
388
- failureReason: "boom",
389
- finishedAt: "2026-10-15T13:01:00.000Z",
390
- },
391
- ],
392
- jev: jevSays(0.9),
393
- });
394
- expect((await run(ctx, { dryRun: true })).state.new_failures_since_previous_heartbeat).toEqual([
395
- "aaaaaaaa: quoted failure | boom",
396
- ]);
397
- });
398
-
399
- test.each([
400
- "- Item 544: fix `## Recent Failures` parsing.\n",
401
- "## Recent Failures\nSnapshot notes.\n",
402
- '## Recent Failures (last 6h) [auto-generated]\n- [bbbbbbbb] "snapshot quote" — agent: 12345678, reason: boom, at: 2026-10-15T12:00:00Z\n',
403
- ])("snapshot failure headings cannot hijack generated status (%s)", async (snapshot) => {
404
- const { ctx, calls } = fakeCtx({
405
- configs: { TYPESAFE_API_KEY: "x" },
406
- reportedFailures: ["aaaaaaaa-0000"],
407
- prevStatus: [
408
- "## Your Standing Orders (snapshot from HEARTBEAT.md)",
409
- snapshot,
410
- "## Instructions",
411
- "Review the generated status.",
412
- "## Current System Status [auto-generated]",
413
- "## Recent Failures (last 6h) [auto-generated]",
414
- '- [aaaaaaaa] "old failure" — agent: 12345678, reason: boom, at: 2026-10-15T12:00:00Z',
415
- ].join("\n"),
416
- failed: [
417
- {
418
- id: "aaaaaaaa-0000",
419
- agentId: "12345678-bbbb",
420
- task: "old failure",
421
- failureReason: "boom",
422
- finishedAt: "2026-10-15T12:00:00Z",
423
- },
424
- {
425
- id: "bbbbbbbb-0000",
426
- agentId: "12345678-bbbb",
427
- task: "snapshot quote",
428
- failureReason: "boom",
429
- finishedAt: "2026-10-15T13:01:00.000Z",
430
- },
431
- ],
432
- jev: jevSays(0.9),
433
- });
434
- expect((await run(ctx, { dryRun: true })).state.new_failures_since_previous_heartbeat).toEqual([
435
- "bbbbbbbb: snapshot quote | boom",
436
- ]);
437
- expect(calls.previousReads).toBe(1);
438
- });
439
-
440
- test("an absent generated failures section cannot use a trailing snapshot heading", async () => {
441
- const { ctx, calls } = fakeCtx({
442
- configs: { TYPESAFE_API_KEY: "x" },
443
- prevStatus: [
444
- "## Current System Status [auto-generated]",
445
- "## Task Overview [auto-generated]",
446
- "- Failed (24h): 0",
447
- "## Agent Status [auto-generated]",
448
- "- Online: 6",
449
- "## Your Standing Orders (snapshot from HEARTBEAT.md)",
450
- "## Recent Failures (last 6h) [auto-generated]",
451
- '- [aaaaaaaa] "snapshot quote" — agent: 12345678, reason: boom, at: 2026-10-15T12:00:00Z',
452
- ].join("\n"),
453
- failed: [
454
- {
455
- id: "aaaaaaaa-0000",
456
- agentId: "12345678-bbbb",
457
- task: "snapshot quote",
458
- failureReason: "boom",
459
- finishedAt: "2026-10-15T13:01:00.000Z",
460
- },
461
- ],
462
- jev: jevSays(0.9),
463
- });
464
- expect((await run(ctx, { dryRun: true })).state.new_failures_since_previous_heartbeat).toEqual([
465
- "aaaaaaaa: snapshot quote | boom",
466
- ]);
467
- expect(calls.previousReads).toBe(1);
468
- });
469
-
470
332
  test("a failed-task title heading does not hide later failures", async () => {
471
333
  const { ctx, calls } = fakeCtx({
472
334
  configs: { TYPESAFE_API_KEY: "x" },
473
335
  reportedFailures: ["aaaaaaaa-0000", "bbbbbbbb-0000"],
474
- prevStatus: [
475
- "## Current System Status [auto-generated]",
476
- "## Recent Failures (last 6h) [auto-generated]",
477
- '- [aaaaaaaa] "Fix the task\n## Requirements" — agent: 12345678, reason: boom, at: 2026-10-15T12:00:00Z',
478
- '- [bbbbbbbb] "later failure" — agent: 12345678, reason: boom, at: 2026-10-15T12:00:00Z',
479
- "## Gate Findings [auto-generated]",
480
- '- [cccccccc] "outside failures" — agent: 12345678, reason: boom, at: 2026-10-15T12:00:00Z',
481
- ].join("\n"),
482
336
  failed: [
483
337
  {
484
338
  id: "aaaaaaaa-0000",
@@ -512,93 +366,6 @@ describe("heartbeat-jev-gate with a provider", () => {
512
366
  expect(calls.previousReads).toBe(1);
513
367
  });
514
368
 
515
- test("code-fenced snapshot system and agent markers cannot hide previous failures", async () => {
516
- const { ctx, calls } = fakeCtx({
517
- configs: { TYPESAFE_API_KEY: "x" },
518
- reportedFailures: ["aaaaaaaa-0000"],
519
- prevStatus: [
520
- "## Your Standing Orders (snapshot from HEARTBEAT.md)",
521
- "```md",
522
- "## Current System Status [auto-generated]",
523
- "## Agent Status [auto-generated]",
524
- "```",
525
- "## Current System Status [auto-generated]",
526
- "## Recent Failures (last 6h) [auto-generated]",
527
- '- [aaaaaaaa] "old failure" — agent: 12345678, reason: boom, at: 2026-10-15T12:00:00.000Z',
528
- "## Agent Status [auto-generated]",
529
- ].join("\n"),
530
- failed: [
531
- {
532
- id: "aaaaaaaa-0000",
533
- agentId: "12345678-bbbb",
534
- task: "old failure",
535
- failureReason: "boom",
536
- finishedAt: "2026-10-15T12:00:00.000Z",
537
- },
538
- ],
539
- jev: jevSays(0.9),
540
- });
541
- expect((await run(ctx, { dryRun: true })).state.new_failures_since_previous_heartbeat).toBe(
542
- "none",
543
- );
544
- expect(calls.previousReads).toBe(1);
545
- });
546
-
547
- test("a generated Requirements heading in a failed title cannot hide later failures", async () => {
548
- const { ctx } = fakeCtx({
549
- configs: { TYPESAFE_API_KEY: "x" },
550
- reportedFailures: ["aaaaaaaa-0000", "bbbbbbbb-0000"],
551
- prevStatus: [
552
- "## Current System Status [auto-generated]",
553
- "## Recent Failures (last 6h) [auto-generated]",
554
- '- [aaaaaaaa] "Fix\n## Requirements [auto-generated]\nbody" — agent: 12345678, reason: boom, at: 2026-10-15T12:00:00.000Z',
555
- '- [bbbbbbbb] "later failure" — agent: 12345678, reason: boom, at: 2026-10-15T12:01:00.000Z',
556
- "## Agent Status [auto-generated]",
557
- ].join("\n"),
558
- failed: [
559
- {
560
- id: "aaaaaaaa-0000",
561
- agentId: "12345678-bbbb",
562
- task: "Fix\n## Requirements [auto-generated]\nbody",
563
- failureReason: "boom",
564
- finishedAt: "2026-10-15T12:00:00.000Z",
565
- },
566
- {
567
- id: "bbbbbbbb-0000",
568
- agentId: "12345678-bbbb",
569
- task: "later failure",
570
- failureReason: "boom",
571
- finishedAt: "2026-10-15T12:01:00.000Z",
572
- },
573
- ],
574
- jev: jevSays(0.9),
575
- });
576
- expect((await run(ctx, { dryRun: true })).state.new_failures_since_previous_heartbeat).toBe(
577
- "none",
578
- );
579
- });
580
-
581
- test("a failure finished after previous creation stays new even if its ID is quoted", async () => {
582
- const { ctx } = fakeCtx({
583
- configs: { TYPESAFE_API_KEY: "x" },
584
- prevStatus:
585
- '## Current System Status [auto-generated]\n## Recent Failures (last 6h) [auto-generated]\n- [aaaaaaaa] "new failure" — agent: 12345678, reason: boom, at: 2026-10-15T13:00:00.001Z',
586
- failed: [
587
- {
588
- id: "aaaaaaaa-0000",
589
- agentId: "12345678-bbbb",
590
- task: "new failure",
591
- failureReason: "boom",
592
- finishedAt: "2026-10-15T13:00:00.001Z",
593
- },
594
- ],
595
- jev: jevSays(0.9),
596
- });
597
- expect((await run(ctx, { dryRun: true })).state.new_failures_since_previous_heartbeat).toEqual([
598
- "aaaaaaaa: new failure | boom",
599
- ]);
600
- });
601
-
602
369
  test.each([
603
370
  { reportedFailures: ["aaaaaaaa-0000"] },
604
371
  { reportedFailures: '["aaaaaaaa-0000"]' },
@@ -633,14 +400,15 @@ describe("heartbeat-jev-gate with a provider", () => {
633
400
  });
634
401
 
635
402
  test.each(
636
- [undefined, "not JSON", {}, [42]].map((reportedFailures) => ({ reportedFailures })),
403
+ [undefined, "not JSON", {}, [42], ["aaaaaaaa-0000", 42]].map((reportedFailures) => ({
404
+ reportedFailures,
405
+ })),
637
406
  )("missing or invalid receipts treat every current failure as new (%j)", async ({
638
407
  reportedFailures,
639
408
  }) => {
640
409
  const { ctx } = fakeCtx({
641
410
  configs: { TYPESAFE_API_KEY: "x" },
642
411
  reportedFailures,
643
- prevStatus: '## Recent Failures (last 6h) [auto-generated]\n- [aaaaaaaa] "quoted"',
644
412
  failed: [
645
413
  {
646
414
  id: "aaaaaaaa-0000",
@@ -322,6 +322,7 @@ describe("Heartbeat — reroute-decision fallback (DES-523)", () => {
322
322
 
323
323
  // R1 was terminalized (no longer pending) and a Lead-owned decision exists.
324
324
  expect((await getTaskById(r1.id))!.status).not.toBe("pending");
325
+ expect((await getTaskById(r1.id))!.statusKind).toBe("system");
325
326
  const decisions = (await getChildTasks(original.id)).filter(
326
327
  (c) => c.taskType === "reroute-decision",
327
328
  );
@@ -404,6 +405,7 @@ describe("Heartbeat — reroute-decision fallback (DES-523)", () => {
404
405
  ).toBe(0);
405
406
  const updated = (await getTaskById(capped.id))!;
406
407
  expect(updated.status).toBe("failed");
408
+ expect(updated.statusKind).toBe("error");
407
409
  expect(updated.failureReason).toBe(RESUME_BUDGET_EXHAUSTED_REASON);
408
410
  });
409
411
 
@@ -92,6 +92,7 @@ describe("Heartbeat — supersede + resume (DES-523)", () => {
92
92
  // Parent transitioned to `superseded` (NOT `failed`).
93
93
  const updatedParent = await getTaskById(parent.id);
94
94
  expect(updatedParent?.status).toBe("superseded");
95
+ expect(updatedParent?.statusKind).toBe("crash_recovery");
95
96
 
96
97
  // A resume follow-up child exists.
97
98
  const children = await getChildTasks(parent.id);
@@ -160,6 +161,7 @@ describe("Heartbeat — supersede + resume (DES-523)", () => {
160
161
 
161
162
  const updatedParent = await getTaskById(parent.id);
162
163
  expect(updatedParent?.status).toBe("failed");
164
+ expect(updatedParent?.statusKind).toBe("error");
163
165
  expect(updatedParent?.failureReason).toBe(RESUME_BUDGET_EXHAUSTED_REASON);
164
166
  expect((await getChildTasks(parent.id)).length).toBe(0);
165
167
  });
@@ -790,6 +790,7 @@ describe("Heartbeat Triage", () => {
790
790
  // Original task should be failed
791
791
  const updated = await getTaskById(task.id);
792
792
  expect(updated?.status).toBe("failed");
793
+ expect(updated?.statusKind).toBe("error");
793
794
  expect(updated?.failureReason).toContain("reboot sweep");
794
795
 
795
796
  // Retry task should exist
@@ -0,0 +1,302 @@
1
+ /**
2
+ * P5: among admissible keys, the worker picks the lowest-usage key. Usage is
3
+ * the max of the live 5h and 7d windows; a reset window and an unobserved key
4
+ * count as 0; the pick is random among keys within 0.10 of the lowest. No usage level ever excludes a key.
5
+ */
6
+ import { afterEach, describe, expect, spyOn, test } from "bun:test";
7
+ import { resolveCredentialPools, selectCredential } from "../utils/credentials";
8
+ import {
9
+ credentialSelectionStrategy,
10
+ keyPaceFromWindows,
11
+ keyUsageFromWindows,
12
+ pickLowestUsage,
13
+ } from "../utils/key-usage";
14
+
15
+ const NOW_MS = Date.parse("2026-11-16T12:00:00Z");
16
+ const future = Math.floor(NOW_MS / 1000) + 3600;
17
+ const past = Math.floor(NOW_MS / 1000) - 60;
18
+ const POOL = "key-aaaaa,key-bbbbb,key-ccccc,key-ddddd";
19
+
20
+ let logSpy: ReturnType<typeof spyOn> | undefined;
21
+ afterEach(() => {
22
+ logSpy?.mockRestore();
23
+ logSpy = undefined;
24
+ });
25
+
26
+ describe("keyUsageFromWindows", () => {
27
+ test("takes the max of the live five_hour and seven_day windows", () => {
28
+ expect(
29
+ keyUsageFromWindows(
30
+ {
31
+ five_hour: { utilization: 0.53, resetsAt: future },
32
+ seven_day: { utilization: 0.11, resetsAt: future },
33
+ },
34
+ NOW_MS,
35
+ ),
36
+ ).toBe(0.53);
37
+ expect(
38
+ keyUsageFromWindows(
39
+ {
40
+ five_hour: { utilization: 0.2, resetsAt: future },
41
+ seven_day: { utilization: 0.8, resetsAt: future },
42
+ },
43
+ NOW_MS,
44
+ ),
45
+ ).toBe(0.8);
46
+ });
47
+
48
+ test("a window past resetsAt counts as 0", () => {
49
+ expect(
50
+ keyUsageFromWindows(
51
+ {
52
+ five_hour: { utilization: 0.99, resetsAt: past },
53
+ seven_day: { utilization: 0.1, resetsAt: future },
54
+ },
55
+ NOW_MS,
56
+ ),
57
+ ).toBe(0.1);
58
+ expect(keyUsageFromWindows({ five_hour: { utilization: 0.99, resetsAt: past } }, NOW_MS)).toBe(
59
+ 0,
60
+ );
61
+ });
62
+
63
+ test("no observation counts as 0", () => {
64
+ expect(keyUsageFromWindows(undefined, NOW_MS)).toBe(0);
65
+ expect(keyUsageFromWindows(null, NOW_MS)).toBe(0);
66
+ expect(keyUsageFromWindows({}, NOW_MS)).toBe(0);
67
+ expect(keyUsageFromWindows({ five_hour: { resetsAt: future } }, NOW_MS)).toBe(0);
68
+ });
69
+
70
+ test("model-scoped weekly windows do not count", () => {
71
+ expect(
72
+ keyUsageFromWindows({ seven_day_opus: { utilization: 0.97, resetsAt: future } }, NOW_MS),
73
+ ).toBe(0);
74
+ });
75
+ });
76
+
77
+ describe("pickLowestUsage", () => {
78
+ test("the lowest usage wins", () => {
79
+ for (let i = 0; i < 50; i++) {
80
+ expect(pickLowestUsage([0, 1, 2, 3], [0.6, 0.04, 0.53, 0.9])).toBe(1);
81
+ }
82
+ });
83
+
84
+ test("an unobserved index ranks as 0", () => {
85
+ expect(pickLowestUsage([0, 1, 2], [0.3, 0.2])).toBe(2);
86
+ expect(pickLowestUsage([0, 1], undefined, undefined, () => 0.99)).toBe(1);
87
+ });
88
+
89
+ test("ties break at random", () => {
90
+ const usage = [0.1, 0.5, 0.1, 0.1];
91
+ expect(pickLowestUsage([0, 1, 2, 3], usage, undefined, () => 0)).toBe(0);
92
+ expect(pickLowestUsage([0, 1, 2, 3], usage, undefined, () => 0.5)).toBe(2);
93
+ expect(pickLowestUsage([0, 1, 2, 3], usage, undefined, () => 0.99)).toBe(3);
94
+ const seen = new Set<number>();
95
+ for (let i = 0; i < 200; i++) seen.add(pickLowestUsage([0, 1, 2, 3], usage)!);
96
+ expect([...seen].sort()).toEqual([0, 2, 3]);
97
+ });
98
+
99
+ test("picks at random within 0.10 of the lowest, never above the band", () => {
100
+ const usage = [0.11, 0.12, 0.21, 0.22, 0.6];
101
+ expect(pickLowestUsage([0, 1, 2, 3, 4], usage, undefined, () => 0)).toBe(0);
102
+ expect(pickLowestUsage([0, 1, 2, 3, 4], usage, undefined, () => 0.5)).toBe(1);
103
+ expect(pickLowestUsage([0, 1, 2, 3, 4], usage, undefined, () => 0.99)).toBe(2);
104
+ const seen = new Set<number>();
105
+ for (let i = 0; i < 300; i++) seen.add(pickLowestUsage([0, 1, 2, 3, 4], usage)!);
106
+ expect([...seen].sort()).toEqual([0, 1, 2]);
107
+ });
108
+
109
+ test("within the band, the fewest running sessions wins, ties at random", () => {
110
+ const usage = [0.11, 0.12, 0.2, 0.6];
111
+ // Index 3 has no sessions but sits above the band.
112
+ const sessions = [2, 1, 1, 0];
113
+ expect(pickLowestUsage([0, 1, 2, 3], usage, sessions, () => 0)).toBe(1);
114
+ expect(pickLowestUsage([0, 1, 2, 3], usage, sessions, () => 0.99)).toBe(2);
115
+ expect(pickLowestUsage([0, 1, 2], usage, [3, 0], () => 0.99)).toBe(2);
116
+ });
117
+
118
+ test("an empty list picks nothing", () => {
119
+ expect(pickLowestUsage([], [0.1])).toBeUndefined();
120
+ });
121
+ });
122
+
123
+ describe("selectCredential with usage", () => {
124
+ test("picks the lowest-usage available key", () => {
125
+ for (let i = 0; i < 50; i++) {
126
+ const pick = selectCredential(
127
+ POOL,
128
+ [0, 2, 3],
129
+ "CLAUDE_CODE_OAUTH_TOKEN",
130
+ undefined,
131
+ [0.9, 0.01, 0.4, 0.7],
132
+ );
133
+ // Index 1 has the lowest usage but is not admissible: admission is unchanged.
134
+ expect(pick.index).toBe(2);
135
+ expect(pick.isRateLimitFallback).toBe(false);
136
+ }
137
+ });
138
+
139
+ test("a 99%-used key is still picked when it is the only admissible key", () => {
140
+ const pick = selectCredential(
141
+ POOL,
142
+ [3],
143
+ "CLAUDE_CODE_OAUTH_TOKEN",
144
+ undefined,
145
+ [0.1, 0.1, 0.1, 0.99],
146
+ );
147
+ expect(pick.index).toBe(3);
148
+ expect(pick.keySuffix).toBe("ddddd");
149
+ });
150
+
151
+ test("no usage data keeps a random pick over every available key", () => {
152
+ const seen = new Set<number>();
153
+ for (let i = 0; i < 200; i++) seen.add(selectCredential(POOL, [0, 1, 2, 3]).index);
154
+ expect([...seen].sort()).toEqual([0, 1, 2, 3]);
155
+ });
156
+
157
+ test("the allowlist still bounds the pick", () => {
158
+ const seen = new Set<number>();
159
+ for (let i = 0; i < 200; i++) {
160
+ seen.add(
161
+ selectCredential(POOL, [0, 1, 2, 3], "CLAUDE_CODE_OAUTH_TOKEN", [2, 3], [0, 0, 0.5, 0.4])
162
+ .index,
163
+ );
164
+ }
165
+ // 0.5 is within 0.10 of 0.4, so both allowlisted keys share the draw.
166
+ expect([...seen].sort()).toEqual([2, 3]);
167
+ });
168
+
169
+ test("a burst spreads over keys within the band", () => {
170
+ const seen = new Set<number>();
171
+ for (let i = 0; i < 200; i++) {
172
+ seen.add(
173
+ selectCredential(
174
+ POOL,
175
+ [0, 1, 2, 3],
176
+ "CLAUDE_CODE_OAUTH_TOKEN",
177
+ undefined,
178
+ [0.11, 0.12, 0.4, 0.9],
179
+ ).index,
180
+ );
181
+ }
182
+ expect([...seen].sort()).toEqual([0, 1]);
183
+ });
184
+ });
185
+
186
+ describe("resolveCredentialPools with keyUsage from the availability read", () => {
187
+ test("selects the lowest-usage key and logs its usage", async () => {
188
+ logSpy = spyOn(console, "log").mockImplementation(() => {});
189
+ const env: Record<string, string | undefined> = { CLAUDE_CODE_OAUTH_TOKEN: POOL };
190
+ const [selection] = await resolveCredentialPools(env, {
191
+ provider: "claude",
192
+ availableIndicesMap: {
193
+ CLAUDE_CODE_OAUTH_TOKEN: {
194
+ availableIndices: [0, 1, 2, 3],
195
+ keyUsage: [0.53, 0.99, 0.07, 0.6],
196
+ },
197
+ },
198
+ });
199
+ expect(selection?.index).toBe(2);
200
+ expect(env.CLAUDE_CODE_OAUTH_TOKEN).toBe("key-ccccc");
201
+ expect(String(logSpy.mock.calls.at(-1)?.[0])).toContain("usage 7%");
202
+ });
203
+ });
204
+
205
+ const DAY_S = 86_400;
206
+ const nowS = Math.floor(NOW_MS / 1000);
207
+ // 18% used with 5 days to the weekly reset, and 39% used with 2 days to it.
208
+ const EARLY_LOW = { seven_day: { utilization: 0.18, resetsAt: nowS + 5 * DAY_S } };
209
+ const LATE_HIGH = { seven_day: { utilization: 0.39, resetsAt: nowS + 2 * DAY_S } };
210
+
211
+ describe("keyPaceFromWindows", () => {
212
+ test("used minus the elapsed share of the window", () => {
213
+ expect(keyPaceFromWindows(EARLY_LOW, NOW_MS)).toBeCloseTo(0.18 - 2 / 7, 6);
214
+ expect(keyPaceFromWindows(LATE_HIGH, NOW_MS)).toBeCloseTo(0.39 - 5 / 7, 6);
215
+ });
216
+
217
+ test("the most over-pace window binds", () => {
218
+ const windows = {
219
+ ...EARLY_LOW,
220
+ // 1 hour into the 5h window, 50% used: +30 points over pace.
221
+ five_hour: { utilization: 0.5, resetsAt: nowS + 4 * 3600 },
222
+ };
223
+ expect(keyPaceFromWindows(windows, NOW_MS)).toBeCloseTo(0.3, 6);
224
+ });
225
+
226
+ test("a model window counts only for its family", () => {
227
+ const windows = {
228
+ ...LATE_HIGH,
229
+ seven_day_overage_included: { utilization: 0.9, resetsAt: nowS + 2 * DAY_S },
230
+ };
231
+ expect(keyPaceFromWindows(windows, NOW_MS, "fable")).toBeCloseTo(0.9 - 5 / 7, 6);
232
+ expect(keyPaceFromWindows(windows, NOW_MS, "opus")).toBeCloseTo(0.39 - 5 / 7, 6);
233
+ expect(keyPaceFromWindows(windows, NOW_MS)).toBeCloseTo(0.39 - 5 / 7, 6);
234
+ });
235
+
236
+ test("no live window is no reading", () => {
237
+ expect(keyPaceFromWindows(undefined, NOW_MS)).toBeUndefined();
238
+ expect(keyPaceFromWindows({ seven_day: { utilization: 0.5, resetsAt: past } }, NOW_MS)).toBe(
239
+ undefined,
240
+ );
241
+ expect(keyPaceFromWindows({ seven_day: { utilization: 0.5 } }, NOW_MS)).toBeUndefined();
242
+ });
243
+ });
244
+
245
+ describe("credentialSelectionStrategy", () => {
246
+ test("defaults to usage; only pace switches", () => {
247
+ expect(credentialSelectionStrategy(undefined)).toBe("usage");
248
+ expect(credentialSelectionStrategy(" PACE ")).toBe("pace");
249
+ expect(credentialSelectionStrategy("nonsense")).toBe("usage");
250
+ });
251
+ });
252
+
253
+ describe("resolveCredentialPools with keyPace", () => {
254
+ const pool = "key-aaaaa,key-bbbbb";
255
+ const read = (selectionStrategy?: "pace" | "usage", withPace = true) => ({
256
+ CLAUDE_CODE_OAUTH_TOKEN: {
257
+ availableIndices: [0, 1],
258
+ keyUsage: [0.18, 0.39],
259
+ ...(withPace
260
+ ? {
261
+ keyPace: [
262
+ keyPaceFromWindows(EARLY_LOW, NOW_MS)!,
263
+ keyPaceFromWindows(LATE_HIGH, NOW_MS)!,
264
+ ],
265
+ }
266
+ : {}),
267
+ ...(selectionStrategy ? { selectionStrategy } : {}),
268
+ },
269
+ });
270
+
271
+ test("pace picks the 39% key resetting in 2 days over the 18% key resetting in 5", async () => {
272
+ logSpy = spyOn(console, "log").mockImplementation(() => {});
273
+ for (let i = 0; i < 50; i++) {
274
+ const env: Record<string, string | undefined> = { CLAUDE_CODE_OAUTH_TOKEN: pool };
275
+ const [selection] = await resolveCredentialPools(env, {
276
+ provider: "claude",
277
+ availableIndicesMap: read("pace"),
278
+ });
279
+ expect(selection?.index).toBe(1);
280
+ }
281
+ const line = String(logSpy.mock.calls.at(-1)?.[0]);
282
+ expect(line).toContain("usage 39% pace -32% strategy pace");
283
+ });
284
+
285
+ test("usage, no strategy, or no keyPace keeps the old pick", async () => {
286
+ logSpy = spyOn(console, "log").mockImplementation(() => {});
287
+ for (const map of [
288
+ read("usage"),
289
+ read(undefined),
290
+ read(undefined, false),
291
+ read("pace", false),
292
+ ]) {
293
+ const env: Record<string, string | undefined> = { CLAUDE_CODE_OAUTH_TOKEN: pool };
294
+ const [selection] = await resolveCredentialPools(env, {
295
+ provider: "claude",
296
+ availableIndicesMap: map,
297
+ });
298
+ expect(selection?.index).toBe(0);
299
+ expect(String(logSpy.mock.calls.at(-1)?.[0])).toContain("strategy usage");
300
+ }
301
+ });
302
+ });