@desplega.ai/agent-swarm 2.7.9 → 2.8.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 (355) hide show
  1. package/dist/acp-adapter-t949vbbp.js +15 -0
  2. package/dist/{actions-g563mbn2.js → actions-7e9czc7s.js} +19 -19
  3. package/dist/{amp-adapter-q61w05eb.js → amp-adapter-gv6cbb7d.js} +2 -3
  4. package/dist/app-b9fbkp9v.js +20 -0
  5. package/dist/{assistant-p477rjwj.js → assistant-wjb7v4c7.js} +22 -22
  6. package/dist/{boot-args-schema-h4qj462e.js → boot-args-schema-2t37e0fe.js} +1 -1
  7. package/dist/{boot-reembed-n1ehfbsw.js → boot-reembed-a35nkyr1.js} +1 -1
  8. package/dist/{boot-reembed-qw4mc0pt.js → boot-reembed-gka3zas7.js} +1 -1
  9. package/dist/{boot-scrub-sweep-9w9jp4sm.js → boot-scrub-sweep-65sdnp6c.js} +3 -3
  10. package/dist/{boot-seeds-qdaf3sdy.js → boot-seeds-x42tzytw.js} +4 -5
  11. package/dist/{claude-adapter-348p5hx9.js → claude-adapter-v221b2fd.js} +7 -8
  12. package/dist/{claude-managed-adapter-h74d06yz.js → claude-managed-adapter-29eq09wd.js} +2 -2
  13. package/dist/{claude-managed-setup-7787fxtp.js → claude-managed-setup-9m6450zk.js} +2 -2
  14. package/dist/{claude-sdk-session-hbszbt8a.js → claude-sdk-session-0q0ptaba.js} +9066 -8818
  15. package/dist/{cli-n5yexkdg.js → cli-0b4zam62.js} +1 -0
  16. package/dist/{cli-21159vea.js → cli-0jzra9ad.js} +2 -2
  17. package/dist/{cli-eqtfgfr6.js → cli-2gxeg5gr.js} +1 -1
  18. package/dist/{cli-k70e3x9e.js → cli-2wnq8jkz.js} +9 -9
  19. package/dist/{cli-cvs2r2vq.js → cli-2zp0et3v.js} +59 -6
  20. package/dist/{cli-650zv9q9.js → cli-334z18y3.js} +4 -4
  21. package/dist/{cli-12xyjhws.js → cli-3rkdne1h.js} +2 -2
  22. package/dist/{cli-2a8py3sx.js → cli-4txa0ter.js} +5 -7
  23. package/dist/{cli-jc1g9ndz.js → cli-5j88qqa8.js} +10 -5
  24. package/dist/{cli-xfjrc279.js → cli-5mrq3ake.js} +48 -14
  25. package/dist/{cli-v3cf1ts9.js → cli-6t09sdhy.js} +3 -3
  26. package/dist/{cli-qzt90yze.js → cli-6wxjceza.js} +8 -8
  27. package/dist/{cli-9aezs55w.js → cli-7b8sbmag.js} +8 -8
  28. package/dist/{cli-3xj33nrx.js → cli-7cp6aa1q.js} +10 -3
  29. package/dist/{cli-jfx48xrj.js → cli-7p1kvkck.js} +6 -6
  30. package/dist/{cli-b932gexn.js → cli-8aes17e4.js} +2 -2
  31. package/dist/{cli-3tqcamp6.js → cli-8fknbnhw.js} +14 -13
  32. package/dist/{cli-587857ys.js → cli-8kzasymh.js} +3 -3
  33. package/dist/{cli-sgs5k6yd.js → cli-a7wvp2km.js} +1 -1
  34. package/dist/{cli-varqt4d1.js → cli-abejgen1.js} +69 -22
  35. package/dist/{cli-rwymrnaz.js → cli-bsywb58a.js} +8 -8
  36. package/dist/{cli-btc9cb4c.js → cli-c55z5chy.js} +4 -6
  37. package/dist/{cli-ms5m5r1y.js → cli-deefvewm.js} +1 -1
  38. package/dist/{cli-p17970qv.js → cli-ee0b971j.js} +3 -3
  39. package/dist/cli-f3vq58d2.js +242 -0
  40. package/dist/{cli-3xcwp3z8.js → cli-ffhtssbp.js} +2 -2
  41. package/dist/{cli-hwprv9e4.js → cli-g70vxtjh.js} +2 -2
  42. package/dist/{cli-4t0fmf3z.js → cli-gxkx0zzp.js} +1 -1
  43. package/dist/{cli-neyag5p6.js → cli-h38x262p.js} +3365 -429
  44. package/dist/{cli-2ccpqyyf.js → cli-j790gp2w.js} +105 -22
  45. package/dist/{cli-f2zenhja.js → cli-kc7v1nfv.js} +1 -1
  46. package/dist/{cli-vxh6acp7.js → cli-m23zjcy2.js} +10 -10
  47. package/dist/{cli-y8nbefpb.js → cli-m28atx8q.js} +12 -10
  48. package/dist/{cli-serm8zrx.js → cli-m4dqmnbp.js} +1 -1
  49. package/dist/{cli-ybjdc978.js → cli-mrt48bx7.js} +7 -4
  50. package/dist/{cli-y5rbvxhc.js → cli-n515ex5z.js} +14 -14
  51. package/dist/{cli-ypff1qwp.js → cli-n9ftpys4.js} +7 -7
  52. package/dist/{cli-605nn699.js → cli-p1bq50aa.js} +894 -226
  53. package/dist/{cli-qkdmz8c8.js → cli-p1w18yhx.js} +1 -1
  54. package/dist/{cli-xk879m3k.js → cli-p2gfst4m.js} +52 -43
  55. package/dist/{cli-sya22y84.js → cli-pasjcnmj.js} +12 -12
  56. package/dist/{cli-0whpms1z.js → cli-qa4wdgsy.js} +5 -5
  57. package/dist/{cli-ejkvvqbe.js → cli-qfedvmcr.js} +50 -24
  58. package/dist/{cli-tqq0hspb.js → cli-qmqk2yc2.js} +1 -1
  59. package/dist/{cli-qmd3mse0.js → cli-qsnnd04k.js} +1 -1
  60. package/dist/{cli-agyaeef4.js → cli-qwca7s44.js} +3 -3
  61. package/dist/{cli-fayav105.js → cli-rfywm85n.js} +724 -792
  62. package/dist/{cli-zgv8gxgs.js → cli-rmmpt0wm.js} +6 -6
  63. package/dist/{cli-hae9j8sv.js → cli-rzsy6nc6.js} +12 -8
  64. package/dist/{cli-btf6fw39.js → cli-s7t6p9my.js} +1 -1
  65. package/dist/{cli-cseh6f8w.js → cli-sdtt3men.js} +1 -1
  66. package/dist/{cli-9r5gtwjr.js → cli-tk7cjj8h.js} +197 -2
  67. package/dist/{cli-q26b9wre.js → cli-tph5wt3y.js} +4 -4
  68. package/dist/{cli-gce09r66.js → cli-tqegb8eq.js} +3 -3
  69. package/dist/{cli-kf3pxxdg.js → cli-v1dv1r37.js} +1 -1
  70. package/dist/{cli-09kmfkmq.js → cli-wcg8x116.js} +1 -1
  71. package/dist/{cli-mnrkgg60.js → cli-wk18cm92.js} +5 -5
  72. package/dist/{cli-p0m0p34z.js → cli-x1wa213s.js} +3 -3
  73. package/dist/{cli-jrp1dt73.js → cli-xzm9vcfg.js} +1 -1
  74. package/dist/{cli-715t1gx6.js → cli-y97qxcfx.js} +9 -9
  75. package/dist/{cli-bc779n4v.js → cli-yq3zak69.js} +1 -1
  76. package/dist/cli.js +17 -17
  77. package/dist/{codex-adapter-ysq6bzha.js → codex-adapter-30j9bpzd.js} +5 -6
  78. package/dist/{codex-hook-fac6175r.js → codex-hook-h3vk6xzr.js} +1 -1
  79. package/dist/{codex-session-runner-qakzfx93.js → codex-session-runner-8vp57v8w.js} +5 -6
  80. package/dist/{commands-rs7hkv9r.js → commands-bngnz9g7.js} +3 -3
  81. package/dist/{controller-r5x9bg6e.js → controller-hxx8wwy1.js} +2 -2
  82. package/dist/{controllers-connect-498hrxvk.js → controllers-connect-e2b4gyxc.js} +20 -20
  83. package/dist/{cursor-adapter-d46qszer.js → cursor-adapter-n5nc3mfe.js} +1 -2
  84. package/dist/{db-s13dqhr6.js → db-bgm7vkc0.js} +3 -3
  85. package/dist/{db-pm96nhdb.js → db-c6np41ca.js} +12 -2
  86. package/dist/{devin-adapter-z46ep5ph.js → devin-adapter-xedz98zv.js} +1 -1
  87. package/dist/{dispatcher-j3h81t7m.js → dispatcher-jnybck15.js} +18 -18
  88. package/dist/{dsh-adapter-rw0d5rc4.js → dsh-adapter-nrhbrjz8.js} +1 -2
  89. package/dist/{easter-egg-3d-qtgfxgze.js → easter-egg-3d-533knw22.js} +1 -1
  90. package/dist/{embedding-space-xh8p4q4x.js → embedding-space-crsm2zp0.js} +6 -6
  91. package/dist/{embeddings-wp745m91.js → embeddings-e7z3f27f.js} +1 -1
  92. package/dist/{execute-2fzb9z74.js → execute-b3mrb1mv.js} +1 -1
  93. package/dist/{external-channel-ys21h3sy.js → external-channel-m77wf5z3.js} +2 -2
  94. package/dist/{fx-adapter-vz0b9z3g.js → fx-adapter-b6w0kf56.js} +7 -8
  95. package/dist/{google-generative-ai-ytpgncx9.js → google-generative-ai-h7s3e9kk.js} +1 -1
  96. package/dist/{google-vertex-tqhze5j7.js → google-vertex-fhxaafzw.js} +1 -1
  97. package/dist/{grok-adapter-spea7zkj.js → grok-adapter-4sqtebdk.js} +7 -8
  98. package/dist/handlers-kdjcyzdj.js +32 -0
  99. package/dist/heartbeat-xgb036sx.js +43 -0
  100. package/dist/{hook-amcj4s10.js → hook-5h02c9ga.js} +6 -6
  101. package/dist/{hook-bc6n49qn.js → hook-xsk1jbha.js} +5 -5
  102. package/dist/{http-7374brv5.js → http-txe7qhvn.js} +448 -165
  103. package/dist/{index-pxn30mse.js → index-0eny15vj.js} +8 -8
  104. package/dist/{index-5a9jtqde.js → index-0rvk3vd4.js} +46 -46
  105. package/dist/{index-fvzke5kx.js → index-1qw6f2dc.js} +10 -10
  106. package/dist/{index-q5cmtbz5.js → index-4a0670yh.js} +15939 -14244
  107. package/dist/index-835xdgnm.js +33 -0
  108. package/dist/index-d469300x.js +37 -0
  109. package/dist/{index-h7f61sy9.js → index-e5pfbsys.js} +3 -3
  110. package/dist/{index-hsad2t3r.js → index-m5gx1xd6.js} +38 -38
  111. package/dist/{index-5fx3b3kw.js → index-rvccek1w.js} +9 -9
  112. package/dist/index-rzqwp11g.js +32 -0
  113. package/dist/{index-w4m9z29z.js → index-tvfafeyy.js} +1 -1
  114. package/dist/{index-c9y31ytf.js → index-vk63emp7.js} +979 -35
  115. package/dist/{index-514d9pf6.js → index-w7em4ktq.js} +8 -8
  116. package/dist/{index-p4hjwbvw.js → index-ysvbhg5m.js} +1 -1
  117. package/dist/{keepalive-wy29kbrg.js → keepalive-4zsrce78.js} +3 -3
  118. package/dist/lead-yqt47dkr.js +70 -0
  119. package/dist/{maintenance-h2ds9w6c.js → maintenance-86jvartk.js} +1 -1
  120. package/dist/{oauth-refresh-sweep-q7mtbyp3.js → oauth-refresh-sweep-j52kg9pr.js} +2 -2
  121. package/dist/{onboard-vw8r2fdw.js → onboard-c2aj5p8q.js} +3 -3
  122. package/dist/{opencode-adapter-mtb0953j.js → opencode-adapter-dsagkhtw.js} +5 -6
  123. package/dist/{otel-impl-j42czw3r.js → otel-impl-yyy203z8.js} +1 -1
  124. package/dist/{pi-mono-adapter-zk8pg9jr.js → pi-mono-adapter-546cd1cc.js} +9 -9
  125. package/dist/{pricing-refresh-x7nhv23d.js → pricing-refresh-1rgzqyzz.js} +2 -3
  126. package/dist/{rbac-roles-9044425x.js → rbac-roles-e70xfeyj.js} +4 -6
  127. package/dist/{rbac-roles-bzvee6cf.js → rbac-roles-q89qe8er.js} +3 -3
  128. package/dist/{registry-hpjfmcfz.js → registry-32b1mft6.js} +1 -1
  129. package/dist/{render-v2-t7bn4skz.js → render-v2-k6tt107q.js} +9 -4
  130. package/dist/{resume-x94gemep.js → resume-cnnrjfxa.js} +6 -6
  131. package/dist/{runtime-breq6fmv.js → runtime-1hv06jsn.js} +1 -1
  132. package/dist/{session-access-q7dmqjvt.js → session-access-xkxkjw3n.js} +4 -4
  133. package/dist/{session-source-backfill-440ysamr.js → session-source-backfill-1d5pdwww.js} +2 -2
  134. package/dist/{setup-07n4727s.js → setup-se8q7gkc.js} +1 -1
  135. package/dist/{virtual-modules-z5h6mcyf.js → virtual-modules-gazyapte.js} +3 -3
  136. package/dist/{work-objects-pnq5y534.js → work-objects-1kxnbzy5.js} +22 -22
  137. package/dist/{worker-follow-up-7z1gfkr8.js → worker-follow-up-zqm8apt4.js} +18 -18
  138. package/dist/worker-j6xxtq7p.js +70 -0
  139. package/openapi.json +334 -7
  140. package/package.json +4 -4
  141. package/src/apps/catalog.generated.json +55 -0
  142. package/src/apps/definition.ts +11 -0
  143. package/src/apps/page-validator.ts +19 -0
  144. package/src/apps/store.ts +28 -8
  145. package/src/assets/key.ts +77 -4
  146. package/src/be/db/model-profiles.ts +147 -38
  147. package/src/be/db/session-labels.ts +57 -0
  148. package/src/be/db/session-tree.ts +51 -7
  149. package/src/be/db/slack-trees.ts +146 -0
  150. package/src/be/db/tasks/read.ts +107 -8
  151. package/src/be/db/workflow-run-definitions.ts +117 -0
  152. package/src/be/db-client.ts +8 -2
  153. package/src/be/db.ts +119 -87
  154. package/src/be/event-loop-lag.ts +146 -0
  155. package/src/be/mcp-overflow-sweep.ts +2 -1
  156. package/src/be/memory/constants.ts +14 -1
  157. package/src/be/memory/key-paths.ts +8 -0
  158. package/src/be/memory/providers/sqlite-store.ts +199 -18
  159. package/src/be/memory/task-learning.ts +79 -14
  160. package/src/be/memory/types.ts +24 -1
  161. package/src/be/migrations/278_slack_render_v2_reconcile.sql +108 -0
  162. package/src/be/migrations/279_backfill_learning_expiry.sql +15 -0
  163. package/src/be/migrations/281_model_profiles_pr2.sql +28 -0
  164. package/src/be/migrations/283_workflow_run_definitions.sql +14 -0
  165. package/src/be/model-profile-check.ts +284 -0
  166. package/src/be/model-tier-resolution.ts +112 -12
  167. package/src/be/multi-runtime.ts +27 -0
  168. package/src/be/runtime-pools/routing.ts +52 -4
  169. package/src/be/scripts/typecheck.ts +1 -0
  170. package/src/be/seed-scripts/catalog/dreaming-learnings.ts +552 -0
  171. package/src/be/seed-scripts/catalog/dreaming-memtax-create.ts +389 -14
  172. package/src/be/seed-scripts/catalog/dreaming-memtax.ts +6 -3
  173. package/src/be/seed-scripts/index.ts +9 -0
  174. package/src/be/steering.ts +8 -2
  175. package/src/be/swarm-config-guard.ts +2 -0
  176. package/src/commands/context-preamble.ts +258 -0
  177. package/src/commands/onboard/agent-fs-version.ts +1 -1
  178. package/src/commands/runner.ts +32 -1
  179. package/src/extensions/catalog.generated.json +3 -3
  180. package/src/fs/provider.ts +17 -3
  181. package/src/heartbeat/heartbeat.ts +17 -5
  182. package/src/heartbeat/index.ts +1 -0
  183. package/src/http/agents.ts +56 -4
  184. package/src/http/apps.ts +3 -1
  185. package/src/http/config.ts +2 -6
  186. package/src/http/core.ts +9 -3
  187. package/src/http/index.ts +12 -9
  188. package/src/http/memory.ts +9 -4
  189. package/src/http/model-profiles.ts +58 -10
  190. package/src/http/poll.ts +36 -3
  191. package/src/http/schedules.ts +42 -3
  192. package/src/http/sessions.ts +16 -3
  193. package/src/http/tasks.ts +37 -12
  194. package/src/http/utils.ts +18 -0
  195. package/src/http/webhooks.ts +48 -0
  196. package/src/http/workflows.ts +15 -2
  197. package/src/integrations/chat-sdk/commands.ts +167 -31
  198. package/src/integrations/chat-sdk/inbound.ts +10 -8
  199. package/src/integrations/chat-sdk/message-text.ts +117 -0
  200. package/src/prompts/session-templates.ts +1 -1
  201. package/src/providers/acp-targets.ts +11 -6
  202. package/src/providers/amp-adapter.ts +1 -1
  203. package/src/providers/claude-sdk-session.ts +132 -23
  204. package/src/providers/codex-skill-resolver.ts +10 -4
  205. package/src/providers/cursor-adapter.ts +1 -1
  206. package/src/providers/devin-skill-resolver.ts +5 -5
  207. package/src/providers/dsh-adapter.ts +1 -1
  208. package/src/scheduler/schedule-task.ts +1 -0
  209. package/src/scheduler/scheduler.ts +4 -1
  210. package/src/scripts-runtime/executors/quickjs-runner.ts +2 -0
  211. package/src/scripts-runtime/executors/quickjs-structured-clone.ts +180 -0
  212. package/src/scripts-runtime/quickjs-compat.ts +0 -1
  213. package/src/scripts-runtime/sdk-allowlist.ts +1 -0
  214. package/src/scripts-runtime/swarm-sdk.ts +18 -1
  215. package/src/scripts-runtime/types/stdlib.d.ts +1 -0
  216. package/src/scripts-runtime/types/swarm-sdk.d.ts +1 -0
  217. package/src/server.ts +2 -0
  218. package/src/slack/app.ts +425 -10
  219. package/src/slack/handlers.ts +9 -2
  220. package/src/slack/inbound-dispatch.ts +18 -1
  221. package/src/slack/index.ts +7 -1
  222. package/src/slack/lifecycle-lock.ts +29 -0
  223. package/src/slack/render-v2.ts +424 -212
  224. package/src/slack/socket-fence.ts +64 -0
  225. package/src/slack/watcher.ts +42 -8
  226. package/src/slack/workspace-guard.ts +345 -0
  227. package/src/tasks/task-terminal-effects.ts +4 -0
  228. package/src/tasks/worker-follow-up.ts +102 -2
  229. package/src/tests/acp-adapter.test.ts +82 -0
  230. package/src/tests/apps-icon.test.ts +120 -0
  231. package/src/tests/apps-schema-form.test.ts +151 -0
  232. package/src/tests/chained-task-follow-up.test.ts +248 -0
  233. package/src/tests/chat-sdk-commands.test.ts +359 -2
  234. package/src/tests/chat-sdk-inbound.test.ts +20 -0
  235. package/src/tests/chat-sdk-slack.test.ts +189 -1
  236. package/src/tests/check-pr-body.test.ts +40 -0
  237. package/src/tests/claude-sdk-lifecycle.test.ts +216 -1
  238. package/src/tests/claude-sdk-process.fixture.ts +57 -0
  239. package/src/tests/codex-skill-resolver.test.ts +31 -0
  240. package/src/tests/credentials-allowlist.test.ts +470 -0
  241. package/src/tests/db-client-busy-retry.test.ts +69 -18
  242. package/src/tests/db-client.test.ts +26 -1
  243. package/src/tests/db-task-read-contract.test.ts +7 -0
  244. package/src/tests/db-task-write-contract.test.ts +6 -0
  245. package/src/tests/deferred-wake-follow-up.test.ts +47 -0
  246. package/src/tests/docs-index-footprint.test.js +3 -3
  247. package/src/tests/dreaming-learnings.test.ts +1395 -0
  248. package/src/tests/dsh-adapter.test.ts +1 -1
  249. package/src/tests/event-loop-lag.test.ts +184 -0
  250. package/src/tests/explicit-model-validation.test.ts +2 -0
  251. package/src/tests/extensions-example-comms-adherence-gate.test.ts +115 -0
  252. package/src/tests/fs-provider.test.ts +42 -0
  253. package/src/tests/github-webhook-early-ack.test.ts +144 -0
  254. package/src/tests/heartbeat.test.ts +28 -1
  255. package/src/tests/http-api-integration.test.ts +2 -0
  256. package/src/tests/http-log-scrubbing.test.ts +68 -2
  257. package/src/tests/human-free-reclassify.test.ts +5 -1
  258. package/src/tests/kv-storage.test.ts +92 -1
  259. package/src/tests/list-endpoint-slimming.test.ts +17 -0
  260. package/src/tests/memory-key-paths.test.ts +57 -0
  261. package/src/tests/memory-retrieval-purge.test.ts +19 -0
  262. package/src/tests/migration-283-workflow-run-definitions.test.ts +69 -0
  263. package/src/tests/model-profile-check.test.ts +387 -0
  264. package/src/tests/model-profiles-api.test.ts +528 -1
  265. package/src/tests/profile-claim.test.ts +415 -3
  266. package/src/tests/profile-tier-alias.test.ts +382 -0
  267. package/src/tests/quickjs-compat.test.ts +1 -1
  268. package/src/tests/runner-context-preamble.test.ts +552 -0
  269. package/src/tests/schedule-profile.test.ts +359 -0
  270. package/src/tests/script-connections.test.ts +9 -0
  271. package/src/tests/script-executor-conformance.test.ts +188 -0
  272. package/src/tests/script-per-script-executor.test.ts +1 -0
  273. package/src/tests/scripts-sdk-bridge-tool-failure.test.ts +141 -0
  274. package/src/tests/seed-scripts.test.ts +2 -2
  275. package/src/tests/sessions-tree.test.ts +133 -2
  276. package/src/tests/slack-app-icon.test.ts +4 -0
  277. package/src/tests/slack-dm-implicit-mention.test.ts +236 -0
  278. package/src/tests/slack-file-tools-attachments.test.ts +1 -0
  279. package/src/tests/slack-inbound-receipts.test.ts +22 -2
  280. package/src/tests/slack-read-boundaries.test.ts +4 -3
  281. package/src/tests/slack-render-v2-query-plans.test.ts +262 -3
  282. package/src/tests/slack-render-v2-reconcile.test.ts +201 -0
  283. package/src/tests/slack-socket-ack.test.ts +16 -1
  284. package/src/tests/slack-socket-watchdog.test.ts +748 -0
  285. package/src/tests/slack-workspace-guard.test.ts +905 -0
  286. package/src/tests/steering-core.test.ts +40 -0
  287. package/src/tests/store-progress-learning.test.ts +605 -20
  288. package/src/tests/tool-annotations.test.ts +1 -1
  289. package/src/tests/vcs-identity-inheritance.test.ts +85 -0
  290. package/src/tests/workflow-convergence.test.ts +46 -40
  291. package/src/tests/workflow-definition-pinning-recovery.test.ts +552 -0
  292. package/src/tests/workflow-definition-pinning.test.ts +645 -0
  293. package/src/tests/workflow-executors.test.ts +16 -7
  294. package/src/tests/workflow-foreach.test.ts +43 -20
  295. package/src/tests/workflow-notify-slack-ready.test.ts +161 -0
  296. package/src/tests/workflow-profile.test.ts +328 -0
  297. package/src/tools/app-upsert.ts +1 -1
  298. package/src/tools/memory-edit.ts +2 -1
  299. package/src/tools/model-profiles/check-model-profile.ts +63 -0
  300. package/src/tools/schedules/create-schedule.ts +45 -4
  301. package/src/tools/schedules/patch-schedule.ts +50 -4
  302. package/src/tools/schedules/update-schedule.ts +50 -4
  303. package/src/tools/send-task.ts +17 -2
  304. package/src/tools/store-progress.ts +15 -3
  305. package/src/tools/task-action.ts +9 -1
  306. package/src/tools/templates.ts +13 -0
  307. package/src/tools/tool-config.ts +2 -1
  308. package/src/tools/utils.ts +77 -11
  309. package/src/tools/workflows/create-workflow.ts +1 -1
  310. package/src/tools/workflows/retry-workflow-run.ts +1 -1
  311. package/src/tools/workflows/update-workflow.ts +10 -1
  312. package/src/types.ts +56 -1
  313. package/src/utils/credentials.ts +89 -12
  314. package/src/workflows/engine.ts +10 -1
  315. package/src/workflows/executors/agent-task.ts +21 -2
  316. package/src/workflows/executors/foreach.ts +4 -1
  317. package/src/workflows/executors/notify.ts +62 -6
  318. package/src/workflows/executors/sub-workflow.ts +1 -1
  319. package/src/workflows/executors/swarm-script.ts +4 -1
  320. package/src/workflows/foreach-driver.ts +8 -5
  321. package/src/workflows/limits.ts +2 -1
  322. package/src/workflows/model-validation.ts +27 -2
  323. package/src/workflows/patch-definition.ts +3 -1
  324. package/src/workflows/recovery.ts +6 -6
  325. package/src/workflows/resume.ts +9 -9
  326. package/src/workflows/retry-poller.ts +2 -2
  327. package/templates/extensions/comms-adherence-gate/README.md +25 -0
  328. package/templates/extensions/comms-adherence-gate/hooks.ts +100 -11
  329. package/templates/extensions/comms-adherence-gate/manifest.json +1 -1
  330. package/templates/skills/apps/SKILL.md +9 -0
  331. package/templates/skills/apps/content.md +9 -0
  332. package/templates/skills/code-quality/content.md +12 -0
  333. package/templates/skills/memory/content.md +8 -0
  334. package/templates/skills/scheduling/config.json +1 -1
  335. package/templates/skills/scheduling/content.md +2 -2
  336. package/templates/skills/swarm-docs/content.md +4 -0
  337. package/templates/skills/swarm-extensions/content.md +1 -1
  338. package/templates/skills/swarm-scripts/SKILL.md +1 -1
  339. package/templates/skills/swarm-scripts/content.md +1 -1
  340. package/templates/skills/work-on-task/SKILL.md +2 -0
  341. package/templates/skills/work-on-task/content.md +2 -0
  342. package/templates/workflows/dreaming/content.md +174 -7
  343. package/dist/acp-adapter-dn4gdsgr.js +0 -16
  344. package/dist/app-a66ssc4k.js +0 -17
  345. package/dist/cli-530hkwdy.js +0 -779
  346. package/dist/cli-a0657jmf.js +0 -339
  347. package/dist/cli-cyn9v8zr.js +0 -748
  348. package/dist/handlers-crckt5dq.js +0 -32
  349. package/dist/heartbeat-8phn1kdq.js +0 -41
  350. package/dist/index-4x08sjtk.js +0 -30
  351. package/dist/index-8ybffts4.js +0 -37
  352. package/dist/index-nfy4kkg5.js +0 -33
  353. package/dist/lead-zdqxsvjn.js +0 -71
  354. package/dist/worker-y62rvvq3.js +0 -71
  355. package/dist/{cli-gxy8y1dt.js → cli-8bpgbr8k.js} +3 -3
package/src/be/db.ts CHANGED
@@ -217,6 +217,7 @@ import {
217
217
  threadRootsSql,
218
218
  } from "./db/session-tree";
219
219
  import { searchSkillRowsRanked } from "./db/skill-search";
220
+ import { SLACK_TREE_NEEDS_RENDER_SQL } from "./db/slack-trees";
220
221
  import { classifyTaskHumanFree, reclassifyTaskHumanFree } from "./db/tasks/human-free";
221
222
  import {
222
223
  type AgentTaskRow,
@@ -231,6 +232,7 @@ import {
231
232
  } from "./db/tasks/read";
232
233
  import { configureTaskWriteDependencies, failTask } from "./db/tasks/write";
233
234
  import { tombstoneWorkflowIdempotencyKeys } from "./db/workflow-idempotency";
235
+ import { deleteOrphanedRunDefinitions } from "./db/workflow-run-definitions";
234
236
  import {
235
237
  emitChildRunFinished,
236
238
  emitRunFinished,
@@ -326,6 +328,13 @@ export {
326
328
  assertAssetKeyMovesAllowed,
327
329
  moveTaskChainKeyInTx,
328
330
  } from "./db/session-moves";
331
+ export {
332
+ getRunWorkflow,
333
+ PINNED_WORKFLOW_KEYS,
334
+ type PinnedWorkflowDefinition,
335
+ pinWorkflowDefinition,
336
+ toPinnedDefinition,
337
+ } from "./db/workflow-run-definitions";
329
338
 
330
339
  configureAgentDependencies({
331
340
  createLogEntry: (entry) => createLogEntry(entry),
@@ -367,6 +376,7 @@ export {
367
376
  getLatestLeadTaskInThread,
368
377
  getLatestScriptRunStepTaskByContextKey,
369
378
  getLatestTaskByContextKey,
379
+ getLatestTaskByContextKeyPrefix,
370
380
  getMostRecentTaskInThread,
371
381
  getPendingChatRelayTasks,
372
382
  getPendingSlackRelayTasks,
@@ -596,6 +606,8 @@ export interface SlackMessageRecord {
596
606
  deliveryAbandonedAt?: string;
597
607
  /** Last Slack verdict for the card, "<code>: <messages>", 500 chars max. */
598
608
  deliveryLastError?: string;
609
+ /** Tree rows only: set once the thread has no render work left. */
610
+ closedAt?: string;
599
611
  createdAt: string;
600
612
  updatedAt: string;
601
613
  }
@@ -628,6 +640,7 @@ type SlackMessageRow = {
628
640
  delivery_attempts: number;
629
641
  delivery_abandoned_at: string | null;
630
642
  delivery_last_error: string | null;
643
+ closed_at: string | null;
631
644
  created_at: string;
632
645
  updated_at: string;
633
646
  };
@@ -650,6 +663,7 @@ function rowToSlackMessage(row: SlackMessageRow): SlackMessageRecord {
650
663
  deliveryAttempts: row.delivery_attempts,
651
664
  deliveryAbandonedAt: row.delivery_abandoned_at ?? undefined,
652
665
  deliveryLastError: row.delivery_last_error ?? undefined,
666
+ closedAt: row.closed_at ?? undefined,
653
667
  createdAt: row.created_at,
654
668
  updatedAt: row.updated_at,
655
669
  };
@@ -872,15 +886,20 @@ export async function getSlackOutcomeMessage(taskId: string): Promise<SlackMessa
872
886
  *
873
887
  * CROSS JOIN and `+wake.taskType` pin the join order to the small chain. Without
874
888
  * them the planner drives the outer join from every terminal task (~1s at 150k tasks).
889
+ * The anchor drives from the partial index idx_agent_tasks_deferred (tasks
890
+ * that ever deferred), not from every outcome card: `deferral_resolved_at IS
891
+ * NULL` stays true forever on cards of tasks that never deferred.
892
+ * `+child.taskType` does the same for the NOT EXISTS: without it the planner
893
+ * walks every deferred-type task through idx_agent_tasks_list_filter per row.
875
894
  */
876
895
  export async function getResolvableDeferralOutcomes(
877
896
  limit = 50,
878
897
  ): Promise<{ card: SlackMessageRecord; deferredTaskId: string; wakeTaskId: string }[]> {
879
898
  const rows = await getDbClient().query<SlackMessageRow & { wake_task_id: string }>(
880
899
  `WITH RECURSIVE chain(card_id, task_id) AS (
881
- SELECT card.id, deferred.id FROM slack_messages card
882
- JOIN agent_tasks deferred ON deferred.id = card.task_id
883
- WHERE card.kind = 'outcome' AND deferred.deferredAt IS NOT NULL
900
+ SELECT card.id, deferred.id FROM agent_tasks deferred
901
+ CROSS JOIN slack_messages card ON card.task_id = deferred.id AND card.kind = 'outcome'
902
+ WHERE deferred.deferredAt IS NOT NULL
884
903
  AND card.deferral_resolved_at IS NULL AND card.finalized_at IS NOT NULL
885
904
  AND card.ts NOT LIKE 'pending:%'
886
905
  UNION
@@ -898,7 +917,7 @@ export async function getResolvableDeferralOutcomes(
898
917
  AND card.ts NOT LIKE 'pending:%'
899
918
  AND NOT EXISTS (
900
919
  SELECT 1 FROM agent_tasks child
901
- WHERE child.parentTaskId = wake.id AND child.taskType = 'deferred'
920
+ WHERE child.parentTaskId = wake.id AND +child.taskType = 'deferred'
902
921
  )
903
922
  AND (wake.deferredAt IS NULL OR wake.finishedAt > card.updated_at)
904
923
  AND wake.status IN ('completed', 'failed', 'cancelled', 'skipped')
@@ -968,57 +987,26 @@ export async function abandonSlackOutcomeDelivery(
968
987
  return row ? rowToSlackMessage(row) : null;
969
988
  }
970
989
 
971
- export async function getSlackTreeMessages(): Promise<SlackMessageRecord[]> {
990
+ /**
991
+ * Trees with pending render work. Reads only open trees (closed_at IS NULL,
992
+ * idx_slack_messages_open_trees): a settled thread is closed after its render
993
+ * pass and reopened by migration 278's triggers on any task or outcome write
994
+ * in the thread. `thread` scopes the sweep to one thread (event-driven render).
995
+ */
996
+ export async function getSlackTreeMessages(
997
+ options: { thread?: { channelId: string; threadTs: string } } = {},
998
+ ): Promise<SlackMessageRecord[]> {
999
+ const thread = options.thread;
972
1000
  const rows = await getDbClient().query<SlackMessageRow>(
973
1001
  `SELECT tree.*
974
1002
  FROM slack_messages tree
975
1003
  JOIN slack_render_v2_state state ON state.id = 1
976
1004
  WHERE tree.kind = 'tree'
977
- AND (
978
- (tree.ts LIKE 'pending:%' AND tree.created_at >= state.activated_at)
979
- OR EXISTS (
980
- SELECT 1 FROM agent_tasks task
981
- WHERE task.slackChannelId = tree.channel_id
982
- AND task.slackThreadTs = tree.thread_ts
983
- AND task.source = 'slack'
984
- AND task.createdAt >= state.activated_at
985
- AND task.status IN ('completed', 'failed', 'cancelled')
986
- AND NOT EXISTS (
987
- SELECT 1 FROM slack_messages outcome
988
- WHERE outcome.kind = 'outcome'
989
- AND outcome.task_id = task.id
990
- AND (outcome.finalized_at IS NOT NULL OR outcome.delivery_abandoned_at IS NOT NULL)
991
- )
992
- )
993
- OR
994
- EXISTS (
995
- SELECT 1 FROM agent_tasks task
996
- WHERE task.slackChannelId = tree.channel_id
997
- AND task.slackThreadTs = tree.thread_ts
998
- AND task.createdAt >= state.activated_at
999
- AND task.status NOT IN ('completed', 'failed', 'cancelled', 'superseded', 'skipped')
1000
- )
1001
- OR EXISTS (
1002
- SELECT 1 FROM agent_tasks task
1003
- WHERE task.slackChannelId = tree.channel_id
1004
- AND task.slackThreadTs = tree.thread_ts
1005
- AND task.createdAt >= state.activated_at
1006
- AND task.lastUpdatedAt > tree.updated_at
1007
- )
1008
- OR EXISTS (
1009
- SELECT 1 FROM slack_messages outcome
1010
- JOIN agent_tasks task ON task.id = outcome.task_id
1011
- WHERE outcome.kind = 'outcome'
1012
- AND outcome.channel_id = tree.channel_id
1013
- AND outcome.thread_ts = tree.thread_ts
1014
- AND task.createdAt >= state.activated_at
1015
- AND (
1016
- (outcome.finalized_at IS NULL AND outcome.delivery_abandoned_at IS NULL)
1017
- OR outcome.updated_at > tree.updated_at
1018
- )
1019
- )
1020
- )
1005
+ AND tree.closed_at IS NULL
1006
+ ${thread ? "AND tree.channel_id = ? AND tree.thread_ts = ?" : ""}
1007
+ AND ${SLACK_TREE_NEEDS_RENDER_SQL}
1021
1008
  ORDER BY tree.created_at ASC`,
1009
+ thread ? [thread.channelId, thread.threadTs] : [],
1022
1010
  );
1023
1011
  return rows.map(rowToSlackMessage);
1024
1012
  }
@@ -2621,32 +2609,44 @@ export async function createTaskExtended(
2621
2609
  // Webhook handlers locate active work via `findTaskByVcs(repo, number, provider)`,
2622
2610
  // so a resume / follow-up child MUST carry the full VCS identity or
2623
2611
  // subsequent review/update events get dropped.
2624
- if (parent.vcsProvider && !options.vcsProvider) {
2625
- options.vcsProvider = parent.vcsProvider;
2626
- }
2627
- if (parent.vcsRepo && !options.vcsRepo) {
2628
- options.vcsRepo = parent.vcsRepo;
2629
- }
2630
- if (parent.vcsNumber != null && options.vcsNumber == null) {
2631
- options.vcsNumber = parent.vcsNumber;
2632
- }
2633
- if (parent.vcsEventType && !options.vcsEventType) {
2634
- options.vcsEventType = parent.vcsEventType;
2635
- }
2636
- if (parent.vcsCommentId != null && options.vcsCommentId == null) {
2637
- options.vcsCommentId = parent.vcsCommentId;
2638
- }
2639
- if (parent.vcsAuthor && !options.vcsAuthor) {
2640
- options.vcsAuthor = parent.vcsAuthor;
2641
- }
2642
- if (parent.vcsUrl && !options.vcsUrl) {
2643
- options.vcsUrl = parent.vcsUrl;
2644
- }
2645
- if (parent.vcsInstallationId != null && options.vcsInstallationId == null) {
2646
- options.vcsInstallationId = parent.vcsInstallationId;
2647
- }
2648
- if (parent.vcsNodeId && !options.vcsNodeId) {
2649
- options.vcsNodeId = parent.vcsNodeId;
2612
+ //
2613
+ // Inherited as an atomic unit, like Slack context above: a child that
2614
+ // names its own vcsRepo different from the parent's (e.g. `send-task
2615
+ // vcsRepo=...`) inherits NONE of the parent's vcs* fields. Otherwise it
2616
+ // would pair its repo with the parent's number/URL, so PR reconciliation
2617
+ // attaches the parent's PR as the child's deliverable and
2618
+ // `findTaskByVcs` matches an unrelated PR with the same number.
2619
+ const inheritParentVcs =
2620
+ !options.vcsRepo ||
2621
+ (!!parent.vcsRepo && options.vcsRepo.toLowerCase() === parent.vcsRepo.toLowerCase());
2622
+ if (inheritParentVcs) {
2623
+ if (parent.vcsProvider && !options.vcsProvider) {
2624
+ options.vcsProvider = parent.vcsProvider;
2625
+ }
2626
+ if (parent.vcsRepo && !options.vcsRepo) {
2627
+ options.vcsRepo = parent.vcsRepo;
2628
+ }
2629
+ if (parent.vcsNumber != null && options.vcsNumber == null) {
2630
+ options.vcsNumber = parent.vcsNumber;
2631
+ }
2632
+ if (parent.vcsEventType && !options.vcsEventType) {
2633
+ options.vcsEventType = parent.vcsEventType;
2634
+ }
2635
+ if (parent.vcsCommentId != null && options.vcsCommentId == null) {
2636
+ options.vcsCommentId = parent.vcsCommentId;
2637
+ }
2638
+ if (parent.vcsAuthor && !options.vcsAuthor) {
2639
+ options.vcsAuthor = parent.vcsAuthor;
2640
+ }
2641
+ if (parent.vcsUrl && !options.vcsUrl) {
2642
+ options.vcsUrl = parent.vcsUrl;
2643
+ }
2644
+ if (parent.vcsInstallationId != null && options.vcsInstallationId == null) {
2645
+ options.vcsInstallationId = parent.vcsInstallationId;
2646
+ }
2647
+ if (parent.vcsNodeId && !options.vcsNodeId) {
2648
+ options.vcsNodeId = parent.vcsNodeId;
2649
+ }
2650
2650
  }
2651
2651
 
2652
2652
  // Execution context (per-task overrides)
@@ -6034,6 +6034,7 @@ type ScheduledTaskRow = {
6034
6034
  lastErrorMessage: string | null;
6035
6035
  model: string | null;
6036
6036
  modelTier: string | null;
6037
+ profile: string | null;
6037
6038
  scheduleType: string;
6038
6039
  targetType: string;
6039
6040
  workflowId: string | null;
@@ -6086,6 +6087,7 @@ function rowToScheduledTask(row: ScheduledTaskRow): ScheduledTask {
6086
6087
  lastErrorMessage: row.lastErrorMessage ?? undefined,
6087
6088
  model: row.model ?? undefined,
6088
6089
  modelTier: parseModelTier(row.modelTier) ?? undefined,
6090
+ profile: row.profile ?? undefined,
6089
6091
  scheduleType: row.scheduleType as "recurring" | "one_time",
6090
6092
  targetType: row.targetType as "agent-task" | "workflow" | "script",
6091
6093
  workflowId: row.workflowId ?? undefined,
@@ -6247,6 +6249,7 @@ export interface CreateScheduledTaskData {
6247
6249
  timezone?: string;
6248
6250
  model?: string;
6249
6251
  modelTier?: ModelTier;
6252
+ profile?: string;
6250
6253
  scheduleType?: "recurring" | "one_time";
6251
6254
  targetType?: "agent-task" | "workflow" | "script";
6252
6255
  workflowId?: string;
@@ -6267,11 +6270,11 @@ export async function createScheduledTask(data: CreateScheduledTaskData): Promis
6267
6270
  id, "key", name, description, cronExpression, intervalMs, taskTemplate,
6268
6271
  taskType, tags, priority, targetAgentId, enabled, nextRunAt,
6269
6272
  createdByAgentId, parentTaskId, requestedDelayMs, requestedRunAt,
6270
- timezone, model, modelTier, scheduleType,
6273
+ timezone, model, modelTier, profile, scheduleType,
6271
6274
  targetType, workflowId, scriptName, scriptArgs, params, requiredParams, requires,
6272
6275
  createdAt, lastUpdatedAt,
6273
6276
  created_by, updated_by
6274
- ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING *`,
6277
+ ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING *`,
6275
6278
  [
6276
6279
  id,
6277
6280
  normalizeAssetKey(data.key ?? defaultAssetKey("schedule", id)),
@@ -6293,6 +6296,7 @@ export async function createScheduledTask(data: CreateScheduledTaskData): Promis
6293
6296
  data.timezone ?? "UTC",
6294
6297
  data.model ?? null,
6295
6298
  data.modelTier ?? null,
6299
+ data.profile ?? null,
6296
6300
  data.scheduleType ?? "recurring",
6297
6301
  data.targetType ?? "agent-task",
6298
6302
  data.workflowId ?? null,
@@ -6332,6 +6336,7 @@ export interface UpdateScheduledTaskData {
6332
6336
  lastErrorMessage?: string | null;
6333
6337
  model?: string | null;
6334
6338
  modelTier?: ModelTier | null;
6339
+ profile?: string | null;
6335
6340
  scheduleType?: "recurring" | "one_time";
6336
6341
  targetType?: "agent-task" | "workflow" | "script";
6337
6342
  workflowId?: string | null;
@@ -6428,6 +6433,10 @@ export async function updateScheduledTask(
6428
6433
  updates.push("modelTier = ?");
6429
6434
  params.push(data.modelTier);
6430
6435
  }
6436
+ if (data.profile !== undefined) {
6437
+ updates.push("profile = ?");
6438
+ params.push(data.profile);
6439
+ }
6431
6440
  if (data.scheduleType !== undefined) {
6432
6441
  updates.push("scheduleType = ?");
6433
6442
  params.push(data.scheduleType);
@@ -7955,6 +7964,7 @@ async function deleteWorkflowRows(id: string, source?: "api" | "mcp"): Promise<b
7955
7964
  );
7956
7965
  // 3. Delete runs (they reference workflow)
7957
7966
  await client.run("DELETE FROM workflow_runs WHERE workflowId = ?", [id]);
7967
+ await deleteOrphanedRunDefinitions();
7958
7968
  await reclassifyTaskHumanFree(linkedTaskIds);
7959
7969
  // 4. Delete every user's favorite of it (no FK to the workflow)
7960
7970
  await client.run("DELETE FROM user_favorites WHERE itemType = 'workflow' AND itemId = ?", [id]);
@@ -8007,10 +8017,12 @@ export async function createWorkflowRun(data: {
8007
8017
  parentStepAttempt?: number;
8008
8018
  /** Server-stamped agent that started the run (migration 247). Never from trigger data. */
8009
8019
  triggeredByAgentId?: string;
8020
+ /** The `workflow_run_definitions` row the run is pinned to (migration 283). */
8021
+ definitionHash?: string;
8010
8022
  }): Promise<WorkflowRun> {
8011
8023
  const now = new Date().toISOString();
8012
8024
  const row = await getDbClient().get<WorkflowRunRow>(
8013
- `INSERT INTO workflow_runs (id, workflowId, triggerType, startedAt, triggerData, created_by, parentStepId, parentStepAttempt, triggeredByAgentId) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING *`,
8025
+ `INSERT INTO workflow_runs (id, workflowId, triggerType, startedAt, triggerData, created_by, parentStepId, parentStepAttempt, triggeredByAgentId, definitionHash) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING *`,
8014
8026
  [
8015
8027
  data.id,
8016
8028
  data.workflowId,
@@ -8021,6 +8033,7 @@ export async function createWorkflowRun(data: {
8021
8033
  data.parentStepId ?? null,
8022
8034
  data.parentStepAttempt ?? 0,
8023
8035
  data.triggeredByAgentId ?? null,
8036
+ data.definitionHash ?? null,
8024
8037
  ],
8025
8038
  );
8026
8039
  if (!row) throw new Error("Failed to create workflow run");
@@ -13895,16 +13908,35 @@ export async function sweepExpiredKv(namespace: string, now = Date.now()): Promi
13895
13908
  /** Delete expired entries across a namespace family (`prefix` and
13896
13909
  * `prefix:*`). Used by per-agent internal stores whose inactive owners may
13897
13910
  * never return to trigger a namespace-local sweep. */
13898
- export async function sweepExpiredKvPrefix(prefix: string, now = Date.now()): Promise<number> {
13911
+ export async function sweepExpiredKvPrefix(
13912
+ prefix: string,
13913
+ now = Date.now(),
13914
+ /** Rows per DELETE, capped at 50 for multi-MB values. Yields between batches. */
13915
+ batchSize = 50,
13916
+ ): Promise<number> {
13917
+ batchSize = Math.max(1, Math.min(batchSize, 50));
13899
13918
  const escaped = prefix.replace(/[\\%_]/g, "\\$&");
13900
- const result = await getDbClient().run(
13901
- `DELETE FROM kv_entries
13902
- WHERE (namespace = ? OR namespace LIKE ? ESCAPE '\\')
13903
- AND expires_at IS NOT NULL
13904
- AND expires_at <= ?`,
13905
- [prefix, `${escaped}:%`, now],
13906
- );
13907
- return result.changes;
13919
+ // Batched, with a macrotask yield between batches: one DELETE of every
13920
+ // expired multi-MB overflow row blocked the event loop for seconds and
13921
+ // starved the Slack socket ping. `now` is fixed for the whole sweep, so the
13922
+ // batches delete the same row set as one statement. kv_entries is WITHOUT
13923
+ // ROWID, so a batch selects the (namespace, key) primary key.
13924
+ let removed = 0;
13925
+ for (;;) {
13926
+ const result = await getDbClient().run(
13927
+ `DELETE FROM kv_entries
13928
+ WHERE (namespace, key) IN (
13929
+ SELECT namespace, key FROM kv_entries
13930
+ WHERE (namespace = ? OR namespace LIKE ? ESCAPE '\\')
13931
+ AND expires_at IS NOT NULL
13932
+ AND expires_at <= ?
13933
+ LIMIT ?)`,
13934
+ [prefix, `${escaped}:%`, now, batchSize],
13935
+ );
13936
+ removed += result.changes;
13937
+ if (result.changes < batchSize) return removed;
13938
+ await new Promise<void>((resolve) => setImmediate(resolve));
13939
+ }
13908
13940
  }
13909
13941
 
13910
13942
  /**
@@ -0,0 +1,146 @@
1
+ /**
2
+ * Event-loop stall attribution.
3
+ *
4
+ * Synchronous SQLite runs on the main thread, and a chain of awaited DB calls
5
+ * resolves in the microtask queue, so the whole chain is one macrotask. A
6
+ * stall over 5 s makes the Slack Socket Mode client miss its pong. The OTel
7
+ * `nodejs.eventloop.delay.max` histogram shows that a stall happened, but not
8
+ * when or which code path ran it.
9
+ *
10
+ * This module adds:
11
+ * - a breadcrumb: `withLoopPath(path, fn)` names the entry point (HTTP route,
12
+ * MCP tool, timer) on an AsyncLocalStorage that follows `await`;
13
+ * - a bounded ring of the DB statements run since the last probe tick, fed by
14
+ * the DB client (`recordStatement`);
15
+ * - a `setTimeout` lag probe. A stall of N ms delays its tick by about N ms.
16
+ * The first tick after the stall sees exactly the statements of the stalled
17
+ * macrotask and logs one `[event-loop] stall` line.
18
+ */
19
+ import { AsyncLocalStorage } from "node:async_hooks";
20
+ import { scrubSecrets } from "../utils/secret-scrubber";
21
+
22
+ const pathContext = new AsyncLocalStorage<string>();
23
+
24
+ /** Run `fn` with `path` as the entry point of everything it starts. */
25
+ export function withLoopPath<T>(path: string, fn: () => T): T {
26
+ return pathContext.run(path, fn);
27
+ }
28
+
29
+ /** Wrap `fn` so each call runs with `path` as its entry point. */
30
+ export function bindLoopPath<A extends unknown[], R>(
31
+ path: string,
32
+ fn: (...args: A) => R,
33
+ ): (...args: A) => R {
34
+ return (...args) => pathContext.run(path, fn, ...args);
35
+ }
36
+
37
+ /** Entry point of the code that runs now, or `unknown`. */
38
+ export function currentLoopPath(): string {
39
+ return pathContext.getStore() ?? "unknown";
40
+ }
41
+
42
+ type RecordedStatement = { path: string; sql: string; ms: number };
43
+
44
+ /** Statements shorter than this cannot make a stall worth a log line. */
45
+ const STATEMENT_RECORD_MIN_MS = 10;
46
+ const STATEMENT_RING_MAX = 256;
47
+ const ring: RecordedStatement[] = [];
48
+
49
+ /**
50
+ * Called by the DB client for each driver call. Stores a reference to the SQL
51
+ * string only; the label is built only when a stall line is logged.
52
+ */
53
+ export function recordStatement(sql: string, ms: number): void {
54
+ if (ms < STATEMENT_RECORD_MIN_MS) return;
55
+ if (ring.length === STATEMENT_RING_MAX) ring.shift();
56
+ ring.push({ path: currentLoopPath(), sql, ms });
57
+ }
58
+
59
+ const PROBE_MS = 100;
60
+ const DEFAULT_STALL_WARN_MS = 1000;
61
+ const TOP_STATEMENTS = 3;
62
+ const LABEL_MAX_CHARS = 60;
63
+
64
+ /** Read per tick, so a config reload applies without a restart. */
65
+ function stallWarnMs(): number {
66
+ const raw = Number(process.env.EVENT_LOOP_STALL_WARN_MS);
67
+ return Number.isFinite(raw) && raw > 0 ? raw : DEFAULT_STALL_WARN_MS;
68
+ }
69
+
70
+ /**
71
+ * Scrub the complete SQL before truncation: a secret that straddles the cut
72
+ * would otherwise leave a partial token that no pattern matches.
73
+ */
74
+ function statementLabel(sql: string): string {
75
+ return scrubSecrets(sql.replace(/\s+/g, " ").trim()).slice(0, LABEL_MAX_CHARS);
76
+ }
77
+
78
+ /**
79
+ * Build the stall line from the statements recorded during the stall.
80
+ * Params are never logged: they can carry secrets and user content.
81
+ */
82
+ export function formatStallLine(lagMs: number, statements: readonly RecordedStatement[]): string {
83
+ let dbMs = 0;
84
+ const perPath = new Map<string, number>();
85
+ for (const s of statements) {
86
+ dbMs += s.ms;
87
+ perPath.set(s.path, (perPath.get(s.path) ?? 0) + 1);
88
+ }
89
+ const top = [...statements].sort((a, b) => b.ms - a.ms).slice(0, TOP_STATEMENTS);
90
+ const path = top[0]?.path ?? "unknown";
91
+ const parts = [
92
+ `[event-loop] stall ${Math.round(lagMs)}ms`,
93
+ `path=${path}`,
94
+ `statements=${statements.length}`,
95
+ `db_ms=${Math.round(dbMs)}`,
96
+ ];
97
+ if (top.length > 0) {
98
+ parts.push(`top=${top.map((s) => `${Math.round(s.ms)}ms:${statementLabel(s.sql)}`).join("|")}`);
99
+ parts.push(
100
+ `paths=${[...perPath]
101
+ .sort((a, b) => b[1] - a[1])
102
+ .map(([p, n]) => `${p}:${n}`)
103
+ .join(",")}`,
104
+ );
105
+ }
106
+ // Paths are labels from code, but scrub the complete line at egress anyway.
107
+ return scrubSecrets(parts.join(" "));
108
+ }
109
+
110
+ let stopProbe: (() => void) | null = null;
111
+
112
+ /**
113
+ * Start the lag probe. A `setTimeout` chain, not `setInterval`: missed
114
+ * intervals coalesce and lose the lag value. Returns a stop function.
115
+ */
116
+ export function startEventLoopLagProbe(probeMs = PROBE_MS): () => void {
117
+ if (stopProbe) return stopProbe;
118
+ let timer: ReturnType<typeof setTimeout>;
119
+ let expected = performance.now() + probeMs;
120
+ const schedule = () => {
121
+ timer = setTimeout(tick, probeMs);
122
+ timer.unref?.();
123
+ };
124
+ const tick = () => {
125
+ const now = performance.now();
126
+ const lag = now - expected;
127
+ if (lag >= stallWarnMs()) console.warn(formatStallLine(lag, ring));
128
+ ring.length = 0;
129
+ expected = now + probeMs;
130
+ schedule();
131
+ };
132
+ // The probe owns no entry point: its own ticks must not inherit the caller's.
133
+ pathContext.exit(schedule);
134
+ const stop = () => {
135
+ clearTimeout(timer);
136
+ ring.length = 0;
137
+ if (stopProbe === stop) stopProbe = null;
138
+ };
139
+ stopProbe = stop;
140
+ return stop;
141
+ }
142
+
143
+ /** Stop the probe started by `startEventLoopLagProbe`, if any. */
144
+ export function stopEventLoopLagProbe(): void {
145
+ stopProbe?.();
146
+ }
@@ -1,5 +1,6 @@
1
1
  import { MCP_OVERFLOW_NAMESPACE } from "../kv-overflow";
2
2
  import { sweepExpiredKvPrefix } from "./db";
3
+ import { withLoopPath } from "./event-loop-lag";
3
4
 
4
5
  export const MCP_OVERFLOW_SWEEP_INTERVAL_MS = 10 * 60 * 1000;
5
6
 
@@ -26,7 +27,7 @@ export async function runMcpOverflowSweep(now = Date.now()): Promise<number> {
26
27
  export function startMcpOverflowSweep(intervalMs = MCP_OVERFLOW_SWEEP_INTERVAL_MS): void {
27
28
  if (sweepTimer) return;
28
29
  sweepTimer = setInterval(() => {
29
- void runMcpOverflowSweep();
30
+ void withLoopPath("mcp-overflow.sweep", () => runMcpOverflowSweep());
30
31
  }, intervalMs);
31
32
  if (typeof sweepTimer?.unref === "function") sweepTimer.unref();
32
33
  }
@@ -15,6 +15,11 @@ export const TTL_DEFAULTS: Record<AgentMemorySource, number | null> = {
15
15
  manual: null,
16
16
  };
17
17
 
18
+ // TTL (in days) of a `/learnings/*` memory, whatever its source. A learning is
19
+ // raw material: one worth keeping is distilled into `/longterm` (a move there
20
+ // clears the expiry); the rest age out.
21
+ export const LEARNING_TTL_DAYS = 30;
22
+
18
23
  // Per-source recency decay half-life (in days).
19
24
  // manual = Infinity (no decay — curated knowledge stays relevant forever).
20
25
  // A global MEMORY_RECENCY_HALF_LIFE_DAYS override forces ALL sources to the
@@ -78,6 +83,13 @@ export function minSimilarity(): number {
78
83
  // pair 0.705.
79
84
  export const LEARNING_DUPLICATE_MIN_SIMILARITY = 0.8;
80
85
 
86
+ // Most eligible rows an exhaustive search scores per call, newest `updatedAt`
87
+ // first. The learning dedup runs it on every store-progress learning (once per
88
+ // prefix), so its cost must not grow with the corpus. Below the cap the scan is
89
+ // exact; past it, older rows are not scored and the search logs `capped=true`.
90
+ // Production holds about 1,260 `/learnings` + `/longterm` rows.
91
+ export const EXHAUSTIVE_SEARCH_MAX_ROWS = 10_000;
92
+
81
93
  // Reranking parameters
82
94
  export function accessBoostMaxMultiplier(): number {
83
95
  return numEnv("MEMORY_ACCESS_BOOST_MAX", 1.5);
@@ -104,5 +116,6 @@ export const EMBEDDING_DIMENSIONS = numEnv("EMBEDDING_DIMENSIONS", 512);
104
116
  export const DEFAULT_EMBEDDING_DIMENSIONS = EMBEDDING_DIMENSIONS;
105
117
  export const DEFAULT_EMBEDDING_MODEL = "openai/text-embedding-3-small";
106
118
 
107
- // Manual memories must NEVER be deleted by automated processes (curator, GC, etc.)
119
+ // Manual memories must NEVER be deleted by automated processes (curator, GC, etc.).
120
+ // The one exception is the expiry purge of `/learnings/*` rows (LEARNING_TTL_DAYS).
108
121
  export const PROTECTED_SOURCES: ReadonlySet<AgentMemorySource> = new Set(["manual"]);
@@ -15,6 +15,9 @@ import type { AgentMemorySource } from "@/types";
15
15
 
16
16
  export const LONGTERM_ROOT = "/longterm";
17
17
 
18
+ /** Where store-progress learnings live. They expire after LEARNING_TTL_DAYS (see `computeExpiresAt`). */
19
+ export const LEARNINGS_ROOT = "/learnings";
20
+
18
21
  /** Shape of a key written through `memory-store` or `memory-edit` `newKey`. */
19
22
  export const MEMORY_KEY_PATTERN = /^\/[a-z0-9-]+(\/[a-z0-9._-]+)*$/;
20
23
 
@@ -53,6 +56,11 @@ export function isLongtermKey(key: string | null | undefined): boolean {
53
56
  return !!key && isKeyUnderRoot(key, LONGTERM_ROOT);
54
57
  }
55
58
 
59
+ /** True when `key` sits beneath `/learnings`: a store-progress learning. */
60
+ export function isLearningKey(key: string | null | undefined): boolean {
61
+ return !!key && key.startsWith(`${LEARNINGS_ROOT}/`);
62
+ }
63
+
56
64
  /**
57
65
  * Why a `/longterm` key is refused, or null when it is allowed (or sits outside
58
66
  * `/longterm`, where keys stay free-form). Only the first segments are checked;