@temelj/workflow 0.0.0-alpha.1

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 (393) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +38 -0
  3. package/dist/attributes.d.mts +15 -0
  4. package/dist/attributes.mjs +30 -0
  5. package/dist/client/create.d.mts +31 -0
  6. package/dist/client/create.mjs +67 -0
  7. package/dist/client/facades.d.mts +21 -0
  8. package/dist/client/facades.mjs +142 -0
  9. package/dist/client/mod.d.mts +2 -0
  10. package/dist/client/mod.mjs +2 -0
  11. package/dist/client/run-handle.d.mts +66 -0
  12. package/dist/client/run-handle.mjs +156 -0
  13. package/dist/client/worker-options.d.mts +17 -0
  14. package/dist/client/worker-options.mjs +25 -0
  15. package/dist/collection.d.mts +15 -0
  16. package/dist/collection.mjs +33 -0
  17. package/dist/compiled-function.d.mts +27 -0
  18. package/dist/compiled-function.mjs +54 -0
  19. package/dist/definition.d.mts +37 -0
  20. package/dist/definition.mjs +83 -0
  21. package/dist/engine/child-workflow.d.mts +10 -0
  22. package/dist/engine/child-workflow.mjs +333 -0
  23. package/dist/engine/command-identity.d.mts +9 -0
  24. package/dist/engine/command-identity.mjs +14 -0
  25. package/dist/engine/create.d.mts +106 -0
  26. package/dist/engine/create.mjs +402 -0
  27. package/dist/engine/deterministic.d.mts +12 -0
  28. package/dist/engine/deterministic.mjs +102 -0
  29. package/dist/engine/durable-command.d.mts +10 -0
  30. package/dist/engine/durable-command.mjs +17 -0
  31. package/dist/engine/execution-api.d.mts +29 -0
  32. package/dist/engine/execution-api.mjs +33 -0
  33. package/dist/engine/execution-service.d.mts +41 -0
  34. package/dist/engine/execution-service.mjs +288 -0
  35. package/dist/engine/hooks.d.mts +56 -0
  36. package/dist/engine/hooks.mjs +237 -0
  37. package/dist/engine/inspection-api.d.mts +34 -0
  38. package/dist/engine/inspection-api.mjs +77 -0
  39. package/dist/engine/inspection.d.mts +18 -0
  40. package/dist/engine/inspection.mjs +149 -0
  41. package/dist/engine/lifecycle.d.mts +18 -0
  42. package/dist/engine/lifecycle.mjs +38 -0
  43. package/dist/engine/lock-replay.d.mts +30 -0
  44. package/dist/engine/lock-replay.mjs +38 -0
  45. package/dist/engine/lock.d.mts +21 -0
  46. package/dist/engine/lock.mjs +197 -0
  47. package/dist/engine/message.d.mts +47 -0
  48. package/dist/engine/message.mjs +357 -0
  49. package/dist/engine/mod.d.mts +4 -0
  50. package/dist/engine/mod.mjs +2 -0
  51. package/dist/engine/observation.d.mts +35 -0
  52. package/dist/engine/observation.mjs +201 -0
  53. package/dist/engine/recovery.d.mts +26 -0
  54. package/dist/engine/recovery.mjs +92 -0
  55. package/dist/engine/replay.d.mts +41 -0
  56. package/dist/engine/replay.mjs +122 -0
  57. package/dist/engine/retry-policy.d.mts +4 -0
  58. package/dist/engine/retry-policy.mjs +4 -0
  59. package/dist/engine/retry.d.mts +23 -0
  60. package/dist/engine/retry.mjs +75 -0
  61. package/dist/engine/run-data.d.mts +9 -0
  62. package/dist/engine/run-data.mjs +74 -0
  63. package/dist/engine/run-service.d.mts +38 -0
  64. package/dist/engine/run-service.mjs +133 -0
  65. package/dist/engine/run-utils.d.mts +16 -0
  66. package/dist/engine/run-utils.mjs +83 -0
  67. package/dist/engine/schedule.d.mts +25 -0
  68. package/dist/engine/schedule.mjs +207 -0
  69. package/dist/engine/serialization.d.mts +9 -0
  70. package/dist/engine/serialization.mjs +85 -0
  71. package/dist/engine/start-api.d.mts +30 -0
  72. package/dist/engine/start-api.mjs +98 -0
  73. package/dist/engine/state.d.mts +77 -0
  74. package/dist/engine/state.mjs +464 -0
  75. package/dist/engine/step-api.d.mts +9 -0
  76. package/dist/engine/step-api.mjs +25 -0
  77. package/dist/engine/stream.d.mts +55 -0
  78. package/dist/engine/stream.mjs +333 -0
  79. package/dist/engine/summary/active-events.d.mts +34 -0
  80. package/dist/engine/summary/active-events.mjs +69 -0
  81. package/dist/engine/summary/child-workflow-runs.d.mts +24 -0
  82. package/dist/engine/summary/child-workflow-runs.mjs +138 -0
  83. package/dist/engine/summary/child-workflow-waits.d.mts +21 -0
  84. package/dist/engine/summary/child-workflow-waits.mjs +67 -0
  85. package/dist/engine/summary/recovery-child-workflow-waits.d.mts +11 -0
  86. package/dist/engine/summary/recovery-child-workflow-waits.mjs +89 -0
  87. package/dist/engine/summary/recovery-due-runs.d.mts +16 -0
  88. package/dist/engine/summary/recovery-due-runs.mjs +52 -0
  89. package/dist/engine/summary/recovery-failed-runs.d.mts +14 -0
  90. package/dist/engine/summary/recovery-failed-runs.mjs +33 -0
  91. package/dist/engine/summary/recovery-lease-runs.d.mts +16 -0
  92. package/dist/engine/summary/recovery-lease-runs.mjs +69 -0
  93. package/dist/engine/summary/recovery-locks.d.mts +18 -0
  94. package/dist/engine/summary/recovery-locks.mjs +100 -0
  95. package/dist/engine/summary/recovery-message-waits.d.mts +11 -0
  96. package/dist/engine/summary/recovery-message-waits.mjs +74 -0
  97. package/dist/engine/summary/recovery-retry-runs.d.mts +17 -0
  98. package/dist/engine/summary/recovery-retry-runs.mjs +65 -0
  99. package/dist/engine/summary/recovery-run-timestamps.d.mts +13 -0
  100. package/dist/engine/summary/recovery-run-timestamps.mjs +18 -0
  101. package/dist/engine/summary/recovery-runs.d.mts +23 -0
  102. package/dist/engine/summary/recovery-runs.mjs +54 -0
  103. package/dist/engine/summary/recovery-schedules.d.mts +10 -0
  104. package/dist/engine/summary/recovery-schedules.mjs +46 -0
  105. package/dist/engine/summary/recovery-sleep-waits.d.mts +10 -0
  106. package/dist/engine/summary/recovery-sleep-waits.mjs +53 -0
  107. package/dist/engine/summary/recovery-waits.d.mts +21 -0
  108. package/dist/engine/summary/recovery-waits.mjs +29 -0
  109. package/dist/engine/summary/run-availability.d.mts +13 -0
  110. package/dist/engine/summary/run-availability.mjs +37 -0
  111. package/dist/engine/summary/run-cleanup.d.mts +14 -0
  112. package/dist/engine/summary/run-cleanup.mjs +44 -0
  113. package/dist/engine/summary/run-failure.d.mts +14 -0
  114. package/dist/engine/summary/run-failure.mjs +62 -0
  115. package/dist/engine/summary/run-hooks.d.mts +14 -0
  116. package/dist/engine/summary/run-hooks.mjs +63 -0
  117. package/dist/engine/summary/run-lease.d.mts +11 -0
  118. package/dist/engine/summary/run-lease.mjs +64 -0
  119. package/dist/engine/summary/run-message-waits.d.mts +15 -0
  120. package/dist/engine/summary/run-message-waits.mjs +62 -0
  121. package/dist/engine/summary/run-metrics.d.mts +31 -0
  122. package/dist/engine/summary/run-metrics.mjs +93 -0
  123. package/dist/engine/summary/run-retry.d.mts +9 -0
  124. package/dist/engine/summary/run-retry.mjs +69 -0
  125. package/dist/engine/summary/run-sleeps.d.mts +15 -0
  126. package/dist/engine/summary/run-sleeps.mjs +61 -0
  127. package/dist/engine/summary/run-state.d.mts +26 -0
  128. package/dist/engine/summary/run-state.mjs +48 -0
  129. package/dist/engine/summary/run-status.d.mts +16 -0
  130. package/dist/engine/summary/run-status.mjs +35 -0
  131. package/dist/engine/summary/run-streams.d.mts +14 -0
  132. package/dist/engine/summary/run-streams.mjs +46 -0
  133. package/dist/engine/summary/run-transition.d.mts +9 -0
  134. package/dist/engine/summary/run-transition.mjs +39 -0
  135. package/dist/engine/summary/run-workflow.d.mts +9 -0
  136. package/dist/engine/summary/run-workflow.mjs +47 -0
  137. package/dist/engine/summary/step-attempt-active-child-workflows.d.mts +13 -0
  138. package/dist/engine/summary/step-attempt-active-child-workflows.mjs +55 -0
  139. package/dist/engine/summary/step-attempt-active-fields.d.mts +23 -0
  140. package/dist/engine/summary/step-attempt-active-fields.mjs +14 -0
  141. package/dist/engine/summary/step-attempt-active-message-waits.d.mts +12 -0
  142. package/dist/engine/summary/step-attempt-active-message-waits.mjs +47 -0
  143. package/dist/engine/summary/step-attempt-active-sleeps.d.mts +19 -0
  144. package/dist/engine/summary/step-attempt-active-sleeps.mjs +44 -0
  145. package/dist/engine/summary/step-attempt-buckets.d.mts +22 -0
  146. package/dist/engine/summary/step-attempt-buckets.mjs +152 -0
  147. package/dist/engine/summary/step-attempt-failure.d.mts +12 -0
  148. package/dist/engine/summary/step-attempt-failure.mjs +61 -0
  149. package/dist/engine/summary/step-attempt-lifecycle.d.mts +13 -0
  150. package/dist/engine/summary/step-attempt-lifecycle.mjs +19 -0
  151. package/dist/engine/summary/step-attempts.d.mts +11 -0
  152. package/dist/engine/summary/step-attempts.mjs +43 -0
  153. package/dist/engine/summary.d.mts +32 -0
  154. package/dist/engine/summary.mjs +37 -0
  155. package/dist/engine/task.d.mts +6 -0
  156. package/dist/engine/task.mjs +207 -0
  157. package/dist/errors/base.d.mts +99 -0
  158. package/dist/errors/base.mjs +426 -0
  159. package/dist/errors/capability.d.mts +11 -0
  160. package/dist/errors/capability.mjs +23 -0
  161. package/dist/errors/definition.d.mts +21 -0
  162. package/dist/errors/definition.mjs +51 -0
  163. package/dist/errors/details.d.mts +13 -0
  164. package/dist/errors/details.mjs +65 -0
  165. package/dist/errors/failures.d.mts +81 -0
  166. package/dist/errors/failures.mjs +168 -0
  167. package/dist/errors/hook.d.mts +14 -0
  168. package/dist/errors/hook.mjs +30 -0
  169. package/dist/errors/implementation.d.mts +11 -0
  170. package/dist/errors/implementation.mjs +25 -0
  171. package/dist/errors/lock.d.mts +28 -0
  172. package/dist/errors/lock.mjs +70 -0
  173. package/dist/errors/message.d.mts +106 -0
  174. package/dist/errors/message.mjs +122 -0
  175. package/dist/errors/mod.d.mts +17 -0
  176. package/dist/errors/mod.mjs +17 -0
  177. package/dist/errors/record-view.d.mts +11 -0
  178. package/dist/errors/record-view.mjs +24 -0
  179. package/dist/errors/records.d.mts +7 -0
  180. package/dist/errors/records.mjs +280 -0
  181. package/dist/errors/run.d.mts +116 -0
  182. package/dist/errors/run.mjs +232 -0
  183. package/dist/errors/runtime.d.mts +12 -0
  184. package/dist/errors/runtime.mjs +24 -0
  185. package/dist/errors/schedule.d.mts +27 -0
  186. package/dist/errors/schedule.mjs +65 -0
  187. package/dist/errors/step.d.mts +76 -0
  188. package/dist/errors/step.mjs +113 -0
  189. package/dist/errors/stream.d.mts +24 -0
  190. package/dist/errors/stream.mjs +64 -0
  191. package/dist/esbuild.d.mts +7 -0
  192. package/dist/esbuild.mjs +6 -0
  193. package/dist/events/descriptors.d.mts +245 -0
  194. package/dist/events/descriptors.mjs +405 -0
  195. package/dist/events/mod.d.mts +3 -0
  196. package/dist/events/mod.mjs +2 -0
  197. package/dist/history/attempt-key.d.mts +6 -0
  198. package/dist/history/attempt-key.mjs +7 -0
  199. package/dist/history/mod.d.mts +71 -0
  200. package/dist/history/mod.mjs +195 -0
  201. package/dist/history/projection.d.mts +8 -0
  202. package/dist/history/projection.mjs +259 -0
  203. package/dist/history/replay-projection.d.mts +88 -0
  204. package/dist/history/replay-projection.mjs +369 -0
  205. package/dist/history/sequencer.d.mts +16 -0
  206. package/dist/history/sequencer.mjs +78 -0
  207. package/dist/history/step-attempts.d.mts +9 -0
  208. package/dist/history/step-attempts.mjs +50 -0
  209. package/dist/history/timeline.d.mts +16 -0
  210. package/dist/history/timeline.mjs +272 -0
  211. package/dist/history/validator.d.mts +9 -0
  212. package/dist/history/validator.mjs +110 -0
  213. package/dist/inspection/mod.d.mts +5 -0
  214. package/dist/inspection/mod.mjs +3 -0
  215. package/dist/message-options.d.mts +8 -0
  216. package/dist/message-options.mjs +25 -0
  217. package/dist/mod.d.mts +52 -0
  218. package/dist/mod.mjs +26 -0
  219. package/dist/otel/support.d.mts +18 -0
  220. package/dist/otel/support.mjs +102 -0
  221. package/dist/otel.d.mts +30 -0
  222. package/dist/otel.mjs +588 -0
  223. package/dist/pagination.d.mts +25 -0
  224. package/dist/pagination.mjs +73 -0
  225. package/dist/registry.d.mts +20 -0
  226. package/dist/registry.mjs +48 -0
  227. package/dist/rolldown.d.mts +7 -0
  228. package/dist/rolldown.mjs +6 -0
  229. package/dist/rollup.d.mts +7 -0
  230. package/dist/rollup.mjs +6 -0
  231. package/dist/rspack.d.mts +7 -0
  232. package/dist/rspack.mjs +6 -0
  233. package/dist/runtime.d.mts +34 -0
  234. package/dist/runtime.mjs +194 -0
  235. package/dist/step-definition.d.mts +10 -0
  236. package/dist/step-definition.mjs +15 -0
  237. package/dist/store/append-projection.d.mts +15 -0
  238. package/dist/store/append-projection.mjs +26 -0
  239. package/dist/store/capabilities.d.mts +14 -0
  240. package/dist/store/capabilities.mjs +42 -0
  241. package/dist/store/cleanup.d.mts +18 -0
  242. package/dist/store/cleanup.mjs +338 -0
  243. package/dist/store/compaction.d.mts +5 -0
  244. package/dist/store/compaction.mjs +8 -0
  245. package/dist/store/create.d.mts +52 -0
  246. package/dist/store/create.mjs +159 -0
  247. package/dist/store/event-append-project.d.mts +15 -0
  248. package/dist/store/event-append-project.mjs +77 -0
  249. package/dist/store/event-append.d.mts +37 -0
  250. package/dist/store/event-append.mjs +80 -0
  251. package/dist/store/event-history.d.mts +22 -0
  252. package/dist/store/event-history.mjs +75 -0
  253. package/dist/store/event.d.mts +10 -0
  254. package/dist/store/event.mjs +28 -0
  255. package/dist/store/idempotency.d.mts +10 -0
  256. package/dist/store/idempotency.mjs +167 -0
  257. package/dist/store/instrumentation.d.mts +7 -0
  258. package/dist/store/instrumentation.mjs +123 -0
  259. package/dist/store/lock.d.mts +8 -0
  260. package/dist/store/lock.mjs +28 -0
  261. package/dist/store/materialization.d.mts +30 -0
  262. package/dist/store/materialization.mjs +127 -0
  263. package/dist/store/message-index.d.mts +21 -0
  264. package/dist/store/message-index.mjs +121 -0
  265. package/dist/store/mod.d.mts +5 -0
  266. package/dist/store/mod.mjs +4 -0
  267. package/dist/store/query.d.mts +24 -0
  268. package/dist/store/query.mjs +82 -0
  269. package/dist/store/revision.d.mts +4 -0
  270. package/dist/store/revision.mjs +7 -0
  271. package/dist/store/run.d.mts +49 -0
  272. package/dist/store/run.mjs +236 -0
  273. package/dist/store/schedule.d.mts +12 -0
  274. package/dist/store/schedule.mjs +41 -0
  275. package/dist/store/schema.d.mts +14 -0
  276. package/dist/store/schema.mjs +17 -0
  277. package/dist/store/step-attempt.d.mts +14 -0
  278. package/dist/store/step-attempt.mjs +23 -0
  279. package/dist/store/storage-value.d.mts +5 -0
  280. package/dist/store/storage-value.mjs +6 -0
  281. package/dist/store/validation.d.mts +35 -0
  282. package/dist/store/validation.mjs +77 -0
  283. package/dist/store-keys.d.mts +49 -0
  284. package/dist/store-keys.mjs +63 -0
  285. package/dist/telemetry.d.mts +132 -0
  286. package/dist/telemetry.mjs +172 -0
  287. package/dist/temporal.d.mts +23 -0
  288. package/dist/temporal.mjs +59 -0
  289. package/dist/timer.d.mts +12 -0
  290. package/dist/timer.mjs +28 -0
  291. package/dist/types/cleanup.d.mts +112 -0
  292. package/dist/types/cleanup.mjs +22 -0
  293. package/dist/types/client.d.mts +270 -0
  294. package/dist/types/client.mjs +1 -0
  295. package/dist/types/common.d.mts +19 -0
  296. package/dist/types/common.mjs +15 -0
  297. package/dist/types/definition.d.mts +92 -0
  298. package/dist/types/definition.mjs +1 -0
  299. package/dist/types/engine-options.d.mts +76 -0
  300. package/dist/types/engine-options.mjs +1 -0
  301. package/dist/types/engine.d.mts +309 -0
  302. package/dist/types/engine.mjs +1 -0
  303. package/dist/types/error-code.d.mts +256 -0
  304. package/dist/types/error-code.mjs +131 -0
  305. package/dist/types/error.d.mts +236 -0
  306. package/dist/types/error.mjs +48 -0
  307. package/dist/types/events.d.mts +2869 -0
  308. package/dist/types/events.mjs +283 -0
  309. package/dist/types/hook.d.mts +149 -0
  310. package/dist/types/hook.mjs +66 -0
  311. package/dist/types/lock.d.mts +63 -0
  312. package/dist/types/lock.mjs +18 -0
  313. package/dist/types/message-id.d.mts +7 -0
  314. package/dist/types/message-id.mjs +5 -0
  315. package/dist/types/message.d.mts +87 -0
  316. package/dist/types/message.mjs +1 -0
  317. package/dist/types/observation.d.mts +146 -0
  318. package/dist/types/observation.mjs +1 -0
  319. package/dist/types/pagination.d.mts +32 -0
  320. package/dist/types/pagination.mjs +13 -0
  321. package/dist/types/result.d.mts +62 -0
  322. package/dist/types/result.mjs +1 -0
  323. package/dist/types/retry.d.mts +48 -0
  324. package/dist/types/retry.mjs +1 -0
  325. package/dist/types/run-id.d.mts +7 -0
  326. package/dist/types/run-id.mjs +5 -0
  327. package/dist/types/run-list.d.mts +43 -0
  328. package/dist/types/run-list.mjs +1 -0
  329. package/dist/types/run-options.d.mts +86 -0
  330. package/dist/types/run-options.mjs +1 -0
  331. package/dist/types/run.d.mts +355 -0
  332. package/dist/types/run.mjs +207 -0
  333. package/dist/types/runtime.d.mts +143 -0
  334. package/dist/types/runtime.mjs +21 -0
  335. package/dist/types/schedule.d.mts +127 -0
  336. package/dist/types/schedule.mjs +48 -0
  337. package/dist/types/step-attempts.d.mts +326 -0
  338. package/dist/types/step-attempts.mjs +43 -0
  339. package/dist/types/step-id.d.mts +7 -0
  340. package/dist/types/step-id.mjs +5 -0
  341. package/dist/types/step.d.mts +183 -0
  342. package/dist/types/step.mjs +5 -0
  343. package/dist/types/store.d.mts +254 -0
  344. package/dist/types/store.mjs +1 -0
  345. package/dist/types/stream-id.d.mts +7 -0
  346. package/dist/types/stream-id.mjs +5 -0
  347. package/dist/types/stream.d.mts +126 -0
  348. package/dist/types/stream.mjs +1 -0
  349. package/dist/types/summary.d.mts +318 -0
  350. package/dist/types/summary.mjs +1 -0
  351. package/dist/types/telemetry.d.mts +74 -0
  352. package/dist/types/telemetry.mjs +9 -0
  353. package/dist/types/timeline.d.mts +237 -0
  354. package/dist/types/timeline.mjs +1 -0
  355. package/dist/types/vite.d.mts +65 -0
  356. package/dist/types/vite.mjs +1 -0
  357. package/dist/types/worker-id.d.mts +7 -0
  358. package/dist/types/worker-id.mjs +5 -0
  359. package/dist/types/worker.d.mts +94 -0
  360. package/dist/types/worker.mjs +1 -0
  361. package/dist/utility.d.mts +49 -0
  362. package/dist/utility.mjs +122 -0
  363. package/dist/vite/ast.d.mts +23 -0
  364. package/dist/vite/ast.mjs +73 -0
  365. package/dist/vite/error.d.mts +19 -0
  366. package/dist/vite/error.mjs +47 -0
  367. package/dist/vite/generated-name.d.mts +7 -0
  368. package/dist/vite/generated-name.mjs +9 -0
  369. package/dist/vite/manifest.d.mts +7 -0
  370. package/dist/vite/manifest.mjs +106 -0
  371. package/dist/vite/plugin.d.mts +7 -0
  372. package/dist/vite/plugin.mjs +190 -0
  373. package/dist/vite/safety.d.mts +12 -0
  374. package/dist/vite/safety.mjs +96 -0
  375. package/dist/vite/source-edit.d.mts +12 -0
  376. package/dist/vite/source-edit.mjs +44 -0
  377. package/dist/vite/source-map.d.mts +11 -0
  378. package/dist/vite/source-map.mjs +53 -0
  379. package/dist/vite/source.d.mts +7 -0
  380. package/dist/vite/source.mjs +14 -0
  381. package/dist/vite/transform.d.mts +7 -0
  382. package/dist/vite/transform.mjs +405 -0
  383. package/dist/vite.d.mts +6 -0
  384. package/dist/vite.mjs +5 -0
  385. package/dist/webhook.d.mts +16 -0
  386. package/dist/webhook.mjs +17 -0
  387. package/dist/webpack.d.mts +7 -0
  388. package/dist/webpack.mjs +6 -0
  389. package/dist/worker/error.d.mts +11 -0
  390. package/dist/worker/error.mjs +40 -0
  391. package/dist/worker.d.mts +18 -0
  392. package/dist/worker.mjs +427 -0
  393. package/package.json +144 -0
@@ -0,0 +1,237 @@
1
+ import { WorkflowHookError } from "../errors/hook.mjs";
2
+ import { WorkflowHookDisposedError } from "../errors/message.mjs";
3
+ import "../errors/mod.mjs";
4
+ import { captureTelemetryContext } from "../telemetry.mjs";
5
+ import { serializeError, toOptionalPersistedValue, toPersistedValue } from "./serialization.mjs";
6
+ import { WorkflowHistory } from "../history/mod.mjs";
7
+ import { enforceWorkflowEventHistoryNextLimit } from "./retry.mjs";
8
+ import { WORKFLOW_HOOK_TOKEN_PREFIX, parseHookToken, parseWebhookToken } from "../types/hook.mjs";
9
+ import { sendWorkflowMessage, waitForWorkflowMessage } from "./message.mjs";
10
+ import { encodeBase64UrlString } from "@temelj/string";
11
+ //#region src/engine/hooks.ts
12
+ const MAX_HOOK_DISPOSAL_APPEND_ATTEMPTS = 16;
13
+ function createHook(runId, options, wait) {
14
+ return new Hook(runId, options, wait);
15
+ }
16
+ function createWebhook(runId, options, wait) {
17
+ return new Webhook(runId, options, wait);
18
+ }
19
+ function createWorkflowStepHookApi(environment) {
20
+ const { runId } = environment;
21
+ const tokens = /* @__PURE__ */ new Set();
22
+ return {
23
+ create(options) {
24
+ const hook = createHook(runId, options, async () => {
25
+ return await waitForWorkflowMessage(environment, {
26
+ commandId: options.commandId ?? options.name,
27
+ messageId: options.messageId ?? options.name,
28
+ source: "hook",
29
+ ...options.timeout === void 0 ? {} : { timeout: options.timeout },
30
+ ...options.schema === void 0 ? {} : { schema: options.schema }
31
+ });
32
+ });
33
+ requireUniqueWorkflowStepHookToken(tokens, "hook", hook.name, hook.token);
34
+ return hook;
35
+ },
36
+ createWebhook(options) {
37
+ const webhook = createWebhook(runId, options, async () => {
38
+ return await waitForWorkflowMessage(environment, {
39
+ commandId: options.commandId ?? options.name,
40
+ messageId: options.messageId ?? options.name,
41
+ source: "webhook",
42
+ ...options.timeout === void 0 ? {} : { timeout: options.timeout },
43
+ ...options.schema === void 0 ? {} : { schema: options.schema }
44
+ });
45
+ });
46
+ requireUniqueWorkflowStepHookToken(tokens, "webhook", webhook.name, webhook.token);
47
+ return webhook;
48
+ }
49
+ };
50
+ }
51
+ var Hook = class {
52
+ token;
53
+ runId;
54
+ name;
55
+ messageId;
56
+ #wait;
57
+ constructor(runId, options, wait) {
58
+ const messageId = options.messageId ?? options.name;
59
+ this.token = formatHookToken({
60
+ runId,
61
+ name: options.name,
62
+ messageId
63
+ });
64
+ this.runId = runId;
65
+ this.name = options.name;
66
+ this.messageId = messageId;
67
+ this.#wait = wait;
68
+ }
69
+ async wait() {
70
+ return await this.#wait();
71
+ }
72
+ then(onfulfilled, onrejected) {
73
+ return this.wait().then(onfulfilled, onrejected);
74
+ }
75
+ };
76
+ function requireUniqueWorkflowStepHookToken(tokens, kind, name, token) {
77
+ if (tokens.has(token)) WorkflowHookError.tokenNotUnique(kind, name);
78
+ tokens.add(token);
79
+ }
80
+ var Webhook = class {
81
+ token;
82
+ runId;
83
+ name;
84
+ messageId;
85
+ metadata;
86
+ #wait;
87
+ constructor(runId, options, wait) {
88
+ const messageId = options.messageId ?? options.name;
89
+ const metadata = options.metadata === void 0 ? void 0 : toPersistedValue(options.metadata, "workflow webhook metadata", {});
90
+ this.token = formatHookToken({
91
+ runId,
92
+ name: options.name,
93
+ messageId,
94
+ kind: "webhook",
95
+ ...metadata === void 0 ? {} : { metadata }
96
+ });
97
+ this.runId = runId;
98
+ this.name = options.name;
99
+ this.messageId = messageId;
100
+ if (options.metadata !== void 0) this.metadata = options.metadata;
101
+ this.#wait = wait;
102
+ }
103
+ async wait() {
104
+ return await this.#wait();
105
+ }
106
+ then(onfulfilled, onrejected) {
107
+ return this.wait().then(onfulfilled, onrejected);
108
+ }
109
+ };
110
+ async function resumeHook(token, options, service) {
111
+ const hook = parseHookToken(token);
112
+ const payload = toOptionalPersistedValue(options?.payload, "hook payload", service.limits);
113
+ const telemetryContext = captureTelemetryContext(service.telemetry);
114
+ await sendWorkflowMessage(service.store, hook.runId, {
115
+ messageId: hook.messageId,
116
+ payload,
117
+ idempotencyKey: options?.idempotencyKey
118
+ }, service.now, service.limits, service.emitMessageSent, service.telemetry, telemetryContext);
119
+ service.emitHookResumed(hook.runId, hook.messageId, payload, telemetryContext);
120
+ }
121
+ async function resumeWebhook(token, options, service) {
122
+ const webhook = parseWebhookToken(token);
123
+ const payload = toOptionalPersistedValue(options?.payload, "webhook payload", service.limits);
124
+ const telemetryContext = captureTelemetryContext(service.telemetry);
125
+ await sendWorkflowMessage(service.store, webhook.runId, {
126
+ messageId: webhook.messageId,
127
+ payload,
128
+ idempotencyKey: options?.idempotencyKey
129
+ }, service.now, service.limits, service.emitMessageSent, service.telemetry, telemetryContext);
130
+ service.emitWebhookResumed(webhook.runId, webhook.messageId, payload, telemetryContext);
131
+ }
132
+ async function disposeHook(token, service) {
133
+ return await disposeHookToken(token, parseHookToken(token), service);
134
+ }
135
+ async function disposeWebhook(token, service) {
136
+ return await disposeHookToken(token, parseWebhookToken(token), service);
137
+ }
138
+ async function getHookByToken(token, store, now) {
139
+ const hook = parseHookToken(token);
140
+ return inspectHookToken(token, await store.getRun(hook.runId), await store.getEvents(hook.runId), now);
141
+ }
142
+ async function disposeHookToken(token, hook, service) {
143
+ let timestamp;
144
+ for (let attempt = 0; attempt < MAX_HOOK_DISPOSAL_APPEND_ATTEMPTS; attempt++) {
145
+ const run = await service.store.getRun(hook.runId);
146
+ const events = run === void 0 ? [] : await service.store.getEvents(hook.runId);
147
+ const inspection = inspectHookToken(token, run, events, service.now());
148
+ if (inspection.status !== "waiting") return inspection;
149
+ if (run === void 0) return inspection;
150
+ if (inspection.stepId === void 0) WorkflowHookError.waitingStepMissing();
151
+ timestamp ??= service.now();
152
+ enforceWorkflowEventHistoryNextLimit(events, service.limits);
153
+ const appendedEvents = await service.store.appendEventIfRunCurrent(run, {
154
+ kind: "message_wait_failed",
155
+ timestamp,
156
+ stepId: inspection.stepId,
157
+ stepName: hook.name,
158
+ messageId: hook.messageId,
159
+ error: serializeError(WorkflowHookDisposedError.create({
160
+ runId: hook.runId,
161
+ stepId: inspection.stepId,
162
+ stepName: hook.name,
163
+ messageId: hook.messageId
164
+ }))
165
+ });
166
+ if (appendedEvents === void 0) continue;
167
+ if (run.status === "waiting") await service.store.updateRunIfCurrent(run, {
168
+ ...run,
169
+ availableAt: run.availableAt !== void 0 && Temporal.Instant.compare(run.availableAt, timestamp) <= 0 ? run.availableAt : timestamp,
170
+ updatedAt: timestamp,
171
+ lastTransitionAt: timestamp,
172
+ lastTransitionReason: "waiting",
173
+ deadlineAt: run.deadlineAt,
174
+ retryAt: run.retryAt,
175
+ error: void 0,
176
+ output: void 0,
177
+ finishedAt: void 0
178
+ });
179
+ return inspectHookToken(token, run, appendedEvents, service.now());
180
+ }
181
+ const run = await service.store.getRun(hook.runId);
182
+ return inspectHookToken(token, run, run === void 0 ? [] : await service.store.getEvents(hook.runId), service.now());
183
+ }
184
+ function inspectHookToken(token, run, events, now) {
185
+ const hook = parseHookToken(token);
186
+ if (run === void 0) return hookInspection(token, hook, "not-found");
187
+ const history = new WorkflowHistory(events);
188
+ const started = history.latestMessageWaitStartedByNameAndMessageId(hook.name, hook.messageId);
189
+ if (started === void 0) return hookInspection(token, hook, "not-found");
190
+ const terminal = history.latestMessageWaitTerminalAfterStarted(started);
191
+ if (terminal?.kind === "message_wait_completed") return hookInspection(token, hook, "completed", {
192
+ stepId: started.stepId,
193
+ timeoutAt: started.timeoutAt,
194
+ payload: terminal.payload
195
+ });
196
+ if (terminal?.kind === "message_wait_failed") return hookInspection(token, hook, WorkflowHookDisposedError.isDisposed(terminal.error) ? "disposed" : "failed", {
197
+ stepId: started.stepId,
198
+ timeoutAt: started.timeoutAt,
199
+ error: terminal.error
200
+ });
201
+ if (run.status === "completed" || run.status === "failed" || run.status === "canceled") return hookInspection(token, hook, "run-terminal", {
202
+ stepId: started.stepId,
203
+ timeoutAt: started.timeoutAt
204
+ });
205
+ if (started.timeoutAt !== void 0 && Temporal.Instant.compare(started.timeoutAt, now) <= 0) return hookInspection(token, hook, "expired", {
206
+ stepId: started.stepId,
207
+ timeoutAt: started.timeoutAt
208
+ });
209
+ return hookInspection(token, hook, "waiting", {
210
+ stepId: started.stepId,
211
+ timeoutAt: started.timeoutAt
212
+ });
213
+ }
214
+ function hookInspection(token, hook, status, state = {}) {
215
+ return {
216
+ token,
217
+ runId: hook.runId,
218
+ name: hook.name,
219
+ messageId: hook.messageId,
220
+ kind: hook.kind,
221
+ status,
222
+ ...hook.metadata === void 0 ? {} : { metadata: hook.metadata },
223
+ ...state.stepId === void 0 ? {} : { stepId: state.stepId },
224
+ ...state.timeoutAt === void 0 ? {} : { timeoutAt: state.timeoutAt },
225
+ ...state.payload === void 0 ? {} : { payload: state.payload },
226
+ ...state.error === void 0 ? {} : { error: state.error }
227
+ };
228
+ }
229
+ function formatHookToken(hook) {
230
+ const payload = {
231
+ v: 1,
232
+ ...hook
233
+ };
234
+ return `${WORKFLOW_HOOK_TOKEN_PREFIX}v1:${encodeBase64UrlString(JSON.stringify(payload))}`;
235
+ }
236
+ //#endregion
237
+ export { Hook, Webhook, createHook, createWebhook, createWorkflowStepHookApi, disposeHook, disposeWebhook, getHookByToken, inspectHookToken, resumeHook, resumeWebhook };
@@ -0,0 +1,34 @@
1
+ import { RunId } from "../types/run-id.mjs";
2
+ import { PageOptions, WorkflowPage } from "../types/pagination.mjs";
3
+ import { WorkflowListRunsOptions, WorkflowListRunsPageOptions, WorkflowListStepAttemptsPageOptions } from "../types/run-list.mjs";
4
+ import { WorkflowRecoverySummary, WorkflowRecoverySummaryOptions, WorkflowRunSummary, WorkflowRunSummaryOptions } from "../types/summary.mjs";
5
+ import { WorkflowListStepAttemptsOptions, WorkflowStepAttemptRecord, WorkflowStepAttemptSummary } from "../types/step-attempts.mjs";
6
+ import { Timeline } from "../types/timeline.mjs";
7
+ import { WorkflowRunRecord } from "../types/run.mjs";
8
+ import { EventRecord } from "../types/events.mjs";
9
+ import { WorkflowEventReader, WorkflowLockListerRepository, WorkflowMessageIdempotencyIndex, WorkflowRunQueryRepository, WorkflowScheduleListerRepository, WorkflowStepAttemptReadModel } from "../types/store.mjs";
10
+ //#region src/engine/inspection-api.d.ts
11
+ interface WorkflowEngineInspectionApi {
12
+ getRun(runId: RunId): Promise<WorkflowRunRecord | undefined>;
13
+ getEvents(runId: RunId): Promise<readonly EventRecord[]>;
14
+ listEventsPage(runId: RunId, options?: PageOptions): Promise<WorkflowPage<EventRecord>>;
15
+ listRuns(options?: WorkflowListRunsOptions): Promise<readonly WorkflowRunRecord[]>;
16
+ listRunsPage(options?: WorkflowListRunsPageOptions): Promise<WorkflowPage<WorkflowRunRecord>>;
17
+ countRuns(options?: WorkflowListRunsOptions): Promise<number>;
18
+ getRunSummary(options?: WorkflowRunSummaryOptions): Promise<WorkflowRunSummary>;
19
+ getRecoverySummary(options?: WorkflowRecoverySummaryOptions): Promise<WorkflowRecoverySummary>;
20
+ getTimeline(runId: RunId): Promise<Timeline>;
21
+ listStepAttempts(runId: RunId, options?: WorkflowListStepAttemptsOptions): Promise<readonly WorkflowStepAttemptRecord[]>;
22
+ listStepAttemptsPage(runId: RunId, options?: WorkflowListStepAttemptsPageOptions): Promise<WorkflowPage<WorkflowStepAttemptRecord>>;
23
+ countStepAttempts(runId: RunId, options?: WorkflowListStepAttemptsOptions): Promise<number>;
24
+ repairStepAttempts(runId: RunId): Promise<readonly WorkflowStepAttemptRecord[]>;
25
+ repairMessageIdempotencyIndexes(runId: RunId): Promise<readonly WorkflowMessageIdempotencyIndex[]>;
26
+ getStepAttemptSummary(runId: RunId, options?: WorkflowListStepAttemptsOptions): Promise<WorkflowStepAttemptSummary>;
27
+ }
28
+ interface WorkflowInspectionRepairStore {
29
+ repairMessageIdempotencyIndexes(runId: RunId): Promise<readonly WorkflowMessageIdempotencyIndex[]>;
30
+ }
31
+ type WorkflowEngineInspectionStore = WorkflowRunQueryRepository & WorkflowEventReader & WorkflowInspectionRepairStore & WorkflowStepAttemptReadModel & WorkflowLockListerRepository & WorkflowScheduleListerRepository;
32
+ declare function createWorkflowEngineInspectionApi(store: WorkflowEngineInspectionStore, now: () => Temporal.Instant): WorkflowEngineInspectionApi;
33
+ //#endregion
34
+ export { WorkflowEngineInspectionApi, createWorkflowEngineInspectionApi };
@@ -0,0 +1,77 @@
1
+ import { getRequiredRun } from "../utility.mjs";
2
+ import { createTimeline } from "../history/timeline.mjs";
3
+ import "../history/mod.mjs";
4
+ import "./state.mjs";
5
+ import { makeWorkflowEventCursorKey, pageItems } from "../pagination.mjs";
6
+ import { getChildRunsForAttempts } from "./summary/child-workflow-runs.mjs";
7
+ import { getWorkflowRecoverySummary, getWorkflowRunSummary } from "./inspection.mjs";
8
+ import { createWorkflowStepAttemptSummaryReducer } from "./summary/step-attempts.mjs";
9
+ //#region src/engine/inspection-api.ts
10
+ function createWorkflowEngineInspectionApi(store, now) {
11
+ return {
12
+ async getRun(runId) {
13
+ return await store.getRun(runId);
14
+ },
15
+ async getEvents(runId) {
16
+ return await store.getEvents(runId);
17
+ },
18
+ async listEventsPage(runId, options) {
19
+ return pageItems(await store.getEvents(runId), options, "Workflow event page", makeWorkflowEventCursorKey);
20
+ },
21
+ async listRuns(options) {
22
+ return await store.listRuns(options);
23
+ },
24
+ async listRunsPage(options) {
25
+ return await store.listRunsPage(options);
26
+ },
27
+ async countRuns(options) {
28
+ return await store.countRuns(options);
29
+ },
30
+ async getRunSummary(options) {
31
+ return await getWorkflowRunSummary({
32
+ store,
33
+ now: now(),
34
+ filters: options
35
+ });
36
+ },
37
+ async getRecoverySummary(options) {
38
+ return await getWorkflowRecoverySummary({
39
+ store,
40
+ now: now(),
41
+ filters: options
42
+ });
43
+ },
44
+ async getTimeline(runId) {
45
+ return createTimeline(await getRequiredRun(store, runId), await store.getEvents(runId));
46
+ },
47
+ async listStepAttempts(runId, options) {
48
+ await getRequiredRun(store, runId);
49
+ return await store.listStepAttempts(runId, options);
50
+ },
51
+ async listStepAttemptsPage(runId, options) {
52
+ await getRequiredRun(store, runId);
53
+ return await store.listStepAttemptsPage(runId, options);
54
+ },
55
+ async countStepAttempts(runId, options) {
56
+ await getRequiredRun(store, runId);
57
+ return await store.countStepAttempts(runId, options);
58
+ },
59
+ async repairStepAttempts(runId) {
60
+ await getRequiredRun(store, runId);
61
+ return await store.repairStepAttempts(runId);
62
+ },
63
+ async repairMessageIdempotencyIndexes(runId) {
64
+ await getRequiredRun(store, runId);
65
+ return await store.repairMessageIdempotencyIndexes(runId);
66
+ },
67
+ async getStepAttemptSummary(runId, options) {
68
+ await getRequiredRun(store, runId);
69
+ const attempts = await store.listStepAttempts(runId, options);
70
+ const reducer = createWorkflowStepAttemptSummaryReducer(await getChildRunsForAttempts(store, runId, attempts));
71
+ for (const attempt of attempts) reducer.addAttempt(attempt);
72
+ return reducer.finish();
73
+ }
74
+ };
75
+ }
76
+ //#endregion
77
+ export { createWorkflowEngineInspectionApi };
@@ -0,0 +1,18 @@
1
+ import { WorkflowRecoverySummary, WorkflowRecoverySummaryOptions, WorkflowRunSummary, WorkflowRunSummaryOptions } from "../types/summary.mjs";
2
+ import { WorkflowEventReader, WorkflowLockListerRepository, WorkflowRunListerRepository, WorkflowRunReaderRepository, WorkflowScheduleListerRepository, WorkflowStepAttemptLookup } from "../types/store.mjs";
3
+ //#region src/engine/inspection.d.ts
4
+ type WorkflowSummaryRunStore = WorkflowRunReaderRepository & WorkflowRunListerRepository;
5
+ type WorkflowRunSummaryStore = WorkflowSummaryRunStore & WorkflowEventReader & WorkflowStepAttemptLookup;
6
+ type WorkflowRecoverySummaryStore = WorkflowRunSummaryStore & WorkflowLockListerRepository & WorkflowScheduleListerRepository;
7
+ declare function getWorkflowRunSummary(options: {
8
+ readonly store: WorkflowRunSummaryStore;
9
+ readonly now: Temporal.Instant;
10
+ readonly filters?: WorkflowRunSummaryOptions;
11
+ }): Promise<WorkflowRunSummary>;
12
+ declare function getWorkflowRecoverySummary(options: {
13
+ readonly store: WorkflowRecoverySummaryStore;
14
+ readonly now: Temporal.Instant;
15
+ readonly filters?: WorkflowRecoverySummaryOptions;
16
+ }): Promise<WorkflowRecoverySummary>;
17
+ //#endregion
18
+ export { getWorkflowRecoverySummary, getWorkflowRunSummary };
@@ -0,0 +1,149 @@
1
+ import { WorkflowOptionsError } from "../errors/base.mjs";
2
+ import { isActiveWorkflowRunStatus, isWorkflowRunStatus } from "../utility.mjs";
3
+ import "../errors/mod.mjs";
4
+ import { summarizeWorkflowRecovery } from "./recovery.mjs";
5
+ import { getChildRunsForAttempts } from "./summary/child-workflow-runs.mjs";
6
+ import { summarizeWorkflowRuns } from "./summary.mjs";
7
+ //#region src/engine/inspection.ts
8
+ function isKnownWorkflowRunStatus(run) {
9
+ return isWorkflowRunStatus(run.status);
10
+ }
11
+ function workflowRunCanHaveActiveWork(run) {
12
+ return isActiveWorkflowRunStatus(run.status);
13
+ }
14
+ function workflowSummaryCleanupCutoff(filters) {
15
+ if (filters?.cleanupFinishedAtBefore === void 0) return;
16
+ const value = filters.cleanupFinishedAtBefore;
17
+ if (!(value instanceof Temporal.Instant)) WorkflowOptionsError.temporalInstant("Workflow cleanup finishedAtBefore");
18
+ return value;
19
+ }
20
+ async function listWorkflowRunStepAttempts(store, runId, options) {
21
+ return await store.listStepAttempts(runId, options);
22
+ }
23
+ async function listActiveWorkflowRunStepAttempts(store, run, kind) {
24
+ return workflowRunCanHaveActiveWork(run) ? await listWorkflowRunStepAttempts(store, run.id, {
25
+ status: "running",
26
+ kind
27
+ }) : [];
28
+ }
29
+ async function getWorkflowRunSummaryActivityInputs(store, runs) {
30
+ const [runningChildWorkflows, runningSleeps, runningMessageWaits, runningEvents] = await Promise.all([
31
+ listRunningChildWorkflowWaits(store, runs),
32
+ listRunningStepWaits(store, runs, "sleep"),
33
+ listRunningStepWaits(store, runs, "message-wait"),
34
+ listRunningEventActivity(store, runs)
35
+ ]);
36
+ return {
37
+ runningChildWorkflows,
38
+ runningSleeps,
39
+ runningMessageWaits,
40
+ runningEvents
41
+ };
42
+ }
43
+ async function listRunningChildWorkflowWaits(store, runs) {
44
+ return await Promise.all(runs.map(async (run) => {
45
+ if (!isKnownWorkflowRunStatus(run)) return {
46
+ runId: run.id,
47
+ attempts: [],
48
+ childRuns: []
49
+ };
50
+ const attempts = await listWorkflowRunStepAttempts(store, run.id, {
51
+ status: "running",
52
+ kind: "workflow"
53
+ });
54
+ return {
55
+ runId: run.id,
56
+ attempts,
57
+ childRuns: await getChildRunsForAttempts(store, run.id, attempts)
58
+ };
59
+ }));
60
+ }
61
+ async function listRunningStepWaits(store, runs, kind) {
62
+ return await Promise.all(runs.map(async (run) => {
63
+ return {
64
+ runId: run.id,
65
+ attempts: await listActiveWorkflowRunStepAttempts(store, run, kind)
66
+ };
67
+ }));
68
+ }
69
+ async function listRunningEventActivity(store, runs) {
70
+ return await Promise.all(runs.map(async (run) => {
71
+ return {
72
+ runId: run.id,
73
+ events: workflowRunCanHaveActiveWork(run) ? await store.getEvents(run.id) : []
74
+ };
75
+ }));
76
+ }
77
+ async function listWorkflowRunAttemptGroups(store, runs, options) {
78
+ return await Promise.all(runs.map(async (run) => await listWorkflowRunStepAttempts(store, run.id, options)));
79
+ }
80
+ async function getWorkflowRunSummary(options) {
81
+ const { store, now, filters } = options;
82
+ const cleanupCutoff = workflowSummaryCleanupCutoff(filters);
83
+ const runs = await store.listRuns(filters);
84
+ return summarizeWorkflowRuns({
85
+ runs,
86
+ now,
87
+ cleanupCutoff,
88
+ ...await getWorkflowRunSummaryActivityInputs(store, runs)
89
+ });
90
+ }
91
+ async function getWorkflowRecoverySummary(options) {
92
+ const { store, now, filters } = options;
93
+ const runs = await store.listRuns(filters);
94
+ return summarizeWorkflowRecovery({
95
+ runs,
96
+ now,
97
+ inputs: await getWorkflowRecoverySummaryInputs(store, runs.filter((run) => {
98
+ return workflowRunCanHaveActiveWork(run);
99
+ }))
100
+ });
101
+ }
102
+ async function getWorkflowRecoverySummaryInputs(store, activeRuns) {
103
+ const [sleepAttemptGroups, messageWaitAttemptGroups, childWorkflowAttemptGroups, lockWaitAttemptGroups, eventsByRunId, locks, schedules] = await Promise.all([
104
+ listWorkflowRunAttemptGroups(store, activeRuns, {
105
+ status: "running",
106
+ kind: "sleep"
107
+ }),
108
+ listWorkflowRunAttemptGroups(store, activeRuns, {
109
+ status: "running",
110
+ kind: "message-wait"
111
+ }),
112
+ listChildWorkflowAttemptGroups(store, activeRuns),
113
+ listWorkflowRunAttemptGroups(store, activeRuns, {
114
+ status: "failed",
115
+ kind: "run"
116
+ }),
117
+ getWorkflowRecoveryEventsByRunId(store, activeRuns),
118
+ store.listLocks(),
119
+ store.listSchedules()
120
+ ]);
121
+ return {
122
+ sleepAttempts: sleepAttemptGroups.flat(),
123
+ messageWaitAttempts: messageWaitAttemptGroups.flat(),
124
+ childWorkflowAttempts: childWorkflowAttemptGroups.flatMap((group) => group.attempts),
125
+ lockWaitAttempts: lockWaitAttemptGroups.flat(),
126
+ childRuns: childWorkflowAttemptGroups.flatMap((group) => group.childRuns),
127
+ eventsByRunId,
128
+ locks,
129
+ schedules
130
+ };
131
+ }
132
+ async function listChildWorkflowAttemptGroups(store, activeRuns) {
133
+ return await Promise.all(activeRuns.map(async (run) => {
134
+ const attempts = await listWorkflowRunStepAttempts(store, run.id, {
135
+ status: "running",
136
+ kind: "workflow"
137
+ });
138
+ return {
139
+ runId: run.id,
140
+ attempts,
141
+ childRuns: await getChildRunsForAttempts(store, run.id, attempts)
142
+ };
143
+ }));
144
+ }
145
+ async function getWorkflowRecoveryEventsByRunId(store, activeRuns) {
146
+ return new Map(await Promise.all(activeRuns.map(async (run) => [run.id, await store.getEvents(run.id)])));
147
+ }
148
+ //#endregion
149
+ export { getWorkflowRecoverySummary, getWorkflowRunSummary };
@@ -0,0 +1,18 @@
1
+ import { ObservationEmitter } from "../types/observation.mjs";
2
+ import { ExecutionResult } from "../types/result.mjs";
3
+ import { WorkflowRunRecord } from "../types/run.mjs";
4
+ import { CreateWorkflowEngineOptions } from "../types/engine-options.mjs";
5
+ //#region src/engine/lifecycle.d.ts
6
+ declare function createWorkflowLifecycleEmitter(options: CreateWorkflowEngineOptions, observation?: ObservationEmitter): WorkflowLifecycleEmitterContract;
7
+ interface WorkflowLifecycleEmitterContract {
8
+ readonly emitLifecycleEvent: (run: WorkflowRunRecord, previousRun?: WorkflowRunRecord) => Promise<void>;
9
+ readonly emitResultLifecycleEvent: <TOutput>(result: ExecutionResult<TOutput>, previousRun?: WorkflowRunRecord) => Promise<ExecutionResult<TOutput>>;
10
+ }
11
+ declare class WorkflowLifecycleEmitter implements WorkflowLifecycleEmitterContract {
12
+ #private;
13
+ constructor(options: CreateWorkflowEngineOptions, observation?: ObservationEmitter);
14
+ emitLifecycleEvent: (run: WorkflowRunRecord, previousRun?: WorkflowRunRecord) => Promise<void>;
15
+ emitResultLifecycleEvent: <TOutput>(result: ExecutionResult<TOutput>, previousRun?: WorkflowRunRecord) => Promise<ExecutionResult<TOutput>>;
16
+ }
17
+ //#endregion
18
+ export { WorkflowLifecycleEmitter, createWorkflowLifecycleEmitter };
@@ -0,0 +1,38 @@
1
+ //#region src/engine/lifecycle.ts
2
+ function createWorkflowLifecycleEmitter(options, observation) {
3
+ return new WorkflowLifecycleEmitter(options, observation);
4
+ }
5
+ var WorkflowLifecycleEmitter = class {
6
+ #options;
7
+ #observation;
8
+ constructor(options, observation) {
9
+ this.#options = options;
10
+ this.#observation = observation;
11
+ }
12
+ emitLifecycleEvent = async (run, previousRun) => {
13
+ const event = {
14
+ run,
15
+ previousRun,
16
+ status: run.status,
17
+ reason: run.lastTransitionReason,
18
+ timestamp: run.lastTransitionAt,
19
+ workflowName: run.workflowName,
20
+ ...run.workflowVersion === void 0 ? {} : { workflowVersion: run.workflowVersion }
21
+ };
22
+ this.#observation?.emitRunEvent(run, previousRun);
23
+ const callbacks = [];
24
+ if (this.#options.onLifecycleEvent !== void 0) callbacks.push(this.#options.onLifecycleEvent);
25
+ if (run.status === "completed" && this.#options.onRunCompleted !== void 0) callbacks.push(this.#options.onRunCompleted);
26
+ else if (run.status === "failed" && this.#options.onRunFailed !== void 0) callbacks.push(this.#options.onRunFailed);
27
+ else if (run.status === "canceled" && this.#options.onRunCanceled !== void 0) callbacks.push(this.#options.onRunCanceled);
28
+ else if (run.status === "waiting" && run.lastTransitionReason === "retry" && this.#options.onRunRetry !== void 0) callbacks.push(this.#options.onRunRetry);
29
+ else if (run.status === "waiting" && this.#options.onRunWaiting !== void 0) callbacks.push(this.#options.onRunWaiting);
30
+ for (const callback of callbacks) await callback(event);
31
+ };
32
+ emitResultLifecycleEvent = async (result, previousRun) => {
33
+ if (result.run.lastTransitionReason === "completed" || result.run.lastTransitionReason === "failed" || result.run.lastTransitionReason === "deadline" || result.run.lastTransitionReason === "waiting" || result.run.lastTransitionReason === "retry" || result.run.lastTransitionReason === "canceled") await this.emitLifecycleEvent(result.run, previousRun);
34
+ return result;
35
+ };
36
+ };
37
+ //#endregion
38
+ export { WorkflowLifecycleEmitter, createWorkflowLifecycleEmitter };
@@ -0,0 +1,30 @@
1
+ import { RunId } from "../types/run-id.mjs";
2
+ import { StepId } from "../types/step-id.mjs";
3
+ import { LockRecord } from "../types/lock.mjs";
4
+ import "../types/step.mjs";
5
+ import "../types/run.mjs";
6
+ import { WorkflowErrorRecord } from "../types/error.mjs";
7
+ //#region src/engine/lock-replay.d.ts
8
+ declare function requireLockWaitDeadlineReplayMatches(recorded: {
9
+ readonly stepId: StepId;
10
+ readonly timestamp: Temporal.Instant;
11
+ readonly value: unknown;
12
+ }, timeout: number): void;
13
+ declare function requireLockReplayMatches(recorded: LockRecord, expected: {
14
+ readonly key: string;
15
+ readonly holderId: string;
16
+ readonly holderRunId: RunId;
17
+ readonly leaseDuration: number;
18
+ }): void;
19
+ declare function requireLockReleaseReplayMatches(recorded: LockRecord, expected: {
20
+ readonly key: string;
21
+ readonly holderId: string;
22
+ }): void;
23
+ declare function requireLockWaitReplayMatches(failedSteps: readonly {
24
+ readonly error: WorkflowErrorRecord;
25
+ }[], expected: {
26
+ readonly key: string;
27
+ readonly wait: boolean;
28
+ }): void;
29
+ //#endregion
30
+ export { requireLockReleaseReplayMatches, requireLockReplayMatches, requireLockWaitDeadlineReplayMatches, requireLockWaitReplayMatches };
@@ -0,0 +1,38 @@
1
+ import { WorkflowReplayDivergenceError, WorkflowRetryableError } from "../errors/failures.mjs";
2
+ import "../errors/mod.mjs";
3
+ //#region src/engine/lock-replay.ts
4
+ function requireLockWaitDeadlineReplayMatches(recorded, timeout) {
5
+ if (!(recorded.value instanceof Temporal.Instant)) WorkflowReplayDivergenceError.diverged(`recorded lock wait deadline ${recorded.stepId} is invalid`, {
6
+ expectedStepId: recorded.stepId,
7
+ actualStepId: recorded.stepId
8
+ });
9
+ if (recorded.value.since(recorded.timestamp).total({ unit: "millisecond" }) === timeout) return;
10
+ WorkflowReplayDivergenceError.diverged(`recorded lock wait deadline ${recorded.stepId} does not match replayed waitTimeout`, {
11
+ expectedStepId: recorded.stepId,
12
+ actualStepId: recorded.stepId
13
+ });
14
+ }
15
+ function requireLockReplayMatches(recorded, expected) {
16
+ const recordedLeaseDuration = recorded.leaseExpiresAt.since(recorded.acquiredAt).total({ unit: "millisecond" });
17
+ if (recorded.key === expected.key && recorded.holderId === expected.holderId && recorded.holderRunId === expected.holderRunId && recordedLeaseDuration === expected.leaseDuration) return;
18
+ WorkflowReplayDivergenceError.diverged(`recorded lock step ${recorded.holderStepId ?? recorded.key} does not match replayed lock acquisition`, {
19
+ expectedStepId: recorded.holderStepId,
20
+ actualStepId: recorded.holderStepId
21
+ });
22
+ }
23
+ function requireLockReleaseReplayMatches(recorded, expected) {
24
+ if (recorded.key === expected.key && recorded.holderId === expected.holderId && recorded.releasedAt !== void 0) return;
25
+ WorkflowReplayDivergenceError.diverged(`recorded lock step ${recorded.holderStepId ?? recorded.key} does not match replayed lock release`, {
26
+ expectedStepId: recorded.holderStepId,
27
+ actualStepId: recorded.holderStepId
28
+ });
29
+ }
30
+ function requireLockWaitReplayMatches(failedSteps, expected) {
31
+ if (!failedSteps.some((failed) => WorkflowRetryableError.isLockAlreadyHeld(failed.error, expected.key)) || expected.wait) return;
32
+ WorkflowReplayDivergenceError.diverged(`recorded lock ${expected.key} waited but replayed lock acquisition does not allow waiting`, {
33
+ expectedStepId: `run:${expected.key}`,
34
+ actualStepId: `run:${expected.key}`
35
+ });
36
+ }
37
+ //#endregion
38
+ export { requireLockReleaseReplayMatches, requireLockReplayMatches, requireLockWaitDeadlineReplayMatches, requireLockWaitReplayMatches };
@@ -0,0 +1,21 @@
1
+ import { AcquireLockOptions, LockRecord, ReleaseLockOptions } from "../types/lock.mjs";
2
+ import { WorkflowStepContext, WorkflowStepLockApi, WorkflowStepRunConfig } from "../types/step.mjs";
3
+ import { WorkflowLockConditionalWriterRepository, WorkflowLockReaderRepository, WorkflowRunListerRepository, WorkflowRunUnconditionalWriterRepository, WorkflowStepAttemptLookup, WorkflowStoreNamespace } from "../types/store.mjs";
4
+ import { WorkflowExecutionEnvironment } from "../types/engine.mjs";
5
+ //#region src/engine/lock.d.ts
6
+ type WorkflowLockCommandStore = WorkflowStoreNamespace & WorkflowLockReaderRepository & WorkflowLockConditionalWriterRepository;
7
+ type WorkflowLockRunWakeStore = WorkflowRunListerRepository & WorkflowRunUnconditionalWriterRepository;
8
+ type WorkflowLockStepAttemptWakeStore = WorkflowStepAttemptLookup;
9
+ type WorkflowLockWakeStore = WorkflowLockRunWakeStore & WorkflowLockStepAttemptWakeStore;
10
+ type WorkflowLockReleaseStore = WorkflowLockCommandStore & WorkflowLockWakeStore;
11
+ interface WorkflowStepLockEngine {
12
+ acquireLock(key: string, options: AcquireLockOptions): Promise<LockRecord>;
13
+ releaseLock(key: string, options: ReleaseLockOptions): Promise<LockRecord>;
14
+ }
15
+ type WorkflowStepLockRunner = <TOutput>(config: WorkflowStepRunConfig, callback: (context: WorkflowStepContext) => Promise<TOutput> | TOutput) => Promise<TOutput>;
16
+ declare function createWorkflowStepLockApi(environment: WorkflowExecutionEnvironment, engine: WorkflowStepLockEngine, run: WorkflowStepLockRunner): WorkflowStepLockApi;
17
+ declare function acquireLock(store: WorkflowLockCommandStore, key: string, lockOptions: AcquireLockOptions, acquiredAt: Temporal.Instant): Promise<LockRecord>;
18
+ declare function releaseLock(store: WorkflowLockReleaseStore, key: string, lockOptions: ReleaseLockOptions, releasedAt: Temporal.Instant): Promise<LockRecord>;
19
+ declare function releaseStaleLock(store: WorkflowLockReleaseStore, key: string, releasedAt: Temporal.Instant): Promise<LockRecord>;
20
+ //#endregion
21
+ export { WorkflowStepLockEngine, acquireLock, createWorkflowStepLockApi, releaseLock, releaseStaleLock };