rivetkit 0.0.0-main.41efcce → 0.0.0-main.49693dc

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 (299) hide show
  1. package/dist/browser/client.d.ts +939 -104
  2. package/dist/browser/client.js +1867 -900
  3. package/dist/browser/client.js.map +1 -1
  4. package/dist/browser/inspector/client.d.ts +252 -129
  5. package/dist/browser/inspector/client.js +2079 -233
  6. package/dist/browser/inspector/client.js.map +1 -1
  7. package/dist/tsup/actor/errors.cjs +6 -2
  8. package/dist/tsup/actor/errors.cjs.map +1 -1
  9. package/dist/tsup/actor/errors.d.cts +1 -74
  10. package/dist/tsup/actor/errors.d.ts +1 -74
  11. package/dist/tsup/actor/errors.js +5 -1
  12. package/dist/tsup/agent-os/index.cjs +2408 -2029
  13. package/dist/tsup/agent-os/index.cjs.map +1 -1
  14. package/dist/tsup/agent-os/index.d.cts +932 -114
  15. package/dist/tsup/agent-os/index.d.ts +932 -114
  16. package/dist/tsup/agent-os/index.js +2412 -2033
  17. package/dist/tsup/agent-os/index.js.map +1 -1
  18. package/dist/tsup/{chunk-J27CEW6F.js → chunk-2OBVQZX6.js} +6 -6
  19. package/dist/tsup/chunk-2OBVQZX6.js.map +1 -0
  20. package/dist/tsup/chunk-3ZWHWMFJ.js +14 -0
  21. package/dist/tsup/chunk-3ZWHWMFJ.js.map +1 -0
  22. package/dist/tsup/{chunk-AK57AL7S.cjs → chunk-4KGXFC6M.cjs} +238 -22
  23. package/dist/tsup/chunk-4KGXFC6M.cjs.map +1 -0
  24. package/dist/tsup/{chunk-CVHUXINE.js → chunk-5IU3MI7X.js} +594 -72
  25. package/dist/tsup/chunk-5IU3MI7X.js.map +1 -0
  26. package/dist/tsup/{chunk-VD7AZIBR.js → chunk-6CXQMHZR.js} +1161 -361
  27. package/dist/tsup/chunk-6CXQMHZR.js.map +1 -0
  28. package/dist/tsup/{chunk-SBEFJ4IQ.js → chunk-6W2UIH3X.js} +70 -8
  29. package/dist/tsup/chunk-6W2UIH3X.js.map +1 -0
  30. package/dist/tsup/{chunk-7MRGNVSR.js → chunk-6W5VGLFT.js} +237 -21
  31. package/dist/tsup/chunk-6W5VGLFT.js.map +1 -0
  32. package/dist/tsup/{chunk-RF3MEJV6.cjs → chunk-B73TBMMG.cjs} +18 -11
  33. package/dist/tsup/chunk-B73TBMMG.cjs.map +1 -0
  34. package/dist/tsup/chunk-D6TORRA4.js +243 -0
  35. package/dist/tsup/chunk-D6TORRA4.js.map +1 -0
  36. package/dist/tsup/{chunk-PXLJOMJD.js → chunk-FXT3HN32.js} +2 -2
  37. package/dist/tsup/{chunk-ZGWSH7YV.js → chunk-IXBD7BXC.js} +48 -72
  38. package/dist/tsup/chunk-IXBD7BXC.js.map +1 -0
  39. package/dist/tsup/{chunk-3BFYSYRU.js → chunk-JI6GZ2C2.js} +4 -12
  40. package/dist/tsup/chunk-JI6GZ2C2.js.map +1 -0
  41. package/dist/tsup/{chunk-PCBNKI2J.js → chunk-JTHHCZCZ.js} +24 -1
  42. package/dist/tsup/chunk-JTHHCZCZ.js.map +1 -0
  43. package/dist/tsup/{chunk-2NDZ7JCR.cjs → chunk-KVFIONKQ.cjs} +25 -2
  44. package/dist/tsup/chunk-KVFIONKQ.cjs.map +1 -0
  45. package/dist/tsup/{chunk-SPMMF6TR.cjs → chunk-LGOWPZJ5.cjs} +3 -3
  46. package/dist/tsup/chunk-LGOWPZJ5.cjs.map +1 -0
  47. package/dist/tsup/chunk-LYNWTX6R.cjs +14 -0
  48. package/dist/tsup/chunk-LYNWTX6R.cjs.map +1 -0
  49. package/dist/tsup/{chunk-LLD2D5LO.cjs → chunk-MYWZCDML.cjs} +4 -12
  50. package/dist/tsup/chunk-MYWZCDML.cjs.map +1 -0
  51. package/dist/tsup/{chunk-XSC2WWXD.js → chunk-NYDZLWHZ.js} +16 -9
  52. package/dist/tsup/chunk-NYDZLWHZ.js.map +1 -0
  53. package/dist/tsup/{chunk-AV54CVAG.cjs → chunk-RB7WUDX6.cjs} +1290 -490
  54. package/dist/tsup/chunk-RB7WUDX6.cjs.map +1 -0
  55. package/dist/tsup/{chunk-JTL6Y2UN.cjs → chunk-SPJORZMM.cjs} +49 -73
  56. package/dist/tsup/chunk-SPJORZMM.cjs.map +1 -0
  57. package/dist/tsup/chunk-SSMKIYEX.cjs +243 -0
  58. package/dist/tsup/chunk-SSMKIYEX.cjs.map +1 -0
  59. package/dist/tsup/{chunk-TQSL4NJ3.cjs → chunk-TQ4HSX2U.cjs} +72 -10
  60. package/dist/tsup/chunk-TQ4HSX2U.cjs.map +1 -0
  61. package/dist/tsup/{chunk-EVADNK6Z.cjs → chunk-U6ZQELGJ.cjs} +10 -10
  62. package/dist/tsup/chunk-U6ZQELGJ.cjs.map +1 -0
  63. package/dist/tsup/chunk-XLBMKZD3.cjs +1257 -0
  64. package/dist/tsup/chunk-XLBMKZD3.cjs.map +1 -0
  65. package/dist/tsup/client/mod.cjs +8 -9
  66. package/dist/tsup/client/mod.cjs.map +1 -1
  67. package/dist/tsup/client/mod.d.cts +18 -7
  68. package/dist/tsup/client/mod.d.ts +18 -7
  69. package/dist/tsup/client/mod.js +7 -8
  70. package/dist/tsup/common/log.cjs +5 -3
  71. package/dist/tsup/common/log.cjs.map +1 -1
  72. package/dist/tsup/common/log.d.cts +5 -1
  73. package/dist/tsup/common/log.d.ts +5 -1
  74. package/dist/tsup/common/log.js +6 -4
  75. package/dist/tsup/common/websocket.cjs +4 -4
  76. package/dist/tsup/common/websocket.cjs.map +1 -1
  77. package/dist/tsup/common/websocket.js +3 -3
  78. package/dist/tsup/{config-B3RIidaJ.d.ts → config-BXSV17G7.d.cts} +987 -72
  79. package/dist/tsup/{config-D6WxBYjV.d.cts → config-Bi1r6gI7.d.ts} +987 -72
  80. package/dist/tsup/{config-B9qUIHX4.d.ts → config-ey2I2zz5.d.cts} +82 -15
  81. package/dist/tsup/{config-B9qUIHX4.d.cts → config-ey2I2zz5.d.ts} +82 -15
  82. package/dist/tsup/context-4fJemY0D.d.cts +129 -0
  83. package/dist/tsup/context-D4sNYboo.d.ts +129 -0
  84. package/dist/tsup/db/drizzle.cjs +168 -103
  85. package/dist/tsup/db/drizzle.cjs.map +1 -1
  86. package/dist/tsup/db/drizzle.d.cts +8 -4
  87. package/dist/tsup/db/drizzle.d.ts +8 -4
  88. package/dist/tsup/db/drizzle.js +170 -105
  89. package/dist/tsup/db/drizzle.js.map +1 -1
  90. package/dist/tsup/db/mod.cjs +6 -163
  91. package/dist/tsup/db/mod.cjs.map +1 -1
  92. package/dist/tsup/db/mod.d.cts +12 -4
  93. package/dist/tsup/db/mod.d.ts +12 -4
  94. package/dist/tsup/db/mod.js +5 -162
  95. package/dist/tsup/db/mod.js.map +1 -1
  96. package/dist/tsup/dynamic/mod.cjs +24 -0
  97. package/dist/tsup/dynamic/mod.cjs.map +1 -0
  98. package/dist/tsup/dynamic/mod.d.cts +38 -0
  99. package/dist/tsup/dynamic/mod.d.ts +38 -0
  100. package/dist/tsup/dynamic/mod.js +24 -0
  101. package/dist/tsup/dynamic/mod.js.map +1 -0
  102. package/dist/tsup/inspector/mod.cjs +47 -12
  103. package/dist/tsup/inspector/mod.cjs.map +1 -1
  104. package/dist/tsup/inspector/mod.d.cts +26 -136
  105. package/dist/tsup/inspector/mod.d.ts +26 -136
  106. package/dist/tsup/inspector/mod.js +46 -11
  107. package/dist/tsup/inspector/mod.js.map +1 -1
  108. package/dist/tsup/inspector/workflow.cjs +21 -0
  109. package/dist/tsup/inspector/workflow.cjs.map +1 -0
  110. package/dist/tsup/inspector/workflow.d.cts +144 -0
  111. package/dist/tsup/inspector/workflow.d.ts +144 -0
  112. package/dist/tsup/inspector/workflow.js +21 -0
  113. package/dist/tsup/inspector/workflow.js.map +1 -0
  114. package/dist/tsup/inspector-tab/mod.cjs +162 -0
  115. package/dist/tsup/inspector-tab/mod.cjs.map +1 -0
  116. package/dist/tsup/inspector-tab/mod.d.cts +241 -0
  117. package/dist/tsup/inspector-tab/mod.d.ts +241 -0
  118. package/dist/tsup/inspector-tab/mod.js +162 -0
  119. package/dist/tsup/inspector-tab/mod.js.map +1 -0
  120. package/dist/tsup/mod.cjs +4268 -944
  121. package/dist/tsup/mod.cjs.map +1 -1
  122. package/dist/tsup/mod.d.cts +9 -8
  123. package/dist/tsup/mod.d.ts +9 -8
  124. package/dist/tsup/mod.js +4229 -905
  125. package/dist/tsup/mod.js.map +1 -1
  126. package/dist/tsup/test/mod.cjs +26 -21
  127. package/dist/tsup/test/mod.cjs.map +1 -1
  128. package/dist/tsup/test/mod.d.cts +7 -6
  129. package/dist/tsup/test/mod.d.ts +7 -6
  130. package/dist/tsup/test/mod.js +25 -20
  131. package/dist/tsup/test/mod.js.map +1 -1
  132. package/dist/tsup/unstable/migrations.cjs +101 -0
  133. package/dist/tsup/unstable/migrations.cjs.map +1 -0
  134. package/dist/tsup/unstable/migrations.d.cts +29 -0
  135. package/dist/tsup/unstable/migrations.d.ts +29 -0
  136. package/dist/tsup/unstable/migrations.js +101 -0
  137. package/dist/tsup/unstable/migrations.js.map +1 -0
  138. package/dist/tsup/utils-Bbz-3411.d.cts +162 -0
  139. package/dist/tsup/utils-BqdWVlzm.d.ts +162 -0
  140. package/dist/tsup/utils-vvJQyygr.d.cts +109 -0
  141. package/dist/tsup/utils-vvJQyygr.d.ts +109 -0
  142. package/dist/tsup/utils.cjs +7 -3
  143. package/dist/tsup/utils.cjs.map +1 -1
  144. package/dist/tsup/utils.d.cts +4 -147
  145. package/dist/tsup/utils.d.ts +4 -147
  146. package/dist/tsup/utils.js +6 -2
  147. package/dist/tsup/workflow/mod.cjs +517 -412
  148. package/dist/tsup/workflow/mod.cjs.map +1 -1
  149. package/dist/tsup/workflow/mod.d.cts +11 -10
  150. package/dist/tsup/workflow/mod.d.ts +11 -10
  151. package/dist/tsup/workflow/mod.js +511 -406
  152. package/dist/tsup/workflow/mod.js.map +1 -1
  153. package/package.json +62 -14
  154. package/src/actor/actions.ts +119 -0
  155. package/src/actor/config.test.ts +78 -0
  156. package/src/actor/config.ts +518 -96
  157. package/src/actor/contexts/index.ts +7 -2
  158. package/src/actor/definition.ts +38 -26
  159. package/src/actor/errors.ts +101 -89
  160. package/src/actor/keys.ts +2 -1
  161. package/src/actor/mod.ts +32 -21
  162. package/src/actor/schema.ts +2 -2
  163. package/src/agent-os/actor/cron.ts +37 -2
  164. package/src/agent-os/actor/index.ts +39 -19
  165. package/src/agent-os/actor/preview.ts +1 -2
  166. package/src/agent-os/actor/session.ts +2 -2
  167. package/src/agent-os/config.ts +1 -1
  168. package/src/agent-os/fs/database-vfs.ts +1 -1
  169. package/src/agent-os/index.ts +16 -15
  170. package/src/agent-os/types.ts +11 -0
  171. package/src/client/actor-common.ts +106 -53
  172. package/src/client/actor-conn.ts +94 -45
  173. package/src/client/actor-handle.ts +198 -79
  174. package/src/client/actor-query.ts +13 -6
  175. package/src/client/client.ts +43 -15
  176. package/src/client/config.ts +8 -0
  177. package/src/client/errors.ts +4 -2
  178. package/src/client/lifecycle-errors.ts +28 -4
  179. package/src/client/mod.ts +9 -4
  180. package/src/client/query.ts +3 -1
  181. package/src/client/queue.ts +8 -3
  182. package/src/client/raw-utils.ts +24 -16
  183. package/src/client/resolve-gateway-target.ts +3 -1
  184. package/src/client/utils.ts +36 -10
  185. package/src/common/actor-router-consts.ts +7 -0
  186. package/src/common/actor-websocket.ts +3 -1
  187. package/src/common/bare/actor-persist/v1.ts +205 -163
  188. package/src/common/bare/actor-persist/v2.ts +265 -213
  189. package/src/common/bare/actor-persist/v3.ts +176 -172
  190. package/src/common/bare/actor-persist/v4.ts +254 -253
  191. package/src/common/bare/generated/actor-runtime-socket-protocol/v1.ts +848 -0
  192. package/src/common/bare/generated/client-protocol/v4.ts +599 -0
  193. package/src/common/bare/generated/inspector/v5.ts +1016 -0
  194. package/src/common/bare/generated/inspector/v6.ts +1367 -0
  195. package/src/common/bare/transport/v1.ts +694 -542
  196. package/src/common/client-protocol-versioned.ts +176 -67
  197. package/src/common/client-protocol-zod.ts +7 -0
  198. package/src/common/client-protocol.ts +1 -1
  199. package/src/common/database/config.ts +103 -10
  200. package/src/common/database/mod.test.ts +364 -0
  201. package/src/common/database/mod.ts +278 -149
  202. package/src/common/database/native-database.test.ts +220 -0
  203. package/src/common/database/native-database.ts +394 -58
  204. package/src/common/database/shared.ts +25 -0
  205. package/src/common/encoding.ts +302 -38
  206. package/src/common/engine.ts +28 -1
  207. package/src/common/eventsource.ts +1 -1
  208. package/src/common/fetch-like.ts +49 -0
  209. package/src/common/inline-websocket-adapter.ts +14 -13
  210. package/src/common/inspector-transport.ts +12 -2
  211. package/src/common/log.ts +117 -14
  212. package/src/common/router.ts +49 -23
  213. package/src/common/utils.ts +23 -203
  214. package/src/common/websocket-interface.ts +1 -1
  215. package/src/db/drizzle.test.ts +164 -0
  216. package/src/db/drizzle.ts +207 -121
  217. package/src/db/mod.ts +6 -1
  218. package/src/devtools-loader/index.ts +4 -7
  219. package/src/devtools-loader/serve-devtools.ts +26 -0
  220. package/src/dynamic/instance.ts +32 -0
  221. package/src/dynamic/internal.ts +50 -0
  222. package/src/dynamic/isolate-runtime.ts +64 -0
  223. package/src/dynamic/mod.ts +32 -0
  224. package/src/engine-client/actor-http-client.ts +23 -4
  225. package/src/engine-client/actor-websocket-client.ts +32 -5
  226. package/src/engine-client/api-endpoints.ts +52 -2
  227. package/src/engine-client/api-utils.ts +2 -2
  228. package/src/engine-client/driver.ts +21 -17
  229. package/src/engine-client/mod.ts +116 -64
  230. package/src/engine-client/ws-proxy.ts +9 -4
  231. package/src/globals.d.ts +4 -0
  232. package/src/inspector/client.browser.ts +127 -15
  233. package/src/inspector/mod.ts +2 -4
  234. package/src/inspector/workflow.ts +61 -0
  235. package/src/inspector-tab/mod.ts +301 -0
  236. package/src/registry/config/envoy.ts +1 -2
  237. package/src/registry/config/index.ts +160 -53
  238. package/src/registry/config/serverless.ts +7 -1
  239. package/src/registry/index.ts +659 -96
  240. package/src/registry/napi-runtime.ts +1154 -0
  241. package/src/registry/native-validation.ts +10 -12
  242. package/src/registry/native.ts +2005 -955
  243. package/src/registry/process-metrics.ts +250 -0
  244. package/src/registry/run-handler-coordinator.test.ts +252 -0
  245. package/src/registry/run-handler-coordinator.ts +219 -0
  246. package/src/registry/runtime.test.ts +50 -0
  247. package/src/registry/runtime.ts +839 -0
  248. package/src/registry/wasm-runtime.ts +1283 -0
  249. package/src/registry/write-through-proxy.ts +141 -0
  250. package/src/serde.ts +8 -2
  251. package/src/serverless/configure.ts +102 -49
  252. package/src/test/mod.ts +2 -17
  253. package/src/test/ready.ts +22 -0
  254. package/src/unstable/migrations.test.ts +170 -0
  255. package/src/unstable/migrations.ts +141 -0
  256. package/src/utils/crypto.ts +14 -0
  257. package/src/utils/endpoint-parser.ts +1 -1
  258. package/src/utils/env-vars.ts +39 -0
  259. package/src/utils/node.ts +44 -28
  260. package/src/utils/router.ts +1 -1
  261. package/src/utils.ts +4 -2
  262. package/src/workflow/context.ts +683 -242
  263. package/src/workflow/driver.test.ts +62 -0
  264. package/src/workflow/driver.ts +246 -119
  265. package/src/workflow/inspector.ts +24 -24
  266. package/src/workflow/mod.ts +76 -84
  267. package/dist/tsup/chunk-2NDZ7JCR.cjs.map +0 -1
  268. package/dist/tsup/chunk-3BFYSYRU.js.map +0 -1
  269. package/dist/tsup/chunk-6AVUJ52Q.cjs +0 -253
  270. package/dist/tsup/chunk-6AVUJ52Q.cjs.map +0 -1
  271. package/dist/tsup/chunk-7MRGNVSR.js.map +0 -1
  272. package/dist/tsup/chunk-7X7RVTFF.cjs +0 -735
  273. package/dist/tsup/chunk-7X7RVTFF.cjs.map +0 -1
  274. package/dist/tsup/chunk-AK57AL7S.cjs.map +0 -1
  275. package/dist/tsup/chunk-AV54CVAG.cjs.map +0 -1
  276. package/dist/tsup/chunk-CVHUXINE.js.map +0 -1
  277. package/dist/tsup/chunk-EVADNK6Z.cjs.map +0 -1
  278. package/dist/tsup/chunk-J27CEW6F.js.map +0 -1
  279. package/dist/tsup/chunk-JTL6Y2UN.cjs.map +0 -1
  280. package/dist/tsup/chunk-LLD2D5LO.cjs.map +0 -1
  281. package/dist/tsup/chunk-PCBNKI2J.js.map +0 -1
  282. package/dist/tsup/chunk-RF3MEJV6.cjs.map +0 -1
  283. package/dist/tsup/chunk-SB4MXRN2.js +0 -253
  284. package/dist/tsup/chunk-SB4MXRN2.js.map +0 -1
  285. package/dist/tsup/chunk-SBEFJ4IQ.js.map +0 -1
  286. package/dist/tsup/chunk-SPMMF6TR.cjs.map +0 -1
  287. package/dist/tsup/chunk-TQSL4NJ3.cjs.map +0 -1
  288. package/dist/tsup/chunk-VD7AZIBR.js.map +0 -1
  289. package/dist/tsup/chunk-XSC2WWXD.js.map +0 -1
  290. package/dist/tsup/chunk-ZGWSH7YV.js.map +0 -1
  291. package/dist/tsup/context-BxvHh9wi.d.cts +0 -92
  292. package/dist/tsup/context-CNIZBIKl.d.ts +0 -92
  293. package/dist/tsup/utils-fwx3o3K9.d.cts +0 -18
  294. package/dist/tsup/utils-fwx3o3K9.d.ts +0 -18
  295. package/src/actor/driver.ts +0 -141
  296. package/src/actor/instance/mod.ts +0 -2501
  297. package/src/drivers/engine/actor-driver.ts +0 -2470
  298. package/src/utils/serve.ts +0 -217
  299. /package/dist/tsup/{chunk-PXLJOMJD.js.map → chunk-FXT3HN32.js.map} +0 -0
@@ -1,41 +1,40 @@
1
- // @ts-nocheck
2
- import type { RunContext } from "@/actor/config";
1
+ import type {
2
+ BranchConfig,
3
+ BranchOutput,
4
+ EntryKindType,
5
+ LoopConfig,
6
+ LoopResult,
7
+ StepConfig,
8
+ TryBlockConfig,
9
+ TryBlockResult,
10
+ TryStepConfig,
11
+ TryStepResult,
12
+ WorkflowContextInterface,
13
+ WorkflowQueueMessage,
14
+ } from "@rivetkit/workflow-engine";
3
15
  import type {
4
16
  QueueFilterName,
5
17
  QueueNextBatchOptions,
6
18
  QueueNextOptions,
7
19
  QueueResultMessageForName,
20
+ RunContext,
8
21
  } from "@/actor/config";
9
- import type { Client } from "@/client/client";
10
- import type { Registry } from "@/registry";
11
22
  import type {
12
- BaseActorDefinition,
13
23
  AnyActorDefinition,
24
+ BaseActorDefinition,
14
25
  } from "@/actor/definition";
15
- import type {
16
- AnyDatabaseProvider,
17
- InferDatabaseClient,
18
- } from "@/common/database/config";
19
26
  import type {
20
27
  EventSchemaConfig,
21
28
  InferEventArgs,
22
29
  InferSchemaMap,
23
30
  QueueSchemaConfig,
24
31
  } from "@/actor/schema";
25
- import type { WorkflowContextInterface } from "@rivetkit/workflow-engine";
32
+ import type { Client } from "@/client/client";
26
33
  import type {
27
- BranchConfig,
28
- BranchOutput,
29
- EntryKindType,
30
- LoopConfig,
31
- LoopResult,
32
- StepConfig,
33
- TryBlockConfig,
34
- TryBlockResult,
35
- TryStepConfig,
36
- TryStepResult,
37
- WorkflowQueueMessage,
38
- } from "@rivetkit/workflow-engine";
34
+ AnyDatabaseProvider,
35
+ InferDatabaseClient,
36
+ } from "@/common/database/config";
37
+ import type { Registry } from "@/registry";
39
38
  import { WORKFLOW_GUARD_KV_KEY } from "./constants";
40
39
 
41
40
  type WorkflowActorQueueNextOptions<
@@ -56,7 +55,177 @@ type WorkflowActorQueueNextBatchOptions<
56
55
  type WorkflowActorQueueNextBatchOptionsFallback<TCompletable extends boolean> =
57
56
  Omit<QueueNextBatchOptions<string, TCompletable>, "signal">;
58
57
 
59
- type ActorWorkflowLoopConfig<
58
+ // Step run callbacks receive a WorkflowStepContext, which is the only place
59
+ // actor data (state/db/vars/client) may be touched.
60
+ type WorkflowStepRun<
61
+ T,
62
+ TState,
63
+ TConnParams,
64
+ TConnState,
65
+ TVars,
66
+ TInput,
67
+ TDatabase extends AnyDatabaseProvider,
68
+ TEvents extends EventSchemaConfig,
69
+ TQueues extends QueueSchemaConfig,
70
+ > = (
71
+ step: WorkflowStepContext<
72
+ TState,
73
+ TConnParams,
74
+ TConnState,
75
+ TVars,
76
+ TInput,
77
+ TDatabase,
78
+ TEvents,
79
+ TQueues
80
+ >,
81
+ ) => Promise<T>;
82
+
83
+ // Step rollback callbacks compensate a committed step, so they also run with a
84
+ // WorkflowStepContext to mutate actor data.
85
+ type WorkflowStepRollback<
86
+ T,
87
+ TState,
88
+ TConnParams,
89
+ TConnState,
90
+ TVars,
91
+ TInput,
92
+ TDatabase extends AnyDatabaseProvider,
93
+ TEvents extends EventSchemaConfig,
94
+ TQueues extends QueueSchemaConfig,
95
+ > = (
96
+ step: WorkflowStepContext<
97
+ TState,
98
+ TConnParams,
99
+ TConnState,
100
+ TVars,
101
+ TInput,
102
+ TDatabase,
103
+ TEvents,
104
+ TQueues
105
+ >,
106
+ output: T,
107
+ ) => Promise<void>;
108
+
109
+ // Orchestration callbacks (try/loop/race/join) receive a WorkflowContext,
110
+ // because inside them you sequence further steps rather than touch actor data.
111
+ type WorkflowContextRun<
112
+ T,
113
+ TState,
114
+ TConnParams,
115
+ TConnState,
116
+ TVars,
117
+ TInput,
118
+ TDatabase extends AnyDatabaseProvider,
119
+ TEvents extends EventSchemaConfig,
120
+ TQueues extends QueueSchemaConfig,
121
+ > = (
122
+ ctx: WorkflowContext<
123
+ TState,
124
+ TConnParams,
125
+ TConnState,
126
+ TVars,
127
+ TInput,
128
+ TDatabase,
129
+ TEvents,
130
+ TQueues
131
+ >,
132
+ ) => Promise<T>;
133
+
134
+ export type WorkflowStepConfig<
135
+ T,
136
+ TState,
137
+ TConnParams,
138
+ TConnState,
139
+ TVars,
140
+ TInput,
141
+ TDatabase extends AnyDatabaseProvider,
142
+ TEvents extends EventSchemaConfig,
143
+ TQueues extends QueueSchemaConfig,
144
+ > = Omit<StepConfig<T>, "run" | "rollback"> & {
145
+ run: WorkflowStepRun<
146
+ T,
147
+ TState,
148
+ TConnParams,
149
+ TConnState,
150
+ TVars,
151
+ TInput,
152
+ TDatabase,
153
+ TEvents,
154
+ TQueues
155
+ >;
156
+ rollback?: WorkflowStepRollback<
157
+ T,
158
+ TState,
159
+ TConnParams,
160
+ TConnState,
161
+ TVars,
162
+ TInput,
163
+ TDatabase,
164
+ TEvents,
165
+ TQueues
166
+ >;
167
+ };
168
+
169
+ export type WorkflowTryStepConfig<
170
+ T,
171
+ TState,
172
+ TConnParams,
173
+ TConnState,
174
+ TVars,
175
+ TInput,
176
+ TDatabase extends AnyDatabaseProvider,
177
+ TEvents extends EventSchemaConfig,
178
+ TQueues extends QueueSchemaConfig,
179
+ > = Omit<TryStepConfig<T>, "run" | "rollback"> & {
180
+ run: WorkflowStepRun<
181
+ T,
182
+ TState,
183
+ TConnParams,
184
+ TConnState,
185
+ TVars,
186
+ TInput,
187
+ TDatabase,
188
+ TEvents,
189
+ TQueues
190
+ >;
191
+ rollback?: WorkflowStepRollback<
192
+ T,
193
+ TState,
194
+ TConnParams,
195
+ TConnState,
196
+ TVars,
197
+ TInput,
198
+ TDatabase,
199
+ TEvents,
200
+ TQueues
201
+ >;
202
+ };
203
+
204
+ export type WorkflowTryConfig<
205
+ T,
206
+ TState,
207
+ TConnParams,
208
+ TConnState,
209
+ TVars,
210
+ TInput,
211
+ TDatabase extends AnyDatabaseProvider,
212
+ TEvents extends EventSchemaConfig,
213
+ TQueues extends QueueSchemaConfig,
214
+ > = Omit<TryBlockConfig<T>, "run"> & {
215
+ run: WorkflowContextRun<
216
+ T,
217
+ TState,
218
+ TConnParams,
219
+ TConnState,
220
+ TVars,
221
+ TInput,
222
+ TDatabase,
223
+ TEvents,
224
+ TQueues
225
+ >;
226
+ };
227
+
228
+ export type WorkflowLoopConfig<
60
229
  S,
61
230
  T,
62
231
  TState,
@@ -69,7 +238,7 @@ type ActorWorkflowLoopConfig<
69
238
  TQueues extends QueueSchemaConfig,
70
239
  > = Omit<LoopConfig<S, T>, "run"> & {
71
240
  run: (
72
- ctx: ActorWorkflowContext<
241
+ ctx: WorkflowContext<
73
242
  TState,
74
243
  TConnParams,
75
244
  TConnState,
@@ -80,10 +249,12 @@ type ActorWorkflowLoopConfig<
80
249
  TQueues
81
250
  >,
82
251
  state: S,
83
- ) => Promise<LoopResult<S, T> | (S extends undefined ? void : never)>;
252
+ ) => Promise<
253
+ LoopResult<S, T> | (S extends undefined ? undefined | void : never)
254
+ >;
84
255
  };
85
256
 
86
- type ActorWorkflowBranchConfig<
257
+ export type WorkflowBranchConfig<
87
258
  TOutput,
88
259
  TState,
89
260
  TConnParams,
@@ -94,8 +265,54 @@ type ActorWorkflowBranchConfig<
94
265
  TEvents extends EventSchemaConfig,
95
266
  TQueues extends QueueSchemaConfig,
96
267
  > = {
97
- run: (
98
- ctx: ActorWorkflowContext<
268
+ run: WorkflowContextRun<
269
+ TOutput,
270
+ TState,
271
+ TConnParams,
272
+ TConnState,
273
+ TVars,
274
+ TInput,
275
+ TDatabase,
276
+ TEvents,
277
+ TQueues
278
+ >;
279
+ };
280
+
281
+ // Marks a step context inactive once its step has finished. Module-private so it
282
+ // never appears on the public surface.
283
+ const DEACTIVATE_STEP = Symbol("workflow.step.deactivate");
284
+
285
+ /**
286
+ * The context handed to a workflow step (`step` / `tryStep` callbacks). This is
287
+ * the only scope where actor data (state, vars, db, client) and side effects
288
+ * (broadcast, queue.send) are reachable. It is valid only while its step is
289
+ * executing; using it after the step resolves throws.
290
+ */
291
+ export class WorkflowStepContext<
292
+ TState,
293
+ TConnParams,
294
+ TConnState,
295
+ TVars,
296
+ TInput,
297
+ TDatabase extends AnyDatabaseProvider,
298
+ TEvents extends EventSchemaConfig = Record<never, never>,
299
+ TQueues extends QueueSchemaConfig = Record<never, never>,
300
+ > {
301
+ #runCtx: RunContext<
302
+ TState,
303
+ TConnParams,
304
+ TConnState,
305
+ TVars,
306
+ TInput,
307
+ TDatabase,
308
+ TEvents,
309
+ TQueues
310
+ >;
311
+ #active = true;
312
+ #onGuardViolation: () => void;
313
+
314
+ constructor(
315
+ runCtx: RunContext<
99
316
  TState,
100
317
  TConnParams,
101
318
  TConnState,
@@ -105,10 +322,133 @@ type ActorWorkflowBranchConfig<
105
322
  TEvents,
106
323
  TQueues
107
324
  >,
108
- ) => Promise<TOutput>;
109
- };
325
+ onGuardViolation: () => void,
326
+ ) {
327
+ this.#runCtx = runCtx;
328
+ this.#onGuardViolation = onGuardViolation;
329
+ }
330
+
331
+ [DEACTIVATE_STEP](): void {
332
+ this.#active = false;
333
+ }
334
+
335
+ #ensureActive(feature: string): void {
336
+ if (!this.#active) {
337
+ this.#onGuardViolation();
338
+ throw new Error(
339
+ `${feature} is only available inside workflow steps`,
340
+ );
341
+ }
342
+ }
343
+
344
+ get actorId(): string {
345
+ return this.#runCtx.actorId;
346
+ }
347
+
348
+ get name(): string {
349
+ return this.#runCtx.name;
350
+ }
351
+
352
+ get key(): string[] {
353
+ return this.#runCtx.key;
354
+ }
110
355
 
111
- export class ActorWorkflowContext<
356
+ get log() {
357
+ return this.#runCtx.log;
358
+ }
359
+
360
+ get abortSignal(): AbortSignal {
361
+ return this.#runCtx.abortSignal;
362
+ }
363
+
364
+ get state(): TState extends never ? never : TState {
365
+ this.#ensureActive("state");
366
+ return this.#runCtx.state as TState extends never ? never : TState;
367
+ }
368
+
369
+ get vars(): TVars extends never ? never : TVars {
370
+ this.#ensureActive("vars");
371
+ return this.#runCtx.vars as TVars extends never ? never : TVars;
372
+ }
373
+
374
+ get db(): TDatabase extends never ? never : InferDatabaseClient<TDatabase> {
375
+ this.#ensureActive("db");
376
+ return this.#runCtx.db as TDatabase extends never
377
+ ? never
378
+ : InferDatabaseClient<TDatabase>;
379
+ }
380
+
381
+ client<R extends Registry<any> = Registry<any>>(): Client<R> {
382
+ this.#ensureActive("client");
383
+ return this.#runCtx.client<R>();
384
+ }
385
+
386
+ broadcast<K extends keyof TEvents & string>(
387
+ name: K,
388
+ ...args: InferEventArgs<InferSchemaMap<TEvents>[K]>
389
+ ): void;
390
+ broadcast(
391
+ name: keyof TEvents extends never ? string : never,
392
+ ...args: Array<unknown>
393
+ ): void;
394
+ broadcast(name: string, ...args: Array<unknown>): void {
395
+ this.#ensureActive("broadcast");
396
+ this.#runCtx.broadcast(
397
+ name as never,
398
+ ...(args as unknown[] as never[]),
399
+ );
400
+ }
401
+
402
+ get queue() {
403
+ const self = this;
404
+ function send<K extends keyof TQueues & string>(
405
+ name: K,
406
+ body: InferSchemaMap<TQueues>[K],
407
+ ): Promise<void>;
408
+ function send(
409
+ name: keyof TQueues extends never ? string : never,
410
+ body: unknown,
411
+ ): Promise<void>;
412
+ async function send(name: string, body: unknown): Promise<void> {
413
+ self.#ensureActive("queue.send");
414
+ await self.#runCtx.queue.send(name as never, body as never);
415
+ }
416
+ return { send };
417
+ }
418
+
419
+ /**
420
+ * Holds the actor awake for the duration of the provided promise. The actor
421
+ * cannot idle-sleep or finalize the sleep grace period until the promise
422
+ * settles.
423
+ */
424
+ keepAwake<T>(promise: Promise<T>): Promise<T> {
425
+ this.#ensureActive("keepAwake");
426
+ return this.#runCtx.keepAwake(promise);
427
+ }
428
+
429
+ /**
430
+ * Registers a promise that the sleep grace period will wait on. Use this for
431
+ * best-effort flush/cleanup work that may complete inside the grace window.
432
+ */
433
+ waitUntil(promise: Promise<void>): void {
434
+ this.#ensureActive("waitUntil");
435
+ this.#runCtx.waitUntil(promise);
436
+ }
437
+
438
+ destroy(): void {
439
+ this.#ensureActive("destroy");
440
+ this.#runCtx.destroy();
441
+ }
442
+ }
443
+
444
+ /**
445
+ * The context handed to the workflow function and to orchestration callbacks
446
+ * (`try` / `loop` / `race` / `join`). It exposes the deterministic, replayable
447
+ * workflow primitives (step, sleep, queue waits, control flow). It deliberately
448
+ * does NOT expose actor data; reach `state`, `db`, `vars`, `client`, and
449
+ * `broadcast` through the step context passed to `step` / `tryStep`.
450
+ */
451
+ export class WorkflowContext<
112
452
  TState,
113
453
  TConnParams,
114
454
  TConnState,
@@ -117,8 +457,7 @@ export class ActorWorkflowContext<
117
457
  TDatabase extends AnyDatabaseProvider,
118
458
  TEvents extends EventSchemaConfig = Record<never, never>,
119
459
  TQueues extends QueueSchemaConfig = Record<never, never>,
120
- > implements WorkflowContextInterface
121
- {
460
+ > {
122
461
  #inner: WorkflowContextInterface;
123
462
  #runCtx: RunContext<
124
463
  TState,
@@ -130,10 +469,6 @@ export class ActorWorkflowContext<
130
469
  TEvents,
131
470
  TQueues
132
471
  >;
133
- #actorAccessDepth = 0;
134
- #allowActorAccess = false;
135
- #guardViolation = false;
136
-
137
472
  constructor(
138
473
  inner: WorkflowContextInterface,
139
474
  runCtx: RunContext<
@@ -159,6 +494,22 @@ export class ActorWorkflowContext<
159
494
  return this.#inner.abortSignal;
160
495
  }
161
496
 
497
+ get actorId(): string {
498
+ return this.#runCtx.actorId;
499
+ }
500
+
501
+ get name(): string {
502
+ return this.#runCtx.name;
503
+ }
504
+
505
+ get key(): string[] {
506
+ return this.#runCtx.key;
507
+ }
508
+
509
+ get log() {
510
+ return this.#runCtx.log;
511
+ }
512
+
162
513
  get queue() {
163
514
  const self = this;
164
515
  function next<
@@ -178,10 +529,9 @@ export class ActorWorkflowContext<
178
529
  TCompletable
179
530
  >
180
531
  >;
181
- async function next(
182
- name: string,
183
- opts?: WorkflowActorQueueNextOptions<string, boolean>,
184
- ): Promise<WorkflowQueueMessage<unknown>> {
532
+ // The implementation signature stays broad so the schema-typed public
533
+ // overloads above remain compatible with it.
534
+ async function next(name: string, opts?: any): Promise<any> {
185
535
  const message = await self.#inner.queue.next(name, opts);
186
536
  return self.#toActorQueueMessage(message);
187
537
  }
@@ -207,108 +557,232 @@ export class ActorWorkflowContext<
207
557
  >
208
558
  >
209
559
  >;
210
- async function nextBatch(
211
- name: string,
212
- opts?: WorkflowActorQueueNextBatchOptions<string, boolean>,
213
- ): Promise<Array<WorkflowQueueMessage<unknown>>> {
560
+ async function nextBatch(name: string, opts?: any): Promise<any> {
214
561
  const messages = await self.#inner.queue.nextBatch(name, opts);
215
562
  return messages.map((message) =>
216
563
  self.#toActorQueueMessage(message),
217
564
  );
218
565
  }
219
566
 
220
- function send<K extends keyof TQueues & string>(
221
- name: K,
222
- body: InferSchemaMap<TQueues>[K],
223
- ): Promise<void>;
224
- function send(
225
- name: keyof TQueues extends never ? string : never,
226
- body: unknown,
227
- ): Promise<void>;
228
- async function send(name: string, body: unknown): Promise<void> {
229
- self.#ensureActorAccess("queue.send");
230
- await self.#runCtx.queue.send(name as never, body as never);
231
- }
232
-
233
567
  return {
234
568
  next,
235
569
  nextBatch,
236
- send,
237
570
  };
238
571
  }
239
572
 
573
+ step<T>(
574
+ name: string,
575
+ run: WorkflowStepRun<
576
+ T,
577
+ TState,
578
+ TConnParams,
579
+ TConnState,
580
+ TVars,
581
+ TInput,
582
+ TDatabase,
583
+ TEvents,
584
+ TQueues
585
+ >,
586
+ ): Promise<T>;
587
+ step<T>(
588
+ config: WorkflowStepConfig<
589
+ T,
590
+ TState,
591
+ TConnParams,
592
+ TConnState,
593
+ TVars,
594
+ TInput,
595
+ TDatabase,
596
+ TEvents,
597
+ TQueues
598
+ >,
599
+ ): Promise<T>;
240
600
  async step<T>(
241
- nameOrConfig: string | Parameters<WorkflowContextInterface["step"]>[0],
242
- run?: () => Promise<T>,
601
+ nameOrConfig:
602
+ | string
603
+ | WorkflowStepConfig<
604
+ T,
605
+ TState,
606
+ TConnParams,
607
+ TConnState,
608
+ TVars,
609
+ TInput,
610
+ TDatabase,
611
+ TEvents,
612
+ TQueues
613
+ >,
614
+ run?: WorkflowStepRun<
615
+ T,
616
+ TState,
617
+ TConnParams,
618
+ TConnState,
619
+ TVars,
620
+ TInput,
621
+ TDatabase,
622
+ TEvents,
623
+ TQueues
624
+ >,
243
625
  ): Promise<T> {
244
626
  if (typeof nameOrConfig === "string") {
245
627
  if (!run) {
246
628
  throw new Error("Step run function missing");
247
629
  }
630
+ const stepRun = run;
248
631
  return await this.#wrapActive(() =>
249
- this.#inner.step(nameOrConfig, () =>
250
- this.#withActorAccess(run),
251
- ),
632
+ this.#inner.step(nameOrConfig, () => this.#runStep(stepRun)),
252
633
  );
253
634
  }
254
- const stepConfig = nameOrConfig as StepConfig<T>;
635
+ const stepConfig = nameOrConfig;
636
+ const rollback = stepConfig.rollback;
255
637
  const config: StepConfig<T> = {
256
638
  ...stepConfig,
257
- run: () => this.#withActorAccess(stepConfig.run),
639
+ run: () => this.#runStep(stepConfig.run),
640
+ rollback: rollback
641
+ ? (_ctx, output) => this.#runRollback(rollback, output)
642
+ : undefined,
258
643
  };
259
644
  return await this.#wrapActive(() => this.#inner.step(config));
260
645
  }
261
646
 
647
+ tryStep<T>(
648
+ name: string,
649
+ run: WorkflowStepRun<
650
+ T,
651
+ TState,
652
+ TConnParams,
653
+ TConnState,
654
+ TVars,
655
+ TInput,
656
+ TDatabase,
657
+ TEvents,
658
+ TQueues
659
+ >,
660
+ ): Promise<TryStepResult<T>>;
661
+ tryStep<T>(
662
+ config: WorkflowTryStepConfig<
663
+ T,
664
+ TState,
665
+ TConnParams,
666
+ TConnState,
667
+ TVars,
668
+ TInput,
669
+ TDatabase,
670
+ TEvents,
671
+ TQueues
672
+ >,
673
+ ): Promise<TryStepResult<T>>;
262
674
  async tryStep<T>(
263
675
  nameOrConfig:
264
676
  | string
265
- | Parameters<WorkflowContextInterface["tryStep"]>[0],
266
- run?: () => Promise<T>,
677
+ | WorkflowTryStepConfig<
678
+ T,
679
+ TState,
680
+ TConnParams,
681
+ TConnState,
682
+ TVars,
683
+ TInput,
684
+ TDatabase,
685
+ TEvents,
686
+ TQueues
687
+ >,
688
+ run?: WorkflowStepRun<
689
+ T,
690
+ TState,
691
+ TConnParams,
692
+ TConnState,
693
+ TVars,
694
+ TInput,
695
+ TDatabase,
696
+ TEvents,
697
+ TQueues
698
+ >,
267
699
  ): Promise<TryStepResult<T>> {
268
700
  if (typeof nameOrConfig === "string") {
269
701
  if (!run) {
270
702
  throw new Error("Step run function missing");
271
703
  }
704
+ const stepRun = run;
272
705
  return await this.#wrapActive(() =>
273
- this.#inner.tryStep(nameOrConfig, () =>
274
- this.#withActorAccess(run),
275
- ),
706
+ this.#inner.tryStep(nameOrConfig, () => this.#runStep(stepRun)),
276
707
  );
277
708
  }
278
- const stepConfig = nameOrConfig as TryStepConfig<T>;
709
+ const stepConfig = nameOrConfig;
710
+ const rollback = stepConfig.rollback;
279
711
  const config: TryStepConfig<T> = {
280
712
  ...stepConfig,
281
- run: () => this.#withActorAccess(stepConfig.run),
713
+ run: () => this.#runStep(stepConfig.run),
714
+ rollback: rollback
715
+ ? (_ctx, output) => this.#runRollback(rollback, output)
716
+ : undefined,
282
717
  };
283
718
  return await this.#wrapActive(() => this.#inner.tryStep(config));
284
719
  }
285
720
 
721
+ try<T>(
722
+ name: string,
723
+ run: WorkflowContextRun<
724
+ T,
725
+ TState,
726
+ TConnParams,
727
+ TConnState,
728
+ TVars,
729
+ TInput,
730
+ TDatabase,
731
+ TEvents,
732
+ TQueues
733
+ >,
734
+ ): Promise<TryBlockResult<T>>;
735
+ try<T>(
736
+ config: WorkflowTryConfig<
737
+ T,
738
+ TState,
739
+ TConnParams,
740
+ TConnState,
741
+ TVars,
742
+ TInput,
743
+ TDatabase,
744
+ TEvents,
745
+ TQueues
746
+ >,
747
+ ): Promise<TryBlockResult<T>>;
286
748
  async try<T>(
287
- nameOrConfig: string | Parameters<WorkflowContextInterface["try"]>[0],
288
- run?: (
289
- ctx: ActorWorkflowContext<
290
- TState,
291
- TConnParams,
292
- TConnState,
293
- TVars,
294
- TInput,
295
- TDatabase,
296
- TEvents,
297
- TQueues
298
- >,
299
- ) => Promise<T>,
749
+ nameOrConfig:
750
+ | string
751
+ | WorkflowTryConfig<
752
+ T,
753
+ TState,
754
+ TConnParams,
755
+ TConnState,
756
+ TVars,
757
+ TInput,
758
+ TDatabase,
759
+ TEvents,
760
+ TQueues
761
+ >,
762
+ run?: WorkflowContextRun<
763
+ T,
764
+ TState,
765
+ TConnParams,
766
+ TConnState,
767
+ TVars,
768
+ TInput,
769
+ TDatabase,
770
+ TEvents,
771
+ TQueues
772
+ >,
300
773
  ): Promise<TryBlockResult<T>> {
301
774
  if (typeof nameOrConfig === "string") {
302
775
  if (!run) {
303
776
  throw new Error("Try run function missing");
304
777
  }
778
+ const tryRun = run;
305
779
  return await this.#wrapActive(() =>
306
780
  this.#inner.try(nameOrConfig, async (ctx) =>
307
- run(this.#createChildContext(ctx)),
781
+ tryRun(this.#createChildContext(ctx)),
308
782
  ),
309
783
  );
310
784
  }
311
- const tryConfig = nameOrConfig as TryBlockConfig<T>;
785
+ const tryConfig = nameOrConfig;
312
786
  const config: TryBlockConfig<T> = {
313
787
  ...tryConfig,
314
788
  run: async (ctx) => tryConfig.run(this.#createChildContext(ctx)),
@@ -316,10 +790,10 @@ export class ActorWorkflowContext<
316
790
  return await this.#wrapActive(() => this.#inner.try(config));
317
791
  }
318
792
 
319
- async loop<T>(
793
+ loop<T>(
320
794
  name: string,
321
795
  run: (
322
- ctx: ActorWorkflowContext<
796
+ ctx: WorkflowContext<
323
797
  TState,
324
798
  TConnParams,
325
799
  TConnState,
@@ -329,16 +803,10 @@ export class ActorWorkflowContext<
329
803
  TEvents,
330
804
  TQueues
331
805
  >,
332
- ) => Promise<LoopResult<undefined, T> | void>,
333
- ): Promise<T>;
334
- async loop<T>(
335
- name: string,
336
- run: (
337
- ctx: WorkflowContextInterface,
338
- ) => Promise<LoopResult<undefined, T> | void>,
806
+ ) => Promise<LoopResult<undefined, T> | undefined | void>,
339
807
  ): Promise<T>;
340
- async loop<S, T>(
341
- config: ActorWorkflowLoopConfig<
808
+ loop<S, T>(
809
+ config: WorkflowLoopConfig<
342
810
  S,
343
811
  T,
344
812
  TState,
@@ -351,12 +819,10 @@ export class ActorWorkflowContext<
351
819
  TQueues
352
820
  >,
353
821
  ): Promise<T>;
354
- async loop<S, T>(config: LoopConfig<S, T>): Promise<T>;
355
822
  async loop(
356
823
  nameOrConfig:
357
824
  | string
358
- | LoopConfig<any, any>
359
- | ActorWorkflowLoopConfig<
825
+ | WorkflowLoopConfig<
360
826
  any,
361
827
  any,
362
828
  TState,
@@ -369,7 +835,7 @@ export class ActorWorkflowContext<
369
835
  TQueues
370
836
  >,
371
837
  run?: (
372
- ctx: ActorWorkflowContext<
838
+ ctx: WorkflowContext<
373
839
  TState,
374
840
  TConnParams,
375
841
  TConnState,
@@ -379,22 +845,32 @@ export class ActorWorkflowContext<
379
845
  TEvents,
380
846
  TQueues
381
847
  >,
382
- ) => Promise<LoopResult<undefined, any> | void>,
848
+ ) => Promise<LoopResult<undefined, any> | undefined | void>,
383
849
  ): Promise<any> {
384
850
  if (typeof nameOrConfig === "string") {
385
851
  if (!run) {
386
852
  throw new Error("Loop run function missing");
387
853
  }
854
+ const loopRun = run;
388
855
  return await this.#wrapActive(() =>
389
- this.#inner.loop(nameOrConfig, async (ctx) =>
390
- run(this.#createChildContext(ctx)),
856
+ this.#inner.loop(
857
+ nameOrConfig,
858
+ // A void return (no explicit Loop result) is undefined at
859
+ // runtime, which the engine treats as continue.
860
+ async (
861
+ ctx,
862
+ ): Promise<LoopResult<undefined, any> | undefined> =>
863
+ (await loopRun(this.#createChildContext(ctx))) ??
864
+ undefined,
391
865
  ),
392
866
  );
393
867
  }
868
+ const loopConfig = nameOrConfig;
394
869
  const wrapped: LoopConfig<any, any> = {
395
- ...nameOrConfig,
396
- run: async (ctx, state) =>
397
- nameOrConfig.run(this.#createChildContext(ctx), state),
870
+ ...loopConfig,
871
+ run: (async (ctx, state) =>
872
+ (await loopConfig.run(this.#createChildContext(ctx), state)) ??
873
+ undefined) as LoopConfig<any, any>["run"],
398
874
  };
399
875
  return await this.#wrapActive(() => this.#inner.loop(wrapped));
400
876
  }
@@ -407,11 +883,6 @@ export class ActorWorkflowContext<
407
883
  return this.#inner.sleepUntil(name, timestampMs);
408
884
  }
409
885
 
410
- destroy(): void {
411
- this.#ensureActorAccess("destroy");
412
- this.#runCtx.destroy();
413
- }
414
-
415
886
  async rollbackCheckpoint(name: string): Promise<void> {
416
887
  await this.#wrapActive(() => this.#inner.rollbackCheckpoint(name));
417
888
  }
@@ -419,7 +890,7 @@ export class ActorWorkflowContext<
419
890
  async join<
420
891
  T extends Record<
421
892
  string,
422
- ActorWorkflowBranchConfig<
893
+ WorkflowBranchConfig<
423
894
  unknown,
424
895
  TState,
425
896
  TConnParams,
@@ -439,7 +910,10 @@ export class ActorWorkflowContext<
439
910
  name: string,
440
911
  branches: T,
441
912
  ): Promise<{ [K in keyof T]: BranchOutput<T[K]> }>;
442
- async join(name: string, branches: Record<string, BranchConfig<unknown>>) {
913
+ async join(
914
+ name: string,
915
+ branches: Record<string, { run: (ctx: any) => Promise<unknown> }>,
916
+ ) {
443
917
  const wrappedBranches = Object.fromEntries(
444
918
  Object.entries(branches).map(([key, branch]) => [
445
919
  key,
@@ -458,25 +932,17 @@ export class ActorWorkflowContext<
458
932
  name: string,
459
933
  branches: Array<{
460
934
  name: string;
461
- run: (
462
- ctx: ActorWorkflowContext<
463
- TState,
464
- TConnParams,
465
- TConnState,
466
- TVars,
467
- TInput,
468
- TDatabase,
469
- TEvents,
470
- TQueues
471
- >,
472
- ) => Promise<T>;
473
- }>,
474
- ): Promise<{ winner: string; value: T }>;
475
- async race<T>(
476
- name: string,
477
- branches: Array<{
478
- name: string;
479
- run: (ctx: WorkflowContextInterface) => Promise<T>;
935
+ run: WorkflowContextRun<
936
+ T,
937
+ TState,
938
+ TConnParams,
939
+ TConnState,
940
+ TVars,
941
+ TInput,
942
+ TDatabase,
943
+ TEvents,
944
+ TQueues
945
+ >;
480
946
  }>,
481
947
  ): Promise<{ winner: string; value: T }> {
482
948
  const wrappedBranches = branches.map((branch) => ({
@@ -493,86 +959,46 @@ export class ActorWorkflowContext<
493
959
  await this.#wrapActive(() => this.#inner.removed(name, originalType));
494
960
  }
495
961
 
496
- isEvicted(): boolean {
497
- return this.#inner.isEvicted();
498
- }
499
-
500
- get state(): TState extends never ? never : TState {
501
- this.#ensureActorAccess("state");
502
- return this.#runCtx.state as TState extends never ? never : TState;
503
- }
504
-
505
- get vars(): TVars extends never ? never : TVars {
506
- this.#ensureActorAccess("vars");
507
- return this.#runCtx.vars as TVars extends never ? never : TVars;
508
- }
509
-
510
- client<R extends Registry<any> = Registry<any>>(): Client<R> {
511
- this.#ensureActorAccess("client");
512
- return this.#runCtx.client<R>();
513
- }
514
-
515
- get db(): TDatabase extends never ? never : InferDatabaseClient<TDatabase> {
516
- this.#ensureActorAccess("db");
517
- return this.#runCtx.db as TDatabase extends never
518
- ? never
519
- : InferDatabaseClient<TDatabase>;
520
- }
521
-
522
- get log() {
523
- return this.#runCtx.log;
524
- }
525
-
526
- /** @deprecated No-op. Use `keepAwake(promise)` or `waitUntil(promise)` instead. */
527
- setPreventSleep(_prevent: boolean): void {
528
- this.#ensureActorAccess("setPreventSleep");
529
- }
530
-
531
- /** @deprecated No-op. Always returns `false`. */
532
- get preventSleep(): boolean {
533
- this.#ensureActorAccess("preventSleep");
534
- return false;
535
- }
536
-
537
- /**
538
- * Holds the actor awake for the duration of the provided promise. The
539
- * actor cannot idle-sleep or finalize the sleep grace period until the
540
- * promise settles.
541
- */
542
- keepAwake<T>(promise: Promise<T>): Promise<T> {
543
- this.#ensureActorAccess("keepAwake");
544
- return this.#runCtx.keepAwake(promise);
962
+ getVersion(name: string, latest: number): Promise<number> {
963
+ return this.#wrapActive(() => this.#inner.getVersion(name, latest));
545
964
  }
546
965
 
547
- /**
548
- * Registers a promise that the sleep grace period will wait on. Use this
549
- * for best-effort flush/cleanup work that may complete inside the grace
550
- * window. For work the actor must stay running through, prefer
551
- * `c.keepAwake(promise)` which also blocks idle sleep.
552
- */
553
- waitUntil(promise: Promise<void>): void {
554
- this.#ensureActorAccess("waitUntil");
555
- this.#runCtx.waitUntil(promise);
966
+ isEvicted(): boolean {
967
+ return this.#inner.isEvicted();
556
968
  }
557
969
 
558
- get actorId(): string {
559
- return this.#runCtx.actorId;
560
- }
970
+ // Runs a user step body inside a fresh step context and deactivates the step
971
+ // context once the body settles. Workflows are roll-forward only, so state and
972
+ // vars mutations made before a failure always remain visible.
973
+ async #runStep<T>(
974
+ run: WorkflowStepRun<
975
+ T,
976
+ TState,
977
+ TConnParams,
978
+ TConnState,
979
+ TVars,
980
+ TInput,
981
+ TDatabase,
982
+ TEvents,
983
+ TQueues
984
+ >,
985
+ ): Promise<T> {
986
+ const stepCtx = new WorkflowStepContext<
987
+ TState,
988
+ TConnParams,
989
+ TConnState,
990
+ TVars,
991
+ TInput,
992
+ TDatabase,
993
+ TEvents,
994
+ TQueues
995
+ >(this.#runCtx, () => this.#markGuardTriggered());
561
996
 
562
- broadcast<K extends keyof TEvents & string>(
563
- name: K,
564
- ...args: InferEventArgs<InferSchemaMap<TEvents>[K]>
565
- ): void;
566
- broadcast(
567
- name: keyof TEvents extends never ? string : never,
568
- ...args: Array<unknown>
569
- ): void;
570
- broadcast(name: string, ...args: Array<unknown>): void {
571
- this.#ensureActorAccess("broadcast");
572
- this.#runCtx.broadcast(
573
- name as never,
574
- ...(args as unknown[] as never[]),
575
- );
997
+ try {
998
+ return await run(stepCtx);
999
+ } finally {
1000
+ stepCtx[DEACTIVATE_STEP]();
1001
+ }
576
1002
  }
577
1003
 
578
1004
  #toActorQueueMessage<T>(
@@ -593,41 +1019,44 @@ export class ActorWorkflowContext<
593
1019
  };
594
1020
  }
595
1021
 
596
- async #wrapActive<T>(run: () => Promise<T>): Promise<T> {
597
- return await this.#runCtx.internalKeepAwake(run);
598
- }
599
-
600
- async #withActorAccess<T>(run: () => Promise<T>): Promise<T> {
601
- this.#actorAccessDepth++;
602
- if (this.#actorAccessDepth === 1) {
603
- this.#allowActorAccess = true;
604
- }
1022
+ // Runs a step rollback compensation with an active step context.
1023
+ async #runRollback<T>(
1024
+ rollback: WorkflowStepRollback<
1025
+ T,
1026
+ TState,
1027
+ TConnParams,
1028
+ TConnState,
1029
+ TVars,
1030
+ TInput,
1031
+ TDatabase,
1032
+ TEvents,
1033
+ TQueues
1034
+ >,
1035
+ output: T,
1036
+ ): Promise<void> {
1037
+ const stepCtx = new WorkflowStepContext<
1038
+ TState,
1039
+ TConnParams,
1040
+ TConnState,
1041
+ TVars,
1042
+ TInput,
1043
+ TDatabase,
1044
+ TEvents,
1045
+ TQueues
1046
+ >(this.#runCtx, () => this.#markGuardTriggered());
605
1047
  try {
606
- return await run();
1048
+ await rollback(stepCtx, output);
607
1049
  } finally {
608
- this.#actorAccessDepth--;
609
- if (this.#actorAccessDepth === 0) {
610
- this.#allowActorAccess = false;
611
- }
612
- }
613
- }
614
-
615
- #ensureActorAccess(feature: string): void {
616
- if (!this.#allowActorAccess) {
617
- this.#guardViolation = true;
618
- this.#markGuardTriggered();
619
- throw new Error(
620
- `${feature} is only available inside workflow steps`,
621
- );
1050
+ stepCtx[DEACTIVATE_STEP]();
622
1051
  }
623
1052
  }
624
1053
 
625
- consumeGuardViolation(): boolean {
626
- const violated = this.#guardViolation;
627
- this.#guardViolation = false;
628
- return violated;
1054
+ async #wrapActive<T>(run: () => Promise<T>): Promise<T> {
1055
+ return await this.#runCtx.internalKeepAwake(run);
629
1056
  }
630
1057
 
1058
+ // Records that a step context was used outside its step. Mirrors the value
1059
+ // onto actor state and a KV flag so callers can observe the violation.
631
1060
  #markGuardTriggered(): void {
632
1061
  try {
633
1062
  const state = this.#runCtx.state as Record<string, unknown>;
@@ -658,7 +1087,7 @@ export class ActorWorkflowContext<
658
1087
 
659
1088
  #createChildContext(
660
1089
  ctx: WorkflowContextInterface,
661
- ): ActorWorkflowContext<
1090
+ ): WorkflowContext<
662
1091
  TState,
663
1092
  TConnParams,
664
1093
  TConnState,
@@ -668,7 +1097,7 @@ export class ActorWorkflowContext<
668
1097
  TEvents,
669
1098
  TQueues
670
1099
  > {
671
- return new ActorWorkflowContext(ctx, this.#runCtx);
1100
+ return new WorkflowContext(ctx, this.#runCtx);
672
1101
  }
673
1102
  }
674
1103
 
@@ -684,7 +1113,7 @@ export type WorkflowContextOf<AD extends AnyActorDefinition> =
684
1113
  infer Q extends QueueSchemaConfig,
685
1114
  any
686
1115
  >
687
- ? ActorWorkflowContext<S, CP, CS, V, I, DB, E, Q>
1116
+ ? WorkflowContext<S, CP, CS, V, I, DB, E, Q>
688
1117
  : never;
689
1118
 
690
1119
  export type WorkflowLoopContextOf<AD extends AnyActorDefinition> =
@@ -694,4 +1123,16 @@ export type WorkflowBranchContextOf<AD extends AnyActorDefinition> =
694
1123
  WorkflowContextOf<AD>;
695
1124
 
696
1125
  export type WorkflowStepContextOf<AD extends AnyActorDefinition> =
697
- WorkflowContextOf<AD>;
1126
+ AD extends BaseActorDefinition<
1127
+ infer S,
1128
+ infer CP,
1129
+ infer CS,
1130
+ infer V,
1131
+ infer I,
1132
+ infer DB extends AnyDatabaseProvider,
1133
+ infer E extends EventSchemaConfig,
1134
+ infer Q extends QueueSchemaConfig,
1135
+ any
1136
+ >
1137
+ ? WorkflowStepContext<S, CP, CS, V, I, DB, E, Q>
1138
+ : never;