@desplega.ai/agent-swarm 2.7.10 → 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 (281) hide show
  1. package/dist/acp-adapter-t949vbbp.js +15 -0
  2. package/dist/{actions-b8aqhrsg.js → actions-7e9czc7s.js} +18 -19
  3. package/dist/{amp-adapter-4c165vgn.js → amp-adapter-gv6cbb7d.js} +2 -3
  4. package/dist/{app-m9khefrh.js → app-b9fbkp9v.js} +3 -3
  5. package/dist/{assistant-rh4v3ntz.js → assistant-wjb7v4c7.js} +20 -21
  6. package/dist/{boot-args-schema-hwkc8bzv.js → boot-args-schema-2t37e0fe.js} +1 -1
  7. package/dist/{boot-reembed-5y6yw9sj.js → boot-reembed-a35nkyr1.js} +1 -1
  8. package/dist/{boot-reembed-fdyk55h4.js → boot-reembed-gka3zas7.js} +1 -1
  9. package/dist/{boot-scrub-sweep-r5r8kqsn.js → boot-scrub-sweep-65sdnp6c.js} +3 -3
  10. package/dist/{boot-seeds-8f9zt5cm.js → boot-seeds-x42tzytw.js} +4 -5
  11. package/dist/{claude-adapter-s5df247c.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-9gsmwzyx.js → claude-managed-setup-9m6450zk.js} +2 -2
  14. package/dist/{claude-sdk-session-dq1j4nz9.js → claude-sdk-session-0q0ptaba.js} +9066 -8818
  15. package/dist/{cli-n5yexkdg.js → cli-0b4zam62.js} +1 -0
  16. package/dist/{cli-5wfz00be.js → cli-0jzra9ad.js} +2 -2
  17. package/dist/{cli-eqtfgfr6.js → cli-2gxeg5gr.js} +1 -1
  18. package/dist/{cli-eqk5p56g.js → cli-2wnq8jkz.js} +9 -9
  19. package/dist/{cli-hfkwwy1h.js → cli-2zp0et3v.js} +21 -2
  20. package/dist/{cli-rt0ag4x1.js → cli-334z18y3.js} +4 -4
  21. package/dist/{cli-6m6b4bep.js → cli-3rkdne1h.js} +2 -2
  22. package/dist/{cli-3xddv0rk.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-n07eecjs.js → cli-6t09sdhy.js} +3 -3
  26. package/dist/{cli-38yww4e9.js → cli-6wxjceza.js} +8 -8
  27. package/dist/{cli-5xcb8gxq.js → cli-7b8sbmag.js} +8 -8
  28. package/dist/{cli-3xj33nrx.js → cli-7cp6aa1q.js} +10 -3
  29. package/dist/{cli-mp9aa8g5.js → cli-7p1kvkck.js} +6 -6
  30. package/dist/{cli-w3bk0dgv.js → cli-8aes17e4.js} +2 -2
  31. package/dist/{cli-n9abj89w.js → cli-8fknbnhw.js} +8 -8
  32. package/dist/{cli-xd5ebp64.js → cli-8kzasymh.js} +3 -3
  33. package/dist/{cli-2tsfpc70.js → cli-a7wvp2km.js} +1 -1
  34. package/dist/{cli-kx4c8j05.js → cli-abejgen1.js} +63 -19
  35. package/dist/{cli-36wdfzgn.js → cli-bsywb58a.js} +8 -8
  36. package/dist/{cli-yz7v0sh4.js → cli-c55z5chy.js} +4 -6
  37. package/dist/{cli-18qt3few.js → cli-deefvewm.js} +1 -1
  38. package/dist/{cli-jg24scat.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-rjyghzg4.js → cli-g70vxtjh.js} +2 -2
  42. package/dist/{cli-g1px1g2e.js → cli-gxkx0zzp.js} +1 -1
  43. package/dist/{cli-j7d73g1y.js → cli-h38x262p.js} +3035 -354
  44. package/dist/{cli-zjphdj1a.js → cli-j790gp2w.js} +40 -18
  45. package/dist/{cli-f2zenhja.js → cli-kc7v1nfv.js} +1 -1
  46. package/dist/{cli-td33w98c.js → cli-m23zjcy2.js} +10 -10
  47. package/dist/{cli-8ewkreqd.js → cli-m28atx8q.js} +7 -9
  48. package/dist/{cli-x9exydey.js → cli-m4dqmnbp.js} +1 -1
  49. package/dist/{cli-ybjdc978.js → cli-mrt48bx7.js} +7 -4
  50. package/dist/{cli-mr97t7cc.js → cli-p1bq50aa.js} +263 -14
  51. package/dist/{cli-ttbqe3q4.js → cli-p1w18yhx.js} +1 -1
  52. package/dist/{cli-nj3x45rw.js → cli-p2gfst4m.js} +52 -43
  53. package/dist/{cli-z9cs5m0w.js → cli-pasjcnmj.js} +12 -12
  54. package/dist/{cli-an1knn2k.js → cli-qa4wdgsy.js} +5 -5
  55. package/dist/{cli-xncjscpm.js → cli-qfedvmcr.js} +50 -24
  56. package/dist/{cli-tmft5dz2.js → cli-qmqk2yc2.js} +1 -1
  57. package/dist/{cli-bxz823sz.js → cli-qsnnd04k.js} +1 -1
  58. package/dist/{cli-cr14kftt.js → cli-qwca7s44.js} +3 -3
  59. package/dist/{cli-7wz05bx3.js → cli-rfywm85n.js} +460 -737
  60. package/dist/{cli-v2fwbnsk.js → cli-rmmpt0wm.js} +6 -6
  61. package/dist/{cli-xbtq4d6s.js → cli-rzsy6nc6.js} +12 -8
  62. package/dist/{cli-0m7rtm11.js → cli-s7t6p9my.js} +1 -1
  63. package/dist/{cli-16phend6.js → cli-sdtt3men.js} +1 -1
  64. package/dist/{cli-pyrxt9gw.js → cli-tk7cjj8h.js} +1 -1
  65. package/dist/{cli-xvymtmv4.js → cli-tqegb8eq.js} +3 -3
  66. package/dist/{cli-kf3pxxdg.js → cli-v1dv1r37.js} +1 -1
  67. package/dist/{cli-h49ejvmc.js → cli-wcg8x116.js} +1 -1
  68. package/dist/{cli-q581b386.js → cli-wk18cm92.js} +5 -5
  69. package/dist/{cli-4vwxp550.js → cli-x1wa213s.js} +1 -1
  70. package/dist/{cli-jrp1dt73.js → cli-xzm9vcfg.js} +1 -1
  71. package/dist/{cli-eb2azq8n.js → cli-y97qxcfx.js} +9 -9
  72. package/dist/{cli-55bkx8hh.js → cli-yq3zak69.js} +1 -1
  73. package/dist/cli.js +17 -17
  74. package/dist/{codex-adapter-yr60sme6.js → codex-adapter-30j9bpzd.js} +5 -6
  75. package/dist/{codex-hook-fac6175r.js → codex-hook-h3vk6xzr.js} +1 -1
  76. package/dist/{codex-session-runner-pch8x584.js → codex-session-runner-8vp57v8w.js} +5 -6
  77. package/dist/{commands-phjfbkv0.js → commands-bngnz9g7.js} +3 -3
  78. package/dist/{controller-jkx994pz.js → controller-hxx8wwy1.js} +2 -2
  79. package/dist/{controllers-connect-c4448kwa.js → controllers-connect-e2b4gyxc.js} +19 -20
  80. package/dist/{cursor-adapter-ndebqnm8.js → cursor-adapter-n5nc3mfe.js} +1 -2
  81. package/dist/{db-ez6egt6v.js → db-bgm7vkc0.js} +3 -3
  82. package/dist/{db-ef32tbfc.js → db-c6np41ca.js} +10 -2
  83. package/dist/{devin-adapter-z46ep5ph.js → devin-adapter-xedz98zv.js} +1 -1
  84. package/dist/{dispatcher-g3tt9hs9.js → dispatcher-jnybck15.js} +17 -18
  85. package/dist/{dsh-adapter-tcyn3099.js → dsh-adapter-nrhbrjz8.js} +1 -2
  86. package/dist/{easter-egg-3d-e36hk5td.js → easter-egg-3d-533knw22.js} +1 -1
  87. package/dist/{embedding-space-4kyh5qqj.js → embedding-space-crsm2zp0.js} +6 -6
  88. package/dist/{embeddings-6a8v4a6t.js → embeddings-e7z3f27f.js} +1 -1
  89. package/dist/{execute-3cbg0twf.js → execute-b3mrb1mv.js} +1 -1
  90. package/dist/{external-channel-se646snz.js → external-channel-m77wf5z3.js} +2 -2
  91. package/dist/{fx-adapter-yw795hqq.js → fx-adapter-b6w0kf56.js} +7 -8
  92. package/dist/{grok-adapter-ttmcss2j.js → grok-adapter-4sqtebdk.js} +7 -8
  93. package/dist/handlers-kdjcyzdj.js +32 -0
  94. package/dist/heartbeat-xgb036sx.js +43 -0
  95. package/dist/{hook-g3nhexsy.js → hook-5h02c9ga.js} +6 -6
  96. package/dist/{hook-hq93y5ej.js → hook-xsk1jbha.js} +5 -5
  97. package/dist/{http-5a9p4h6y.js → http-txe7qhvn.js} +282 -140
  98. package/dist/{index-6haqt3v5.js → index-4a0670yh.js} +8917 -8476
  99. package/dist/index-835xdgnm.js +33 -0
  100. package/dist/index-d469300x.js +37 -0
  101. package/dist/index-rzqwp11g.js +32 -0
  102. package/dist/{index-s72rqgsx.js → index-tvfafeyy.js} +1 -1
  103. package/dist/{index-hgn3c961.js → index-vk63emp7.js} +978 -35
  104. package/dist/{index-3c5pxbge.js → index-ysvbhg5m.js} +1 -1
  105. package/dist/{keepalive-1j4n4dmk.js → keepalive-4zsrce78.js} +3 -3
  106. package/dist/lead-yqt47dkr.js +70 -0
  107. package/dist/{maintenance-dmghacy5.js → maintenance-86jvartk.js} +1 -1
  108. package/dist/{oauth-refresh-sweep-yy06c03c.js → oauth-refresh-sweep-j52kg9pr.js} +2 -2
  109. package/dist/{onboard-52f9t5dm.js → onboard-c2aj5p8q.js} +3 -3
  110. package/dist/{opencode-adapter-krnh9jkn.js → opencode-adapter-dsagkhtw.js} +5 -6
  111. package/dist/{otel-impl-xjqz6wjj.js → otel-impl-yyy203z8.js} +1 -1
  112. package/dist/{pi-mono-adapter-dbxyp0qv.js → pi-mono-adapter-546cd1cc.js} +9 -9
  113. package/dist/{pricing-refresh-ctdr74pd.js → pricing-refresh-1rgzqyzz.js} +2 -3
  114. package/dist/{rbac-roles-q0gr8eyj.js → rbac-roles-e70xfeyj.js} +4 -6
  115. package/dist/{rbac-roles-ehv8s10n.js → rbac-roles-q89qe8er.js} +3 -3
  116. package/dist/{registry-hpjfmcfz.js → registry-32b1mft6.js} +1 -1
  117. package/dist/{render-v2-5b6chvcq.js → render-v2-k6tt107q.js} +3 -3
  118. package/dist/{resume-5a5d2845.js → resume-cnnrjfxa.js} +6 -6
  119. package/dist/{runtime-khxszyfp.js → runtime-1hv06jsn.js} +1 -1
  120. package/dist/{session-access-gj0bjhqg.js → session-access-xkxkjw3n.js} +4 -4
  121. package/dist/{session-source-backfill-2sy0krfk.js → session-source-backfill-1d5pdwww.js} +2 -2
  122. package/dist/{setup-7ssfakk1.js → setup-se8q7gkc.js} +1 -1
  123. package/dist/{virtual-modules-pjj0knwf.js → virtual-modules-gazyapte.js} +3 -3
  124. package/dist/{work-objects-bf3syvhq.js → work-objects-1kxnbzy5.js} +20 -21
  125. package/dist/{worker-follow-up-38xevga6.js → worker-follow-up-zqm8apt4.js} +17 -18
  126. package/dist/worker-j6xxtq7p.js +70 -0
  127. package/openapi.json +334 -7
  128. package/package.json +4 -4
  129. package/src/apps/definition.ts +11 -0
  130. package/src/apps/store.ts +28 -8
  131. package/src/be/db/model-profiles.ts +147 -38
  132. package/src/be/db/tasks/read.ts +5 -1
  133. package/src/be/db/workflow-run-definitions.ts +117 -0
  134. package/src/be/db.ts +24 -3
  135. package/src/be/memory/constants.ts +14 -1
  136. package/src/be/memory/key-paths.ts +8 -0
  137. package/src/be/memory/providers/sqlite-store.ts +160 -10
  138. package/src/be/memory/task-learning.ts +79 -14
  139. package/src/be/memory/types.ts +24 -1
  140. package/src/be/migrations/279_backfill_learning_expiry.sql +15 -0
  141. package/src/be/migrations/281_model_profiles_pr2.sql +28 -0
  142. package/src/be/migrations/283_workflow_run_definitions.sql +14 -0
  143. package/src/be/model-profile-check.ts +284 -0
  144. package/src/be/model-tier-resolution.ts +112 -12
  145. package/src/be/multi-runtime.ts +27 -0
  146. package/src/be/runtime-pools/routing.ts +52 -4
  147. package/src/be/scripts/typecheck.ts +1 -0
  148. package/src/be/seed-scripts/catalog/dreaming-learnings.ts +552 -0
  149. package/src/be/seed-scripts/catalog/dreaming-memtax-create.ts +389 -14
  150. package/src/be/seed-scripts/catalog/dreaming-memtax.ts +6 -3
  151. package/src/be/seed-scripts/index.ts +9 -0
  152. package/src/be/steering.ts +8 -2
  153. package/src/commands/onboard/agent-fs-version.ts +1 -1
  154. package/src/commands/runner.ts +32 -1
  155. package/src/fs/provider.ts +17 -3
  156. package/src/heartbeat/heartbeat.ts +12 -1
  157. package/src/heartbeat/index.ts +1 -0
  158. package/src/http/agents.ts +56 -4
  159. package/src/http/apps.ts +3 -1
  160. package/src/http/config.ts +2 -6
  161. package/src/http/index.ts +2 -3
  162. package/src/http/model-profiles.ts +58 -10
  163. package/src/http/poll.ts +36 -3
  164. package/src/http/schedules.ts +42 -3
  165. package/src/http/tasks.ts +37 -12
  166. package/src/http/workflows.ts +15 -2
  167. package/src/prompts/session-templates.ts +1 -1
  168. package/src/providers/acp-targets.ts +11 -6
  169. package/src/providers/amp-adapter.ts +1 -1
  170. package/src/providers/claude-sdk-session.ts +132 -23
  171. package/src/providers/codex-skill-resolver.ts +10 -4
  172. package/src/providers/cursor-adapter.ts +1 -1
  173. package/src/providers/devin-skill-resolver.ts +5 -5
  174. package/src/providers/dsh-adapter.ts +1 -1
  175. package/src/scheduler/schedule-task.ts +1 -0
  176. package/src/scripts-runtime/sdk-allowlist.ts +1 -0
  177. package/src/scripts-runtime/swarm-sdk.ts +18 -1
  178. package/src/scripts-runtime/types/stdlib.d.ts +1 -0
  179. package/src/scripts-runtime/types/swarm-sdk.d.ts +1 -0
  180. package/src/server.ts +2 -0
  181. package/src/slack/app.ts +93 -0
  182. package/src/slack/inbound-dispatch.ts +18 -1
  183. package/src/slack/workspace-guard.ts +345 -0
  184. package/src/tasks/task-terminal-effects.ts +4 -0
  185. package/src/tasks/worker-follow-up.ts +102 -2
  186. package/src/tests/acp-adapter.test.ts +82 -0
  187. package/src/tests/apps-icon.test.ts +120 -0
  188. package/src/tests/chained-task-follow-up.test.ts +248 -0
  189. package/src/tests/check-pr-body.test.ts +40 -0
  190. package/src/tests/claude-sdk-lifecycle.test.ts +216 -1
  191. package/src/tests/claude-sdk-process.fixture.ts +57 -0
  192. package/src/tests/codex-skill-resolver.test.ts +31 -0
  193. package/src/tests/credentials-allowlist.test.ts +470 -0
  194. package/src/tests/db-task-read-contract.test.ts +5 -0
  195. package/src/tests/db-task-write-contract.test.ts +5 -0
  196. package/src/tests/deferred-wake-follow-up.test.ts +47 -0
  197. package/src/tests/docs-index-footprint.test.js +3 -3
  198. package/src/tests/dreaming-learnings.test.ts +1395 -0
  199. package/src/tests/dsh-adapter.test.ts +1 -1
  200. package/src/tests/explicit-model-validation.test.ts +2 -0
  201. package/src/tests/fs-provider.test.ts +42 -0
  202. package/src/tests/heartbeat.test.ts +28 -1
  203. package/src/tests/human-free-reclassify.test.ts +5 -1
  204. package/src/tests/list-endpoint-slimming.test.ts +17 -0
  205. package/src/tests/migration-283-workflow-run-definitions.test.ts +69 -0
  206. package/src/tests/model-profile-check.test.ts +387 -0
  207. package/src/tests/model-profiles-api.test.ts +528 -1
  208. package/src/tests/profile-claim.test.ts +415 -3
  209. package/src/tests/profile-tier-alias.test.ts +382 -0
  210. package/src/tests/schedule-profile.test.ts +359 -0
  211. package/src/tests/script-connections.test.ts +6 -0
  212. package/src/tests/script-per-script-executor.test.ts +1 -0
  213. package/src/tests/scripts-sdk-bridge-tool-failure.test.ts +141 -0
  214. package/src/tests/seed-scripts.test.ts +2 -2
  215. package/src/tests/slack-inbound-receipts.test.ts +22 -2
  216. package/src/tests/slack-socket-ack.test.ts +16 -1
  217. package/src/tests/slack-workspace-guard.test.ts +905 -0
  218. package/src/tests/steering-core.test.ts +40 -0
  219. package/src/tests/store-progress-learning.test.ts +605 -20
  220. package/src/tests/tool-annotations.test.ts +1 -1
  221. package/src/tests/workflow-convergence.test.ts +46 -40
  222. package/src/tests/workflow-definition-pinning-recovery.test.ts +552 -0
  223. package/src/tests/workflow-definition-pinning.test.ts +645 -0
  224. package/src/tests/workflow-executors.test.ts +16 -7
  225. package/src/tests/workflow-foreach.test.ts +43 -20
  226. package/src/tests/workflow-notify-slack-ready.test.ts +161 -0
  227. package/src/tests/workflow-profile.test.ts +328 -0
  228. package/src/tools/app-upsert.ts +1 -1
  229. package/src/tools/memory-edit.ts +2 -1
  230. package/src/tools/model-profiles/check-model-profile.ts +63 -0
  231. package/src/tools/schedules/create-schedule.ts +45 -4
  232. package/src/tools/schedules/patch-schedule.ts +50 -4
  233. package/src/tools/schedules/update-schedule.ts +50 -4
  234. package/src/tools/send-task.ts +17 -2
  235. package/src/tools/store-progress.ts +15 -3
  236. package/src/tools/task-action.ts +9 -1
  237. package/src/tools/templates.ts +13 -0
  238. package/src/tools/tool-config.ts +2 -1
  239. package/src/tools/utils.ts +61 -4
  240. package/src/tools/workflows/create-workflow.ts +1 -1
  241. package/src/tools/workflows/retry-workflow-run.ts +1 -1
  242. package/src/tools/workflows/update-workflow.ts +10 -1
  243. package/src/types.ts +56 -1
  244. package/src/utils/credentials.ts +89 -12
  245. package/src/workflows/engine.ts +10 -1
  246. package/src/workflows/executors/agent-task.ts +21 -2
  247. package/src/workflows/executors/foreach.ts +4 -1
  248. package/src/workflows/executors/notify.ts +62 -6
  249. package/src/workflows/executors/sub-workflow.ts +1 -1
  250. package/src/workflows/executors/swarm-script.ts +4 -1
  251. package/src/workflows/foreach-driver.ts +8 -5
  252. package/src/workflows/limits.ts +2 -1
  253. package/src/workflows/model-validation.ts +27 -2
  254. package/src/workflows/patch-definition.ts +3 -1
  255. package/src/workflows/recovery.ts +6 -6
  256. package/src/workflows/resume.ts +9 -9
  257. package/src/workflows/retry-poller.ts +2 -2
  258. package/templates/skills/apps/SKILL.md +2 -0
  259. package/templates/skills/apps/content.md +2 -0
  260. package/templates/skills/code-quality/content.md +12 -0
  261. package/templates/skills/memory/content.md +8 -0
  262. package/templates/skills/scheduling/config.json +1 -1
  263. package/templates/skills/scheduling/content.md +2 -2
  264. package/templates/skills/swarm-docs/content.md +4 -0
  265. package/templates/skills/swarm-extensions/content.md +1 -1
  266. package/templates/skills/swarm-scripts/SKILL.md +1 -1
  267. package/templates/skills/swarm-scripts/content.md +1 -1
  268. package/templates/skills/work-on-task/SKILL.md +2 -0
  269. package/templates/skills/work-on-task/content.md +2 -0
  270. package/templates/workflows/dreaming/content.md +174 -7
  271. package/dist/acp-adapter-srfhs1s9.js +0 -16
  272. package/dist/cli-bzvs88tv.js +0 -339
  273. package/dist/cli-cyn9v8zr.js +0 -748
  274. package/dist/cli-ed6yqpf1.js +0 -779
  275. package/dist/handlers-17wha3ze.js +0 -33
  276. package/dist/heartbeat-gwewgdms.js +0 -42
  277. package/dist/index-8arwq8mw.js +0 -34
  278. package/dist/index-tpw054az.js +0 -38
  279. package/dist/index-waq75j2j.js +0 -31
  280. package/dist/lead-re73kdgc.js +0 -72
  281. package/dist/worker-r8mz7a65.js +0 -72
@@ -380,7 +380,7 @@ describe("Tool Annotations & Classification", () => {
380
380
  // the six Chat SDK tools (chat-* + register/unregister-chat-channel),
381
381
  // and the seven app row tools plus app-action-run.
382
382
  expect(count).toBeGreaterThanOrEqual(45);
383
- expect(count).toBeLessThanOrEqual(175);
383
+ expect(count).toBeLessThanOrEqual(176);
384
384
  });
385
385
 
386
386
  test("core tools are fewer than deferred tools", () => {
@@ -461,46 +461,52 @@ describe("Workflow Convergence & Active Edge Tracking (Phase 2)", () => {
461
461
  expect(steps).toHaveLength(50);
462
462
  });
463
463
 
464
- test("exceeding MAX_ITERATIONS fails with 'node executions' message", async () => {
465
- // We set WORKFLOW_MAX_ITERATIONS env to a low number for this test
466
- const originalMax = process.env.WORKFLOW_MAX_ITERATIONS;
467
- process.env.WORKFLOW_MAX_ITERATIONS = "3";
468
-
469
- // Need to re-import to pick up env change — but since MAX_ITERATIONS is
470
- // read at module load, we test the error message pattern instead.
471
- // Build a chain of 5 nodes which will exceed limit of 3
472
- const registry = createTestRegistry();
473
- const def: WorkflowDefinition = {
474
- nodes: [
475
- { id: "a", type: "echo", config: { message: "1" }, next: "b" },
476
- { id: "b", type: "echo", config: { message: "2" }, next: "c" },
477
- { id: "c", type: "echo", config: { message: "3" }, next: "d" },
478
- { id: "d", type: "echo", config: { message: "4" }, next: "e" },
479
- { id: "e", type: "echo", config: { message: "5" } },
480
- ],
481
- };
482
-
483
- const workflow = await makeWorkflow(def);
484
- const runId = await startWorkflowExecution(workflow, {}, registry);
485
-
486
- const run = await getWorkflowRun(runId);
487
- // With MAX_ITERATIONS=100 (module-level constant), this should complete.
488
- // The env var is read at module load time, so we can't dynamically change it.
489
- // Instead, verify the workflow runs correctly at the default limit.
490
- // The error message format is verified structurally in the code.
491
- if (run!.status === "failed") {
492
- expect(run!.error).toContain("node executions");
493
- } else {
494
- expect(run!.status).toBe("completed");
495
- }
496
-
497
- // Restore
498
- if (originalMax !== undefined) {
499
- process.env.WORKFLOW_MAX_ITERATIONS = originalMax;
500
- } else {
501
- delete process.env.WORKFLOW_MAX_ITERATIONS;
502
- }
503
- });
464
+ for (const [value, limit] of [
465
+ [undefined, 100],
466
+ ["", 100],
467
+ [" ", 100],
468
+ ["Infinity", 100],
469
+ ["-Infinity", 100],
470
+ ["NaN", 100],
471
+ ["invalid", 100],
472
+ ["0", 100],
473
+ ["-1", 100],
474
+ ["1.5", 100],
475
+ ["1", 1],
476
+ ["3", 3],
477
+ ["101", 101],
478
+ ] as const) {
479
+ test(`WORKFLOW_MAX_ITERATIONS=${value} stops execution at ${limit} nodes`, async () => {
480
+ const originalMax = process.env.WORKFLOW_MAX_ITERATIONS;
481
+ if (value === undefined) delete process.env.WORKFLOW_MAX_ITERATIONS;
482
+ else process.env.WORKFLOW_MAX_ITERATIONS = value;
483
+
484
+ try {
485
+ const registry = createTestRegistry();
486
+ // A finite chain exposes a disabled breaker without hanging the test.
487
+ const def: WorkflowDefinition = {
488
+ nodes: Array.from({ length: limit + 1 }, (_, i) => ({
489
+ id: `n${i}`,
490
+ type: "echo",
491
+ config: { message: `step ${i}` },
492
+ ...(i < limit ? { next: `n${i + 1}` } : {}),
493
+ })),
494
+ };
495
+ const workflow = await makeWorkflow(def);
496
+ const runId = await startWorkflowExecution(workflow, {}, registry);
497
+
498
+ const run = await getWorkflowRun(runId);
499
+ expect(run?.status).toBe("failed");
500
+ expect(run?.error).toContain(`Max node executions (${limit}) exceeded`);
501
+ const steps = await getWorkflowRunStepsByRunId(runId);
502
+ expect(steps).toHaveLength(limit);
503
+ expect(steps.every((step) => step.status === "completed")).toBe(true);
504
+ } finally {
505
+ if (originalMax === undefined) delete process.env.WORKFLOW_MAX_ITERATIONS;
506
+ else process.env.WORKFLOW_MAX_ITERATIONS = originalMax;
507
+ }
508
+ });
509
+ }
504
510
  });
505
511
 
506
512
  // ─── nextPort persistence ──────────────────────────────────
@@ -0,0 +1,552 @@
1
+ import { afterAll, beforeAll, describe, expect, test } from "bun:test";
2
+ import { unlink } from "node:fs/promises";
3
+ import { z } from "zod";
4
+ import * as db from "../be/db";
5
+ import {
6
+ closeDb,
7
+ completeTask,
8
+ createAgent,
9
+ createWorkflow,
10
+ createWorkflowRun,
11
+ createWorkflowRunStep,
12
+ failTask,
13
+ getDbClient,
14
+ getTaskByWorkflowRunStepId,
15
+ getWaitStateByStepId,
16
+ getWorkflowRun,
17
+ getWorkflowRunStepsByRunId,
18
+ initDb,
19
+ pinWorkflowDefinition,
20
+ resolveApprovalRequest,
21
+ updateWorkflowRun,
22
+ updateWorkflowRunStep,
23
+ } from "../be/db";
24
+ import type {
25
+ ExecutorMeta,
26
+ Workflow,
27
+ WorkflowDefinition,
28
+ WorkflowRun,
29
+ WorkflowRunStep,
30
+ } from "../types";
31
+ import { startWorkflowExecution } from "../workflows/engine";
32
+ import { workflowEventBus } from "../workflows/event-bus";
33
+ import {
34
+ BaseExecutor,
35
+ type ExecutorDependencies,
36
+ type ExecutorResult,
37
+ } from "../workflows/executors/base";
38
+ import { createExecutorRegistry } from "../workflows/executors/registry";
39
+ import { drainForeachDrives, runForeachDrive } from "../workflows/foreach-driver";
40
+ import { recoverIncompleteRuns } from "../workflows/recovery";
41
+ import { resumeWaitState, setupWorkflowResumeListener } from "../workflows/resume";
42
+ import { snapshotAndUpdateWorkflow } from "../workflows/version";
43
+
44
+ // Each test starts a run on definition A, edits the workflow to B through the
45
+ // real edit path, then triggers one resume or recovery path. A routes the
46
+ // resumed node to `a2` (or a renamed node) with A's config; B routes it to
47
+ // `b2` with B's config. Every step after the edit must follow A.
48
+
49
+ const TEST_DB_PATH = "./test-workflow-definition-pinning-recovery.sqlite";
50
+
51
+ const deps: ExecutorDependencies = {
52
+ db,
53
+ eventBus: workflowEventBus,
54
+ interpolate: (t: string) => t,
55
+ };
56
+ const registry = createExecutorRegistry(deps);
57
+
58
+ /** Every `test-record` execution: which run, which node, which config value. */
59
+ const records: Array<{ runId: string; nodeId: string; value: string }> = [];
60
+
61
+ class RecordExecutor extends BaseExecutor<z.ZodObject, z.ZodObject> {
62
+ readonly type = "test-record";
63
+ readonly mode = "instant" as const;
64
+ readonly configSchema = z.object({ value: z.string() });
65
+ readonly outputSchema = z.object({ value: z.string() });
66
+
67
+ protected async execute(
68
+ config: { value: string },
69
+ _context: Readonly<Record<string, unknown>>,
70
+ meta: ExecutorMeta,
71
+ ): Promise<ExecutorResult<{ value: string }>> {
72
+ records.push({ runId: meta.runId, nodeId: meta.nodeId, value: config.value });
73
+ return { status: "success", output: { value: config.value } };
74
+ }
75
+ }
76
+ registry.register(new RecordExecutor(deps));
77
+
78
+ /** An approval node that only opens a request; nothing delivers its resolution live. */
79
+ class MockApprovalExecutor extends BaseExecutor<z.ZodObject, z.ZodObject> {
80
+ readonly type = "test-approval";
81
+ readonly mode = "async" as const;
82
+ readonly configSchema = z.object({ title: z.string() });
83
+ readonly outputSchema = z.object({
84
+ requestId: z.string(),
85
+ status: z.string(),
86
+ responses: z.record(z.string(), z.unknown()).nullable(),
87
+ });
88
+ lastRequestId: string | null = null;
89
+
90
+ protected async execute(
91
+ config: { title: string },
92
+ _context: Readonly<Record<string, unknown>>,
93
+ meta: ExecutorMeta,
94
+ ): Promise<ExecutorResult<Record<string, unknown>>> {
95
+ const requestId = crypto.randomUUID();
96
+ this.lastRequestId = requestId;
97
+ await this.deps.db.createApprovalRequest({
98
+ id: requestId,
99
+ title: config.title,
100
+ questions: [{ id: "q1", type: "approval", label: "Approve?", required: true }],
101
+ approvers: { policy: "any" as const },
102
+ workflowRunId: meta.runId,
103
+ workflowRunStepId: meta.stepId,
104
+ });
105
+ return {
106
+ status: "success",
107
+ async: true,
108
+ waitFor: "approval.resolved",
109
+ correlationId: requestId,
110
+ } as unknown as ExecutorResult<Record<string, unknown>>;
111
+ }
112
+ }
113
+ const approvalExecutor = new MockApprovalExecutor(deps);
114
+ registry.register(approvalExecutor);
115
+
116
+ let teardown: (() => void) | undefined;
117
+ let agentId: string;
118
+
119
+ /** Runs `fn` with the live resume listener off, as after a crash, then turns it back on. */
120
+ async function withoutListener(fn: () => Promise<void>): Promise<void> {
121
+ teardown?.();
122
+ teardown = undefined;
123
+ try {
124
+ await fn();
125
+ } finally {
126
+ teardown = setupWorkflowResumeListener(workflowEventBus, registry);
127
+ }
128
+ }
129
+
130
+ beforeAll(async () => {
131
+ await removeDbFiles();
132
+ initDb(TEST_DB_PATH);
133
+ agentId = (await createAgent({ name: "Pinning recovery", status: "idle" })).id;
134
+ teardown = setupWorkflowResumeListener(workflowEventBus, registry);
135
+ });
136
+
137
+ afterAll(async () => {
138
+ teardown?.();
139
+ await drainForeachDrives();
140
+ closeDb();
141
+ await removeDbFiles();
142
+ });
143
+
144
+ // ─── Helpers ─────────────────────────────────────────────────
145
+
146
+ function makeWorkflow(definition: WorkflowDefinition): Promise<Workflow> {
147
+ return createWorkflow({ name: `pin-recovery-${crypto.randomUUID()}`, definition });
148
+ }
149
+
150
+ /** Edit through the function behind HTTP PUT and the `update-workflow` tool. */
151
+ async function editTo(workflowId: string, definition: WorkflowDefinition): Promise<void> {
152
+ const { workflow } = await snapshotAndUpdateWorkflow(workflowId, { definition });
153
+ expect(workflow?.definition).toEqual(definition);
154
+ }
155
+
156
+ function record(id: string, value: string): WorkflowDefinition["nodes"][number] {
157
+ return { id, type: "test-record", config: { value } };
158
+ }
159
+
160
+ async function nodeIds(runId: string): Promise<string[]> {
161
+ return (await getWorkflowRunStepsByRunId(runId)).map((step) => step.nodeId);
162
+ }
163
+
164
+ function recordsOf(runId: string): Array<[string, string]> {
165
+ return records.filter((r) => r.runId === runId).map((r) => [r.nodeId, r.value]);
166
+ }
167
+
168
+ async function stepOf(runId: string, nodeId: string): Promise<WorkflowRunStep> {
169
+ const step = (await getWorkflowRunStepsByRunId(runId)).find((s) => s.nodeId === nodeId);
170
+ if (!step) throw new Error(`no step ${nodeId} in run ${runId}`);
171
+ return step;
172
+ }
173
+
174
+ async function waitForRun(
175
+ runId: string,
176
+ done: (run: WorkflowRun) => boolean,
177
+ timeoutMs = 3_000,
178
+ ): Promise<WorkflowRun> {
179
+ const deadline = Date.now() + timeoutMs;
180
+ let run: WorkflowRun | null = null;
181
+ while (Date.now() < deadline) {
182
+ run = await getWorkflowRun(runId);
183
+ if (run && done(run)) return run;
184
+ await Bun.sleep(10);
185
+ }
186
+ throw new Error(`run ${runId} did not settle; last status ${run?.status} ${run?.error}`);
187
+ }
188
+
189
+ const isDone = (run: WorkflowRun) => run.status !== "running" && run.status !== "waiting";
190
+
191
+ async function waitForStep(stepId: string, status: string): Promise<void> {
192
+ const deadline = Date.now() + 3_000;
193
+ while (Date.now() < deadline) {
194
+ if ((await db.getWorkflowRunStep(stepId))?.status === status) return;
195
+ await Bun.sleep(10);
196
+ }
197
+ throw new Error(`step ${stepId} never reached ${status}`);
198
+ }
199
+
200
+ async function childRunOf(workflowId: string, status: string): Promise<WorkflowRun> {
201
+ for (let i = 0; i < 150; i++) {
202
+ const row = await getDbClient().get<{ id: string }>(
203
+ "SELECT id FROM workflow_runs WHERE workflowId = ?",
204
+ [workflowId],
205
+ );
206
+ if (row) return waitForRun(row.id, (r) => r.status === status);
207
+ await Bun.sleep(20);
208
+ }
209
+ throw new Error(`no child run of ${workflowId}`);
210
+ }
211
+
212
+ async function fireWait(runId: string, nodeId: string): Promise<void> {
213
+ const wait = await getWaitStateByStepId((await stepOf(runId, nodeId)).id);
214
+ await resumeWaitState(wait!.id, "fired", undefined, registry);
215
+ }
216
+
217
+ async function taskOf(runId: string, nodeId: string) {
218
+ const task = await getTaskByWorkflowRunStepId((await stepOf(runId, nodeId)).id);
219
+ if (!task) throw new Error(`no task for ${nodeId}`);
220
+ return task;
221
+ }
222
+
223
+ const longWait = { mode: "time", durationMs: 3_600_000 };
224
+
225
+ function subWorkflowParent(childId: string, successor: string, value: string): WorkflowDefinition {
226
+ return {
227
+ nodes: [
228
+ {
229
+ id: "child",
230
+ type: "sub-workflow",
231
+ config: { workflowId: childId, inputs: { n: 1 } },
232
+ next: successor,
233
+ },
234
+ record(successor, value),
235
+ ],
236
+ };
237
+ }
238
+
239
+ async function removeDbFiles(): Promise<void> {
240
+ for (const suffix of ["", "-wal", "-shm"]) {
241
+ await unlink(TEST_DB_PATH + suffix).catch(() => {});
242
+ }
243
+ }
244
+
245
+ // ─── Tests ───────────────────────────────────────────────────
246
+
247
+ describe("child completion", () => {
248
+ test("child finishes on A′ and the parent resumes on A after both are edited", async () => {
249
+ const childA: WorkflowDefinition = {
250
+ nodes: [{ id: "pause", type: "wait", config: longWait, next: "c2a" }, record("c2a", "A′")],
251
+ };
252
+ const childB: WorkflowDefinition = {
253
+ nodes: [{ id: "pause", type: "wait", config: longWait, next: "c2b" }, record("c2b", "B′")],
254
+ };
255
+ const child = await makeWorkflow(childA);
256
+ const parent = await makeWorkflow(subWorkflowParent(child.id, "a2", "A"));
257
+
258
+ const runId = await startWorkflowExecution(parent, {}, registry);
259
+ const childRun = await childRunOf(child.id, "waiting");
260
+
261
+ await editTo(parent.id, subWorkflowParent(child.id, "b2", "B"));
262
+ await editTo(child.id, childB);
263
+ await fireWait(childRun.id, "pause");
264
+
265
+ expect((await waitForRun(runId, isDone)).status).toBe("completed");
266
+ expect((await getWorkflowRun(childRun.id))?.status).toBe("completed");
267
+ expect(await nodeIds(childRun.id)).toEqual(["pause", "c2a"]);
268
+ expect(recordsOf(childRun.id)).toEqual([["c2a", "A′"]]);
269
+ expect(await nodeIds(runId)).toEqual(["child", "a2"]);
270
+ expect(recordsOf(runId)).toEqual([["a2", "A"]]);
271
+ });
272
+ });
273
+
274
+ describe("foreach", () => {
275
+ // B renames the foreach node itself: with a live read the late outcome and
276
+ // the queued item have no parent in the definition and are dropped.
277
+ function fanOut(
278
+ id: string,
279
+ template: string,
280
+ successor: string,
281
+ value: string,
282
+ extra: Record<string, unknown> = {},
283
+ ): WorkflowDefinition {
284
+ return {
285
+ nodes: [
286
+ {
287
+ id,
288
+ type: "foreach",
289
+ config: {
290
+ over: [{ id: "a" }, { id: "b" }],
291
+ itemKey: "id",
292
+ body: { type: "agent-task", config: { agentId, template } },
293
+ ...extra,
294
+ },
295
+ next: successor,
296
+ },
297
+ record(successor, value),
298
+ ],
299
+ };
300
+ }
301
+
302
+ test("a late item outcome on a failed fail-fast run settles on A's foreach", async () => {
303
+ const workflow = await makeWorkflow(
304
+ fanOut("fan", "A item", "a2", "A", { failurePolicy: "fail-fast" }),
305
+ );
306
+ const runId = await startWorkflowExecution(workflow, {}, registry);
307
+ await drainForeachDrives();
308
+ const first = await taskOf(runId, "fan#a");
309
+ const second = await taskOf(runId, "fan#b");
310
+ await failTask(first.id, "a failed");
311
+ await waitForRun(runId, (r) => r.status === "failed");
312
+ expect((await stepOf(runId, "fan#b")).status).toBe("waiting");
313
+
314
+ await editTo(workflow.id, fanOut("fanB", "B item", "b2", "B", { failurePolicy: "fail-fast" }));
315
+ await completeTask(second.id, "b finished late");
316
+ await waitForStep((await stepOf(runId, "fan#b")).id, "completed");
317
+
318
+ expect((await getWorkflowRun(runId))?.status).toBe("failed");
319
+ expect((await nodeIds(runId)).sort()).toEqual(["fan", "fan#a", "fan#b"]);
320
+ expect(recordsOf(runId)).toEqual([]);
321
+ });
322
+
323
+ test("the driver dispatches queued items and joins on A after an edit", async () => {
324
+ const workflow = await makeWorkflow(fanOut("fan", "A item", "a2", "A", { concurrency: 1 }));
325
+ const runId = await startWorkflowExecution(workflow, {}, registry);
326
+ await drainForeachDrives();
327
+ expect((await stepOf(runId, "fan#b")).status).toBe("pending");
328
+
329
+ // B keeps the node id and changes the body config and the successor.
330
+ await editTo(workflow.id, fanOut("fan", "B item", "b2", "B", { concurrency: 1 }));
331
+ await completeTask((await taskOf(runId, "fan#a")).id, "a ok");
332
+ await waitForStep((await stepOf(runId, "fan#a")).id, "completed");
333
+ await drainForeachDrives();
334
+ // The settle's drive dispatched the queued item from A's body.
335
+ const queued = await taskOf(runId, "fan#b");
336
+ expect(queued.task).toBe("A item");
337
+
338
+ await completeTask(queued.id, "b ok");
339
+ await waitForStep((await stepOf(runId, "fan#b")).id, "completed");
340
+ await drainForeachDrives();
341
+ // A direct drive after the join claims nothing more.
342
+ expect((await runForeachDrive(runId, "fan", registry)).claimed).toBe(0);
343
+
344
+ expect((await waitForRun(runId, isDone)).status).toBe("completed");
345
+ expect((await nodeIds(runId)).sort()).toEqual(["a2", "fan", "fan#a", "fan#b"]);
346
+ expect(recordsOf(runId)).toEqual([["a2", "A"]]);
347
+ });
348
+
349
+ test("an exact-cap fan-out is allowed when A's foreach has no successor", async () => {
350
+ const terminal = (template: string): WorkflowDefinition => ({
351
+ nodes: [
352
+ { id: "n1", type: "wait", config: longWait, next: "fan" },
353
+ {
354
+ id: "fan",
355
+ type: "foreach",
356
+ config: {
357
+ over: [{ id: "a" }, { id: "b" }],
358
+ itemKey: "id",
359
+ body: { type: "test-record", config: { value: template } },
360
+ },
361
+ },
362
+ ],
363
+ });
364
+ const workflowB = terminal("B item");
365
+ workflowB.nodes[1]!.next = "b2";
366
+ workflowB.nodes.push(record("b2", "B"));
367
+
368
+ const workflow = await makeWorkflow(terminal("A item"));
369
+ const runId = await startWorkflowExecution(workflow, {}, registry);
370
+ await editTo(workflow.id, workflowB);
371
+
372
+ // n1 + fan + 2 items = 4: allowed for a terminal foreach, refused with a successor.
373
+ const previous = process.env.WORKFLOW_MAX_STEPS_PER_RUN;
374
+ process.env.WORKFLOW_MAX_STEPS_PER_RUN = "4";
375
+ try {
376
+ await fireWait(runId, "n1");
377
+ await drainForeachDrives();
378
+ const run = await waitForRun(runId, isDone);
379
+ expect(run.error).toBeUndefined();
380
+ expect(run.status).toBe("completed");
381
+ } finally {
382
+ if (previous === undefined) delete process.env.WORKFLOW_MAX_STEPS_PER_RUN;
383
+ else process.env.WORKFLOW_MAX_STEPS_PER_RUN = previous;
384
+ }
385
+ expect((await nodeIds(runId)).sort()).toEqual(["fan", "fan#a", "fan#b", "n1"]);
386
+ expect(recordsOf(runId).map(([, value]) => value)).toEqual(["A item", "A item"]);
387
+ });
388
+ });
389
+
390
+ describe("restart recovery", () => {
391
+ const twoStep = (successor: string, value: string): WorkflowDefinition => ({
392
+ nodes: [{ ...record("n1", "first"), next: successor }, record(successor, value)],
393
+ });
394
+
395
+ test("a running run interrupted after n1 walks on A", async () => {
396
+ const workflow = await makeWorkflow(twoStep("a2", "A"));
397
+ // What startWorkflowExecution commits, then a crash after n1's checkpoint.
398
+ const runId = crypto.randomUUID();
399
+ await createWorkflowRun({
400
+ id: runId,
401
+ workflowId: workflow.id,
402
+ triggerType: "manual",
403
+ definitionHash: await pinWorkflowDefinition(workflow),
404
+ });
405
+ await updateWorkflowRun(runId, { context: { n1: { value: "first" } } });
406
+ const stepId = crypto.randomUUID();
407
+ await createWorkflowRunStep({ id: stepId, runId, nodeId: "n1", nodeType: "test-record" });
408
+ await updateWorkflowRunStep(stepId, {
409
+ status: "completed",
410
+ output: { value: "first" },
411
+ finishedAt: new Date().toISOString(),
412
+ });
413
+
414
+ await editTo(workflow.id, twoStep("b2", "B"));
415
+ await recoverIncompleteRuns(registry);
416
+
417
+ expect((await waitForRun(runId, isDone)).status).toBe("completed");
418
+ expect(await nodeIds(runId)).toEqual(["n1", "a2"]);
419
+ expect(recordsOf(runId)).toEqual([["a2", "A"]]);
420
+ });
421
+
422
+ test("a sub-workflow step left running in a parked run reconnects and walks on A", async () => {
423
+ const child = await makeWorkflow({
424
+ nodes: [{ id: "pause", type: "wait", config: longWait }],
425
+ });
426
+ // A second branch parks the run in `waiting`, the shape the running-run case misses.
427
+ const parked = (successor: string, value: string): WorkflowDefinition => {
428
+ const def = subWorkflowParent(child.id, successor, value);
429
+ def.nodes.push({ id: "hold", type: "wait", config: longWait });
430
+ return def;
431
+ };
432
+ const parent = await makeWorkflow(parked("a2", "A"));
433
+ const runId = await startWorkflowExecution(parent, {}, registry);
434
+ const childRun = await childRunOf(child.id, "waiting");
435
+ const parentStep = await stepOf(runId, "child");
436
+
437
+ await withoutListener(async () => {
438
+ // The child finishes while nothing listens; the crash left the parent step running.
439
+ await fireWait(childRun.id, "pause");
440
+ await waitForRun(childRun.id, isDone);
441
+ await getDbClient().run("UPDATE workflow_run_steps SET status = 'running' WHERE id = ?", [
442
+ parentStep.id,
443
+ ]);
444
+ expect((await getWorkflowRun(runId))?.status).toBe("waiting");
445
+
446
+ await editTo(parent.id, parked("b2", "B"));
447
+ await recoverIncompleteRuns(registry);
448
+ });
449
+
450
+ const steps = await getWorkflowRunStepsByRunId(runId);
451
+ expect(steps.map((s) => [s.nodeId, s.status]).sort()).toEqual([
452
+ ["a2", "completed"],
453
+ ["child", "completed"],
454
+ ["hold", "waiting"],
455
+ ]);
456
+ expect(recordsOf(runId)).toEqual([["a2", "A"]]);
457
+ });
458
+
459
+ test("a waiting agent-task step whose task finished during downtime resumes on A", async () => {
460
+ const def = (successor: string, value: string): WorkflowDefinition => ({
461
+ nodes: [
462
+ { id: "n1", type: "agent-task", config: { agentId, template: "work" }, next: successor },
463
+ record(successor, value),
464
+ ],
465
+ });
466
+ const workflow = await makeWorkflow(def("a2", "A"));
467
+ const runId = await startWorkflowExecution(workflow, {}, registry);
468
+ expect((await getWorkflowRun(runId))?.status).toBe("waiting");
469
+
470
+ await withoutListener(async () => {
471
+ await completeTask((await taskOf(runId, "n1")).id, "done while down");
472
+ await editTo(workflow.id, def("b2", "B"));
473
+ await recoverIncompleteRuns(registry);
474
+ });
475
+
476
+ expect((await waitForRun(runId, isDone)).status).toBe("completed");
477
+ expect(await nodeIds(runId)).toEqual(["n1", "a2"]);
478
+ expect(recordsOf(runId)).toEqual([["a2", "A"]]);
479
+ });
480
+
481
+ test("an approval resolved during downtime routes on A", async () => {
482
+ const def = (successor: string, value: string): WorkflowDefinition => ({
483
+ nodes: [
484
+ {
485
+ id: "review",
486
+ type: "test-approval",
487
+ config: { title: "Review" },
488
+ next: { approved: successor, rejected: successor, timeout: successor },
489
+ },
490
+ record(successor, value),
491
+ ],
492
+ });
493
+ const workflow = await makeWorkflow(def("a2", "A"));
494
+ const runId = await startWorkflowExecution(workflow, {}, registry);
495
+ const requestId = approvalExecutor.lastRequestId!;
496
+ expect((await getWorkflowRun(runId))?.status).toBe("waiting");
497
+
498
+ await withoutListener(async () => {
499
+ await resolveApprovalRequest(requestId, { status: "approved", responses: { q1: true } });
500
+ await editTo(workflow.id, def("b2", "B"));
501
+ await recoverIncompleteRuns(registry);
502
+ });
503
+
504
+ expect((await waitForRun(runId, isDone)).status).toBe("completed");
505
+ expect(await nodeIds(runId)).toEqual(["review", "a2"]);
506
+ expect(recordsOf(runId)).toEqual([["a2", "A"]]);
507
+ });
508
+
509
+ test("a foreach fan-out with a queued item is re-driven on A", async () => {
510
+ const def = (id: string, template: string, successor: string, value: string) => ({
511
+ nodes: [
512
+ {
513
+ id,
514
+ type: "foreach",
515
+ config: {
516
+ over: [{ id: "a" }, { id: "b" }],
517
+ itemKey: "id",
518
+ concurrency: 1,
519
+ body: { type: "agent-task", config: { agentId, template } },
520
+ },
521
+ next: successor,
522
+ },
523
+ record(successor, value),
524
+ ],
525
+ });
526
+ const workflow = await makeWorkflow(def("fan", "A item", "a2", "A"));
527
+ const runId = await startWorkflowExecution(workflow, {}, registry);
528
+ await drainForeachDrives();
529
+ const first = await taskOf(runId, "fan#a");
530
+ expect((await stepOf(runId, "fan#b")).status).toBe("pending");
531
+
532
+ // The first item's settlement committed, then the API stopped before the next drive.
533
+ await withoutListener(async () => {
534
+ await completeTask(first.id, "a ok");
535
+ await updateWorkflowRunStep((await stepOf(runId, "fan#a")).id, {
536
+ status: "completed",
537
+ output: { taskId: first.id, taskOutput: "a ok" },
538
+ finishedAt: new Date().toISOString(),
539
+ });
540
+ await editTo(workflow.id, def("fanB", "B item", "b2", "B"));
541
+ await recoverIncompleteRuns(registry);
542
+ await drainForeachDrives();
543
+ });
544
+
545
+ const queued = await taskOf(runId, "fan#b");
546
+ expect(queued.task).toBe("A item");
547
+ await completeTask(queued.id, "b ok");
548
+ expect((await waitForRun(runId, isDone)).status).toBe("completed");
549
+ expect((await nodeIds(runId)).sort()).toEqual(["a2", "fan", "fan#a", "fan#b"]);
550
+ expect(recordsOf(runId)).toEqual([["a2", "A"]]);
551
+ });
552
+ });