@workflow/core 5.0.0-beta.9 → 5.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 (339) hide show
  1. package/README.md +41 -0
  2. package/dist/attribute-changes.d.ts +4 -5
  3. package/dist/attribute-changes.d.ts.map +1 -1
  4. package/dist/attribute-changes.js +12 -4
  5. package/dist/capabilities.d.ts +35 -2
  6. package/dist/capabilities.d.ts.map +1 -1
  7. package/dist/capabilities.js +74 -5
  8. package/dist/capture-stack.d.ts +1 -1
  9. package/dist/capture-stack.js +2 -2
  10. package/dist/class-serialization.d.ts +32 -0
  11. package/dist/class-serialization.d.ts.map +1 -1
  12. package/dist/class-serialization.js +37 -1
  13. package/dist/classify-error.d.ts +15 -0
  14. package/dist/classify-error.d.ts.map +1 -1
  15. package/dist/classify-error.js +72 -4
  16. package/dist/context-violation-error.d.ts +5 -5
  17. package/dist/context-violation-error.d.ts.map +1 -1
  18. package/dist/context-violation-error.js +8 -6
  19. package/dist/create-hook.d.ts +109 -7
  20. package/dist/create-hook.d.ts.map +1 -1
  21. package/dist/create-hook.js +1 -1
  22. package/dist/define-hook.js +2 -2
  23. package/dist/describe-error.d.ts +7 -7
  24. package/dist/describe-error.d.ts.map +1 -1
  25. package/dist/describe-error.js +46 -7
  26. package/dist/encryption.d.ts +32 -4
  27. package/dist/encryption.d.ts.map +1 -1
  28. package/dist/encryption.js +85 -10
  29. package/dist/events-consumer.d.ts +300 -0
  30. package/dist/events-consumer.d.ts.map +1 -1
  31. package/dist/events-consumer.js +672 -42
  32. package/dist/flushable-stream.d.ts +97 -1
  33. package/dist/flushable-stream.d.ts.map +1 -1
  34. package/dist/flushable-stream.js +286 -10
  35. package/dist/global.d.ts +38 -2
  36. package/dist/global.d.ts.map +1 -1
  37. package/dist/global.js +21 -6
  38. package/dist/index.d.ts +2 -2
  39. package/dist/index.d.ts.map +1 -1
  40. package/dist/index.js +3 -3
  41. package/dist/log-format.d.ts +6 -2
  42. package/dist/log-format.d.ts.map +1 -1
  43. package/dist/log-format.js +60 -15
  44. package/dist/logger.d.ts +1 -0
  45. package/dist/logger.d.ts.map +1 -1
  46. package/dist/logger.js +10 -6
  47. package/dist/private.d.ts +258 -9
  48. package/dist/private.d.ts.map +1 -1
  49. package/dist/private.js +640 -7
  50. package/dist/replay-divergence.d.ts +25 -0
  51. package/dist/replay-divergence.d.ts.map +1 -0
  52. package/dist/replay-divergence.js +61 -0
  53. package/dist/replay-payload-cache.d.ts +64 -0
  54. package/dist/replay-payload-cache.d.ts.map +1 -0
  55. package/dist/replay-payload-cache.js +158 -0
  56. package/dist/runtime/compute-instance.d.ts +13 -0
  57. package/dist/runtime/compute-instance.d.ts.map +1 -0
  58. package/dist/runtime/compute-instance.js +14 -0
  59. package/dist/runtime/constants.d.ts +298 -5
  60. package/dist/runtime/constants.d.ts.map +1 -1
  61. package/dist/runtime/constants.js +500 -17
  62. package/dist/runtime/count-step-started-events.d.ts +52 -0
  63. package/dist/runtime/count-step-started-events.d.ts.map +1 -0
  64. package/dist/runtime/count-step-started-events.js +72 -0
  65. package/dist/runtime/deployment-guard.d.ts +100 -0
  66. package/dist/runtime/deployment-guard.d.ts.map +1 -0
  67. package/dist/runtime/deployment-guard.js +159 -0
  68. package/dist/runtime/dynamic-workflow.d.ts +148 -0
  69. package/dist/runtime/dynamic-workflow.d.ts.map +1 -0
  70. package/dist/runtime/dynamic-workflow.js +290 -0
  71. package/dist/runtime/get-port-lazy.d.ts +15 -0
  72. package/dist/runtime/get-port-lazy.d.ts.map +1 -1
  73. package/dist/runtime/get-port-lazy.js +75 -6
  74. package/dist/runtime/get-world-lazy.d.ts +5 -14
  75. package/dist/runtime/get-world-lazy.d.ts.map +1 -1
  76. package/dist/runtime/get-world-lazy.js +19 -29
  77. package/dist/runtime/helpers.d.ts +445 -29
  78. package/dist/runtime/helpers.d.ts.map +1 -1
  79. package/dist/runtime/helpers.js +717 -62
  80. package/dist/runtime/hook-wake.d.ts +122 -0
  81. package/dist/runtime/hook-wake.d.ts.map +1 -0
  82. package/dist/runtime/hook-wake.js +225 -0
  83. package/dist/runtime/invocations.d.ts +35 -0
  84. package/dist/runtime/invocations.d.ts.map +1 -0
  85. package/dist/runtime/invocations.js +172 -0
  86. package/dist/runtime/lifecycle-hooks.d.ts +93 -0
  87. package/dist/runtime/lifecycle-hooks.d.ts.map +1 -0
  88. package/dist/runtime/lifecycle-hooks.js +178 -0
  89. package/dist/runtime/open-hook-wait-state.d.ts +55 -0
  90. package/dist/runtime/open-hook-wait-state.d.ts.map +1 -0
  91. package/dist/runtime/open-hook-wait-state.js +93 -0
  92. package/dist/runtime/quickjs-assets.generated.d.ts +14 -0
  93. package/dist/runtime/quickjs-assets.generated.d.ts.map +1 -0
  94. package/dist/runtime/quickjs-assets.generated.js +30 -0
  95. package/dist/runtime/quickjs-entrypoint.d.ts +158 -0
  96. package/dist/runtime/quickjs-entrypoint.d.ts.map +1 -0
  97. package/dist/runtime/quickjs-entrypoint.js +1939 -0
  98. package/dist/runtime/quickjs-log-view.d.ts +129 -0
  99. package/dist/runtime/quickjs-log-view.d.ts.map +1 -0
  100. package/dist/runtime/quickjs-log-view.js +229 -0
  101. package/dist/runtime/quickjs-runtime.d.ts +247 -0
  102. package/dist/runtime/quickjs-runtime.d.ts.map +1 -0
  103. package/dist/runtime/quickjs-runtime.js +2599 -0
  104. package/dist/runtime/quickjs-serde.d.ts +107 -0
  105. package/dist/runtime/quickjs-serde.d.ts.map +1 -0
  106. package/dist/runtime/quickjs-serde.js +2234 -0
  107. package/dist/runtime/replay-budget.d.ts +17 -18
  108. package/dist/runtime/replay-budget.d.ts.map +1 -1
  109. package/dist/runtime/replay-budget.js +33 -81
  110. package/dist/runtime/replay-recovery-reporter.d.ts +36 -0
  111. package/dist/runtime/replay-recovery-reporter.d.ts.map +1 -0
  112. package/dist/runtime/replay-recovery-reporter.js +64 -0
  113. package/dist/runtime/resume-hook.d.ts +93 -8
  114. package/dist/runtime/resume-hook.d.ts.map +1 -1
  115. package/dist/runtime/resume-hook.js +709 -110
  116. package/dist/runtime/resume-latency.d.ts +150 -0
  117. package/dist/runtime/resume-latency.d.ts.map +1 -0
  118. package/dist/runtime/resume-latency.js +155 -0
  119. package/dist/runtime/run-id-time.d.ts +19 -0
  120. package/dist/runtime/run-id-time.d.ts.map +1 -0
  121. package/dist/runtime/run-id-time.js +42 -0
  122. package/dist/runtime/run-spec-version.d.ts +16 -0
  123. package/dist/runtime/run-spec-version.d.ts.map +1 -0
  124. package/dist/runtime/run-spec-version.js +19 -0
  125. package/dist/runtime/run.d.ts +72 -7
  126. package/dist/runtime/run.d.ts.map +1 -1
  127. package/dist/runtime/run.js +295 -58
  128. package/dist/runtime/runs.d.ts +54 -3
  129. package/dist/runtime/runs.d.ts.map +1 -1
  130. package/dist/runtime/runs.js +148 -21
  131. package/dist/runtime/start.d.ts +164 -3
  132. package/dist/runtime/start.d.ts.map +1 -1
  133. package/dist/runtime/start.js +700 -78
  134. package/dist/runtime/step-executor.d.ts +186 -5
  135. package/dist/runtime/step-executor.d.ts.map +1 -1
  136. package/dist/runtime/step-executor.js +756 -123
  137. package/dist/runtime/step-latency.d.ts +221 -0
  138. package/dist/runtime/step-latency.d.ts.map +1 -0
  139. package/dist/runtime/step-latency.js +215 -0
  140. package/dist/runtime/step-ownership.d.ts +72 -0
  141. package/dist/runtime/step-ownership.d.ts.map +1 -0
  142. package/dist/runtime/step-ownership.js +114 -0
  143. package/dist/runtime/step-single-flight.d.ts +12 -0
  144. package/dist/runtime/step-single-flight.d.ts.map +1 -0
  145. package/dist/runtime/step-single-flight.js +71 -0
  146. package/dist/runtime/suspension-handler.d.ts +279 -10
  147. package/dist/runtime/suspension-handler.d.ts.map +1 -1
  148. package/dist/runtime/suspension-handler.js +1442 -169
  149. package/dist/runtime/unserializable-step.d.ts +34 -0
  150. package/dist/runtime/unserializable-step.d.ts.map +1 -0
  151. package/dist/runtime/unserializable-step.js +52 -0
  152. package/dist/runtime/vm-mode.d.ts +44 -0
  153. package/dist/runtime/vm-mode.d.ts.map +1 -0
  154. package/dist/runtime/vm-mode.js +62 -0
  155. package/dist/runtime/vm-serde-bundle.generated.d.ts +14 -0
  156. package/dist/runtime/vm-serde-bundle.generated.d.ts.map +1 -0
  157. package/dist/runtime/vm-serde-bundle.generated.js +16 -0
  158. package/dist/runtime/wait-continuation.d.ts +106 -0
  159. package/dist/runtime/wait-continuation.d.ts.map +1 -0
  160. package/dist/runtime/wait-continuation.js +136 -0
  161. package/dist/runtime/wait-until.d.ts +18 -0
  162. package/dist/runtime/wait-until.d.ts.map +1 -0
  163. package/dist/runtime/wait-until.js +42 -0
  164. package/dist/runtime/world-compatibility.d.ts +32 -0
  165. package/dist/runtime/world-compatibility.d.ts.map +1 -0
  166. package/dist/runtime/world-compatibility.js +44 -0
  167. package/dist/runtime/world-init.d.ts +17 -42
  168. package/dist/runtime/world-init.d.ts.map +1 -1
  169. package/dist/runtime/world-init.js +18 -43
  170. package/dist/runtime/world.d.ts +14 -2
  171. package/dist/runtime/world.d.ts.map +1 -1
  172. package/dist/runtime/world.js +75 -19
  173. package/dist/runtime.d.ts +11 -7
  174. package/dist/runtime.d.ts.map +1 -1
  175. package/dist/runtime.js +4111 -446
  176. package/dist/schemas.d.ts +1 -1
  177. package/dist/schemas.d.ts.map +1 -1
  178. package/dist/schemas.js +1 -1
  179. package/dist/sealed-box.d.ts +167 -0
  180. package/dist/sealed-box.d.ts.map +1 -0
  181. package/dist/sealed-box.js +571 -0
  182. package/dist/serialization/client.d.ts +3 -3
  183. package/dist/serialization/client.d.ts.map +1 -1
  184. package/dist/serialization/client.js +8 -4
  185. package/dist/serialization/codec-devalue-vm.d.ts +16 -0
  186. package/dist/serialization/codec-devalue-vm.d.ts.map +1 -0
  187. package/dist/serialization/codec-devalue-vm.js +148 -0
  188. package/dist/serialization/codec-devalue.d.ts +1 -1
  189. package/dist/serialization/codec-devalue.d.ts.map +1 -1
  190. package/dist/serialization/codec-devalue.js +24 -6
  191. package/dist/serialization/codec.d.ts +37 -2
  192. package/dist/serialization/codec.d.ts.map +1 -1
  193. package/dist/serialization/codec.js +2 -2
  194. package/dist/serialization/compression.d.ts +105 -0
  195. package/dist/serialization/compression.d.ts.map +1 -0
  196. package/dist/serialization/compression.js +261 -0
  197. package/dist/serialization/encryption.d.ts +119 -17
  198. package/dist/serialization/encryption.d.ts.map +1 -1
  199. package/dist/serialization/encryption.js +152 -17
  200. package/dist/serialization/errors.d.ts +13 -0
  201. package/dist/serialization/errors.d.ts.map +1 -1
  202. package/dist/serialization/errors.js +20 -2
  203. package/dist/serialization/format.d.ts +6 -6
  204. package/dist/serialization/format.js +7 -7
  205. package/dist/serialization/hardened.d.ts +156 -0
  206. package/dist/serialization/hardened.d.ts.map +1 -0
  207. package/dist/serialization/hardened.js +538 -0
  208. package/dist/serialization/index.d.ts +10 -8
  209. package/dist/serialization/index.d.ts.map +1 -1
  210. package/dist/serialization/index.js +10 -8
  211. package/dist/serialization/reducers/class-vm.d.ts +20 -0
  212. package/dist/serialization/reducers/class-vm.d.ts.map +1 -0
  213. package/dist/serialization/reducers/class-vm.js +77 -0
  214. package/dist/serialization/reducers/class.d.ts.map +1 -1
  215. package/dist/serialization/reducers/class.js +11 -6
  216. package/dist/serialization/reducers/common-vm.d.ts +15 -0
  217. package/dist/serialization/reducers/common-vm.d.ts.map +1 -0
  218. package/dist/serialization/reducers/common-vm.js +661 -0
  219. package/dist/serialization/reducers/common.d.ts +1 -1
  220. package/dist/serialization/reducers/common.d.ts.map +1 -1
  221. package/dist/serialization/reducers/common.js +227 -66
  222. package/dist/serialization/reducers/step-function-vm.d.ts +44 -0
  223. package/dist/serialization/reducers/step-function-vm.d.ts.map +1 -0
  224. package/dist/serialization/reducers/step-function-vm.js +97 -0
  225. package/dist/serialization/reducers/step-function.d.ts.map +1 -1
  226. package/dist/serialization/reducers/step-function.js +25 -9
  227. package/dist/serialization/step.d.ts +4 -4
  228. package/dist/serialization/step.d.ts.map +1 -1
  229. package/dist/serialization/step.js +9 -5
  230. package/dist/serialization/types.d.ts +75 -1
  231. package/dist/serialization/types.d.ts.map +1 -1
  232. package/dist/serialization/types.js +15 -1
  233. package/dist/serialization/workflow-vm.d.ts +29 -0
  234. package/dist/serialization/workflow-vm.d.ts.map +1 -0
  235. package/dist/serialization/workflow-vm.js +74 -0
  236. package/dist/serialization-format.d.ts +64 -8
  237. package/dist/serialization-format.d.ts.map +1 -1
  238. package/dist/serialization-format.js +239 -73
  239. package/dist/serialization.d.ts +319 -34
  240. package/dist/serialization.d.ts.map +1 -1
  241. package/dist/serialization.js +1961 -321
  242. package/dist/set-attributes.d.ts +7 -7
  243. package/dist/set-attributes.d.ts.map +1 -1
  244. package/dist/set-attributes.js +42 -21
  245. package/dist/source-map.d.ts +25 -0
  246. package/dist/source-map.d.ts.map +1 -1
  247. package/dist/source-map.js +113 -7
  248. package/dist/step/context-storage.d.ts +50 -3
  249. package/dist/step/context-storage.d.ts.map +1 -1
  250. package/dist/step/context-storage.js +7 -5
  251. package/dist/step/writable-stream.d.ts +3 -3
  252. package/dist/step/writable-stream.d.ts.map +1 -1
  253. package/dist/step/writable-stream.js +53 -19
  254. package/dist/step.d.ts.map +1 -1
  255. package/dist/step.js +142 -28
  256. package/dist/symbols.d.ts +35 -1
  257. package/dist/symbols.d.ts.map +1 -1
  258. package/dist/symbols.js +36 -2
  259. package/dist/telemetry/semantic-conventions.d.ts +481 -2
  260. package/dist/telemetry/semantic-conventions.d.ts.map +1 -1
  261. package/dist/telemetry/semantic-conventions.js +349 -3
  262. package/dist/telemetry.d.ts +91 -0
  263. package/dist/telemetry.d.ts.map +1 -1
  264. package/dist/telemetry.js +244 -17
  265. package/dist/test-support/orchestrator-context.d.ts +25 -0
  266. package/dist/test-support/orchestrator-context.d.ts.map +1 -0
  267. package/dist/test-support/orchestrator-context.js +107 -0
  268. package/dist/types.d.ts +23 -0
  269. package/dist/types.d.ts.map +1 -1
  270. package/dist/types.js +76 -1
  271. package/dist/util.d.ts +0 -5
  272. package/dist/util.d.ts.map +1 -1
  273. package/dist/util.js +1 -15
  274. package/dist/version.d.ts +1 -1
  275. package/dist/version.d.ts.map +1 -1
  276. package/dist/version.js +2 -2
  277. package/dist/vm/index.d.ts.map +1 -1
  278. package/dist/vm/index.js +84 -14
  279. package/dist/vm/script-cache.d.ts +26 -0
  280. package/dist/vm/script-cache.d.ts.map +1 -0
  281. package/dist/vm/script-cache.js +131 -0
  282. package/dist/vm/uint8array-base64.js +1 -1
  283. package/dist/workflow/abort-controller.d.ts +2 -2
  284. package/dist/workflow/abort-controller.d.ts.map +1 -1
  285. package/dist/workflow/abort-controller.js +75 -21
  286. package/dist/workflow/attribute-dispatcher.d.ts +6 -0
  287. package/dist/workflow/attribute-dispatcher.d.ts.map +1 -0
  288. package/dist/workflow/attribute-dispatcher.js +65 -0
  289. package/dist/workflow/create-hook.d.ts.map +1 -1
  290. package/dist/workflow/create-hook.js +24 -3
  291. package/dist/workflow/get-workflow-metadata.js +2 -2
  292. package/dist/workflow/hook.d.ts.map +1 -1
  293. package/dist/workflow/hook.js +403 -53
  294. package/dist/workflow/index.d.ts +1 -1
  295. package/dist/workflow/index.d.ts.map +1 -1
  296. package/dist/workflow/index.js +2 -2
  297. package/dist/workflow/set-attributes.d.ts +19 -21
  298. package/dist/workflow/set-attributes.d.ts.map +1 -1
  299. package/dist/workflow/set-attributes.js +23 -25
  300. package/dist/workflow/sleep.d.ts.map +1 -1
  301. package/dist/workflow/sleep.js +42 -13
  302. package/dist/workflow/world-init-stub.d.ts +3 -3
  303. package/dist/workflow/world-init-stub.js +4 -4
  304. package/dist/workflow.d.ts +89 -3
  305. package/dist/workflow.d.ts.map +1 -1
  306. package/dist/workflow.js +920 -568
  307. package/docs/api-reference/create-hook.mdx +166 -10
  308. package/docs/api-reference/create-webhook.mdx +16 -15
  309. package/docs/api-reference/define-hook.mdx +37 -33
  310. package/docs/api-reference/fatal-error.mdx +30 -8
  311. package/docs/api-reference/fetch.mdx +14 -10
  312. package/docs/api-reference/get-step-metadata.mdx +2 -2
  313. package/docs/api-reference/get-workflow-metadata.mdx +3 -3
  314. package/docs/api-reference/get-writable.mdx +7 -7
  315. package/docs/api-reference/index.mdx +3 -3
  316. package/docs/api-reference/retryable-error.mdx +1 -1
  317. package/docs/api-reference/set-attributes.mdx +63 -0
  318. package/docs/api-reference/sleep.mdx +4 -4
  319. package/docs/foundations/cancellation.mdx +31 -32
  320. package/docs/foundations/errors-and-retries.mdx +54 -11
  321. package/docs/foundations/hooks.mdx +98 -35
  322. package/docs/foundations/idempotency.mdx +267 -12
  323. package/docs/foundations/index.mdx +1 -26
  324. package/docs/foundations/serialization.mdx +22 -22
  325. package/docs/foundations/starting-workflows.mdx +104 -30
  326. package/docs/foundations/streaming.mdx +108 -60
  327. package/docs/foundations/versioning.mdx +4 -4
  328. package/docs/foundations/workflows-and-steps.mdx +9 -9
  329. package/docs/how-it-works/cancellation.mdx +63 -63
  330. package/docs/how-it-works/code-transform.mdx +82 -66
  331. package/docs/how-it-works/encryption.mdx +30 -26
  332. package/docs/how-it-works/event-sourcing.mdx +132 -35
  333. package/docs/how-it-works/framework-integrations.mdx +96 -337
  334. package/docs/how-it-works/understanding-directives.mdx +22 -22
  335. package/package.json +29 -15
  336. package/dist/runtime/step-handler.d.ts +0 -2
  337. package/dist/runtime/step-handler.d.ts.map +0 -1
  338. package/dist/runtime/step-handler.js +0 -779
  339. package/docs/api-reference/experimental-set-attributes.mdx +0 -63
package/dist/private.d.ts CHANGED
@@ -1,10 +1,12 @@
1
1
  /**
2
2
  * Utils used by the bundler when transforming code
3
3
  */
4
- import type { CryptoKey } from './encryption.js';
4
+ import type { WorldCapabilities } from '@workflow/world';
5
5
  import type { EventsConsumer } from './events-consumer.js';
6
- import type { QueueItem } from './global.js';
6
+ import { type QueueItem } from './global.js';
7
+ import type { ReplayPayloadCache } from './replay-payload-cache.js';
7
8
  import type { Serializable } from './schemas.js';
9
+ import type { PayloadKey } from './serialization/encryption.js';
8
10
  export type StepFunction<Args extends Serializable[] = any[], Result extends Serializable | unknown = unknown> = ((...args: Args) => Promise<Result>) & {
9
11
  maxRetries?: number;
10
12
  stepId?: string;
@@ -25,8 +27,17 @@ export declare function registerStepFunction(stepId: string, stepFn: StepFunctio
25
27
  export declare function getStepFunction(stepId: string): StepFunction | undefined;
26
28
  export interface WorkflowOrchestratorContext {
27
29
  runId: string;
28
- encryptionKey: CryptoKey | undefined;
30
+ encryptionKey: PayloadKey | undefined;
31
+ worldCapabilities?: WorldCapabilities;
29
32
  globalThis: typeof globalThis;
33
+ /**
34
+ * Increments when a suspension is accepted and on every retained-session
35
+ * resume. Step, hook, wait, and attribute suspension signals capture it when
36
+ * scheduled and no-op if it moved, which drops same-boundary sibling signals
37
+ * and timers queued at boundary N that would fire after the session resumed
38
+ * into boundary N+1.
39
+ */
40
+ suspensionGeneration: number;
30
41
  eventsConsumer: EventsConsumer;
31
42
  /**
32
43
  * Map of pending invocations keyed by correlationId.
@@ -34,7 +45,20 @@ export interface WorkflowOrchestratorContext {
34
45
  */
35
46
  invocationsQueue: Map<string, QueueItem>;
36
47
  onWorkflowError: (error: Error) => void;
48
+ /**
49
+ * Mints the ULID body of a correlation id. Every entity a replay creates
50
+ * draws from this one monotonic sequence, so an id is an ordinal over the
51
+ * whole run and both replays of a run must draw in the same order.
52
+ */
37
53
  generateUlid: () => string;
54
+ /**
55
+ * Monotone count of correlation-id draws this replay has made. Progress
56
+ * metric for {@link quiesceEarlierCascades}: a macrotask turn in which it
57
+ * does not move (and no hydration is in flight) means every woken branch has
58
+ * run as far as it can without another delivery. Optional so lightweight
59
+ * test contexts degrade to the single-yield behavior.
60
+ */
61
+ readonly mintCount?: number;
38
62
  generateNanoid: () => string;
39
63
  /**
40
64
  * Sequential promise queue that ensures all event-driven promise resolutions
@@ -46,18 +70,243 @@ export interface WorkflowOrchestratorContext {
46
70
  promiseQueue: Promise<void>;
47
71
  /**
48
72
  * Counter of in-flight async data delivery operations (step result
49
- * hydration, hook payload hydration). Suspensions must wait for this
50
- * to reach 0 before firing, to avoid preempting data delivery.
73
+ * hydration, hook payload hydration, abort signal hydration). Suspensions
74
+ * must wait for this to reach 0 before firing, to avoid preempting data
75
+ * delivery, e.g. dehydrating a step's arguments while an abort that should
76
+ * be reflected in those arguments is still hydrating its reason.
51
77
  */
52
78
  pendingDeliveries: number;
79
+ /**
80
+ * Ordered registry of in-flight "branch-deciding" deliveries: the
81
+ * resolutions a workflow typically `Promise.race`s on, or awaits from
82
+ * independent concurrent branches: hook payloads (`hook_received`), hook
83
+ * registration outcomes (`hook_created` / `hook_conflict`), wait completions
84
+ * (`wait_completed`), and step results (`step_completed` / `step_failed`).
85
+ * Keyed by the delivery's position (index) in the consumed event log.
86
+ *
87
+ * The problem: each of these resolutions reaches workflow code after a
88
+ * different, workload-dependent number of microtask hops. A buffered hook
89
+ * payload is observed via the async hook iterator (`yield await this`),
90
+ * costing extra hops; a `wait_completed` resolves with fewer, and a reused
91
+ * sleep can resolve in an entirely earlier loop iteration; a step result is
92
+ * gated on hydration whose cost varies between replays of the SAME
93
+ * invocation: the first replay pays the full decrypt/decompress/revive,
94
+ * while later replays sharing the invocation's `ReplayPayloadCache`
95
+ * memo-hit small primitive results and resolve in one or two hops. Either
96
+ * way, the resolution that the committed event log ordered first can lose a
97
+ * `Promise.race` (or a `useStep` ULID allocation) to a faster- or
98
+ * already-resolved competitor, diverging from the log and surfacing as
99
+ * `CorruptedEventLogError`.
100
+ *
101
+ * The fix is a strict, deterministic delivery order anchored on
102
+ * event-log position: a delivery does not resolve to the workflow until
103
+ * every relevant earlier-in-log delivery has been delivered. Because the
104
+ * gate is "the earlier delivery resolved", not "won a timing race", the
105
+ * outcome is independent of microtask hops, hydration/decryption time,
106
+ * and `Promise.race` argument order. Which earlier kinds a delivery defers
107
+ * behind is spelled out on {@link awaitEarlierDeliveries}.
108
+ *
109
+ * Index is used rather than the `eventId` string because `eventId` is an
110
+ * opaque, world-assigned value not guaranteed to sort in creation order
111
+ * (only the bundled ULID worlds happen to).
112
+ *
113
+ * Optional so older/out-of-tree contexts (and lightweight test harnesses)
114
+ * that do not initialize it degrade gracefully to the previous behavior.
115
+ */
116
+ pendingDeliveryBarriers?: Map<number, DeliveryBarrierEntry>;
117
+ /**
118
+ * Advance the workflow's deterministic clock (`Date.now()` inside the VM)
119
+ * to `at`, never backwards. Called by {@link registerDeliveryBarrier} when a
120
+ * branch-deciding delivery is handed to the workflow, so the clock a
121
+ * cascade observes is the timestamp of the delivery that woke it.
122
+ *
123
+ * The clock is NOT advanced when an event is merely consumed. The
124
+ * `EventsConsumer` walks ahead of delivery: within one drain window it
125
+ * consumes every event whose consumer exists, so a `hook_received` for a
126
+ * hook the workflow has not read, or a `wait_completed` the body is still
127
+ * hops away from observing, is consumed while an earlier delivery is
128
+ * still parked on its barrier. Advancing the clock at consumption let those
129
+ * later timestamps leak into the earlier delivery's cascade, and the value
130
+ * `Date.now()` returned at a body position then depended on how much log
131
+ * the replay had loaded: the execution that wrote the log saw the
132
+ * heartbeat's time, a later replay holding one more payload saw the
133
+ * payload's. A workflow whose control flow reads the clock (an idle loop
134
+ * budgeted by `Date.now()`, a deadline) then drew different ordinals in
135
+ * different replays and died `CORRUPTED_EVENT_LOG`.
136
+ *
137
+ * Optional so older/out-of-tree contexts degrade gracefully.
138
+ */
139
+ advanceClock?: (at: number) => void;
140
+ /**
141
+ * Invocation-scoped cache of prepared serialized payloads and immutable final
142
+ * values. Prepared bytes survive fresh replay VMs; object graphs do not.
143
+ */
144
+ replayPayloadCache: ReplayPayloadCache;
145
+ }
146
+ /** The kind of branch-deciding delivery a barrier represents. */
147
+ export type DeliveryKind = 'hook' | 'wait' | 'step';
148
+ interface DeliveryBarrierEntry {
149
+ kind: DeliveryKind;
150
+ /** Resolves once this delivery is handed to the workflow or retired. */
151
+ released: Promise<void>;
152
+ /**
153
+ * Whether this delivery is committed to reaching the workflow without any
154
+ * further action by workflow code. True for wait completions and step
155
+ * results, which always resolve from their own chain, and for a hook payload
156
+ * that already had a waiting consumer when it was consumed.
157
+ *
158
+ * False for a BUFFERED hook payload no consumer has claimed yet: it is
159
+ * delivered by `claim()`, i.e. whenever the workflow next reads the hook,
160
+ * which may be causally *after* a later-in-log delivery. `arm()` flips it
161
+ * once a consumer takes the payload.
162
+ */
163
+ armed: boolean;
164
+ /** Whether this entry has been removed and its `released` promise settled. */
165
+ retired: boolean;
166
+ /**
167
+ * Retire this entry: resolve `released` and remove it from the registry,
168
+ * without marking the handle delivered to the workflow. A safety-retired
169
+ * buffered payload may therefore install a fresh entry if it is claimed by
170
+ * a retained VM later. Called only by the context's safety-net dispenser
171
+ * ({@link ensureBarrierSafetyNet}), and only on the lowest-index entry at
172
+ * delivery idle. Idempotent.
173
+ */
174
+ retire: () => void;
53
175
  }
176
+ export declare function awaitEarlierDeliveries(ctx: WorkflowOrchestratorContext, eventIndex: number | undefined, kind: DeliveryKind): Promise<void>;
177
+ /** Handle for a registered branch-deciding delivery barrier. */
178
+ export interface DeliveryBarrier {
179
+ /**
180
+ * Mark this delivery as delivered to the workflow. Resolves its
181
+ * `released` promise so any later-in-log delivery gated on it (via
182
+ * {@link awaitEarlierDeliveries}) may proceed, and removes it from the
183
+ * registry. Idempotent.
184
+ */
185
+ markDelivered: () => void;
186
+ /**
187
+ * Mark this delivery as committed to happening, for a barrier registered
188
+ * unarmed (a buffered hook payload) once a consumer has claimed it. From
189
+ * then on a later step result may be ordered behind it. Idempotent.
190
+ */
191
+ arm: () => void;
192
+ }
193
+ /**
194
+ * Register a branch-deciding delivery at its event-log index so that later
195
+ * deliveries can be ordered strictly after it. Returns an inert handle when
196
+ * `pendingDeliveryBarriers` is not initialized.
197
+ *
198
+ * Pass `armed: false` for a delivery whose resolution waits on workflow code
199
+ * asking for it (a buffered hook payload); call `arm()` when it does.
200
+ *
201
+ * To guarantee a later delivery gated on this one can never hang when this
202
+ * delivery is abandoned (the workflow took a different branch or is
203
+ * suspending and never observes it), the barrier auto-resolves at idle.
204
+ *
205
+ * INVARIANT required of every call site: a barrier that is ever `armed` must
206
+ * be paired with a delivery chain that runs unconditionally, attached when
207
+ * the event is consumed (waits, step results, waiting-consumer hook payloads,
208
+ * aborts), or by the `claim()` whose invocation is what arms it (buffered
209
+ * hook payloads). The idle check ({@link scheduleWhenIdle}) refuses to
210
+ * observe idle while an armed, self-resolving barrier is undelivered, and the
211
+ * safety net below is itself idle-gated, so an armed barrier with no
212
+ * unconditional chain would livelock every idle check in the run, including
213
+ * its own retirement.
214
+ */
215
+ export declare function registerDeliveryBarrier(ctx: WorkflowOrchestratorContext, eventIndex: number | undefined, kind: DeliveryKind, options: {
216
+ armed?: boolean;
217
+ /**
218
+ * The delivered event's `createdAt`. On `markDelivered` the workflow
219
+ * clock advances to it (see {@link WorkflowOrchestratorContext.advanceClock}),
220
+ * so `Date.now()` in the cascade this delivery wakes reads the delivery's
221
+ * own time, whatever the consumer walk has read ahead of it. Required so
222
+ * that a new delivery site cannot forget the clock: a delivery that does
223
+ * not move it leaves the cascade it wakes reading a stale time.
224
+ */
225
+ deliveredAt: number;
226
+ }): DeliveryBarrier;
227
+ /**
228
+ * Whether some registered branch-deciding delivery is going to reach the
229
+ * workflow without any further help (it is armed and not transitively parked
230
+ * behind an unclaimed buffered payload, see {@link resolvesOnItsOwn}) but
231
+ * has not been handed over yet.
232
+ *
233
+ * This is the delivery state `pendingDeliveries` cannot see. That counter
234
+ * covers the hydration window inside a serial `promiseQueue` slot and is
235
+ * released there, while the delivery's `resolve()` runs later, from a
236
+ * detached continuation behind {@link awaitEarlierDeliveries}, including its
237
+ * macrotask yield whenever the delivery had to defer. Replaying a batch of N
238
+ * parallel step results consumed in one drain window leaves N-1 of them
239
+ * parked on that yield with `pendingDeliveries` already at 0. An idle check
240
+ * armed during the same window (a pending `sleep()` arms one on every replay)
241
+ * could then observe "idle" mid-deferral and raise a `WorkflowSuspension`
242
+ * BEFORE the workflow's own continuations ran: a suspension carrying none of
243
+ * the follow-up work the batch was about to create, which the runtime
244
+ * dutifully schedules as nothing, leaving the run dormant until an unrelated
245
+ * timer fires (vercel/workflow#3183).
246
+ *
247
+ * Deliveries that do NOT resolve on their own must be excluded, not for
248
+ * accuracy but for termination: an unclaimed buffered hook payload is retired
249
+ * BY the idle safety net in {@link registerDeliveryBarrier}, so counting it
250
+ * here would gate its own retirement. That reasoning extends to whatever is
251
+ * parked behind such a payload (a wait, and a step gating on that wait) for
252
+ * the same reason: the whole chain moves only once the net fires, and it
253
+ * cannot fire while the chain is counted. Self-resolving deliveries always
254
+ * deliver from their own chains (see the INVARIANT on
255
+ * {@link registerDeliveryBarrier}) and never need that net, so waiting on
256
+ * them is deadlock-free.
257
+ */
258
+ export declare function hasParkedCommittedDelivery(ctx: WorkflowOrchestratorContext): boolean;
259
+ /**
260
+ * Whether no data delivery (step result, hook payload) is in flight right now.
261
+ *
262
+ * "In flight" is two distinct windows, each with its own guard:
263
+ * `pendingDeliveries > 0` covers hydration inside the serial queue slots, and
264
+ * {@link hasParkedCommittedDelivery} covers the detached gap between a slot
265
+ * releasing that counter and the delivery's `resolve()` actually running,
266
+ * deliberately outside `pendingDeliveries` (see step.ts), and invisible to it.
267
+ *
268
+ * Anything that decides a replay is over, or that a replay went wrong, has to
269
+ * consult this first: while it is false the workflow VM is mid-reaction, so
270
+ * what it has and has not done yet says nothing about the run. Two callers
271
+ * read it, for the two such decisions: {@link scheduleWhenIdle} for the
272
+ * suspension, and the events consumer's unconsumed-event check for divergence.
273
+ *
274
+ * A non-empty barrier registry counts as in flight, even when every remaining
275
+ * entry is parked behind an unclaimed buffered payload. Those entries only
276
+ * move when the safety-net dispenser retires them (lowest-first, see
277
+ * {@link ensureBarrierSafetyNet}), and the deliveries they release are real
278
+ * workflow reactions: a suspension raised before they run would be computed
279
+ * from a VM that has not seen them, scheduling none of their follow-up work
280
+ * and leaving the run dormant (the vercel/workflow#3183 shape). The dispenser
281
+ * itself is gated on {@link canRetireAbandonedBarriers}, the weaker predicate
282
+ * without the registry term, precisely so it can do the draining that this
283
+ * predicate waits for; registry size strictly decreases at each retirement,
284
+ * so idle is always reached.
285
+ */
286
+ export declare function isDeliveryIdle(ctx: WorkflowOrchestratorContext): boolean;
54
287
  /**
55
288
  * Schedule a callback to fire only after all pending data deliveries
56
289
  * (step results, hook payloads) and async deserialization have completed.
57
- * Uses a polling loop: setTimeout(0) → check pendingDeliveries →
58
- * if > 0, wait for promiseQueue → repeat. This handles the multi-round
59
- * delivery pattern where each hook payload delivery cycle appends new
60
- * async work to the promiseQueue.
290
+ * Uses a polling loop: setTimeout(0) → check pendingDeliveries and the
291
+ * barrier registry → if anything is still in flight, wait for promiseQueue →
292
+ * repeat. This handles the multi-round delivery pattern where each hook
293
+ * payload delivery cycle appends new async work to the promiseQueue. What
294
+ * counts as in flight is {@link isDeliveryIdle}.
295
+ *
296
+ * The initial `setTimeout(0)` macrotask is load-bearing and must NOT be
297
+ * downgraded to a microtask (`queueMicrotask`/`Promise.resolve().then`).
298
+ * `pendingDeliveries` only guards the host-side hydration window; between a
299
+ * delivery's `resolve()` and the workflow VM body running its continuation to
300
+ * register the next subscriber, `pendingDeliveries` is already 0 even though
301
+ * the VM is mid-reaction. Node does not guarantee a microtask scheduled in
302
+ * the host context settles after the cross-VM promise chain (resolve in host
303
+ * → workflow code in VM → subscribe back in host); the macrotask boundary
304
+ * gives that chain time to run, so the suspension does not preempt a sibling
305
+ * delivery still in flight. Empirically, replacing it with `queueMicrotask`
306
+ * breaks hook/sleep `Promise.race` ordering (CorruptedEventLogError).
61
307
  */
62
308
  export declare function scheduleWhenIdle(ctx: WorkflowOrchestratorContext, fn: () => void): void;
309
+ /** Schedule a generation-guarded suspension after deliveries settle. */
310
+ export declare function scheduleWorkflowSuspension(ctx: WorkflowOrchestratorContext): void;
311
+ export {};
63
312
  //# sourceMappingURL=private.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"private.d.ts","sourceRoot":"","sources":["../src/private.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EA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