@workflow/core 5.0.0-beta.4 → 5.0.0-beta.40

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 (325) hide show
  1. package/dist/attribute-changes.d.ts +7 -0
  2. package/dist/attribute-changes.d.ts.map +1 -0
  3. package/dist/attribute-changes.js +25 -0
  4. package/dist/capabilities.d.ts +33 -1
  5. package/dist/capabilities.d.ts.map +1 -1
  6. package/dist/capabilities.js +71 -4
  7. package/dist/capture-stack.d.ts +16 -0
  8. package/dist/capture-stack.d.ts.map +1 -0
  9. package/dist/capture-stack.js +21 -0
  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 +26 -3
  14. package/dist/classify-error.d.ts.map +1 -1
  15. package/dist/classify-error.js +109 -6
  16. package/dist/context-errors.d.ts +27 -0
  17. package/dist/context-errors.d.ts.map +1 -0
  18. package/dist/context-errors.js +101 -0
  19. package/dist/context-violation-error.d.ts +97 -0
  20. package/dist/context-violation-error.d.ts.map +1 -0
  21. package/dist/context-violation-error.js +149 -0
  22. package/dist/correlation-id.d.ts +109 -0
  23. package/dist/correlation-id.d.ts.map +1 -0
  24. package/dist/correlation-id.js +139 -0
  25. package/dist/create-hook.d.ts +62 -4
  26. package/dist/create-hook.d.ts.map +1 -1
  27. package/dist/create-hook.js +4 -3
  28. package/dist/define-hook.d.ts.map +1 -1
  29. package/dist/define-hook.js +20 -5
  30. package/dist/describe-error.d.ts +70 -0
  31. package/dist/describe-error.d.ts.map +1 -0
  32. package/dist/describe-error.js +189 -0
  33. package/dist/encryption.d.ts +37 -3
  34. package/dist/encryption.d.ts.map +1 -1
  35. package/dist/encryption.js +97 -31
  36. package/dist/events-consumer.d.ts +26 -1
  37. package/dist/events-consumer.d.ts.map +1 -1
  38. package/dist/events-consumer.js +105 -18
  39. package/dist/flushable-stream.d.ts +65 -10
  40. package/dist/flushable-stream.d.ts.map +1 -1
  41. package/dist/flushable-stream.js +131 -18
  42. package/dist/global.d.ts +37 -1
  43. package/dist/global.d.ts.map +1 -1
  44. package/dist/global.js +21 -3
  45. package/dist/index.d.ts +1 -0
  46. package/dist/index.d.ts.map +1 -1
  47. package/dist/index.js +2 -1
  48. package/dist/log-format.d.ts +25 -0
  49. package/dist/log-format.d.ts.map +1 -0
  50. package/dist/log-format.js +250 -0
  51. package/dist/logger.d.ts +29 -30
  52. package/dist/logger.d.ts.map +1 -1
  53. package/dist/logger.js +82 -32
  54. package/dist/private.d.ts +178 -9
  55. package/dist/private.d.ts.map +1 -1
  56. package/dist/private.js +273 -9
  57. package/dist/replay-payload-cache.d.ts +68 -0
  58. package/dist/replay-payload-cache.d.ts.map +1 -0
  59. package/dist/replay-payload-cache.js +160 -0
  60. package/dist/runtime/compute-instance.d.ts +12 -0
  61. package/dist/runtime/compute-instance.d.ts.map +1 -0
  62. package/dist/runtime/compute-instance.js +13 -0
  63. package/dist/runtime/constants.d.ts +237 -0
  64. package/dist/runtime/constants.d.ts.map +1 -1
  65. package/dist/runtime/constants.js +405 -15
  66. package/dist/runtime/count-step-started-events.d.ts +52 -0
  67. package/dist/runtime/count-step-started-events.d.ts.map +1 -0
  68. package/dist/runtime/count-step-started-events.js +72 -0
  69. package/dist/runtime/deployment-guard.d.ts +99 -0
  70. package/dist/runtime/deployment-guard.d.ts.map +1 -0
  71. package/dist/runtime/deployment-guard.js +154 -0
  72. package/dist/runtime/get-port-lazy.d.ts +25 -0
  73. package/dist/runtime/get-port-lazy.d.ts.map +1 -0
  74. package/dist/runtime/get-port-lazy.js +92 -0
  75. package/dist/runtime/get-world-lazy.d.ts +23 -0
  76. package/dist/runtime/get-world-lazy.d.ts.map +1 -0
  77. package/dist/runtime/get-world-lazy.js +46 -0
  78. package/dist/runtime/helpers.d.ts +219 -10
  79. package/dist/runtime/helpers.d.ts.map +1 -1
  80. package/dist/runtime/helpers.js +414 -40
  81. package/dist/runtime/quickjs-assets.generated.d.ts +14 -0
  82. package/dist/runtime/quickjs-assets.generated.d.ts.map +1 -0
  83. package/dist/runtime/quickjs-assets.generated.js +30 -0
  84. package/dist/runtime/quickjs-entrypoint.d.ts +124 -0
  85. package/dist/runtime/quickjs-entrypoint.d.ts.map +1 -0
  86. package/dist/runtime/quickjs-entrypoint.js +1429 -0
  87. package/dist/runtime/quickjs-runtime.d.ts +193 -0
  88. package/dist/runtime/quickjs-runtime.d.ts.map +1 -0
  89. package/dist/runtime/quickjs-runtime.js +1944 -0
  90. package/dist/runtime/replay-budget.d.ts +98 -0
  91. package/dist/runtime/replay-budget.d.ts.map +1 -0
  92. package/dist/runtime/replay-budget.js +192 -0
  93. package/dist/runtime/replay-recovery-reporter.d.ts +36 -0
  94. package/dist/runtime/replay-recovery-reporter.d.ts.map +1 -0
  95. package/dist/runtime/replay-recovery-reporter.js +64 -0
  96. package/dist/runtime/resume-hook.d.ts +27 -4
  97. package/dist/runtime/resume-hook.d.ts.map +1 -1
  98. package/dist/runtime/resume-hook.js +448 -76
  99. package/dist/runtime/run-id-time.d.ts +19 -0
  100. package/dist/runtime/run-id-time.d.ts.map +1 -0
  101. package/dist/runtime/run-id-time.js +42 -0
  102. package/dist/runtime/run.d.ts +8 -2
  103. package/dist/runtime/run.d.ts.map +1 -1
  104. package/dist/runtime/run.js +62 -15
  105. package/dist/runtime/runs.d.ts +54 -3
  106. package/dist/runtime/runs.d.ts.map +1 -1
  107. package/dist/runtime/runs.js +117 -10
  108. package/dist/runtime/start.d.ts +70 -1
  109. package/dist/runtime/start.d.ts.map +1 -1
  110. package/dist/runtime/start.js +312 -54
  111. package/dist/runtime/step-executor.d.ts +177 -0
  112. package/dist/runtime/step-executor.d.ts.map +1 -0
  113. package/dist/runtime/step-executor.js +949 -0
  114. package/dist/runtime/step-latency.d.ts +197 -0
  115. package/dist/runtime/step-latency.d.ts.map +1 -0
  116. package/dist/runtime/step-latency.js +207 -0
  117. package/dist/runtime/step-ownership.d.ts +72 -0
  118. package/dist/runtime/step-ownership.d.ts.map +1 -0
  119. package/dist/runtime/step-ownership.js +114 -0
  120. package/dist/runtime/step-single-flight.d.ts +12 -0
  121. package/dist/runtime/step-single-flight.d.ts.map +1 -0
  122. package/dist/runtime/step-single-flight.js +69 -0
  123. package/dist/runtime/suspension-handler.d.ts +102 -7
  124. package/dist/runtime/suspension-handler.d.ts.map +1 -1
  125. package/dist/runtime/suspension-handler.js +439 -140
  126. package/dist/runtime/vm-mode.d.ts +44 -0
  127. package/dist/runtime/vm-mode.d.ts.map +1 -0
  128. package/dist/runtime/vm-mode.js +62 -0
  129. package/dist/runtime/vm-serde-bundle.generated.d.ts +14 -0
  130. package/dist/runtime/vm-serde-bundle.generated.d.ts.map +1 -0
  131. package/dist/runtime/vm-serde-bundle.generated.js +16 -0
  132. package/dist/runtime/wait-continuation.d.ts +84 -0
  133. package/dist/runtime/wait-continuation.d.ts.map +1 -0
  134. package/dist/runtime/wait-continuation.js +105 -0
  135. package/dist/runtime/wait-until.d.ts +18 -0
  136. package/dist/runtime/wait-until.d.ts.map +1 -0
  137. package/dist/runtime/wait-until.js +42 -0
  138. package/dist/runtime/world-compatibility.d.ts +5 -0
  139. package/dist/runtime/world-compatibility.d.ts.map +1 -0
  140. package/dist/runtime/world-compatibility.js +12 -0
  141. package/dist/runtime/world-init.d.ts +50 -0
  142. package/dist/runtime/world-init.d.ts.map +1 -0
  143. package/dist/runtime/world-init.js +50 -0
  144. package/dist/runtime/world.d.ts +14 -2
  145. package/dist/runtime/world.d.ts.map +1 -1
  146. package/dist/runtime/world.js +101 -26
  147. package/dist/runtime.d.ts +17 -12
  148. package/dist/runtime.d.ts.map +1 -1
  149. package/dist/runtime.js +3078 -327
  150. package/dist/schemas.d.ts +1 -1
  151. package/dist/schemas.d.ts.map +1 -1
  152. package/dist/schemas.js +1 -1
  153. package/dist/sealed-box.d.ts +167 -0
  154. package/dist/sealed-box.d.ts.map +1 -0
  155. package/dist/sealed-box.js +571 -0
  156. package/dist/serialization/client.d.ts +17 -0
  157. package/dist/serialization/client.d.ts.map +1 -0
  158. package/dist/serialization/client.js +48 -0
  159. package/dist/serialization/codec-devalue-vm.d.ts +9 -0
  160. package/dist/serialization/codec-devalue-vm.d.ts.map +1 -0
  161. package/dist/serialization/codec-devalue-vm.js +137 -0
  162. package/dist/serialization/codec-devalue.d.ts +14 -0
  163. package/dist/serialization/codec-devalue.d.ts.map +1 -0
  164. package/dist/serialization/codec-devalue.js +105 -0
  165. package/dist/serialization/codec.d.ts +125 -0
  166. package/dist/serialization/codec.d.ts.map +1 -0
  167. package/dist/serialization/codec.js +17 -0
  168. package/dist/serialization/compression.d.ts +104 -0
  169. package/dist/serialization/compression.d.ts.map +1 -0
  170. package/dist/serialization/compression.js +260 -0
  171. package/dist/serialization/encryption.d.ts +134 -0
  172. package/dist/serialization/encryption.d.ts.map +1 -0
  173. package/dist/serialization/encryption.js +186 -0
  174. package/dist/serialization/errors.d.ts +34 -0
  175. package/dist/serialization/errors.d.ts.map +1 -0
  176. package/dist/serialization/errors.js +59 -0
  177. package/dist/serialization/format.d.ts +60 -0
  178. package/dist/serialization/format.d.ts.map +1 -0
  179. package/dist/serialization/format.js +97 -0
  180. package/dist/serialization/hardened.d.ts +155 -0
  181. package/dist/serialization/hardened.d.ts.map +1 -0
  182. package/dist/serialization/hardened.js +523 -0
  183. package/dist/serialization/index.d.ts +20 -0
  184. package/dist/serialization/index.d.ts.map +1 -0
  185. package/dist/serialization/index.js +22 -0
  186. package/dist/serialization/reducers/class-vm.d.ts +20 -0
  187. package/dist/serialization/reducers/class-vm.d.ts.map +1 -0
  188. package/dist/serialization/reducers/class-vm.js +77 -0
  189. package/dist/serialization/reducers/class.d.ts +11 -0
  190. package/dist/serialization/reducers/class.d.ts.map +1 -0
  191. package/dist/serialization/reducers/class.js +73 -0
  192. package/dist/serialization/reducers/common-vm.d.ts +15 -0
  193. package/dist/serialization/reducers/common-vm.d.ts.map +1 -0
  194. package/dist/serialization/reducers/common-vm.js +579 -0
  195. package/dist/serialization/reducers/common.d.ts +16 -0
  196. package/dist/serialization/reducers/common.d.ts.map +1 -0
  197. package/dist/serialization/reducers/common.js +478 -0
  198. package/dist/serialization/reducers/step-function-vm.d.ts +44 -0
  199. package/dist/serialization/reducers/step-function-vm.d.ts.map +1 -0
  200. package/dist/serialization/reducers/step-function-vm.js +97 -0
  201. package/dist/serialization/reducers/step-function.d.ts +35 -0
  202. package/dist/serialization/reducers/step-function.d.ts.map +1 -0
  203. package/dist/serialization/reducers/step-function.js +104 -0
  204. package/dist/serialization/step.d.ts +17 -0
  205. package/dist/serialization/step.d.ts.map +1 -0
  206. package/dist/serialization/step.js +48 -0
  207. package/dist/serialization/types.d.ts +273 -0
  208. package/dist/serialization/types.d.ts.map +1 -0
  209. package/dist/serialization/types.js +35 -0
  210. package/dist/serialization/vm-bundle-entry.d.ts +12 -0
  211. package/dist/serialization/vm-bundle-entry.d.ts.map +1 -0
  212. package/dist/serialization/vm-bundle-entry.js +58 -0
  213. package/dist/serialization/workflow-vm.d.ts +24 -0
  214. package/dist/serialization/workflow-vm.d.ts.map +1 -0
  215. package/dist/serialization/workflow-vm.js +66 -0
  216. package/dist/serialization/workflow.d.ts +29 -0
  217. package/dist/serialization/workflow.d.ts.map +1 -0
  218. package/dist/serialization/workflow.js +54 -0
  219. package/dist/serialization-format.d.ts +60 -4
  220. package/dist/serialization-format.d.ts.map +1 -1
  221. package/dist/serialization-format.js +276 -56
  222. package/dist/serialization.d.ts +374 -221
  223. package/dist/serialization.d.ts.map +1 -1
  224. package/dist/serialization.js +2300 -783
  225. package/dist/set-attributes.d.ts +13 -0
  226. package/dist/set-attributes.d.ts.map +1 -0
  227. package/dist/set-attributes.js +60 -0
  228. package/dist/sleep.d.ts.map +1 -1
  229. package/dist/sleep.js +3 -2
  230. package/dist/source-map.d.ts +25 -0
  231. package/dist/source-map.d.ts.map +1 -1
  232. package/dist/source-map.js +148 -10
  233. package/dist/step/context-storage.d.ts +61 -2
  234. package/dist/step/context-storage.d.ts.map +1 -1
  235. package/dist/step/context-storage.js +7 -5
  236. package/dist/step/get-closure-vars.d.ts.map +1 -1
  237. package/dist/step/get-closure-vars.js +3 -2
  238. package/dist/step/get-step-metadata.d.ts.map +1 -1
  239. package/dist/step/get-step-metadata.js +3 -2
  240. package/dist/step/get-workflow-metadata.d.ts.map +1 -1
  241. package/dist/step/get-workflow-metadata.js +3 -2
  242. package/dist/step/writable-stream.d.ts.map +1 -1
  243. package/dist/step/writable-stream.js +71 -7
  244. package/dist/step.d.ts.map +1 -1
  245. package/dist/step.js +226 -30
  246. package/dist/symbols.d.ts +54 -0
  247. package/dist/symbols.d.ts.map +1 -1
  248. package/dist/symbols.js +55 -1
  249. package/dist/telemetry/semantic-conventions.d.ts +242 -2
  250. package/dist/telemetry/semantic-conventions.d.ts.map +1 -1
  251. package/dist/telemetry/semantic-conventions.js +173 -1
  252. package/dist/telemetry.d.ts +73 -0
  253. package/dist/telemetry.d.ts.map +1 -1
  254. package/dist/telemetry.js +153 -16
  255. package/dist/types.d.ts +6 -0
  256. package/dist/types.d.ts.map +1 -1
  257. package/dist/types.js +23 -1
  258. package/dist/util.d.ts +16 -6
  259. package/dist/util.d.ts.map +1 -1
  260. package/dist/util.js +25 -16
  261. package/dist/version.d.ts +1 -1
  262. package/dist/version.d.ts.map +1 -1
  263. package/dist/version.js +2 -2
  264. package/dist/vm/index.d.ts.map +1 -1
  265. package/dist/vm/index.js +77 -9
  266. package/dist/vm/script-cache.d.ts +28 -0
  267. package/dist/vm/script-cache.d.ts.map +1 -0
  268. package/dist/vm/script-cache.js +140 -0
  269. package/dist/workflow/abort-controller.d.ts +65 -0
  270. package/dist/workflow/abort-controller.d.ts.map +1 -0
  271. package/dist/workflow/abort-controller.js +314 -0
  272. package/dist/workflow/attribute-dispatcher.d.ts +6 -0
  273. package/dist/workflow/attribute-dispatcher.d.ts.map +1 -0
  274. package/dist/workflow/attribute-dispatcher.js +48 -0
  275. package/dist/workflow/create-hook.d.ts.map +1 -1
  276. package/dist/workflow/create-hook.js +25 -3
  277. package/dist/workflow/define-hook.d.ts +1 -1
  278. package/dist/workflow/define-hook.d.ts.map +1 -1
  279. package/dist/workflow/define-hook.js +8 -4
  280. package/dist/workflow/get-workflow-metadata.d.ts.map +1 -1
  281. package/dist/workflow/get-workflow-metadata.js +14 -3
  282. package/dist/workflow/hook.d.ts.map +1 -1
  283. package/dist/workflow/hook.js +303 -37
  284. package/dist/workflow/index.d.ts +1 -0
  285. package/dist/workflow/index.d.ts.map +1 -1
  286. package/dist/workflow/index.js +5 -3
  287. package/dist/workflow/set-attributes.d.ts +68 -0
  288. package/dist/workflow/set-attributes.d.ts.map +1 -0
  289. package/dist/workflow/set-attributes.js +60 -0
  290. package/dist/workflow/sleep.d.ts.map +1 -1
  291. package/dist/workflow/sleep.js +58 -9
  292. package/dist/workflow/world-init-stub.d.ts +15 -0
  293. package/dist/workflow/world-init-stub.d.ts.map +1 -0
  294. package/dist/workflow/world-init-stub.js +15 -0
  295. package/dist/workflow.d.ts +69 -3
  296. package/dist/workflow.d.ts.map +1 -1
  297. package/dist/workflow.js +791 -554
  298. package/docs/api-reference/create-hook.mdx +79 -0
  299. package/docs/api-reference/create-webhook.mdx +1 -0
  300. package/docs/api-reference/define-hook.mdx +26 -24
  301. package/docs/api-reference/fatal-error.mdx +29 -7
  302. package/docs/api-reference/fetch.mdx +8 -4
  303. package/docs/api-reference/index.mdx +3 -0
  304. package/docs/api-reference/set-attributes.mdx +61 -0
  305. package/docs/api-reference/sleep.mdx +1 -1
  306. package/docs/foundations/cancellation.mdx +459 -0
  307. package/docs/foundations/errors-and-retries.mdx +7 -3
  308. package/docs/foundations/hooks.mdx +29 -0
  309. package/docs/foundations/idempotency.mdx +243 -11
  310. package/docs/foundations/index.mdx +1 -23
  311. package/docs/foundations/meta.json +3 -1
  312. package/docs/foundations/serialization.mdx +77 -41
  313. package/docs/foundations/starting-workflows.mdx +79 -2
  314. package/docs/foundations/streaming.mdx +14 -23
  315. package/docs/foundations/versioning.mdx +263 -0
  316. package/docs/how-it-works/cancellation.mdx +287 -0
  317. package/docs/how-it-works/code-transform.mdx +21 -17
  318. package/docs/how-it-works/encryption.mdx +5 -5
  319. package/docs/how-it-works/event-sourcing.mdx +6 -6
  320. package/docs/how-it-works/framework-integrations.mdx +96 -337
  321. package/docs/how-it-works/meta.json +2 -1
  322. package/package.json +31 -13
  323. package/dist/runtime/step-handler.d.ts +0 -2
  324. package/dist/runtime/step-handler.d.ts.map +0 -1
  325. package/dist/runtime/step-handler.js +0 -678
package/dist/private.d.ts CHANGED
@@ -1,10 +1,13 @@
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
+ import type { CorrelationIdGenerator } from './correlation-id.js';
5
6
  import type { EventsConsumer } from './events-consumer.js';
6
7
  import type { QueueItem } from './global.js';
8
+ import type { ReplayPayloadCache } from './replay-payload-cache.js';
7
9
  import type { Serializable } from './schemas.js';
10
+ import type { PayloadKey } from './serialization/encryption.js';
8
11
  export type StepFunction<Args extends Serializable[] = any[], Result extends Serializable | unknown = unknown> = ((...args: Args) => Promise<Result>) & {
9
12
  maxRetries?: number;
10
13
  stepId?: string;
@@ -25,8 +28,19 @@ export declare function registerStepFunction(stepId: string, stepFn: StepFunctio
25
28
  export declare function getStepFunction(stepId: string): StepFunction | undefined;
26
29
  export interface WorkflowOrchestratorContext {
27
30
  runId: string;
28
- encryptionKey: CryptoKey | undefined;
31
+ encryptionKey: PayloadKey | undefined;
32
+ worldCapabilities?: WorldCapabilities;
29
33
  globalThis: typeof globalThis;
34
+ /**
35
+ * Increments when a suspension is accepted and on every retained-session
36
+ * resume. STEP suspension signals capture it when scheduled and no-op if
37
+ * it moved (see step.ts) — this drops same-boundary sibling signals and
38
+ * timers queued at boundary N that would fire after the session resumed
39
+ * into boundary N+1. Sleep/hook/attribute signals are intentionally
40
+ * unguarded: their presence makes the boundary unretainable, so a late
41
+ * signal correctly demotes the session (workflow.ts `onWorkflowError`).
42
+ */
43
+ suspensionGeneration: number;
30
44
  eventsConsumer: EventsConsumer;
31
45
  /**
32
46
  * Map of pending invocations keyed by correlationId.
@@ -34,7 +48,12 @@ export interface WorkflowOrchestratorContext {
34
48
  */
35
49
  invocationsQueue: Map<string, QueueItem>;
36
50
  onWorkflowError: (error: Error) => void;
37
- generateUlid: () => string;
51
+ /**
52
+ * Mints a correlation id body for one entity family. Every entity a replay
53
+ * creates draws from here, and the family is what keeps a disagreement about
54
+ * one family's count from renumbering another's.
55
+ */
56
+ generateCorrelationId: CorrelationIdGenerator;
38
57
  generateNanoid: () => string;
39
58
  /**
40
59
  * Sequential promise queue that ensures all event-driven promise resolutions
@@ -46,18 +65,168 @@ export interface WorkflowOrchestratorContext {
46
65
  promiseQueue: Promise<void>;
47
66
  /**
48
67
  * 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.
68
+ * hydration, hook payload hydration, abort signal hydration). Suspensions
69
+ * must wait for this to reach 0 before firing, to avoid preempting data
70
+ * delivery — e.g. dehydrating a step's arguments while an abort that should
71
+ * be reflected in those arguments is still hydrating its reason.
51
72
  */
52
73
  pendingDeliveries: number;
74
+ /**
75
+ * Ordered registry of in-flight "branch-deciding" deliveries — the
76
+ * resolutions a workflow typically `Promise.race`s on, or awaits from
77
+ * independent concurrent branches: hook payloads (`hook_received`), wait
78
+ * completions (`wait_completed`), and step results (`step_completed` /
79
+ * `step_failed`). Keyed by the delivery's position (index) in the consumed
80
+ * event log.
81
+ *
82
+ * The problem: each of these resolutions reaches workflow code after a
83
+ * different, workload-dependent number of microtask hops. A buffered hook
84
+ * payload is observed via the async hook iterator (`yield await this`),
85
+ * costing extra hops; a `wait_completed` resolves with fewer, and a reused
86
+ * sleep can resolve in an entirely earlier loop iteration; a step result is
87
+ * gated on hydration whose cost varies between replays of the SAME
88
+ * invocation — the first replay pays the full decrypt/decompress/revive,
89
+ * while later replays sharing the invocation's `ReplayPayloadCache`
90
+ * memo-hit small primitive results and resolve in one or two hops. Either
91
+ * way, the resolution that the committed event log ordered first can lose a
92
+ * `Promise.race` (or a `useStep` ULID allocation) to a faster- or
93
+ * already-resolved competitor, diverging from the log and surfacing as
94
+ * `CorruptedEventLogError`.
95
+ *
96
+ * The fix is a strict, deterministic delivery order anchored on
97
+ * event-log position: a delivery does not resolve to the workflow until
98
+ * every relevant earlier-in-log delivery has been delivered. Because the
99
+ * gate is "the earlier delivery resolved", not "won a timing race", the
100
+ * outcome is independent of microtask hops, hydration/decryption time,
101
+ * and `Promise.race` argument order. Which earlier kinds a delivery defers
102
+ * behind is spelled out on {@link awaitEarlierDeliveries}.
103
+ *
104
+ * Index is used rather than the `eventId` string because `eventId` is an
105
+ * opaque, world-assigned value not guaranteed to sort in creation order
106
+ * (only the bundled ULID worlds happen to).
107
+ *
108
+ * Optional so older/out-of-tree contexts (and lightweight test harnesses)
109
+ * that do not initialize it degrade gracefully to the previous behavior.
110
+ */
111
+ pendingDeliveryBarriers?: Map<number, DeliveryBarrierEntry>;
112
+ /**
113
+ * Invocation-scoped cache of prepared serialized payloads and immutable final
114
+ * values. Prepared bytes survive fresh replay VMs; object graphs do not.
115
+ */
116
+ replayPayloadCache: ReplayPayloadCache;
117
+ }
118
+ /** The kind of branch-deciding delivery a barrier represents. */
119
+ export type DeliveryKind = 'hook' | 'wait' | 'step';
120
+ interface DeliveryBarrierEntry {
121
+ kind: DeliveryKind;
122
+ /** Resolves once this delivery has resolved to the workflow. */
123
+ delivered: Promise<void>;
124
+ /**
125
+ * Whether this delivery is committed to reaching the workflow without any
126
+ * further action by workflow code. True for wait completions and step
127
+ * results, which always resolve from their own chain, and for a hook payload
128
+ * that already had a waiting consumer when it was consumed.
129
+ *
130
+ * False for a BUFFERED hook payload no consumer has claimed yet: it is
131
+ * delivered by `claim()`, i.e. whenever the workflow next reads the hook —
132
+ * which may be causally *after* a later-in-log delivery. `arm()` flips it
133
+ * once a consumer takes the payload.
134
+ */
135
+ armed: boolean;
136
+ }
137
+ /**
138
+ * Awaits, in strict event-log order, every still-registered delivery that is
139
+ * earlier in the log than `eventIndex` and that a delivery of `kind` defers
140
+ * behind (see {@link DEFER_BEHIND}), so that this resolution is handed to the
141
+ * workflow only after all relevant earlier-in-log deliveries have been. This
142
+ * is what keeps a `Promise.race` — or the ULID a follow-up `useStep` draws on
143
+ * a concurrent branch — deterministic and aligned with the committed event
144
+ * log, independent of microtask-hop counts, hydration time, or race-argument
145
+ * order. When this delivery does have to wait, it also yields a macrotask
146
+ * afterwards so the earlier delivery's consumer can run to its own next
147
+ * suspension point first; see the comment at that `await` for why ordering the
148
+ * `resolve()` calls alone is not enough.
149
+ *
150
+ * One asymmetry: a STEP result additionally skips any earlier delivery that
151
+ * will not resolve on its own, i.e. one blocked (directly or transitively) on
152
+ * a buffered hook payload no consumer has claimed. Such a payload is delivered
153
+ * only when the workflow next reads the hook, and reaching that read very
154
+ * commonly requires the step result itself (`await stepX()` before the read).
155
+ * Gating the step on it would stall the workflow until the barrier's idle
156
+ * safety net fires, which then releases every delivery queued behind that
157
+ * payload at once — losing exactly the race this ordering exists to protect.
158
+ * Waits and hooks keep gating on unclaimed payloads: for them, waiting for the
159
+ * claim IS the ordering guarantee (a `wait_completed` must not preempt a
160
+ * payload the log ordered first).
161
+ */
162
+ export declare function awaitEarlierDeliveries(ctx: WorkflowOrchestratorContext, eventIndex: number | undefined, kind: DeliveryKind): Promise<void>;
163
+ /** Handle for a registered branch-deciding delivery barrier. */
164
+ export interface DeliveryBarrier {
165
+ /**
166
+ * Mark this delivery as delivered to the workflow. Resolves its
167
+ * `delivered` promise so any later-in-log delivery gated on it (via
168
+ * {@link awaitEarlierDeliveries}) may proceed, and removes it from the
169
+ * registry. Idempotent.
170
+ */
171
+ markDelivered: () => void;
172
+ /**
173
+ * Mark this delivery as committed to happening, for a barrier registered
174
+ * unarmed (a buffered hook payload) once a consumer has claimed it. From
175
+ * then on a later step result may be ordered behind it. Idempotent.
176
+ */
177
+ arm: () => void;
53
178
  }
179
+ /**
180
+ * Register a branch-deciding delivery at its event-log index so that later
181
+ * deliveries can be ordered strictly after it. Returns an inert handle when
182
+ * `pendingDeliveryBarriers` is not initialized.
183
+ *
184
+ * Pass `armed: false` for a delivery whose resolution waits on workflow code
185
+ * asking for it (a buffered hook payload); call `arm()` when it does.
186
+ *
187
+ * To guarantee a later delivery gated on this one can never hang when this
188
+ * delivery is abandoned (the workflow took a different branch or is
189
+ * suspending and never observes it), the barrier auto-resolves at idle.
190
+ *
191
+ * INVARIANT required of every call site: a barrier that is ever `armed` must
192
+ * be paired with a delivery chain that runs unconditionally — attached when
193
+ * the event is consumed (waits, step results, waiting-consumer hook payloads,
194
+ * aborts), or by the `claim()` whose invocation is what arms it (buffered
195
+ * hook payloads). The idle check ({@link scheduleWhenIdle}) refuses to
196
+ * observe idle while an armed, self-resolving barrier is undelivered, and the
197
+ * safety net below is itself idle-gated — so an armed barrier with no
198
+ * unconditional chain would livelock every idle check in the run, including
199
+ * its own retirement.
200
+ */
201
+ export declare function registerDeliveryBarrier(ctx: WorkflowOrchestratorContext, eventIndex: number | undefined, kind: DeliveryKind, options?: {
202
+ armed?: boolean;
203
+ }): DeliveryBarrier;
54
204
  /**
55
205
  * Schedule a callback to fire only after all pending data deliveries
56
206
  * (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.
207
+ * Uses a polling loop: setTimeout(0) → check pendingDeliveries and the
208
+ * barrier registry → if anything is still in flight, wait for promiseQueue →
209
+ * repeat. This handles the multi-round delivery pattern where each hook
210
+ * payload delivery cycle appends new async work to the promiseQueue.
211
+ *
212
+ * "In flight" is two distinct windows, each with its own guard:
213
+ * `pendingDeliveries > 0` covers hydration inside the serial queue slots, and
214
+ * {@link hasParkedCommittedDelivery} covers the detached gap between a slot
215
+ * releasing that counter and the delivery's `resolve()` actually running —
216
+ * deliberately outside `pendingDeliveries` (see step.ts), and invisible to it.
217
+ *
218
+ * The initial `setTimeout(0)` macrotask is load-bearing and must NOT be
219
+ * downgraded to a microtask (`queueMicrotask`/`Promise.resolve().then`).
220
+ * `pendingDeliveries` only guards the host-side hydration window; between a
221
+ * delivery's `resolve()` and the workflow VM body running its continuation to
222
+ * register the next subscriber, `pendingDeliveries` is already 0 even though
223
+ * the VM is mid-reaction. Node does not guarantee a microtask scheduled in
224
+ * the host context settles after the cross-VM promise chain (resolve in host
225
+ * → workflow code in VM → subscribe back in host); the macrotask boundary
226
+ * gives that chain time to run, so the suspension does not preempt a sibling
227
+ * delivery still in flight. Empirically, replacing it with `queueMicrotask`
228
+ * breaks hook/sleep `Promise.race` ordering (CorruptedEventLogError).
61
229
  */
62
230
  export declare function scheduleWhenIdle(ctx: WorkflowOrchestratorContext, fn: () => void): void;
231
+ export {};
63
232
  //# 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,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AACjD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAC3D,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAC7C,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAEjD,MAAM,MAAM,YAAY,CACtB,IAAI,SAAS,YAAY,EAAE,GAAG,GAAG,EAAE,EACnC,MAAM,SAAS,YAAY,GAAG,OAAO,GAAG,OAAO,IAC7C,CAAC,CAAC,GAAG,IAAI,EAAE,IAAI,KAAK,OAAO,CAAC,MAAM,CAAC,CAAC,GAAG;IACzC,UAAU,CAAC,EAAE,MAAM,CAAC;I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