@desplega.ai/agent-swarm 2.8.1 → 2.8.3

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 (318) hide show
  1. package/dist/acp-adapter-xq6s2z6t.js +14 -0
  2. package/dist/{actions-pwjpn0j6.js → actions-ewz7bp4g.js} +20 -19
  3. package/dist/{amp-adapter-cq9jgj6x.js → amp-adapter-f25tqhnf.js} +3 -3
  4. package/dist/{app-cnfffgwg.js → app-mkptw0k7.js} +4 -4
  5. package/dist/{assistant-kpn317y1.js → assistant-c47rfe77.js} +23 -22
  6. package/dist/{boot-args-schema-pbbnhmc3.js → boot-args-schema-4vb0xsat.js} +1 -1
  7. package/dist/{boot-reembed-9m0ysxma.js → boot-reembed-1ghy2sab.js} +1 -1
  8. package/dist/{boot-reembed-majbn53m.js → boot-reembed-htr73hbs.js} +1 -1
  9. package/dist/{boot-scrub-sweep-app8g8ym.js → boot-scrub-sweep-yrytg42z.js} +4 -4
  10. package/dist/{boot-seeds-pbtmvcjm.js → boot-seeds-n2b3dx4e.js} +4 -4
  11. package/dist/{boxd-qbkxy6ap.js → boxd-jmfqgkqa.js} +2 -2
  12. package/dist/{claude-adapter-wn1g3qp5.js → claude-adapter-r33pqnzx.js} +8 -9
  13. package/dist/{claude-managed-adapter-jh6c523j.js → claude-managed-adapter-hddmxjyj.js} +4 -4
  14. package/dist/{claude-managed-setup-pd47nzch.js → claude-managed-setup-hfvry1bd.js} +5 -5
  15. package/dist/{claude-sdk-session-3a2a6589.js → claude-sdk-session-0hamqzc7.js} +10 -11
  16. package/dist/{cli-v1dv1r37.js → cli-0bcgyj5b.js} +74 -3
  17. package/dist/{cli-a3b239vq.js → cli-0vksaxvr.js} +2 -2
  18. package/dist/{cli-mrt48bx7.js → cli-15204js0.js} +1 -1
  19. package/dist/{cli-8aes17e4.js → cli-1m96dc36.js} +3 -3
  20. package/dist/{cli-2r0jcc3k.js → cli-2d0cghfn.js} +1 -1
  21. package/dist/{cli-wk18cm92.js → cli-4ax9mvvg.js} +4 -4
  22. package/dist/{cli-9wngrawf.js → cli-4dpkjc2e.js} +6 -6
  23. package/dist/{cli-x1wa213s.js → cli-4e1hfbpq.js} +5 -5
  24. package/dist/{cli-7349va39.js → cli-500c09j9.js} +921 -431
  25. package/dist/{cli-m4dqmnbp.js → cli-54qr8mcd.js} +2 -2
  26. package/dist/{cli-m88rpzkh.js → cli-5a7szzfh.js} +2 -2
  27. package/dist/{cli-bst1vp7e.js → cli-5y2fvw4z.js} +13 -5
  28. package/dist/{cli-vj75tyaw.js → cli-6yygkr0w.js} +7 -7
  29. package/dist/{cli-7b8sbmag.js → cli-7c5tgqhf.js} +5 -5
  30. package/dist/{cli-2gxeg5gr.js → cli-8476gtwf.js} +1 -1
  31. package/dist/{cli-yncmerje.js → cli-922z7gwm.js} +4 -4
  32. package/dist/{cli-jrzkw01m.js → cli-93t5s4zq.js} +2 -2
  33. package/dist/{cli-sn7rtnj4.js → cli-9zmb7ptc.js} +9 -9
  34. package/dist/{cli-tk7cjj8h.js → cli-am1ptset.js} +3 -3
  35. package/dist/{cli-cyhbbd1z.js → cli-awwptga4.js} +1 -1
  36. package/dist/{cli-89cqnasv.js → cli-bhsfbmk8.js} +19 -7
  37. package/dist/{cli-9vhebtjz.js → cli-bj6qhgte.js} +15 -32
  38. package/dist/{cli-g70vxtjh.js → cli-c0xjnr8z.js} +2 -2
  39. package/dist/{cli-vqptjbr7.js → cli-c3ng0mb4.js} +14 -14
  40. package/dist/{cli-re4raaj0.js → cli-c71n3bxq.js} +11 -6
  41. package/dist/{cli-7p1kvkck.js → cli-crpf163n.js} +25 -18
  42. package/dist/{cli-k0wwfqbg.js → cli-dns0y538.js} +4 -0
  43. package/dist/{cli-d3p5vwbk.js → cli-dpsfwh7n.js} +13 -21
  44. package/dist/{cli-xzm9vcfg.js → cli-dtdht77h.js} +2 -2
  45. package/dist/{cli-bb431xey.js → cli-e2w34h6z.js} +231 -68
  46. package/dist/{cli-eddnyg3t.js → cli-emekyyhr.js} +1 -1
  47. package/dist/{cli-d84byzn8.js → cli-f1kwb5fc.js} +109 -33
  48. package/dist/{cli-0jzra9ad.js → cli-gn9y6fn7.js} +2 -2
  49. package/dist/{cli-tqegb8eq.js → cli-gpz1d0t2.js} +4 -4
  50. package/dist/{cli-7w69ne2f.js → cli-h9yy18gy.js} +239 -67
  51. package/dist/{cli-m5akxq8q.js → cli-j8cjdzan.js} +1 -1
  52. package/dist/{cli-m9s2qff2.js → cli-jg5txpm6.js} +187 -93
  53. package/dist/{cli-k26bkr9t.js → cli-k2rw11py.js} +3 -3
  54. package/dist/{cli-93k32c37.js → cli-k96cecs7.js} +377 -61
  55. package/dist/{cli-3vjhs18n.js → cli-kd56bmk4.js} +1 -1
  56. package/dist/{cli-k0615eh8.js → cli-kmemts3w.js} +13 -13
  57. package/dist/{cli-pcad4cb1.js → cli-knva3nmx.js} +2 -2
  58. package/dist/{cli-b085njxz.js → cli-kw44j53p.js} +15 -14
  59. package/dist/{cli-bsywb58a.js → cli-mdpbjnzq.js} +10 -10
  60. package/dist/{cli-syybb0p1.js → cli-me0r8gzr.js} +35 -76
  61. package/dist/{cli-4r05hnqa.js → cli-mwwmka77.js} +15 -15
  62. package/dist/{cli-1xh9a7hn.js → cli-n5t077me.js} +2 -2
  63. package/dist/{cli-rvhdmjvt.js → cli-nx2n3z6p.js} +1 -1
  64. package/dist/{cli-28fq781p.js → cli-pe4d96rb.js} +1 -1
  65. package/dist/{cli-3rkdne1h.js → cli-pxwtcgcp.js} +2 -2
  66. package/dist/{cli-zcsw143p.js → cli-qmqt520k.js} +2 -2
  67. package/dist/{cli-2zp0et3v.js → cli-qps8je6j.js} +70 -19
  68. package/dist/{cli-xae4g51r.js → cli-rh15cakt.js} +1 -1
  69. package/dist/{cli-k93bq1wq.js → cli-rjvsf9qa.js} +5 -5
  70. package/dist/{cli-6mma83xa.js → cli-rqfvkx5t.js} +7 -7
  71. package/dist/{cli-dy32zecz.js → cli-s7zz734a.js} +1 -1
  72. package/dist/{cli-yn2bbre9.js → cli-s8p8hhty.js} +2 -2
  73. package/dist/{cli-vbh10fqa.js → cli-sdvcrq5x.js} +5 -5
  74. package/dist/{cli-b5rs97a6.js → cli-sg27rk2t.js} +5 -5
  75. package/dist/{cli-d6521kb5.js → cli-skmhsnqh.js} +1 -1
  76. package/dist/{cli-5mrq3ake.js → cli-t4dv07kr.js} +1 -1
  77. package/dist/{cli-hxpyc9sn.js → cli-t5ys4ebm.js} +3 -3
  78. package/dist/{cli-qmqk2yc2.js → cli-tas6ckky.js} +2 -2
  79. package/dist/{cli-p1w18yhx.js → cli-tf79ryyy.js} +1 -1
  80. package/dist/{cli-kc7v1nfv.js → cli-tnzebztn.js} +1 -1
  81. package/dist/{cli-gax7fk3t.js → cli-vx421edw.js} +3 -3
  82. package/dist/{cli-kchjb7qy.js → cli-x3ex5bqf.js} +1 -1
  83. package/dist/cli-yss07azk.js +142 -0
  84. package/dist/{cli-0ntbxyaa.js → cli-z8cvws4c.js} +363 -47
  85. package/dist/{cli-tacge1w0.js → cli-zdaypkq9.js} +146 -52
  86. package/dist/{cli-b8e9nb04.js → cli-zeaehgyd.js} +2 -2
  87. package/dist/{cli-jcpr5smb.js → cli-ztq5z4e1.js} +4 -4
  88. package/dist/cli.js +21 -21
  89. package/dist/{codex-adapter-nha71q3n.js → codex-adapter-2brxx5ta.js} +7 -8
  90. package/dist/{codex-hook-h3vk6xzr.js → codex-hook-c930c3ea.js} +2 -2
  91. package/dist/{codex-login-bcda78ge.js → codex-login-cbrs1h5w.js} +1 -1
  92. package/dist/{codex-session-runner-ak9x1k1h.js → codex-session-runner-y9anfj0r.js} +8 -9
  93. package/dist/{commands-f587qpq8.js → commands-wn9q06qr.js} +3 -3
  94. package/dist/{controller-hxx8wwy1.js → controller-fw608czd.js} +4 -4
  95. package/dist/{controllers-connect-k5sg9m9h.js → controllers-connect-63j5rdcm.js} +20 -20
  96. package/dist/{cursor-adapter-n5nc3mfe.js → cursor-adapter-6mb9jme8.js} +2 -2
  97. package/dist/{db-nzf8zb5z.js → db-0m6v5tq4.js} +3 -3
  98. package/dist/{db-amm8w15a.js → db-y4wfrzbm.js} +4 -2
  99. package/dist/{devin-adapter-xedz98zv.js → devin-adapter-r0yx3gv8.js} +1 -1
  100. package/dist/{dispatcher-89zrts8f.js → dispatcher-9tq2zzdj.js} +17 -17
  101. package/dist/{docker-866qpzt9.js → docker-j75atvpt.js} +2 -2
  102. package/dist/{dsh-adapter-nrhbrjz8.js → dsh-adapter-t09sv4tq.js} +2 -2
  103. package/dist/{e2b-s16txnpx.js → e2b-7s9dscap.js} +3 -3
  104. package/dist/{e2b-jkwfhbcv.js → e2b-a7vzs2vj.js} +3 -3
  105. package/dist/{easter-egg-3d-533knw22.js → easter-egg-3d-fbwvmwhe.js} +1 -1
  106. package/dist/{embedding-space-ezwenjmj.js → embedding-space-wh8de1t9.js} +6 -6
  107. package/dist/{embeddings-np74ht6g.js → embeddings-3hpbdtjr.js} +1 -1
  108. package/dist/{event-bus-e66gh7cc.js → event-bus-tr6h3gv8.js} +1 -1
  109. package/dist/{execute-b3mrb1mv.js → execute-4bdf8sdy.js} +1 -1
  110. package/dist/{external-channel-z46yx9d7.js → external-channel-ts2fwqh0.js} +2 -2
  111. package/dist/fx-adapter-1rnxnf56.js +17 -0
  112. package/dist/grok-adapter-efhmf1r8.js +17 -0
  113. package/dist/handlers-anrr3py9.js +32 -0
  114. package/dist/{heartbeat-dt9qw9c8.js → heartbeat-2ze5fzpc.js} +20 -20
  115. package/dist/{hook-8k6fbz9b.js → hook-ca54yd4s.js} +5 -5
  116. package/dist/{hook-jgcgygex.js → hook-gaehe2w5.js} +8 -8
  117. package/dist/{http-haw4r8ne.js → http-jfg8t2yk.js} +3839 -122
  118. package/dist/{index-tvfafeyy.js → index-3gv6sc4z.js} +1 -1
  119. package/dist/{index-tez78qj3.js → index-3yvzbhxd.js} +100 -23
  120. package/dist/index-8w2aznds.js +33 -0
  121. package/dist/{index-5gk12my8.js → index-csgrxedz.js} +1 -1
  122. package/dist/index-qnjvr362.js +37 -0
  123. package/dist/index-yzxd3sn9.js +32 -0
  124. package/dist/{keepalive-xn6s7rmg.js → keepalive-k2a3qe73.js} +4 -4
  125. package/dist/lead-zhxqbfdv.js +69 -0
  126. package/dist/{maintenance-vdts800b.js → maintenance-gb5sd5nc.js} +1 -1
  127. package/dist/{node-4c5jqxr3.js → node-7qsx5kvm.js} +1 -1
  128. package/dist/{oauth-refresh-sweep-094c8wfk.js → oauth-refresh-sweep-y7rm6bkb.js} +3 -3
  129. package/dist/{onboard-v99eh56m.js → onboard-jmc5qerv.js} +4 -4
  130. package/dist/{opencode-adapter-dsagkhtw.js → opencode-adapter-7eade9ev.js} +6 -7
  131. package/dist/{otel-impl-q7yr9yp0.js → otel-impl-8j1xhxqd.js} +2 -2
  132. package/dist/{pi-mono-adapter-546cd1cc.js → pi-mono-adapter-yqmaf32x.js} +14 -13
  133. package/dist/{pricing-refresh-7dh2yase.js → pricing-refresh-1nj722h6.js} +2 -2
  134. package/dist/{rbac-roles-mdmc96p4.js → rbac-roles-b1mtr6dr.js} +3 -3
  135. package/dist/{rbac-roles-npbmdh77.js → rbac-roles-n1f3frk1.js} +2 -2
  136. package/dist/{render-v2-twhhwmms.js → render-v2-1gfpd8rj.js} +4 -4
  137. package/dist/{resume-5gsrjd5e.js → resume-w1s1eh5v.js} +7 -7
  138. package/dist/{runtime-1hv06jsn.js → runtime-84g5egsc.js} +1 -1
  139. package/dist/{session-access-m191x30x.js → session-access-y6z1txk4.js} +4 -4
  140. package/dist/{session-source-backfill-nt923ask.js → session-source-backfill-gxxwfh12.js} +3 -3
  141. package/dist/{setup-wv467afy.js → setup-r91gstyk.js} +1 -1
  142. package/dist/{virtual-modules-gazyapte.js → virtual-modules-cgnj6kme.js} +3 -3
  143. package/dist/{work-objects-8zkrpy13.js → work-objects-qcxzg61c.js} +20 -20
  144. package/dist/worker-7j2907g6.js +69 -0
  145. package/dist/{worker-follow-up-cecqszem.js → worker-follow-up-y2d2h7km.js} +19 -17
  146. package/dist/{x-2fyrt5pp.js → x-qhxq2ama.js} +2 -2
  147. package/openapi.json +2908 -351
  148. package/package.json +4 -4
  149. package/src/apps/catalog.generated.json +120 -1
  150. package/src/apps/definition.ts +38 -11
  151. package/src/apps/page-validator.ts +88 -0
  152. package/src/be/db/model-profiles.ts +110 -11
  153. package/src/be/db/tasks/read.ts +53 -0
  154. package/src/be/db/workflow-run-definitions.ts +19 -8
  155. package/src/be/db-client.ts +43 -5
  156. package/src/be/db.ts +40 -9
  157. package/src/be/migrations/284_model_profile_fallbacks.sql +9 -0
  158. package/src/be/migrations/289_automation_savings_daily.sql +47 -0
  159. package/src/be/migrations/291_task_slack_participants.sql +4 -0
  160. package/src/be/migrations/293_savings_component_links.sql +58 -0
  161. package/src/be/migrations/296_automation_savings_daily_read_index.sql +6 -0
  162. package/src/be/model-profile-check.ts +53 -20
  163. package/src/be/runtime-pools/routing.ts +1 -0
  164. package/src/be/savings/calibrate.ts +468 -0
  165. package/src/be/savings/compute.ts +677 -0
  166. package/src/be/savings/model.ts +674 -0
  167. package/src/be/savings/read.ts +1500 -0
  168. package/src/be/savings/rollup.ts +571 -0
  169. package/src/be/scripts/run-credential.ts +15 -3
  170. package/src/be/scripts/typecheck.ts +91 -4
  171. package/src/be/seed/agent-fs-provision.ts +48 -11
  172. package/src/be/seed/workflows-seeder.ts +67 -23
  173. package/src/be/seed-scripts/catalog/dreaming-learnings.ts +10 -2
  174. package/src/be/seed-scripts/catalog/dreaming-memtax-create.ts +70 -4
  175. package/src/be/skill-parser.ts +19 -7
  176. package/src/be/swarm-config-guard.ts +8 -0
  177. package/src/be/users.ts +29 -2
  178. package/src/commands/onboard/agent-fs-version.ts +1 -1
  179. package/src/commands/provider-error-class.ts +97 -0
  180. package/src/commands/runner.ts +258 -58
  181. package/src/extensions/contract-types.generated.ts +1 -1
  182. package/src/http/all-routes.ts +2 -0
  183. package/src/http/fs.ts +8 -2
  184. package/src/http/index.ts +11 -0
  185. package/src/http/model-profiles.ts +43 -10
  186. package/src/http/poll.ts +15 -1
  187. package/src/http/requested-by.ts +97 -1
  188. package/src/http/savings-calibration.ts +277 -0
  189. package/src/http/savings.ts +849 -0
  190. package/src/http/scripts.ts +26 -15
  191. package/src/http/tasks.ts +89 -9
  192. package/src/http/trackers/authorize-gate.ts +41 -0
  193. package/src/http/trackers/jira.ts +26 -4
  194. package/src/http/trackers/linear.ts +26 -4
  195. package/src/integrations/chat-sdk/limits.ts +6 -1
  196. package/src/jira/oauth.ts +7 -1
  197. package/src/linear/README.md +2 -1
  198. package/src/linear/oauth.ts +7 -1
  199. package/src/prompts/session-templates.ts +80 -3
  200. package/src/providers/acp-targets.ts +1 -1
  201. package/src/providers/amp-adapter.ts +1 -1
  202. package/src/providers/claude-adapter.ts +0 -20
  203. package/src/providers/claude-flag-settings.ts +12 -10
  204. package/src/providers/codex-adapter.ts +12 -34
  205. package/src/providers/opencode-adapter.ts +34 -12
  206. package/src/providers/pi-mono-adapter.ts +6 -5
  207. package/src/providers/types.ts +7 -0
  208. package/src/scheduler/scheduler.ts +20 -14
  209. package/src/scripts-runtime/ctx.ts +1 -1
  210. package/src/scripts-runtime/executors/quickjs-bundle.ts +1 -0
  211. package/src/scripts-runtime/executors/quickjs-prelude.ts +162 -2
  212. package/src/scripts-runtime/executors/quickjs-runner.ts +4 -1
  213. package/src/scripts-runtime/sdk-allowlist.ts +11 -1
  214. package/src/scripts-runtime/stdlib/fetch.ts +113 -15
  215. package/src/scripts-runtime/stdlib/index.ts +3 -1
  216. package/src/scripts-runtime/stdlib/retry.ts +174 -0
  217. package/src/scripts-runtime/swarm-sdk.ts +192 -17
  218. package/src/scripts-runtime/types/stdlib.d.ts +95 -4
  219. package/src/scripts-runtime/types/swarm-extension.d.ts +60 -0
  220. package/src/scripts-runtime/types/swarm-sdk.d.ts +69 -2
  221. package/src/slack/actions.ts +1 -0
  222. package/src/slack/assistant.ts +2 -0
  223. package/src/slack/enrich.ts +144 -50
  224. package/src/slack/handlers.ts +37 -95
  225. package/src/slack/templates.ts +9 -0
  226. package/src/slack/thread-buffer.ts +45 -71
  227. package/src/slack/thread-participants.ts +187 -0
  228. package/src/tasks/worker-follow-up.ts +142 -3
  229. package/src/tests/agent-fs-provision-seeder.test.ts +81 -0
  230. package/src/tests/apps-chart-shell.test.ts +170 -0
  231. package/src/tests/apps-icon.test.ts +156 -2
  232. package/src/tests/base-prompt.test.ts +5 -4
  233. package/src/tests/chat-sdk-relay.test.ts +12 -0
  234. package/src/tests/claude-adapter-binary.test.ts +7 -1
  235. package/src/tests/claude-adapter.test.ts +10 -64
  236. package/src/tests/claude-flag-settings.test.ts +15 -11
  237. package/src/tests/claude-hook-credentials.test.ts +0 -1
  238. package/src/tests/claude-sdk-lifecycle.test.ts +5 -1
  239. package/src/tests/codex-adapter.test.ts +28 -78
  240. package/src/tests/comb-default.test.ts +6 -0
  241. package/src/tests/core-auth.test.ts +7 -6
  242. package/src/tests/create-task-vcs-repo.test.ts +136 -0
  243. package/src/tests/db-client-busy-retry.test.ts +58 -6
  244. package/src/tests/db-task-read-contract.test.ts +2 -0
  245. package/src/tests/db-task-write-contract.test.ts +1 -0
  246. package/src/tests/deploy-guard.test.ts +154 -0
  247. package/src/tests/docs-index-footprint.test.js +3 -3
  248. package/src/tests/dreaming-learnings.test.ts +104 -0
  249. package/src/tests/extensions-pre-task.test.ts +4 -2
  250. package/src/tests/fetch-resolved-env.test.ts +47 -1
  251. package/src/tests/fixtures/app-shell-charts-definition.json.txt +180 -0
  252. package/src/tests/model-profile-fallbacks.test.ts +482 -0
  253. package/src/tests/model-profiles-api.test.ts +1 -0
  254. package/src/tests/opencode-adapter.test.ts +53 -6
  255. package/src/tests/pi-mono-session-cost-on-abort.test.ts +115 -0
  256. package/src/tests/prompt-template-session.test.ts +17 -1
  257. package/src/tests/provider-error-class.test.ts +174 -0
  258. package/src/tests/runner-provider-fallback.test.ts +197 -0
  259. package/src/tests/runner-requester-profile.test.ts +139 -1
  260. package/src/tests/savings-calibration-api.test.ts +264 -0
  261. package/src/tests/savings-calibration.test.ts +201 -0
  262. package/src/tests/savings-compound.test.ts +774 -0
  263. package/src/tests/savings-compute.test.ts +215 -0
  264. package/src/tests/savings-fleet-latency.test.ts +143 -0
  265. package/src/tests/savings-fleet-read.test.ts +195 -0
  266. package/src/tests/savings-fleet-scale.test.ts +222 -0
  267. package/src/tests/savings-model.test.ts +499 -0
  268. package/src/tests/savings-read.test.ts +367 -0
  269. package/src/tests/savings-rollup.test.ts +766 -0
  270. package/src/tests/savings-scripts.test.ts +611 -0
  271. package/src/tests/script-connections.test.ts +4 -0
  272. package/src/tests/script-executor-conformance.test.ts +444 -0
  273. package/src/tests/scripts-http.test.ts +55 -0
  274. package/src/tests/scripts-runtime-fetch.test.ts +2 -2
  275. package/src/tests/sdk-allowlist.test.ts +40 -0
  276. package/src/tests/secret-rules.test.ts +124 -1
  277. package/src/tests/secret-scrubber.test.ts +1 -1
  278. package/src/tests/seed-workflows.test.ts +167 -11
  279. package/src/tests/skill-parser.test.ts +45 -0
  280. package/src/tests/slack-dm-implicit-mention.test.ts +20 -1
  281. package/src/tests/slack-identity-resolution.test.ts +23 -0
  282. package/src/tests/slack-inbound-files.test.ts +2 -1
  283. package/src/tests/slack-thread-participants.test.ts +429 -0
  284. package/src/tests/tool-annotations.test.ts +30 -0
  285. package/src/tests/tracker-oauth-authorize-auth.test.ts +262 -0
  286. package/src/tests/workflow-definition-pinning.test.ts +128 -3
  287. package/src/tests/workflow-script-principal.test.ts +94 -4
  288. package/src/tools/app-upsert.ts +1 -1
  289. package/src/tools/model-profiles/list-model-profiles.ts +4 -2
  290. package/src/tools/update-profile.ts +1 -1
  291. package/src/types.ts +146 -33
  292. package/src/utils/constants.ts +2 -0
  293. package/src/utils/mcp-server-fetcher.test.ts +89 -0
  294. package/src/utils/mcp-server-fetcher.ts +13 -5
  295. package/src/utils/request-auth-context.ts +24 -0
  296. package/src/utils/secret-rules.generated.ts +1 -1
  297. package/src/utils/secret-scrubber.ts +154 -29
  298. package/src/workflows/executors/swarm-script.ts +23 -19
  299. package/templates/skills/apps/SKILL.md +62 -3
  300. package/templates/skills/apps/content.md +62 -3
  301. package/templates/skills/swarm-docs/content.md +2 -0
  302. package/templates/skills/swarm-scripts/SKILL.md +5 -4
  303. package/templates/skills/swarm-scripts/content.md +5 -4
  304. package/templates/skills/work-on-task/SKILL.md +1 -1
  305. package/templates/skills/work-on-task/content.md +1 -1
  306. package/templates/workflows/dreaming/content.md +1 -1
  307. package/dist/acp-adapter-pdy3qsvg.js +0 -15
  308. package/dist/cli-4bd1rck6.js +0 -4
  309. package/dist/cli-jywyhqws.js +0 -104
  310. package/dist/fx-adapter-3q12snn6.js +0 -18
  311. package/dist/grok-adapter-ahb1w033.js +0 -18
  312. package/dist/handlers-vbww69re.js +0 -32
  313. package/dist/index-4xwaf5th.js +0 -32
  314. package/dist/index-f0hcrs8e.js +0 -33
  315. package/dist/index-s4ewdma4.js +0 -37
  316. package/dist/lead-6ygbfhbh.js +0 -70
  317. package/dist/worker-mz5z7kj0.js +0 -70
  318. package/src/providers/ctx-mode-env.ts +0 -10
package/src/types.ts CHANGED
@@ -488,11 +488,48 @@ export const ModelProfileSchema = z.object({
488
488
  seededFrom: z.string().nullable(),
489
489
  /** Pooled-key allowlist (column `credentialAllowlist`); null uses the whole pool. */
490
490
  credentials: ModelProfileCredentialsSchema.nullable(),
491
+ /**
492
+ * Profiles on the same harness to hop to, in order, when a provider error
493
+ * (rate limit, seat, overload, auth, empty pool) ends a task running on
494
+ * this one. Empty: the task fails as before.
495
+ */
496
+ fallbacks: z.array(z.string()),
491
497
  createdAt: z.string(),
492
498
  lastUpdatedAt: z.string(),
493
499
  });
494
500
  export type ModelProfile = z.infer<typeof ModelProfileSchema>;
495
501
 
502
+ /**
503
+ * Why a provider session ended in a way another profile may survive. Set by
504
+ * the worker's classifier (`src/commands/provider-error-class.ts`); never a
505
+ * task failure, tool error, context overflow, cancellation or budget refusal.
506
+ */
507
+ export const ProviderErrorClassSchema = z.enum([
508
+ "rate_limit",
509
+ "model_window",
510
+ "seat",
511
+ "credits",
512
+ "overloaded",
513
+ "server_error",
514
+ "auth",
515
+ "pool_empty",
516
+ ]);
517
+ export type ProviderErrorClass = z.infer<typeof ProviderErrorClassSchema>;
518
+
519
+ /**
520
+ * One same-agent fallback hop: the task left `from` on `errorClass` and its
521
+ * resume runs `profile`. `skipped` lists the fallbacks passed over because
522
+ * they failed the profile check on the agent.
523
+ */
524
+ export const FallbackHopSchema = z.object({
525
+ from: z.string(),
526
+ profile: z.string(),
527
+ errorClass: ProviderErrorClassSchema,
528
+ at: z.string(),
529
+ skipped: z.array(z.object({ profile: z.string(), reason: z.string() })).optional(),
530
+ });
531
+ export type FallbackHop = z.infer<typeof FallbackHopSchema>;
532
+
496
533
  /** One check's verdict: only `fail` blocks; `unverified` means nothing to judge yet. */
497
534
  export const ProfileCheckStateSchema = z.enum(["pass", "warn", "fail", "unverified"]);
498
535
  export type ProfileCheckState = z.infer<typeof ProfileCheckStateSchema>;
@@ -714,6 +751,34 @@ export const RoutingAffinitySchema = z
714
751
  .openapi("RoutingAffinity");
715
752
  export type RoutingAffinity = z.infer<typeof RoutingAffinitySchema>;
716
753
 
754
+ // People in a multi-person Slack thread, captured when the task is created.
755
+ // `trust`: internal (company domain or audience override), external (other
756
+ // domain, other Slack team), unknown (no email). External and unknown are
757
+ // untrusted: their messages are information, never instructions.
758
+ export const SlackParticipantTrustSchema = z.enum(["internal", "external", "unknown"]);
759
+ export type SlackParticipantTrust = z.infer<typeof SlackParticipantTrustSchema>;
760
+
761
+ export const TaskSlackParticipantSchema = z.object({
762
+ slackUserId: z.string(),
763
+ userId: z.string().optional(),
764
+ trust: SlackParticipantTrustSchema,
765
+ /** Sent the first task in this thread (who started the swarm's work). */
766
+ starter: z.boolean().optional(),
767
+ /** Sent the message that triggered this task. */
768
+ newest: z.boolean().optional(),
769
+ /** Sent a message in the batch that triggered this task. */
770
+ asked: z.boolean().optional(),
771
+ });
772
+ export type TaskSlackParticipant = z.infer<typeof TaskSlackParticipantSchema>;
773
+
774
+ export const TaskSlackParticipantsSchema = z.object({
775
+ /** At most the cap, primary senders first. */
776
+ people: z.array(TaskSlackParticipantSchema),
777
+ /** Humans past the cap, rendered as `<N more...>`. */
778
+ more: z.number().int().min(0),
779
+ });
780
+ export type TaskSlackParticipants = z.infer<typeof TaskSlackParticipantsSchema>;
781
+
717
782
  export const AgentTaskSchema = z
718
783
  .object({
719
784
  id: z.uuid(),
@@ -768,6 +833,7 @@ export const AgentTaskSchema = z
768
833
  slackThreadTs: z.string().optional(),
769
834
  slackTriggerMessageTs: z.string().optional(),
770
835
  slackUserId: z.string().optional(),
836
+ slackParticipants: TaskSlackParticipantsSchema.optional(),
771
837
  slackReplySent: z.boolean().default(false),
772
838
  slackProgressMessageTs: z.string().optional(),
773
839
  slackTreeRootMessageTs: z.string().optional(),
@@ -830,6 +896,9 @@ export const AgentTaskSchema = z
830
896
  // the agent's default). Unset when no profile applied.
831
897
  profile: z.string().optional(),
832
898
  resolvedProfile: z.string().optional(),
899
+ // Provider-error fallback hops so far, oldest first; carried by every
900
+ // resume of the same work. Unset when the work never hopped.
901
+ fallbackHops: z.array(FallbackHopSchema).optional(),
833
902
 
834
903
  // Schedule linking (optional — set when task was created by a schedule).
835
904
  // z.string(): also reachable verbatim from the create-task query param.
@@ -932,6 +1001,8 @@ export const CreateTaskOptionsSchema = z.object({
932
1001
  /** Exact Slack message that directly triggered this task; never inherited. */
933
1002
  slackTriggerMessageTs: z.string().optional(),
934
1003
  slackUserId: z.string().optional(),
1004
+ /** People in the Slack thread; inherited by children like `slackUserId`. */
1005
+ slackParticipants: TaskSlackParticipantsSchema.optional(),
935
1006
  /**
936
1007
  * Opt out of the residual Slack/contextKey normalization (see the
937
1008
  * "Residual-mismatch guard" comment near the INSERT in src/be/db.ts): a
@@ -962,6 +1033,8 @@ export const CreateTaskOptionsSchema = z.object({
962
1033
  modelTier: ModelTierSchema.optional(),
963
1034
  effort: ReasoningEffortSchema.optional(),
964
1035
  profile: z.string().optional(),
1036
+ /** Internal: the fallback hops a resume carries over (`createResumeFollowUp`). */
1037
+ fallbackHops: z.array(FallbackHopSchema).optional(),
965
1038
  scheduleId: z.string().optional(),
966
1039
  workflowRunId: z.string().optional(),
967
1040
  workflowRunStepId: z.string().optional(),
@@ -1309,32 +1382,80 @@ export type TaskTemplate = z.infer<typeof TaskTemplateSchema>;
1309
1382
 
1310
1383
  export const AgentStatusSchema = z.enum(["idle", "busy", "offline", "waiting_for_credentials"]);
1311
1384
 
1312
- // Discriminated union so future avatar types (emoji, image, ...) can be added
1313
- // with zero migrations — just a new zod branch + a UI renderer branch. Server
1314
- // validates shape only; the UI owns the curated icon catalog and deterministic
1315
- // fallback, so an unrecognized/hand-set icon name can never break rendering.
1316
- export const AgentAvatarSchema = z.discriminatedUnion("type", [
1317
- z.object({
1318
- type: z.literal("lucide"),
1319
- icon: z
1320
- .string()
1321
- .regex(/^[a-z0-9-]+$/)
1322
- .max(64),
1323
- color: z
1385
+ // An https image link, shared by Slack icons and media icons so the rules
1386
+ // can't drift: https only, at most 2048 characters, no embedded credentials.
1387
+ export const HttpsImageUrlSchema = z
1388
+ .url({ protocol: /^https$/, error: "Must be an https:// URL" })
1389
+ .max(2048)
1390
+ .refine((value) => {
1391
+ try {
1392
+ const parsed = new URL(value);
1393
+ return !parsed.username && !parsed.password;
1394
+ } catch {
1395
+ return false;
1396
+ }
1397
+ }, "URL must be valid and contain no credentials");
1398
+
1399
+ // Server validates shape only; the UI owns the curated icon catalog and
1400
+ // deterministic fallback, so an unrecognized/hand-set icon name can never
1401
+ // break rendering.
1402
+ export const LucideIconSchema = z.object({
1403
+ type: z.literal("lucide"),
1404
+ icon: z
1405
+ .string()
1406
+ .regex(/^[a-z0-9-]+$/)
1407
+ .max(64),
1408
+ color: z
1409
+ .string()
1410
+ .regex(/^#[0-9a-fA-F]{6}$/)
1411
+ .optional(),
1412
+ });
1413
+
1414
+ export const UrlIconSchema = z.object({
1415
+ type: z.literal("url"),
1416
+ url: HttpsImageUrlSchema,
1417
+ });
1418
+
1419
+ // Same addressing as the agent-fs task attachment ref: an absolute path, plus
1420
+ // both `orgId` and `driveId` for a specific drive, or neither for the swarm
1421
+ // drive. A single ID is never mixed with the swarm defaults.
1422
+ export const AgentFsIconSchema = z
1423
+ .object({
1424
+ type: z.literal("agent-fs"),
1425
+ path: z
1324
1426
  .string()
1325
- .regex(/^#[0-9a-fA-F]{6}$/)
1326
- .optional(),
1327
- }),
1427
+ .min(2)
1428
+ .max(1024)
1429
+ .refine((value) => value.startsWith("/"), "Path must be absolute (start with /)")
1430
+ .refine((value) => !value.split("/").includes(".."), 'Path must not contain ".." segments'),
1431
+ orgId: z.string().min(1).optional(),
1432
+ driveId: z.string().min(1).optional(),
1433
+ })
1434
+ .refine(
1435
+ (value) => (value.orgId === undefined) === (value.driveId === undefined),
1436
+ "Pass both orgId and driveId, or neither for the swarm drive",
1437
+ );
1438
+
1439
+ // Discriminated union so future icon types can be added with zero migrations:
1440
+ // a new zod branch plus a UI renderer branch. Images that can't load fall back
1441
+ // to a lucide glyph in the dashboard.
1442
+ export const MediaIconSchema = z.discriminatedUnion("type", [
1443
+ LucideIconSchema,
1444
+ UrlIconSchema,
1445
+ AgentFsIconSchema,
1328
1446
  ]);
1329
- export type AgentAvatar = z.infer<typeof AgentAvatarSchema>;
1447
+ export type MediaIcon = z.infer<typeof MediaIconSchema>;
1448
+
1449
+ // Agent avatars accept the same shapes as app icons.
1450
+ export const AgentAvatarSchema = MediaIconSchema;
1451
+ export type AgentAvatar = MediaIcon;
1330
1452
 
1331
1453
  /**
1332
- * Apps reuse the same avatar shape as agents (lucide icon + optional hex
1333
- * color) for their dashboard tile icon. A distinct export name keeps app call
1334
- * sites self-documenting.
1454
+ * Icon shown for an app on the dashboard Apps page: a lucide glyph, an https
1455
+ * image link, or an image in agent-fs.
1335
1456
  */
1336
- export const AppIconSchema = AgentAvatarSchema;
1337
- export type AppIcon = AgentAvatar;
1457
+ export const AppIconSchema = MediaIconSchema;
1458
+ export type AppIcon = MediaIcon;
1338
1459
 
1339
1460
  // Separate from `avatar` so an agent can keep its lucide disc and still post
1340
1461
  // to Slack with its own emoji or image.
@@ -1349,17 +1470,7 @@ export const AgentSlackIconSchema = z
1349
1470
  }),
1350
1471
  z.object({
1351
1472
  type: z.literal("url"),
1352
- url: z
1353
- .url({ protocol: /^https$/ })
1354
- .max(2048)
1355
- .refine((value) => {
1356
- try {
1357
- const parsed = new URL(value);
1358
- return !parsed.username && !parsed.password;
1359
- } catch {
1360
- return false;
1361
- }
1362
- }, "URL must be valid and contain no credentials"),
1473
+ url: HttpsImageUrlSchema,
1363
1474
  }),
1364
1475
  // No icon override: Slack shows the app's own avatar.
1365
1476
  z.object({ type: z.literal("app") }),
@@ -1432,7 +1543,8 @@ export const AgentSchema = z
1432
1543
  .nullable()
1433
1544
  .optional(),
1434
1545
 
1435
- // Custom avatar (icon + color). Null/missing = fall back to the
1546
+ // Custom avatar: a lucide glyph, an https image link, or an agent-fs image.
1547
+ // Null/missing = fall back to the
1436
1548
  // deterministic hash-derived icon and color (see apps/ui/src/lib/agent-icon.ts
1437
1549
  // and agent-color.ts). Migration 119 adds the column.
1438
1550
  avatar: z
@@ -1787,6 +1899,7 @@ export const ResumeReasonSchema = z.enum([
1787
1899
  "context_limits", // Provider session approaching context-window limits (Phase 6)
1788
1900
  "manual_supersede", // Operator-triggered (e.g. dashboard button)
1789
1901
  "crash_recovery", // Heartbeat sweep detected dead/stalled worker (DES-523)
1902
+ "provider_fallback", // Provider error; the resume runs the next fallback profile
1790
1903
  ]);
1791
1904
  export type ResumeReason = z.infer<typeof ResumeReasonSchema>;
1792
1905
 
@@ -273,6 +273,7 @@ export function isBrowserReachableUrl(value: string): boolean {
273
273
  // IPv4-mapped `::ffff:a.b.c.d`: judge the embedded IPv4 address.
274
274
  if (parsed.slice(0, 5).every((x) => x === 0) && g(5) === 0xffff) {
275
275
  const a = g(6) >> 8;
276
+ if (a === 0) return false;
276
277
  return a === 127 || !isPrivateIpv4(a, g(6) & 0xff);
277
278
  }
278
279
  return true;
@@ -280,6 +281,7 @@ export function isBrowserReachableUrl(value: string): boolean {
280
281
  const octets = host.match(/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/);
281
282
  if (octets) {
282
283
  const [a, b] = [Number(octets[1]), Number(octets[2])];
284
+ if (a === 0) return false;
283
285
  return a === 127 || !isPrivateIpv4(a, b);
284
286
  }
285
287
  if (!host.includes(".")) return false;
@@ -0,0 +1,89 @@
1
+ import { afterEach, beforeEach, describe, expect, it, mock } from "bun:test";
2
+ import { fetchInstalledMcpServers } from "./mcp-server-fetcher.ts";
3
+
4
+ const originalFetch = global.fetch;
5
+
6
+ function mockFetchWithServers(servers: unknown[]) {
7
+ global.fetch = mock(
8
+ async () => new Response(JSON.stringify({ servers }), { status: 200 }),
9
+ ) as unknown as typeof fetch;
10
+ }
11
+
12
+ describe("fetchInstalledMcpServers", () => {
13
+ afterEach(() => {
14
+ global.fetch = originalFetch;
15
+ });
16
+
17
+ describe("stdio transport", () => {
18
+ const stdioServer = {
19
+ name: "gitlab",
20
+ transport: "stdio",
21
+ isActive: true,
22
+ isEnabled: true,
23
+ command: "npx",
24
+ args: JSON.stringify(["-y", "@zereight/mcp-gitlab"]),
25
+ resolvedEnv: { GITLAB_TOKEN: "secret" },
26
+ };
27
+
28
+ beforeEach(() => {
29
+ mockFetchWithServers([stdioServer]);
30
+ });
31
+
32
+ it("uses command/args/env shape for the claude format", async () => {
33
+ const result = await fetchInstalledMcpServers("https://api", "key", "agent", "claude");
34
+ expect(result).toEqual({
35
+ gitlab: {
36
+ command: "npx",
37
+ args: ["-y", "@zereight/mcp-gitlab"],
38
+ env: { GITLAB_TOKEN: "secret" },
39
+ },
40
+ });
41
+ });
42
+
43
+ it("uses the local/command-array/environment shape for the opencode format", async () => {
44
+ const result = await fetchInstalledMcpServers("https://api", "key", "agent", "opencode");
45
+ expect(result).toEqual({
46
+ gitlab: {
47
+ type: "local",
48
+ command: ["npx", "-y", "@zereight/mcp-gitlab"],
49
+ environment: { GITLAB_TOKEN: "secret" },
50
+ },
51
+ });
52
+ });
53
+ });
54
+
55
+ describe("http/sse transport", () => {
56
+ const httpServer = {
57
+ name: "docs",
58
+ transport: "http",
59
+ isActive: true,
60
+ isEnabled: true,
61
+ url: "https://example.com/mcp",
62
+ resolvedHeaders: { Authorization: "Bearer token" },
63
+ };
64
+
65
+ it("keeps the transport-specific type for the claude format", async () => {
66
+ mockFetchWithServers([httpServer]);
67
+ const result = await fetchInstalledMcpServers("https://api", "key", "agent", "claude");
68
+ expect(result).toEqual({
69
+ docs: {
70
+ type: "http",
71
+ url: "https://example.com/mcp",
72
+ headers: { Authorization: "Bearer token" },
73
+ },
74
+ });
75
+ });
76
+
77
+ it("uses the remote type for the opencode format", async () => {
78
+ mockFetchWithServers([httpServer]);
79
+ const result = await fetchInstalledMcpServers("https://api", "key", "agent", "opencode");
80
+ expect(result).toEqual({
81
+ docs: {
82
+ type: "remote",
83
+ url: "https://example.com/mcp",
84
+ headers: { Authorization: "Bearer token" },
85
+ },
86
+ });
87
+ });
88
+ });
89
+ });
@@ -38,11 +38,19 @@ export async function fetchInstalledMcpServers(
38
38
  } catch {
39
39
  // invalid JSON — use empty args
40
40
  }
41
- entries[srv.name] = {
42
- command: srv.command,
43
- args,
44
- env: srv.resolvedEnv || {},
45
- };
41
+ if (format === "opencode") {
42
+ entries[srv.name] = {
43
+ type: "local",
44
+ command: [srv.command, ...args],
45
+ environment: srv.resolvedEnv || {},
46
+ };
47
+ } else {
48
+ entries[srv.name] = {
49
+ command: srv.command,
50
+ args,
51
+ env: srv.resolvedEnv || {},
52
+ };
53
+ }
46
54
  } else if ((srv.transport === "http" || srv.transport === "sse") && srv.url) {
47
55
  let parsedHeaders: Record<string, string> = {};
48
56
  try {
@@ -22,6 +22,13 @@ export type HttpRequestAuth = (
22
22
  runtimeId: string | null;
23
23
  tokenId: string;
24
24
  expiresAt: string;
25
+ /**
26
+ * Set on a script run's per-run token (`mintScriptRunToken`): the
27
+ * swarm-script workflow step that started the run, and the run's own
28
+ * `script_runs` row. Rows the request creates are stamped with them.
29
+ */
30
+ sourceStepId?: string | null;
31
+ sourceScriptRunId?: string | null;
25
32
  }
26
33
  ) & {
27
34
  /** Verified page context for agent-scoped operations. This does not identify the viewer. */
@@ -96,3 +103,20 @@ export function isOperatorAuth(
96
103
  export function getCurrentRequestUserId(): string | undefined {
97
104
  return authUserId(getCurrentRequestAuth());
98
105
  }
106
+
107
+ /** Where the current request's script run came from, when it presents a script-run token. */
108
+ export type RunProvenance = { sourceStepId: string | null; sourceScriptRunId: string | null };
109
+
110
+ /**
111
+ * Provenance of the current request: set only when it authenticated with a
112
+ * script run's per-run token that recorded a step or a script run. Async work
113
+ * started inside the request inherits it (the workflow engine continuing a run
114
+ * it triggered), so callers stamp only rows a script can create directly.
115
+ */
116
+ export function getCurrentRunProvenance(): RunProvenance | null {
117
+ const auth = getCurrentRequestAuth();
118
+ if (auth?.kind !== "agent") return null;
119
+ const sourceStepId = auth.sourceStepId ?? null;
120
+ const sourceScriptRunId = auth.sourceScriptRunId ?? null;
121
+ return sourceStepId || sourceScriptRunId ? { sourceStepId, sourceScriptRunId } : null;
122
+ }
@@ -41,7 +41,7 @@ export const GITLEAKS_RULES: GeneratedSecretRuleSet = {
41
41
  "globalAllowlist": {
42
42
  "regexes": [
43
43
  {
44
- "source": "^true|false|null$", // gitleaks:allow
44
+ "source": "^(?:true|false|null)$", // gitleaks:allow
45
45
  "flags": "i"
46
46
  },
47
47
  {
@@ -44,8 +44,14 @@ import { GITLEAKS_RULES } from "./secret-rules.generated";
44
44
  * v10 = the sweep covers the dreaming-v2 KV namespace and decodes its legacy
45
45
  * hex copies. A new sweep target needs a bump too: an install that finished
46
46
  * the previous version never sweeps it otherwise.
47
+ * v11 = a pass 2 hit inside a longer gitleaks secret redacts that whole
48
+ * secret, so a generic `sk-` match in a Heroku key's tail no longer leaves
49
+ * the key's prefix behind.
50
+ * v12 = the gitleaks global allowlist `^true|false|null$` is anchored as a
51
+ * whole, so a secret containing "false", starting with "true" or ending with
52
+ * "null" is no longer allowed.
47
53
  */
48
- export const SCRUBBER_RULES_VERSION = 10;
54
+ export const SCRUBBER_RULES_VERSION = 12;
49
55
 
50
56
  /** Env-var names that are always considered secrets, even without suffix hints. */
51
57
  const SENSITIVE_KEY_EXACT = new Set<string>([
@@ -670,12 +676,14 @@ function allowedBy(list: CompiledAllowlist, secret: string, match: string): bool
670
676
  return list.stopwords.some((word) => lower.includes(word));
671
677
  }
672
678
 
673
- function applyGitleaksRule(text: string, rule: CompiledGitleaksRule, global: CompiledAllowlist) {
679
+ /** Secret spans `[start, end)` a rule redacts in `text`, after its entropy and allowlist gates. */
680
+ function* gitleaksSecretSpans(
681
+ text: string,
682
+ rule: CompiledGitleaksRule,
683
+ global: CompiledAllowlist,
684
+ ): Generator<[number, number]> {
674
685
  const { re } = rule;
675
686
  re.lastIndex = 0;
676
- let out = "";
677
- let last = 0;
678
- let changed = false;
679
687
  for (let m = re.exec(text); m !== null; m = re.exec(text)) {
680
688
  if (m[0].length === 0) {
681
689
  re.lastIndex++;
@@ -700,8 +708,17 @@ function applyGitleaksRule(text: string, rule: CompiledGitleaksRule, global: Com
700
708
  if (rule.entropy && shannonEntropy(secret) <= rule.entropy) continue;
701
709
  if (allowedBy(global, secret, m[0])) continue;
702
710
  if (rule.allowlists.some((list) => allowedBy(list, secret, m[0]))) continue;
703
- out += `${text.slice(last, span[0])}[REDACTED:gitleaks:${rule.id}]`;
704
- last = span[1];
711
+ yield [span[0], span[1]];
712
+ }
713
+ }
714
+
715
+ function applyGitleaksRule(text: string, rule: CompiledGitleaksRule, global: CompiledAllowlist) {
716
+ let out = "";
717
+ let last = 0;
718
+ let changed = false;
719
+ for (const [start, end] of gitleaksSecretSpans(text, rule, global)) {
720
+ out += `${text.slice(last, start)}[REDACTED:gitleaks:${rule.id}]`;
721
+ last = end;
705
722
  changed = true;
706
723
  }
707
724
  return changed ? out + text.slice(last) : text;
@@ -714,9 +731,10 @@ export function setGitleaksPassEnabledForTesting(enabled: boolean): void {
714
731
  gitleaksPassEnabled = enabled;
715
732
  }
716
733
 
717
- function scrubGitleaks(text: string): string {
718
- if (!gitleaksPassEnabled) return text;
719
- const { keywordRe, implied, rules, global } = getGitleaks();
734
+ /** The gitleaks rules whose keywords appear in `text` (empty when pass 5 is off). */
735
+ function activeGitleaksRules(text: string): CompiledGitleaksRule[] {
736
+ if (!gitleaksPassEnabled) return [];
737
+ const { keywordRe, implied, rules } = getGitleaks();
720
738
  // Visit every start position that has a keyword. The alternation is
721
739
  // longest-first, so a shorter keyword starting at the same position is a
722
740
  // substring of the one found, and `implied` adds it.
@@ -726,16 +744,134 @@ function scrubGitleaks(text: string): string {
726
744
  for (const keyword of implied.get(m[0].toLowerCase()) ?? []) present.add(keyword);
727
745
  keywordRe.lastIndex = m.index + 1;
728
746
  }
729
- if (present.size === 0) return text;
747
+ if (present.size === 0) return [];
748
+ return rules.filter((rule) => rule.keywords.some((keyword) => present.has(keyword)));
749
+ }
750
+
751
+ function scrubGitleaks(text: string): string {
730
752
  let out = text;
731
- for (const rule of rules) {
732
- if (rule.keywords.some((keyword) => present.has(keyword))) {
733
- out = applyGitleaksRule(out, rule, global);
753
+ for (const rule of activeGitleaksRules(text)) {
754
+ out = applyGitleaksRule(out, rule, getGitleaks().global);
755
+ }
756
+ return out;
757
+ }
758
+
759
+ type VendorSpan = { start: number; end: number; id: string };
760
+ type Edit = { start: number; end: number; len: number };
761
+
762
+ /**
763
+ * Gitleaks spans shared by the pass 2 rules of one scrub call. The sweep runs
764
+ * at most once per call, on the first structural hit; each rule then moves the
765
+ * spans through its own edits instead of sweeping its output again.
766
+ */
767
+ type VendorSpanCache = { spans: VendorSpan[] | null };
768
+
769
+ let pass2SweepCount = 0;
770
+
771
+ /** Test-only: how many pass 2 gitleaks sweeps ran since the last call. */
772
+ export function takePass2SweepCountForTesting(): number {
773
+ const n = pass2SweepCount;
774
+ pass2SweepCount = 0;
775
+ return n;
776
+ }
777
+
778
+ function vendorSpansOf(text: string, cache: VendorSpanCache): VendorSpan[] {
779
+ if (cache.spans === null) {
780
+ pass2SweepCount++;
781
+ const { global } = getGitleaks();
782
+ cache.spans = activeGitleaksRules(text).flatMap((rule) =>
783
+ [...gitleaksSecretSpans(text, rule, global)].map(([s, e]) => ({
784
+ start: s,
785
+ end: e,
786
+ id: rule.id,
787
+ })),
788
+ );
789
+ }
790
+ return cache.spans;
791
+ }
792
+
793
+ /**
794
+ * Moves spans through sorted, non-overlapping edits. A span an edit touches
795
+ * is dropped: its secret was redacted or cut, and pass 5 still sweeps the
796
+ * final text for anything a fresh sweep would have found.
797
+ */
798
+ function shiftSpans(spans: VendorSpan[], edits: Edit[]): VendorSpan[] {
799
+ if (edits.length === 0 || spans.length === 0) return spans;
800
+ const out: VendorSpan[] = [];
801
+ let i = 0;
802
+ let delta = 0;
803
+ for (const v of [...spans].sort((x, y) => x.start - y.start)) {
804
+ let e = edits[i];
805
+ while (e && e.end <= v.start) {
806
+ delta += e.len - (e.end - e.start);
807
+ e = edits[++i];
734
808
  }
809
+ if (e && e.start < v.end) continue;
810
+ out.push({ start: v.start + delta, end: v.end + delta, id: v.id });
735
811
  }
736
812
  return out;
737
813
  }
738
814
 
815
+ /** `text.replace(re, marker)` that also returns the edits it made. */
816
+ function replaceAll(text: string, re: RegExp, marker: string): { out: string; edits: Edit[] } {
817
+ const edits: Edit[] = [];
818
+ const out = text.replace(re, (match: string, ...rest: unknown[]) => {
819
+ const start = rest.find((x) => typeof x === "number") as number;
820
+ edits.push({ start, end: start + match.length, len: marker.length });
821
+ return marker;
822
+ });
823
+ return { out, edits };
824
+ }
825
+
826
+ /**
827
+ * Pass 2 for one structural rule. A gitleaks secret that strictly contains a
828
+ * hit wins (longest span): it is redacted whole first, so a generic rule
829
+ * matching inside it (`sk-…` in a Heroku key's tail) cannot leave its prefix
830
+ * behind for pass 5 to miss.
831
+ */
832
+ function applyTokenRegex(text: string, name: string, re: RegExp, cache: VendorSpanCache): string {
833
+ const marker = `[REDACTED:${name}]`;
834
+ const covering: VendorSpan[] = [];
835
+ const edits: Edit[] = [];
836
+ const out = text.replace(re, (match: string, ...rest: unknown[]) => {
837
+ const start = rest.find((x) => typeof x === "number") as number;
838
+ const end = start + match.length;
839
+ let best: VendorSpan | undefined;
840
+ for (const v of vendorSpansOf(text, cache)) {
841
+ if (v.start <= start && end <= v.end && v.end - v.start > match.length) {
842
+ if (!best || v.end - v.start > best.end - best.start) best = v;
843
+ }
844
+ }
845
+ if (!best) {
846
+ edits.push({ start, end, len: marker.length });
847
+ return marker;
848
+ }
849
+ covering.push(best);
850
+ return match;
851
+ });
852
+ if (covering.length === 0) {
853
+ if (cache.spans) cache.spans = shiftSpans(cache.spans, edits);
854
+ return out;
855
+ }
856
+ // Redact the covering secrets right to left, longest first on a tie; one
857
+ // that overlaps the marker to its right merges into that marker.
858
+ covering.sort((x, y) => y.start - x.start || y.end - x.end);
859
+ let result = text;
860
+ let floor = Number.POSITIVE_INFINITY;
861
+ const vendorEdits: Edit[] = [];
862
+ for (const v of covering) {
863
+ if (v.start >= floor) continue;
864
+ const end = Math.min(v.end, floor);
865
+ const vendorMarker = v.end > floor ? "" : `[REDACTED:gitleaks:${v.id}]`;
866
+ result = result.slice(0, v.start) + vendorMarker + result.slice(end);
867
+ vendorEdits.push({ start: v.start, end, len: vendorMarker.length });
868
+ floor = v.start;
869
+ }
870
+ const structural = replaceAll(result, re, marker);
871
+ cache.spans = shiftSpans(shiftSpans(cache.spans ?? [], vendorEdits.reverse()), structural.edits);
872
+ return structural.out;
873
+ }
874
+
739
875
  interface EnvValueEntry {
740
876
  value: string;
741
877
  name: string;
@@ -940,9 +1076,8 @@ export function scrubSecrets(text: string | null | undefined): ScrubbedText {
940
1076
  // Pass 2: structural patterns (catches secrets we never saw in env, e.g.
941
1077
  // a token pasted into a tool_result by the operator or fetched from a
942
1078
  // third-party API during a task).
943
- for (const { name, re } of TOKEN_REGEXES) {
944
- out = out.replace(re, `[REDACTED:${name}]`);
945
- }
1079
+ const vendorSpans: VendorSpanCache = { spans: null };
1080
+ for (const { name, re } of TOKEN_REGEXES) out = applyTokenRegex(out, name, re, vendorSpans);
946
1081
 
947
1082
  // Pass 3: values of sensitive `KEY=value` assignments (env dumps, .env
948
1083
  // files, shell traces), including ones too short for pass 1.
@@ -990,18 +1125,8 @@ export function findSecretsIn(text: string): SecretFindings {
990
1125
  }
991
1126
  }
992
1127
 
993
- if (gitleaksPassEnabled) {
994
- const { keywordRe, implied, rules, global } = getGitleaks();
995
- const present = new Set<string>();
996
- keywordRe.lastIndex = 0;
997
- for (let m = keywordRe.exec(text); m !== null; m = keywordRe.exec(text)) {
998
- for (const keyword of implied.get(m[0].toLowerCase()) ?? []) present.add(keyword);
999
- keywordRe.lastIndex = m.index + 1;
1000
- }
1001
- for (const rule of rules) {
1002
- if (!rule.keywords.some((keyword) => present.has(keyword))) continue;
1003
- if (applyGitleaksRule(text, rule, global) !== text) gitleaksRules.add(rule.id);
1004
- }
1128
+ for (const rule of activeGitleaksRules(text)) {
1129
+ if (applyGitleaksRule(text, rule, getGitleaks().global) !== text) gitleaksRules.add(rule.id);
1005
1130
  }
1006
1131
 
1007
1132
  return { knownValues: [...knownValues].sort(), gitleaksRules: [...gitleaksRules].sort() };