@demlik/tea 0.1.0 → 0.1.2

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 (243) hide show
  1. package/LICENSE +21 -0
  2. package/dist/agent/index.d.ts +907 -0
  3. package/dist/agent/index.js +20 -0
  4. package/dist/agent/index.js.map +1 -0
  5. package/dist/authed-call/index.d.ts +119 -0
  6. package/dist/authed-call/index.js +148 -0
  7. package/dist/authed-call/index.js.map +1 -0
  8. package/dist/await-terminal/index.d.ts +99 -0
  9. package/dist/await-terminal/index.js +75 -0
  10. package/dist/await-terminal/index.js.map +1 -0
  11. package/dist/batch-window/index.d.ts +259 -0
  12. package/dist/batch-window/index.js +43 -0
  13. package/dist/batch-window/index.js.map +1 -0
  14. package/dist/cache/index.d.ts +2 -2
  15. package/dist/cache/index.js +2 -50
  16. package/dist/cache/index.js.map +1 -1
  17. package/dist/chunk-27N2SPTN.js +222 -0
  18. package/dist/chunk-27N2SPTN.js.map +1 -0
  19. package/dist/chunk-2YOE5PIS.js +45 -0
  20. package/dist/chunk-2YOE5PIS.js.map +1 -0
  21. package/dist/chunk-3545BOYL.js +65 -0
  22. package/dist/chunk-3545BOYL.js.map +1 -0
  23. package/dist/chunk-3XJWC7VW.js +61 -0
  24. package/dist/chunk-3XJWC7VW.js.map +1 -0
  25. package/dist/chunk-5DEQ5I3U.js +132 -0
  26. package/dist/chunk-5DEQ5I3U.js.map +1 -0
  27. package/dist/chunk-5VNWZADP.js +49 -0
  28. package/dist/chunk-5VNWZADP.js.map +1 -0
  29. package/dist/chunk-6RGBKODV.js +268 -0
  30. package/dist/chunk-6RGBKODV.js.map +1 -0
  31. package/dist/{chunk-ZRUST6BK.js → chunk-7FYQGDMF.js} +2 -2
  32. package/dist/chunk-7FYQGDMF.js.map +1 -0
  33. package/dist/chunk-7W4LMPUW.js +186 -0
  34. package/dist/chunk-7W4LMPUW.js.map +1 -0
  35. package/dist/chunk-BYQUGDD3.js +16 -0
  36. package/dist/chunk-BYQUGDD3.js.map +1 -0
  37. package/dist/{chunk-PYBQREFU.js → chunk-DILMKOZ7.js} +22 -26
  38. package/dist/chunk-DILMKOZ7.js.map +1 -0
  39. package/dist/chunk-EHIX7NYZ.js +93 -0
  40. package/dist/chunk-EHIX7NYZ.js.map +1 -0
  41. package/dist/chunk-HCEVHFJ7.js +54 -0
  42. package/dist/chunk-HCEVHFJ7.js.map +1 -0
  43. package/dist/chunk-HJO6VNQI.js +15 -0
  44. package/dist/chunk-HJO6VNQI.js.map +1 -0
  45. package/dist/chunk-IN33Q4VJ.js +52 -0
  46. package/dist/chunk-IN33Q4VJ.js.map +1 -0
  47. package/dist/chunk-J6527VRQ.js +56 -0
  48. package/dist/chunk-J6527VRQ.js.map +1 -0
  49. package/dist/chunk-JELSZN25.js +47 -0
  50. package/dist/chunk-JELSZN25.js.map +1 -0
  51. package/dist/chunk-K575PT4S.js +58 -0
  52. package/dist/chunk-K575PT4S.js.map +1 -0
  53. package/dist/chunk-K7Q23EQ4.js +20 -0
  54. package/dist/chunk-K7Q23EQ4.js.map +1 -0
  55. package/dist/chunk-LEC4U6CX.js +91 -0
  56. package/dist/chunk-LEC4U6CX.js.map +1 -0
  57. package/dist/{chunk-PYOCUBQJ.js → chunk-LP7VQBRG.js} +22 -15
  58. package/dist/chunk-LP7VQBRG.js.map +1 -0
  59. package/dist/{chunk-CMG2KMWX.js → chunk-MMH7CW5K.js} +220 -189
  60. package/dist/chunk-MMH7CW5K.js.map +1 -0
  61. package/dist/{chunk-Y5JM3UUH.js → chunk-NSDFA6M6.js} +75 -3
  62. package/dist/chunk-NSDFA6M6.js.map +1 -0
  63. package/dist/chunk-ON24RKSE.js +60 -0
  64. package/dist/chunk-ON24RKSE.js.map +1 -0
  65. package/dist/chunk-PS23H6KQ.js +87 -0
  66. package/dist/chunk-PS23H6KQ.js.map +1 -0
  67. package/dist/chunk-QLA76GIT.js +162 -0
  68. package/dist/chunk-QLA76GIT.js.map +1 -0
  69. package/dist/chunk-S3FQN4SL.js +506 -0
  70. package/dist/chunk-S3FQN4SL.js.map +1 -0
  71. package/dist/chunk-SC3FAFZN.js +53 -0
  72. package/dist/chunk-SC3FAFZN.js.map +1 -0
  73. package/dist/chunk-SINXE47Y.js +55 -0
  74. package/dist/chunk-SINXE47Y.js.map +1 -0
  75. package/dist/chunk-SQTCS44W.js +33 -0
  76. package/dist/chunk-SQTCS44W.js.map +1 -0
  77. package/dist/chunk-TGG26XMK.js +52 -0
  78. package/dist/chunk-TGG26XMK.js.map +1 -0
  79. package/dist/chunk-UV6UWGEO.js +147 -0
  80. package/dist/chunk-UV6UWGEO.js.map +1 -0
  81. package/dist/chunk-UYWUVEP6.js +158 -0
  82. package/dist/chunk-UYWUVEP6.js.map +1 -0
  83. package/dist/chunk-VLGARN6H.js +202 -0
  84. package/dist/chunk-VLGARN6H.js.map +1 -0
  85. package/dist/circuit-breaker/index.js +1 -51
  86. package/dist/circuit-breaker/index.js.map +1 -1
  87. package/dist/core-BoB6t7dB.d.ts +188 -0
  88. package/dist/deadline/index.d.ts +24 -5
  89. package/dist/deadline/index.js +4 -18
  90. package/dist/deadline/index.js.map +1 -1
  91. package/dist/debounce/index.d.ts +6 -0
  92. package/dist/debounce/index.js +1 -54
  93. package/dist/debounce/index.js.map +1 -1
  94. package/dist/devtools/index.d.ts +1 -1
  95. package/dist/devtools/index.js +18 -6
  96. package/dist/devtools/index.js.map +1 -1
  97. package/dist/do/index.d.ts +1723 -92
  98. package/dist/do/index.js +673 -31
  99. package/dist/do/index.js.map +1 -1
  100. package/dist/durable-effects-CO9Uh971.d.ts +180 -0
  101. package/dist/extension/index.d.ts +31 -13
  102. package/dist/extension/index.js +5 -3
  103. package/dist/extension/index.js.map +1 -1
  104. package/dist/extension/react.js +8 -3
  105. package/dist/extension/react.js.map +1 -1
  106. package/dist/extension/subs/index.d.ts +2 -2
  107. package/dist/extension/subs/index.js.map +1 -1
  108. package/dist/extension/test-utils.js +11 -3
  109. package/dist/extension/test-utils.js.map +1 -1
  110. package/dist/fan-out/index.d.ts +239 -0
  111. package/dist/fan-out/index.js +5 -0
  112. package/dist/fan-out/index.js.map +1 -0
  113. package/dist/idempotency/adapter.d.ts +62 -0
  114. package/dist/idempotency/adapter.js +4 -0
  115. package/dist/idempotency/adapter.js.map +1 -0
  116. package/dist/idempotency/index.js +1 -56
  117. package/dist/idempotency/index.js.map +1 -1
  118. package/dist/idempotent-intake/index.d.ts +186 -0
  119. package/dist/idempotent-intake/index.js +7 -0
  120. package/dist/idempotent-intake/index.js.map +1 -0
  121. package/dist/index.d.ts +341 -191
  122. package/dist/index.js +2 -1
  123. package/dist/llm-call/index.d.ts +309 -0
  124. package/dist/llm-call/index.js +14 -0
  125. package/dist/llm-call/index.js.map +1 -0
  126. package/dist/machine-viz/index.d.ts +4 -4
  127. package/dist/machine-viz/index.js +5 -9
  128. package/dist/machine-viz/index.js.map +1 -1
  129. package/dist/mem/index.d.ts +1 -0
  130. package/dist/monitored-run/index.d.ts +253 -0
  131. package/dist/monitored-run/index.js +10 -0
  132. package/dist/monitored-run/index.js.map +1 -0
  133. package/dist/node/index.d.ts +5 -5
  134. package/dist/node/index.js.map +1 -1
  135. package/dist/paginated-walk/index.d.ts +234 -0
  136. package/dist/paginated-walk/index.js +15 -0
  137. package/dist/paginated-walk/index.js.map +1 -0
  138. package/dist/paginator/index.d.ts +271 -0
  139. package/dist/paginator/index.js +3 -0
  140. package/dist/paginator/index.js.map +1 -0
  141. package/dist/parity/index.d.ts +92 -0
  142. package/dist/parity/index.js +78 -0
  143. package/dist/parity/index.js.map +1 -0
  144. package/dist/pbt/arbitraries/index.d.ts +1 -1
  145. package/dist/pbt/arbitraries/index.js +1 -1
  146. package/dist/pbt/index.d.ts +15 -8
  147. package/dist/pbt/index.js +6 -11
  148. package/dist/pbt/index.js.map +1 -1
  149. package/dist/pbt/runners/index.d.ts +1 -1
  150. package/dist/pbt/runners/index.js +3 -2
  151. package/dist/poller/index.d.ts +288 -0
  152. package/dist/poller/index.js +79 -0
  153. package/dist/poller/index.js.map +1 -0
  154. package/dist/prediction/index.d.ts +130 -0
  155. package/dist/prediction/index.js +4 -0
  156. package/dist/prediction/index.js.map +1 -0
  157. package/dist/protocol-B5YmJr0d.d.ts +45 -0
  158. package/dist/pure/index.d.ts +2 -0
  159. package/dist/pure/index.js +4 -0
  160. package/dist/pure/index.js.map +1 -0
  161. package/dist/raft/demo.d.ts +105 -0
  162. package/dist/raft/demo.js +172 -0
  163. package/dist/raft/demo.js.map +1 -0
  164. package/dist/raft/index.d.ts +548 -0
  165. package/dist/raft/index.js +7 -0
  166. package/dist/raft/index.js.map +1 -0
  167. package/dist/raft/sim.d.ts +222 -0
  168. package/dist/raft/sim.js +8 -0
  169. package/dist/raft/sim.js.map +1 -0
  170. package/dist/raft/timeline.d.ts +46 -0
  171. package/dist/raft/timeline.js +62 -0
  172. package/dist/raft/timeline.js.map +1 -0
  173. package/dist/rate-limit/index.js +1 -43
  174. package/dist/rate-limit/index.js.map +1 -1
  175. package/dist/react/index.d.ts +2 -1
  176. package/dist/react/index.js +43 -20
  177. package/dist/react/index.js.map +1 -1
  178. package/dist/reconciler/index.d.ts +192 -0
  179. package/dist/reconciler/index.js +166 -0
  180. package/dist/reconciler/index.js.map +1 -0
  181. package/dist/recorder/index.d.ts +7 -3
  182. package/dist/recorder/index.js +1 -155
  183. package/dist/recorder/index.js.map +1 -1
  184. package/dist/resilient-call/index.d.ts +294 -0
  185. package/dist/resilient-call/index.js +13 -0
  186. package/dist/resilient-call/index.js.map +1 -0
  187. package/dist/retry-backoff/index.d.ts +42 -5
  188. package/dist/retry-backoff/index.js +1 -35
  189. package/dist/retry-backoff/index.js.map +1 -1
  190. package/dist/retry-to-success/index.d.ts +109 -0
  191. package/dist/retry-to-success/index.js +35 -0
  192. package/dist/retry-to-success/index.js.map +1 -0
  193. package/dist/saga/index.d.ts +238 -0
  194. package/dist/saga/index.js +150 -0
  195. package/dist/saga/index.js.map +1 -0
  196. package/dist/snapshot/index.d.ts +334 -0
  197. package/dist/snapshot/index.js +5 -0
  198. package/dist/snapshot/index.js.map +1 -0
  199. package/dist/subs/index.d.ts +48 -10
  200. package/dist/subs/index.js +1 -1
  201. package/dist/testing/index.d.ts +107 -2
  202. package/dist/testing/index.js +204 -6
  203. package/dist/testing/index.js.map +1 -1
  204. package/dist/throttle/index.d.ts +9 -0
  205. package/dist/throttle/index.js +1 -52
  206. package/dist/throttle/index.js.map +1 -1
  207. package/dist/throttled-input/index.d.ts +380 -0
  208. package/dist/throttled-input/index.js +88 -0
  209. package/dist/throttled-input/index.js.map +1 -0
  210. package/dist/token-refresh/index.d.ts +215 -0
  211. package/dist/token-refresh/index.js +6 -0
  212. package/dist/token-refresh/index.js.map +1 -0
  213. package/dist/trace-replay/index.d.ts +11 -2
  214. package/dist/trace-replay/index.js +3 -55
  215. package/dist/trace-replay/index.js.map +1 -1
  216. package/dist/{types-CwBC5wMn.d.ts → types-B-8bcAp8.d.ts} +1 -1
  217. package/dist/with-deadline/index.d.ts +196 -0
  218. package/dist/with-deadline/index.js +111 -0
  219. package/dist/with-deadline/index.js.map +1 -0
  220. package/dist/with-resilience/index.d.ts +205 -0
  221. package/dist/with-resilience/index.js +251 -0
  222. package/dist/with-resilience/index.js.map +1 -0
  223. package/dist/with-telemetry/index.d.ts +149 -0
  224. package/dist/with-telemetry/index.js +70 -0
  225. package/dist/with-telemetry/index.js.map +1 -0
  226. package/dist/work-queue/adapter.d.ts +82 -0
  227. package/dist/work-queue/adapter.js +4 -0
  228. package/dist/work-queue/adapter.js.map +1 -0
  229. package/dist/work-queue/index.d.ts +25 -11
  230. package/dist/work-queue/index.js +1 -46
  231. package/dist/work-queue/index.js.map +1 -1
  232. package/dist/work-queue/ops.d.ts +69 -0
  233. package/dist/work-queue/ops.js +3 -0
  234. package/dist/work-queue/ops.js.map +1 -0
  235. package/dist/workflow/index.d.ts +450 -0
  236. package/dist/workflow/index.js +225 -0
  237. package/dist/workflow/index.js.map +1 -0
  238. package/package.json +168 -25
  239. package/dist/chunk-CMG2KMWX.js.map +0 -1
  240. package/dist/chunk-PYBQREFU.js.map +0 -1
  241. package/dist/chunk-PYOCUBQJ.js.map +0 -1
  242. package/dist/chunk-Y5JM3UUH.js.map +0 -1
  243. package/dist/chunk-ZRUST6BK.js.map +0 -1
@@ -0,0 +1,450 @@
1
+ import { D as DeliveryId, E as EffectLedgerEvent } from '../durable-effects-CO9Uh971.js';
2
+ export { a as EffectConfirmed, b as EffectOwed, e as emptyLedger } from '../durable-effects-CO9Uh971.js';
3
+ import { C as Cmd } from '../core-BoB6t7dB.js';
4
+
5
+ /**
6
+ * @demlik/tea/workflow — the durable-workflow runtime core (#124, the first
7
+ * Phase-1 slice of the Temporal-style durable-workflow engine, epic #118).
8
+ *
9
+ * A workflow is a long-running, multi-step transaction whose every step (an
10
+ * **activity**) is a side effect that may evict the actor mid-flight. The
11
+ * substrate already solves "a state survives eviction" (the event-sourced
12
+ * store) and "an owed effect survives eviction" (the `do/` pending-effects
13
+ * ledger, #67). This module composes those two into the third primitive: a
14
+ * **pure TEA reducer over a workflow's progress**, where each step is dispatched
15
+ * as a durable owed effect and the next step is decided only after the previous
16
+ * one's result is folded back in. That is exactly Temporal's contract — a
17
+ * deterministic workflow function whose activities are the only impurity, so a
18
+ * replay of the event log re-decides every step identically.
19
+ *
20
+ * The mapping onto TEA, mirroring `../raft`:
21
+ *
22
+ * - **Reducer** — {@link createWorkflow}'s verbs (`init`, `onActivityOk`,
23
+ * `onActivityErr`) are the transition function over {@link WorkflowState}.
24
+ * PURE: they read no clock and no RNG. Wall time, randomness, retries — all
25
+ * impurity lives in the consumer's injected *interpret cell* (the thing that
26
+ * actually performs the activity `Cmd` and dispatches its result `Msg`).
27
+ * A workflow driven by a recorded result schedule re-decides bit-for-bit —
28
+ * the headline replay-determinism payoff (acceptance criterion 4).
29
+ * - **Cmd** — the activity dispatch is an {@link ActivityCmd}, recorded on the
30
+ * #67 ledger as an `effect_owed` BEFORE it is performed and `effect_confirmed`
31
+ * on its result. This module emits the Cmd literals; it performs NO side
32
+ * effects. The owed/confirmed events are persisted into the same log the
33
+ * reducer folds, so the ledger (and thus "which activity is still owed")
34
+ * rebuilds for free on cold wake — re-emit-on-wake is idempotent by the
35
+ * ledger's monotonic delivery id (acceptance criterion 3).
36
+ * - **Msg** — an activity result (`activity_ok` / `activity_err`) is a `Msg`
37
+ * the consumer routes back into a verb. The verb folds the result, records
38
+ * the completed step, and advances to the next activity (or to `completed`
39
+ * when the sequence is exhausted).
40
+ *
41
+ * **Invalid states unrepresentable.** The in-flight activity (`current`, with
42
+ * its delivery id) lives ONLY on the `running` variant of {@link WorkflowState}.
43
+ * A `completed` workflow carries its final output and no current activity; a
44
+ * `failed` one carries its failure and no current activity. So "the current
45
+ * activity exists only while running" is a type-level guarantee, not a runtime
46
+ * convention (acceptance criterion 1).
47
+ *
48
+ * **Boundary with `@demlik/tea/saga` — the compensation test.** Both drive an
49
+ * ordered multi-step transaction with reverse-order compensation (#125 here);
50
+ * the line between them is whether every step carries a true inverse. Here
51
+ * {@link WorkflowStep.compensation} is OPTIONAL — an irreversible step (a sent
52
+ * email) is simply SKIPPED during the unwind, and the run still reaches a
53
+ * terminal state. Saga's `SagaStep<D, U>` REQUIRES both `do` and `undo` — a
54
+ * step without its inverse is unrepresentable, so full reversibility is a
55
+ * type-level guarantee. Pick by the test: **every step must be reversible →
56
+ * saga; some steps are irreversible but the run must finish → workflow.**
57
+ * Cross to saga when you want the type system to enforce that every step can
58
+ * be undone; stay here when the transaction includes steps that can't be.
59
+ *
60
+ * This module is the pure reducer only — like `../raft/index.ts` is the pure
61
+ * Raft reducer with the DO grain deferred to its `./do` sibling (here #126).
62
+ *
63
+ * Generic over three opaque payloads, like raft's command generic: the activity
64
+ * descriptor `A` (what a step asks to do — the consumer's interpret cell knows
65
+ * how to perform it), the activity result `R`, and the failure `F`. The reducer
66
+ * never inspects them; it only sequences them.
67
+ *
68
+ * NOT a substrate primitive: it depends only on the core `Cmd` type and the
69
+ * sibling `../do` durable-effects ledger. Consumers reach it via the
70
+ * `@demlik/tea/workflow` subpath.
71
+ */
72
+
73
+ /**
74
+ * One step of a workflow: a named activity descriptor. `A` is the opaque
75
+ * payload the consumer's interpret cell knows how to perform (an HTTP call, a
76
+ * sibling-grain message, a tool round-trip). The reducer never inspects
77
+ * `activity` — it only sequences the steps and correlates results.
78
+ *
79
+ * A step may declare a **compensation** — the inverse activity that undoes the
80
+ * forward one (cancel the reservation, refund the charge). Compensation is the
81
+ * `{ do, undo }` pair of `@demlik/tea/saga` mapped onto the workflow: the step's
82
+ * `activity` is the `do`, its `compensation` is the `undo`. The pair is the unit
83
+ * of reversibility — a step that completed (its forward activity succeeded) is
84
+ * exactly a step whose `compensation` must run, in reverse order, if a LATER
85
+ * step fails (#125). A step WITHOUT a compensation contributes nothing to the
86
+ * unwind: it is skipped during the reverse walk (an irreversible step — a sent
87
+ * email — that the saga model treats as a no-op `undo`).
88
+ */
89
+ interface WorkflowStep<A> {
90
+ /** Stable, human-readable step name — appears in the completed-step record. */
91
+ readonly name: string;
92
+ /** The opaque activity this step performs. Interpreted by the consumer. */
93
+ readonly activity: A;
94
+ /**
95
+ * The opaque compensating (inverse) activity, performed in reverse order on a
96
+ * downstream failure (#125). Optional: a step with no compensation is skipped
97
+ * during the unwind. Interpreted by the consumer's interpret cell, exactly
98
+ * like {@link activity}.
99
+ */
100
+ readonly compensation?: A;
101
+ }
102
+ /**
103
+ * A completed step: the step that ran plus the result its activity produced.
104
+ * The `running` state accumulates these in execution order, so the full
105
+ * forward history is available to the consumer (and to #125's compensation,
106
+ * which will walk it in reverse).
107
+ */
108
+ interface CompletedStep<A, R> {
109
+ readonly step: WorkflowStep<A>;
110
+ readonly result: R;
111
+ }
112
+ /** The activity currently in flight on a `running` workflow. */
113
+ interface InFlightActivity<A> {
114
+ /** The 0-based index of this step in the workflow's step sequence. */
115
+ readonly index: number;
116
+ /** The step whose activity is in flight. */
117
+ readonly step: WorkflowStep<A>;
118
+ /** The #67 ledger delivery id this activity was owed under (the dedup key). */
119
+ readonly id: DeliveryId;
120
+ }
121
+ /**
122
+ * The compensation currently in flight on a `compensating` workflow (#125). The
123
+ * mirror of {@link InFlightActivity} for the reverse walk: exactly one
124
+ * compensation is owed at a time, on the SAME #67 ledger, with its own monotonic
125
+ * delivery id. `index` is the index (into `completed`/`steps`) of the step being
126
+ * undone — the unwind walks these from high to low (strict reverse order).
127
+ */
128
+ interface InFlightCompensation<A> {
129
+ /** The 0-based index (into the step sequence) of the step being compensated. */
130
+ readonly index: number;
131
+ /** The step whose compensation is in flight. */
132
+ readonly step: WorkflowStep<A>;
133
+ /** The #67 ledger delivery id this compensation was owed under (the dedup key). */
134
+ readonly id: DeliveryId;
135
+ }
136
+ /**
137
+ * A workflow in progress. Carries the ordered `completed` steps (forward
138
+ * history), the `steps` it is sequencing, and the `current` in-flight activity
139
+ * (with its ledger delivery id). `completed.length === current.index` always
140
+ * holds — the current activity is the next step after the last completed one.
141
+ */
142
+ interface RunningWorkflow<A, R> {
143
+ readonly status: "running";
144
+ /** The full, static step sequence this workflow runs. */
145
+ readonly steps: readonly WorkflowStep<A>[];
146
+ /** Steps that have produced a result, in execution order. */
147
+ readonly completed: readonly CompletedStep<A, R>[];
148
+ /** The single activity in flight right now. */
149
+ readonly current: InFlightActivity<A>;
150
+ }
151
+ /**
152
+ * A workflow that ran every step to completion. `output` is the result of the
153
+ * final step — the workflow's overall output. No activity is in flight.
154
+ */
155
+ interface CompletedWorkflow<A, R> {
156
+ readonly status: "completed";
157
+ readonly steps: readonly WorkflowStep<A>[];
158
+ /** Every step, in execution order, with its result. */
159
+ readonly completed: readonly CompletedStep<A, R>[];
160
+ /** The final step's result — the workflow's output. */
161
+ readonly output: R;
162
+ }
163
+ /**
164
+ * A workflow that failed on an activity with NOTHING to compensate — the
165
+ * forward failure happened with zero completed steps (the first activity
166
+ * failed). `failedStep` is the step whose activity reported the failure;
167
+ * `failure` is the opaque failure payload. `completed` is empty by construction:
168
+ * a failure WITH completed steps pivots into {@link CompensatingWorkflow}
169
+ * instead, so `failed` is precisely the empty-rollback terminal (the analogue
170
+ * of saga's `aborted` reached straight from the first step). No activity is in
171
+ * flight; this is terminal (#125 acceptance criterion 3).
172
+ */
173
+ interface FailedWorkflow<A, R, F> {
174
+ readonly status: "failed";
175
+ readonly steps: readonly WorkflowStep<A>[];
176
+ /** Empty by construction — a failure with completed steps compensates instead. */
177
+ readonly completed: readonly CompletedStep<A, R>[];
178
+ /** The step whose activity failed. */
179
+ readonly failedStep: WorkflowStep<A>;
180
+ /** The opaque failure the activity reported. */
181
+ readonly failure: F;
182
+ }
183
+ /**
184
+ * A workflow unwinding after a forward failure (#125): the compensations of the
185
+ * `completed` steps are being emitted in STRICT REVERSE order, one at a time, on
186
+ * the same #67 ledger. The analogue of saga's `compensating` phase.
187
+ *
188
+ * `current` is the single compensation in flight (the mirror of `running`'s
189
+ * in-flight activity). `compensated` records, in unwind order, the completed
190
+ * steps whose compensation has confirmed — the reverse-walk's progress log, so
191
+ * a cold wake resumes mid-rollback exactly. `failedStep`/`failure` carry the
192
+ * forward failure that triggered the unwind (verbatim, never interpreted),
193
+ * preserved for the consumer to read and to fold onto the terminal state.
194
+ *
195
+ * Invariant: the unwind walks `completed` from its tail toward index 0, skipping
196
+ * steps with no compensation, so `current.index` is strictly below the index of
197
+ * every entry in `compensated`.
198
+ */
199
+ interface CompensatingWorkflow<A, R, F> {
200
+ readonly status: "compensating";
201
+ readonly steps: readonly WorkflowStep<A>[];
202
+ /** The completed forward steps, in execution order — the history being unwound. */
203
+ readonly completed: readonly CompletedStep<A, R>[];
204
+ /** The step whose forward activity failed, triggering the unwind. */
205
+ readonly failedStep: WorkflowStep<A>;
206
+ /** The opaque forward failure that triggered the unwind. Carried, never interpreted. */
207
+ readonly failure: F;
208
+ /** The single compensation in flight right now. */
209
+ readonly current: InFlightCompensation<A>;
210
+ /** Completed steps whose compensation has confirmed, in unwind (reverse) order. */
211
+ readonly compensated: readonly CompletedStep<A, R>[];
212
+ }
213
+ /**
214
+ * A workflow that failed forward and then fully unwound: every completed step's
215
+ * compensation confirmed, in reverse order. Terminal. The analogue of saga's
216
+ * `aborted` (fully unwound) — named `failed_compensated` to align with #125's
217
+ * vocabulary. No activity or compensation is in flight.
218
+ */
219
+ interface FailedCompensatedWorkflow<A, R, F> {
220
+ readonly status: "failed_compensated";
221
+ readonly steps: readonly WorkflowStep<A>[];
222
+ /** The forward steps that completed (and were each compensated), in execution order. */
223
+ readonly completed: readonly CompletedStep<A, R>[];
224
+ /** The step whose forward activity failed, triggering the (now-finished) unwind. */
225
+ readonly failedStep: WorkflowStep<A>;
226
+ /** The opaque forward failure that triggered compensation. Carried, never interpreted. */
227
+ readonly failure: F;
228
+ }
229
+ /**
230
+ * A workflow whose ROLLBACK itself failed: a compensation activity reported a
231
+ * failure mid-unwind. Terminal, PARTIALLY unwound — the analogue of saga's
232
+ * `compensation_failed`. A failed compensation is a real-world fact (a refund
233
+ * that bounced, a hold that won't release) that must become visible terminal
234
+ * state, never a workflow wedged in `compensating` forever.
235
+ *
236
+ * `compensated` is left intact (the steps already rolled back); `uncompensated`
237
+ * is the step whose compensation bounced PLUS the steps still owed a
238
+ * compensation behind it — exactly what the consumer must reconcile by hand.
239
+ * `compensationFailure` is the rollback failure (distinct from `failure`, the
240
+ * forward failure that started the unwind), carried verbatim.
241
+ */
242
+ interface CompensationFailedWorkflow<A, R, F> {
243
+ readonly status: "compensation_failed";
244
+ readonly steps: readonly WorkflowStep<A>[];
245
+ /** The full completed-forward history, in execution order. */
246
+ readonly completed: readonly CompletedStep<A, R>[];
247
+ /** The step whose forward activity failed, triggering the unwind. */
248
+ readonly failedStep: WorkflowStep<A>;
249
+ /** The opaque forward failure that triggered compensation. Carried, never interpreted. */
250
+ readonly failure: F;
251
+ /** Completed steps successfully compensated before the rollback broke, in unwind order. */
252
+ readonly compensated: readonly CompletedStep<A, R>[];
253
+ /** The step whose compensation bounced. Its forward effect stands un-reversed. */
254
+ readonly failedCompensationStep: WorkflowStep<A>;
255
+ /** The opaque rollback failure (distinct from {@link failure}). Carried, never interpreted. */
256
+ readonly compensationFailure: F;
257
+ }
258
+ /**
259
+ * The workflow's state — a discriminated union on `status`. The current
260
+ * in-flight activity is reachable ONLY through `running`, and the in-flight
261
+ * compensation ONLY through `compensating`, making "an in-flight effect exists
262
+ * only while the workflow is actively driving one" unrepresentable to violate.
263
+ */
264
+ type WorkflowState<A, R, F> = RunningWorkflow<A, R> | CompletedWorkflow<A, R> | FailedWorkflow<A, R, F> | CompensatingWorkflow<A, R, F> | FailedCompensatedWorkflow<A, R, F> | CompensationFailedWorkflow<A, R, F>;
265
+ /**
266
+ * Dispatch the in-flight activity. Routed through the consumer's `interpret`
267
+ * over an injected activity port. The reducer emits this literal; it performs
268
+ * no side effect. The result MUST be dispatched back as a Msg carrying `id`.
269
+ */
270
+ interface ActivityCmd<A> extends Cmd<"workflow_activity"> {
271
+ /** The 0-based step index this activity belongs to. */
272
+ readonly index: number;
273
+ /** The #67 delivery id the result Msg must echo (the dedup key). */
274
+ readonly id: DeliveryId;
275
+ /** The opaque activity to perform. */
276
+ readonly activity: A;
277
+ }
278
+ /**
279
+ * Dispatch a compensation (#125). Same shape + ledger discipline as
280
+ * {@link ActivityCmd} — a compensation is just another durable owed effect on
281
+ * the same #67 ledger, with its own delivery id — but the consumer's interpret
282
+ * cell performs the inverse `compensation` payload, and the result is dispatched
283
+ * back as a {@link CompensationOk}/{@link CompensationErr} carrying the SAME id.
284
+ * Distinguished by `type` so the consumer routes forward vs. inverse work to the
285
+ * right port, and the re-emit-on-wake stays type-correct.
286
+ */
287
+ interface CompensationCmd<A> extends Cmd<"workflow_compensation"> {
288
+ /** The 0-based step index whose compensation this is. */
289
+ readonly index: number;
290
+ /** The #67 delivery id the result Msg must echo (the dedup key). */
291
+ readonly id: DeliveryId;
292
+ /** The opaque compensating (inverse) activity to perform. */
293
+ readonly compensation: A;
294
+ }
295
+ /** The Cmd union this module emits: forward activity dispatches AND (#125)
296
+ * reverse compensation dispatches. The owed/confirmed ledger events are not
297
+ * `WorkflowCmd`s — they are Msg variants persisted into the event log (see
298
+ * {@link EffectLedgerEvent}); the {@link WorkflowStep0} tuple carries them out
299
+ * alongside the dispatch Cmd so the host persists owed-before-dispatch. Both
300
+ * Cmd kinds are owed effects on the same ledger, so `survivingActivities`
301
+ * re-emits either kind on cold wake. */
302
+ type WorkflowCmd<A> = ActivityCmd<A> | CompensationCmd<A>;
303
+ /** An activity succeeded: `id` echoes the {@link ActivityCmd} it answers; the
304
+ * reducer matches it against `current.id`, records the completed step, and
305
+ * advances. */
306
+ interface ActivityOk<R> {
307
+ readonly type: "activity_ok";
308
+ readonly id: DeliveryId;
309
+ readonly result: R;
310
+ }
311
+ /** An activity failed (retries already exhausted by the consumer's interpret
312
+ * cell — this module does not retry). `id` echoes the {@link ActivityCmd};
313
+ * the reducer begins compensation (or settles `failed` when nothing has
314
+ * completed). */
315
+ interface ActivityErr<F> {
316
+ readonly type: "activity_err";
317
+ readonly id: DeliveryId;
318
+ readonly failure: F;
319
+ }
320
+ /** A compensation succeeded (#125): the inverse activity took. `id` echoes the
321
+ * {@link CompensationCmd} it answers; the reducer matches it against the
322
+ * in-flight compensation's id, records the step as compensated, and either
323
+ * emits the next compensation (continuing in reverse) or settles
324
+ * `failed_compensated` when the unwind is complete. A compensation produces no
325
+ * result the workflow carries — its only signal is "the undo took". */
326
+ interface CompensationOk {
327
+ readonly type: "compensation_ok";
328
+ readonly id: DeliveryId;
329
+ }
330
+ /** A compensation itself failed (#125): the inverse activity bounced (a refund
331
+ * that won't go through). `id` echoes the {@link CompensationCmd}; the reducer
332
+ * halts the rollback at the terminal `compensation_failed` state — never wedged
333
+ * in `compensating` forever. */
334
+ interface CompensationErr<F> {
335
+ readonly type: "compensation_err";
336
+ readonly id: DeliveryId;
337
+ readonly failure: F;
338
+ }
339
+ /** The Msg union the reducer folds: forward activity results AND (#125) reverse
340
+ * compensation results. */
341
+ type WorkflowMsg<R, F> = ActivityOk<R> | ActivityErr<F> | CompensationOk | CompensationErr<F>;
342
+ /** A workflow reducer step: the next state, the ledger events to persist
343
+ * (owed-before-dispatch), and the activity Cmds to dispatch. */
344
+ interface WorkflowStep0<A, R, F> {
345
+ readonly state: WorkflowState<A, R, F>;
346
+ /** Ledger events to persist into the event log, in order, BEFORE `cmds`. */
347
+ readonly ledger: readonly EffectLedgerEvent<WorkflowCmd<A>>[];
348
+ /** Activity dispatch Cmds, after the ledger events are durable. */
349
+ readonly cmds: readonly WorkflowCmd<A>[];
350
+ }
351
+ /** The hook bag returned by {@link createWorkflow}. Spread its verbs into a
352
+ * machine; `init(steps)` seeds a fresh workflow and emits the first activity,
353
+ * the verbs fold results and advance, and `survivingActivities` re-emits the
354
+ * owed-but-unconfirmed dispatch on cold wake (idempotent by delivery id). */
355
+ interface Workflow<A, R, F> {
356
+ /**
357
+ * Seed a fresh workflow over `steps` and dispatch its first activity.
358
+ *
359
+ * - Empty `steps` ⇒ a workflow with nothing to do completes only when it has
360
+ * an output; with no steps there is no output, so an empty sequence is
361
+ * rejected as a misuse (a workflow must have at least one step). The reducer
362
+ * never reaches `completed` without a final result to carry.
363
+ * - Otherwise → `running` with `current` = step 0, owed on the ledger.
364
+ */
365
+ init(steps: readonly WorkflowStep<A>[]): WorkflowStep0<A, R, F>;
366
+ /**
367
+ * Fold an activity success. If `msg.id` does not match the in-flight
368
+ * activity's id (a stale/duplicate result from re-emit-on-wake, or a result
369
+ * for an already-advanced step), the state is returned UNCHANGED with no
370
+ * effects — idempotent by delivery id (acceptance criterion 3). Otherwise:
371
+ * record the completed step, confirm the owed activity on the ledger, and
372
+ * either advance to the next step (owing its activity) or transition to
373
+ * `completed` carrying the final result.
374
+ *
375
+ * Only meaningful on `running`; a result arriving for a terminal workflow is
376
+ * a no-op (the workflow is done — at-least-once tolerates the late echo).
377
+ */
378
+ onActivityOk(state: WorkflowState<A, R, F>, msg: ActivityOk<R>): WorkflowStep0<A, R, F>;
379
+ /**
380
+ * Fold an activity failure (#125). Same id-match idempotency guard as
381
+ * {@link onActivityOk}. On a match: confirm the owed activity on the ledger,
382
+ * then PIVOT into compensation — walk the completed-step history in strict
383
+ * reverse and owe the first declared compensation as a durable effect on the
384
+ * same ledger, transitioning to `compensating`. If NO completed step declares
385
+ * a compensation (the empty-rollback edge — the first activity failed, or
386
+ * every completed step is irreversible), settle `failed` directly with
387
+ * nothing to unwind.
388
+ */
389
+ onActivityErr(state: WorkflowState<A, R, F>, msg: ActivityErr<F>): WorkflowStep0<A, R, F>;
390
+ /**
391
+ * Fold a compensation success (#125). Same id-match idempotency guard against
392
+ * the in-flight compensation. On a match: confirm the owed compensation,
393
+ * record the step as compensated, and continue the reverse walk — owe the
394
+ * next declared compensation strictly below the step just undone, or settle
395
+ * `failed_compensated` when the unwind is complete. Only meaningful on
396
+ * `compensating`; a result for a terminal/forward workflow is a no-op.
397
+ */
398
+ onCompensationOk(state: WorkflowState<A, R, F>, msg: CompensationOk): WorkflowStep0<A, R, F>;
399
+ /**
400
+ * Fold a compensation failure (#125). Same id-match idempotency guard. On a
401
+ * match: confirm the owed compensation and HALT the rollback at the terminal
402
+ * `compensation_failed` state — a bounced compensation is visible terminal
403
+ * state to reconcile by hand, never a workflow wedged in `compensating`
404
+ * forever. Mirrors `@demlik/tea/saga`'s `undoErr`.
405
+ */
406
+ onCompensationErr(state: WorkflowState<A, R, F>, msg: CompensationErr<F>): WorkflowStep0<A, R, F>;
407
+ /**
408
+ * The owed-but-unconfirmed dispatch(es) to re-emit on activation — a forward
409
+ * activity OR (#125) a compensation, rebuilt by folding the persisted ledger
410
+ * events. On a fresh / fully-settled workflow this is empty. The re-fired Cmd
411
+ * carries the SAME delivery id, so a duplicate result is a no-op at the
412
+ * reducer (the id-match guard). This is the cold-wake re-emit that makes both
413
+ * activities AND compensations exactly-once-observable despite the
414
+ * at-least-once transport (acceptance criterion 2 & 3) — including a
415
+ * compensation owed mid-rollback when the actor was evicted.
416
+ */
417
+ survivingActivities(ledgerEvents: Iterable<EffectLedgerEvent<WorkflowCmd<A>>>): readonly WorkflowCmd<A>[];
418
+ }
419
+ /**
420
+ * Build a workflow hook bag. The factory captures NO clock and NO RNG — pure
421
+ * bookkeeping over the injected #67 recorder, exactly like
422
+ * `pendingEffectsLedger`. `restore` rehydrates the monotonic delivery-id
423
+ * counter from the persisted ledger events on cold wake, so re-dispatched
424
+ * activities resume their ids without gaps.
425
+ *
426
+ * @param restore - the persisted ledger events (read back from the log) and/or
427
+ * the last issued delivery id, so the id counter resumes gap-free. Omit for a
428
+ * fresh workflow.
429
+ */
430
+ declare function createWorkflow<A, R, F>(restore?: {
431
+ readonly events?: Iterable<EffectLedgerEvent<WorkflowCmd<A>>>;
432
+ readonly lastId?: DeliveryId;
433
+ }): Workflow<A, R, F>;
434
+ /**
435
+ * Replay a workflow from its event log: seed over `steps`, then fold each
436
+ * activity-result `Msg` in order. Returns the final {@link WorkflowState}.
437
+ *
438
+ * Determinism: a freshly-constructed {@link createWorkflow} reissues delivery
439
+ * ids monotonically from 0, and the reducer reads no ambient state — so two
440
+ * calls with the same `steps` + `msgs` return deeply-equal states (and equal
441
+ * `JSON.stringify`). This is the byte-identity property #124 pins.
442
+ *
443
+ * Note this folds ONLY the result Msgs; the `init` dispatch + per-step
444
+ * owe/confirm ledger events are an internal effect channel reconstructed by the
445
+ * fresh recorder, not part of the replayed Msg log — exactly as the activity
446
+ * results are the only thing the consumer records and replays.
447
+ */
448
+ declare function foldWorkflow<A, R, F>(steps: readonly WorkflowStep<A>[], msgs: Iterable<WorkflowMsg<R, F>>): WorkflowState<A, R, F>;
449
+
450
+ export { type ActivityCmd, type ActivityErr, type ActivityOk, type CompensatingWorkflow, type CompensationCmd, type CompensationErr, type CompensationFailedWorkflow, type CompensationOk, type CompletedStep, type CompletedWorkflow, DeliveryId, EffectLedgerEvent, type FailedCompensatedWorkflow, type FailedWorkflow, type InFlightActivity, type InFlightCompensation, type RunningWorkflow, type Workflow, type WorkflowCmd, type WorkflowMsg, type WorkflowState, type WorkflowStep, type WorkflowStep0, createWorkflow, foldWorkflow };
@@ -0,0 +1,225 @@
1
+ import { pendingEffectsLedger, foldLedger, survivingEffects } from '../chunk-3545BOYL.js';
2
+ export { emptyLedger } from '../chunk-3545BOYL.js';
3
+
4
+ // src/workflow/index.ts
5
+ function createWorkflow(restore) {
6
+ const recorder = pendingEffectsLedger(restore);
7
+ function stepAt(steps, index) {
8
+ const step = steps[index];
9
+ if (step === void 0) {
10
+ throw new Error(
11
+ `createWorkflow: step index ${index} out of range (length ${steps.length}) \u2014 a reducer bounds proof was violated.`
12
+ );
13
+ }
14
+ return step;
15
+ }
16
+ function oweOnLedger(build) {
17
+ const id = recorder.lastId() + 1;
18
+ const cmd = build(id);
19
+ const { id: issued, event } = recorder.owe(cmd);
20
+ if (issued !== id) {
21
+ throw new Error(
22
+ `createWorkflow: delivery-id prediction (${id}) diverged from the recorder's issued id (${issued}). The #67 recorder's monotonic counter is not gap-free as assumed \u2014 this is a substrate invariant violation, not a recoverable condition.`
23
+ );
24
+ }
25
+ return { id, owed: event, cmd };
26
+ }
27
+ function oweActivity(step, index) {
28
+ const { id, owed, cmd } = oweOnLedger((id2) => ({
29
+ type: "workflow_activity",
30
+ index,
31
+ id: id2,
32
+ activity: step.activity
33
+ }));
34
+ const current = { index, step, id };
35
+ return { current, owed, cmd };
36
+ }
37
+ function oweCompensation(step, compensation, index) {
38
+ const { id, owed, cmd } = oweOnLedger((id2) => ({
39
+ type: "workflow_compensation",
40
+ index,
41
+ id: id2,
42
+ compensation
43
+ }));
44
+ const current = { index, step, id };
45
+ return { current, owed, cmd };
46
+ }
47
+ function oweNextCompensation(completed, belowIndex) {
48
+ for (let i = belowIndex - 1; i >= 0; i--) {
49
+ const entry = completed[i];
50
+ if (entry !== void 0 && entry.step.compensation !== void 0) {
51
+ return oweCompensation(entry.step, entry.step.compensation, i);
52
+ }
53
+ }
54
+ return null;
55
+ }
56
+ return {
57
+ init(steps) {
58
+ if (steps.length === 0) {
59
+ throw new Error(
60
+ "createWorkflow.init: a workflow must have at least one step. An empty step sequence has no final result to carry into `completed`, so it is unrepresentable as a completed workflow \u2014 reject it at the boundary rather than admit a resultless terminal state."
61
+ );
62
+ }
63
+ const { current, owed, cmd } = oweActivity(stepAt(steps, 0), 0);
64
+ const state = {
65
+ status: "running",
66
+ steps,
67
+ completed: [],
68
+ current
69
+ };
70
+ return { state, ledger: [owed], cmds: [cmd] };
71
+ },
72
+ onActivityOk(state, msg) {
73
+ if (state.status !== "running" || msg.id !== state.current.id) {
74
+ return { state, ledger: [], cmds: [] };
75
+ }
76
+ const confirmed = confirm(state.current.id);
77
+ const completedStep = {
78
+ step: state.current.step,
79
+ result: msg.result
80
+ };
81
+ const completed = [...state.completed, completedStep];
82
+ const nextIndex = state.current.index + 1;
83
+ if (nextIndex >= state.steps.length) {
84
+ const done = {
85
+ status: "completed",
86
+ steps: state.steps,
87
+ completed,
88
+ output: msg.result
89
+ };
90
+ return { state: done, ledger: [confirmed], cmds: [] };
91
+ }
92
+ const { current, owed, cmd } = oweActivity(
93
+ stepAt(state.steps, nextIndex),
94
+ nextIndex
95
+ );
96
+ const running = {
97
+ status: "running",
98
+ steps: state.steps,
99
+ completed,
100
+ current
101
+ };
102
+ return { state: running, ledger: [confirmed, owed], cmds: [cmd] };
103
+ },
104
+ onActivityErr(state, msg) {
105
+ if (state.status !== "running" || msg.id !== state.current.id) {
106
+ return { state, ledger: [], cmds: [] };
107
+ }
108
+ const confirmed = confirm(state.current.id);
109
+ const next = oweNextCompensation(state.completed, state.completed.length);
110
+ if (next === null) {
111
+ const failed = {
112
+ status: "failed",
113
+ steps: state.steps,
114
+ completed: state.completed,
115
+ failedStep: state.current.step,
116
+ failure: msg.failure
117
+ };
118
+ return { state: failed, ledger: [confirmed], cmds: [] };
119
+ }
120
+ const compensating = {
121
+ status: "compensating",
122
+ steps: state.steps,
123
+ completed: state.completed,
124
+ failedStep: state.current.step,
125
+ failure: msg.failure,
126
+ current: next.current,
127
+ compensated: []
128
+ };
129
+ return {
130
+ state: compensating,
131
+ ledger: [confirmed, next.owed],
132
+ cmds: [next.cmd]
133
+ };
134
+ },
135
+ onCompensationOk(state, msg) {
136
+ if (state.status !== "compensating" || msg.id !== state.current.id) {
137
+ return { state, ledger: [], cmds: [] };
138
+ }
139
+ const confirmed = confirm(state.current.id);
140
+ const undoneStep = state.completed[state.current.index];
141
+ if (undoneStep === void 0) {
142
+ throw new Error(
143
+ `createWorkflow.onCompensationOk: compensation index ${state.current.index} has no completed step \u2014 a reverse-walk bounds proof was violated.`
144
+ );
145
+ }
146
+ const compensated = [...state.compensated, undoneStep];
147
+ const next = oweNextCompensation(state.completed, state.current.index);
148
+ if (next === null) {
149
+ const settled = {
150
+ status: "failed_compensated",
151
+ steps: state.steps,
152
+ completed: state.completed,
153
+ failedStep: state.failedStep,
154
+ failure: state.failure
155
+ };
156
+ return { state: settled, ledger: [confirmed], cmds: [] };
157
+ }
158
+ const compensating = {
159
+ status: "compensating",
160
+ steps: state.steps,
161
+ completed: state.completed,
162
+ failedStep: state.failedStep,
163
+ failure: state.failure,
164
+ current: next.current,
165
+ compensated
166
+ };
167
+ return {
168
+ state: compensating,
169
+ ledger: [confirmed, next.owed],
170
+ cmds: [next.cmd]
171
+ };
172
+ },
173
+ onCompensationErr(state, msg) {
174
+ if (state.status !== "compensating" || msg.id !== state.current.id) {
175
+ return { state, ledger: [], cmds: [] };
176
+ }
177
+ const confirmed = confirm(state.current.id);
178
+ const halted = {
179
+ status: "compensation_failed",
180
+ steps: state.steps,
181
+ completed: state.completed,
182
+ failedStep: state.failedStep,
183
+ failure: state.failure,
184
+ compensated: state.compensated,
185
+ failedCompensationStep: state.current.step,
186
+ compensationFailure: msg.failure
187
+ };
188
+ return { state: halted, ledger: [confirmed], cmds: [] };
189
+ },
190
+ survivingActivities(ledgerEvents) {
191
+ const ledger = foldLedger(ledgerEvents);
192
+ return survivingEffects(ledger).map((owed) => owed.effect);
193
+ }
194
+ };
195
+ function confirm(id) {
196
+ return recorder.confirm(id).event;
197
+ }
198
+ }
199
+ function foldWorkflow(steps, msgs) {
200
+ const wf = createWorkflow();
201
+ let { state } = wf.init(steps);
202
+ for (const msg of msgs) {
203
+ let step;
204
+ switch (msg.type) {
205
+ case "activity_ok":
206
+ step = wf.onActivityOk(state, msg);
207
+ break;
208
+ case "activity_err":
209
+ step = wf.onActivityErr(state, msg);
210
+ break;
211
+ case "compensation_ok":
212
+ step = wf.onCompensationOk(state, msg);
213
+ break;
214
+ case "compensation_err":
215
+ step = wf.onCompensationErr(state, msg);
216
+ break;
217
+ }
218
+ state = step.state;
219
+ }
220
+ return state;
221
+ }
222
+
223
+ export { createWorkflow, foldWorkflow };
224
+ //# sourceMappingURL=index.js.map
225
+ //# sourceMappingURL=index.js.map