@desplega.ai/agent-swarm 2.9.0 → 2.10.0

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 (453) hide show
  1. package/dist/acp-adapter-bht22g3k.js +16 -0
  2. package/dist/{acp-targets-swxq55dr.js → acp-targets-arrwv4f0.js} +7 -7
  3. package/dist/{actions-pb678g3e.js → actions-y0d2n1c2.js} +102 -21
  4. package/dist/{agent-fs-provision-dmhnc38q.js → agent-fs-provision-tpj7sktd.js} +4 -4
  5. package/dist/{amp-adapter-3pa66q27.js → amp-adapter-vmmnmw8d.js} +2 -2
  6. package/dist/{anthropic-messages-x2cp5fvf.js → anthropic-messages-zdca3gv1.js} +22 -22
  7. package/dist/{app-17d12ev4.js → app-wb7w45dq.js} +6 -3
  8. package/dist/{approval-notifications-h7vd9pmg.js → approval-notifications-7mqr6vbd.js} +8 -5
  9. package/dist/{assistant-t7k39ree.js → assistant-gf60454p.js} +23 -21
  10. package/dist/{boot-args-schema-0gqmkfbe.js → boot-args-schema-qxmd59sr.js} +1 -1
  11. package/dist/{boot-reembed-f064w382.js → boot-reembed-5tmeskj2.js} +1 -1
  12. package/dist/{boot-reembed-n6anc01d.js → boot-reembed-wb44vc3s.js} +1 -1
  13. package/dist/boot-scrub-sweep-25bxhnw4.js +9 -0
  14. package/dist/{boot-seeds-f99fjs5q.js → boot-seeds-nj9za0vk.js} +4 -4
  15. package/dist/{claude-adapter-gvznwxgc.js → claude-adapter-d3krpdyb.js} +7 -7
  16. package/dist/{claude-managed-adapter-pbrbvc64.js → claude-managed-adapter-t8nfgcgz.js} +2 -2
  17. package/dist/{claude-managed-setup-kdxttvc3.js → claude-managed-setup-7y4pwd81.js} +3 -3
  18. package/dist/{claude-sdk-session-0t2teq6n.js → claude-sdk-session-d138zy0e.js} +7 -7
  19. package/dist/{cli-6dp0jkfj.js → cli-01k6wpr9.js} +4 -4
  20. package/dist/{cli-c07v0qfa.js → cli-124ntmf0.js} +83 -74
  21. package/dist/{cli-1tvdx9t5.js → cli-2rnd5j3t.js} +1 -1
  22. package/dist/{cli-4ghh072d.js → cli-2spnz7ye.js} +36 -36
  23. package/dist/{cli-dd37e0cm.js → cli-2v2zdm0a.js} +1 -1
  24. package/dist/{cli-aa8s22gn.js → cli-3aaeb78m.js} +195 -186
  25. package/dist/{cli-8x8y70av.js → cli-3caf7pmg.js} +2 -2
  26. package/dist/{cli-jd3kxppb.js → cli-3et6fmfe.js} +1 -1
  27. package/dist/{cli-kaa4bd7x.js → cli-3f8zbtqa.js} +9 -8
  28. package/dist/{cli-57pvhnyt.js → cli-43evxwj4.js} +1 -1
  29. package/dist/cli-43ms35q9.js +270 -0
  30. package/dist/cli-47hz21se.js +25584 -0
  31. package/dist/{cli-n515ex5z.js → cli-4ymxrwg5.js} +16 -16
  32. package/dist/{cli-2tnbc3gw.js → cli-539h29es.js} +2379 -66
  33. package/dist/{cli-rts1qvb4.js → cli-5qhfv9rw.js} +2 -2
  34. package/dist/{cli-faf46gdq.js → cli-6037prsx.js} +10 -10
  35. package/dist/{cli-bk9v1xrv.js → cli-6cwr32fe.js} +25 -25
  36. package/dist/{cli-812paz57.js → cli-6ehs4mta.js} +18 -2
  37. package/dist/{cli-95930z69.js → cli-6ym4jhzp.js} +2 -2
  38. package/dist/{cli-zg3z63x3.js → cli-7mv4h3sn.js} +1 -1
  39. package/dist/{cli-9a2rtd77.js → cli-7r162fxe.js} +24 -15
  40. package/dist/{cli-eg437b5p.js → cli-7sf9sb0w.js} +2 -1
  41. package/dist/{cli-vw5x18yj.js → cli-8st34zhr.js} +20 -10
  42. package/dist/{cli-fgphc007.js → cli-91pkxres.js} +1 -1
  43. package/dist/{cli-2kg31ted.js → cli-9fjr27ez.js} +11 -11
  44. package/dist/{cli-wr1h0k28.js → cli-9w8712tq.js} +617 -278
  45. package/dist/cli-aw83sr40.js +98 -0
  46. package/dist/{cli-ssvdrk4y.js → cli-b6px1k1q.js} +1 -1
  47. package/dist/{cli-w7dxb2fj.js → cli-cc93xzes.js} +6 -6
  48. package/dist/{cli-js8zrfyg.js → cli-fcfbct2j.js} +5 -5
  49. package/dist/{cli-3zxdz9r7.js → cli-fq2rr3jb.js} +6 -6
  50. package/dist/{boot-scrub-sweep-ejd5dnrz.js → cli-had1p04a.js} +8 -9
  51. package/dist/{cli-7cgf614g.js → cli-j6te8keb.js} +5 -5
  52. package/dist/{cli-25nstr34.js → cli-jrbq4djp.js} +1 -1
  53. package/dist/{cli-pz1c00aa.js → cli-k3q2va86.js} +2 -2
  54. package/dist/{cli-nhq5js1b.js → cli-k5yewqgd.js} +3 -3
  55. package/dist/cli-kbtzfrxj.js +404 -0
  56. package/dist/{cli-dx439zpg.js → cli-kpgcm7xy.js} +13 -1
  57. package/dist/{cli-mc1fx4xd.js → cli-m9cvf7vs.js} +10 -10
  58. package/dist/{cli-kmvcm34h.js → cli-md50mj0r.js} +2 -2
  59. package/dist/{cli-q8cfh1t9.js → cli-mk7c5ac4.js} +1 -1
  60. package/dist/cli-mys27d03.js +321 -0
  61. package/dist/{cli-spzkdgtg.js → cli-n3qe7tyr.js} +172 -168
  62. package/dist/{cli-7akn6ze9.js → cli-nh7x3133.js} +7 -28
  63. package/dist/{cli-fnh4b12r.js → cli-nstkanmx.js} +6 -6
  64. package/dist/{cli-g3zzby8t.js → cli-nv4xsvmk.js} +5 -5
  65. package/dist/{cli-sn4pc4qx.js → cli-p0c3vtch.js} +2 -2
  66. package/dist/{cli-5ntc4we6.js → cli-p0n7b5rz.js} +859 -2058
  67. package/dist/{cli-pn1v1pb0.js → cli-psffsanb.js} +3 -2
  68. package/dist/{cli-svc803vq.js → cli-q5srsxq2.js} +15 -15
  69. package/dist/{cli-nmcpzbty.js → cli-qrta34nb.js} +4 -4
  70. package/dist/{cli-k09cxm6m.js → cli-qx4f057j.js} +2 -2
  71. package/dist/{cli-eepnpee7.js → cli-s1khw52h.js} +12 -12
  72. package/dist/{cli-h474mvp5.js → cli-s1zqk1aj.js} +21 -11
  73. package/dist/{cli-8axx8vnw.js → cli-sb4426be.js} +10 -10
  74. package/dist/{cli-kqh27v2y.js → cli-th1dkc2v.js} +1 -1
  75. package/dist/{cli-cw7mc1je.js → cli-tr27kwyg.js} +3 -3
  76. package/dist/{cli-5kcmxwxv.js → cli-tvgqesec.js} +7 -5
  77. package/dist/{cli-a9hfzg2r.js → cli-ty3gha1p.js} +6 -6
  78. package/dist/{cli-fjggej24.js → cli-v46zsvqk.js} +1 -1
  79. package/dist/{cli-5gygrr67.js → cli-v8z0yga9.js} +4 -4
  80. package/dist/{cli-k7pt28ad.js → cli-vfqvrd82.js} +5 -5
  81. package/dist/{cli-vvgaz8t6.js → cli-vq2eg24x.js} +2 -2
  82. package/dist/{cli-pwsax0vp.js → cli-vsw91x06.js} +23 -22
  83. package/dist/{cli-7fx9jyfs.js → cli-w1q68d8x.js} +67 -11
  84. package/dist/{cli-c5cspahx.js → cli-wafmk3fn.js} +45 -20
  85. package/dist/{cli-ymct0ycy.js → cli-wwp1bd3n.js} +5495 -5095
  86. package/dist/{cli-me4maa4j.js → cli-y4ccxtz5.js} +45 -9
  87. package/dist/{cli-m1fbz2wj.js → cli-y8p78ekx.js} +178 -19
  88. package/dist/{cli-nr46j5pw.js → cli-z0m452jr.js} +1 -1
  89. package/dist/{cli-gddgcnav.js → cli-zeh2mhkg.js} +1 -1
  90. package/dist/{cli-e9973bf0.js → cli-zk7v5s89.js} +2 -2
  91. package/dist/cli.js +24 -23
  92. package/dist/{cloudflare-workers-ai-system-one-9y1zx3pt.js → cloudflare-workers-ai-system-one-4aysy6rh.js} +2 -2
  93. package/dist/{codex-adapter-44yq2fkn.js → codex-adapter-exfdbj73.js} +6 -6
  94. package/dist/{codex-hook-h1zb7cfk.js → codex-hook-59atdwd8.js} +1 -1
  95. package/dist/{codex-login-cdyrr5y1.js → codex-login-a239g1ah.js} +1 -1
  96. package/dist/{codex-session-runner-zbh26gbw.js → codex-session-runner-xhznc4g3.js} +6 -6
  97. package/dist/{commands-54f02v5b.js → commands-d8g349v4.js} +3 -3
  98. package/dist/{controller-9gpwcdek.js → controller-mwgdmb8y.js} +6 -3
  99. package/dist/{controllers-connect-61re6k1n.js → controllers-connect-5v7qx04j.js} +22 -20
  100. package/dist/core-sxy4epp2.js +38 -0
  101. package/dist/{cursor-adapter-7e4g81vd.js → cursor-adapter-p2q5h35d.js} +1 -1
  102. package/dist/{db-wnh0qjy1.js → db-qpganbeb.js} +3 -3
  103. package/dist/{db-dg2gc7fd.js → db-wcw0b2j8.js} +8 -2
  104. package/dist/{dispatcher-jrm24r43.js → dispatcher-8d8s49yz.js} +20 -18
  105. package/dist/{docker-9aq30pp2.js → docker-09tkkg0n.js} +42 -3
  106. package/dist/{dsh-adapter-1yqk267c.js → dsh-adapter-me0twn13.js} +1 -1
  107. package/dist/{easter-egg-3d-r4h9cs9m.js → easter-egg-3d-wv7w5ty6.js} +1 -1
  108. package/dist/{embedding-space-bafjr5f7.js → embedding-space-e48xchqa.js} +6 -6
  109. package/dist/{embeddings-7xmgqe0e.js → embeddings-tgq2afbq.js} +1 -1
  110. package/dist/{execute-d6jg5ns0.js → execute-8rhwrk7f.js} +1 -1
  111. package/dist/{external-channel-frq1xvq2.js → external-channel-8bt3j2pm.js} +2 -2
  112. package/dist/fx-adapter-adtdb3jb.js +19 -0
  113. package/dist/{google-generative-ai-hh7strj3.js → google-generative-ai-bh0djqjp.js} +5 -5
  114. package/dist/{google-vertex-fy5bw2pm.js → google-vertex-prjbw8dj.js} +9 -9
  115. package/dist/grok-adapter-accnn9km.js +19 -0
  116. package/dist/handlers-1x77561e.js +35 -0
  117. package/dist/{heartbeat-6bpa611t.js → heartbeat-x0056ra5.js} +23 -21
  118. package/dist/{hook-hvm5nf46.js → hook-a7vmn2st.js} +5 -5
  119. package/dist/{hook-w12k146y.js → hook-mrd2ayxv.js} +6 -6
  120. package/dist/{http-43tthdvs.js → http-ym9c5svp.js} +1335 -363
  121. package/dist/{index-fn054vsf.js → index-01ns1pkw.js} +1 -1
  122. package/dist/{index-1f0vypbg.js → index-0h6qd4p1.js} +34 -24
  123. package/dist/index-48qzcqzj.js +35 -0
  124. package/dist/{index-4b1q8kan.js → index-4k7m5dgj.js} +629 -354
  125. package/dist/index-620y7jzq.js +15 -0
  126. package/dist/{index-cret47a5.js → index-8p0j4h4j.js} +506 -43
  127. package/dist/{index-m5gx1xd6.js → index-9myfj74s.js} +40 -40
  128. package/dist/{index-bs7gw5fd.js → index-b5wgnm78.js} +44 -44
  129. package/dist/{index-ftywhyr0.js → index-ny60jcat.js} +394 -66
  130. package/dist/{index-tx9mc2ap.js → index-p46tvwtx.js} +24 -22
  131. package/dist/{index-585gyczt.js → index-pe3d8jfy.js} +3 -3
  132. package/dist/index-rkvaambe.js +36 -0
  133. package/dist/{index-mhq4sh5w.js → index-x6azjwm8.js} +1 -1
  134. package/dist/index-xj1mq27m.js +40 -0
  135. package/dist/k8s-m56sw868.js +217 -0
  136. package/dist/{keepalive-rk0j0jvh.js → keepalive-7yxg8z4c.js} +3 -3
  137. package/dist/lead-enzn86bz.js +74 -0
  138. package/dist/{maintenance-g6z6kaxw.js → maintenance-6tefxmk1.js} +1 -1
  139. package/dist/{node-jftbyawr.js → node-2wfcey7d.js} +1 -1
  140. package/dist/{oauth-refresh-sweep-2k3g2q4w.js → oauth-refresh-sweep-70cavc3a.js} +2 -2
  141. package/dist/{onboard-6fdbvh3v.js → onboard-fja65z08.js} +5 -5
  142. package/dist/{openai-decisions-q3hh60wr.js → openai-decisions-ytp6w6xh.js} +1 -1
  143. package/dist/{opencode-adapter-fdbsvts7.js → opencode-adapter-hakkm7yh.js} +6 -6
  144. package/dist/{otel-impl-rvwtc09q.js → otel-impl-7zhnt6gj.js} +1 -1
  145. package/dist/{pending-sqkrb836.js → pending-h01yzpx4.js} +21 -19
  146. package/dist/{pi-mono-adapter-asqayqcg.js → pi-mono-adapter-f52hz743.js} +40 -13
  147. package/dist/{pricing-refresh-f5tkgmw2.js → pricing-refresh-fzxvk2zf.js} +2 -2
  148. package/dist/{rbac-roles-06s555fa.js → rbac-roles-9v6vhgbs.js} +3 -3
  149. package/dist/{rbac-roles-szxsg8t1.js → rbac-roles-hfgh8sba.js} +2 -2
  150. package/dist/{render-v2-103p266q.js → render-v2-1rgspxzf.js} +6 -3
  151. package/dist/{resume-v7kjewt9.js → resume-tqy61x24.js} +10 -7
  152. package/dist/{runtime-f1gmk0b7.js → runtime-exkxyx5f.js} +1 -1
  153. package/dist/{session-access-6shev8ts.js → session-access-3act7vt4.js} +4 -4
  154. package/dist/{session-source-backfill-bd7spzjd.js → session-source-backfill-82qe7fqq.js} +3 -3
  155. package/dist/{setup-kmxmztdb.js → setup-ry1vfdef.js} +1 -1
  156. package/dist/thread-buffer-3x93yexa.js +38 -0
  157. package/dist/{typesafe-system-one-f2en2knd.js → typesafe-system-one-vcfj2ak8.js} +2 -2
  158. package/dist/user-merge-jobs-bx6bf7db.js +22 -0
  159. package/dist/{virtual-modules-57eje5wy.js → virtual-modules-s0rjzcqq.js} +3 -3
  160. package/dist/{work-objects-zr47ygmf.js → work-objects-z2e5tv03.js} +29 -23
  161. package/dist/{worker-follow-up-grrr3fda.js → worker-follow-up-y3xeg89w.js} +20 -18
  162. package/dist/worker-zfcv2wbf.js +74 -0
  163. package/openapi.json +2763 -147
  164. package/package.json +9 -8
  165. package/src/apps/catalog.generated.json +43 -3
  166. package/src/apps/page-validator.ts +27 -0
  167. package/src/be/boot-seeds.ts +7 -1
  168. package/src/be/db/model-groups.ts +238 -0
  169. package/src/be/db/model-profiles.ts +28 -7
  170. package/src/be/db/session-costs.ts +225 -0
  171. package/src/be/db/session-log-reasoning.ts +36 -0
  172. package/src/be/db/tasks/read.ts +8 -3
  173. package/src/be/db/tasks/write.ts +34 -1
  174. package/src/be/db/user-references.ts +75 -0
  175. package/src/be/db/users/references.ts +185 -0
  176. package/src/be/db-client.ts +70 -19
  177. package/src/be/db.ts +232 -361
  178. package/src/be/key-window-usage.ts +386 -0
  179. package/src/be/memory/demotion-evidence.ts +34 -0
  180. package/src/be/memory/reranker.ts +34 -12
  181. package/src/be/memory/review-recall-filter.ts +83 -0
  182. package/src/be/memory/search-candidates.ts +19 -6
  183. package/src/be/memory/types.ts +11 -0
  184. package/src/be/migrations/303_task_ui_specs.sql +28 -0
  185. package/src/be/migrations/306_provider_account_telemetry.sql +1 -0
  186. package/src/be/migrations/307_session_cost_credential_attribution.sql +23 -0
  187. package/src/be/migrations/310_session_logs_reasoning.sql +12 -0
  188. package/src/be/migrations/312_task_ui_action_runs_repeatable.sql +6 -0
  189. package/src/be/migrations/314_model_groups.sql +85 -0
  190. package/src/be/migrations/318_superseded_workflow_cut_backfill.sql +22 -0
  191. package/src/be/migrations/319_user_fk_indexes.sql +153 -0
  192. package/src/be/migrations/320_session_costs_kind.sql +4 -0
  193. package/src/be/migrations/321_user_merge_jobs.sql +52 -0
  194. package/src/be/pricing-normalize.ts +2 -0
  195. package/src/be/rbac-roles.ts +6 -4
  196. package/src/be/runtime-pools/db.ts +23 -5
  197. package/src/be/runtime-pools/supervisor.ts +15 -1
  198. package/src/be/scripts/db.ts +11 -0
  199. package/src/be/scripts/typecheck.ts +2 -0
  200. package/src/be/seed-model-groups.ts +86 -0
  201. package/src/be/seed-model-profiles.ts +20 -13
  202. package/src/be/seed-scripts/catalog/boot-triage.inline.ts +1 -0
  203. package/src/be/seed-scripts/catalog/boot-triage.ts +1 -0
  204. package/src/be/seed-scripts/catalog/dreaming-gather.ts +165 -1
  205. package/src/be/seed-scripts/catalog/wait-for-task.ts +5 -1
  206. package/src/be/seed-skills/index.ts +3 -0
  207. package/src/be/swarm-config-guard.ts +58 -1
  208. package/src/be/task-ui-spec.ts +259 -0
  209. package/src/be/user-merge-jobs.ts +564 -0
  210. package/src/be/users.ts +41 -23
  211. package/src/commands/controller.ts +3 -0
  212. package/src/commands/credential-outcome-report.ts +13 -3
  213. package/src/commands/onboard/agent-fs-version.ts +1 -1
  214. package/src/commands/onboard/compose-generator.ts +1 -1
  215. package/src/commands/onboard/env-generator.ts +1 -1
  216. package/src/commands/provider-credentials.ts +1 -1
  217. package/src/commands/rate-limit-outcome.ts +6 -0
  218. package/src/commands/runner.ts +34 -53
  219. package/src/extensions/catalog.generated.json +3 -3
  220. package/src/extensions/contract-types.generated.ts +1 -1
  221. package/src/extensions/contract.ts +15 -0
  222. package/src/extensions/dispatcher.ts +14 -4
  223. package/src/github/handlers.ts +57 -0
  224. package/src/github/types.ts +4 -0
  225. package/src/http/all-routes.ts +2 -0
  226. package/src/http/api-keys.ts +258 -30
  227. package/src/http/approval-requests.ts +5 -1
  228. package/src/http/auth.ts +3 -1
  229. package/src/http/config.ts +34 -1
  230. package/src/http/index.ts +13 -0
  231. package/src/http/memory.ts +19 -6
  232. package/src/http/model-groups.ts +277 -0
  233. package/src/http/model-profiles.ts +30 -13
  234. package/src/http/session-data.ts +74 -23
  235. package/src/http/sessions.ts +6 -2
  236. package/src/http/tasks.ts +140 -5
  237. package/src/http/tool-hooks.ts +140 -0
  238. package/src/http/users.ts +142 -19
  239. package/src/http/workflows.ts +19 -1
  240. package/src/integrations/chat-sdk/inbound.ts +8 -2
  241. package/src/integrations/chat-sdk/limits.ts +8 -3
  242. package/src/integrations/chat-sdk/relay.ts +87 -0
  243. package/src/jira/outbound.ts +5 -3
  244. package/src/linear/outbound.ts +5 -1
  245. package/src/prompts/base-prompt.ts +8 -0
  246. package/src/prompts/session-templates.ts +19 -2
  247. package/src/providers/acp-targets.ts +2 -2
  248. package/src/providers/amp-adapter.ts +1 -1
  249. package/src/providers/claude-adapter.ts +27 -4
  250. package/src/providers/claude-flag-settings.ts +39 -1
  251. package/src/providers/codex-adapter.ts +85 -3
  252. package/src/providers/codex-app-server.ts +9 -5
  253. package/src/providers/codex-oauth/auth-json.ts +1 -0
  254. package/src/providers/pi-mono-adapter.ts +35 -2
  255. package/src/providers/pricing-sources.md +41 -0
  256. package/src/providers/types.ts +11 -0
  257. package/src/rbac/legacy-policy.ts +1 -0
  258. package/src/rbac/permissions.ts +5 -0
  259. package/src/runtime-providers/docker.ts +65 -1
  260. package/src/runtime-providers/index.ts +7 -5
  261. package/src/runtime-providers/k8s.ts +250 -0
  262. package/src/scheduler/deferred-task-waits.ts +18 -5
  263. package/src/scripts-runtime/types/stdlib.d.ts +2 -0
  264. package/src/scripts-runtime/types/swarm-extension.d.ts +24 -9
  265. package/src/scripts-runtime/types/swarm-sdk.d.ts +2 -0
  266. package/src/slack/actions.ts +79 -0
  267. package/src/slack/app.ts +22 -15
  268. package/src/slack/blocks.ts +23 -2
  269. package/src/slack/inbound-files.ts +2 -0
  270. package/src/slack/render-v2.ts +154 -23
  271. package/src/slack/responses.ts +3 -2
  272. package/src/slack/work-objects.ts +4 -1
  273. package/src/tasks/errors.ts +5 -0
  274. package/src/tests/acp-adapter.test.ts +30 -6
  275. package/src/tests/api-key-tracking.test.ts +0 -72
  276. package/src/tests/api-keys-status-model-limits.test.ts +93 -1
  277. package/src/tests/api-keys-validation.test.ts +42 -0
  278. package/src/tests/approval-card-resolution.test.ts +7 -1
  279. package/src/tests/approval-requests-notifications.test.ts +11 -1
  280. package/src/tests/apps-catalog-page-frame.test.ts +49 -0
  281. package/src/tests/apps-session-thread.test.ts +79 -0
  282. package/src/tests/base-prompt.test.ts +111 -5
  283. package/src/tests/chat-sdk-relay.test.ts +170 -2
  284. package/src/tests/check-pr-body.test.ts +43 -0
  285. package/src/tests/claude-adapter-binary.test.ts +67 -6
  286. package/src/tests/claude-flag-settings.test.ts +29 -0
  287. package/src/tests/claude-sdk-lifecycle.test.ts +5 -0
  288. package/src/tests/codex-adapter.test.ts +340 -2
  289. package/src/tests/codex-app-server.test.ts +16 -0
  290. package/src/tests/codex-oauth.test.ts +3 -1
  291. package/src/tests/codex-rate-limits.test.ts +384 -0
  292. package/src/tests/credential-check.test.ts +14 -6
  293. package/src/tests/credential-outcome-report.test.ts +20 -0
  294. package/src/tests/db-client.test.ts +146 -0
  295. package/src/tests/db-task-read-contract.test.ts +3 -0
  296. package/src/tests/db-task-write-contract.test.ts +4 -0
  297. package/src/tests/deferred-task-wake.test.ts +63 -1
  298. package/src/tests/deploy-guard.test.ts +7 -7
  299. package/src/tests/deploy-select.test.ts +83 -0
  300. package/src/tests/docker-isolation-config-guard.test.ts +29 -0
  301. package/src/tests/docs-index-footprint.test.js +11 -3
  302. package/src/tests/dreaming-gather-corrections.test.ts +467 -0
  303. package/src/tests/e2e-visuals-comment.test.ts +82 -0
  304. package/src/tests/e2e-visuals-verify-pr.test.ts +138 -0
  305. package/src/tests/event-loop-lag.test.ts +9 -7
  306. package/src/tests/extensions-example-sql-admin-only.test.ts +7 -4
  307. package/src/tests/fixtures/extensions/native-tool-recorder/hooks.ts +26 -0
  308. package/src/tests/fixtures/extensions/native-tool-recorder/manifest.json +10 -0
  309. package/src/tests/fixtures/onboard/content.compose.yml.txt +1 -1
  310. package/src/tests/fixtures/onboard/content.env.txt +1 -1
  311. package/src/tests/fixtures/onboard/dev.compose.yml.txt +1 -1
  312. package/src/tests/fixtures/onboard/dev.env.txt +1 -1
  313. package/src/tests/fixtures/onboard/full-all.compose.yml.txt +1 -1
  314. package/src/tests/fixtures/onboard/full-all.env.txt +1 -1
  315. package/src/tests/fixtures/onboard/full.compose.yml.txt +1 -1
  316. package/src/tests/fixtures/onboard/full.env.txt +1 -1
  317. package/src/tests/fixtures/onboard/research.compose.yml.txt +1 -1
  318. package/src/tests/fixtures/onboard/research.env.txt +1 -1
  319. package/src/tests/fixtures/onboard/solo.compose.yml.txt +1 -1
  320. package/src/tests/fixtures/onboard/solo.env.txt +1 -1
  321. package/src/tests/fixtures/ui-spec-slack/deploys.json +121 -0
  322. package/src/tests/fixtures/ui-spec-slack/incident.json +183 -0
  323. package/src/tests/fixtures/ui-spec-slack/three-charts-stale.json +196 -0
  324. package/src/tests/fixtures/ui-spec-slack/video-rejected-by-slack.json +183 -0
  325. package/src/tests/fixtures/ui-spec-slack/video.json +446 -0
  326. package/src/tests/github-event-filter.test.ts +8 -0
  327. package/src/tests/github-handlers-inline-comments.test.ts +2 -0
  328. package/src/tests/github-handlers-reactions.test.ts +4 -0
  329. package/src/tests/github-handlers.test.ts +3 -0
  330. package/src/tests/github-sender-trust.test.ts +223 -0
  331. package/src/tests/gitleaks-download-retry.test.ts +85 -0
  332. package/src/tests/http-users.test.ts +142 -41
  333. package/src/tests/human-thread-routing.test.ts +187 -0
  334. package/src/tests/jira-outbound-sync.test.ts +33 -1
  335. package/src/tests/k8s-pool-smoke.test.ts +190 -0
  336. package/src/tests/key-window-usage.test.ts +846 -0
  337. package/src/tests/linear-outbound-sync.test.ts +30 -1
  338. package/src/tests/memory-rater-store.test.ts +35 -1
  339. package/src/tests/memory-reranker.test.ts +39 -4
  340. package/src/tests/memory-review-recall-filter.test.ts +205 -0
  341. package/src/tests/migration-318-superseded-workflow-cut-backfill.test.ts +74 -0
  342. package/src/tests/model-groups-api.test.ts +415 -0
  343. package/src/tests/model-profiles-api.test.ts +1 -0
  344. package/src/tests/operator-granted-credentials.test.ts +7 -2
  345. package/src/tests/operator-granted-role-wire.test.ts +2 -1
  346. package/src/tests/pi-mono-adapter.test.ts +79 -0
  347. package/src/tests/preload-env.test.ts +2 -0
  348. package/src/tests/preload.ts +3 -1
  349. package/src/tests/prompt-template-session.test.ts +4 -3
  350. package/src/tests/provider-quota-persistence.test.ts +362 -0
  351. package/src/tests/rate-limit-event.test.ts +22 -0
  352. package/src/tests/rbac-engine.test.ts +1 -0
  353. package/src/tests/reasoning-events.test.ts +164 -0
  354. package/src/tests/routing-affinity-inventory.test.ts +22 -0
  355. package/src/tests/runtime-k8s-pools.test.ts +142 -0
  356. package/src/tests/runtime-pools-db.test.ts +42 -2
  357. package/src/tests/runtime-pools-http.test.ts +47 -2
  358. package/src/tests/runtime-provider-registration.test.ts +5 -14
  359. package/src/tests/runtime-providers-docker.test.ts +91 -0
  360. package/src/tests/runtime-providers-k8s.test.ts +303 -0
  361. package/src/tests/script-connections.test.ts +18 -1
  362. package/src/tests/secret-canary-sweep.test.ts +41 -5
  363. package/src/tests/seed-scripts.test.ts +23 -0
  364. package/src/tests/session-cost-attribution.test.ts +748 -0
  365. package/src/tests/session-costs.test.ts +14 -15
  366. package/src/tests/session-logs.test.ts +83 -0
  367. package/src/tests/sessions-detail-batch.test.ts +7 -0
  368. package/src/tests/short-id.test.ts +23 -0
  369. package/src/tests/slack-blocks.test.ts +69 -0
  370. package/src/tests/slack-render-v2.test.ts +411 -0
  371. package/src/tests/slack-start-reload-race.test.ts +67 -0
  372. package/src/tests/spec-base-url.test.ts +13 -1
  373. package/src/tests/subscription-plans.test.ts +20 -1
  374. package/src/tests/system-one-decide-session-cost.test.ts +196 -0
  375. package/src/tests/task-ui-action-route.test.ts +249 -0
  376. package/src/tests/task-ui-actions.test.ts +195 -0
  377. package/src/tests/task-ui-script-actions.test.ts +517 -0
  378. package/src/tests/task-ui-spec-fallback.test.ts +65 -0
  379. package/src/tests/task-ui-spec.test.ts +368 -0
  380. package/src/tests/tool-hooks-http.test.ts +187 -0
  381. package/src/tests/ui-spec-script-actions-config.test.ts +196 -0
  382. package/src/tests/user-delete-perf.test.ts +321 -0
  383. package/src/tests/user-merge-jobs.test.ts +680 -0
  384. package/src/tests/vcs-provider.test.ts +25 -5
  385. package/src/tests/vendored-openapi.test.ts +13 -1
  386. package/src/tests/workflow-agent-task-retry.test.ts +96 -1
  387. package/src/tests/workflow-foreach-supersede-retry.test.ts +10 -0
  388. package/src/tests/workflow-http-v2.test.ts +27 -0
  389. package/src/tests/workflow-replay.test.ts +10 -1
  390. package/src/tests/workflow-triggers-v2.test.ts +128 -0
  391. package/src/tests/workflow-wait-event.test.ts +109 -1
  392. package/src/tools/defer-task.ts +1 -1
  393. package/src/tools/memory-search.ts +14 -3
  394. package/src/tools/store-progress.ts +69 -3
  395. package/src/tools/swarm-config/delete-config.ts +12 -1
  396. package/src/tools/swarm-config/set-config.ts +8 -0
  397. package/src/tools/system-one-decide.ts +113 -3
  398. package/src/tools/utils.ts +4 -1
  399. package/src/tools/workflows/create-workflow.ts +2 -0
  400. package/src/tools/workflows/update-workflow.ts +3 -1
  401. package/src/types.ts +93 -4
  402. package/src/ui-spec/actions.ts +112 -0
  403. package/src/ui-spec/fallback.ts +19 -0
  404. package/src/ui-spec/schema.ts +38 -0
  405. package/src/ui-spec/script-actions.ts +340 -0
  406. package/src/ui-spec/script-allowlist.ts +48 -0
  407. package/src/ui-spec/slack.ts +17 -0
  408. package/src/ui-spec/validate.ts +169 -0
  409. package/src/utils/codex-rate-limits.ts +159 -0
  410. package/src/utils/credentials.ts +2 -0
  411. package/src/utils/error-tracker.ts +19 -0
  412. package/src/utils/provider-account-telemetry.ts +331 -0
  413. package/src/utils/reasoning-events.ts +107 -0
  414. package/src/utils/short-id.ts +20 -14
  415. package/src/utils/subscription-plans.ts +11 -59
  416. package/src/utils/task-ui-spec.ts +79 -0
  417. package/src/vcs/index.ts +12 -2
  418. package/src/workflows/executors/system-one-decision.ts +2 -2
  419. package/src/workflows/resume.ts +3 -2
  420. package/src/workflows/task-step-routing.ts +38 -13
  421. package/src/workflows/triggers.ts +35 -3
  422. package/templates/extensions/manifest.schema.json +13 -0
  423. package/templates/extensions/sql-admin-only/README.md +2 -1
  424. package/templates/extensions/sql-admin-only/hooks.ts +7 -3
  425. package/templates/extensions/sql-admin-only/manifest.json +1 -1
  426. package/templates/skills/apps/SKILL.md +32 -5
  427. package/templates/skills/apps/content.md +32 -5
  428. package/templates/skills/swarm-docs/content.md +2 -0
  429. package/templates/skills/ui-spec/config.json +14 -0
  430. package/templates/skills/ui-spec/content.md +133 -0
  431. package/templates/workflows/dreaming/content.md +1 -1
  432. package/dist/acp-adapter-jqn86aam.js +0 -16
  433. package/dist/cli-7831nhaw.js +0 -93
  434. package/dist/cli-srtkr1h2.js +0 -12078
  435. package/dist/fx-adapter-0zrkfbkd.js +0 -19
  436. package/dist/grok-adapter-3db1c538.js +0 -19
  437. package/dist/handlers-k22n8mnn.js +0 -33
  438. package/dist/index-8xcyd3mc.js +0 -33
  439. package/dist/index-9sy2bh6n.js +0 -38
  440. package/dist/index-b24j7t2m.js +0 -12797
  441. package/dist/index-wjd56k1v.js +0 -34
  442. package/dist/lead-atxf3q6e.js +0 -72
  443. package/dist/thread-buffer-4yz236d0.js +0 -36
  444. package/dist/worker-p4hbtcy3.js +0 -72
  445. package/dist/{azure-openai-responses-590s1eea.js → azure-openai-responses-b3y7fz36.js} +8 -8
  446. package/dist/{cli-j49yqpvh.js → cli-7evph51z.js} +3 -3
  447. package/dist/{cli-g0vv9dg4.js → cli-8y43jx2r.js} +8 -8
  448. package/dist/{cli-m0cy2vyf.js → cli-k6nejceb.js} +4 -4
  449. package/dist/{mistral-conversations-0c30em7t.js → mistral-conversations-tm2vd4gc.js} +8 -8
  450. package/dist/{openai-codex-responses-bkcd8pjj.js → openai-codex-responses-sxjv60xx.js} +4 -4
  451. package/dist/{openai-completions-f3n69rep.js → openai-completions-0c7qnhsx.js} +20 -20
  452. package/dist/{openai-responses-gbh0wfqe.js → openai-responses-a7h29chp.js} +15 -15
  453. package/dist/{pi-messages-zebyt7gb.js → pi-messages-2j0fpqwq.js} +5 -5
@@ -0,0 +1,564 @@
1
+ /**
2
+ * Resumable background user merges (`user_merge_jobs`).
3
+ *
4
+ * POST /api/users/{id}/merge does the short part in one transaction (moves
5
+ * identities and email aliases, runs the guards, marks the source with
6
+ * `users.mergedIntoUserId`, records the manual_merge event) and inserts a
7
+ * pending job. This module runs the long part: it moves every retained
8
+ * reference to the source user over to the target, in batches, each in its
9
+ * own short transaction, and finally deletes the source row.
10
+ *
11
+ * Why: deleteUser(source, target) inside the request froze the API for 278 s
12
+ * on an 85 GB production database (incident 2026-11-09). Every statement is
13
+ * synchronous bun:sqlite on the event loop, so a request can only stay
14
+ * responsive if no single statement touches more than a few thousand rows.
15
+ *
16
+ * Shape:
17
+ * - One job at a time, process-wide (`runUserMergeJobs` is a mutex; a kick
18
+ * while a drain runs schedules another pass).
19
+ * - Phase `workflow-context`: rewrite context.swarm.requestedByUserId in the
20
+ * runs the source requested, by id, found through the expression index on
21
+ * the context requester (selectWorkflowRunsRequestedBy).
22
+ * - Phase `<table>.<column>`: one discovered user FK column at a time,
23
+ * `batchSize` rows per transaction, until a batch comes back short.
24
+ * - Phase `delete-user`: one short transaction that sweeps every column once
25
+ * more (only the rows written since that column's pass: a running workflow
26
+ * or an agent task still holds the source id in memory and keeps inserting
27
+ * rows that name it), then DELETE FROM users, then the done mark. The sweep
28
+ * and the delete share the write lock, so no row can slip in between and
29
+ * fail the FK parent-delete check.
30
+ * - Paced like src/be/boot-scrub-sweep.ts: after each batch the runner rests
31
+ * for a multiple of the batch's own duration, and the multiple follows the
32
+ * event-loop lag it measures on its own timer.
33
+ *
34
+ * Every batch is idempotent (rows already moved no longer match the source),
35
+ * so a crash or a thrown test hook leaves a consistent database and the next
36
+ * run continues from the first unprocessed row. `progress` records rows
37
+ * rewritten per phase for the UI; it is written in the same transaction as
38
+ * the batch.
39
+ *
40
+ * Failures: a thrown attempt puts the job back to `pending` with the error
41
+ * and a `nextAttemptAt` in the future (bounded exponential backoff), and a
42
+ * timer kicks the runner then. After USER_MERGE_MAX_ATTEMPTS the job is
43
+ * `failed`; a repeat POST of the same merge re-queues it (requeueUserMergeJob).
44
+ * A missing target is terminal at once (TerminalUserMergeError): no retry can
45
+ * help, and the route lets the operator merge the source elsewhere. On boot,
46
+ * resumeUserMergeJobs re-queues the jobs the previous process left running and
47
+ * clears pending backoffs; failed jobs stay failed.
48
+ *
49
+ * Human-free reclassification (reclassifyTaskHumanFree) is not needed here:
50
+ * the rule only changes when a requester becomes NULL, and a merge always
51
+ * replaces the source with the target.
52
+ */
53
+
54
+ import { invalidateTelemetryUserRole, scheduleOrgDomainRecompute } from "../telemetry-identity";
55
+ import type { UserMergeJob, UserMergeJobStatus } from "../types";
56
+ import { scrubSecrets } from "../utils/secret-scrubber";
57
+ import { nextRestRatio } from "./boot-scrub-sweep";
58
+ import { getDbClient } from "./db";
59
+ import {
60
+ discoverUserReferenceColumns,
61
+ rewriteUserReferenceColumn,
62
+ rewriteWorkflowRunRequester,
63
+ selectWorkflowRunsRequestedBy,
64
+ type UserReferenceColumn,
65
+ } from "./db/users/references";
66
+ import type { DbExecutor } from "./db-client";
67
+
68
+ /** Rows per transaction. On the incident tables this keeps a batch well under 100 ms. */
69
+ export const USER_MERGE_BATCH_SIZE = 5000;
70
+
71
+ /** Claims of one job before it is marked failed for good. */
72
+ export const USER_MERGE_MAX_ATTEMPTS = 5;
73
+
74
+ export const WORKFLOW_CONTEXT_PHASE = "workflow-context";
75
+ export const DELETE_USER_PHASE = "delete-user";
76
+
77
+ /** An error no retry can fix: the job is marked failed without a backoff. */
78
+ export class TerminalUserMergeError extends Error {}
79
+
80
+ type UserMergeJobRow = {
81
+ id: string;
82
+ sourceUserId: string;
83
+ targetUserId: string;
84
+ status: UserMergeJobStatus;
85
+ phase: string | null;
86
+ progress: string;
87
+ error: string | null;
88
+ attempts: number;
89
+ nextAttemptAt: string | null;
90
+ createdAt: string;
91
+ startedAt: string | null;
92
+ finishedAt: string | null;
93
+ };
94
+
95
+ function rowToJob(row: UserMergeJobRow): UserMergeJob {
96
+ let progress: Record<string, number> = {};
97
+ try {
98
+ const parsed = JSON.parse(row.progress) as unknown;
99
+ if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
100
+ progress = Object.fromEntries(
101
+ Object.entries(parsed as Record<string, unknown>).filter(
102
+ (entry): entry is [string, number] => typeof entry[1] === "number",
103
+ ),
104
+ );
105
+ }
106
+ } catch {
107
+ // A malformed progress blob only loses the per-phase counts.
108
+ }
109
+ return {
110
+ id: row.id,
111
+ sourceUserId: row.sourceUserId,
112
+ targetUserId: row.targetUserId,
113
+ status: row.status,
114
+ phase: row.phase,
115
+ progress,
116
+ error: row.error,
117
+ attempts: row.attempts,
118
+ nextAttemptAt: row.nextAttemptAt,
119
+ createdAt: row.createdAt,
120
+ startedAt: row.startedAt,
121
+ finishedAt: row.finishedAt,
122
+ };
123
+ }
124
+
125
+ // ---------------------------------------------------------------------------
126
+ // Job rows
127
+ // ---------------------------------------------------------------------------
128
+
129
+ /** Insert a pending job. Joins the caller's transaction when there is one. */
130
+ export async function createUserMergeJob(input: {
131
+ sourceUserId: string;
132
+ targetUserId: string;
133
+ createdBy?: string | null;
134
+ }): Promise<UserMergeJob> {
135
+ if (input.sourceUserId === input.targetUserId) {
136
+ throw new Error("Merge source and target must differ");
137
+ }
138
+ const id = crypto.randomUUID();
139
+ const now = new Date().toISOString();
140
+ const row = await getDbClient().get<UserMergeJobRow>(
141
+ `INSERT INTO user_merge_jobs
142
+ (id, sourceUserId, targetUserId, status, progress, createdAt, created_by, updated_by)
143
+ VALUES (?, ?, ?, 'pending', '{}', ?, ?, ?)
144
+ RETURNING *`,
145
+ [
146
+ id,
147
+ input.sourceUserId,
148
+ input.targetUserId,
149
+ now,
150
+ input.createdBy ?? null,
151
+ input.createdBy ?? null,
152
+ ],
153
+ );
154
+ if (!row) throw new Error("Failed to create user merge job");
155
+ return rowToJob(row);
156
+ }
157
+
158
+ export async function getUserMergeJob(id: string): Promise<UserMergeJob | null> {
159
+ const row = await getDbClient().get<UserMergeJobRow>(
160
+ "SELECT * FROM user_merge_jobs WHERE id = ?",
161
+ [id],
162
+ );
163
+ return row ? rowToJob(row) : null;
164
+ }
165
+
166
+ export async function listUserMergeJobs(opts?: {
167
+ status?: UserMergeJobStatus;
168
+ limit?: number;
169
+ }): Promise<UserMergeJob[]> {
170
+ const limit = Math.min(Math.max(opts?.limit ?? 50, 1), 200);
171
+ const rows = opts?.status
172
+ ? await getDbClient().query<UserMergeJobRow>(
173
+ "SELECT * FROM user_merge_jobs WHERE status = ? ORDER BY createdAt DESC LIMIT ?",
174
+ [opts.status, limit],
175
+ )
176
+ : await getDbClient().query<UserMergeJobRow>(
177
+ "SELECT * FROM user_merge_jobs ORDER BY createdAt DESC LIMIT ?",
178
+ [limit],
179
+ );
180
+ return rows.map(rowToJob);
181
+ }
182
+
183
+ /** The pending or running job that names `userId` as source or target, if any. */
184
+ export async function findActiveUserMergeJob(userId: string): Promise<UserMergeJob | null> {
185
+ const row = await getDbClient().get<UserMergeJobRow>(
186
+ `SELECT * FROM user_merge_jobs
187
+ WHERE status IN ('pending', 'running') AND (sourceUserId = ? OR targetUserId = ?)
188
+ ORDER BY createdAt LIMIT 1`,
189
+ [userId, userId],
190
+ );
191
+ return row ? rowToJob(row) : null;
192
+ }
193
+
194
+ /** The newest failed job that merges `sourceUserId` into `targetUserId`, if any. */
195
+ export async function findFailedUserMergeJob(
196
+ sourceUserId: string,
197
+ targetUserId: string,
198
+ ): Promise<UserMergeJob | null> {
199
+ const row = await getDbClient().get<UserMergeJobRow>(
200
+ `SELECT * FROM user_merge_jobs
201
+ WHERE status = 'failed' AND sourceUserId = ? AND targetUserId = ?
202
+ ORDER BY createdAt DESC LIMIT 1`,
203
+ [sourceUserId, targetUserId],
204
+ );
205
+ return row ? rowToJob(row) : null;
206
+ }
207
+
208
+ /**
209
+ * Put a failed job back in the queue with a fresh attempt budget. The caller
210
+ * kicks runUserMergeJobs (after its transaction commits, when it has one).
211
+ */
212
+ export async function requeueUserMergeJob(jobId: string): Promise<UserMergeJob | null> {
213
+ const row = await getDbClient().get<UserMergeJobRow>(
214
+ `UPDATE user_merge_jobs
215
+ SET status = 'pending', error = NULL, attempts = 0, nextAttemptAt = NULL, finishedAt = NULL
216
+ WHERE id = ? AND status = 'failed'
217
+ RETURNING *`,
218
+ [jobId],
219
+ );
220
+ return row ? rowToJob(row) : null;
221
+ }
222
+
223
+ // ---------------------------------------------------------------------------
224
+ // Runner
225
+ // ---------------------------------------------------------------------------
226
+
227
+ export type UserMergeRunnerOptions = {
228
+ /** Rows per transaction. Default USER_MERGE_BATCH_SIZE. */
229
+ batchSize?: number;
230
+ /**
231
+ * Test hook, called after each batch commits. A throw fails the attempt
232
+ * with the batch already committed, which is what a crash mid-job looks like.
233
+ */
234
+ afterBatch?: (info: { jobId: string; phase: string; changes: number }) => void | Promise<void>;
235
+ /** Rest between batches. Default: the lag-following pacer. Tests pass false. */
236
+ pace?: boolean;
237
+ /** Backoff before the next attempt of a failed job. Default: retryDelayMs. */
238
+ retryDelayMs?: (attempt: number) => number;
239
+ };
240
+
241
+ /** Rest ratio bounds, as in boot-scrub-sweep: fastest when idle, slowest when busy. */
242
+ const INITIAL_REST_RATIO = 1;
243
+ const LAG_SMOOTHING = 0.3;
244
+
245
+ /** 2 s, 8 s, 32 s, 2 min, capped at 5 min. */
246
+ export function retryDelayMs(attempt: number): number {
247
+ return Math.min(2_000 * 4 ** Math.max(0, attempt - 1), 5 * 60_000);
248
+ }
249
+
250
+ /**
251
+ * Rest after each batch for `ratio` times the batch's duration, and move the
252
+ * ratio with how late the rest timer fires (event-loop lag).
253
+ */
254
+ function createPacer() {
255
+ let ratio = INITIAL_REST_RATIO;
256
+ let lag = 0;
257
+ return {
258
+ async rest(workedMs: number): Promise<void> {
259
+ const restMs = Math.max(1, workedMs * ratio);
260
+ const restStart = performance.now();
261
+ await new Promise<void>((resolve) => setTimeout(resolve, restMs));
262
+ const late = Math.max(0, performance.now() - restStart - restMs);
263
+ lag = lag * (1 - LAG_SMOOTHING) + late * LAG_SMOOTHING;
264
+ ratio = nextRestRatio(ratio, lag);
265
+ },
266
+ };
267
+ }
268
+
269
+ async function saveProgress(
270
+ tx: DbExecutor,
271
+ jobId: string,
272
+ phase: string,
273
+ progress: Record<string, number>,
274
+ ): Promise<void> {
275
+ await tx.run("UPDATE user_merge_jobs SET phase = ?, progress = ? WHERE id = ?", [
276
+ phase,
277
+ JSON.stringify(progress),
278
+ jobId,
279
+ ]);
280
+ }
281
+
282
+ async function userExists(executor: DbExecutor, id: string): Promise<boolean> {
283
+ const row = await executor.get<{ present: number }>(
284
+ "SELECT EXISTS(SELECT 1 FROM users WHERE id = ?) AS present",
285
+ [id],
286
+ );
287
+ return row?.present === 1;
288
+ }
289
+
290
+ /** Claim the oldest pending job whose backoff has passed, counting the attempt. */
291
+ async function claimNextJob(): Promise<UserMergeJob | null> {
292
+ const now = new Date().toISOString();
293
+ const row = await getDbClient().get<UserMergeJobRow>(
294
+ `UPDATE user_merge_jobs
295
+ SET status = 'running', startedAt = COALESCE(startedAt, ?), attempts = attempts + 1,
296
+ nextAttemptAt = NULL, finishedAt = NULL
297
+ WHERE id = (
298
+ SELECT id FROM user_merge_jobs
299
+ WHERE status = 'pending' AND (nextAttemptAt IS NULL OR nextAttemptAt <= ?)
300
+ ORDER BY createdAt LIMIT 1
301
+ )
302
+ RETURNING *`,
303
+ [now, now],
304
+ );
305
+ return row ? rowToJob(row) : null;
306
+ }
307
+
308
+ function errorMessage(error: unknown): string {
309
+ // Free text from an exception: scrub it before it lands (.text-columns.json).
310
+ const message = scrubSecrets(error instanceof Error ? error.message : String(error));
311
+ return message.slice(0, 2000);
312
+ }
313
+
314
+ async function markFailed(jobId: string, error: unknown): Promise<void> {
315
+ await getDbClient().run(
316
+ "UPDATE user_merge_jobs SET status = 'failed', error = ?, finishedAt = ? WHERE id = ?",
317
+ [errorMessage(error), new Date().toISOString(), jobId],
318
+ );
319
+ }
320
+
321
+ /** Back to pending, with the error kept for the UI and a backoff before the next claim. */
322
+ async function scheduleRetry(jobId: string, error: unknown, delayMs: number): Promise<void> {
323
+ await getDbClient().run(
324
+ "UPDATE user_merge_jobs SET status = 'pending', error = ?, nextAttemptAt = ? WHERE id = ?",
325
+ [errorMessage(error), new Date(Date.now() + delayMs).toISOString(), jobId],
326
+ );
327
+ }
328
+
329
+ /**
330
+ * Phase 3, one short transaction: sweep every column once more (the rows
331
+ * written since that column's pass), rewrite the context of any run the
332
+ * source requested meanwhile, delete the source row, mark the job done.
333
+ */
334
+ async function finishJob(
335
+ job: UserMergeJob,
336
+ progress: Record<string, number>,
337
+ references: UserReferenceColumn[],
338
+ ): Promise<void> {
339
+ const client = getDbClient();
340
+ // Outside the transaction, so a failed delete still reports its phase.
341
+ await saveProgress(client, job.id, DELETE_USER_PHASE, progress);
342
+ await client.transaction(async (tx) => {
343
+ if (references.length > 0) {
344
+ const runIds = await selectWorkflowRunsRequestedBy(job.sourceUserId, tx);
345
+ const contexts = await rewriteWorkflowRunRequester(
346
+ runIds,
347
+ job.sourceUserId,
348
+ job.targetUserId,
349
+ );
350
+ if (contexts > 0) {
351
+ progress[WORKFLOW_CONTEXT_PHASE] = (progress[WORKFLOW_CONTEXT_PHASE] ?? 0) + contexts;
352
+ }
353
+ for (const reference of references) {
354
+ const changes = await rewriteUserReferenceColumn(
355
+ reference,
356
+ job.sourceUserId,
357
+ job.targetUserId,
358
+ );
359
+ if (changes > 0) {
360
+ const phase = `${reference.tableName}.${reference.columnName}`;
361
+ progress[phase] = (progress[phase] ?? 0) + changes;
362
+ }
363
+ }
364
+ }
365
+ const result = await tx.run("DELETE FROM users WHERE id = ?", [job.sourceUserId]);
366
+ progress[DELETE_USER_PHASE] = (progress[DELETE_USER_PHASE] ?? 0) + result.changes;
367
+ await tx.run(
368
+ `UPDATE user_merge_jobs
369
+ SET status = 'done', phase = ?, progress = ?, error = NULL, finishedAt = ?
370
+ WHERE id = ?`,
371
+ [DELETE_USER_PHASE, JSON.stringify(progress), new Date().toISOString(), job.id],
372
+ );
373
+ client.afterCommit(() => {
374
+ invalidateTelemetryUserRole(job.sourceUserId);
375
+ invalidateTelemetryUserRole(job.targetUserId);
376
+ scheduleOrgDomainRecompute();
377
+ });
378
+ });
379
+ }
380
+
381
+ async function runJob(job: UserMergeJob, options: UserMergeRunnerOptions): Promise<void> {
382
+ const client = getDbClient();
383
+ const batchSize = Math.max(1, Math.floor(options.batchSize ?? USER_MERGE_BATCH_SIZE));
384
+ const pacer = options.pace === false ? null : createPacer();
385
+ const progress: Record<string, number> = { ...job.progress };
386
+
387
+ // After every committed batch: the test hook (real batches only), then a
388
+ // rest so queued requests get the event loop.
389
+ const afterBatch = async (phase: string, changes: number, workedMs: number) => {
390
+ if (changes > 0) await options.afterBatch?.({ jobId: job.id, phase, changes });
391
+ if (pacer && changes > 0) await pacer.rest(workedMs);
392
+ else await new Promise<void>((resolve) => setTimeout(resolve, 0));
393
+ };
394
+
395
+ if (!(await userExists(client, job.sourceUserId))) {
396
+ // Deleted by another path (the manage-user tool) or by this job's own
397
+ // delete before the done mark was written: nothing left to move.
398
+ await finishJob(job, progress, []);
399
+ return;
400
+ }
401
+ if (!(await userExists(client, job.targetUserId))) {
402
+ throw new TerminalUserMergeError("Target user not found");
403
+ }
404
+
405
+ // Phase 1: the workflow context requester. Each batch selects the next ids
406
+ // (a rewritten run no longer matches the filter, so the same query walks
407
+ // forward) and rewrites them in one transaction.
408
+ while (true) {
409
+ const started = performance.now();
410
+ const changes = await client.transaction(async (tx) => {
411
+ const runIds = await selectWorkflowRunsRequestedBy(job.sourceUserId, tx, batchSize);
412
+ if (runIds.length === 0) return 0;
413
+ const rewritten = await rewriteWorkflowRunRequester(
414
+ runIds,
415
+ job.sourceUserId,
416
+ job.targetUserId,
417
+ );
418
+ if (rewritten > 0) {
419
+ progress[WORKFLOW_CONTEXT_PHASE] = (progress[WORKFLOW_CONTEXT_PHASE] ?? 0) + rewritten;
420
+ }
421
+ await saveProgress(tx, job.id, WORKFLOW_CONTEXT_PHASE, progress);
422
+ return rewritten;
423
+ });
424
+ await afterBatch(WORKFLOW_CONTEXT_PHASE, changes, performance.now() - started);
425
+ // A short batch means no run names the source any more. A selected run
426
+ // always changes (its requester is the source), so this also ends the
427
+ // loop when the filter and the UPDATE ever disagree.
428
+ if (changes < batchSize) break;
429
+ }
430
+
431
+ // Phase 2: every discovered user FK column, batchSize rows per transaction.
432
+ const references = await discoverUserReferenceColumns();
433
+ for (const reference of references) {
434
+ const phase = `${reference.tableName}.${reference.columnName}`;
435
+ while (true) {
436
+ const started = performance.now();
437
+ const changes = await client.transaction(async (tx) => {
438
+ const rewritten = await rewriteUserReferenceColumn(
439
+ reference,
440
+ job.sourceUserId,
441
+ job.targetUserId,
442
+ batchSize,
443
+ );
444
+ if (rewritten > 0) progress[phase] = (progress[phase] ?? 0) + rewritten;
445
+ await saveProgress(tx, job.id, phase, progress);
446
+ return rewritten;
447
+ });
448
+ await afterBatch(phase, changes, performance.now() - started);
449
+ // A short batch means the column holds no more rows for the source.
450
+ if (changes < batchSize) break;
451
+ }
452
+ }
453
+
454
+ // Phase 3: the tail sweep and the source row itself.
455
+ await finishJob(job, progress, references);
456
+ }
457
+
458
+ const retryTimers = new Set<ReturnType<typeof setTimeout>>();
459
+
460
+ function armRetry(delayMs: number, options: UserMergeRunnerOptions): void {
461
+ const timer = setTimeout(() => {
462
+ retryTimers.delete(timer);
463
+ void runUserMergeJobs(options);
464
+ }, delayMs);
465
+ timer.unref?.();
466
+ retryTimers.add(timer);
467
+ }
468
+
469
+ async function drain(options: UserMergeRunnerOptions): Promise<void> {
470
+ while (true) {
471
+ const job = await claimNextJob();
472
+ if (!job) return;
473
+ const started = performance.now();
474
+ try {
475
+ await runJob(job, options);
476
+ const seconds = ((performance.now() - started) / 1000).toFixed(1);
477
+ console.log(`[user-merge] job ${job.id} done in ${seconds}s`);
478
+ } catch (err) {
479
+ const terminal =
480
+ err instanceof TerminalUserMergeError || job.attempts >= USER_MERGE_MAX_ATTEMPTS;
481
+ try {
482
+ if (terminal) {
483
+ console.error(`[user-merge] job ${job.id} failed (attempt ${job.attempts}):`, err);
484
+ await markFailed(job.id, err);
485
+ } else {
486
+ const delayMs = (options.retryDelayMs ?? retryDelayMs)(job.attempts);
487
+ console.error(
488
+ `[user-merge] job ${job.id} attempt ${job.attempts} failed, retrying in ${Math.round(delayMs / 1000)}s:`,
489
+ err,
490
+ );
491
+ await scheduleRetry(job.id, err, delayMs);
492
+ armRetry(delayMs, options);
493
+ }
494
+ } catch (markErr) {
495
+ console.error(`[user-merge] could not record the failure of job ${job.id}:`, markErr);
496
+ }
497
+ }
498
+ }
499
+ }
500
+
501
+ let active: Promise<void> | null = null;
502
+ let rerun = false;
503
+ let pausedForTests = false;
504
+
505
+ /**
506
+ * Test switch: while paused, runUserMergeJobs returns at once without
507
+ * draining, so a test can observe a pending job. Unpausing does not start a
508
+ * drain; the test calls runUserMergeJobs itself.
509
+ */
510
+ export function pauseUserMergeRunnerForTests(paused: boolean): void {
511
+ pausedForTests = paused;
512
+ }
513
+
514
+ /** Test helper: drop the retry timers armed by failed attempts. */
515
+ export function clearUserMergeRetryTimersForTests(): void {
516
+ for (const timer of retryTimers) clearTimeout(timer);
517
+ retryTimers.clear();
518
+ }
519
+
520
+ /**
521
+ * Drain pending jobs, one at a time, process-wide. A call while a drain is
522
+ * running returns that drain and schedules one more pass after it, so a job
523
+ * queued meanwhile is never left pending.
524
+ */
525
+ export function runUserMergeJobs(options: UserMergeRunnerOptions = {}): Promise<void> {
526
+ if (pausedForTests) return Promise.resolve();
527
+ if (active) {
528
+ rerun = true;
529
+ return active;
530
+ }
531
+ active = (async () => {
532
+ try {
533
+ do {
534
+ rerun = false;
535
+ await drain(options);
536
+ } while (rerun);
537
+ } finally {
538
+ active = null;
539
+ }
540
+ })();
541
+ return active;
542
+ }
543
+
544
+ /**
545
+ * Boot: a job left running by a crash goes back to pending, a pending job
546
+ * waiting out a backoff becomes due now, and the drain starts. Every batch is
547
+ * idempotent, so the job continues from the first row the last committed
548
+ * batch did not reach. Failed jobs stay failed: they already spent their
549
+ * attempts or hit a terminal error, and a repeat POST re-queues them.
550
+ */
551
+ export async function resumeUserMergeJobs(options: UserMergeRunnerOptions = {}): Promise<void> {
552
+ const requeued = await getDbClient().run(
553
+ `UPDATE user_merge_jobs SET status = 'pending', nextAttemptAt = NULL, finishedAt = NULL
554
+ WHERE status = 'running' OR (status = 'pending' AND nextAttemptAt IS NOT NULL)`,
555
+ );
556
+ const pending = await getDbClient().get<{ n: number }>(
557
+ "SELECT COUNT(*) AS n FROM user_merge_jobs WHERE status = 'pending'",
558
+ );
559
+ if (!pending || pending.n === 0) return;
560
+ console.log(
561
+ `[user-merge] resuming ${pending.n} job(s) (${requeued.changes} re-queued after restart)`,
562
+ );
563
+ await runUserMergeJobs(options);
564
+ }