rivetkit 2.3.0-rc.9 → 2.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (226) hide show
  1. package/dist/browser/client.d.ts +511 -62
  2. package/dist/browser/client.js +230 -174
  3. package/dist/browser/client.js.map +1 -1
  4. package/dist/browser/inspector/client.js +53 -23
  5. package/dist/browser/inspector/client.js.map +1 -1
  6. package/dist/tsup/actor/errors.cjs +4 -2
  7. package/dist/tsup/actor/errors.cjs.map +1 -1
  8. package/dist/tsup/actor/errors.d.cts +1 -1
  9. package/dist/tsup/actor/errors.d.ts +1 -1
  10. package/dist/tsup/actor/errors.js +3 -1
  11. package/dist/tsup/agent-os/index.cjs +2163 -2087
  12. package/dist/tsup/agent-os/index.cjs.map +1 -1
  13. package/dist/tsup/agent-os/index.d.cts +509 -69
  14. package/dist/tsup/agent-os/index.d.ts +509 -69
  15. package/dist/tsup/agent-os/index.js +2163 -2087
  16. package/dist/tsup/agent-os/index.js.map +1 -1
  17. package/dist/tsup/{chunk-WQ4HNA4W.cjs → chunk-3MHDOUD7.cjs} +73 -3
  18. package/dist/tsup/chunk-3MHDOUD7.cjs.map +1 -0
  19. package/dist/tsup/{chunk-QAZLM4WT.cjs → chunk-4FC7TVS6.cjs} +8 -4
  20. package/dist/tsup/chunk-4FC7TVS6.cjs.map +1 -0
  21. package/dist/tsup/{chunk-4CGA6QJO.cjs → chunk-4UUEB43Y.cjs} +24 -9
  22. package/dist/tsup/chunk-4UUEB43Y.cjs.map +1 -0
  23. package/dist/tsup/{chunk-GVTOE34S.cjs → chunk-5IWLUJ6W.cjs} +222 -235
  24. package/dist/tsup/chunk-5IWLUJ6W.cjs.map +1 -0
  25. package/dist/tsup/{chunk-MMMEZM5J.js → chunk-H6VVZMWN.js} +4 -4
  26. package/dist/tsup/chunk-H6VVZMWN.js.map +1 -0
  27. package/dist/tsup/{chunk-3YY5S6TV.js → chunk-HXUEHHJF.js} +2 -2
  28. package/dist/tsup/chunk-HXUEHHJF.js.map +1 -0
  29. package/dist/tsup/{chunk-H7P7WR2Y.js → chunk-I35VSLEM.js} +6 -6
  30. package/dist/tsup/chunk-I35VSLEM.js.map +1 -0
  31. package/dist/tsup/{chunk-H37XQU3I.js → chunk-JBUZRPY5.js} +2 -2
  32. package/dist/tsup/{chunk-CPA4Y3RG.cjs → chunk-JLJJZYCJ.cjs} +10 -10
  33. package/dist/tsup/chunk-JLJJZYCJ.cjs.map +1 -0
  34. package/dist/tsup/{chunk-PCBNKI2J.js → chunk-JZ7TWV65.js} +1 -1
  35. package/dist/tsup/chunk-JZ7TWV65.js.map +1 -0
  36. package/dist/tsup/{chunk-VRCIXJRN.js → chunk-L2X3YFER.js} +64 -10
  37. package/dist/tsup/chunk-L2X3YFER.js.map +1 -0
  38. package/dist/tsup/{chunk-Y5NSCZA2.cjs → chunk-MNHKOS6L.cjs} +72 -18
  39. package/dist/tsup/chunk-MNHKOS6L.cjs.map +1 -0
  40. package/dist/tsup/{chunk-KJTA3ATT.js → chunk-NERUIBOT.js} +22 -7
  41. package/dist/tsup/chunk-NERUIBOT.js.map +1 -0
  42. package/dist/tsup/{chunk-4WPEZBK4.cjs → chunk-OST76LRW.cjs} +10 -10
  43. package/dist/tsup/chunk-OST76LRW.cjs.map +1 -0
  44. package/dist/tsup/{chunk-MALSPBAF.cjs → chunk-OZBCXBVP.cjs} +3 -3
  45. package/dist/tsup/{chunk-MALSPBAF.cjs.map → chunk-OZBCXBVP.cjs.map} +1 -1
  46. package/dist/tsup/{chunk-F3Q5BFQ6.js → chunk-PT6OIW5E.js} +66 -79
  47. package/dist/tsup/chunk-PT6OIW5E.js.map +1 -0
  48. package/dist/tsup/{chunk-W7EYSYVI.js → chunk-R6KPN5EW.js} +134 -20
  49. package/dist/tsup/chunk-R6KPN5EW.js.map +1 -0
  50. package/dist/tsup/{chunk-VJFRBJVQ.cjs → chunk-V5KMAMX3.cjs} +138 -24
  51. package/dist/tsup/chunk-V5KMAMX3.cjs.map +1 -0
  52. package/dist/tsup/{chunk-LD5YASJU.cjs → chunk-VE2X4KMG.cjs} +2 -2
  53. package/dist/tsup/{chunk-LD5YASJU.cjs.map → chunk-VE2X4KMG.cjs.map} +1 -1
  54. package/dist/tsup/{chunk-T6YVRM4K.js → chunk-XIX5DOZN.js} +72 -2
  55. package/dist/tsup/chunk-XIX5DOZN.js.map +1 -0
  56. package/dist/tsup/{chunk-2NDZ7JCR.cjs → chunk-ZA7FLHKH.cjs} +1 -1
  57. package/dist/tsup/chunk-ZA7FLHKH.cjs.map +1 -0
  58. package/dist/tsup/{chunk-KIWH5H3K.js → chunk-ZZ3WBRPD.js} +7 -3
  59. package/dist/tsup/chunk-ZZ3WBRPD.js.map +1 -0
  60. package/dist/tsup/client/mod.cjs +9 -9
  61. package/dist/tsup/client/mod.d.cts +5 -5
  62. package/dist/tsup/client/mod.d.ts +5 -5
  63. package/dist/tsup/client/mod.js +8 -8
  64. package/dist/tsup/common/log.cjs +3 -3
  65. package/dist/tsup/common/log.js +2 -2
  66. package/dist/tsup/common/websocket.cjs +4 -4
  67. package/dist/tsup/common/websocket.js +3 -3
  68. package/dist/tsup/{config-Ca8dN4cS.d.cts → config-CzvopP5m.d.cts} +544 -23
  69. package/dist/tsup/{config-CxjGYf4K.d.cts → config-D49x8NpL.d.cts} +1 -2
  70. package/dist/tsup/{config-CxjGYf4K.d.ts → config-D49x8NpL.d.ts} +1 -2
  71. package/dist/tsup/{config-0Ta55UV0.d.ts → config-DZuT7tcp.d.ts} +544 -23
  72. package/dist/tsup/context-CyAdY-aA.d.ts +128 -0
  73. package/dist/tsup/context-sNB28g0N.d.cts +128 -0
  74. package/dist/tsup/db/drizzle.cjs +3 -3
  75. package/dist/tsup/db/drizzle.d.cts +1 -1
  76. package/dist/tsup/db/drizzle.d.ts +1 -1
  77. package/dist/tsup/db/drizzle.js +1 -1
  78. package/dist/tsup/db/mod.cjs +2 -2
  79. package/dist/tsup/db/mod.d.cts +2 -2
  80. package/dist/tsup/db/mod.d.ts +2 -2
  81. package/dist/tsup/db/mod.js +1 -1
  82. package/dist/tsup/dynamic/mod.cjs +24 -0
  83. package/dist/tsup/dynamic/mod.cjs.map +1 -0
  84. package/dist/tsup/dynamic/mod.d.cts +37 -0
  85. package/dist/tsup/dynamic/mod.d.ts +37 -0
  86. package/dist/tsup/dynamic/mod.js +24 -0
  87. package/dist/tsup/dynamic/mod.js.map +1 -0
  88. package/dist/tsup/inspector/mod.cjs +6 -6
  89. package/dist/tsup/inspector/mod.js +5 -5
  90. package/dist/tsup/inspector-tab/mod.cjs +173 -0
  91. package/dist/tsup/inspector-tab/mod.cjs.map +1 -0
  92. package/dist/tsup/inspector-tab/mod.d.cts +250 -0
  93. package/dist/tsup/inspector-tab/mod.d.ts +250 -0
  94. package/dist/tsup/inspector-tab/mod.js +173 -0
  95. package/dist/tsup/inspector-tab/mod.js.map +1 -0
  96. package/dist/tsup/mod.cjs +758 -348
  97. package/dist/tsup/mod.cjs.map +1 -1
  98. package/dist/tsup/mod.d.cts +5 -5
  99. package/dist/tsup/mod.d.ts +5 -5
  100. package/dist/tsup/mod.js +662 -252
  101. package/dist/tsup/mod.js.map +1 -1
  102. package/dist/tsup/test/mod.cjs +21 -18
  103. package/dist/tsup/test/mod.cjs.map +1 -1
  104. package/dist/tsup/test/mod.d.cts +4 -4
  105. package/dist/tsup/test/mod.d.ts +4 -4
  106. package/dist/tsup/test/mod.js +18 -15
  107. package/dist/tsup/test/mod.js.map +1 -1
  108. package/dist/tsup/{utils-DVekpm4I.d.cts → utils-CqDnC_PS.d.cts} +2 -1
  109. package/dist/tsup/{utils-DVekpm4I.d.ts → utils-CqDnC_PS.d.ts} +2 -1
  110. package/dist/tsup/utils.cjs +3 -3
  111. package/dist/tsup/utils.d.cts +1 -1
  112. package/dist/tsup/utils.d.ts +1 -1
  113. package/dist/tsup/utils.js +2 -2
  114. package/dist/tsup/workflow/mod.cjs +383 -322
  115. package/dist/tsup/workflow/mod.cjs.map +1 -1
  116. package/dist/tsup/workflow/mod.d.cts +8 -8
  117. package/dist/tsup/workflow/mod.d.ts +8 -8
  118. package/dist/tsup/workflow/mod.js +360 -299
  119. package/dist/tsup/workflow/mod.js.map +1 -1
  120. package/package.json +35 -14
  121. package/src/actor/config.ts +173 -51
  122. package/src/actor/contexts/index.ts +7 -2
  123. package/src/actor/definition.ts +17 -19
  124. package/src/actor/driver.ts +3 -3
  125. package/src/actor/errors.ts +20 -3
  126. package/src/actor/instance/mod.ts +26 -34
  127. package/src/actor/keys.ts +1 -1
  128. package/src/actor/mod.ts +22 -20
  129. package/src/actor/schema.ts +2 -2
  130. package/src/agent-os/actor/index.ts +38 -18
  131. package/src/agent-os/actor/preview.ts +1 -2
  132. package/src/agent-os/actor/session.ts +2 -2
  133. package/src/agent-os/config.ts +1 -1
  134. package/src/agent-os/fs/database-vfs.ts +1 -1
  135. package/src/agent-os/index.ts +16 -15
  136. package/src/client/actor-common.ts +87 -54
  137. package/src/client/actor-conn.ts +8 -36
  138. package/src/client/actor-handle.ts +69 -51
  139. package/src/client/actor-query.ts +5 -5
  140. package/src/client/errors.ts +1 -1
  141. package/src/client/lifecycle-errors.ts +2 -4
  142. package/src/client/query.ts +1 -1
  143. package/src/client/queue.ts +8 -3
  144. package/src/client/raw-utils.ts +8 -6
  145. package/src/client/resolve-gateway-target.ts +1 -1
  146. package/src/client/utils.ts +2 -7
  147. package/src/common/actor-websocket.ts +3 -1
  148. package/src/common/bare/actor-persist/v1.ts +205 -163
  149. package/src/common/bare/actor-persist/v2.ts +265 -213
  150. package/src/common/bare/actor-persist/v3.ts +176 -172
  151. package/src/common/bare/actor-persist/v4.ts +254 -253
  152. package/src/common/bare/transport/v1.ts +659 -543
  153. package/src/common/client-protocol-versioned.ts +66 -64
  154. package/src/common/database/config.ts +2 -8
  155. package/src/common/database/native-database.ts +1 -1
  156. package/src/common/database/shared.ts +1 -0
  157. package/src/common/encoding.ts +250 -16
  158. package/src/common/engine.ts +28 -1
  159. package/src/common/eventsource.ts +1 -1
  160. package/src/common/inline-websocket-adapter.ts +14 -13
  161. package/src/common/log.ts +1 -0
  162. package/src/common/router.ts +13 -17
  163. package/src/common/utils.ts +1 -150
  164. package/src/common/websocket-interface.ts +1 -1
  165. package/src/db/mod.ts +1 -1
  166. package/src/devtools-loader/index.ts +4 -7
  167. package/src/devtools-loader/serve-devtools.ts +26 -0
  168. package/src/drivers/engine/actor-driver.ts +58 -56
  169. package/src/dynamic/instance.ts +32 -0
  170. package/src/dynamic/internal.ts +50 -0
  171. package/src/dynamic/isolate-runtime.ts +66 -0
  172. package/src/dynamic/mod.ts +32 -0
  173. package/src/engine-client/actor-http-client.ts +3 -3
  174. package/src/engine-client/actor-websocket-client.ts +6 -5
  175. package/src/engine-client/api-endpoints.ts +51 -2
  176. package/src/engine-client/api-utils.ts +2 -2
  177. package/src/engine-client/driver.ts +1 -1
  178. package/src/engine-client/mod.ts +6 -3
  179. package/src/engine-client/ws-proxy.ts +9 -4
  180. package/src/inspector/client.browser.ts +5 -11
  181. package/src/inspector/mod.ts +1 -3
  182. package/src/inspector-tab/mod.ts +315 -0
  183. package/src/registry/config/envoy.ts +1 -2
  184. package/src/registry/config/index.ts +40 -16
  185. package/src/registry/index.ts +209 -73
  186. package/src/registry/napi-runtime.ts +29 -2
  187. package/src/registry/native-validation.ts +10 -12
  188. package/src/registry/native.ts +433 -198
  189. package/src/registry/process-metrics.ts +250 -0
  190. package/src/registry/runtime.ts +52 -1
  191. package/src/registry/wasm-runtime.ts +29 -2
  192. package/src/registry/write-through-proxy.ts +40 -0
  193. package/src/serde.ts +2 -2
  194. package/src/serverless/configure.ts +18 -7
  195. package/src/test/mod.ts +11 -8
  196. package/src/utils/endpoint-parser.ts +1 -1
  197. package/src/utils/env-vars.ts +37 -0
  198. package/src/utils/router.ts +1 -1
  199. package/src/utils.ts +1 -2
  200. package/src/workflow/context.ts +699 -240
  201. package/src/workflow/driver.ts +23 -12
  202. package/src/workflow/inspector.ts +4 -3
  203. package/src/workflow/mod.ts +37 -23
  204. package/dist/tsup/chunk-2NDZ7JCR.cjs.map +0 -1
  205. package/dist/tsup/chunk-3YY5S6TV.js.map +0 -1
  206. package/dist/tsup/chunk-4CGA6QJO.cjs.map +0 -1
  207. package/dist/tsup/chunk-4WPEZBK4.cjs.map +0 -1
  208. package/dist/tsup/chunk-CPA4Y3RG.cjs.map +0 -1
  209. package/dist/tsup/chunk-F3Q5BFQ6.js.map +0 -1
  210. package/dist/tsup/chunk-GVTOE34S.cjs.map +0 -1
  211. package/dist/tsup/chunk-H7P7WR2Y.js.map +0 -1
  212. package/dist/tsup/chunk-KIWH5H3K.js.map +0 -1
  213. package/dist/tsup/chunk-KJTA3ATT.js.map +0 -1
  214. package/dist/tsup/chunk-MMMEZM5J.js.map +0 -1
  215. package/dist/tsup/chunk-PCBNKI2J.js.map +0 -1
  216. package/dist/tsup/chunk-QAZLM4WT.cjs.map +0 -1
  217. package/dist/tsup/chunk-T6YVRM4K.js.map +0 -1
  218. package/dist/tsup/chunk-VJFRBJVQ.cjs.map +0 -1
  219. package/dist/tsup/chunk-VRCIXJRN.js.map +0 -1
  220. package/dist/tsup/chunk-W7EYSYVI.js.map +0 -1
  221. package/dist/tsup/chunk-WQ4HNA4W.cjs.map +0 -1
  222. package/dist/tsup/chunk-Y5NSCZA2.cjs.map +0 -1
  223. package/dist/tsup/context-B_IWbWne.d.ts +0 -92
  224. package/dist/tsup/context-CUrQ9MHc.d.cts +0 -92
  225. package/src/utils/serve.ts +0 -217
  226. /package/dist/tsup/{chunk-H37XQU3I.js.map → chunk-JBUZRPY5.js.map} +0 -0
@@ -1,22 +1,24 @@
1
1
  import {
2
2
  createWorkflowInspectorAdapter
3
- } from "../chunk-MMMEZM5J.js";
3
+ } from "../chunk-H6VVZMWN.js";
4
+ import "../chunk-NERUIBOT.js";
4
5
  import {
5
6
  ACTOR_CONTEXT_INTERNAL_SYMBOL,
7
+ RAW_STATE_SYMBOL,
6
8
  RUN_FUNCTION_CONFIG_SYMBOL
7
- } from "../chunk-T6YVRM4K.js";
8
- import "../chunk-KJTA3ATT.js";
9
+ } from "../chunk-XIX5DOZN.js";
9
10
  import {
10
11
  makeWorkflowKey,
11
12
  workflowStoragePrefix
12
- } from "../chunk-3YY5S6TV.js";
13
- import "../chunk-W7EYSYVI.js";
13
+ } from "../chunk-HXUEHHJF.js";
14
+ import "../chunk-R6KPN5EW.js";
14
15
  import {
15
16
  stringifyError
16
- } from "../chunk-VRCIXJRN.js";
17
+ } from "../chunk-L2X3YFER.js";
17
18
  import {
18
- RivetError
19
- } from "../chunk-KIWH5H3K.js";
19
+ RivetError,
20
+ isActorAbortedError
21
+ } from "../chunk-ZZ3WBRPD.js";
20
22
 
21
23
  // src/workflow/mod.ts
22
24
  import {
@@ -25,14 +27,351 @@ import {
25
27
  HistoryDivergedError,
26
28
  JoinError,
27
29
  RaceError,
28
- replayWorkflowFromStep,
29
30
  RollbackCheckpointError,
30
31
  RollbackError,
32
+ replayWorkflowFromStep,
31
33
  runWorkflow,
32
34
  StepExhaustedError
33
35
  } from "@rivetkit/workflow-engine";
34
36
  import invariant from "invariant";
35
37
 
38
+ // src/workflow/constants.ts
39
+ var WORKFLOW_GUARD_KV_KEY = "__rivet_actor_workflow_guard_triggered";
40
+
41
+ // src/workflow/context.ts
42
+ var DEACTIVATE_STEP = /* @__PURE__ */ Symbol("workflow.step.deactivate");
43
+ var WorkflowStepContext = class {
44
+ #runCtx;
45
+ #active = true;
46
+ #onGuardViolation;
47
+ constructor(runCtx, onGuardViolation) {
48
+ this.#runCtx = runCtx;
49
+ this.#onGuardViolation = onGuardViolation;
50
+ }
51
+ [DEACTIVATE_STEP]() {
52
+ this.#active = false;
53
+ }
54
+ #ensureActive(feature) {
55
+ if (!this.#active) {
56
+ this.#onGuardViolation();
57
+ throw new Error(
58
+ `${feature} is only available inside workflow steps`
59
+ );
60
+ }
61
+ }
62
+ get actorId() {
63
+ return this.#runCtx.actorId;
64
+ }
65
+ get name() {
66
+ return this.#runCtx.name;
67
+ }
68
+ get key() {
69
+ return this.#runCtx.key;
70
+ }
71
+ get log() {
72
+ return this.#runCtx.log;
73
+ }
74
+ get abortSignal() {
75
+ return this.#runCtx.abortSignal;
76
+ }
77
+ get state() {
78
+ this.#ensureActive("state");
79
+ return this.#runCtx.state;
80
+ }
81
+ get vars() {
82
+ this.#ensureActive("vars");
83
+ return this.#runCtx.vars;
84
+ }
85
+ get db() {
86
+ this.#ensureActive("db");
87
+ return this.#runCtx.db;
88
+ }
89
+ client() {
90
+ this.#ensureActive("client");
91
+ return this.#runCtx.client();
92
+ }
93
+ broadcast(name, ...args) {
94
+ this.#ensureActive("broadcast");
95
+ this.#runCtx.broadcast(
96
+ name,
97
+ ...args
98
+ );
99
+ }
100
+ get queue() {
101
+ const self = this;
102
+ async function send(name, body) {
103
+ self.#ensureActive("queue.send");
104
+ await self.#runCtx.queue.send(name, body);
105
+ }
106
+ return { send };
107
+ }
108
+ /**
109
+ * Holds the actor awake for the duration of the provided promise. The actor
110
+ * cannot idle-sleep or finalize the sleep grace period until the promise
111
+ * settles.
112
+ */
113
+ keepAwake(promise) {
114
+ this.#ensureActive("keepAwake");
115
+ return this.#runCtx.keepAwake(promise);
116
+ }
117
+ /**
118
+ * Registers a promise that the sleep grace period will wait on. Use this for
119
+ * best-effort flush/cleanup work that may complete inside the grace window.
120
+ */
121
+ waitUntil(promise) {
122
+ this.#ensureActive("waitUntil");
123
+ this.#runCtx.waitUntil(promise);
124
+ }
125
+ destroy() {
126
+ this.#ensureActive("destroy");
127
+ this.#runCtx.destroy();
128
+ }
129
+ };
130
+ var WorkflowContext = class _WorkflowContext {
131
+ #inner;
132
+ #runCtx;
133
+ constructor(inner, runCtx) {
134
+ this.#inner = inner;
135
+ this.#runCtx = runCtx;
136
+ }
137
+ get workflowId() {
138
+ return this.#inner.workflowId;
139
+ }
140
+ get abortSignal() {
141
+ return this.#inner.abortSignal;
142
+ }
143
+ get actorId() {
144
+ return this.#runCtx.actorId;
145
+ }
146
+ get name() {
147
+ return this.#runCtx.name;
148
+ }
149
+ get key() {
150
+ return this.#runCtx.key;
151
+ }
152
+ get log() {
153
+ return this.#runCtx.log;
154
+ }
155
+ get queue() {
156
+ const self = this;
157
+ async function next(name, opts) {
158
+ const message = await self.#inner.queue.next(name, opts);
159
+ return self.#toActorQueueMessage(message);
160
+ }
161
+ async function nextBatch(name, opts) {
162
+ const messages = await self.#inner.queue.nextBatch(name, opts);
163
+ return messages.map(
164
+ (message) => self.#toActorQueueMessage(message)
165
+ );
166
+ }
167
+ return {
168
+ next,
169
+ nextBatch
170
+ };
171
+ }
172
+ async step(nameOrConfig, run) {
173
+ if (typeof nameOrConfig === "string") {
174
+ if (!run) {
175
+ throw new Error("Step run function missing");
176
+ }
177
+ const stepRun = run;
178
+ return await this.#wrapActive(
179
+ () => this.#inner.step(nameOrConfig, () => this.#runStep(stepRun))
180
+ );
181
+ }
182
+ const stepConfig = nameOrConfig;
183
+ const rollback = stepConfig.rollback;
184
+ const config = {
185
+ ...stepConfig,
186
+ run: () => this.#runStep(stepConfig.run),
187
+ rollback: rollback ? (_ctx, output) => this.#runRollback(rollback, output) : void 0
188
+ };
189
+ return await this.#wrapActive(() => this.#inner.step(config));
190
+ }
191
+ async tryStep(nameOrConfig, run) {
192
+ if (typeof nameOrConfig === "string") {
193
+ if (!run) {
194
+ throw new Error("Step run function missing");
195
+ }
196
+ const stepRun = run;
197
+ return await this.#wrapActive(
198
+ () => this.#inner.tryStep(nameOrConfig, () => this.#runStep(stepRun))
199
+ );
200
+ }
201
+ const stepConfig = nameOrConfig;
202
+ const rollback = stepConfig.rollback;
203
+ const config = {
204
+ ...stepConfig,
205
+ run: () => this.#runStep(stepConfig.run),
206
+ rollback: rollback ? (_ctx, output) => this.#runRollback(rollback, output) : void 0
207
+ };
208
+ return await this.#wrapActive(() => this.#inner.tryStep(config));
209
+ }
210
+ async try(nameOrConfig, run) {
211
+ if (typeof nameOrConfig === "string") {
212
+ if (!run) {
213
+ throw new Error("Try run function missing");
214
+ }
215
+ const tryRun = run;
216
+ return await this.#wrapActive(
217
+ () => this.#inner.try(
218
+ nameOrConfig,
219
+ async (ctx) => tryRun(this.#createChildContext(ctx))
220
+ )
221
+ );
222
+ }
223
+ const tryConfig = nameOrConfig;
224
+ const config = {
225
+ ...tryConfig,
226
+ run: async (ctx) => tryConfig.run(this.#createChildContext(ctx))
227
+ };
228
+ return await this.#wrapActive(() => this.#inner.try(config));
229
+ }
230
+ async loop(nameOrConfig, run) {
231
+ if (typeof nameOrConfig === "string") {
232
+ if (!run) {
233
+ throw new Error("Loop run function missing");
234
+ }
235
+ const loopRun = run;
236
+ return await this.#wrapActive(
237
+ () => this.#inner.loop(
238
+ nameOrConfig,
239
+ // A void return (no explicit Loop result) is undefined at
240
+ // runtime, which the engine treats as continue.
241
+ async (ctx) => await loopRun(this.#createChildContext(ctx)) ?? void 0
242
+ )
243
+ );
244
+ }
245
+ const loopConfig = nameOrConfig;
246
+ const wrapped = {
247
+ ...loopConfig,
248
+ run: (async (ctx, state) => await loopConfig.run(this.#createChildContext(ctx), state) ?? void 0)
249
+ };
250
+ return await this.#wrapActive(() => this.#inner.loop(wrapped));
251
+ }
252
+ sleep(name, durationMs) {
253
+ return this.#inner.sleep(name, durationMs);
254
+ }
255
+ sleepUntil(name, timestampMs) {
256
+ return this.#inner.sleepUntil(name, timestampMs);
257
+ }
258
+ async rollbackCheckpoint(name) {
259
+ await this.#wrapActive(() => this.#inner.rollbackCheckpoint(name));
260
+ }
261
+ async join(name, branches) {
262
+ const wrappedBranches = Object.fromEntries(
263
+ Object.entries(branches).map(([key, branch]) => [
264
+ key,
265
+ {
266
+ run: async (ctx) => branch.run(this.#createChildContext(ctx))
267
+ }
268
+ ])
269
+ );
270
+ return await this.#wrapActive(
271
+ () => this.#inner.join(name, wrappedBranches)
272
+ );
273
+ }
274
+ async race(name, branches) {
275
+ const wrappedBranches = branches.map((branch) => ({
276
+ name: branch.name,
277
+ run: (ctx) => branch.run(this.#createChildContext(ctx))
278
+ }));
279
+ return await this.#wrapActive(
280
+ () => this.#inner.race(name, wrappedBranches)
281
+ );
282
+ }
283
+ async removed(name, originalType) {
284
+ await this.#wrapActive(() => this.#inner.removed(name, originalType));
285
+ }
286
+ isEvicted() {
287
+ return this.#inner.isEvicted();
288
+ }
289
+ // Runs a user step body inside a fresh step context, snapshotting actor
290
+ // state/vars so a thrown step rolls back its mutations, and deactivating the
291
+ // step context once the body settles so it cannot be used after the step.
292
+ async #runStep(run) {
293
+ const stepCtx = new WorkflowStepContext(this.#runCtx, () => this.#markGuardTriggered());
294
+ let stateSnapshot = null;
295
+ try {
296
+ stateSnapshot = { state: this.#runCtx[RAW_STATE_SYMBOL]() };
297
+ } catch (error) {
298
+ this.#runCtx.log.debug({
299
+ msg: "failed to get state, likely due to being stateless workflow",
300
+ error
301
+ });
302
+ }
303
+ if (stateSnapshot) {
304
+ stateSnapshot.state = structuredClone(stateSnapshot.state);
305
+ }
306
+ const varsSnapshot = structuredClone(this.#runCtx.vars);
307
+ try {
308
+ return await run(stepCtx);
309
+ } catch (error) {
310
+ if (stateSnapshot) {
311
+ this.#runCtx.state = stateSnapshot.state;
312
+ }
313
+ this.#runCtx.vars = varsSnapshot;
314
+ throw error;
315
+ } finally {
316
+ stepCtx[DEACTIVATE_STEP]();
317
+ }
318
+ }
319
+ #toActorQueueMessage(message) {
320
+ let id;
321
+ try {
322
+ id = BigInt(message.id);
323
+ } catch {
324
+ throw new Error(`Invalid queue message id "${message.id}"`);
325
+ }
326
+ return {
327
+ id,
328
+ name: message.name,
329
+ body: message.body,
330
+ createdAt: message.createdAt,
331
+ ...message.complete ? { complete: message.complete } : {}
332
+ };
333
+ }
334
+ // Runs a step rollback compensation with an active step context. Rollbacks
335
+ // intentionally mutate actor state, so their writes are not snapshotted.
336
+ async #runRollback(rollback, output) {
337
+ const stepCtx = new WorkflowStepContext(this.#runCtx, () => this.#markGuardTriggered());
338
+ try {
339
+ await rollback(stepCtx, output);
340
+ } finally {
341
+ stepCtx[DEACTIVATE_STEP]();
342
+ }
343
+ }
344
+ async #wrapActive(run) {
345
+ return await this.#runCtx.internalKeepAwake(run);
346
+ }
347
+ // Records that a step context was used outside its step. Mirrors the value
348
+ // onto actor state and a KV flag so callers can observe the violation.
349
+ #markGuardTriggered() {
350
+ try {
351
+ const state = this.#runCtx.state;
352
+ if (state && typeof state === "object" && "guardTriggered" in state) {
353
+ state.guardTriggered = true;
354
+ }
355
+ } catch {
356
+ }
357
+ this.#runCtx.waitUntil(
358
+ (async () => {
359
+ try {
360
+ await this.#runCtx.kv.put(WORKFLOW_GUARD_KV_KEY, "true");
361
+ } catch (error) {
362
+ this.#runCtx.log.error({
363
+ msg: "failed to persist workflow guard flag",
364
+ error
365
+ });
366
+ }
367
+ })()
368
+ );
369
+ }
370
+ #createChildContext(ctx) {
371
+ return new _WorkflowContext(ctx, this.#runCtx);
372
+ }
373
+ };
374
+
36
375
  // src/workflow/driver.ts
37
376
  var WORKFLOW_STORAGE_PREFIX = workflowStoragePrefix();
38
377
  function stripWorkflowKey(prefixed) {
@@ -296,293 +635,6 @@ var ActorWorkflowControlDriver = class {
296
635
  }
297
636
  };
298
637
 
299
- // src/workflow/constants.ts
300
- var WORKFLOW_GUARD_KV_KEY = "__rivet_actor_workflow_guard_triggered";
301
-
302
- // src/workflow/context.ts
303
- var ActorWorkflowContext = class _ActorWorkflowContext {
304
- #inner;
305
- #runCtx;
306
- #actorAccessDepth = 0;
307
- #allowActorAccess = false;
308
- #guardViolation = false;
309
- constructor(inner, runCtx) {
310
- this.#inner = inner;
311
- this.#runCtx = runCtx;
312
- }
313
- get workflowId() {
314
- return this.#inner.workflowId;
315
- }
316
- get abortSignal() {
317
- return this.#inner.abortSignal;
318
- }
319
- get queue() {
320
- const self = this;
321
- async function next(name, opts) {
322
- const message = await self.#inner.queue.next(name, opts);
323
- return self.#toActorQueueMessage(message);
324
- }
325
- async function nextBatch(name, opts) {
326
- const messages = await self.#inner.queue.nextBatch(name, opts);
327
- return messages.map(
328
- (message) => self.#toActorQueueMessage(message)
329
- );
330
- }
331
- async function send(name, body) {
332
- self.#ensureActorAccess("queue.send");
333
- await self.#runCtx.queue.send(name, body);
334
- }
335
- return {
336
- next,
337
- nextBatch,
338
- send
339
- };
340
- }
341
- async step(nameOrConfig, run) {
342
- if (typeof nameOrConfig === "string") {
343
- if (!run) {
344
- throw new Error("Step run function missing");
345
- }
346
- return await this.#wrapActive(
347
- () => this.#inner.step(
348
- nameOrConfig,
349
- () => this.#withActorAccess(run)
350
- )
351
- );
352
- }
353
- const stepConfig = nameOrConfig;
354
- const config = {
355
- ...stepConfig,
356
- run: () => this.#withActorAccess(stepConfig.run)
357
- };
358
- return await this.#wrapActive(() => this.#inner.step(config));
359
- }
360
- async tryStep(nameOrConfig, run) {
361
- if (typeof nameOrConfig === "string") {
362
- if (!run) {
363
- throw new Error("Step run function missing");
364
- }
365
- return await this.#wrapActive(
366
- () => this.#inner.tryStep(
367
- nameOrConfig,
368
- () => this.#withActorAccess(run)
369
- )
370
- );
371
- }
372
- const stepConfig = nameOrConfig;
373
- const config = {
374
- ...stepConfig,
375
- run: () => this.#withActorAccess(stepConfig.run)
376
- };
377
- return await this.#wrapActive(() => this.#inner.tryStep(config));
378
- }
379
- async try(nameOrConfig, run) {
380
- if (typeof nameOrConfig === "string") {
381
- if (!run) {
382
- throw new Error("Try run function missing");
383
- }
384
- return await this.#wrapActive(
385
- () => this.#inner.try(
386
- nameOrConfig,
387
- async (ctx) => run(this.#createChildContext(ctx))
388
- )
389
- );
390
- }
391
- const tryConfig = nameOrConfig;
392
- const config = {
393
- ...tryConfig,
394
- run: async (ctx) => tryConfig.run(this.#createChildContext(ctx))
395
- };
396
- return await this.#wrapActive(() => this.#inner.try(config));
397
- }
398
- async loop(nameOrConfig, run) {
399
- if (typeof nameOrConfig === "string") {
400
- if (!run) {
401
- throw new Error("Loop run function missing");
402
- }
403
- return await this.#wrapActive(
404
- () => this.#inner.loop(
405
- nameOrConfig,
406
- async (ctx) => run(this.#createChildContext(ctx))
407
- )
408
- );
409
- }
410
- const wrapped = {
411
- ...nameOrConfig,
412
- run: async (ctx, state) => nameOrConfig.run(this.#createChildContext(ctx), state)
413
- };
414
- return await this.#wrapActive(() => this.#inner.loop(wrapped));
415
- }
416
- sleep(name, durationMs) {
417
- return this.#inner.sleep(name, durationMs);
418
- }
419
- sleepUntil(name, timestampMs) {
420
- return this.#inner.sleepUntil(name, timestampMs);
421
- }
422
- destroy() {
423
- this.#ensureActorAccess("destroy");
424
- this.#runCtx.destroy();
425
- }
426
- async rollbackCheckpoint(name) {
427
- await this.#wrapActive(() => this.#inner.rollbackCheckpoint(name));
428
- }
429
- async join(name, branches) {
430
- const wrappedBranches = Object.fromEntries(
431
- Object.entries(branches).map(([key, branch]) => [
432
- key,
433
- {
434
- run: async (ctx) => branch.run(this.#createChildContext(ctx))
435
- }
436
- ])
437
- );
438
- return await this.#wrapActive(
439
- () => this.#inner.join(name, wrappedBranches)
440
- );
441
- }
442
- async race(name, branches) {
443
- const wrappedBranches = branches.map((branch) => ({
444
- name: branch.name,
445
- run: (ctx) => branch.run(this.#createChildContext(ctx))
446
- }));
447
- return await this.#wrapActive(
448
- () => this.#inner.race(name, wrappedBranches)
449
- );
450
- }
451
- async removed(name, originalType) {
452
- await this.#wrapActive(() => this.#inner.removed(name, originalType));
453
- }
454
- isEvicted() {
455
- return this.#inner.isEvicted();
456
- }
457
- get state() {
458
- this.#ensureActorAccess("state");
459
- return this.#runCtx.state;
460
- }
461
- get vars() {
462
- this.#ensureActorAccess("vars");
463
- return this.#runCtx.vars;
464
- }
465
- client() {
466
- this.#ensureActorAccess("client");
467
- return this.#runCtx.client();
468
- }
469
- get db() {
470
- this.#ensureActorAccess("db");
471
- return this.#runCtx.db;
472
- }
473
- get log() {
474
- return this.#runCtx.log;
475
- }
476
- /** @deprecated No-op. Use `keepAwake(promise)` or `waitUntil(promise)` instead. */
477
- setPreventSleep(_prevent) {
478
- this.#ensureActorAccess("setPreventSleep");
479
- }
480
- /** @deprecated No-op. Always returns `false`. */
481
- get preventSleep() {
482
- this.#ensureActorAccess("preventSleep");
483
- return false;
484
- }
485
- /**
486
- * Holds the actor awake for the duration of the provided promise. The
487
- * actor cannot idle-sleep or finalize the sleep grace period until the
488
- * promise settles.
489
- */
490
- keepAwake(promise) {
491
- this.#ensureActorAccess("keepAwake");
492
- return this.#runCtx.keepAwake(promise);
493
- }
494
- /**
495
- * Registers a promise that the sleep grace period will wait on. Use this
496
- * for best-effort flush/cleanup work that may complete inside the grace
497
- * window. For work the actor must stay running through, prefer
498
- * `c.keepAwake(promise)` which also blocks idle sleep.
499
- */
500
- waitUntil(promise) {
501
- this.#ensureActorAccess("waitUntil");
502
- this.#runCtx.waitUntil(promise);
503
- }
504
- get actorId() {
505
- return this.#runCtx.actorId;
506
- }
507
- broadcast(name, ...args) {
508
- this.#ensureActorAccess("broadcast");
509
- this.#runCtx.broadcast(
510
- name,
511
- ...args
512
- );
513
- }
514
- #toActorQueueMessage(message) {
515
- let id;
516
- try {
517
- id = BigInt(message.id);
518
- } catch {
519
- throw new Error(`Invalid queue message id "${message.id}"`);
520
- }
521
- return {
522
- id,
523
- name: message.name,
524
- body: message.body,
525
- createdAt: message.createdAt,
526
- ...message.complete ? { complete: message.complete } : {}
527
- };
528
- }
529
- async #wrapActive(run) {
530
- return await this.#runCtx.internalKeepAwake(run);
531
- }
532
- async #withActorAccess(run) {
533
- this.#actorAccessDepth++;
534
- if (this.#actorAccessDepth === 1) {
535
- this.#allowActorAccess = true;
536
- }
537
- try {
538
- return await run();
539
- } finally {
540
- this.#actorAccessDepth--;
541
- if (this.#actorAccessDepth === 0) {
542
- this.#allowActorAccess = false;
543
- }
544
- }
545
- }
546
- #ensureActorAccess(feature) {
547
- if (!this.#allowActorAccess) {
548
- this.#guardViolation = true;
549
- this.#markGuardTriggered();
550
- throw new Error(
551
- `${feature} is only available inside workflow steps`
552
- );
553
- }
554
- }
555
- consumeGuardViolation() {
556
- const violated = this.#guardViolation;
557
- this.#guardViolation = false;
558
- return violated;
559
- }
560
- #markGuardTriggered() {
561
- try {
562
- const state = this.#runCtx.state;
563
- if (state && typeof state === "object" && "guardTriggered" in state) {
564
- state.guardTriggered = true;
565
- }
566
- } catch {
567
- }
568
- this.#runCtx.waitUntil(
569
- (async () => {
570
- try {
571
- await this.#runCtx.kv.put(WORKFLOW_GUARD_KV_KEY, "true");
572
- } catch (error) {
573
- this.#runCtx.log.error({
574
- msg: "failed to persist workflow guard flag",
575
- error
576
- });
577
- }
578
- })()
579
- );
580
- }
581
- #createChildContext(ctx) {
582
- return new _ActorWorkflowContext(ctx, this.#runCtx);
583
- }
584
- };
585
-
586
638
  // src/workflow/mod.ts
587
639
  import { Loop } from "@rivetkit/workflow-engine";
588
640
  function shouldRethrowWorkflowError(error) {
@@ -650,11 +702,13 @@ function workflow(fn, options = {}) {
650
702
  });
651
703
  const handle = runWorkflow(
652
704
  actor.id,
653
- async (ctx) => await fn(new ActorWorkflowContext(ctx, runCtx)),
705
+ async (ctx) => await fn(new WorkflowContext(ctx, runCtx)),
654
706
  void 0,
655
707
  driver,
656
708
  {
657
709
  mode: "live",
710
+ // The actor logger and the engine's pino logger are runtime
711
+ // compatible but not structurally assignable.
658
712
  logger: runCtx.log,
659
713
  onHistoryUpdated: workflowInspector.update,
660
714
  onError: onError ? async (event) => await onError(runCtx, event) : void 0
@@ -674,7 +728,7 @@ function workflow(fn, options = {}) {
674
728
  try {
675
729
  await handle.result;
676
730
  } catch (error) {
677
- if (runCtx.abortSignal.aborted) {
731
+ if (runCtx.abortSignal.aborted || isActorAbortedError(error)) {
678
732
  return;
679
733
  }
680
734
  if (shouldRethrowWorkflowError(error)) {
@@ -695,6 +749,12 @@ function workflow(fn, options = {}) {
695
749
  const runWithConfig = run;
696
750
  runWithConfig[RUN_FUNCTION_CONFIG_SYMBOL] = {
697
751
  icon: "diagram-project",
752
+ // Drop the per-actor inspector when the actor is destroyed so this map
753
+ // does not retain one inspector (and its encoded history) per actor id
754
+ // for the process lifetime.
755
+ disposeInspector: (actorId) => {
756
+ workflowInspectors.delete(actorId);
757
+ },
698
758
  inspectorFactory: (actor) => {
699
759
  const actorId = resolveWorkflowInspectorActorId(actor);
700
760
  return {
@@ -726,8 +786,9 @@ function resolveWorkflowInspectorActorId(actor) {
726
786
  return void 0;
727
787
  }
728
788
  export {
729
- ActorWorkflowContext,
730
789
  Loop,
790
+ WorkflowContext,
791
+ WorkflowStepContext,
731
792
  workflow
732
793
  };
733
794
  //# sourceMappingURL=mod.js.map