rivetkit 2.3.0 → 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 (120) hide show
  1. package/dist/browser/client.d.ts +19 -6
  2. package/dist/browser/client.js +4 -4
  3. package/dist/browser/client.js.map +1 -1
  4. package/dist/browser/inspector/client.js +4 -4
  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 +4 -4
  12. package/dist/tsup/agent-os/index.cjs.map +1 -1
  13. package/dist/tsup/agent-os/index.d.cts +18 -5
  14. package/dist/tsup/agent-os/index.d.ts +18 -5
  15. package/dist/tsup/agent-os/index.js +4 -4
  16. package/dist/tsup/agent-os/index.js.map +1 -1
  17. package/dist/tsup/{chunk-NIY3RSPX.cjs → chunk-3MHDOUD7.cjs} +11 -2
  18. package/dist/tsup/chunk-3MHDOUD7.cjs.map +1 -0
  19. package/dist/tsup/{chunk-KORQB2IR.cjs → chunk-4FC7TVS6.cjs} +6 -2
  20. package/dist/tsup/chunk-4FC7TVS6.cjs.map +1 -0
  21. package/dist/tsup/{chunk-47HHIEXH.cjs → chunk-4UUEB43Y.cjs} +4 -4
  22. package/dist/tsup/{chunk-47HHIEXH.cjs.map → chunk-4UUEB43Y.cjs.map} +1 -1
  23. package/dist/tsup/{chunk-7QKCIVAY.cjs → chunk-5IWLUJ6W.cjs} +167 -167
  24. package/dist/tsup/{chunk-7QKCIVAY.cjs.map → chunk-5IWLUJ6W.cjs.map} +1 -1
  25. package/dist/tsup/{chunk-P2GNQ4RN.js → chunk-H6VVZMWN.js} +4 -4
  26. package/dist/tsup/{chunk-MEHBWPLJ.js → chunk-I35VSLEM.js} +3 -3
  27. package/dist/tsup/{chunk-BEI24WTI.js → chunk-JBUZRPY5.js} +2 -2
  28. package/dist/tsup/{chunk-B6VUNZUD.cjs → chunk-JLJJZYCJ.cjs} +10 -10
  29. package/dist/tsup/{chunk-B6VUNZUD.cjs.map → chunk-JLJJZYCJ.cjs.map} +1 -1
  30. package/dist/tsup/{chunk-VTTFNQQI.js → chunk-L2X3YFER.js} +31 -6
  31. package/dist/tsup/chunk-L2X3YFER.js.map +1 -0
  32. package/dist/tsup/{chunk-DXXJPH55.cjs → chunk-MNHKOS6L.cjs} +39 -14
  33. package/dist/tsup/chunk-MNHKOS6L.cjs.map +1 -0
  34. package/dist/tsup/{chunk-UMZVD6DQ.js → chunk-NERUIBOT.js} +2 -2
  35. package/dist/tsup/{chunk-I4LN3FNT.cjs → chunk-OST76LRW.cjs} +7 -7
  36. package/dist/tsup/{chunk-I4LN3FNT.cjs.map → chunk-OST76LRW.cjs.map} +1 -1
  37. package/dist/tsup/{chunk-LVTBW2RE.cjs → chunk-OZBCXBVP.cjs} +3 -3
  38. package/dist/tsup/{chunk-LVTBW2RE.cjs.map → chunk-OZBCXBVP.cjs.map} +1 -1
  39. package/dist/tsup/{chunk-4JDSFJS5.js → chunk-PT6OIW5E.js} +5 -5
  40. package/dist/tsup/{chunk-2OTRTA3J.js → chunk-R6KPN5EW.js} +2 -2
  41. package/dist/tsup/{chunk-3677IIOV.cjs → chunk-V5KMAMX3.cjs} +5 -5
  42. package/dist/tsup/{chunk-3677IIOV.cjs.map → chunk-V5KMAMX3.cjs.map} +1 -1
  43. package/dist/tsup/{chunk-DPIMKYNB.js → chunk-XIX5DOZN.js} +10 -1
  44. package/dist/tsup/chunk-XIX5DOZN.js.map +1 -0
  45. package/dist/tsup/{chunk-BRP62GZC.js → chunk-ZZ3WBRPD.js} +5 -1
  46. package/dist/tsup/chunk-ZZ3WBRPD.js.map +1 -0
  47. package/dist/tsup/client/mod.cjs +7 -7
  48. package/dist/tsup/client/mod.d.cts +3 -3
  49. package/dist/tsup/client/mod.d.ts +3 -3
  50. package/dist/tsup/client/mod.js +6 -6
  51. package/dist/tsup/common/log.cjs +3 -3
  52. package/dist/tsup/common/log.js +2 -2
  53. package/dist/tsup/common/websocket.cjs +4 -4
  54. package/dist/tsup/common/websocket.js +3 -3
  55. package/dist/tsup/{config-CZQQ-mso.d.cts → config-CzvopP5m.d.cts} +22 -7
  56. package/dist/tsup/{config-BxWAw3iH.d.ts → config-DZuT7tcp.d.ts} +22 -7
  57. package/dist/tsup/context-CyAdY-aA.d.ts +128 -0
  58. package/dist/tsup/context-sNB28g0N.d.cts +128 -0
  59. package/dist/tsup/dynamic/mod.cjs +2 -2
  60. package/dist/tsup/dynamic/mod.d.cts +2 -2
  61. package/dist/tsup/dynamic/mod.d.ts +2 -2
  62. package/dist/tsup/dynamic/mod.js +1 -1
  63. package/dist/tsup/inspector/mod.cjs +6 -6
  64. package/dist/tsup/inspector/mod.js +5 -5
  65. package/dist/tsup/inspector-tab/mod.cjs +2 -2
  66. package/dist/tsup/inspector-tab/mod.cjs.map +1 -1
  67. package/dist/tsup/inspector-tab/mod.d.cts +3 -3
  68. package/dist/tsup/inspector-tab/mod.d.ts +3 -3
  69. package/dist/tsup/inspector-tab/mod.js +1 -1
  70. package/dist/tsup/inspector-tab/mod.js.map +1 -1
  71. package/dist/tsup/mod.cjs +292 -276
  72. package/dist/tsup/mod.cjs.map +1 -1
  73. package/dist/tsup/mod.d.cts +4 -4
  74. package/dist/tsup/mod.d.ts +4 -4
  75. package/dist/tsup/mod.js +202 -186
  76. package/dist/tsup/mod.js.map +1 -1
  77. package/dist/tsup/test/mod.cjs +10 -10
  78. package/dist/tsup/test/mod.d.cts +2 -2
  79. package/dist/tsup/test/mod.d.ts +2 -2
  80. package/dist/tsup/test/mod.js +6 -6
  81. package/dist/tsup/{utils-DQosb24I.d.ts → utils-CqDnC_PS.d.cts} +2 -1
  82. package/dist/tsup/{utils-DQosb24I.d.cts → utils-CqDnC_PS.d.ts} +2 -1
  83. package/dist/tsup/utils.cjs +3 -3
  84. package/dist/tsup/utils.d.cts +1 -1
  85. package/dist/tsup/utils.d.ts +1 -1
  86. package/dist/tsup/utils.js +2 -2
  87. package/dist/tsup/workflow/mod.cjs +184 -148
  88. package/dist/tsup/workflow/mod.cjs.map +1 -1
  89. package/dist/tsup/workflow/mod.d.cts +6 -6
  90. package/dist/tsup/workflow/mod.d.ts +6 -6
  91. package/dist/tsup/workflow/mod.js +177 -141
  92. package/dist/tsup/workflow/mod.js.map +1 -1
  93. package/package.json +11 -11
  94. package/src/actor/config.ts +14 -0
  95. package/src/actor/errors.ts +11 -0
  96. package/src/inspector-tab/mod.ts +1 -1
  97. package/src/registry/index.ts +74 -18
  98. package/src/registry/napi-runtime.ts +16 -0
  99. package/src/registry/native.ts +79 -30
  100. package/src/registry/runtime.ts +11 -0
  101. package/src/registry/wasm-runtime.ts +11 -0
  102. package/src/utils/env-vars.ts +31 -0
  103. package/src/workflow/context.ts +675 -244
  104. package/src/workflow/driver.ts +20 -7
  105. package/src/workflow/mod.ts +25 -9
  106. package/dist/tsup/chunk-BRP62GZC.js.map +0 -1
  107. package/dist/tsup/chunk-DPIMKYNB.js.map +0 -1
  108. package/dist/tsup/chunk-DXXJPH55.cjs.map +0 -1
  109. package/dist/tsup/chunk-KORQB2IR.cjs.map +0 -1
  110. package/dist/tsup/chunk-NIY3RSPX.cjs.map +0 -1
  111. package/dist/tsup/chunk-VTTFNQQI.js.map +0 -1
  112. package/dist/tsup/context-Bw7xq8w3.d.cts +0 -92
  113. package/dist/tsup/context-D8QA76sV.d.ts +0 -92
  114. package/src/utils/serve.ts +0 -216
  115. /package/dist/tsup/{chunk-P2GNQ4RN.js.map → chunk-H6VVZMWN.js.map} +0 -0
  116. /package/dist/tsup/{chunk-MEHBWPLJ.js.map → chunk-I35VSLEM.js.map} +0 -0
  117. /package/dist/tsup/{chunk-BEI24WTI.js.map → chunk-JBUZRPY5.js.map} +0 -0
  118. /package/dist/tsup/{chunk-UMZVD6DQ.js.map → chunk-NERUIBOT.js.map} +0 -0
  119. /package/dist/tsup/{chunk-4JDSFJS5.js.map → chunk-PT6OIW5E.js.map} +0 -0
  120. /package/dist/tsup/{chunk-2OTRTA3J.js.map → chunk-R6KPN5EW.js.map} +0 -0
@@ -1,23 +1,24 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } var _class; var _class2;
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } async function _asyncNullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return await rhsFn(); } } var _class; var _class2;
2
2
 
3
- var _chunkB6VUNZUDcjs = require('../chunk-B6VUNZUD.cjs');
4
- require('../chunk-47HHIEXH.cjs');
3
+ var _chunkJLJJZYCJcjs = require('../chunk-JLJJZYCJ.cjs');
4
+ require('../chunk-4UUEB43Y.cjs');
5
5
 
6
6
 
7
7
 
8
8
 
9
- var _chunkNIY3RSPXcjs = require('../chunk-NIY3RSPX.cjs');
9
+ var _chunk3MHDOUD7cjs = require('../chunk-3MHDOUD7.cjs');
10
10
 
11
11
 
12
12
 
13
13
  var _chunkVE2X4KMGcjs = require('../chunk-VE2X4KMG.cjs');
14
- require('../chunk-3677IIOV.cjs');
14
+ require('../chunk-V5KMAMX3.cjs');
15
15
 
16
16
 
17
- var _chunkDXXJPH55cjs = require('../chunk-DXXJPH55.cjs');
17
+ var _chunkMNHKOS6Lcjs = require('../chunk-MNHKOS6L.cjs');
18
18
 
19
19
 
20
- var _chunkKORQB2IRcjs = require('../chunk-KORQB2IR.cjs');
20
+
21
+ var _chunk4FC7TVS6cjs = require('../chunk-4FC7TVS6.cjs');
21
22
 
22
23
  // src/workflow/mod.ts
23
24
 
@@ -38,12 +39,97 @@ var _invariant = require('invariant'); var _invariant2 = _interopRequireDefault(
38
39
  var WORKFLOW_GUARD_KV_KEY = "__rivet_actor_workflow_guard_triggered";
39
40
 
40
41
  // src/workflow/context.ts
41
- var ActorWorkflowContext = class _ActorWorkflowContext {
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 {
42
131
  #inner;
43
132
  #runCtx;
44
- #actorAccessDepth = 0;
45
- #allowActorAccess = false;
46
- #guardViolation = false;
47
133
  constructor(inner, runCtx) {
48
134
  this.#inner = inner;
49
135
  this.#runCtx = runCtx;
@@ -54,6 +140,18 @@ var ActorWorkflowContext = class _ActorWorkflowContext {
54
140
  get abortSignal() {
55
141
  return this.#inner.abortSignal;
56
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
+ }
57
155
  get queue() {
58
156
  const self = this;
59
157
  async function next(name, opts) {
@@ -66,14 +164,9 @@ var ActorWorkflowContext = class _ActorWorkflowContext {
66
164
  (message) => self.#toActorQueueMessage(message)
67
165
  );
68
166
  }
69
- async function send(name, body) {
70
- self.#ensureActorAccess("queue.send");
71
- await self.#runCtx.queue.send(name, body);
72
- }
73
167
  return {
74
168
  next,
75
- nextBatch,
76
- send
169
+ nextBatch
77
170
  };
78
171
  }
79
172
  async step(nameOrConfig, run) {
@@ -81,17 +174,17 @@ var ActorWorkflowContext = class _ActorWorkflowContext {
81
174
  if (!run) {
82
175
  throw new Error("Step run function missing");
83
176
  }
177
+ const stepRun = run;
84
178
  return await this.#wrapActive(
85
- () => this.#inner.step(
86
- nameOrConfig,
87
- () => this.#withActorAccessAndStateRollback(run)
88
- )
179
+ () => this.#inner.step(nameOrConfig, () => this.#runStep(stepRun))
89
180
  );
90
181
  }
91
182
  const stepConfig = nameOrConfig;
183
+ const rollback = stepConfig.rollback;
92
184
  const config = {
93
185
  ...stepConfig,
94
- run: () => this.#withActorAccessAndStateRollback(stepConfig.run)
186
+ run: () => this.#runStep(stepConfig.run),
187
+ rollback: rollback ? (_ctx, output) => this.#runRollback(rollback, output) : void 0
95
188
  };
96
189
  return await this.#wrapActive(() => this.#inner.step(config));
97
190
  }
@@ -100,17 +193,17 @@ var ActorWorkflowContext = class _ActorWorkflowContext {
100
193
  if (!run) {
101
194
  throw new Error("Step run function missing");
102
195
  }
196
+ const stepRun = run;
103
197
  return await this.#wrapActive(
104
- () => this.#inner.tryStep(
105
- nameOrConfig,
106
- () => this.#withActorAccessAndStateRollback(run)
107
- )
198
+ () => this.#inner.tryStep(nameOrConfig, () => this.#runStep(stepRun))
108
199
  );
109
200
  }
110
201
  const stepConfig = nameOrConfig;
202
+ const rollback = stepConfig.rollback;
111
203
  const config = {
112
204
  ...stepConfig,
113
- run: () => this.#withActorAccessAndStateRollback(stepConfig.run)
205
+ run: () => this.#runStep(stepConfig.run),
206
+ rollback: rollback ? (_ctx, output) => this.#runRollback(rollback, output) : void 0
114
207
  };
115
208
  return await this.#wrapActive(() => this.#inner.tryStep(config));
116
209
  }
@@ -119,10 +212,11 @@ var ActorWorkflowContext = class _ActorWorkflowContext {
119
212
  if (!run) {
120
213
  throw new Error("Try run function missing");
121
214
  }
215
+ const tryRun = run;
122
216
  return await this.#wrapActive(
123
217
  () => this.#inner.try(
124
218
  nameOrConfig,
125
- async (ctx) => run(this.#createChildContext(ctx))
219
+ async (ctx) => tryRun(this.#createChildContext(ctx))
126
220
  )
127
221
  );
128
222
  }
@@ -138,16 +232,20 @@ var ActorWorkflowContext = class _ActorWorkflowContext {
138
232
  if (!run) {
139
233
  throw new Error("Loop run function missing");
140
234
  }
235
+ const loopRun = run;
141
236
  return await this.#wrapActive(
142
237
  () => this.#inner.loop(
143
238
  nameOrConfig,
144
- async (ctx) => run(this.#createChildContext(ctx))
239
+ // A void return (no explicit Loop result) is undefined at
240
+ // runtime, which the engine treats as continue.
241
+ async (ctx) => await _asyncNullishCoalesce(await loopRun(this.#createChildContext(ctx)), async () => ( void 0))
145
242
  )
146
243
  );
147
244
  }
245
+ const loopConfig = nameOrConfig;
148
246
  const wrapped = {
149
- ...nameOrConfig,
150
- run: async (ctx, state) => nameOrConfig.run(this.#createChildContext(ctx), state)
247
+ ...loopConfig,
248
+ run: (async (ctx, state) => await _asyncNullishCoalesce(await loopConfig.run(this.#createChildContext(ctx), state), async () => ( void 0)))
151
249
  };
152
250
  return await this.#wrapActive(() => this.#inner.loop(wrapped));
153
251
  }
@@ -157,10 +255,6 @@ var ActorWorkflowContext = class _ActorWorkflowContext {
157
255
  sleepUntil(name, timestampMs) {
158
256
  return this.#inner.sleepUntil(name, timestampMs);
159
257
  }
160
- destroy() {
161
- this.#ensureActorAccess("destroy");
162
- this.#runCtx.destroy();
163
- }
164
258
  async rollbackCheckpoint(name) {
165
259
  await this.#wrapActive(() => this.#inner.rollbackCheckpoint(name));
166
260
  }
@@ -192,99 +286,14 @@ var ActorWorkflowContext = class _ActorWorkflowContext {
192
286
  isEvicted() {
193
287
  return this.#inner.isEvicted();
194
288
  }
195
- get state() {
196
- this.#ensureActorAccess("state");
197
- return this.#runCtx.state;
198
- }
199
- get vars() {
200
- this.#ensureActorAccess("vars");
201
- return this.#runCtx.vars;
202
- }
203
- client() {
204
- this.#ensureActorAccess("client");
205
- return this.#runCtx.client();
206
- }
207
- get db() {
208
- this.#ensureActorAccess("db");
209
- return this.#runCtx.db;
210
- }
211
- get log() {
212
- return this.#runCtx.log;
213
- }
214
- /** @deprecated No-op. Use `keepAwake(promise)` or `waitUntil(promise)` instead. */
215
- setPreventSleep(_prevent) {
216
- this.#ensureActorAccess("setPreventSleep");
217
- }
218
- /** @deprecated No-op. Always returns `false`. */
219
- get preventSleep() {
220
- this.#ensureActorAccess("preventSleep");
221
- return false;
222
- }
223
- /**
224
- * Holds the actor awake for the duration of the provided promise. The
225
- * actor cannot idle-sleep or finalize the sleep grace period until the
226
- * promise settles.
227
- */
228
- keepAwake(promise) {
229
- this.#ensureActorAccess("keepAwake");
230
- return this.#runCtx.keepAwake(promise);
231
- }
232
- /**
233
- * Registers a promise that the sleep grace period will wait on. Use this
234
- * for best-effort flush/cleanup work that may complete inside the grace
235
- * window. For work the actor must stay running through, prefer
236
- * `c.keepAwake(promise)` which also blocks idle sleep.
237
- */
238
- waitUntil(promise) {
239
- this.#ensureActorAccess("waitUntil");
240
- this.#runCtx.waitUntil(promise);
241
- }
242
- get actorId() {
243
- return this.#runCtx.actorId;
244
- }
245
- broadcast(name, ...args) {
246
- this.#ensureActorAccess("broadcast");
247
- this.#runCtx.broadcast(
248
- name,
249
- ...args
250
- );
251
- }
252
- #toActorQueueMessage(message) {
253
- let id;
254
- try {
255
- id = BigInt(message.id);
256
- } catch (e) {
257
- throw new Error(`Invalid queue message id "${message.id}"`);
258
- }
259
- return {
260
- id,
261
- name: message.name,
262
- body: message.body,
263
- createdAt: message.createdAt,
264
- ...message.complete ? { complete: message.complete } : {}
265
- };
266
- }
267
- async #wrapActive(run) {
268
- return await this.#runCtx.internalKeepAwake(run);
269
- }
270
- async #withActorAccess(run) {
271
- this.#actorAccessDepth++;
272
- if (this.#actorAccessDepth === 1) {
273
- this.#allowActorAccess = true;
274
- }
275
- try {
276
- return await run();
277
- } finally {
278
- this.#actorAccessDepth--;
279
- if (this.#actorAccessDepth === 0) {
280
- this.#allowActorAccess = false;
281
- }
282
- }
283
- }
284
- async #withActorAccessAndStateRollback(run) {
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());
285
294
  let stateSnapshot = null;
286
295
  try {
287
- stateSnapshot = { state: this.#runCtx[_chunkNIY3RSPXcjs.RAW_STATE_SYMBOL]() };
296
+ stateSnapshot = { state: this.#runCtx[_chunk3MHDOUD7cjs.RAW_STATE_SYMBOL]() };
288
297
  } catch (error) {
289
298
  this.#runCtx.log.debug({
290
299
  msg: "failed to get state, likely due to being stateless workflow",
@@ -296,29 +305,47 @@ var ActorWorkflowContext = class _ActorWorkflowContext {
296
305
  }
297
306
  const varsSnapshot = structuredClone(this.#runCtx.vars);
298
307
  try {
299
- return await this.#withActorAccess(run);
308
+ return await run(stepCtx);
300
309
  } catch (error) {
301
310
  if (stateSnapshot) {
302
311
  this.#runCtx.state = stateSnapshot.state;
303
312
  }
304
313
  this.#runCtx.vars = varsSnapshot;
305
314
  throw error;
315
+ } finally {
316
+ stepCtx[DEACTIVATE_STEP]();
306
317
  }
307
318
  }
308
- #ensureActorAccess(feature) {
309
- if (!this.#allowActorAccess) {
310
- this.#guardViolation = true;
311
- this.#markGuardTriggered();
312
- throw new Error(
313
- `${feature} is only available inside workflow steps`
314
- );
319
+ #toActorQueueMessage(message) {
320
+ let id;
321
+ try {
322
+ id = BigInt(message.id);
323
+ } catch (e) {
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]();
315
342
  }
316
343
  }
317
- consumeGuardViolation() {
318
- const violated = this.#guardViolation;
319
- this.#guardViolation = false;
320
- return violated;
344
+ async #wrapActive(run) {
345
+ return await this.#runCtx.internalKeepAwake(run);
321
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.
322
349
  #markGuardTriggered() {
323
350
  try {
324
351
  const state = this.#runCtx.state;
@@ -341,7 +368,7 @@ var ActorWorkflowContext = class _ActorWorkflowContext {
341
368
  );
342
369
  }
343
370
  #createChildContext(ctx) {
344
- return new _ActorWorkflowContext(ctx, this.#runCtx);
371
+ return new _WorkflowContext(ctx, this.#runCtx);
345
372
  }
346
373
  };
347
374
 
@@ -620,7 +647,7 @@ function shouldRethrowWorkflowError(error) {
620
647
  return true;
621
648
  }
622
649
  function workflowReplayInFlightError() {
623
- return new (0, _chunkKORQB2IRcjs.RivetError)(
650
+ return new (0, _chunk4FC7TVS6cjs.RivetError)(
624
651
  "actor",
625
652
  "workflow_in_flight",
626
653
  "Workflow replay is unavailable while the workflow is currently in flight.",
@@ -639,13 +666,13 @@ function workflow(fn, options = {}) {
639
666
  function getWorkflowInspector(actorId) {
640
667
  let workflowInspector = workflowInspectors.get(actorId);
641
668
  if (!workflowInspector) {
642
- workflowInspector = _chunkB6VUNZUDcjs.createWorkflowInspectorAdapter.call(void 0, );
669
+ workflowInspector = _chunkJLJJZYCJcjs.createWorkflowInspectorAdapter.call(void 0, );
643
670
  workflowInspectors.set(actorId, workflowInspector);
644
671
  }
645
672
  return workflowInspector;
646
673
  }
647
674
  async function run(runCtx) {
648
- const actor = runCtx[_chunkNIY3RSPXcjs.ACTOR_CONTEXT_INTERNAL_SYMBOL];
675
+ const actor = runCtx[_chunk3MHDOUD7cjs.ACTOR_CONTEXT_INTERNAL_SYMBOL];
649
676
  _invariant2.default.call(void 0, actor, "workflow() requires an actor instance");
650
677
  const workflowInspector = getWorkflowInspector(actor.id);
651
678
  const driver = new ActorWorkflowDriver(actor, runCtx);
@@ -675,11 +702,13 @@ function workflow(fn, options = {}) {
675
702
  });
676
703
  const handle = _workflowengine.runWorkflow.call(void 0,
677
704
  actor.id,
678
- async (ctx) => await fn(new ActorWorkflowContext(ctx, runCtx)),
705
+ async (ctx) => await fn(new WorkflowContext(ctx, runCtx)),
679
706
  void 0,
680
707
  driver,
681
708
  {
682
709
  mode: "live",
710
+ // The actor logger and the engine's pino logger are runtime
711
+ // compatible but not structurally assignable.
683
712
  logger: runCtx.log,
684
713
  onHistoryUpdated: workflowInspector.update,
685
714
  onError: onError ? async (event) => await onError(runCtx, event) : void 0
@@ -699,27 +728,33 @@ function workflow(fn, options = {}) {
699
728
  try {
700
729
  await handle.result;
701
730
  } catch (error) {
702
- if (runCtx.abortSignal.aborted) {
731
+ if (runCtx.abortSignal.aborted || _chunk4FC7TVS6cjs.isActorAbortedError.call(void 0, error)) {
703
732
  return;
704
733
  }
705
734
  if (shouldRethrowWorkflowError(error)) {
706
735
  runCtx.log.error({
707
736
  msg: "workflow run failed",
708
- error: _chunkDXXJPH55cjs.stringifyError.call(void 0, error)
737
+ error: _chunkMNHKOS6Lcjs.stringifyError.call(void 0, error)
709
738
  });
710
739
  throw error;
711
740
  }
712
741
  runCtx.log.warn({
713
742
  msg: "workflow failed and will sleep until woken",
714
- error: _chunkDXXJPH55cjs.stringifyError.call(void 0, error)
743
+ error: _chunkMNHKOS6Lcjs.stringifyError.call(void 0, error)
715
744
  });
716
745
  } finally {
717
746
  runCtx.abortSignal.removeEventListener("abort", onAbort);
718
747
  }
719
748
  }
720
749
  const runWithConfig = run;
721
- runWithConfig[_chunkNIY3RSPXcjs.RUN_FUNCTION_CONFIG_SYMBOL] = {
750
+ runWithConfig[_chunk3MHDOUD7cjs.RUN_FUNCTION_CONFIG_SYMBOL] = {
722
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
+ },
723
758
  inspectorFactory: (actor) => {
724
759
  const actorId = resolveWorkflowInspectorActorId(actor);
725
760
  return {
@@ -754,5 +789,6 @@ function resolveWorkflowInspectorActorId(actor) {
754
789
 
755
790
 
756
791
 
757
- exports.ActorWorkflowContext = ActorWorkflowContext; exports.Loop = _workflowengine.Loop; exports.workflow = workflow;
792
+
793
+ exports.Loop = _workflowengine.Loop; exports.WorkflowContext = WorkflowContext; exports.WorkflowStepContext = WorkflowStepContext; exports.workflow = workflow;
758
794
  //# sourceMappingURL=mod.cjs.map