@elie-laloum/outpost 2.0.0 → 4.0.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 (588) hide show
  1. package/CHANGELOG.md +42 -0
  2. package/README.md +3 -1
  3. package/SECURITY.md +3 -1
  4. package/dist/adapters/agents/claude-diagnostics.d.ts +1 -0
  5. package/dist/adapters/agents/claude-diagnostics.js +7 -0
  6. package/dist/adapters/agents/claude-diagnostics.js.map +1 -0
  7. package/dist/adapters/agents/cli-diagnostics.constants.d.ts +12 -0
  8. package/dist/adapters/agents/cli-diagnostics.constants.js +24 -0
  9. package/dist/adapters/agents/cli-diagnostics.constants.js.map +1 -0
  10. package/dist/adapters/agents/cli-diagnostics.d.ts +4 -0
  11. package/dist/adapters/agents/cli-diagnostics.js +14 -0
  12. package/dist/adapters/agents/cli-diagnostics.js.map +1 -0
  13. package/dist/adapters/agents/cli-diagnostics.types.d.ts +13 -0
  14. package/dist/adapters/agents/cli-diagnostics.types.js +2 -0
  15. package/dist/adapters/agents/cli-diagnostics.types.js.map +1 -0
  16. package/dist/adapters/agents/codex-diagnostics.d.ts +1 -0
  17. package/dist/adapters/agents/codex-diagnostics.js +7 -0
  18. package/dist/adapters/agents/codex-diagnostics.js.map +1 -0
  19. package/dist/adapters/agents/gemini-adapter.d.ts +3 -0
  20. package/dist/adapters/agents/gemini-adapter.js +15 -0
  21. package/dist/adapters/agents/gemini-adapter.js.map +1 -0
  22. package/dist/adapters/agents/gemini-diagnostics.d.ts +2 -0
  23. package/dist/adapters/agents/gemini-diagnostics.js +15 -0
  24. package/dist/adapters/agents/gemini-diagnostics.js.map +1 -0
  25. package/dist/adapters/agents/gemini-events.d.ts +2 -0
  26. package/dist/adapters/agents/gemini-events.js +50 -0
  27. package/dist/adapters/agents/gemini-events.js.map +1 -0
  28. package/dist/adapters/agents/gemini-request.d.ts +4 -0
  29. package/dist/adapters/agents/gemini-request.js +21 -0
  30. package/dist/adapters/agents/gemini-request.js.map +1 -0
  31. package/dist/adapters/agents/gemini.types.d.ts +6 -0
  32. package/dist/adapters/agents/gemini.types.js +2 -0
  33. package/dist/adapters/agents/gemini.types.js.map +1 -0
  34. package/dist/adapters/agents/protocol-fixtures.constants.d.ts +2 -0
  35. package/dist/adapters/agents/protocol-fixtures.constants.js +105 -0
  36. package/dist/adapters/agents/protocol-fixtures.constants.js.map +1 -0
  37. package/dist/adapters/agents/protocol-fixtures.types.d.ts +6 -0
  38. package/dist/adapters/agents/protocol-fixtures.types.js +2 -0
  39. package/dist/adapters/agents/protocol-fixtures.types.js.map +1 -0
  40. package/dist/application/agent-bootstrap.constants.d.ts +1 -0
  41. package/dist/application/agent-bootstrap.constants.js +1 -0
  42. package/dist/application/agent-bootstrap.constants.js.map +1 -1
  43. package/dist/application/agent-bootstrap.js +6 -2
  44. package/dist/application/agent-bootstrap.js.map +1 -1
  45. package/dist/application/agent-output.js +9 -2
  46. package/dist/application/agent-output.js.map +1 -1
  47. package/dist/application/artifact-tasks.d.ts +5 -0
  48. package/dist/application/artifact-tasks.js +31 -0
  49. package/dist/application/artifact-tasks.js.map +1 -0
  50. package/dist/application/artifact-tasks.types.d.ts +8 -0
  51. package/dist/application/artifact-tasks.types.js +2 -0
  52. package/dist/application/artifact-tasks.types.js.map +1 -0
  53. package/dist/application/diagnostic-probe.d.ts +3 -0
  54. package/dist/application/diagnostic-probe.js +55 -0
  55. package/dist/application/diagnostic-probe.js.map +1 -0
  56. package/dist/application/dispatch-validation.js +2 -0
  57. package/dist/application/dispatch-validation.js.map +1 -1
  58. package/dist/application/doctor-agent.constants.d.ts +11 -0
  59. package/dist/application/doctor-agent.constants.js +10 -0
  60. package/dist/application/doctor-agent.constants.js.map +1 -0
  61. package/dist/application/doctor-agent.d.ts +3 -0
  62. package/dist/application/doctor-agent.js +53 -0
  63. package/dist/application/doctor-agent.js.map +1 -0
  64. package/dist/application/doctor-image.d.ts +3 -0
  65. package/dist/application/doctor-image.js +133 -0
  66. package/dist/application/doctor-image.js.map +1 -0
  67. package/dist/application/doctor-protocol.d.ts +3 -0
  68. package/dist/application/doctor-protocol.js +30 -0
  69. package/dist/application/doctor-protocol.js.map +1 -0
  70. package/dist/application/doctor-protocol.types.d.ts +10 -0
  71. package/dist/application/doctor-protocol.types.js +2 -0
  72. package/dist/application/doctor-protocol.types.js.map +1 -0
  73. package/dist/application/doctor-sandbox.constants.d.ts +5 -0
  74. package/dist/application/doctor-sandbox.constants.js +6 -0
  75. package/dist/application/doctor-sandbox.constants.js.map +1 -0
  76. package/dist/application/doctor-sandbox.d.ts +4 -0
  77. package/dist/application/doctor-sandbox.js +133 -0
  78. package/dist/application/doctor-sandbox.js.map +1 -0
  79. package/dist/application/doctor-sandbox.types.d.ts +23 -0
  80. package/dist/application/doctor-sandbox.types.js +2 -0
  81. package/dist/application/doctor-sandbox.types.js.map +1 -0
  82. package/dist/application/doctor-transfers.constants.d.ts +6 -0
  83. package/dist/application/doctor-transfers.constants.js +7 -0
  84. package/dist/application/doctor-transfers.constants.js.map +1 -0
  85. package/dist/application/doctor-transfers.d.ts +4 -0
  86. package/dist/application/doctor-transfers.js +88 -0
  87. package/dist/application/doctor-transfers.js.map +1 -0
  88. package/dist/application/doctor.constants.d.ts +9 -0
  89. package/dist/application/doctor.constants.js +15 -0
  90. package/dist/application/doctor.constants.js.map +1 -0
  91. package/dist/application/doctor.d.ts +3 -0
  92. package/dist/application/doctor.js +106 -0
  93. package/dist/application/doctor.js.map +1 -0
  94. package/dist/application/doctor.types.d.ts +39 -0
  95. package/dist/application/doctor.types.js +2 -0
  96. package/dist/application/doctor.types.js.map +1 -0
  97. package/dist/application/execution.js +1 -0
  98. package/dist/application/execution.js.map +1 -1
  99. package/dist/application/outpost.types.d.ts +4 -0
  100. package/dist/application/outpost.types.js.map +1 -1
  101. package/dist/application/queue-worker.d.ts +2 -0
  102. package/dist/application/queue-worker.js +75 -0
  103. package/dist/application/queue-worker.js.map +1 -0
  104. package/dist/application/queue-worker.types.d.ts +15 -0
  105. package/dist/application/queue-worker.types.js +2 -0
  106. package/dist/application/queue-worker.types.js.map +1 -0
  107. package/dist/application/queued-task.d.ts +3 -0
  108. package/dist/application/queued-task.js +56 -0
  109. package/dist/application/queued-task.js.map +1 -0
  110. package/dist/application/queued-task.types.d.ts +11 -0
  111. package/dist/application/queued-task.types.js +2 -0
  112. package/dist/application/queued-task.types.js.map +1 -0
  113. package/dist/application/recovery-checksum-capture.d.ts +2 -0
  114. package/dist/application/recovery-checksum-capture.js +32 -0
  115. package/dist/application/recovery-checksum-capture.js.map +1 -0
  116. package/dist/application/recovery-checksum-paths.d.ts +2 -0
  117. package/dist/application/recovery-checksum-paths.js +11 -0
  118. package/dist/application/recovery-checksum-paths.js.map +1 -0
  119. package/dist/application/recovery-checksums.constants.d.ts +5 -0
  120. package/dist/application/recovery-checksums.constants.js +6 -0
  121. package/dist/application/recovery-checksums.constants.js.map +1 -0
  122. package/dist/application/recovery-checksums.d.ts +3 -0
  123. package/dist/application/recovery-checksums.js +94 -0
  124. package/dist/application/recovery-checksums.js.map +1 -0
  125. package/dist/application/recovery-checksums.types.d.ts +17 -0
  126. package/dist/application/recovery-checksums.types.js +2 -0
  127. package/dist/application/recovery-checksums.types.js.map +1 -0
  128. package/dist/application/recovery-inspection.d.ts +2 -0
  129. package/dist/application/recovery-inspection.js +32 -0
  130. package/dist/application/recovery-inspection.js.map +1 -0
  131. package/dist/application/recovery-inspection.types.d.ts +18 -0
  132. package/dist/application/recovery-inspection.types.js +2 -0
  133. package/dist/application/recovery-inspection.types.js.map +1 -0
  134. package/dist/application/recovery-restorability.d.ts +2 -0
  135. package/dist/application/recovery-restorability.js +137 -0
  136. package/dist/application/recovery-restorability.js.map +1 -0
  137. package/dist/application/recovery-restore-snapshot.d.ts +2 -0
  138. package/dist/application/recovery-restore-snapshot.js +57 -0
  139. package/dist/application/recovery-restore-snapshot.js.map +1 -0
  140. package/dist/application/recovery-restore.d.ts +3 -0
  141. package/dist/application/recovery-restore.js +161 -0
  142. package/dist/application/recovery-restore.js.map +1 -0
  143. package/dist/application/recovery-restore.types.d.ts +28 -0
  144. package/dist/application/recovery-restore.types.js +2 -0
  145. package/dist/application/recovery-restore.types.js.map +1 -0
  146. package/dist/application/recovery-retention-policy.d.ts +2 -0
  147. package/dist/application/recovery-retention-policy.js +21 -0
  148. package/dist/application/recovery-retention-policy.js.map +1 -0
  149. package/dist/application/recovery-retention.constants.d.ts +3 -0
  150. package/dist/application/recovery-retention.constants.js +2 -0
  151. package/dist/application/recovery-retention.constants.js.map +1 -0
  152. package/dist/application/recovery-retention.d.ts +4 -0
  153. package/dist/application/recovery-retention.js +207 -0
  154. package/dist/application/recovery-retention.js.map +1 -0
  155. package/dist/application/recovery-retention.types.d.ts +52 -0
  156. package/dist/application/recovery-retention.types.js +2 -0
  157. package/dist/application/recovery-retention.types.js.map +1 -0
  158. package/dist/application/recovery-verification.constants.d.ts +5 -0
  159. package/dist/application/recovery-verification.constants.js +10 -0
  160. package/dist/application/recovery-verification.constants.js.map +1 -0
  161. package/dist/application/recovery-verification.d.ts +3 -0
  162. package/dist/application/recovery-verification.js +107 -0
  163. package/dist/application/recovery-verification.js.map +1 -0
  164. package/dist/application/recovery-verification.types.d.ts +26 -0
  165. package/dist/application/recovery-verification.types.js +2 -0
  166. package/dist/application/recovery-verification.types.js.map +1 -0
  167. package/dist/application/remote-apply.js +30 -6
  168. package/dist/application/remote-apply.js.map +1 -1
  169. package/dist/application/remote-backup.js +6 -2
  170. package/dist/application/remote-backup.js.map +1 -1
  171. package/dist/application/remote-download.js +38 -6
  172. package/dist/application/remote-download.js.map +1 -1
  173. package/dist/application/remote-history.d.ts +6 -0
  174. package/dist/application/remote-history.js +114 -0
  175. package/dist/application/remote-history.js.map +1 -0
  176. package/dist/application/remote-upload.d.ts +2 -0
  177. package/dist/application/remote-upload.js +17 -0
  178. package/dist/application/remote-upload.js.map +1 -0
  179. package/dist/application/remote-validation.js +39 -28
  180. package/dist/application/remote-validation.js.map +1 -1
  181. package/dist/application/remote-workspace.constants.d.ts +3 -0
  182. package/dist/application/remote-workspace.constants.js +2 -0
  183. package/dist/application/remote-workspace.constants.js.map +1 -0
  184. package/dist/application/remote-workspace.js +10 -10
  185. package/dist/application/remote-workspace.js.map +1 -1
  186. package/dist/application/remote-workspace.types.d.ts +3 -1
  187. package/dist/application/remote-workspace.types.js.map +1 -1
  188. package/dist/application/sandbox-activity.d.ts +3 -0
  189. package/dist/application/sandbox-activity.js +25 -0
  190. package/dist/application/sandbox-activity.js.map +1 -0
  191. package/dist/application/sandbox-provision.js +55 -10
  192. package/dist/application/sandbox-provision.js.map +1 -1
  193. package/dist/application/sandbox-session.types.d.ts +2 -0
  194. package/dist/application/sandbox-session.types.js.map +1 -1
  195. package/dist/application/sandbox.js +33 -10
  196. package/dist/application/sandbox.js.map +1 -1
  197. package/dist/application/speculation-host.d.ts +2 -0
  198. package/dist/application/speculation-host.js +30 -0
  199. package/dist/application/speculation-host.js.map +1 -0
  200. package/dist/application/speculation.constants.d.ts +4 -0
  201. package/dist/application/speculation.constants.js +2 -0
  202. package/dist/application/speculation.constants.js.map +1 -0
  203. package/dist/application/speculation.d.ts +2 -0
  204. package/dist/application/speculation.js +166 -0
  205. package/dist/application/speculation.js.map +1 -0
  206. package/dist/application/speculation.types.d.ts +56 -0
  207. package/dist/application/speculation.types.js +2 -0
  208. package/dist/application/speculation.types.js.map +1 -0
  209. package/dist/application/storage-reservation.d.ts +3 -0
  210. package/dist/application/storage-reservation.js +10 -0
  211. package/dist/application/storage-reservation.js.map +1 -0
  212. package/dist/application/storage-reservation.types.d.ts +4 -0
  213. package/dist/application/storage-reservation.types.js +2 -0
  214. package/dist/application/storage-reservation.types.js.map +1 -0
  215. package/dist/application/task-usage.d.ts +4 -0
  216. package/dist/application/task-usage.js +41 -0
  217. package/dist/application/task-usage.js.map +1 -0
  218. package/dist/application/task-usage.types.d.ts +5 -0
  219. package/dist/application/task-usage.types.js +2 -0
  220. package/dist/application/task-usage.types.js.map +1 -0
  221. package/dist/application/tasks.js +23 -2
  222. package/dist/application/tasks.js.map +1 -1
  223. package/dist/application/tasks.types.d.ts +4 -3
  224. package/dist/application/tasks.types.js.map +1 -1
  225. package/dist/application/workspace-allocation.d.ts +3 -0
  226. package/dist/application/workspace-allocation.js +34 -0
  227. package/dist/application/workspace-allocation.js.map +1 -0
  228. package/dist/application/workspace.js +2 -2
  229. package/dist/application/workspace.js.map +1 -1
  230. package/dist/cli/commands.js +4 -0
  231. package/dist/cli/commands.js.map +1 -1
  232. package/dist/cli/doctor-command.d.ts +3 -0
  233. package/dist/cli/doctor-command.js +38 -0
  234. package/dist/cli/doctor-command.js.map +1 -0
  235. package/dist/cli/main.constants.d.ts +38 -2
  236. package/dist/cli/main.constants.js +21 -2
  237. package/dist/cli/main.constants.js.map +1 -1
  238. package/dist/cli/recovery-command.d.ts +2 -0
  239. package/dist/cli/recovery-command.js +93 -0
  240. package/dist/cli/recovery-command.js.map +1 -0
  241. package/dist/cli/recovery-prune-command.d.ts +2 -0
  242. package/dist/cli/recovery-prune-command.js +34 -0
  243. package/dist/cli/recovery-prune-command.js.map +1 -0
  244. package/dist/cli/recovery-restore-command.d.ts +2 -0
  245. package/dist/cli/recovery-restore-command.js +34 -0
  246. package/dist/cli/recovery-restore-command.js.map +1 -0
  247. package/dist/cli/recovery-verify-command.d.ts +2 -0
  248. package/dist/cli/recovery-verify-command.js +43 -0
  249. package/dist/cli/recovery-verify-command.js.map +1 -0
  250. package/dist/cli/scaffold-files.js +2 -2
  251. package/dist/cli/scaffold-files.js.map +1 -1
  252. package/dist/cli/scaffold-validation.js +1 -1
  253. package/dist/cli/scaffold-validation.js.map +1 -1
  254. package/dist/cli/scaffold.constants.d.ts +5 -0
  255. package/dist/cli/scaffold.constants.js +7 -2
  256. package/dist/cli/scaffold.constants.js.map +1 -1
  257. package/dist/cli/scaffold.types.d.ts +1 -1
  258. package/dist/cli/scaffold.types.js.map +1 -1
  259. package/dist/domain/agent.types.d.ts +2 -0
  260. package/dist/domain/agent.types.js.map +1 -1
  261. package/dist/domain/artifact-reference.d.ts +4 -0
  262. package/dist/domain/artifact-reference.js +90 -0
  263. package/dist/domain/artifact-reference.js.map +1 -0
  264. package/dist/domain/artifact.constants.d.ts +2 -0
  265. package/dist/domain/artifact.constants.js +3 -0
  266. package/dist/domain/artifact.constants.js.map +1 -0
  267. package/dist/domain/artifact.d.ts +7 -0
  268. package/dist/domain/artifact.js +104 -0
  269. package/dist/domain/artifact.js.map +1 -0
  270. package/dist/domain/artifact.types.d.ts +44 -0
  271. package/dist/domain/artifact.types.js +2 -0
  272. package/dist/domain/artifact.types.js.map +1 -0
  273. package/dist/domain/egress.constants.d.ts +1 -0
  274. package/dist/domain/egress.constants.js +7 -0
  275. package/dist/domain/egress.constants.js.map +1 -0
  276. package/dist/domain/egress.d.ts +2 -0
  277. package/dist/domain/egress.js +48 -0
  278. package/dist/domain/egress.js.map +1 -0
  279. package/dist/domain/egress.types.d.ts +8 -0
  280. package/dist/domain/egress.types.js +2 -0
  281. package/dist/domain/egress.types.js.map +1 -0
  282. package/dist/domain/ports.d.ts +1 -1
  283. package/dist/domain/ports.js.map +1 -1
  284. package/dist/domain/sandbox.types.d.ts +13 -0
  285. package/dist/domain/sandbox.types.js.map +1 -1
  286. package/dist/domain/task-queue.constants.d.ts +8 -0
  287. package/dist/domain/task-queue.constants.js +9 -0
  288. package/dist/domain/task-queue.constants.js.map +1 -0
  289. package/dist/domain/task-queue.d.ts +11 -0
  290. package/dist/domain/task-queue.js +105 -0
  291. package/dist/domain/task-queue.js.map +1 -0
  292. package/dist/domain/task-queue.types.d.ts +41 -0
  293. package/dist/domain/task-queue.types.js +2 -0
  294. package/dist/domain/task-queue.types.js.map +1 -0
  295. package/dist/domain/workflow/budget.constants.d.ts +1 -0
  296. package/dist/domain/workflow/budget.constants.js +7 -0
  297. package/dist/domain/workflow/budget.constants.js.map +1 -0
  298. package/dist/domain/workflow/budget.d.ts +9 -0
  299. package/dist/domain/workflow/budget.js +69 -0
  300. package/dist/domain/workflow/budget.js.map +1 -0
  301. package/dist/domain/workflow/budget.types.d.ts +15 -0
  302. package/dist/domain/workflow/budget.types.js +2 -0
  303. package/dist/domain/workflow/budget.types.js.map +1 -0
  304. package/dist/domain/workflow/checkpoint-validation.d.ts +3 -0
  305. package/dist/domain/workflow/checkpoint-validation.js +81 -0
  306. package/dist/domain/workflow/checkpoint-validation.js.map +1 -0
  307. package/dist/domain/workflow/checkpoint-value.d.ts +2 -0
  308. package/dist/domain/workflow/checkpoint-value.js +49 -0
  309. package/dist/domain/workflow/checkpoint-value.js.map +1 -0
  310. package/dist/domain/workflow/checkpoint.d.ts +3 -0
  311. package/dist/domain/workflow/checkpoint.js +70 -0
  312. package/dist/domain/workflow/checkpoint.js.map +1 -0
  313. package/dist/domain/workflow/checkpoint.types.d.ts +40 -0
  314. package/dist/domain/workflow/checkpoint.types.js +2 -0
  315. package/dist/domain/workflow/checkpoint.types.js.map +1 -0
  316. package/dist/domain/workflow/gate-validation.d.ts +4 -0
  317. package/dist/domain/workflow/gate-validation.js +71 -0
  318. package/dist/domain/workflow/gate-validation.js.map +1 -0
  319. package/dist/domain/workflow/gates.constants.d.ts +4 -0
  320. package/dist/domain/workflow/gates.constants.js +5 -0
  321. package/dist/domain/workflow/gates.constants.js.map +1 -0
  322. package/dist/domain/workflow/gates.d.ts +7 -0
  323. package/dist/domain/workflow/gates.js +85 -0
  324. package/dist/domain/workflow/gates.js.map +1 -0
  325. package/dist/domain/workflow/gates.types.d.ts +27 -0
  326. package/dist/domain/workflow/gates.types.js +2 -0
  327. package/dist/domain/workflow/gates.types.js.map +1 -0
  328. package/dist/domain/workflow/task-runner.js +22 -3
  329. package/dist/domain/workflow/task-runner.js.map +1 -1
  330. package/dist/domain/workflow/usage-receipt.constants.d.ts +2 -0
  331. package/dist/domain/workflow/usage-receipt.constants.js +3 -0
  332. package/dist/domain/workflow/usage-receipt.constants.js.map +1 -0
  333. package/dist/domain/workflow/usage-receipt.d.ts +2 -0
  334. package/dist/domain/workflow/usage-receipt.js +18 -0
  335. package/dist/domain/workflow/usage-receipt.js.map +1 -0
  336. package/dist/domain/workflow/validation.js +6 -0
  337. package/dist/domain/workflow/validation.js.map +1 -1
  338. package/dist/domain/workflow/workflow-scheduler.js +68 -33
  339. package/dist/domain/workflow/workflow-scheduler.js.map +1 -1
  340. package/dist/domain/workflow/workflow-state.d.ts +2 -1
  341. package/dist/domain/workflow/workflow-state.js +107 -3
  342. package/dist/domain/workflow/workflow-state.js.map +1 -1
  343. package/dist/domain/workflow.d.ts +2 -0
  344. package/dist/domain/workflow.js +1 -0
  345. package/dist/domain/workflow.js.map +1 -1
  346. package/dist/domain/workflow.types.d.ts +23 -3
  347. package/dist/domain/workflow.types.js.map +1 -1
  348. package/dist/index.d.ts +44 -5
  349. package/dist/index.js +19 -2
  350. package/dist/index.js.map +1 -1
  351. package/dist/infrastructure/artifact-store.constants.d.ts +1 -0
  352. package/dist/infrastructure/artifact-store.constants.js +2 -0
  353. package/dist/infrastructure/artifact-store.constants.js.map +1 -0
  354. package/dist/infrastructure/artifact-store.d.ts +3 -0
  355. package/dist/infrastructure/artifact-store.js +100 -0
  356. package/dist/infrastructure/artifact-store.js.map +1 -0
  357. package/dist/infrastructure/artifact-store.types.d.ts +4 -0
  358. package/dist/infrastructure/artifact-store.types.js +2 -0
  359. package/dist/infrastructure/artifact-store.types.js.map +1 -0
  360. package/dist/infrastructure/file-manifest.d.ts +6 -0
  361. package/dist/infrastructure/file-manifest.js +92 -0
  362. package/dist/infrastructure/file-manifest.js.map +1 -0
  363. package/dist/infrastructure/git/lock-inspection.constants.d.ts +4 -0
  364. package/dist/infrastructure/git/lock-inspection.constants.js +5 -0
  365. package/dist/infrastructure/git/lock-inspection.constants.js.map +1 -0
  366. package/dist/infrastructure/git/lock-inspection.d.ts +3 -0
  367. package/dist/infrastructure/git/lock-inspection.js +64 -0
  368. package/dist/infrastructure/git/lock-inspection.js.map +1 -0
  369. package/dist/infrastructure/git/lock-inspection.types.d.ts +27 -0
  370. package/dist/infrastructure/git/lock-inspection.types.js +2 -0
  371. package/dist/infrastructure/git/lock-inspection.types.js.map +1 -0
  372. package/dist/infrastructure/git/lock.d.ts +1 -0
  373. package/dist/infrastructure/git/lock.js +70 -29
  374. package/dist/infrastructure/git/lock.js.map +1 -1
  375. package/dist/infrastructure/git/process-identity.d.ts +3 -0
  376. package/dist/infrastructure/git/process-identity.js +73 -0
  377. package/dist/infrastructure/git/process-identity.js.map +1 -0
  378. package/dist/infrastructure/git/process-identity.types.d.ts +10 -0
  379. package/dist/infrastructure/git/process-identity.types.js +2 -0
  380. package/dist/infrastructure/git/process-identity.types.js.map +1 -0
  381. package/dist/infrastructure/git/verification-command.d.ts +1 -0
  382. package/dist/infrastructure/git/verification-command.js +37 -0
  383. package/dist/infrastructure/git/verification-command.js.map +1 -0
  384. package/dist/infrastructure/git/workspace-lease.js +1 -0
  385. package/dist/infrastructure/git/workspace-lease.js.map +1 -1
  386. package/dist/infrastructure/git/workspace.js +5 -1
  387. package/dist/infrastructure/git/workspace.js.map +1 -1
  388. package/dist/infrastructure/git/worktree-inspection.constants.d.ts +5 -0
  389. package/dist/infrastructure/git/worktree-inspection.constants.js +16 -0
  390. package/dist/infrastructure/git/worktree-inspection.constants.js.map +1 -0
  391. package/dist/infrastructure/git/worktree-inspection.d.ts +3 -0
  392. package/dist/infrastructure/git/worktree-inspection.js +120 -0
  393. package/dist/infrastructure/git/worktree-inspection.js.map +1 -0
  394. package/dist/infrastructure/git/worktree-inspection.types.d.ts +19 -0
  395. package/dist/infrastructure/git/worktree-inspection.types.js +2 -0
  396. package/dist/infrastructure/git/worktree-inspection.types.js.map +1 -0
  397. package/dist/infrastructure/inspection-file.constants.d.ts +1 -0
  398. package/dist/infrastructure/inspection-file.constants.js +5 -0
  399. package/dist/infrastructure/inspection-file.constants.js.map +1 -0
  400. package/dist/infrastructure/inspection-file.d.ts +1 -0
  401. package/dist/infrastructure/inspection-file.js +43 -0
  402. package/dist/infrastructure/inspection-file.js.map +1 -0
  403. package/dist/infrastructure/inspection-hash.constants.d.ts +1 -0
  404. package/dist/infrastructure/inspection-hash.constants.js +2 -0
  405. package/dist/infrastructure/inspection-hash.constants.js.map +1 -0
  406. package/dist/infrastructure/inspection-hash.d.ts +2 -0
  407. package/dist/infrastructure/inspection-hash.js +62 -0
  408. package/dist/infrastructure/inspection-hash.js.map +1 -0
  409. package/dist/infrastructure/inspection-hash.types.d.ts +5 -0
  410. package/dist/infrastructure/inspection-hash.types.js +2 -0
  411. package/dist/infrastructure/inspection-hash.types.js.map +1 -0
  412. package/dist/infrastructure/journal-retention.constants.d.ts +4 -0
  413. package/dist/infrastructure/journal-retention.constants.js +5 -0
  414. package/dist/infrastructure/journal-retention.constants.js.map +1 -0
  415. package/dist/infrastructure/journal-retention.d.ts +2 -0
  416. package/dist/infrastructure/journal-retention.js +51 -0
  417. package/dist/infrastructure/journal-retention.js.map +1 -0
  418. package/dist/infrastructure/journal.js +37 -13
  419. package/dist/infrastructure/journal.js.map +1 -1
  420. package/dist/infrastructure/opentelemetry.constants.d.ts +10 -0
  421. package/dist/infrastructure/opentelemetry.constants.js +11 -0
  422. package/dist/infrastructure/opentelemetry.constants.js.map +1 -0
  423. package/dist/infrastructure/opentelemetry.d.ts +3 -0
  424. package/dist/infrastructure/opentelemetry.js +131 -0
  425. package/dist/infrastructure/opentelemetry.js.map +1 -0
  426. package/dist/infrastructure/opentelemetry.types.d.ts +21 -0
  427. package/dist/infrastructure/opentelemetry.types.js +2 -0
  428. package/dist/infrastructure/opentelemetry.types.js.map +1 -0
  429. package/dist/infrastructure/resource-activity-inspection.d.ts +2 -0
  430. package/dist/infrastructure/resource-activity-inspection.js +152 -0
  431. package/dist/infrastructure/resource-activity-inspection.js.map +1 -0
  432. package/dist/infrastructure/resource-activity.constants.d.ts +8 -0
  433. package/dist/infrastructure/resource-activity.constants.js +26 -0
  434. package/dist/infrastructure/resource-activity.constants.js.map +1 -0
  435. package/dist/infrastructure/resource-activity.d.ts +2 -0
  436. package/dist/infrastructure/resource-activity.js +166 -0
  437. package/dist/infrastructure/resource-activity.js.map +1 -0
  438. package/dist/infrastructure/resource-activity.types.d.ts +53 -0
  439. package/dist/infrastructure/resource-activity.types.js +2 -0
  440. package/dist/infrastructure/resource-activity.types.js.map +1 -0
  441. package/dist/infrastructure/storage-inventory.constants.d.ts +6 -0
  442. package/dist/infrastructure/storage-inventory.constants.js +11 -0
  443. package/dist/infrastructure/storage-inventory.constants.js.map +1 -0
  444. package/dist/infrastructure/storage-inventory.d.ts +2 -0
  445. package/dist/infrastructure/storage-inventory.js +123 -0
  446. package/dist/infrastructure/storage-inventory.js.map +1 -0
  447. package/dist/infrastructure/storage-inventory.types.d.ts +38 -0
  448. package/dist/infrastructure/storage-inventory.types.js +2 -0
  449. package/dist/infrastructure/storage-inventory.types.js.map +1 -0
  450. package/dist/infrastructure/storage-mutation-lock.constants.d.ts +5 -0
  451. package/dist/infrastructure/storage-mutation-lock.constants.js +6 -0
  452. package/dist/infrastructure/storage-mutation-lock.constants.js.map +1 -0
  453. package/dist/infrastructure/storage-mutation-lock.d.ts +1 -0
  454. package/dist/infrastructure/storage-mutation-lock.js +21 -0
  455. package/dist/infrastructure/storage-mutation-lock.js.map +1 -0
  456. package/dist/infrastructure/storage-reservations.constants.d.ts +4 -0
  457. package/dist/infrastructure/storage-reservations.constants.js +5 -0
  458. package/dist/infrastructure/storage-reservations.constants.js.map +1 -0
  459. package/dist/infrastructure/storage-reservations.d.ts +2 -0
  460. package/dist/infrastructure/storage-reservations.js +150 -0
  461. package/dist/infrastructure/storage-reservations.js.map +1 -0
  462. package/dist/infrastructure/storage-reservations.types.d.ts +21 -0
  463. package/dist/infrastructure/storage-reservations.types.js +2 -0
  464. package/dist/infrastructure/storage-reservations.types.js.map +1 -0
  465. package/dist/infrastructure/task-queue-http.constants.d.ts +2 -0
  466. package/dist/infrastructure/task-queue-http.constants.js +3 -0
  467. package/dist/infrastructure/task-queue-http.constants.js.map +1 -0
  468. package/dist/infrastructure/task-queue-http.d.ts +4 -0
  469. package/dist/infrastructure/task-queue-http.js +158 -0
  470. package/dist/infrastructure/task-queue-http.js.map +1 -0
  471. package/dist/infrastructure/task-queue-http.types.d.ts +16 -0
  472. package/dist/infrastructure/task-queue-http.types.js +2 -0
  473. package/dist/infrastructure/task-queue-http.types.js.map +1 -0
  474. package/dist/infrastructure/task-queue.d.ts +2 -0
  475. package/dist/infrastructure/task-queue.js +134 -0
  476. package/dist/infrastructure/task-queue.js.map +1 -0
  477. package/dist/infrastructure/transfer.js +17 -0
  478. package/dist/infrastructure/transfer.js.map +1 -1
  479. package/dist/infrastructure/workflow-checkpoint.constants.d.ts +1 -0
  480. package/dist/infrastructure/workflow-checkpoint.constants.js +2 -0
  481. package/dist/infrastructure/workflow-checkpoint.constants.js.map +1 -0
  482. package/dist/infrastructure/workflow-checkpoint.d.ts +3 -0
  483. package/dist/infrastructure/workflow-checkpoint.js +77 -0
  484. package/dist/infrastructure/workflow-checkpoint.js.map +1 -0
  485. package/dist/infrastructure/workflow-checkpoint.types.d.ts +4 -0
  486. package/dist/infrastructure/workflow-checkpoint.types.js +2 -0
  487. package/dist/infrastructure/workflow-checkpoint.types.js.map +1 -0
  488. package/dist/providers/agents.d.ts +2 -0
  489. package/dist/providers/agents.js +1 -0
  490. package/dist/providers/agents.js.map +1 -1
  491. package/dist/providers/cloud.constants.d.ts +1 -0
  492. package/dist/providers/cloud.constants.js +1 -0
  493. package/dist/providers/cloud.constants.js.map +1 -1
  494. package/dist/providers/container-cache.constants.d.ts +11 -0
  495. package/dist/providers/container-cache.constants.js +9 -0
  496. package/dist/providers/container-cache.constants.js.map +1 -0
  497. package/dist/providers/container-cache.d.ts +4 -0
  498. package/dist/providers/container-cache.js +48 -0
  499. package/dist/providers/container-cache.js.map +1 -0
  500. package/dist/providers/container-cache.types.d.ts +8 -0
  501. package/dist/providers/container-cache.types.js +2 -0
  502. package/dist/providers/container-cache.types.js.map +1 -0
  503. package/dist/providers/container-isolation.d.ts +2 -0
  504. package/dist/providers/container-isolation.js +29 -0
  505. package/dist/providers/container-isolation.js.map +1 -0
  506. package/dist/providers/container-mounts.js +7 -2
  507. package/dist/providers/container-mounts.js.map +1 -1
  508. package/dist/providers/container-network.d.ts +2 -0
  509. package/dist/providers/container-network.js +14 -0
  510. package/dist/providers/container-network.js.map +1 -0
  511. package/dist/providers/container-plan.js +6 -2
  512. package/dist/providers/container-plan.js.map +1 -1
  513. package/dist/providers/container.constants.d.ts +1 -0
  514. package/dist/providers/container.constants.js +1 -0
  515. package/dist/providers/container.constants.js.map +1 -1
  516. package/dist/providers/container.js +50 -3
  517. package/dist/providers/container.js.map +1 -1
  518. package/dist/providers/container.types.d.ts +5 -0
  519. package/dist/providers/container.types.js.map +1 -1
  520. package/dist/providers/daytona-command.js +10 -6
  521. package/dist/providers/daytona-command.js.map +1 -1
  522. package/dist/providers/daytona-terminal.d.ts +3 -0
  523. package/dist/providers/daytona-terminal.js +128 -0
  524. package/dist/providers/daytona-terminal.js.map +1 -0
  525. package/dist/providers/daytona.js +15 -4
  526. package/dist/providers/daytona.js.map +1 -1
  527. package/dist/providers/diagnostic-container.d.ts +4 -0
  528. package/dist/providers/diagnostic-container.js +15 -0
  529. package/dist/providers/diagnostic-container.js.map +1 -0
  530. package/dist/providers/docker.d.ts +2 -0
  531. package/dist/providers/docker.js.map +1 -1
  532. package/dist/providers/file-batches.constants.d.ts +10 -0
  533. package/dist/providers/file-batches.constants.js +57 -0
  534. package/dist/providers/file-batches.constants.js.map +1 -0
  535. package/dist/providers/file-batches.d.ts +2 -0
  536. package/dist/providers/file-batches.js +160 -0
  537. package/dist/providers/file-batches.js.map +1 -0
  538. package/dist/providers/file-upload.constants.d.ts +1 -0
  539. package/dist/providers/file-upload.constants.js +68 -0
  540. package/dist/providers/file-upload.constants.js.map +1 -0
  541. package/dist/providers/file-upload.d.ts +2 -0
  542. package/dist/providers/file-upload.js +146 -0
  543. package/dist/providers/file-upload.js.map +1 -0
  544. package/dist/providers/firecracker-command.d.ts +5 -0
  545. package/dist/providers/firecracker-command.js +114 -0
  546. package/dist/providers/firecracker-command.js.map +1 -0
  547. package/dist/providers/firecracker-files.constants.d.ts +1 -0
  548. package/dist/providers/firecracker-files.constants.js +21 -0
  549. package/dist/providers/firecracker-files.constants.js.map +1 -0
  550. package/dist/providers/firecracker-files.d.ts +3 -0
  551. package/dist/providers/firecracker-files.js +110 -0
  552. package/dist/providers/firecracker-files.js.map +1 -0
  553. package/dist/providers/firecracker-machine.d.ts +3 -0
  554. package/dist/providers/firecracker-machine.js +155 -0
  555. package/dist/providers/firecracker-machine.js.map +1 -0
  556. package/dist/providers/firecracker.constants.d.ts +10 -0
  557. package/dist/providers/firecracker.constants.js +11 -0
  558. package/dist/providers/firecracker.constants.js.map +1 -0
  559. package/dist/providers/firecracker.d.ts +4 -0
  560. package/dist/providers/firecracker.js +103 -0
  561. package/dist/providers/firecracker.js.map +1 -0
  562. package/dist/providers/firecracker.types.d.ts +36 -0
  563. package/dist/providers/firecracker.types.js +2 -0
  564. package/dist/providers/firecracker.types.js.map +1 -0
  565. package/dist/providers/local.js +2 -1
  566. package/dist/providers/local.js.map +1 -1
  567. package/dist/providers/podman.d.ts +2 -0
  568. package/dist/providers/podman.js.map +1 -1
  569. package/dist/providers/terminal-bridge.d.ts +14 -0
  570. package/dist/providers/terminal-bridge.js +100 -0
  571. package/dist/providers/terminal-bridge.js.map +1 -0
  572. package/dist/providers/vercel-directory.d.ts +2 -0
  573. package/dist/providers/vercel-directory.js +7 -0
  574. package/dist/providers/vercel-directory.js.map +1 -0
  575. package/dist/providers/vercel-files.js +3 -2
  576. package/dist/providers/vercel-files.js.map +1 -1
  577. package/dist/providers/vercel-network.d.ts +2 -0
  578. package/dist/providers/vercel-network.js +18 -0
  579. package/dist/providers/vercel-network.js.map +1 -0
  580. package/dist/providers/vercel.d.ts +1 -0
  581. package/dist/providers/vercel.js +8 -2
  582. package/dist/providers/vercel.js.map +1 -1
  583. package/dist/providers/vercel.types.d.ts +2 -0
  584. package/dist/providers/vercel.types.js.map +1 -1
  585. package/dist/providers/versions.constants.d.ts +1 -0
  586. package/dist/providers/versions.constants.js +1 -0
  587. package/dist/providers/versions.constants.js.map +1 -1
  588. package/package.json +16 -1
@@ -0,0 +1 @@
1
+ {"version":3,"file":"gates.js","sourceRoot":"","sources":["../../../src/domain/workflow/gates.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAOzC,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AACnD,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AAEpD,SAAS,QAAQ,CACf,IAA0B,EAC1B,OAA4B;IAE5B,YAAY,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC;IACvE,OAAO,IAAI,CAAC;QACV,GAAG,EAAE,OAAO,CAAC,GAAG;QAChB,KAAK,EAAE,OAAO,CAAC,KAAK,IAAI,EAAE;QAC1B,IAAI,EAAE,MAAM,CAAC,MAAM,CAAC;YAClB,IAAI;YACJ,MAAM,EAAE,OAAO,CAAC,MAAM;YACtB,MAAM,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;SAC3C,CAAC;QACF,OAAO;YACL,MAAM,IAAI,KAAK,CAAC,oDAAoD,CAAC,CAAC;QACxE,CAAC;KACF,CAAC,CAAC;AACL,CAAC;AAED,MAAM,UAAU,YAAY,CAC1B,OAA4B;IAE5B,OAAO,QAAQ,CAAC,UAAU,EAAE,OAAO,CAAC,CAAC;AACvC,CAAC;AAED,MAAM,UAAU,SAAS,CACvB,OAA4B;IAE5B,OAAO,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC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,CAAC,CAC5D,CAAC;IACF,KAAK,MAAM,QAAQ,IAAI,SAAS,EAAE,CAAC;QACjC,MAAM,IAAI,GAAG,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,GAAG,CAAE,CAAC;QACxC,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;QACpC,MAAM,CAAC,QAAQ,GAAG,QAAQ,CAAC;QAC3B,IAAI,QAAQ,CAAC,MAAM,KAAK,QAAQ,EAAE,CAAC;YACjC,MAAM,CAAC,KAAK,GAAG,GAAG,IAAI,CAAC,GAAG,gBAAgB,QAAQ,CAAC,KAAK,KAAK,QAAQ,CAAC,MAAM,EAAE,CAAC;YAC/E,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC;YAC7C,OAAO,CAAC,MAAM,CAAC,IAAI,EAAE,UAAU,CAAC,CAAC;YACjC,SAAS;QACX,CAAC;QACD,OAAO,CAAC,MAAM,CAAC,GAAG,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC;QACnC,OAAO,CAAC,MAAM,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;IAC/B,CAAC;AACH,CAAC","sourcesContent":["import { randomUUID } from \"node:crypto\";\nimport type { Task, WorkflowExecutionState } from \"../workflow.types.ts\";\nimport type {\n WorkflowDecisionRecord,\n WorkflowGate,\n WorkflowGateOptions,\n} from \"./gates.types.ts\";\nimport { task } from \"./task.ts\";\nimport { gateActions } from \"./gates.constants.ts\";\nimport { validateGate } from \"./gate-validation.ts\";\n\nfunction gateTask(\n kind: WorkflowGate[\"kind\"],\n options: WorkflowGateOptions,\n): Task<WorkflowDecisionRecord> {\n validateGate({ kind, prompt: options.prompt, actors: options.actors });\n return task({\n key: options.key,\n after: options.after ?? [],\n gate: Object.freeze({\n kind,\n prompt: options.prompt,\n actors: Object.freeze([...options.actors]),\n }),\n perform() {\n throw new Error(\"Workflow gates must be scheduled with a checkpoint\");\n },\n });\n}\n\nexport function approvalTask(\n options: WorkflowGateOptions,\n): Task<WorkflowDecisionRecord> {\n return gateTask(\"approval\", options);\n}\n\nexport function pauseTask(\n options: WorkflowGateOptions,\n): Task<WorkflowDecisionRecord> {\n return gateTask(\"pause\", options);\n}\n\nexport async function pauseGate(\n item: Task,\n runtime: WorkflowExecutionState,\n): Promise<void> {\n const gate = item.gate!;\n runtime.record(item).pause = Object.freeze({\n ...gate,\n id: randomUUID(),\n requestedAt: new Date().toISOString(),\n });\n runtime.finish(item, \"paused\");\n await runtime.persist();\n}\n\nexport function prepareDecisions(\n runtime: WorkflowExecutionState,\n): readonly WorkflowDecisionRecord[] {\n const decisions = (runtime.options.decisions ?? []).map((decision) =>\n Object.freeze({\n executionId: decision.executionId,\n key: decision.key,\n requestId: decision.requestId,\n action: decision.action,\n actor: decision.actor,\n reason: decision.reason,\n decidedAt: new Date().toISOString(),\n }),\n );\n const pending = new Map(\n [...runtime.records.keys()].map((item) => [item.key, item]),\n );\n const seen = new Set<string>();\n for (const decision of decisions) {\n const item = pending.get(decision.key);\n const record = item && runtime.record(item);\n const request = record?.pause;\n if (\n !record ||\n !request ||\n record.status !== \"paused\" ||\n record.decision ||\n decision.executionId !== runtime.executionId ||\n decision.requestId !== request.id ||\n seen.has(decision.key) ||\n typeof decision.actor !== \"string\" ||\n !request.actors.includes(decision.actor) ||\n typeof decision.reason !== \"string\" ||\n !decision.reason.trim() ||\n ![gateActions[request.kind], \"reject\"].includes(decision.action)\n )\n throw new Error(\n `Invalid or unauthorized workflow decision: ${decision.key}`,\n );\n seen.add(decision.key);\n }\n return decisions;\n}\n\nexport function applyDecisions(\n runtime: WorkflowExecutionState,\n decisions: readonly WorkflowDecisionRecord[],\n): void {\n const pending = new Map(\n [...runtime.records.keys()].map((item) => [item.key, item]),\n );\n for (const decision of decisions) {\n const item = pending.get(decision.key)!;\n const record = runtime.record(item);\n record.decision = decision;\n if (decision.action === \"reject\") {\n record.error = `${item.key} rejected by ${decision.actor}: ${decision.reason}`;\n runtime.errors.push(new Error(record.error));\n runtime.finish(item, \"rejected\");\n continue;\n }\n runtime.values.set(item, decision);\n runtime.finish(item, \"done\");\n }\n}\n"]}
@@ -0,0 +1,27 @@
1
+ import type { Task } from "../workflow.types.ts";
2
+ export interface WorkflowGate {
3
+ readonly kind: "approval" | "pause";
4
+ readonly prompt: string;
5
+ readonly actors: readonly string[];
6
+ }
7
+ export interface WorkflowGateOptions {
8
+ readonly key: string;
9
+ readonly after?: readonly Task[];
10
+ readonly prompt: string;
11
+ readonly actors: readonly string[];
12
+ }
13
+ export interface WorkflowPauseRequest extends WorkflowGate {
14
+ readonly id: string;
15
+ readonly requestedAt: string;
16
+ }
17
+ export interface WorkflowDecision {
18
+ readonly executionId: string;
19
+ readonly key: string;
20
+ readonly requestId: string;
21
+ readonly action: "approve" | "resume" | "reject";
22
+ readonly actor: string;
23
+ readonly reason: string;
24
+ }
25
+ export interface WorkflowDecisionRecord extends WorkflowDecision {
26
+ readonly decidedAt: string;
27
+ }
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=gates.types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"gates.types.js","sourceRoot":"","sources":["../../../src/domain/workflow/gates.types.ts"],"names":[],"mappings":"","sourcesContent":["import type { Task } from \"../workflow.types.ts\";\n\nexport interface WorkflowGate {\n readonly kind: \"approval\" | \"pause\";\n readonly prompt: string;\n readonly actors: readonly string[];\n}\n\nexport interface WorkflowGateOptions {\n readonly key: string;\n readonly after?: readonly Task[];\n readonly prompt: string;\n readonly actors: readonly string[];\n}\n\nexport interface WorkflowPauseRequest extends WorkflowGate {\n readonly id: string;\n readonly requestedAt: string;\n}\n\nexport interface WorkflowDecision {\n readonly executionId: string;\n readonly key: string;\n readonly requestId: string;\n readonly action: \"approve\" | \"resume\" | \"reject\";\n readonly actor: string;\n readonly reason: string;\n}\n\nexport interface WorkflowDecisionRecord extends WorkflowDecision {\n readonly decidedAt: string;\n}\n"]}
@@ -1,11 +1,16 @@
1
+ import { checkpointValue } from "./checkpoint-value.js";
2
+ import { WorkflowBudgetExceeded } from "./budget.js";
1
3
  import { setTimeout as delay } from "node:timers/promises";
2
4
  export async function runTask(item, runtime) {
3
5
  const { record, signal, context, emit, finish, values, errors, options, stop, } = runtime;
4
6
  const state = record(item);
5
7
  state.status = "active";
8
+ delete state.error;
9
+ delete state.finishedAt;
6
10
  state.startedAt = new Date().toISOString();
7
11
  emit({ type: "task", key: item.key, status: "active", attempt: 0 });
8
12
  try {
13
+ await runtime.persist();
9
14
  signal.throwIfAborted();
10
15
  if (item.condition && !(await item.condition(context(item, 0, signal)))) {
11
16
  signal.throwIfAborted();
@@ -13,29 +18,39 @@ export async function runTask(item, runtime) {
13
18
  return;
14
19
  }
15
20
  const limit = item.retry?.attempts ?? 1;
16
- for (let attempt = 1; attempt <= limit; attempt++) {
21
+ const priorAttempts = state.attempts;
22
+ for (let cycle = 1; cycle <= limit; cycle++) {
23
+ const attempt = priorAttempts + cycle;
17
24
  signal.throwIfAborted();
25
+ runtime.accounting.admit();
18
26
  state.attempts = attempt;
27
+ emit({ type: "attempt", key: item.key, attempt });
28
+ await runtime.persist();
19
29
  const deadline = new AbortController();
20
30
  const timer = item.timeoutMs === undefined
21
31
  ? undefined
22
32
  : setTimeout(() => deadline.abort(new Error(`${item.key} timed out`)), item.timeoutMs);
23
33
  const taskSignal = AbortSignal.any([signal, deadline.signal]);
24
34
  try {
35
+ taskSignal.throwIfAborted();
25
36
  const value = await item.perform(context(item, attempt, taskSignal));
26
37
  taskSignal.throwIfAborted();
38
+ if (options.checkpoint)
39
+ checkpointValue(value);
27
40
  values.set(item, value);
28
41
  finish(item, "done");
29
42
  return;
30
43
  }
31
44
  catch (error) {
45
+ runtime.closeAttempt(item);
32
46
  if (signal.aborted ||
33
- attempt === limit ||
47
+ cycle === limit ||
34
48
  item.retry?.accepts?.(error, attempt) === false)
35
49
  throw error;
36
50
  emit({ type: "retry", key: item.key, attempt });
37
51
  }
38
52
  finally {
53
+ runtime.closeAttempt(item);
39
54
  clearTimeout(timer);
40
55
  }
41
56
  await delay(item.retry?.delayMs ?? 0, undefined, { signal });
@@ -43,7 +58,8 @@ export async function runTask(item, runtime) {
43
58
  }
44
59
  catch (error) {
45
60
  state.error = error instanceof Error ? error.message : String(error);
46
- if (signal.aborted)
61
+ if (signal.aborted ||
62
+ (error instanceof WorkflowBudgetExceeded && runtime.accounting.exhausted))
47
63
  finish(item, "cancelled");
48
64
  else {
49
65
  errors.push(error);
@@ -52,5 +68,8 @@ export async function runTask(item, runtime) {
52
68
  stop.abort(error);
53
69
  }
54
70
  }
71
+ finally {
72
+ await runtime.persist();
73
+ }
55
74
  }
56
75
  //# sourceMappingURL=task-runner.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"task-runner.js","sourceRoot":"","sources":["../../../src/domain/workflow/task-runner.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,IAAI,KAAK,EAAE,MAAM,sBAAsB,CAAC;AAG3D,MAAM,CAAC,KAAK,UAAU,OAAO,CAC3B,IAAU,EACV,OAA+B;IAE/B,MAAM,EACJ,MAAM,EACN,MAAM,EACN,OAAO,EACP,IAAI,EACJ,MAAM,EACN,MAAM,EACN,MAAM,EACN,OAAO,EACP,IAAI,GACL,GAAG,OAAO,CAAC;IACZ,MAAM,KAAK,GAAG,MAAM,CAAC,IAAI,CAAC,CAAC;IAC3B,KAAK,CAAC,MAAM,GAAG,QAAQ,CAAC;IACxB,KAAK,CAAC,SAAS,GAAG,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE,CAAC;IAC3C,IAAI,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,GAAG,EAAE,IAAI,CAAC,GAAG,EAAE,MAAM,EAAE,QAAQ,EAAE,OAAO,EAAE,CAAC,EAAE,CAAC,CAAC;IACpE,IAAI,CAAC;QACH,MAAM,CAAC,cAAc,EAAE,CAAC;QACxB,IAAI,IAAI,CAA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AC,OAAO;oBACd,OAAO,KAAK,KAAK;oBACjB,IAAI,CAAC,KAAK,EAAE,OAAO,EAAE,CAAC,KAAK,EAAE,OAAO,CAAC,KAAK,KAAK;oBAE/C,MAAM,KAAK,CAAC;gBACd,IAAI,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,GAAG,EAAE,IAAI,CAAC,GAAG,EAAE,OAAO,EAAE,CAAC,CAAC;YAClD,CAAC;oBAAS,CAAC;gBACT,YAAY,CAAC,KAAK,CAAC,CAAC;YACtB,CAAC;YACD,MAAM,KAAK,CAAC,IAAI,CAAC,KAAK,EAAE,OAAO,IAAI,CAAC,EAAE,SAAS,EAAE,EAAE,MAAM,EAAE,CAAC,CAAC;QAC/D,CAAC;IACH,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,KAAK,CAAC,KAAK,GAAG,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QACrE,IAAI,MAAM,CAAC,OAAO;YAAE,MAAM,CAAC,IAAI,EAAE,WAAW,CAAC,CAAC;aACzC,CAAC;YACJ,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;YACnB,MAAM,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC;YACvB,IAAI,OAAO,CAAC,WAAW,KAAK,KAAK;gBAAE,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;QACvD,CAAC;IACH,CAAC;AACH,CAAC","sourcesContent":["import { setTimeout as delay } from \"node:timers/promises\";\nimport type { Task, WorkflowExecutionState } from \"../workflow.types.ts\";\n\nexport async function runTask(\n item: Task,\n runtime: WorkflowExecutionState,\n): Promise<void> {\n const {\n record,\n signal,\n context,\n emit,\n finish,\n values,\n errors,\n options,\n stop,\n } = runtime;\n const state = record(item);\n state.status = \"active\";\n state.startedAt = new Date().toISOString();\n emit({ type: \"task\", key: item.key, status: \"active\", attempt: 0 });\n try {\n signal.throwIfAborted();\n if (item.condition && !(await item.condition(context(item, 0, signal)))) {\n signal.throwIfAborted();\n finish(item, \"skipped\");\n return;\n }\n const limit = item.retry?.attempts ?? 1;\n for (let attempt = 1; attempt <= limit; attempt++) {\n signal.throwIfAborted();\n state.attempts = attempt;\n const deadline = new AbortController();\n const timer =\n item.timeoutMs === undefined\n ? undefined\n : setTimeout(\n () => deadline.abort(new Error(`${item.key} timed out`)),\n item.timeoutMs,\n );\n const taskSignal = AbortSignal.any([signal, deadline.signal]);\n try {\n const value = await item.perform(context(item, attempt, taskSignal));\n taskSignal.throwIfAborted();\n values.set(item, value);\n finish(item, \"done\");\n return;\n } catch (error) {\n if (\n signal.aborted ||\n attempt === limit ||\n item.retry?.accepts?.(error, attempt) === false\n )\n throw error;\n emit({ type: \"retry\", key: item.key, attempt });\n } finally {\n clearTimeout(timer);\n }\n await delay(item.retry?.delayMs ?? 0, undefined, { signal });\n }\n } catch (error) {\n state.error = error instanceof Error ? error.message : String(error);\n if (signal.aborted) finish(item, \"cancelled\");\n else {\n errors.push(error);\n finish(item, \"failed\");\n if (options.stopOnError !== false) stop.abort(error);\n }\n }\n}\n"]}
1
+ {"version":3,"file":"task-runner.js","sourceRoot":"","sources":["../../../src/domain/workflow/task-runner.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AACxD,OAAO,EAAE,sBAAsB,EAAE,MAAM,aAAa,CAAC;AACrD,OAAO,EAAE,UAAU,IAAI,KAAK,EAAE,MAAM,sBAAsB,CAAC;AAG3D,MAAM,CAAC,KAAK,UAAU,OAAO,CAC3B,IAAU,EACV,OAA+B;IAE/B,MAAM,EACJ,MAAM,EACN,MAAM,EACN,OAAO,EACP,IAAI,EACJ,MAAM,EACN,MAAM,EACN,MAAM,EACN,OAAO,EACP,IAAI,GACL,GAAG,OAAO,CAAC;IACZ,MAAM,KAAK,GAAG,MAAM,CAAC,IAAI,CAAC,CAAC;IAC3B,KAAK,CAAC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CAAC,KAAK,CAAC,CAAC;YACtB,CAAC;YACD,MAAM,KAAK,CAAC,IAAI,CAAC,KAAK,EAAE,OAAO,IAAI,CAAC,EAAE,SAAS,EAAE,EAAE,MAAM,EAAE,CAAC,CAAC;QAC/D,CAAC;IACH,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,KAAK,CAAC,KAAK,GAAG,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QACrE,IACE,MAAM,CAAC,OAAO;YACd,CAAC,KAAK,YAAY,sBAAsB,IAAI,OAAO,CAAC,UAAU,CAAC,SAAS,CAAC;YAEzE,MAAM,CAAC,IAAI,EAAE,WAAW,CAAC,CAAC;aACvB,CAAC;YACJ,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;YACnB,MAAM,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC;YACvB,IAAI,OAAO,CAAC,WAAW,KAAK,KAAK;gBAAE,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;QACvD,CAAC;IACH,CAAC;YAAS,CAAC;QACT,MAAM,OAAO,CAAC,OAAO,EAAE,CAAC;IAC1B,CAAC;AACH,CAAC","sourcesContent":["import { checkpointValue } from \"./checkpoint-value.ts\";\nimport { WorkflowBudgetExceeded } from \"./budget.ts\";\nimport { setTimeout as delay } from \"node:timers/promises\";\nimport type { Task, WorkflowExecutionState } from \"../workflow.types.ts\";\n\nexport async function runTask(\n item: Task,\n runtime: WorkflowExecutionState,\n): Promise<void> {\n const {\n record,\n signal,\n context,\n emit,\n finish,\n values,\n errors,\n options,\n stop,\n } = runtime;\n const state = record(item);\n state.status = \"active\";\n delete state.error;\n delete state.finishedAt;\n state.startedAt = new Date().toISOString();\n emit({ type: \"task\", key: item.key, status: \"active\", attempt: 0 });\n try {\n await runtime.persist();\n signal.throwIfAborted();\n if (item.condition && !(await item.condition(context(item, 0, signal)))) {\n signal.throwIfAborted();\n finish(item, \"skipped\");\n return;\n }\n const limit = item.retry?.attempts ?? 1;\n const priorAttempts = state.attempts;\n for (let cycle = 1; cycle <= limit; cycle++) {\n const attempt = priorAttempts + cycle;\n signal.throwIfAborted();\n runtime.accounting.admit();\n state.attempts = attempt;\n emit({ type: \"attempt\", key: item.key, attempt });\n await runtime.persist();\n const deadline = new AbortController();\n const timer =\n item.timeoutMs === undefined\n ? undefined\n : setTimeout(\n () => deadline.abort(new Error(`${item.key} timed out`)),\n item.timeoutMs,\n );\n const taskSignal = AbortSignal.any([signal, deadline.signal]);\n try {\n taskSignal.throwIfAborted();\n const value = await item.perform(context(item, attempt, taskSignal));\n taskSignal.throwIfAborted();\n if (options.checkpoint) checkpointValue(value);\n values.set(item, value);\n finish(item, \"done\");\n return;\n } catch (error) {\n runtime.closeAttempt(item);\n if (\n signal.aborted ||\n cycle === limit ||\n item.retry?.accepts?.(error, attempt) === false\n )\n throw error;\n emit({ type: \"retry\", key: item.key, attempt });\n } finally {\n runtime.closeAttempt(item);\n clearTimeout(timer);\n }\n await delay(item.retry?.delayMs ?? 0, undefined, { signal });\n }\n } catch (error) {\n state.error = error instanceof Error ? error.message : String(error);\n if (\n signal.aborted ||\n (error instanceof WorkflowBudgetExceeded && runtime.accounting.exhausted)\n )\n finish(item, \"cancelled\");\n else {\n errors.push(error);\n finish(item, \"failed\");\n if (options.stopOnError !== false) stop.abort(error);\n }\n } finally {\n await runtime.persist();\n }\n}\n"]}
@@ -0,0 +1,2 @@
1
+ export declare const maxUsageReceiptsPerTask = 65536;
2
+ export declare const maxUsageReceiptLength = 512;
@@ -0,0 +1,3 @@
1
+ export const maxUsageReceiptsPerTask = 65_536;
2
+ export const maxUsageReceiptLength = 512;
3
+ //# sourceMappingURL=usage-receipt.constants.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"usage-receipt.constants.js","sourceRoot":"","sources":["../../../src/domain/workflow/usage-receipt.constants.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,uBAAuB,GAAG,MAAM,CAAC;AAC9C,MAAM,CAAC,MAAM,qBAAqB,GAAG,GAAG,CAAC","sourcesContent":["export const maxUsageReceiptsPerTask = 65_536;\nexport const maxUsageReceiptLength = 512;\n"]}
@@ -0,0 +1,2 @@
1
+ export declare function validateUsageReceipt(receipt: unknown): asserts receipt is string;
2
+ export declare function validateUsageReceipts(receipts: unknown): void;
@@ -0,0 +1,18 @@
1
+ import { maxUsageReceiptLength, maxUsageReceiptsPerTask, } from "./usage-receipt.constants.js";
2
+ export function validateUsageReceipt(receipt) {
3
+ if (typeof receipt !== "string" ||
4
+ !receipt.length ||
5
+ receipt.length > maxUsageReceiptLength)
6
+ throw new Error("Invalid workflow usage receipt");
7
+ }
8
+ export function validateUsageReceipts(receipts) {
9
+ if (receipts === undefined)
10
+ return;
11
+ if (!Array.isArray(receipts) || receipts.length > maxUsageReceiptsPerTask)
12
+ throw new Error("Invalid workflow usage receipts");
13
+ for (const receipt of receipts)
14
+ validateUsageReceipt(receipt);
15
+ if (new Set(receipts).size !== receipts.length)
16
+ throw new Error("Duplicate workflow usage receipts");
17
+ }
18
+ //# sourceMappingURL=usage-receipt.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"usage-receipt.js","sourceRoot":"","sources":["../../../src/domain/workflow/usage-receipt.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,qBAAqB,EACrB,uBAAuB,GACxB,MAAM,8BAA8B,CAAC;AAEtC,MAAM,UAAU,oBAAoB,CAClC,OAAgB;IAEhB,IACE,OAAO,OAAO,KAAK,QAAQ;QAC3B,CAAC,OAAO,CAAC,MAAM;QACf,OAAO,CAAC,MAAM,GAAG,qBAAqB;QAEtC,MAAM,IAAI,KAAK,CAAC,gCAAgC,CAAC,CAAC;AACtD,CAAC;AACD,MAAM,UAAU,qBAAqB,CAAC,QAAiB;IACrD,IAAI,QAAQ,KAAK,SAAS;QAAE,OAAO;IACnC,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,QAAQ,CAAC,IAAI,QAAQ,CAAC,MAAM,GAAG,uBAAuB;QACvE,MAAM,IAAI,KAAK,CAAC,iCAAiC,CAAC,CAAC;IACrD,KAAK,MAAM,OAAO,IAAI,QAAQ;QAAE,oBAAoB,CAAC,OAAO,CAAC,CAAC;IAC9D,IAAI,IAAI,GAAG,CAAC,QAAQ,CAAC,CAAC,IAAI,KAAK,QAAQ,CAAC,MAAM;QAC5C,MAAM,IAAI,KAAK,CAAC,mCAAmC,CAAC,CAAC;AACzD,CAAC","sourcesContent":["import {\n maxUsageReceiptLength,\n maxUsageReceiptsPerTask,\n} from \"./usage-receipt.constants.ts\";\n\nexport function validateUsageReceipt(\n receipt: unknown,\n): asserts receipt is string {\n if (\n typeof receipt !== \"string\" ||\n !receipt.length ||\n receipt.length > maxUsageReceiptLength\n )\n throw new Error(\"Invalid workflow usage receipt\");\n}\nexport function validateUsageReceipts(receipts: unknown): void {\n if (receipts === undefined) return;\n if (!Array.isArray(receipts) || receipts.length > maxUsageReceiptsPerTask)\n throw new Error(\"Invalid workflow usage receipts\");\n for (const receipt of receipts) validateUsageReceipt(receipt);\n if (new Set(receipts).size !== receipts.length)\n throw new Error(\"Duplicate workflow usage receipts\");\n}\n"]}
@@ -1,3 +1,4 @@
1
+ import { validateGate } from "./gate-validation.js";
1
2
  export function positive(value, label) {
2
3
  if (!Number.isSafeInteger(value) || value < 1)
3
4
  throw new Error(`${label} must be a positive integer`);
@@ -8,6 +9,11 @@ export function validate(tasks) {
8
9
  const visiting = new Set();
9
10
  const visited = new Set();
10
11
  for (const item of tasks) {
12
+ if (item.gate) {
13
+ validateGate(item.gate);
14
+ if (item.condition || item.retry || item.timeoutMs !== undefined)
15
+ throw new Error("Workflow gates cannot have conditions, retries or timeouts");
16
+ }
11
17
  if (keys.has(item.key))
12
18
  throw new Error(`Duplicate task: ${item.key}`);
13
19
  keys.add(item.key);
@@ -1 +1 @@
1
- {"version":3,"file":"validation.js","sourceRoot":"","sources":["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KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;QAC1B,QAAQ,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;QACtB,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;IACpB,CAAC;IACD,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;AACvB,CAAC","sourcesContent":["import type { Task } from \"../workflow.types.ts\";\n\nexport function positive(value: number, label: string): void {\n if (!Number.isSafeInteger(value) || value < 1)\n throw new Error(`${label} must be a positive integer`);\n}\n\nexport function validate(tasks: readonly Task[]): void {\n const keys = new Set<string>();\n const members = new Set(tasks);\n const visiting = new Set<Task>();\n const visited = new Set<Task>();\n for (const item of tasks) {\n if (keys.has(item.key)) throw new Error(`Duplicate task: ${item.key}`);\n keys.add(item.key);\n for (const dependency of item.after)\n if (!members.has(dependency))\n throw new Error(`${item.key}: missing dependency ${dependency.key}`);\n }\n function visit(item: Task): void {\n if (visiting.has(item)) throw new Error(`Dependency cycle at ${item.key}`);\n if (visited.has(item)) return;\n visiting.add(item);\n item.after.forEach(visit);\n visiting.delete(item);\n visited.add(item);\n }\n tasks.forEach(visit);\n}\n"]}
1
+ {"version":3,"file":"validation.js","sourceRoot":"","sources":["../../../src/domain/workflow/validation.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AAGpD,MAAM,UAAU,QAAQ,CAAC,KAAa,EAAE,KAAa;IACnD,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,KAAK,CAAC,IAAI,KAAK,GAAG,CAAC;QAC3C,MAAM,IAAI,KAAK,CAAC,GAAG,KAAK,6BAA6B,CAAC,CAAC;AAC3D,CAAC;AAED,MAAM,UAAU,QAAQ,CAAC,KAAsB;IAC7C,MAAM,IAAI,GAAG,IAAI,GAAG,EAAU,CAAC;IAC/B,MAAM,OAAO,GAAG,IAAI,GAAG,CAAC,KAAK,CAAC,CAAC;IAC/B,MAAM,QAAQ,GAAG,IAAI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AC,KAAK,CAAC,CAAC;AACvB,CAAC","sourcesContent":["import { validateGate } from \"./gate-validation.ts\";\nimport type { Task } from \"../workflow.types.ts\";\n\nexport function positive(value: number, label: string): void {\n if (!Number.isSafeInteger(value) || value < 1)\n throw new Error(`${label} must be a positive integer`);\n}\n\nexport function validate(tasks: readonly Task[]): void {\n const keys = new Set<string>();\n const members = new Set(tasks);\n const visiting = new Set<Task>();\n const visited = new Set<Task>();\n for (const item of tasks) {\n if (item.gate) {\n validateGate(item.gate);\n if (item.condition || item.retry || item.timeoutMs !== undefined)\n throw new Error(\n \"Workflow gates cannot have conditions, retries or timeouts\",\n );\n }\n if (keys.has(item.key)) throw new Error(`Duplicate task: ${item.key}`);\n keys.add(item.key);\n for (const dependency of item.after)\n if (!members.has(dependency))\n throw new Error(`${item.key}: missing dependency ${dependency.key}`);\n }\n function visit(item: Task): void {\n if (visiting.has(item)) throw new Error(`Dependency cycle at ${item.key}`);\n if (visited.has(item)) return;\n visiting.add(item);\n item.after.forEach(visit);\n visiting.delete(item);\n visited.add(item);\n }\n tasks.forEach(visit);\n}\n"]}
@@ -1,58 +1,93 @@
1
+ import { applyDecisions, pauseGate, prepareDecisions } from "./gates.js";
2
+ import { openCheckpoint } from "./checkpoint.js";
1
3
  import { WorkflowFailure } from "./failure.js";
2
4
  import { runTask } from "./task-runner.js";
3
5
  import { positive } from "./validation.js";
4
6
  import { workflowState } from "./workflow-state.js";
5
7
  export async function schedule(name, tasks, options) {
8
+ if (!options.checkpoint &&
9
+ (tasks.some((item) => item.gate) || options.decisions?.length))
10
+ throw new Error("Workflow gates and decisions require a checkpoint");
11
+ if (options.decisions && !options.decisions.length)
12
+ throw new Error("Workflow decisions cannot be empty");
13
+ const checkpoint = options.checkpoint
14
+ ? await openCheckpoint(name, tasks, options.checkpoint)
15
+ : undefined;
16
+ try {
17
+ return await scheduleRun(name, tasks, options, checkpoint);
18
+ }
19
+ finally {
20
+ await checkpoint?.release();
21
+ }
22
+ }
23
+ async function scheduleRun(name, tasks, options, checkpoint) {
6
24
  const concurrency = options.concurrency ?? 1;
7
25
  positive(concurrency, "concurrency");
8
- const state = workflowState(name, tasks, options);
26
+ const state = workflowState(name, tasks, options, checkpoint);
9
27
  const { executionId, signal, values, records, errors, observerErrors, record, emit, finish, } = state;
10
28
  const active = new Map();
29
+ const started = Date.now();
30
+ const decisions = prepareDecisions(state);
11
31
  emit({ type: "start" });
12
- while (true) {
13
- let changed = false;
14
- for (const item of tasks) {
15
- if (record(item).status !== "waiting")
16
- continue;
17
- if (signal.aborted) {
18
- finish(item, "cancelled");
32
+ applyDecisions(state, decisions);
33
+ await state.persist();
34
+ try {
35
+ while (true) {
36
+ let changed = false;
37
+ for (const item of tasks) {
38
+ if (record(item).status !== "waiting")
39
+ continue;
40
+ if (signal.aborted || state.accounting.exhausted) {
41
+ finish(item, "cancelled");
42
+ changed = true;
43
+ continue;
44
+ }
45
+ const dependencies = item.after.map((dependency) => record(dependency).status);
46
+ if (dependencies.some((status) => ["failed", "skipped", "cancelled", "rejected"].includes(status))) {
47
+ finish(item, "skipped");
48
+ changed = true;
49
+ continue;
50
+ }
51
+ if (active.size >= concurrency ||
52
+ item.after.some((dependency) => active.has(dependency)) ||
53
+ !dependencies.every((status) => status === "done"))
54
+ continue;
55
+ const running = (item.gate ? pauseGate(item, state) : runTask(item, state)).finally(() => {
56
+ active.delete(item);
57
+ });
58
+ active.set(item, running);
19
59
  changed = true;
20
- continue;
21
60
  }
22
- const dependencies = item.after.map((dependency) => record(dependency).status);
23
- if (dependencies.some((status) => ["failed", "skipped", "cancelled"].includes(status))) {
24
- finish(item, "skipped");
25
- changed = true;
61
+ if (active.size) {
62
+ await Promise.race(active.values());
26
63
  continue;
27
64
  }
28
- if (active.size >= concurrency ||
29
- !dependencies.every((status) => status === "done"))
30
- continue;
31
- const running = runTask(item, state).finally(() => {
32
- active.delete(item);
33
- });
34
- active.set(item, running);
35
- changed = true;
36
- }
37
- if (active.size) {
38
- await Promise.race(active.values());
39
- continue;
65
+ if (!changed)
66
+ break;
40
67
  }
41
- if (!changed)
42
- break;
43
68
  }
69
+ catch (error) {
70
+ state.stop.abort(error);
71
+ await Promise.allSettled(active.values());
72
+ throw error;
73
+ }
74
+ await state.persist();
44
75
  if (options.signal?.aborted)
45
76
  errors.push(options.signal.reason);
46
- const status = options.signal?.aborted
47
- ? "cancelled"
48
- : errors.length
49
- ? "failed"
50
- : "done";
51
- emit({ type: "finish" });
77
+ const paused = [...records.values()].some((entry) => entry.status === "paused");
78
+ let status = "done";
79
+ if (paused)
80
+ status = "paused";
81
+ if (errors.length)
82
+ status = "failed";
83
+ if (options.signal?.aborted)
84
+ status = "cancelled";
85
+ emit({ type: "finish", status, durationMs: Date.now() - started });
52
86
  const result = Object.freeze({
53
87
  executionId,
54
88
  name,
55
89
  status,
90
+ usage: state.accounting.snapshot(),
56
91
  tasks: Object.freeze([...records.values()].map((value) => Object.freeze({ ...value }))),
57
92
  errors: Object.freeze(errors),
58
93
  observerErrors: Object.freeze(observerErrors),
@@ -1 +1 @@
1
- {"version":3,"file":"workflow-scheduler.js","sourceRoot":"","sources":["../../../src/domain/workflow/workflow-scheduler.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAC/C,OAAO,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAC3C,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAC3C,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAEpD,MAAM,CAAC,KAAK,UAAU,QAAQ,CAC5B,IAAY,EACZ,KAAsB,EACtB,OAAwB;IAExB,MAAM,WAAW,GAAG,OAAO,CAAC,WAAW,IAAI,CAAC,CAAC;IAC7C,QAAQ,CAAC,WAAW,EAAE,aAAa,CAAC,CAAC;IACrC,MAAM,KAAK,GAAG,aAAa,CAAC,IAAI,EAAE,KAAK,EAAE,OAAO,CAAC,CAAC;IAClD,MAAM,EACJ,WAAW,EAC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AAI,CAAM,CAAC;QAC/B,CAAC;QACD,MAAM;YACJ,IAAI,MAAM,KAAK,MAAM;gBAAE,MAAM,IAAI,eAAe,CAAC,MAAM,CAAC,CAAC;QAC3D,CAAC;KACF,CAAC,CAAC;IACH,OAAO,MAAM,CAAC;AAChB,CAAC","sourcesContent":["import type {\n Task,\n WorkflowOptions,\n WorkflowResult,\n} from \"../workflow.types.ts\";\nimport { WorkflowFailure } from \"./failure.ts\";\nimport { runTask } from \"./task-runner.ts\";\nimport { positive } from \"./validation.ts\";\nimport { workflowState } from \"./workflow-state.ts\";\n\nexport async function schedule(\n name: string,\n tasks: readonly Task[],\n options: WorkflowOptions,\n): Promise<WorkflowResult> {\n const concurrency = options.concurrency ?? 1;\n positive(concurrency, \"concurrency\");\n const state = workflowState(name, tasks, options);\n const {\n executionId,\n signal,\n values,\n records,\n errors,\n observerErrors,\n record,\n emit,\n finish,\n } = state;\n const active = new Map<Task, Promise<void>>();\n emit({ type: \"start\" });\n while (true) {\n let changed = false;\n for (const item of tasks) {\n if (record(item).status !== \"waiting\") continue;\n if (signal.aborted) {\n finish(item, \"cancelled\");\n changed = true;\n continue;\n }\n const dependencies = item.after.map(\n (dependency) => record(dependency).status,\n );\n if (\n dependencies.some((status) =>\n [\"failed\", \"skipped\", \"cancelled\"].includes(status),\n )\n ) {\n finish(item, \"skipped\");\n changed = true;\n continue;\n }\n if (\n active.size >= concurrency ||\n !dependencies.every((status) => status === \"done\")\n )\n continue;\n const running = runTask(item, state).finally(() => {\n active.delete(item);\n });\n active.set(item, running);\n changed = true;\n }\n if (active.size) {\n await Promise.race(active.values());\n continue;\n }\n if (!changed) break;\n }\n if (options.signal?.aborted) errors.push(options.signal.reason);\n const status = options.signal?.aborted\n ? \"cancelled\"\n : errors.length\n ? \"failed\"\n : \"done\";\n emit({ type: \"finish\" });\n const result: WorkflowResult = Object.freeze({\n executionId,\n name,\n status,\n tasks: Object.freeze(\n [...records.values()].map((value) => Object.freeze({ ...value })),\n ),\n errors: Object.freeze(errors),\n observerErrors: Object.freeze(observerErrors),\n value<T>(item: Task<T>): T {\n if (!values.has(item))\n throw new Error(\n `${item.key} has no successful value in this execution`,\n );\n return values.get(item) as T;\n },\n unwrap() {\n if (status !== \"done\") throw new WorkflowFailure(result);\n },\n });\n return result;\n}\n"]}
1
+ {"version":3,"file":"workflow-scheduler.js","sourceRoot":"","sources":["../../../src/domain/workflow/workflow-scheduler.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,SAAS,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AACzE,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAOjD,OAAO,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAC/C,OAAO,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAC3C,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAC3C,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAEpD,MAAM,CAAC,KAAK,UAAU,QAAQ,CAC5B,IAAY,EACZ,KAAsB,EACtB,OAAwB;IAExB,IACE,CAAC,OAAO,CAAC,UAAU;QACnB,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,OAAO,CAAC,SAAS,EAAE,MAAM,CAAC;QAE9D,MAAM,IAAI,KAAK,CAAC,mDAAmD,CAAC,CAAC;IACvE,IAAI,OAAO,CAAC,SAAS,IAAI,CAAC,OAAO,CAAC,SAAS,CAAC,MAAM;QAChD,MAAM,IAAI,KAAK,CAAC,oCAAoC,CAAC,CAAC;IACxD,MAAM,UAAU,GAAG,OAAO,CAAC,UAAU;QACnC,CAAC,CAAC,MAAM,cAAc,CAAC,IAAI,EAAE,KAAK,EAAE,OAAO,CAAC,UAAU,CAAC;QACvD,CAAC,CAAC,SAAS,CAAC;IACd,IAAI,CAAC;QACH,OAAO,MAAM,WAAW,CAA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,CAClE;QACD,MAAM,EAAE,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC;QAC7B,cAAc,EAAE,MAAM,CAAC,MAAM,CAAC,cAAc,CAAC;QAC7C,KAAK,CAAI,IAAa;YACpB,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC;gBACnB,MAAM,IAAI,KAAK,CACb,GAAG,IAAI,CAAC,GAAG,4CAA4C,CACxD,CAAC;YACJ,OAAO,MAAM,CAAC,GAAG,CAAC,IAAI,CAAM,CAAC;QAC/B,CAAC;QACD,MAAM;YACJ,IAAI,MAAM,KAAK,MAAM;gBAAE,MAAM,IAAI,eAAe,CAAC,MAAM,CAAC,CAAC;QAC3D,CAAC;KACF,CAAC,CAAC;IACH,OAAO,MAAM,CAAC;AAChB,CAAC","sourcesContent":["import { applyDecisions, pauseGate, prepareDecisions } from \"./gates.ts\";\nimport { openCheckpoint } from \"./checkpoint.ts\";\nimport type { WorkflowCheckpointSession } from \"./checkpoint.types.ts\";\nimport type {\n Task,\n WorkflowOptions,\n WorkflowResult,\n} from \"../workflow.types.ts\";\nimport { WorkflowFailure } from \"./failure.ts\";\nimport { runTask } from \"./task-runner.ts\";\nimport { positive } from \"./validation.ts\";\nimport { workflowState } from \"./workflow-state.ts\";\n\nexport async function schedule(\n name: string,\n tasks: readonly Task[],\n options: WorkflowOptions,\n): Promise<WorkflowResult> {\n if (\n !options.checkpoint &&\n (tasks.some((item) => item.gate) || options.decisions?.length)\n )\n throw new Error(\"Workflow gates and decisions require a checkpoint\");\n if (options.decisions && !options.decisions.length)\n throw new Error(\"Workflow decisions cannot be empty\");\n const checkpoint = options.checkpoint\n ? await openCheckpoint(name, tasks, options.checkpoint)\n : undefined;\n try {\n return await scheduleRun(name, tasks, options, checkpoint);\n } finally {\n await checkpoint?.release();\n }\n}\n\nasync function scheduleRun(\n name: string,\n tasks: readonly Task[],\n options: WorkflowOptions,\n checkpoint?: WorkflowCheckpointSession,\n): Promise<WorkflowResult> {\n const concurrency = options.concurrency ?? 1;\n positive(concurrency, \"concurrency\");\n const state = workflowState(name, tasks, options, checkpoint);\n const {\n executionId,\n signal,\n values,\n records,\n errors,\n observerErrors,\n record,\n emit,\n finish,\n } = state;\n const active = new Map<Task, Promise<void>>();\n const started = Date.now();\n const decisions = prepareDecisions(state);\n emit({ type: \"start\" });\n applyDecisions(state, decisions);\n await state.persist();\n try {\n while (true) {\n let changed = false;\n for (const item of tasks) {\n if (record(item).status !== \"waiting\") continue;\n if (signal.aborted || state.accounting.exhausted) {\n finish(item, \"cancelled\");\n changed = true;\n continue;\n }\n const dependencies = item.after.map(\n (dependency) => record(dependency).status,\n );\n if (\n dependencies.some((status) =>\n [\"failed\", \"skipped\", \"cancelled\", \"rejected\"].includes(status),\n )\n ) {\n finish(item, \"skipped\");\n changed = true;\n continue;\n }\n if (\n active.size >= concurrency ||\n item.after.some((dependency) => active.has(dependency)) ||\n !dependencies.every((status) => status === \"done\")\n )\n continue;\n const running = (\n item.gate ? pauseGate(item, state) : runTask(item, state)\n ).finally(() => {\n active.delete(item);\n });\n active.set(item, running);\n changed = true;\n }\n if (active.size) {\n await Promise.race(active.values());\n continue;\n }\n if (!changed) break;\n }\n } catch (error) {\n state.stop.abort(error);\n await Promise.allSettled(active.values());\n throw error;\n }\n await state.persist();\n if (options.signal?.aborted) errors.push(options.signal.reason);\n const paused = [...records.values()].some(\n (entry) => entry.status === \"paused\",\n );\n let status: WorkflowResult[\"status\"] = \"done\";\n if (paused) status = \"paused\";\n if (errors.length) status = \"failed\";\n if (options.signal?.aborted) status = \"cancelled\";\n emit({ type: \"finish\", status, durationMs: Date.now() - started });\n const result: WorkflowResult = Object.freeze({\n executionId,\n name,\n status,\n usage: state.accounting.snapshot(),\n tasks: Object.freeze(\n [...records.values()].map((value) => Object.freeze({ ...value })),\n ),\n errors: Object.freeze(errors),\n observerErrors: Object.freeze(observerErrors),\n value<T>(item: Task<T>): T {\n if (!values.has(item))\n throw new Error(\n `${item.key} has no successful value in this execution`,\n );\n return values.get(item) as T;\n },\n unwrap() {\n if (status !== \"done\") throw new WorkflowFailure(result);\n },\n });\n return result;\n}\n"]}
@@ -1,2 +1,3 @@
1
+ import type { WorkflowCheckpointSession } from "./checkpoint.types.ts";
1
2
  import type { Task, WorkflowExecutionState, WorkflowOptions } from "../workflow.types.ts";
2
- export declare function workflowState(name: string, tasks: readonly Task[], options: WorkflowOptions): WorkflowExecutionState;
3
+ export declare function workflowState(name: string, tasks: readonly Task[], options: WorkflowOptions, checkpoint?: WorkflowCheckpointSession): WorkflowExecutionState;
@@ -1,16 +1,64 @@
1
+ import { maxUsageReceiptsPerTask } from "./usage-receipt.constants.js";
2
+ import { validateUsageReceipt } from "./usage-receipt.js";
3
+ import { workflowAccounting } from "./budget.js";
1
4
  import { randomUUID } from "node:crypto";
2
- export function workflowState(name, tasks, options) {
3
- const executionId = randomUUID();
5
+ export function workflowState(name, tasks, options, checkpoint) {
6
+ const executionId = checkpoint?.initial?.executionId ?? randomUUID();
4
7
  const stop = new AbortController();
5
8
  const signal = options.signal
6
9
  ? AbortSignal.any([options.signal, stop.signal])
7
10
  : stop.signal;
8
11
  const values = new Map();
12
+ const activeAttempts = new Map();
9
13
  const records = new Map(tasks.map((item) => [
10
14
  item,
11
15
  { key: item.key, status: "waiting", attempts: 0 },
12
16
  ]));
17
+ const saved = checkpoint?.initial;
18
+ if (saved) {
19
+ const complete = saved.records.every((entry) => ["done", "skipped"].includes(entry.status));
20
+ const settledPause = saved.records.some((entry) => ["paused", "rejected"].includes(entry.status)) &&
21
+ saved.records.every((entry) => ["done", "skipped", "paused", "rejected"].includes(entry.status) ||
22
+ (entry.status === "waiting" && entry.attempts === 0));
23
+ for (const item of tasks) {
24
+ const entry = saved.records.find((entry) => entry.key === item.key);
25
+ records.set(item, {
26
+ ...entry,
27
+ ...(entry.usageReceipts
28
+ ? { usageReceipts: Object.freeze([...entry.usageReceipts]) }
29
+ : {}),
30
+ ...(entry.pause
31
+ ? {
32
+ pause: Object.freeze({
33
+ ...entry.pause,
34
+ actors: Object.freeze([...entry.pause.actors]),
35
+ }),
36
+ }
37
+ : {}),
38
+ ...(entry.decision
39
+ ? { decision: Object.freeze({ ...entry.decision }) }
40
+ : {}),
41
+ status: ["done", "paused", "rejected"].includes(entry.status) ||
42
+ complete ||
43
+ (settledPause && entry.status === "skipped")
44
+ ? entry.status
45
+ : "waiting",
46
+ });
47
+ if (entry.status === "done") {
48
+ const output = saved.values[item.key];
49
+ values.set(item, output.kind === "json" ? output.value : undefined);
50
+ }
51
+ }
52
+ }
13
53
  const errors = [], observerErrors = [];
54
+ for (const entry of records.values())
55
+ if (entry.status === "rejected")
56
+ errors.push(new Error(entry.error));
57
+ const accounting = workflowAccounting(options.budget, (error) => {
58
+ errors.push(error);
59
+ if (error.dimension !== "attempts")
60
+ stop.abort(error);
61
+ }, saved?.usage);
14
62
  const record = (item) => records.get(item);
15
63
  function emit(event) {
16
64
  try {
@@ -35,13 +83,61 @@ export function workflowState(name, tasks, options) {
35
83
  key: item.key,
36
84
  status,
37
85
  attempt: record(item).attempts,
86
+ ...(record(item).startedAt
87
+ ? { durationMs: Date.now() - Date.parse(record(item).startedAt) }
88
+ : {}),
38
89
  });
39
90
  }
40
91
  function context(item, attempt, taskSignal) {
92
+ if (attempt > 0)
93
+ activeAttempts.set(item, attempt);
94
+ function report(usage, receipt) {
95
+ const current = record(item);
96
+ if (attempt === 0 ||
97
+ current.status !== "active" ||
98
+ activeAttempts.get(item) !== attempt)
99
+ throw new Error("Usage must be reported during its active task attempt");
100
+ if (receipt !== undefined) {
101
+ validateUsageReceipt(receipt);
102
+ if (current.usageReceipts?.includes(receipt))
103
+ return;
104
+ if ((current.usageReceipts?.length ?? 0) >= maxUsageReceiptsPerTask)
105
+ throw new Error("Workflow usage receipt limit exceeded");
106
+ }
107
+ const previousReceipts = current.usageReceipts;
108
+ if (receipt !== undefined)
109
+ current.usageReceipts = Object.freeze([
110
+ ...(previousReceipts ?? []),
111
+ receipt,
112
+ ]);
113
+ try {
114
+ accounting.report(usage);
115
+ }
116
+ catch (error) {
117
+ if (previousReceipts)
118
+ current.usageReceipts = previousReceipts;
119
+ else
120
+ delete current.usageReceipts;
121
+ throw error;
122
+ }
123
+ void runtime.persist().catch((error) => stop.abort(error));
124
+ emit({
125
+ type: "usage",
126
+ key: item.key,
127
+ attempt,
128
+ usage: Object.freeze({ ...usage }),
129
+ });
130
+ }
41
131
  return {
42
132
  signal: taskSignal,
43
133
  attempt,
44
134
  executionId,
135
+ checkpoint: () => runtime.persist(),
136
+ reportUsage: (usage) => report(usage),
137
+ reportUsageOnce: (receipt, usage) => {
138
+ validateUsageReceipt(receipt);
139
+ report(usage, receipt);
140
+ },
45
141
  value(dependency) {
46
142
  if (!item.after.includes(dependency))
47
143
  throw new Error(`${item.key}: undeclared dependency ${dependency.key}`);
@@ -51,8 +147,15 @@ export function workflowState(name, tasks, options) {
51
147
  },
52
148
  };
53
149
  }
54
- return {
150
+ const runtime = {
151
+ async persist() {
152
+ await checkpoint?.save(runtime);
153
+ },
55
154
  executionId,
155
+ accounting,
156
+ closeAttempt(item) {
157
+ activeAttempts.delete(item);
158
+ },
56
159
  stop,
57
160
  signal,
58
161
  values,
@@ -65,5 +168,6 @@ export function workflowState(name, tasks, options) {
65
168
  context,
66
169
  options,
67
170
  };
171
+ return runtime;
68
172
  }
69
173
  //# sourceMappingURL=workflow-state.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"workflow-state.js","sourceRoot":"","sources":["../../../src/domain/workflow/workflow-state.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAWzC,MAAM,UAAU,aAAa,CAC3B,IAAY,EACZ,KAAsB,EACtB,OAAwB;IAExB,MAAM,WAAW,GAAG,UAAU,EAAE,CAAC;IACjC,MAAM,IAAI,GAAG,IAAI,eAAe,EAAE,CAAC;IACnC,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM;QAC3B,CAAC,CAAC,WAAW,CAA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C;IACD,SAAS,OAAO,CACd,IAAU,EACV,OAAe,EACf,UAAuB;QAEvB,OAAO;YACL,MAAM,EAAE,UAAU;YAClB,OAAO;YACP,WAAW;YACX,KAAK,CAAI,UAAmB;gBAC1B,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,UAAU,CAAC;oBAClC,MAAM,IAAI,KAAK,CACb,GAAG,IAAI,CAAC,GAAG,2BAA2B,UAAU,CAAC,GAAG,EAAE,CACvD,CAAC;gBACJ,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,UAAU,CAAC;oBACzB,MAAM,IAAI,KAAK,CAAC,GAAG,UAAU,CAAC,GAAG,0BAA0B,CAAC,CAAC;gBAC/D,OAAO,MAAM,CAAC,GAAG,CAAC,UAAU,CAAM,CAAC;YACrC,CAAC;SACF,CAAC;IACJ,CAAC;IAED,OAAO;QACL,WAAW;QACX,IAAI;QACJ,MAAM;QACN,MAAM;QACN,OAAO;QACP,MAAM;QACN,cAAc;QACd,MAAM;QACN,IAAI;QACJ,MAAM;QACN,OAAO;QACP,OAAO;KACR,CAAC;AACJ,CAAC","sourcesContent":["import { randomUUID } from \"node:crypto\";\nimport type {\n Task,\n TaskContext,\n TaskRecord,\n TaskStatus,\n WorkflowEvent,\n WorkflowExecutionState,\n WorkflowOptions,\n} from \"../workflow.types.ts\";\n\nexport function workflowState(\n name: string,\n tasks: readonly Task[],\n options: WorkflowOptions,\n): WorkflowExecutionState {\n const executionId = randomUUID();\n const stop = new AbortController();\n const signal = options.signal\n ? AbortSignal.any([options.signal, stop.signal])\n : stop.signal;\n const values = new Map<Task, unknown>();\n const records = new Map<Task, TaskRecord>(\n tasks.map((item) => [\n item,\n { key: item.key, status: \"waiting\", attempts: 0 },\n ]),\n );\n const errors: unknown[] = [],\n observerErrors: unknown[] = [];\n const record = (item: Task) => records.get(item)!;\n function emit(\n event: Omit<WorkflowEvent, \"executionId\" | \"workflow\" | \"timestamp\">,\n ): void {\n try {\n options.observe?.({\n ...event,\n executionId,\n workflow: name,\n timestamp: new Date().toISOString(),\n });\n } catch (error) {\n observerErrors.push(error);\n }\n }\n function finish(item: Task, status: TaskStatus): void {\n Object.assign(record(item), {\n status,\n finishedAt: new Date().toISOString(),\n });\n emit({\n type: \"task\",\n key: item.key,\n status,\n attempt: record(item).attempts,\n });\n }\n function context(\n item: Task,\n attempt: number,\n taskSignal: AbortSignal,\n ): TaskContext {\n return {\n signal: taskSignal,\n attempt,\n executionId,\n value<T>(dependency: Task<T>): T {\n if (!item.after.includes(dependency))\n throw new Error(\n `${item.key}: undeclared dependency ${dependency.key}`,\n );\n if (!values.has(dependency))\n throw new Error(`${dependency.key} has no successful value`);\n return values.get(dependency) as T;\n },\n };\n }\n\n return {\n executionId,\n stop,\n signal,\n values,\n records,\n errors,\n observerErrors,\n record,\n emit,\n finish,\n context,\n options,\n };\n}\n"]}
1
+ {"version":3,"file":"workflow-state.js","sourceRoot":"","sources":["../../../src/domain/workflow/workflow-state.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,uBAAuB,EAAE,MAAM,8BAA8B,CAAC;AACvE,OAAO,EAAE,oBAAoB,EAAE,MAAM,oBAAoB,CAAC;AAG1D,OAAO,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AACjD,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAWzC,MAAM,UAAU,aAAa,CAC3B,IAAY,EACZ,KAAsB,EACtB,OAAwB,EACxB,UAAsC;IAEtC,MAAM,WAAW,GAAG,UAAU,EAAE,OAAO,EAAE,WAAW,IAAI,UAAU,EAAE,CAAC;IACrE,MAAM,IAAI,GAAG,IAAI,eAAe,EAAE,CAAC;IACnC,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM;QAC3B,CAAC,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC,OAAO,CAAC,MAAM,EAAE,IAAI,CAAC,MAAM,CAAC,CAAC;QAChD,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC;IAChB,MAA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;gBAC/D,OAAO,MAAM,CAAC,GAAG,CAAC,UAAU,CAAM,CAAC;YACrC,CAAC;SACF,CAAC;IACJ,CAAC;IAED,MAAM,OAAO,GAA2B;QACtC,KAAK,CAAC,OAAO;YACX,MAAM,UAAU,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC;QAClC,CAAC;QACD,WAAW;QACX,UAAU;QACV,YAAY,CAAC,IAAI;YACf,cAAc,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;QAC9B,CAAC;QACD,IAAI;QACJ,MAAM;QACN,MAAM;QACN,OAAO;QACP,MAAM;QACN,cAAc;QACd,MAAM;QACN,IAAI;QACJ,MAAM;QACN,OAAO;QACP,OAAO;KACR,CAAC;IACF,OAAO,OAAO,CAAC;AACjB,CAAC","sourcesContent":["import { maxUsageReceiptsPerTask } from \"./usage-receipt.constants.ts\";\nimport { validateUsageReceipt } from \"./usage-receipt.ts\";\nimport type { Usage } from \"../agent.types.ts\";\nimport type { WorkflowCheckpointSession } from \"./checkpoint.types.ts\";\nimport { workflowAccounting } from \"./budget.ts\";\nimport { randomUUID } from \"node:crypto\";\nimport type {\n Task,\n TaskContext,\n TaskRecord,\n TaskStatus,\n WorkflowEvent,\n WorkflowExecutionState,\n WorkflowOptions,\n} from \"../workflow.types.ts\";\n\nexport function workflowState(\n name: string,\n tasks: readonly Task[],\n options: WorkflowOptions,\n checkpoint?: WorkflowCheckpointSession,\n): WorkflowExecutionState {\n const executionId = checkpoint?.initial?.executionId ?? randomUUID();\n const stop = new AbortController();\n const signal = options.signal\n ? AbortSignal.any([options.signal, stop.signal])\n : stop.signal;\n const values = new Map<Task, unknown>();\n const activeAttempts = new Map<Task, number>();\n const records = new Map<Task, TaskRecord>(\n tasks.map((item) => [\n item,\n { key: item.key, status: \"waiting\", attempts: 0 },\n ]),\n );\n const saved = checkpoint?.initial;\n if (saved) {\n const complete = saved.records.every((entry) =>\n [\"done\", \"skipped\"].includes(entry.status),\n );\n const settledPause =\n saved.records.some((entry) =>\n [\"paused\", \"rejected\"].includes(entry.status),\n ) &&\n saved.records.every(\n (entry) =>\n [\"done\", \"skipped\", \"paused\", \"rejected\"].includes(entry.status) ||\n (entry.status === \"waiting\" && entry.attempts === 0),\n );\n for (const item of tasks) {\n const entry = saved.records.find((entry) => entry.key === item.key)!;\n records.set(item, {\n ...entry,\n ...(entry.usageReceipts\n ? { usageReceipts: Object.freeze([...entry.usageReceipts]) }\n : {}),\n ...(entry.pause\n ? {\n pause: Object.freeze({\n ...entry.pause,\n actors: Object.freeze([...entry.pause.actors]),\n }),\n }\n : {}),\n ...(entry.decision\n ? { decision: Object.freeze({ ...entry.decision }) }\n : {}),\n status:\n [\"done\", \"paused\", \"rejected\"].includes(entry.status) ||\n complete ||\n (settledPause && entry.status === \"skipped\")\n ? entry.status\n : \"waiting\",\n });\n if (entry.status === \"done\") {\n const output = saved.values[item.key]!;\n values.set(item, output.kind === \"json\" ? output.value : undefined);\n }\n }\n }\n const errors: unknown[] = [],\n observerErrors: unknown[] = [];\n for (const entry of records.values())\n if (entry.status === \"rejected\") errors.push(new Error(entry.error));\n const accounting = workflowAccounting(\n options.budget,\n (error) => {\n errors.push(error);\n if (error.dimension !== \"attempts\") stop.abort(error);\n },\n saved?.usage,\n );\n const record = (item: Task) => records.get(item)!;\n function emit(\n event: Omit<WorkflowEvent, \"executionId\" | \"workflow\" | \"timestamp\">,\n ): void {\n try {\n options.observe?.({\n ...event,\n executionId,\n workflow: name,\n timestamp: new Date().toISOString(),\n });\n } catch (error) {\n observerErrors.push(error);\n }\n }\n function finish(item: Task, status: TaskStatus): void {\n Object.assign(record(item), {\n status,\n finishedAt: new Date().toISOString(),\n });\n emit({\n type: \"task\",\n key: item.key,\n status,\n attempt: record(item).attempts,\n ...(record(item).startedAt\n ? { durationMs: Date.now() - Date.parse(record(item).startedAt!) }\n : {}),\n });\n }\n function context(\n item: Task,\n attempt: number,\n taskSignal: AbortSignal,\n ): TaskContext {\n if (attempt > 0) activeAttempts.set(item, attempt);\n function report(usage: Usage, receipt?: string): void {\n const current = record(item);\n if (\n attempt === 0 ||\n current.status !== \"active\" ||\n activeAttempts.get(item) !== attempt\n )\n throw new Error(\n \"Usage must be reported during its active task attempt\",\n );\n if (receipt !== undefined) {\n validateUsageReceipt(receipt);\n if (current.usageReceipts?.includes(receipt)) return;\n if ((current.usageReceipts?.length ?? 0) >= maxUsageReceiptsPerTask)\n throw new Error(\"Workflow usage receipt limit exceeded\");\n }\n const previousReceipts = current.usageReceipts;\n if (receipt !== undefined)\n current.usageReceipts = Object.freeze([\n ...(previousReceipts ?? []),\n receipt,\n ]);\n try {\n accounting.report(usage);\n } catch (error) {\n if (previousReceipts) current.usageReceipts = previousReceipts;\n else delete current.usageReceipts;\n throw error;\n }\n void runtime.persist().catch((error: unknown) => stop.abort(error));\n emit({\n type: \"usage\",\n key: item.key,\n attempt,\n usage: Object.freeze({ ...usage }),\n });\n }\n\n return {\n signal: taskSignal,\n attempt,\n executionId,\n checkpoint: () => runtime.persist(),\n reportUsage: (usage) => report(usage),\n reportUsageOnce: (receipt, usage) => {\n validateUsageReceipt(receipt);\n report(usage, receipt);\n },\n value<T>(dependency: Task<T>): T {\n if (!item.after.includes(dependency))\n throw new Error(\n `${item.key}: undeclared dependency ${dependency.key}`,\n );\n if (!values.has(dependency))\n throw new Error(`${dependency.key} has no successful value`);\n return values.get(dependency) as T;\n },\n };\n }\n\n const runtime: WorkflowExecutionState = {\n async persist() {\n await checkpoint?.save(runtime);\n },\n executionId,\n accounting,\n closeAttempt(item) {\n activeAttempts.delete(item);\n },\n stop,\n signal,\n values,\n records,\n errors,\n observerErrors,\n record,\n emit,\n finish,\n context,\n options,\n };\n return runtime;\n}\n"]}
@@ -3,3 +3,5 @@ export type { Retry, Task, TaskContext, TaskOptions, TaskRecord, TaskStatus, Wor
3
3
  export { WorkflowFailure } from "./workflow/failure.ts";
4
4
  export { task } from "./workflow/task.ts";
5
5
  export declare function workflow(name: string, tasks: readonly Task[]): Workflow;
6
+ export { WorkflowBudgetExceeded } from "./workflow/budget.ts";
7
+ export type { WorkflowBudget, WorkflowUsage } from "./workflow/budget.types.ts";
@@ -23,4 +23,5 @@ export function workflow(name, tasks) {
23
23
  },
24
24
  });
25
25
  }
26
+ export { WorkflowBudgetExceeded } from "./workflow/budget.js";
26
27
  //# sourceMappingURL=workflow.js.map