@dudousxd/nestjs-durable-core 0.27.1 → 0.28.0

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 (113) hide show
  1. package/LICENSE +21 -0
  2. package/dist/index.cjs +3923 -0
  3. package/dist/index.cjs.map +1 -0
  4. package/dist/index.d.cts +2044 -0
  5. package/dist/index.d.ts +2044 -20
  6. package/dist/index.js +3857 -38
  7. package/dist/index.js.map +1 -1
  8. package/package.json +19 -5
  9. package/dist/backoff.d.ts +0 -5
  10. package/dist/backoff.d.ts.map +0 -1
  11. package/dist/backoff.js +0 -14
  12. package/dist/backoff.js.map +0 -1
  13. package/dist/checkpoints.d.ts +0 -17
  14. package/dist/checkpoints.d.ts.map +0 -1
  15. package/dist/checkpoints.js +0 -28
  16. package/dist/checkpoints.js.map +0 -1
  17. package/dist/codec-state-store.d.ts +0 -57
  18. package/dist/codec-state-store.d.ts.map +0 -1
  19. package/dist/codec-state-store.js +0 -111
  20. package/dist/codec-state-store.js.map +0 -1
  21. package/dist/completion.d.ts +0 -15
  22. package/dist/completion.d.ts.map +0 -1
  23. package/dist/completion.js +0 -13
  24. package/dist/completion.js.map +0 -1
  25. package/dist/duration.d.ts +0 -6
  26. package/dist/duration.d.ts.map +0 -1
  27. package/dist/duration.js +0 -42
  28. package/dist/duration.js.map +0 -1
  29. package/dist/engine.d.ts +0 -525
  30. package/dist/engine.d.ts.map +0 -1
  31. package/dist/engine.js +0 -1785
  32. package/dist/engine.js.map +0 -1
  33. package/dist/entities.d.ts +0 -44
  34. package/dist/entities.d.ts.map +0 -1
  35. package/dist/entities.js +0 -68
  36. package/dist/entities.js.map +0 -1
  37. package/dist/errors.d.ts +0 -56
  38. package/dist/errors.d.ts.map +0 -1
  39. package/dist/errors.js +0 -93
  40. package/dist/errors.js.map +0 -1
  41. package/dist/event-accumulators.d.ts +0 -39
  42. package/dist/event-accumulators.d.ts.map +0 -1
  43. package/dist/event-accumulators.js +0 -74
  44. package/dist/event-accumulators.js.map +0 -1
  45. package/dist/events.d.ts +0 -9
  46. package/dist/events.d.ts.map +0 -1
  47. package/dist/events.js +0 -41
  48. package/dist/events.js.map +0 -1
  49. package/dist/index.d.ts.map +0 -1
  50. package/dist/interfaces.d.ts +0 -830
  51. package/dist/interfaces.d.ts.map +0 -1
  52. package/dist/interfaces.js +0 -3
  53. package/dist/interfaces.js.map +0 -1
  54. package/dist/metrics.d.ts +0 -44
  55. package/dist/metrics.d.ts.map +0 -1
  56. package/dist/metrics.js +0 -76
  57. package/dist/metrics.js.map +0 -1
  58. package/dist/protocol.d.ts +0 -17
  59. package/dist/protocol.d.ts.map +0 -1
  60. package/dist/protocol.js +0 -53
  61. package/dist/protocol.js.map +0 -1
  62. package/dist/queue.d.ts +0 -48
  63. package/dist/queue.d.ts.map +0 -1
  64. package/dist/queue.js +0 -45
  65. package/dist/queue.js.map +0 -1
  66. package/dist/remote-step-factory.d.ts +0 -15
  67. package/dist/remote-step-factory.d.ts.map +0 -1
  68. package/dist/remote-step-factory.js +0 -15
  69. package/dist/remote-step-factory.js.map +0 -1
  70. package/dist/remote-workflow-executor.d.ts +0 -24
  71. package/dist/remote-workflow-executor.d.ts.map +0 -1
  72. package/dist/remote-workflow-executor.js +0 -73
  73. package/dist/remote-workflow-executor.js.map +0 -1
  74. package/dist/scheduler.d.ts +0 -42
  75. package/dist/scheduler.d.ts.map +0 -1
  76. package/dist/scheduler.js +0 -70
  77. package/dist/scheduler.js.map +0 -1
  78. package/dist/search-attributes.d.ts +0 -17
  79. package/dist/search-attributes.d.ts.map +0 -1
  80. package/dist/search-attributes.js +0 -47
  81. package/dist/search-attributes.js.map +0 -1
  82. package/dist/step-logger.d.ts +0 -9
  83. package/dist/step-logger.d.ts.map +0 -1
  84. package/dist/step-logger.js +0 -92
  85. package/dist/step-logger.js.map +0 -1
  86. package/dist/test-helpers.d.ts +0 -11
  87. package/dist/test-helpers.d.ts.map +0 -1
  88. package/dist/test-helpers.js +0 -15
  89. package/dist/test-helpers.js.map +0 -1
  90. package/dist/testing/in-memory-state-store.d.ts +0 -37
  91. package/dist/testing/in-memory-state-store.d.ts.map +0 -1
  92. package/dist/testing/in-memory-state-store.js +0 -134
  93. package/dist/testing/in-memory-state-store.js.map +0 -1
  94. package/dist/testing/in-memory-transport.d.ts +0 -25
  95. package/dist/testing/in-memory-transport.d.ts.map +0 -1
  96. package/dist/testing/in-memory-transport.js +0 -53
  97. package/dist/testing/in-memory-transport.js.map +0 -1
  98. package/dist/tokens.d.ts +0 -8
  99. package/dist/tokens.d.ts.map +0 -1
  100. package/dist/tokens.js +0 -11
  101. package/dist/tokens.js.map +0 -1
  102. package/dist/transport-pool.d.ts +0 -30
  103. package/dist/transport-pool.d.ts.map +0 -1
  104. package/dist/transport-pool.js +0 -86
  105. package/dist/transport-pool.js.map +0 -1
  106. package/dist/workflow-ctx.d.ts +0 -61
  107. package/dist/workflow-ctx.d.ts.map +0 -1
  108. package/dist/workflow-ctx.js +0 -451
  109. package/dist/workflow-ctx.js.map +0 -1
  110. package/dist/workflow-ref.d.ts +0 -34
  111. package/dist/workflow-ref.d.ts.map +0 -1
  112. package/dist/workflow-ref.js +0 -32
  113. package/dist/workflow-ref.js.map +0 -1
package/dist/index.cjs ADDED
@@ -0,0 +1,3923 @@
1
+ const __importMetaUrl = require('url').pathToFileURL(__filename).href;
2
+ "use strict";
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
21
+
22
+ // src/index.ts
23
+ var index_exports = {};
24
+ __export(index_exports, {
25
+ CodecStateStore: () => CodecStateStore,
26
+ ContinueAsNew: () => ContinueAsNew,
27
+ DURABLE_OPTIONS: () => DURABLE_OPTIONS,
28
+ Entities: () => Entities,
29
+ EventAccumulators: () => EventAccumulators,
30
+ FatalError: () => FatalError,
31
+ InMemoryStateStore: () => InMemoryStateStore,
32
+ InMemoryTransport: () => InMemoryTransport,
33
+ NonDeterminismError: () => NonDeterminismError,
34
+ QueueController: () => QueueController,
35
+ RemoteStepError: () => RemoteStepError,
36
+ RemoteStepTimeout: () => RemoteStepTimeout,
37
+ RemoteWorkflowExecutor: () => RemoteWorkflowExecutor,
38
+ STATE_STORE: () => STATE_STORE,
39
+ SignalTimeoutError: () => SignalTimeoutError,
40
+ SingletonQueueFullError: () => SingletonQueueFullError,
41
+ TRANSPORT: () => TRANSPORT,
42
+ WORKFLOW_NAME_KEY: () => WORKFLOW_NAME_KEY,
43
+ WorkflowEngine: () => WorkflowEngine,
44
+ WorkflowSuspended: () => WorkflowSuspended,
45
+ applyAttributeQuery: () => applyAttributeQuery,
46
+ attributeColumnFor: () => attributeColumnFor,
47
+ attributeOperand: () => attributeOperand,
48
+ breakpointToken: () => breakpointToken,
49
+ collectMetrics: () => collectMetrics,
50
+ eventMatchOf: () => eventMatchOf,
51
+ eventMatches: () => eventMatches,
52
+ eventPrefix: () => eventPrefix,
53
+ eventToken: () => eventToken,
54
+ matchesAttributes: () => matchesAttributes,
55
+ normalizeAttributeRows: () => normalizeAttributeRows,
56
+ parseDuration: () => parseDuration,
57
+ prevCronFireMs: () => prevCronFireMs,
58
+ remoteStep: () => remoteStep,
59
+ runSchedules: () => runSchedules,
60
+ runStepHandler: () => runStepHandler,
61
+ scheduledRunId: () => scheduledRunId,
62
+ sqlComparator: () => sqlComparator,
63
+ stepId: () => stepId,
64
+ workflowName: () => workflowName
65
+ });
66
+ module.exports = __toCommonJS(index_exports);
67
+
68
+ // src/duration.ts
69
+ var UNIT_MS = {
70
+ ms: 1,
71
+ s: 1e3,
72
+ sec: 1e3,
73
+ second: 1e3,
74
+ seconds: 1e3,
75
+ m: 6e4,
76
+ min: 6e4,
77
+ minute: 6e4,
78
+ minutes: 6e4,
79
+ h: 36e5,
80
+ hr: 36e5,
81
+ hour: 36e5,
82
+ hours: 36e5,
83
+ d: 864e5,
84
+ day: 864e5,
85
+ days: 864e5,
86
+ w: 6048e5,
87
+ week: 6048e5,
88
+ weeks: 6048e5
89
+ };
90
+ function parseDuration(duration) {
91
+ if (typeof duration === "number") return duration;
92
+ const match = duration.trim().match(/^(\d+(?:\.\d+)?)\s*([a-z]+)$/i);
93
+ if (!match) throw new Error(`invalid duration: ${duration}`);
94
+ const value = Number(match[1]);
95
+ const unit = (match[2] ?? "").toLowerCase();
96
+ const factor = UNIT_MS[unit];
97
+ if (factor === void 0) throw new Error(`unknown duration unit: ${unit}`);
98
+ return value * factor;
99
+ }
100
+ __name(parseDuration, "parseDuration");
101
+
102
+ // src/backoff.ts
103
+ function backoffDelay(attempt, options) {
104
+ const base = options?.backoffMs ?? 0;
105
+ if (base <= 0) return 0;
106
+ const raw = options?.backoff === "exp" ? base * 2 ** (attempt - 1) : base;
107
+ const capped = options?.backoffMaxMs ? Math.min(raw, options.backoffMaxMs) : raw;
108
+ return options?.jitter ? Math.round(capped * (0.5 + Math.random() * 0.5)) : capped;
109
+ }
110
+ __name(backoffDelay, "backoffDelay");
111
+
112
+ // src/step-logger.ts
113
+ function withDuration(start, now, data) {
114
+ if (data && typeof data === "object" && "durationMs" in data) {
115
+ return data;
116
+ }
117
+ const base = data && typeof data === "object" ? data : {};
118
+ return {
119
+ ...base,
120
+ durationMs: now() - start
121
+ };
122
+ }
123
+ __name(withDuration, "withDuration");
124
+ function createStepLogger(events, now) {
125
+ let currentSub;
126
+ const push = /* @__PURE__ */ __name((level, message, data) => events.push({
127
+ at: now(),
128
+ level,
129
+ message,
130
+ ...currentSub ? {
131
+ subId: currentSub.id
132
+ } : {},
133
+ ...data === void 0 ? {} : {
134
+ data
135
+ }
136
+ }), "push");
137
+ const subEvent = /* @__PURE__ */ __name((e) => events.push({
138
+ at: now(),
139
+ level: e.status === "failed" ? "error" : e.status === "skipped" ? "warn" : "info",
140
+ message: e.message ?? e.phase ?? e.name,
141
+ subId: e.id,
142
+ name: e.name,
143
+ ...e.group === void 0 ? {} : {
144
+ group: e.group
145
+ },
146
+ ...e.phase === void 0 ? {} : {
147
+ phase: e.phase
148
+ },
149
+ ...e.status === void 0 ? {} : {
150
+ status: e.status
151
+ },
152
+ ...e.data === void 0 ? {} : {
153
+ data: e.data
154
+ }
155
+ }), "subEvent");
156
+ const subProcess = /* @__PURE__ */ __name(async (name, body, opts) => {
157
+ const id = opts?.id ?? globalThis.crypto.randomUUID();
158
+ const group = opts?.group;
159
+ const start = now();
160
+ const prevSub = currentSub;
161
+ currentSub = {
162
+ id
163
+ };
164
+ let terminal = false;
165
+ const emit = /* @__PURE__ */ __name((status, message, data) => {
166
+ if (terminal) return;
167
+ terminal = true;
168
+ subEvent({
169
+ id,
170
+ name,
171
+ group,
172
+ status,
173
+ message,
174
+ data: withDuration(start, now, data)
175
+ });
176
+ }, "emit");
177
+ const handle = {
178
+ phase: /* @__PURE__ */ __name((phase, data) => {
179
+ if (!terminal) subEvent({
180
+ id,
181
+ name,
182
+ group,
183
+ phase,
184
+ data
185
+ });
186
+ return handle;
187
+ }, "phase"),
188
+ skip: /* @__PURE__ */ __name((reason, data) => emit("skipped", reason, data), "skip"),
189
+ fail: /* @__PURE__ */ __name((reason, data) => emit("failed", reason, data), "fail")
190
+ };
191
+ try {
192
+ const result = await body(handle);
193
+ emit("ok");
194
+ return result;
195
+ } catch (err) {
196
+ emit("failed", err instanceof Error ? err.message : String(err));
197
+ throw err;
198
+ } finally {
199
+ currentSub = prevSub;
200
+ }
201
+ }, "subProcess");
202
+ return {
203
+ debug: /* @__PURE__ */ __name((m, d) => push("debug", m, d), "debug"),
204
+ info: /* @__PURE__ */ __name((m, d) => push("info", m, d), "info"),
205
+ warn: /* @__PURE__ */ __name((m, d) => push("warn", m, d), "warn"),
206
+ error: /* @__PURE__ */ __name((m, d) => push("error", m, d), "error"),
207
+ sub: /* @__PURE__ */ __name((name, status, message, data) => events.push({
208
+ at: now(),
209
+ level: status === "failed" ? "error" : status === "skipped" ? "warn" : "info",
210
+ message: message ?? name,
211
+ name,
212
+ status,
213
+ ...data === void 0 ? {} : {
214
+ data
215
+ }
216
+ }), "sub"),
217
+ subEvent,
218
+ subProcess
219
+ };
220
+ }
221
+ __name(createStepLogger, "createStepLogger");
222
+
223
+ // src/protocol.ts
224
+ function stepId(runId, seq) {
225
+ return `${runId}:${seq}`;
226
+ }
227
+ __name(stepId, "stepId");
228
+ var breakpointToken = /* @__PURE__ */ __name((runId, seq) => `bp:${runId}:${seq}`, "breakpointToken");
229
+ async function runStepHandler(task, handler) {
230
+ const base = {
231
+ runId: task.runId,
232
+ seq: task.seq,
233
+ stepId: task.stepId,
234
+ startedAt: Date.now()
235
+ };
236
+ if (!handler) {
237
+ return {
238
+ ...base,
239
+ status: "failed",
240
+ error: {
241
+ message: `no handler for ${task.name}`,
242
+ retryable: false
243
+ }
244
+ };
245
+ }
246
+ const events = [];
247
+ const withEvents = /* @__PURE__ */ __name((result) => events.length > 0 ? {
248
+ ...result,
249
+ events
250
+ } : result, "withEvents");
251
+ try {
252
+ const output = await handler(task.input, createStepLogger(events, Date.now));
253
+ return withEvents({
254
+ ...base,
255
+ status: "completed",
256
+ output
257
+ });
258
+ } catch (err) {
259
+ const e = err;
260
+ return withEvents({
261
+ ...base,
262
+ status: "failed",
263
+ error: {
264
+ message: err instanceof Error ? err.message : String(err),
265
+ ...typeof e?.code === "string" ? {
266
+ code: e.code
267
+ } : {},
268
+ ...typeof e?.retryable === "boolean" ? {
269
+ retryable: e.retryable
270
+ } : {}
271
+ }
272
+ });
273
+ }
274
+ }
275
+ __name(runStepHandler, "runStepHandler");
276
+
277
+ // src/checkpoints.ts
278
+ function instantCheckpoint(p) {
279
+ const at = /* @__PURE__ */ new Date();
280
+ return {
281
+ runId: p.runId,
282
+ seq: p.seq,
283
+ name: p.name,
284
+ kind: p.kind,
285
+ stepId: stepId(p.runId, p.seq),
286
+ status: p.status ?? "completed",
287
+ output: p.output,
288
+ wakeAt: p.wakeAt,
289
+ attempts: 1,
290
+ enqueuedAt: at,
291
+ startedAt: at,
292
+ finishedAt: at
293
+ };
294
+ }
295
+ __name(instantCheckpoint, "instantCheckpoint");
296
+
297
+ // src/errors.ts
298
+ var FatalError = class extends Error {
299
+ static {
300
+ __name(this, "FatalError");
301
+ }
302
+ code;
303
+ constructor(message, code) {
304
+ super(message);
305
+ this.name = "FatalError";
306
+ this.code = code;
307
+ }
308
+ };
309
+ var SingletonQueueFullError = class extends Error {
310
+ static {
311
+ __name(this, "SingletonQueueFullError");
312
+ }
313
+ workflow;
314
+ key;
315
+ maxQueueDepth;
316
+ constructor(workflow, key, maxQueueDepth) {
317
+ super(`singleton queue for ${workflow} key "${key}" is full (maxQueueDepth=${maxQueueDepth}); retry later`);
318
+ this.name = "SingletonQueueFullError";
319
+ this.workflow = workflow;
320
+ this.key = key;
321
+ this.maxQueueDepth = maxQueueDepth;
322
+ }
323
+ };
324
+ var SignalTimeoutError = class extends Error {
325
+ static {
326
+ __name(this, "SignalTimeoutError");
327
+ }
328
+ token;
329
+ timeoutMs;
330
+ constructor(token, timeoutMs) {
331
+ super(`timed out after ${timeoutMs}ms waiting for signal "${token}"`);
332
+ this.name = "SignalTimeoutError";
333
+ this.token = token;
334
+ this.timeoutMs = timeoutMs;
335
+ }
336
+ };
337
+ var RemoteStepTimeout = class extends Error {
338
+ static {
339
+ __name(this, "RemoteStepTimeout");
340
+ }
341
+ stepId;
342
+ timeoutMs;
343
+ constructor(stepId2, timeoutMs) {
344
+ super(`remote step ${stepId2} produced no result/heartbeat within ${timeoutMs}ms`);
345
+ this.name = "RemoteStepTimeout";
346
+ this.stepId = stepId2;
347
+ this.timeoutMs = timeoutMs;
348
+ }
349
+ };
350
+ var NonDeterminismError = class extends Error {
351
+ static {
352
+ __name(this, "NonDeterminismError");
353
+ }
354
+ runId;
355
+ seq;
356
+ constructor(runId, seq, expected, recorded) {
357
+ super(`non-determinism at ${runId}#${seq}: code expects "${expected}" but history recorded "${recorded}". The workflow changed under an in-flight run \u2014 register a new @Workflow version.`);
358
+ this.name = "NonDeterminismError";
359
+ this.runId = runId;
360
+ this.seq = seq;
361
+ }
362
+ };
363
+ var WorkflowSuspended = class extends Error {
364
+ static {
365
+ __name(this, "WorkflowSuspended");
366
+ }
367
+ /** Epoch ms to auto-resume (durable sleep), or undefined when waiting on an external signal. */
368
+ wakeAt;
369
+ constructor(wakeAt) {
370
+ super("workflow suspended");
371
+ this.name = "WorkflowSuspended";
372
+ this.wakeAt = wakeAt;
373
+ }
374
+ };
375
+ var ContinueAsNew = class extends Error {
376
+ static {
377
+ __name(this, "ContinueAsNew");
378
+ }
379
+ input;
380
+ constructor(input) {
381
+ super("workflow continued as new"), this.input = input;
382
+ this.name = "ContinueAsNew";
383
+ }
384
+ };
385
+
386
+ // src/entities.ts
387
+ var ENTITY_WORKFLOW = "__entity";
388
+ var entityRunId = /* @__PURE__ */ __name((name, key) => `entity:${name}:${key}`, "entityRunId");
389
+ var Entities = class {
390
+ static {
391
+ __name(this, "Entities");
392
+ }
393
+ host;
394
+ configs = /* @__PURE__ */ new Map();
395
+ constructor(host) {
396
+ this.host = host;
397
+ host.register(ENTITY_WORKFLOW, "1", (ctx, input) => this.run(ctx, input));
398
+ }
399
+ register(name, config) {
400
+ this.configs.set(name, config);
401
+ }
402
+ /** Deliver an op to an entity's runner, starting it on first contact. `reply` (a signal token)
403
+ * receives the handler's result, for `ctx.callEntity`. */
404
+ dispatch(name, key, op, arg, reply) {
405
+ const runId = entityRunId(name, key);
406
+ return this.host.signalWithStart(ENTITY_WORKFLOW, {
407
+ name,
408
+ key
409
+ }, runId, {
410
+ token: runId,
411
+ payload: {
412
+ op,
413
+ arg,
414
+ reply
415
+ }
416
+ });
417
+ }
418
+ async signal(name, key, op, arg) {
419
+ await this.dispatch(name, key, op, arg);
420
+ }
421
+ getState(name, key) {
422
+ return this.host.getEvent(entityRunId(name, key), "state");
423
+ }
424
+ /** The built-in runner: one long-lived run per key, processing ops serially over state. */
425
+ async run(ctx, input) {
426
+ const { name, key } = input;
427
+ const config = this.configs.get(name);
428
+ if (!config) throw new FatalError(`entity "${name}" is not registered`);
429
+ const token = entityRunId(name, key);
430
+ let state = config.initialState();
431
+ for (let i = 0; ; i += 1) {
432
+ const msg = await ctx.waitForSignal(token);
433
+ const out = await ctx.step(`op:${i}`, async () => {
434
+ const handler = config.handlers[msg.op];
435
+ if (!handler) throw new FatalError(`entity "${name}" has no handler for op "${msg.op}"`);
436
+ const result = await handler(state, msg.arg);
437
+ return {
438
+ result,
439
+ state
440
+ };
441
+ });
442
+ state = out.state;
443
+ await ctx.setEvent("state", state);
444
+ if (msg.reply) {
445
+ await ctx.step(`reply:${i}`, async () => {
446
+ await this.host.signal(msg.reply, out.result);
447
+ });
448
+ }
449
+ }
450
+ }
451
+ };
452
+
453
+ // src/event-accumulators.ts
454
+ var accToken = /* @__PURE__ */ __name((target) => `__evtacc__:${target}`, "accToken");
455
+ var EventAccumulators = class {
456
+ static {
457
+ __name(this, "EventAccumulators");
458
+ }
459
+ host;
460
+ constructor(host) {
461
+ this.host = host;
462
+ host.register("__evt_debounce", "1", (ctx, input) => this.debounce(ctx, input));
463
+ host.register("__evt_batch", "1", (ctx, input) => this.batch(ctx, input));
464
+ }
465
+ /** Route an `onEvent` payload into its target's debounce/batch accumulator. */
466
+ route(target, config, payload) {
467
+ const [workflow, input] = config.mode === "debounce" ? [
468
+ "__evt_debounce",
469
+ {
470
+ target,
471
+ windowMs: config.windowMs
472
+ }
473
+ ] : [
474
+ "__evt_batch",
475
+ {
476
+ target,
477
+ maxSize: config.maxSize,
478
+ windowMs: config.windowMs
479
+ }
480
+ ];
481
+ return this.host.signalWithStart(workflow, input, accToken(target), {
482
+ token: accToken(target),
483
+ payload
484
+ });
485
+ }
486
+ /** Fire the target with the LAST payload once events have been quiet for `windowMs`. */
487
+ async debounce(ctx, input) {
488
+ const { target, windowMs } = input;
489
+ const token = accToken(target);
490
+ let last = await ctx.waitForSignal(token);
491
+ for (; ; ) {
492
+ try {
493
+ last = await ctx.waitForSignal(token, {
494
+ timeoutMs: windowMs
495
+ });
496
+ } catch (e) {
497
+ if (e instanceof SignalTimeoutError) break;
498
+ throw e;
499
+ }
500
+ }
501
+ await ctx.startChild(target, last);
502
+ return ctx.continueAsNew(input);
503
+ }
504
+ /** Fire the target with all payloads (`{ events }`) once `maxSize` or `windowMs` from the first. */
505
+ async batch(ctx, input) {
506
+ const { target, maxSize, windowMs } = input;
507
+ const token = accToken(target);
508
+ const events = [
509
+ await ctx.waitForSignal(token)
510
+ ];
511
+ const deadline = await ctx.now() + windowMs;
512
+ while (events.length < maxSize) {
513
+ const remaining = deadline - await ctx.now();
514
+ if (remaining <= 0) break;
515
+ try {
516
+ events.push(await ctx.waitForSignal(token, {
517
+ timeoutMs: remaining
518
+ }));
519
+ } catch (e) {
520
+ if (e instanceof SignalTimeoutError) break;
521
+ throw e;
522
+ }
523
+ }
524
+ await ctx.startChild(target, {
525
+ events
526
+ });
527
+ return ctx.continueAsNew(input);
528
+ }
529
+ };
530
+
531
+ // src/events.ts
532
+ var enc = /* @__PURE__ */ __name((s) => Buffer.from(s, "utf8").toString("base64"), "enc");
533
+ var dec = /* @__PURE__ */ __name((s) => Buffer.from(s, "base64").toString("utf8"), "dec");
534
+ function eventPrefix(name) {
535
+ return `event:${enc(name)}:`;
536
+ }
537
+ __name(eventPrefix, "eventPrefix");
538
+ function eventToken(name, match, runId, seq) {
539
+ return `${eventPrefix(name)}${enc(JSON.stringify(match ?? {}))}:${runId}#${seq}`;
540
+ }
541
+ __name(eventToken, "eventToken");
542
+ function eventMatchOf(token) {
543
+ try {
544
+ return JSON.parse(dec(token.split(":")[2] ?? ""));
545
+ } catch {
546
+ return {};
547
+ }
548
+ }
549
+ __name(eventMatchOf, "eventMatchOf");
550
+ function eventMatches(payload, match) {
551
+ const keys = Object.keys(match);
552
+ if (keys.length === 0) return true;
553
+ if (!payload || typeof payload !== "object") return false;
554
+ const p = payload;
555
+ return keys.every((k) => JSON.stringify(p[k]) === JSON.stringify(match[k]));
556
+ }
557
+ __name(eventMatches, "eventMatches");
558
+
559
+ // src/queue.ts
560
+ var QueueController = class {
561
+ static {
562
+ __name(this, "QueueController");
563
+ }
564
+ config;
565
+ clock;
566
+ inFlight = 0;
567
+ windowStart = 0;
568
+ windowCount = 0;
569
+ /** Registered waiters by id (only used when priority/fairness ordering is in play). */
570
+ waiters = /* @__PURE__ */ new Map();
571
+ /** Monotonic counter stamping each newly-seen waiter for a stable FIFO tiebreak. */
572
+ waiterSeq = 0;
573
+ /** Per-key "last served" order for round-robin fairness — higher = more recently served. */
574
+ keyServed = /* @__PURE__ */ new Map();
575
+ servedTick = 0;
576
+ constructor(config, clock) {
577
+ this.config = config;
578
+ this.clock = clock;
579
+ }
580
+ /** Whether this controller does any priority/fairness ordering (else it's a plain FIFO gate). */
581
+ get ordered() {
582
+ return this.config.fairness === "key";
583
+ }
584
+ /**
585
+ * Try to admit one unit of work. On `ok`, the caller has taken a concurrency slot and must call
586
+ * {@link release} when the step settles. On block, `retryAt` is when admission may next succeed.
587
+ *
588
+ * `item` carries optional priority/fairness hints; omitting it is the legacy FIFO path. When
589
+ * ordering is configured (or a priority is given), a blocked call REGISTERS as a waiter and is only
590
+ * admitted on a later retry once it is the rightful next under (priority desc, fairness, FIFO).
591
+ */
592
+ tryAdmit(item) {
593
+ const now = this.clock();
594
+ const rl = this.config.rateLimit;
595
+ if (rl) {
596
+ if (now - this.windowStart >= rl.periodMs) {
597
+ this.windowStart = now;
598
+ this.windowCount = 0;
599
+ }
600
+ if (this.windowCount >= rl.limit) return {
601
+ ok: false,
602
+ retryAt: this.windowStart + rl.periodMs
603
+ };
604
+ }
605
+ const usingOrder = this.ordered || item?.priority != null || item?.waiterId != null;
606
+ const waiterId = item?.waiterId;
607
+ const full = this.config.concurrency != null && this.inFlight >= this.config.concurrency;
608
+ if (full) {
609
+ if (usingOrder && waiterId != null) this.register(waiterId, item);
610
+ return {
611
+ ok: false,
612
+ retryAt: now + (this.config.retryMs ?? 1e3)
613
+ };
614
+ }
615
+ if (usingOrder && this.waiters.size > 0) {
616
+ if (waiterId != null) this.register(waiterId, item);
617
+ if (!this.isNext(waiterId, item)) {
618
+ return {
619
+ ok: false,
620
+ retryAt: now + (this.config.retryMs ?? 1e3)
621
+ };
622
+ }
623
+ }
624
+ if (rl) this.windowCount += 1;
625
+ this.inFlight += 1;
626
+ if (waiterId != null) this.waiters.delete(waiterId);
627
+ if (item?.key != null) this.keyServed.set(item.key, ++this.servedTick);
628
+ return {
629
+ ok: true
630
+ };
631
+ }
632
+ /** Release a concurrency slot taken by a successful {@link tryAdmit}. */
633
+ release() {
634
+ if (this.inFlight > 0) this.inFlight -= 1;
635
+ }
636
+ /** Track (or refresh) a blocked call as a waiter; its FIFO seq is stamped only on first sight. */
637
+ register(waiterId, item) {
638
+ const existing = this.waiters.get(waiterId);
639
+ if (existing) {
640
+ existing.priority = item?.priority ?? 0;
641
+ existing.key = item?.key;
642
+ return;
643
+ }
644
+ this.waiters.set(waiterId, {
645
+ priority: item?.priority ?? 0,
646
+ key: item?.key,
647
+ seq: this.waiterSeq++
648
+ });
649
+ }
650
+ /**
651
+ * Is the calling item the best candidate among all registered waiters? Ordering, best first:
652
+ * 1. higher `priority`;
653
+ * 2. fairness: with `fairness: 'key'`, the least-recently-served key wins (round-robin);
654
+ * 3. earliest registration (FIFO) — stable tiebreak.
655
+ * A bare item (no waiterId) is compared on its hints directly.
656
+ */
657
+ isNext(waiterId, item) {
658
+ const self = (waiterId != null ? this.waiters.get(waiterId) : void 0) ?? {
659
+ priority: item?.priority ?? 0,
660
+ key: item?.key,
661
+ seq: Number.POSITIVE_INFINITY
662
+ };
663
+ let best = self;
664
+ for (const w of this.waiters.values()) {
665
+ if (this.better(w, best)) best = w;
666
+ }
667
+ return best === self;
668
+ }
669
+ /** Strict "a should be admitted before b" comparison under the active policy. */
670
+ better(a, b) {
671
+ if (a.priority !== b.priority) return a.priority > b.priority;
672
+ if (this.config.fairness === "key") {
673
+ const sa = this.servedOf(a.key);
674
+ const sb = this.servedOf(b.key);
675
+ if (sa !== sb) return sa < sb;
676
+ }
677
+ return a.seq < b.seq;
678
+ }
679
+ /** Last-served tick for a key (0 = never served, so an unseen key is admitted before a busy one). */
680
+ servedOf(key) {
681
+ if (key == null) return 0;
682
+ return this.keyServed.get(key) ?? 0;
683
+ }
684
+ };
685
+
686
+ // src/transport-pool.ts
687
+ var TransportPool = class {
688
+ static {
689
+ __name(this, "TransportPool");
690
+ }
691
+ transports;
692
+ constructor(transports) {
693
+ this.transports = transports;
694
+ }
695
+ get size() {
696
+ return this.transports.length;
697
+ }
698
+ /** Register the engine's result/heartbeat/step-event handlers on EVERY transport — a result can come
699
+ * back on whichever one delivered the task, so failover stays symmetric. `onStepEvent` is wired only
700
+ * on transports that carry streamed workflow step lifecycle (BullMQ); others simply skip it. */
701
+ bind(onResult, onHeartbeat, onStepEvent) {
702
+ for (const { transport } of this.transports) {
703
+ transport.onResult(onResult);
704
+ transport.onHeartbeat(onHeartbeat);
705
+ if (onStepEvent && transport.onStepEvent) {
706
+ transport.onStepEvent(onStepEvent);
707
+ }
708
+ }
709
+ }
710
+ /**
711
+ * Dispatch `task` over the pool: try the preferred (or first) transport, fail over to the next on
712
+ * a dispatch error, and stamp the task with the id of the transport that accepted it (so a worker
713
+ * replies on the matching one). Throws if every transport fails.
714
+ */
715
+ async dispatch(task, preferId) {
716
+ let lastErr;
717
+ for (const { id, transport } of this.ordered(preferId)) {
718
+ try {
719
+ await transport.dispatch({
720
+ ...task,
721
+ transport: id
722
+ });
723
+ return;
724
+ } catch (err) {
725
+ lastErr = err;
726
+ }
727
+ }
728
+ throw lastErr ?? new Error("no transport accepted the dispatch");
729
+ }
730
+ /** Worker-health for `group`, merged across every transport that can report it (a group's queue
731
+ * and its workers may live on any pinned transport). Undefined when no transport implements
732
+ * `groupHealth` (e.g. a pure in-process transport — nothing to introspect). */
733
+ async groupHealth(group) {
734
+ const reports = [];
735
+ for (const { transport } of this.transports) {
736
+ if (transport.groupHealth) reports.push(await transport.groupHealth(group));
737
+ }
738
+ if (reports.length === 0) return void 0;
739
+ return {
740
+ group,
741
+ depth: reports.reduce((sum, r) => sum + r.depth, 0),
742
+ liveWorkers: reports.flatMap((r) => r.liveWorkers)
743
+ };
744
+ }
745
+ /** Distinct worker groups with a live heartbeat, merged across every transport that can report it. */
746
+ async listWorkerGroups() {
747
+ const groups = /* @__PURE__ */ new Set();
748
+ for (const { transport } of this.transports) {
749
+ if (transport.listWorkerGroups) {
750
+ for (const g of await transport.listWorkerGroups()) groups.add(g);
751
+ }
752
+ }
753
+ return [
754
+ ...groups
755
+ ];
756
+ }
757
+ /** Pinned `preferId` first, then the rest (failover order). */
758
+ ordered(preferId) {
759
+ if (!preferId) return this.transports;
760
+ const pref = this.transports.filter((t) => t.id === preferId);
761
+ if (pref.length === 0) throw new Error(`transport "${preferId}" is not registered`);
762
+ return [
763
+ ...pref,
764
+ ...this.transports.filter((t) => t.id !== preferId)
765
+ ];
766
+ }
767
+ };
768
+
769
+ // src/completion.ts
770
+ function unwrapCompletion(payload, label) {
771
+ const c = payload;
772
+ if (c && typeof c === "object" && "ok" in c && c.ok === false) {
773
+ throw new FatalError(`${label} failed: ${c.error}`);
774
+ }
775
+ return c?.value;
776
+ }
777
+ __name(unwrapCompletion, "unwrapCompletion");
778
+
779
+ // src/workflow-ref.ts
780
+ var WORKFLOW_NAME_KEY = Symbol.for("nestjs-durable:workflow-name");
781
+ function workflowName(ref) {
782
+ if (typeof ref === "string") return ref;
783
+ const name = ref[WORKFLOW_NAME_KEY];
784
+ if (!name) {
785
+ throw new Error(`workflow class ${ref.name} has no registered name \u2014 is it decorated with @Workflow({ name })?`);
786
+ }
787
+ return name;
788
+ }
789
+ __name(workflowName, "workflowName");
790
+
791
+ // src/workflow-ctx.ts
792
+ var Position = class Position2 {
793
+ static {
794
+ __name(this, "Position");
795
+ }
796
+ seq = -1;
797
+ next() {
798
+ this.seq += 1;
799
+ return this.seq;
800
+ }
801
+ rewind() {
802
+ this.seq -= 1;
803
+ }
804
+ };
805
+ function createWorkflowCtx(host, runId, compensations, workflow = "") {
806
+ const { store, replay } = host;
807
+ const pos = new Position();
808
+ const readCheckpoint = /* @__PURE__ */ __name((seq) => {
809
+ const hit = replay?.get(seq);
810
+ if (hit !== void 0) return hit;
811
+ return store.getCheckpoint(runId, seq);
812
+ }, "readCheckpoint");
813
+ const writeCheckpoint = /* @__PURE__ */ __name(async (cp) => {
814
+ await store.saveCheckpoint(cp);
815
+ replay?.set(cp.seq, cp);
816
+ }, "writeCheckpoint");
817
+ const step = /* @__PURE__ */ __name(async (name, fn, options) => {
818
+ const current = pos.next();
819
+ const existing = await readCheckpoint(current);
820
+ if (existing && existing.name !== name) {
821
+ throw new NonDeterminismError(runId, current, name, existing.name);
822
+ }
823
+ if (existing && existing.status === "completed") {
824
+ if (options?.compensate) compensations.push({
825
+ name,
826
+ fn: options.compensate
827
+ });
828
+ return existing.output;
829
+ }
830
+ const maxAttempts = Math.max(1, options?.retries ?? 1);
831
+ const startedAt = /* @__PURE__ */ new Date();
832
+ await host.startStep({
833
+ runId,
834
+ seq: current,
835
+ name,
836
+ kind: "local",
837
+ attempts: 1,
838
+ enqueuedAt: startedAt,
839
+ startedAt
840
+ });
841
+ for (let attempt = 1; ; attempt += 1) {
842
+ const events = [];
843
+ try {
844
+ const invocation = {
845
+ runId,
846
+ workflow,
847
+ stepName: name,
848
+ seq: current,
849
+ attempt
850
+ };
851
+ const body = /* @__PURE__ */ __name(() => fn(createStepLogger(events, host.clock)), "body");
852
+ const output = host.interceptStep ? await host.interceptStep(invocation, body) : await body();
853
+ await host.completeStep({
854
+ runId,
855
+ seq: current,
856
+ name,
857
+ kind: "local",
858
+ output,
859
+ events,
860
+ attempts: attempt,
861
+ enqueuedAt: startedAt,
862
+ startedAt
863
+ });
864
+ if (options?.compensate) compensations.push({
865
+ name,
866
+ fn: options.compensate
867
+ });
868
+ return output;
869
+ } catch (err) {
870
+ if (err instanceof FatalError || attempt >= maxAttempts) {
871
+ await host.failStep({
872
+ runId,
873
+ seq: current,
874
+ name,
875
+ kind: "local",
876
+ error: {
877
+ message: err instanceof Error ? err.message : String(err),
878
+ stack: err instanceof Error ? err.stack : void 0
879
+ },
880
+ events,
881
+ attempts: attempt,
882
+ enqueuedAt: startedAt,
883
+ startedAt
884
+ });
885
+ throw err;
886
+ }
887
+ const wait = backoffDelay(attempt, options);
888
+ if (wait > 0) await new Promise((resolve) => setTimeout(resolve, wait));
889
+ }
890
+ }
891
+ }, "step");
892
+ const transaction = /* @__PURE__ */ __name(async (name, fn) => {
893
+ if (!store.transaction) {
894
+ throw new Error("ctx.transaction needs a store that supports transactions (the SQL adapters do). Use ctx.step for non-transactional work.");
895
+ }
896
+ const current = pos.next();
897
+ const existing = await readCheckpoint(current);
898
+ if (existing && existing.name !== name) {
899
+ throw new NonDeterminismError(runId, current, name, existing.name);
900
+ }
901
+ if (existing && existing.status === "completed") return existing.output;
902
+ return store.transaction(async (tx) => {
903
+ const output = await fn(tx.raw);
904
+ const cp = instantCheckpoint({
905
+ runId,
906
+ seq: current,
907
+ name,
908
+ kind: "local",
909
+ output
910
+ });
911
+ await tx.saveCheckpoint(cp);
912
+ replay?.set(current, cp);
913
+ return output;
914
+ });
915
+ }, "transaction");
916
+ const suspendUntil = /* @__PURE__ */ __name(async (wakeAt) => {
917
+ const current = pos.next();
918
+ const now2 = host.clock();
919
+ const existing = await readCheckpoint(current);
920
+ if (existing) {
921
+ if (now2 >= (existing.wakeAt ?? 0)) return;
922
+ throw new WorkflowSuspended(existing.wakeAt ?? now2);
923
+ }
924
+ const at = wakeAt();
925
+ await writeCheckpoint(instantCheckpoint({
926
+ runId,
927
+ seq: current,
928
+ name: "sleep",
929
+ kind: "sleep",
930
+ wakeAt: at
931
+ }));
932
+ throw new WorkflowSuspended(at);
933
+ }, "suspendUntil");
934
+ const sleep = /* @__PURE__ */ __name((duration) => suspendUntil(() => host.clock() + parseDuration(duration)), "sleep");
935
+ const sleepUntil = /* @__PURE__ */ __name((when) => suspendUntil(() => typeof when === "number" ? when : when.getTime()), "sleepUntil");
936
+ const continueAsNew = /* @__PURE__ */ __name((input) => {
937
+ throw new ContinueAsNew(input);
938
+ }, "continueAsNew");
939
+ const consumeBuffered = /* @__PURE__ */ __name(async (token, seq) => {
940
+ const buffered = await store.takeBufferedSignal(token);
941
+ if (!buffered) return null;
942
+ await writeCheckpoint(instantCheckpoint({
943
+ runId,
944
+ seq,
945
+ name: `signal:${token}`,
946
+ kind: "signal",
947
+ output: buffered.payload
948
+ }));
949
+ return {
950
+ value: buffered.payload
951
+ };
952
+ }, "consumeBuffered");
953
+ const waitForSignal = /* @__PURE__ */ __name(async (token, opts) => {
954
+ if (opts?.timeoutMs == null) {
955
+ const current = pos.next();
956
+ const existing = await readCheckpoint(current);
957
+ if (existing && existing.status === "completed") return existing.output;
958
+ const buffered2 = await consumeBuffered(token, current);
959
+ if (buffered2) return buffered2.value;
960
+ await store.putSignalWaiter({
961
+ token,
962
+ runId,
963
+ seq: current
964
+ });
965
+ throw new WorkflowSuspended();
966
+ }
967
+ const timeoutMs = opts.timeoutMs;
968
+ const deadlineSeq = pos.next();
969
+ const waitSeq = pos.next();
970
+ const recorded = await readCheckpoint(deadlineSeq);
971
+ const deadline = recorded?.wakeAt ?? host.clock() + timeoutMs;
972
+ if (!recorded) {
973
+ await writeCheckpoint(instantCheckpoint({
974
+ runId,
975
+ seq: deadlineSeq,
976
+ name: `timeout:${token}`,
977
+ kind: "sleep",
978
+ wakeAt: deadline
979
+ }));
980
+ }
981
+ const waited = await readCheckpoint(waitSeq);
982
+ if (waited && waited.status === "completed") return waited.output;
983
+ const buffered = await consumeBuffered(token, waitSeq);
984
+ if (buffered) return buffered.value;
985
+ if (host.clock() >= deadline) {
986
+ await store.takeSignalWaiter(token).catch(() => void 0);
987
+ throw new SignalTimeoutError(token, timeoutMs);
988
+ }
989
+ await store.putSignalWaiter({
990
+ token,
991
+ runId,
992
+ seq: waitSeq
993
+ });
994
+ throw new WorkflowSuspended(deadline);
995
+ }, "waitForSignal");
996
+ const waitForEvent = /* @__PURE__ */ __name(async (name, opts) => {
997
+ if (opts?.timeoutMs == null) {
998
+ const current = pos.next();
999
+ const token2 = eventToken(name, opts?.match, runId, current);
1000
+ const existing = await readCheckpoint(current);
1001
+ if (existing && existing.status === "completed") return existing.output;
1002
+ await store.putSignalWaiter({
1003
+ token: token2,
1004
+ runId,
1005
+ seq: current
1006
+ });
1007
+ throw new WorkflowSuspended();
1008
+ }
1009
+ const timeoutMs = opts.timeoutMs;
1010
+ const deadlineSeq = pos.next();
1011
+ const waitSeq = pos.next();
1012
+ const token = eventToken(name, opts.match, runId, waitSeq);
1013
+ const recorded = await readCheckpoint(deadlineSeq);
1014
+ const deadline = recorded?.wakeAt ?? host.clock() + timeoutMs;
1015
+ if (!recorded) {
1016
+ await writeCheckpoint(instantCheckpoint({
1017
+ runId,
1018
+ seq: deadlineSeq,
1019
+ name: `timeout:event:${name}`,
1020
+ kind: "sleep",
1021
+ wakeAt: deadline
1022
+ }));
1023
+ }
1024
+ const waited = await readCheckpoint(waitSeq);
1025
+ if (waited && waited.status === "completed") return waited.output;
1026
+ if (host.clock() >= deadline) {
1027
+ await store.takeSignalWaiter(token).catch(() => void 0);
1028
+ throw new SignalTimeoutError(`event:${name}`, timeoutMs);
1029
+ }
1030
+ await store.putSignalWaiter({
1031
+ token,
1032
+ runId,
1033
+ seq: waitSeq
1034
+ });
1035
+ throw new WorkflowSuspended(deadline);
1036
+ }, "waitForEvent");
1037
+ const task = /* @__PURE__ */ __name(async (name, dispatch, options) => {
1038
+ await step(`task:dispatch:${name}`, dispatch, options);
1039
+ return unwrapCompletion(await waitForSignal(`task:${runId}:${name}`), `task "${name}"`);
1040
+ }, "task");
1041
+ const child = /* @__PURE__ */ __name(async (workflow2, input, childId) => {
1042
+ const current = pos.next();
1043
+ const id = childId ?? `${runId}.child.${current}`;
1044
+ const existing = await readCheckpoint(current);
1045
+ if (existing && existing.status === "completed") {
1046
+ return unwrapCompletion(existing.output, `child "${id}"`);
1047
+ }
1048
+ await store.putSignalWaiter({
1049
+ token: `child:${id}`,
1050
+ runId,
1051
+ seq: current
1052
+ });
1053
+ if (!await store.getRun(id)) host.startChild(workflowName(workflow2), input, id);
1054
+ if (!existing) {
1055
+ await writeCheckpoint(instantCheckpoint({
1056
+ runId,
1057
+ seq: current,
1058
+ name: `signal:child:${id}`,
1059
+ kind: "signal",
1060
+ status: "running"
1061
+ }));
1062
+ }
1063
+ throw new WorkflowSuspended();
1064
+ }, "child");
1065
+ const startChild = /* @__PURE__ */ __name(async (workflow2, input, childId) => {
1066
+ const current = pos.next();
1067
+ const id = childId ?? `${runId}.child.${current}`;
1068
+ const existing = await readCheckpoint(current);
1069
+ if (existing && existing.status === "completed") return existing.output;
1070
+ if (!await store.getRun(id)) host.startChild(workflowName(workflow2), input, id);
1071
+ await writeCheckpoint(instantCheckpoint({
1072
+ runId,
1073
+ seq: current,
1074
+ name: `spawn:${id}`,
1075
+ kind: "local",
1076
+ output: id
1077
+ }));
1078
+ return id;
1079
+ }, "startChild");
1080
+ const callEntity = /* @__PURE__ */ __name(async (name, key, op, arg) => {
1081
+ const current = pos.next();
1082
+ const existing = await readCheckpoint(current);
1083
+ if (existing && existing.status === "completed") return existing.output;
1084
+ const reply = `entityreply:${runId}:${current}`;
1085
+ await store.putSignalWaiter({
1086
+ token: reply,
1087
+ runId,
1088
+ seq: current
1089
+ });
1090
+ host.signalEntity?.(name, key, op, arg, reply);
1091
+ throw new WorkflowSuspended();
1092
+ }, "callEntity");
1093
+ const signalEntity = /* @__PURE__ */ __name(async (name, key, op, arg) => {
1094
+ const current = pos.next();
1095
+ const existing = await readCheckpoint(current);
1096
+ if (existing && existing.status === "completed") return;
1097
+ host.signalEntity?.(name, key, op, arg);
1098
+ await writeCheckpoint(instantCheckpoint({
1099
+ runId,
1100
+ seq: current,
1101
+ name: `entitysig:${name}:${key}`,
1102
+ kind: "local"
1103
+ }));
1104
+ }, "signalEntity");
1105
+ const breakpoint = /* @__PURE__ */ __name(async (label) => {
1106
+ const current = pos.next();
1107
+ const existing = await readCheckpoint(current);
1108
+ if (existing && existing.status === "completed") return;
1109
+ if (!existing) {
1110
+ await writeCheckpoint(instantCheckpoint({
1111
+ runId,
1112
+ seq: current,
1113
+ name: label ? `breakpoint:${label}` : "breakpoint",
1114
+ kind: "signal",
1115
+ status: "pending"
1116
+ }));
1117
+ await store.putSignalWaiter({
1118
+ token: breakpointToken(runId, current),
1119
+ runId,
1120
+ seq: current
1121
+ });
1122
+ }
1123
+ throw new WorkflowSuspended();
1124
+ }, "breakpoint");
1125
+ const patched = /* @__PURE__ */ __name(async (id) => {
1126
+ const marker = `patch:${id}`;
1127
+ const current = pos.next();
1128
+ const existing = await readCheckpoint(current);
1129
+ if (existing) {
1130
+ if (existing.name === marker) return true;
1131
+ if (existing.name.startsWith("patch:")) {
1132
+ throw new NonDeterminismError(runId, current, marker, existing.name);
1133
+ }
1134
+ pos.rewind();
1135
+ return false;
1136
+ }
1137
+ await writeCheckpoint(instantCheckpoint({
1138
+ runId,
1139
+ seq: current,
1140
+ name: marker,
1141
+ kind: "local",
1142
+ output: true
1143
+ }));
1144
+ return true;
1145
+ }, "patched");
1146
+ const onUpdate = /* @__PURE__ */ __name((name, opts) => waitForSignal(`update:${runId}:${name}`, opts), "onUpdate");
1147
+ const setEvent = /* @__PURE__ */ __name(async (key, value) => {
1148
+ const current = pos.next();
1149
+ const name = `event:${key}`;
1150
+ const existing = await readCheckpoint(current);
1151
+ if (existing && existing.name !== name) {
1152
+ throw new NonDeterminismError(runId, current, name, existing.name);
1153
+ }
1154
+ if (existing && existing.status === "completed") return;
1155
+ await writeCheckpoint(instantCheckpoint({
1156
+ runId,
1157
+ seq: current,
1158
+ name,
1159
+ kind: "local",
1160
+ output: value
1161
+ }));
1162
+ }, "setEvent");
1163
+ const webhook = /* @__PURE__ */ __name(() => {
1164
+ const current = pos.next();
1165
+ const token = `wh:${runId}:${current}`;
1166
+ const wait = /* @__PURE__ */ __name(async () => {
1167
+ const existing = await readCheckpoint(current);
1168
+ if (existing && existing.status === "completed") return existing.output;
1169
+ await store.putSignalWaiter({
1170
+ token,
1171
+ runId,
1172
+ seq: current
1173
+ });
1174
+ throw new WorkflowSuspended();
1175
+ }, "wait");
1176
+ return {
1177
+ token,
1178
+ url: host.webhookUrl?.(token),
1179
+ wait
1180
+ };
1181
+ }, "webhook");
1182
+ const now = /* @__PURE__ */ __name(() => step("now", async () => host.clock()), "now");
1183
+ const random = /* @__PURE__ */ __name(() => step("random", async () => Math.random()), "random");
1184
+ const uuid = /* @__PURE__ */ __name(() => step("uuid", async () => globalThis.crypto.randomUUID()), "uuid");
1185
+ return {
1186
+ runId,
1187
+ step,
1188
+ transaction,
1189
+ callEntity,
1190
+ signalEntity,
1191
+ sleep,
1192
+ sleepUntil,
1193
+ continueAsNew,
1194
+ waitForSignal,
1195
+ waitForEvent,
1196
+ task,
1197
+ child,
1198
+ startChild,
1199
+ breakpoint,
1200
+ webhook,
1201
+ setEvent,
1202
+ onUpdate,
1203
+ patched,
1204
+ now,
1205
+ random,
1206
+ uuid,
1207
+ call: /* @__PURE__ */ __name((remote, input, opts) => host.callRemote(runId, pos.next(), remote, input, opts?.queue, opts?.transport, {
1208
+ priority: opts?.priority,
1209
+ fairnessKey: opts?.fairnessKey
1210
+ }), "call")
1211
+ };
1212
+ }
1213
+ __name(createWorkflowCtx, "createWorkflowCtx");
1214
+
1215
+ // src/engine.ts
1216
+ var SINGLETON_RETRY_MS = 1e3;
1217
+ var SINGLETON_RETRY_JITTER_MS = 250;
1218
+ var singletonRetryWakeAt = /* @__PURE__ */ __name((nowMs) => nowMs + SINGLETON_RETRY_MS + Math.floor((Math.random() * 2 - 1) * SINGLETON_RETRY_JITTER_MS), "singletonRetryWakeAt");
1219
+ var singletonTag = /* @__PURE__ */ __name((cfg, input) => `singleton:${cfg.key(input)}`, "singletonTag");
1220
+ function mergeTags(staticTags, runTags) {
1221
+ if (!staticTags?.length && !runTags?.length) return void 0;
1222
+ return [
1223
+ .../* @__PURE__ */ new Set([
1224
+ ...staticTags ?? [],
1225
+ ...runTags ?? []
1226
+ ])
1227
+ ];
1228
+ }
1229
+ __name(mergeTags, "mergeTags");
1230
+ var BREAKPOINT = "breakpoint";
1231
+ var isBreakpoint = /* @__PURE__ */ __name((cp) => cp.status === "pending" && cp.name.startsWith(BREAKPOINT), "isBreakpoint");
1232
+ var versionKey = /* @__PURE__ */ __name((name, version) => `${name}@${version}`, "versionKey");
1233
+ function nextContinuationId(runId) {
1234
+ const m = runId.match(/^(.*)~(\d+)$/);
1235
+ return m ? `${m[1]}~${Number(m[2]) + 1}` : `${runId}~1`;
1236
+ }
1237
+ __name(nextContinuationId, "nextContinuationId");
1238
+ function isNewerVersion(a, b) {
1239
+ const na = Number(a);
1240
+ const nb = Number(b);
1241
+ if (!Number.isNaN(na) && !Number.isNaN(nb)) return na > nb;
1242
+ return a.localeCompare(b, void 0, {
1243
+ numeric: true
1244
+ }) > 0;
1245
+ }
1246
+ __name(isNewerVersion, "isNewerVersion");
1247
+ var WorkflowEngine = class {
1248
+ static {
1249
+ __name(this, "WorkflowEngine");
1250
+ }
1251
+ store;
1252
+ /** Ordered transport pool (dispatch + failover). Empty = no remote steps. */
1253
+ pool;
1254
+ controlPlane;
1255
+ clock;
1256
+ instanceId;
1257
+ leaseMs;
1258
+ maxRecoveryAttempts;
1259
+ webhookUrl;
1260
+ traceparent;
1261
+ context;
1262
+ /** Establish the originating context ambiently around a local step body (see {@link WorkflowEngineDeps.rehydrate}). Default passthrough. */
1263
+ rehydrate;
1264
+ compensationRetries;
1265
+ /** Persist a `running` checkpoint at the start of a local step body (see {@link WorkflowEngineDeps.trackStepStart}). */
1266
+ trackStepStart;
1267
+ /** Where a freshly-started run executes — in-process by default (see {@link RunDispatcher}). */
1268
+ runDispatcher;
1269
+ /** Every registered workflow, keyed by `name@version` — so old versions stay runnable. */
1270
+ workflows = /* @__PURE__ */ new Map();
1271
+ /** The newest registered version per workflow name — used to `start` new runs. */
1272
+ latest = /* @__PURE__ */ new Map();
1273
+ /** Event name → workflow names started when that event is published (see `onEvent`). */
1274
+ eventTriggers = /* @__PURE__ */ new Map();
1275
+ /** Durable-entity subsystem (registers the `__entity` runner; see `registerEntity`). */
1276
+ entities;
1277
+ /** Event debounce/batch accumulators (register the `__evt_*` runners; see `accumulators.route`). */
1278
+ accumulators;
1279
+ /** In-flight remote steps awaiting a worker result, keyed by stepId. */
1280
+ pending = /* @__PURE__ */ new Map();
1281
+ /** Per-step "reset the liveness timer" callbacks, called when a heartbeat arrives. */
1282
+ heartbeatResets = /* @__PURE__ */ new Map();
1283
+ listeners = /* @__PURE__ */ new Set();
1284
+ /** Step interceptors (onion middleware around real local-step execution), first = outermost. */
1285
+ interceptors = [];
1286
+ /** Callbacks notified (on any instance) when a run is cancelled — for cooperative cancellation. */
1287
+ cancelListeners = /* @__PURE__ */ new Set();
1288
+ /** Callbacks notified when a run is enqueued elsewhere — for low-latency cross-pod dispatch. */
1289
+ enqueuedListeners = /* @__PURE__ */ new Set();
1290
+ /** Notified when a run is dead-lettered (moved to `dead`) — a hook for a DLQ handler. */
1291
+ deadListeners = /* @__PURE__ */ new Set();
1292
+ /** Validators gating `engine.update`, keyed by `<workflow>:<updateName>`. */
1293
+ updateValidators = /* @__PURE__ */ new Map();
1294
+ /** Runs being cancelled WITH saga compensation — see `cancel({ compensate: true })`. */
1295
+ cancelRequested = /* @__PURE__ */ new Set();
1296
+ /** Flow-control queues for remote steps, keyed by name (see {@link registerQueue}). */
1297
+ queues = /* @__PURE__ */ new Map();
1298
+ /** Which queue a dispatched step took a slot from, by stepId — so the result can release it. */
1299
+ stepQueue = /* @__PURE__ */ new Map();
1300
+ /** Executions currently in flight, so a graceful shutdown can wait for them to settle. */
1301
+ inflight = /* @__PURE__ */ new Set();
1302
+ draining = false;
1303
+ constructor(deps) {
1304
+ this.store = deps.store;
1305
+ this.pool = new TransportPool(deps.transports ?? (deps.transport ? [
1306
+ {
1307
+ id: "default",
1308
+ transport: deps.transport
1309
+ }
1310
+ ] : []));
1311
+ this.controlPlane = deps.controlPlane;
1312
+ this.clock = deps.clock ?? Date.now;
1313
+ this.instanceId = deps.instanceId ?? globalThis.crypto.randomUUID();
1314
+ this.leaseMs = deps.leaseMs ?? 3e4;
1315
+ this.maxRecoveryAttempts = deps.maxRecoveryAttempts;
1316
+ this.webhookUrl = deps.webhookUrl;
1317
+ this.traceparent = deps.traceparent;
1318
+ this.context = deps.context;
1319
+ this.rehydrate = deps.rehydrate ?? ((_carrier, fn) => fn());
1320
+ this.compensationRetries = Math.max(1, deps.compensationRetries ?? 1);
1321
+ this.trackStepStart = deps.trackStepStart ?? true;
1322
+ this.runDispatcher = deps.runDispatcher ?? {
1323
+ dispatch: /* @__PURE__ */ __name((runId) => queueMicrotask(() => void this.runOne(runId).catch(() => {
1324
+ })), "dispatch")
1325
+ };
1326
+ this.pool.bind(
1327
+ async (result) => {
1328
+ const waiter = this.pending.get(result.stepId);
1329
+ if (waiter) {
1330
+ this.pending.delete(result.stepId);
1331
+ if (result.status === "completed") {
1332
+ waiter.resolve({
1333
+ output: result.output,
1334
+ startedAt: result.startedAt,
1335
+ events: result.events
1336
+ });
1337
+ } else {
1338
+ waiter.reject(new RemoteStepError(result.error));
1339
+ }
1340
+ return;
1341
+ }
1342
+ await this.completeRemoteResult(result);
1343
+ },
1344
+ // A heartbeat for an in-flight long step resets its liveness window (see callRemote).
1345
+ async (beat) => {
1346
+ this.heartbeatResets.get(beat.stepId)?.();
1347
+ },
1348
+ // A remote workflow worker streams each local step's lifecycle (running → completed/failed) so
1349
+ // it's checkpointed live, not all-at-once when the long turn ends.
1350
+ async (event) => {
1351
+ await this.persistStepEvent(event);
1352
+ }
1353
+ );
1354
+ this.controlPlane?.onControl((msg) => {
1355
+ if (msg.from === this.instanceId) return;
1356
+ if (msg.kind === "event") {
1357
+ this.deliver({
1358
+ ...msg.event,
1359
+ at: new Date(msg.event.at)
1360
+ });
1361
+ } else if (msg.kind === "cancel") {
1362
+ this.notifyCancelled(msg.runId);
1363
+ } else if (msg.kind === "enqueued") {
1364
+ for (const fn of this.enqueuedListeners) {
1365
+ try {
1366
+ fn(msg.runId);
1367
+ } catch {
1368
+ }
1369
+ }
1370
+ }
1371
+ });
1372
+ this.accumulators = new EventAccumulators(this);
1373
+ this.entities = new Entities(this);
1374
+ }
1375
+ /**
1376
+ * Register a **durable entity** (a virtual object): a keyed actor whose `handlers` run **serialized
1377
+ * per key** over **durable state**, exactly once. Drive it with `signalEntity` (fire) /
1378
+ * `ctx.callEntity` (call + await result) and read its state with `getEntityState`. See {@link Entities}.
1379
+ */
1380
+ registerEntity(name, config) {
1381
+ this.entities.register(name, config);
1382
+ }
1383
+ /** Send an operation to an entity (fire-and-forget). Ordered + exactly-once per key. */
1384
+ signalEntity(name, key, op, arg) {
1385
+ return this.entities.signal(name, key, op, arg);
1386
+ }
1387
+ /** Read an entity's current durable state (published after each op), or undefined if it has none yet. */
1388
+ getEntityState(name, key) {
1389
+ return this.entities.getState(name, key);
1390
+ }
1391
+ /**
1392
+ * Be notified when a run is enqueued on ANOTHER instance (via the control plane), so a worker can
1393
+ * pick it up at once — e.g. `engine.onEnqueued((runId) => engine.runOne(runId))`. Returns an
1394
+ * unsubscribe function. Only wire this on instances that should execute runs (workers).
1395
+ */
1396
+ onEnqueued(listener) {
1397
+ this.enqueuedListeners.add(listener);
1398
+ return () => this.enqueuedListeners.delete(listener);
1399
+ }
1400
+ /** Fire cooperative-cancellation listeners for `runId` (a worker bridge aborts in-flight work). */
1401
+ notifyCancelled(runId) {
1402
+ for (const fn of this.cancelListeners) {
1403
+ try {
1404
+ fn(runId);
1405
+ } catch {
1406
+ }
1407
+ }
1408
+ }
1409
+ /**
1410
+ * Register a workflow version. Register multiple versions of the same name to keep in-flight
1411
+ * runs working across a breaking change: old runs resume on the version they started on, new
1412
+ * runs start on the newest registered version.
1413
+ */
1414
+ register(name, version, fn, opts) {
1415
+ const registered = {
1416
+ name,
1417
+ version,
1418
+ fn,
1419
+ tags: opts?.tags,
1420
+ singleton: opts?.singleton,
1421
+ executionTimeoutMs: opts?.executionTimeout != null ? parseDuration(opts.executionTimeout) : void 0,
1422
+ validateInput: opts?.validateInput,
1423
+ onEvent: opts?.onEvent,
1424
+ eventBatch: opts?.eventBatch
1425
+ };
1426
+ this.workflows.set(versionKey(name, version), registered);
1427
+ const current = this.latest.get(name);
1428
+ if (!current || isNewerVersion(version, current.version)) this.latest.set(name, registered);
1429
+ for (const event of opts?.onEvent ?? []) {
1430
+ const subscribers = this.eventTriggers.get(event) ?? /* @__PURE__ */ new Set();
1431
+ subscribers.add(name);
1432
+ this.eventTriggers.set(event, subscribers);
1433
+ }
1434
+ }
1435
+ /**
1436
+ * Register a workflow whose body runs in another SDK (e.g. Python). The engine owns the run exactly
1437
+ * as for a TS workflow — it persists checkpoints, recovers, runs timers — but advances it by handing
1438
+ * the run's history to `executor` (which dispatches a {@link WorkflowTask} to the worker) and applying
1439
+ * the {@link WorkflowDecision} the worker's replay returns. The worker never touches the store.
1440
+ */
1441
+ registerRemote(name, version, opts) {
1442
+ const registered = {
1443
+ name,
1444
+ version,
1445
+ // A remote workflow has no in-process body; execute() branches on `remote` before this is read.
1446
+ fn: /* @__PURE__ */ __name(() => {
1447
+ throw new Error(`workflow ${name} is remote \u2014 it has no in-process body`);
1448
+ }, "fn"),
1449
+ tags: opts.tags,
1450
+ singleton: opts.singleton,
1451
+ executionTimeoutMs: opts.executionTimeout != null ? parseDuration(opts.executionTimeout) : void 0,
1452
+ validateInput: opts.validateInput,
1453
+ remote: {
1454
+ group: opts.group,
1455
+ executor: opts.executor
1456
+ }
1457
+ };
1458
+ this.workflows.set(versionKey(name, version), registered);
1459
+ const current = this.latest.get(name);
1460
+ if (!current || isNewerVersion(version, current.version)) this.latest.set(name, registered);
1461
+ }
1462
+ /**
1463
+ * Register a flow-control queue referenced by `ctx.call(step, input, { queue })`. Caps concurrent
1464
+ * in-flight steps and/or the admission rate; blocked calls re-suspend and retry, so the limit is
1465
+ * durable. Per engine instance (see {@link QueueConfig}). Registering the same name replaces it.
1466
+ */
1467
+ registerQueue(config) {
1468
+ this.queues.set(config.name, new QueueController(config, this.clock));
1469
+ }
1470
+ /** Subscribe to lifecycle events. Returns an unsubscribe function. */
1471
+ subscribe(listener) {
1472
+ this.listeners.add(listener);
1473
+ return () => this.listeners.delete(listener);
1474
+ }
1475
+ /**
1476
+ * Register a {@link StepInterceptor} — onion middleware run around the real execution of every
1477
+ * local `ctx.step` (timing, logging, tracing, error enrichment, context propagation). First
1478
+ * registered is outermost; interceptors fire only when a step executes, never on replay. Returns
1479
+ * an unsubscribe function.
1480
+ */
1481
+ use(interceptor) {
1482
+ this.interceptors.push(interceptor);
1483
+ return () => {
1484
+ const i = this.interceptors.indexOf(interceptor);
1485
+ if (i >= 0) this.interceptors.splice(i, 1);
1486
+ };
1487
+ }
1488
+ /**
1489
+ * Fold the registered interceptors around a local step body (identity when there are none), then
1490
+ * run the whole thing inside the re-hydrated originating context. The carrier is read at execution
1491
+ * time from {@link context} — the SAME reader stamped on dispatched remote tasks — so a local step
1492
+ * sees the live tenant / user / trace ids ambiently (via the {@link rehydrate} bridge). Default
1493
+ * `rehydrate` is a passthrough, so this is byte-identical to a bare body call when unwired.
1494
+ */
1495
+ interceptStep(invocation, body) {
1496
+ const carrier = this.context?.();
1497
+ const run = /* @__PURE__ */ __name(() => {
1498
+ if (this.interceptors.length === 0) return body();
1499
+ const chain = this.interceptors.reduceRight((next, interceptor) => () => interceptor(invocation, next), body);
1500
+ return chain();
1501
+ }, "run");
1502
+ return this.rehydrate(carrier, run);
1503
+ }
1504
+ /**
1505
+ * Be notified when a run is cancelled — on ANY instance, via the transport control plane. A
1506
+ * worker bridge can use this for cooperative cancellation: abort the in-flight work for `runId`
1507
+ * instead of finishing it just to have the result discarded. Returns an unsubscribe function.
1508
+ */
1509
+ onCancel(listener) {
1510
+ this.cancelListeners.add(listener);
1511
+ return () => this.cancelListeners.delete(listener);
1512
+ }
1513
+ /**
1514
+ * Be notified when a run is **dead-lettered** — moved to `dead` after exceeding
1515
+ * `maxRecoveryAttempts`. The listener receives the dead run (status `dead`, with its error), so a
1516
+ * DLQ handler can do something other than just leaving it parked: alert, push to a real queue, or
1517
+ * start a dead-letter workflow (e.g. `engine.onDead((run) => engine.start('pipeline-dlq', run, ...))`).
1518
+ * Returns an unsubscribe function.
1519
+ */
1520
+ onDead(listener) {
1521
+ this.deadListeners.add(listener);
1522
+ return () => this.deadListeners.delete(listener);
1523
+ }
1524
+ notifyDead(run) {
1525
+ for (const fn of this.deadListeners) {
1526
+ try {
1527
+ fn(run);
1528
+ } catch {
1529
+ }
1530
+ }
1531
+ }
1532
+ /** Emit a locally-produced lifecycle event: deliver to subscribers AND broadcast it on the
1533
+ * control plane so other instances (e.g. a dashboard pod) can live-tail this run. */
1534
+ emit(event) {
1535
+ const full = {
1536
+ ...event,
1537
+ at: /* @__PURE__ */ new Date()
1538
+ };
1539
+ this.deliver(full);
1540
+ if (this.controlPlane) {
1541
+ void this.controlPlane.publishControl({
1542
+ kind: "event",
1543
+ event: full,
1544
+ from: this.instanceId
1545
+ }).catch(() => {
1546
+ });
1547
+ }
1548
+ }
1549
+ /** Deliver an event to local subscribers only (no re-broadcast) — used for both locally-produced
1550
+ * events and ones received from the control plane, so an event shows up once on every instance. */
1551
+ deliver(event) {
1552
+ for (const listener of this.listeners) {
1553
+ try {
1554
+ listener(event);
1555
+ } catch {
1556
+ }
1557
+ }
1558
+ }
1559
+ async start(workflow, input, runId, opts) {
1560
+ const name = workflowName(workflow);
1561
+ const registered = this.latest.get(name);
1562
+ if (!registered) throw new Error(`workflow ${name} is not registered`);
1563
+ await registered.validateInput?.(input);
1564
+ const prior = await this.store.getRun(runId);
1565
+ if (prior) {
1566
+ return {
1567
+ runId,
1568
+ status: prior.status,
1569
+ output: prior.output,
1570
+ error: prior.error
1571
+ };
1572
+ }
1573
+ const now = /* @__PURE__ */ new Date();
1574
+ const tags = mergeTags(registered.tags, registered.singleton ? [
1575
+ ...opts?.tags ?? [],
1576
+ singletonTag(registered.singleton, input)
1577
+ ] : opts?.tags);
1578
+ const maxQueueDepth = registered.singleton?.maxQueueDepth;
1579
+ if (registered.singleton && maxQueueDepth != null) {
1580
+ const cfg = registered.singleton;
1581
+ const key = cfg.key(input);
1582
+ const cap = (cfg.limit ?? 1) + maxQueueDepth;
1583
+ const queued = await this.store.listRuns({
1584
+ tag: singletonTag(cfg, input),
1585
+ workflow: name,
1586
+ statuses: [
1587
+ "pending",
1588
+ "running",
1589
+ "suspended"
1590
+ ]
1591
+ });
1592
+ if (queued.length >= cap) {
1593
+ throw new SingletonQueueFullError(name, key, maxQueueDepth);
1594
+ }
1595
+ }
1596
+ const run = {
1597
+ id: runId,
1598
+ workflow: name,
1599
+ workflowVersion: registered.version,
1600
+ status: "pending",
1601
+ input,
1602
+ tags,
1603
+ searchAttributes: opts?.searchAttributes,
1604
+ createdAt: now,
1605
+ updatedAt: now
1606
+ };
1607
+ await this.store.createRun(run);
1608
+ await this.runDispatcher.dispatch(runId);
1609
+ if (this.controlPlane) {
1610
+ void this.controlPlane.publishControl({
1611
+ kind: "enqueued",
1612
+ runId,
1613
+ from: this.instanceId
1614
+ }).catch(() => void 0);
1615
+ }
1616
+ return {
1617
+ runId,
1618
+ status: "pending"
1619
+ };
1620
+ }
1621
+ /** Read a run's current persisted state (or null if unknown). A thin pass-through to the store. */
1622
+ getRun(runId) {
1623
+ return this.store.getRun(runId);
1624
+ }
1625
+ async resume(runId) {
1626
+ const run = await this.store.getRun(runId);
1627
+ if (!run) throw new Error(`run ${runId} not found`);
1628
+ if (run.status === "cancelled" || run.status === "completed" || run.status === "dead") {
1629
+ return {
1630
+ runId,
1631
+ status: run.status,
1632
+ output: run.output,
1633
+ error: run.error
1634
+ };
1635
+ }
1636
+ const registered = this.workflows.get(versionKey(run.workflow, run.workflowVersion));
1637
+ if (!registered) {
1638
+ throw new Error(`workflow ${run.workflow}@${run.workflowVersion} is not registered \u2014 keep the prior version deployed so in-flight runs can drain (skew protection)`);
1639
+ }
1640
+ return this.track(this.execute(run, registered.fn));
1641
+ }
1642
+ /** Track an in-flight execution so {@link drain} can wait for it. */
1643
+ track(p) {
1644
+ this.inflight.add(p);
1645
+ void p.finally(() => this.inflight.delete(p));
1646
+ return p;
1647
+ }
1648
+ /**
1649
+ * Graceful shutdown: stop picking up new runs (recovery/timer become no-ops) and wait for
1650
+ * in-flight executions to settle, up to `timeoutMs`. Call from your app's shutdown hook so a
1651
+ * deploy hands off cleanly instead of leaving runs to the lease timeout.
1652
+ */
1653
+ async drain(timeoutMs = 1e4) {
1654
+ this.draining = true;
1655
+ if (this.inflight.size === 0) return;
1656
+ const timer = new Promise((resolve) => {
1657
+ const t = setTimeout(resolve, timeoutMs);
1658
+ t.unref?.();
1659
+ });
1660
+ await Promise.race([
1661
+ Promise.allSettled([
1662
+ ...this.inflight
1663
+ ]),
1664
+ timer
1665
+ ]);
1666
+ }
1667
+ /**
1668
+ * Cancel in-flight runs that have outlived their workflow's `executionTimeout`. Call it from the
1669
+ * timer poller alongside {@link resumeDueTimers}. A timed-out run is moved to `cancelled` with an
1670
+ * `execution_timeout` error (terminal, so a late step result can't resurrect it).
1671
+ */
1672
+ async sweepTimeouts(now = this.clock()) {
1673
+ for (const reg of new Set(this.latest.values())) {
1674
+ if (reg.executionTimeoutMs == null) continue;
1675
+ const deadline = now - reg.executionTimeoutMs;
1676
+ const inflight = [
1677
+ ...await this.store.listRuns({
1678
+ workflow: reg.name,
1679
+ status: "running"
1680
+ }),
1681
+ ...await this.store.listRuns({
1682
+ workflow: reg.name,
1683
+ status: "suspended"
1684
+ })
1685
+ ];
1686
+ for (const run of inflight) {
1687
+ if (run.createdAt.getTime() > deadline) continue;
1688
+ const error = {
1689
+ message: "execution timeout",
1690
+ code: "execution_timeout"
1691
+ };
1692
+ await this.store.updateRun(run.id, {
1693
+ status: "cancelled",
1694
+ error,
1695
+ updatedAt: /* @__PURE__ */ new Date()
1696
+ });
1697
+ this.emit({
1698
+ type: "run.failed",
1699
+ runId: run.id,
1700
+ workflow: run.workflow,
1701
+ error
1702
+ });
1703
+ }
1704
+ }
1705
+ }
1706
+ /**
1707
+ * Resume every run left incomplete by a crash or deploy. Called on boot. Completed steps
1708
+ * replay from their checkpoints, so only the work that had not finished runs again.
1709
+ */
1710
+ async recoverIncomplete(nowMs = this.clock()) {
1711
+ if (this.draining) return [];
1712
+ const results = [];
1713
+ for (const run of await this.store.listIncompleteRuns()) {
1714
+ const acquired = await this.store.tryLockRun(run.id, this.instanceId, nowMs + this.leaseMs, nowMs);
1715
+ if (!acquired) continue;
1716
+ const settled = await this.countRecovery(run);
1717
+ if (settled) {
1718
+ results.push(settled);
1719
+ continue;
1720
+ }
1721
+ await this.store.releaseRunLock(run.id);
1722
+ await this.store.updateRun(run.id, {
1723
+ status: "pending",
1724
+ updatedAt: /* @__PURE__ */ new Date()
1725
+ });
1726
+ await this.runDispatcher.dispatch(run.id);
1727
+ results.push({
1728
+ runId: run.id,
1729
+ status: "pending"
1730
+ });
1731
+ }
1732
+ return results;
1733
+ }
1734
+ /**
1735
+ * Per-recovery bookkeeping (called once the lease is held): count the attempt, or — past
1736
+ * `maxRecoveryAttempts` — move a poison pill to the `dead` dead-letter state. Returns a terminal
1737
+ * result to skip the resume, or `undefined` to proceed.
1738
+ */
1739
+ async countRecovery(run) {
1740
+ const attempts = (run.recoveryAttempts ?? 0) + 1;
1741
+ if (this.maxRecoveryAttempts != null && attempts > this.maxRecoveryAttempts) {
1742
+ const error = {
1743
+ message: `run exceeded maxRecoveryAttempts (${this.maxRecoveryAttempts}) \u2014 moved to dead-letter`,
1744
+ code: "max_recovery_attempts"
1745
+ };
1746
+ await this.store.updateRun(run.id, {
1747
+ status: "dead",
1748
+ error,
1749
+ updatedAt: /* @__PURE__ */ new Date()
1750
+ });
1751
+ await this.store.releaseRunLock(run.id);
1752
+ this.emit({
1753
+ type: "run.failed",
1754
+ runId: run.id,
1755
+ workflow: run.workflow,
1756
+ error
1757
+ });
1758
+ this.notifyDead({
1759
+ ...run,
1760
+ status: "dead",
1761
+ error,
1762
+ recoveryAttempts: attempts
1763
+ });
1764
+ return {
1765
+ runId: run.id,
1766
+ status: "dead",
1767
+ error
1768
+ };
1769
+ }
1770
+ await this.store.updateRun(run.id, {
1771
+ recoveryAttempts: attempts,
1772
+ updatedAt: /* @__PURE__ */ new Date()
1773
+ });
1774
+ return void 0;
1775
+ }
1776
+ /**
1777
+ * Resume every suspended run whose durable timer is due. Call periodically (a poller) and on
1778
+ * boot. A run still not due re-suspends cheaply without running new work.
1779
+ */
1780
+ async resumeDueTimers(nowMs = this.clock()) {
1781
+ return this.resumeLeased(await this.store.listDueTimers(nowMs), nowMs);
1782
+ }
1783
+ /**
1784
+ * Lease and execute one run by id — the worker side of dispatch. Acquires the recovery lease (so
1785
+ * exactly one instance runs it), then resumes the body. Returns the result, or null if another
1786
+ * instance holds the lease or the engine is draining. The default in-process dispatcher calls this;
1787
+ * a broker-backed worker calls it per consumed run id.
1788
+ */
1789
+ async runOne(runId) {
1790
+ if (this.draining) return null;
1791
+ const nowMs = this.clock();
1792
+ const acquired = await this.store.tryLockRun(runId, this.instanceId, nowMs + this.leaseMs, nowMs);
1793
+ if (!acquired) return null;
1794
+ return this.resume(runId);
1795
+ }
1796
+ /**
1797
+ * Re-enqueue a run for a worker to (re-)execute — the dispatch-model **retry**. Sets it back to
1798
+ * `pending`, clears any stale lease, and dispatches; a worker resumes it (replaying its checkpoints,
1799
+ * re-attempting the failed step). Returns the enqueued state immediately — never runs the body
1800
+ * inline — or null if the run is unknown. The dashboard "retry" goes through here so it can't block
1801
+ * the HTTP request on workflow execution.
1802
+ */
1803
+ async requeue(runId) {
1804
+ const run = await this.store.getRun(runId);
1805
+ if (!run) return null;
1806
+ await this.store.releaseRunLock(runId);
1807
+ await this.store.updateRun(runId, {
1808
+ status: "pending",
1809
+ updatedAt: /* @__PURE__ */ new Date()
1810
+ });
1811
+ await this.runDispatcher.dispatch(runId);
1812
+ return {
1813
+ runId,
1814
+ status: "pending"
1815
+ };
1816
+ }
1817
+ /**
1818
+ * **Fix-and-replay**: re-run a run (typically a `dead`/`failed` one) with a corrected `input`, as a
1819
+ * fresh run with clean history. It's a NEW run — `newRunId` defaults to `<runId>~retry~<uuid>` — so
1820
+ * the original stays inspectable. Returns the new run's id, or null if `runId` is unknown.
1821
+ */
1822
+ async retryWithInput(runId, input, newRunId) {
1823
+ const run = await this.store.getRun(runId);
1824
+ if (!run) return null;
1825
+ const id = newRunId ?? `${runId}~retry~${globalThis.crypto.randomUUID().slice(0, 8)}`;
1826
+ await this.start(run.workflow, input, id, {
1827
+ tags: run.tags
1828
+ });
1829
+ return {
1830
+ runId: id
1831
+ };
1832
+ }
1833
+ /**
1834
+ * Pick up and execute every `pending` run — the poll-based side of dispatch for a worker pod with
1835
+ * no broker. Runs enqueued by other pods (or by a caller using a no-op dispatcher) sit `pending`
1836
+ * in the store until polled; leasing ensures exactly one pod runs each. Call periodically alongside
1837
+ * {@link resumeDueTimers}.
1838
+ */
1839
+ async runPending(nowMs = this.clock()) {
1840
+ return this.resumeLeased(await this.store.listPendingRuns(100), nowMs);
1841
+ }
1842
+ /**
1843
+ * Resolve once `runId` reaches a settled state — terminal (completed/failed/cancelled/dead) or
1844
+ * suspended (handed off to a timer/signal/event). The async counterpart to dispatch: pair it with
1845
+ * `start` when a call site needs the outcome — `await start(...); const r = await waitForRun(id)`.
1846
+ */
1847
+ waitForRun(runId, opts) {
1848
+ const isSettled = /* @__PURE__ */ __name((s) => s !== "pending" && s !== "running", "isSettled");
1849
+ const toResult = /* @__PURE__ */ __name((run) => ({
1850
+ runId,
1851
+ status: run.status,
1852
+ output: run.output,
1853
+ error: run.error
1854
+ }), "toResult");
1855
+ return new Promise((resolve, reject) => {
1856
+ let done = false;
1857
+ let off = /* @__PURE__ */ __name(() => {
1858
+ }, "off");
1859
+ const timer = opts?.timeoutMs != null ? setTimeout(() => {
1860
+ if (done) return;
1861
+ done = true;
1862
+ off();
1863
+ reject(new Error(`waitForRun(${runId}) timed out after ${opts.timeoutMs}ms`));
1864
+ }, opts.timeoutMs) : void 0;
1865
+ const finish = /* @__PURE__ */ __name((run) => {
1866
+ if (done) return;
1867
+ done = true;
1868
+ if (timer) clearTimeout(timer);
1869
+ off();
1870
+ resolve(toResult(run));
1871
+ }, "finish");
1872
+ const check = /* @__PURE__ */ __name(() => {
1873
+ void this.store.getRun(runId).then((run) => {
1874
+ if (run && isSettled(run.status)) finish(run);
1875
+ });
1876
+ }, "check");
1877
+ off = this.subscribe((ev) => {
1878
+ if (ev.runId === runId && (ev.type === "run.completed" || ev.type === "run.failed" || ev.type === "run.suspended")) {
1879
+ check();
1880
+ }
1881
+ });
1882
+ check();
1883
+ });
1884
+ }
1885
+ /**
1886
+ * Resume each run only if this instance can acquire its recovery lease — so when several
1887
+ * replicas recover or poll at once, each run is picked up by exactly one of them.
1888
+ */
1889
+ async resumeLeased(runs, nowMs = this.clock(), onLocked) {
1890
+ if (this.draining) return [];
1891
+ const results = [];
1892
+ for (const run of runs) {
1893
+ const acquired = await this.store.tryLockRun(run.id, this.instanceId, nowMs + this.leaseMs, nowMs);
1894
+ if (!acquired) continue;
1895
+ const settled = onLocked ? await onLocked(run) : void 0;
1896
+ results.push(settled ?? await this.resume(run.id));
1897
+ }
1898
+ return results;
1899
+ }
1900
+ /**
1901
+ * Deliver an external signal to the run waiting on `token` and resume it with `payload`.
1902
+ * Returns the run result, or null if no run is waiting for that token.
1903
+ */
1904
+ /**
1905
+ * Publish a named event. It does two things, and returns how many runs it touched (the sum):
1906
+ * 1. **Resumes** every in-flight run waiting on it via `ctx.waitForEvent(name, { match })` whose
1907
+ * match the payload satisfies (fan-out, vs `signal`'s point-to-point token).
1908
+ * 2. **Starts** a fresh run of every workflow registered with `onEvent: [name]`, passing the
1909
+ * payload as input. Idempotent by `evt:<id>:<workflow>` — pass `opts.id` to dedupe redeliveries
1910
+ * of the same logical event (default: a fresh uuid, so each publish triggers once).
1911
+ */
1912
+ async publishEvent(name, payload, opts) {
1913
+ let touched = 0;
1914
+ const waiters = await this.store.listSignalWaiters(eventPrefix(name));
1915
+ for (const w of waiters) {
1916
+ if (eventMatches(payload, eventMatchOf(w.token))) {
1917
+ await this.signal(w.token, payload);
1918
+ touched += 1;
1919
+ }
1920
+ }
1921
+ const subscribers = this.eventTriggers.get(name);
1922
+ if (subscribers?.size) {
1923
+ const eventId = opts?.id ?? globalThis.crypto.randomUUID();
1924
+ for (const workflow of subscribers) {
1925
+ try {
1926
+ const batch = this.latest.get(workflow)?.eventBatch;
1927
+ if (batch) {
1928
+ await this.accumulators.route(workflow, batch, payload);
1929
+ } else {
1930
+ await this.start(workflow, payload, `evt:${eventId}:${workflow}`);
1931
+ }
1932
+ touched += 1;
1933
+ } catch {
1934
+ }
1935
+ }
1936
+ }
1937
+ return touched;
1938
+ }
1939
+ async signal(token, payload) {
1940
+ const waiter = await this.store.takeSignalWaiter(token);
1941
+ if (!waiter) {
1942
+ await this.store.bufferSignal(token, payload);
1943
+ return null;
1944
+ }
1945
+ await this.store.saveCheckpoint(instantCheckpoint({
1946
+ runId: waiter.runId,
1947
+ seq: waiter.seq,
1948
+ name: `signal:${token}`,
1949
+ kind: "signal",
1950
+ output: payload
1951
+ }));
1952
+ return this.resume(waiter.runId);
1953
+ }
1954
+ /**
1955
+ * Ensure a run exists for `runId`, then deliver a signal to it — atomically race-free thanks to
1956
+ * signal buffering: if the run is new (or busy / not yet waiting), the signal is buffered and
1957
+ * consumed when it reaches `waitForSignal(token)`. The canonical **durable-entity / accumulator**
1958
+ * pattern: one long-lived run per key (the `runId`) that loops on `waitForSignal`, fed events by
1959
+ * many `signalWithStart` calls. `start` is idempotent by `runId`, so concurrent callers converge on
1960
+ * one run. (Use a per-run `token`, e.g. derived from `runId`, so the signal targets this entity.)
1961
+ */
1962
+ async signalWithStart(workflow, input, runId, signal, opts) {
1963
+ await this.start(workflow, input, runId, opts);
1964
+ await this.signal(signal.token, signal.payload);
1965
+ return {
1966
+ runId
1967
+ };
1968
+ }
1969
+ /**
1970
+ * Report the result of a `ctx.task(name, …)` back to its run (async completion). The external
1971
+ * worker that the task dispatched to calls this when done; the run resumes with `result`. Returns
1972
+ * null if no run is waiting on the task (e.g. a duplicate/late delivery) — a safe no-op.
1973
+ */
1974
+ async completeTask(runId, name, result) {
1975
+ return this.signal(`task:${runId}:${name}`, {
1976
+ ok: true,
1977
+ value: result
1978
+ });
1979
+ }
1980
+ /** Report that a `ctx.task` failed — the run resumes and throws a FatalError at the task. */
1981
+ async failTask(runId, name, error) {
1982
+ return this.signal(`task:${runId}:${name}`, {
1983
+ ok: false,
1984
+ error
1985
+ });
1986
+ }
1987
+ /**
1988
+ * Notify a parent that's waiting on `runId` as a child of its terminal outcome (the `ctx.child`
1989
+ * rendezvous). A no-op when no parent is waiting, so `execute()` can call it on every run without
1990
+ * knowing about the child feature.
1991
+ */
1992
+ notifyParent(runId, completion) {
1993
+ void this.signal(`child:${runId}`, completion).catch(() => void 0);
1994
+ }
1995
+ /**
1996
+ * Cancel a run (e.g. from the dashboard). Returns null if the run does not exist. Pass
1997
+ * `{ compensate: true }` to undo the saga first: the suspended run is resumed so its completed
1998
+ * steps' compensations run in reverse (visible as `compensate:<step>` events), then it's marked
1999
+ * cancelled. Without it, cancellation is immediate (no undo).
2000
+ */
2001
+ async cancel(runId, opts) {
2002
+ const run = await this.store.getRun(runId);
2003
+ if (!run) return null;
2004
+ if (run.status === "completed" || run.status === "cancelled" || run.status === "dead") {
2005
+ return {
2006
+ runId,
2007
+ status: run.status,
2008
+ output: run.output,
2009
+ error: run.error
2010
+ };
2011
+ }
2012
+ if (opts?.compensate && (run.status === "suspended" || run.status === "running")) {
2013
+ this.cancelRequested.add(runId);
2014
+ if (this.controlPlane) {
2015
+ void this.controlPlane.publishControl({
2016
+ kind: "cancel",
2017
+ runId,
2018
+ from: this.instanceId
2019
+ }).catch(() => void 0);
2020
+ }
2021
+ queueMicrotask(() => {
2022
+ void this.resume(runId).then(() => this.notifyCancelled(runId)).catch(() => void 0);
2023
+ });
2024
+ await this.cancelChildren(runId, opts);
2025
+ return {
2026
+ runId,
2027
+ status: run.status
2028
+ };
2029
+ }
2030
+ const error = {
2031
+ message: "cancelled"
2032
+ };
2033
+ await this.store.updateRun(runId, {
2034
+ status: "cancelled",
2035
+ error,
2036
+ updatedAt: /* @__PURE__ */ new Date()
2037
+ });
2038
+ this.emit({
2039
+ type: "run.failed",
2040
+ runId,
2041
+ workflow: run.workflow,
2042
+ error
2043
+ });
2044
+ await this.cancelChildren(runId, opts);
2045
+ this.notifyCancelled(runId);
2046
+ if (this.controlPlane) {
2047
+ void this.controlPlane.publishControl({
2048
+ kind: "cancel",
2049
+ runId,
2050
+ from: this.instanceId
2051
+ }).catch(() => void 0);
2052
+ }
2053
+ void this.wakeNextSingletons(run).catch(() => void 0);
2054
+ return {
2055
+ runId,
2056
+ status: "cancelled",
2057
+ error
2058
+ };
2059
+ }
2060
+ /**
2061
+ * Bulk-cancel every run matching a filter — e.g. cancel all `order` runs tagged `vip`, or every run
2062
+ * whose `tier` search attribute is `free`. The filter is a {@link RunQuery} (workflow / status / tag
2063
+ * / search-attribute predicates), so it reuses the same matching the dashboard list uses. Each match
2064
+ * is run through {@link cancel}, so the same plumbing applies per run: child cascade, the optional
2065
+ * saga `compensate`, local cancel listeners, and the control-plane broadcast that tells the owning
2066
+ * worker to abort. Returns one {@link RunResult} per matched run (already-finished matches report
2067
+ * their terminal status — `cancel` is a no-op on them, never clobbering a completed/dead run).
2068
+ */
2069
+ async cancelWhere(filter, opts) {
2070
+ const runs = await this.store.listRuns(filter);
2071
+ const results = [];
2072
+ for (const run of runs) {
2073
+ const r = await this.cancel(run.id, opts);
2074
+ if (r) results.push(r);
2075
+ }
2076
+ return results;
2077
+ }
2078
+ /**
2079
+ * Cascade cancellation to a run's children — both awaited (`ctx.child`, found via its live
2080
+ * `child:<id>` waiter) and fire-and-forget (`ctx.startChild`, found via its `spawn:<id>`
2081
+ * checkpoint). Recursive, so a whole subtree is cancelled; the terminal guard in `cancel` stops it
2082
+ * at finished / already-cancelled runs (no loops, no re-cancel).
2083
+ */
2084
+ async cancelChildren(parentRunId, opts) {
2085
+ for (const id of await this.getRunChildren(parentRunId)) {
2086
+ await this.cancel(id, opts).catch(() => void 0);
2087
+ }
2088
+ }
2089
+ /**
2090
+ * The ids of the runs a run spawned — both awaited (`ctx.child`) and fire-and-forget
2091
+ * (`ctx.startChild`, found via its `spawn:<id>` checkpoint). The canonical parent→children edge,
2092
+ * used for both cancellation cascades and the dashboard run-tree.
2093
+ *
2094
+ * An awaited child is discovered two ways, because the live `child:<id>` waiter only exists WHILE
2095
+ * the parent is suspended on it: the waiter resolves and is consumed the moment the child settles,
2096
+ * so a completed parent (or a completed awaited child) would otherwise drop out of the tree. The
2097
+ * `signal:child:<id>` checkpoint (the placeholder written when the parent first awaits the child,
2098
+ * overwritten as completed/failed when it settles) persists across completion, so we read both and
2099
+ * dedupe — the edge stays stable for a finished run, not just a live one.
2100
+ */
2101
+ async getRunChildren(parentRunId) {
2102
+ const childIds = /* @__PURE__ */ new Set();
2103
+ for (const w of await this.store.listSignalWaiters("child:")) {
2104
+ if (w.runId === parentRunId) childIds.add(w.token.slice("child:".length));
2105
+ }
2106
+ for (const cp of await this.store.listCheckpoints(parentRunId)) {
2107
+ if (cp.name.startsWith("signal:child:")) childIds.add(cp.name.slice("signal:child:".length));
2108
+ if (cp.name.startsWith("spawn:") && typeof cp.output === "string") childIds.add(cp.output);
2109
+ }
2110
+ return [
2111
+ ...childIds
2112
+ ];
2113
+ }
2114
+ /** The worker groups this engine dispatches to: every registered remote workflow's group, plus any
2115
+ * `extra` the caller declares. Local-step groups (a group consumed by in-process `@DurableStep`
2116
+ * workers, e.g. `pipeline`) aren't derivable from registrations — pass them via `extra` so a group
2117
+ * with backlog and ZERO workers is still reported (the alert case has no heartbeat to discover). */
2118
+ knownGroups(extra = []) {
2119
+ const groups = new Set(extra);
2120
+ for (const def of this.workflows.values()) {
2121
+ if (def.remote?.group) groups.add(def.remote.group);
2122
+ }
2123
+ return [
2124
+ ...groups
2125
+ ];
2126
+ }
2127
+ /** Per-group worker health (queue backlog + live worker heartbeats). Covers {@link knownGroups}
2128
+ * (so a registered group with backlog and ZERO workers still reports — the alert case) UNION the
2129
+ * groups discovered from live heartbeats (so a local-step group like `pipeline`, not derivable
2130
+ * from registrations, shows once its workers beat). Empty when no transport can introspect health
2131
+ * (only the BullMQ transport implements `groupHealth`). */
2132
+ async workerHealth(extra = []) {
2133
+ const groups = /* @__PURE__ */ new Set([
2134
+ ...this.knownGroups(extra),
2135
+ ...await this.pool.listWorkerGroups()
2136
+ ]);
2137
+ const out = [];
2138
+ for (const group of groups) {
2139
+ const health = await this.pool.groupHealth(group);
2140
+ if (health) out.push(health);
2141
+ }
2142
+ return out;
2143
+ }
2144
+ /**
2145
+ * Persist a streamed local-step lifecycle event from a remote workflow worker (see
2146
+ * {@link WorkflowStepEvent}). A Python `@workflow` runs its `ctx.step`s inline over one turn that
2147
+ * can last minutes; the worker streams each step's start/finish so the engine checkpoints it LIVE —
2148
+ * a step shows `running` the moment its body begins, then resolves to `completed`/`failed` with its
2149
+ * real wall-clock window and sub-process events — instead of every step appearing at once when the
2150
+ * turn ends. The turn's final `recordStep` command re-persists the same (runId, seq) checkpoint
2151
+ * idempotently, so this is purely additive observability and never changes the run's outcome.
2152
+ */
2153
+ async persistStepEvent(event) {
2154
+ const startedAt = new Date(event.startedAt);
2155
+ const events = event.events && event.events.length > 0 ? event.events : void 0;
2156
+ if (event.phase === "running") {
2157
+ await this.store.saveCheckpoint({
2158
+ runId: event.runId,
2159
+ seq: event.seq,
2160
+ name: event.name,
2161
+ kind: "local",
2162
+ stepId: stepId(event.runId, event.seq),
2163
+ status: "running",
2164
+ events,
2165
+ attempts: 1,
2166
+ enqueuedAt: startedAt,
2167
+ startedAt,
2168
+ finishedAt: startedAt
2169
+ });
2170
+ this.emit({
2171
+ type: "step.started",
2172
+ runId: event.runId,
2173
+ seq: event.seq,
2174
+ name: event.name,
2175
+ kind: "local"
2176
+ });
2177
+ return;
2178
+ }
2179
+ const failed = event.phase === "failed";
2180
+ await this.store.saveCheckpoint({
2181
+ runId: event.runId,
2182
+ seq: event.seq,
2183
+ name: event.name,
2184
+ kind: "local",
2185
+ stepId: stepId(event.runId, event.seq),
2186
+ status: failed ? "failed" : "completed",
2187
+ output: failed ? void 0 : event.output,
2188
+ error: failed ? event.error : void 0,
2189
+ events,
2190
+ attempts: 1,
2191
+ enqueuedAt: startedAt,
2192
+ startedAt,
2193
+ finishedAt: event.finishedAt != null ? new Date(event.finishedAt) : /* @__PURE__ */ new Date()
2194
+ });
2195
+ this.emit({
2196
+ type: failed ? "step.failed" : "step.completed",
2197
+ runId: event.runId,
2198
+ seq: event.seq,
2199
+ name: event.name,
2200
+ kind: "local",
2201
+ output: failed ? void 0 : event.output,
2202
+ error: failed ? event.error : void 0
2203
+ });
2204
+ }
2205
+ /**
2206
+ * Announce a local step's body has begun and (when `trackStepStart`) checkpoint it as `running`,
2207
+ * so it's visible in flight rather than appearing only on completion. The checkpoint is a
2208
+ * placeholder overwritten by {@link completeStep}/{@link failStep}; it never short-circuits replay
2209
+ * (only `completed` does), so a crash mid-body just re-runs the step. The `step.started` event
2210
+ * fires regardless — the live SSE view sees the start even with persistence off.
2211
+ */
2212
+ async startStep(step) {
2213
+ if (this.trackStepStart) {
2214
+ await this.store.saveCheckpoint({
2215
+ runId: step.runId,
2216
+ seq: step.seq,
2217
+ name: step.name,
2218
+ kind: step.kind,
2219
+ stepId: stepId(step.runId, step.seq),
2220
+ status: "running",
2221
+ input: step.input,
2222
+ events: step.events && step.events.length > 0 ? step.events : void 0,
2223
+ attempts: step.attempts,
2224
+ workerGroup: step.workerGroup,
2225
+ enqueuedAt: step.enqueuedAt,
2226
+ startedAt: step.startedAt,
2227
+ finishedAt: step.startedAt
2228
+ });
2229
+ }
2230
+ this.emit({
2231
+ type: "step.started",
2232
+ runId: step.runId,
2233
+ seq: step.seq,
2234
+ name: step.name,
2235
+ kind: step.kind
2236
+ });
2237
+ }
2238
+ /** Checkpoint a finished step and announce it — the two things that must always happen together. */
2239
+ async completeStep(step) {
2240
+ await this.store.saveCheckpoint({
2241
+ runId: step.runId,
2242
+ seq: step.seq,
2243
+ name: step.name,
2244
+ kind: step.kind,
2245
+ stepId: stepId(step.runId, step.seq),
2246
+ status: "completed",
2247
+ input: step.input,
2248
+ output: step.output,
2249
+ events: step.events && step.events.length > 0 ? step.events : void 0,
2250
+ attempts: step.attempts,
2251
+ workerGroup: step.workerGroup,
2252
+ enqueuedAt: step.enqueuedAt,
2253
+ startedAt: step.startedAt,
2254
+ finishedAt: /* @__PURE__ */ new Date()
2255
+ });
2256
+ this.emit({
2257
+ type: "step.completed",
2258
+ runId: step.runId,
2259
+ seq: step.seq,
2260
+ name: step.name,
2261
+ kind: step.kind,
2262
+ output: step.output,
2263
+ queueMs: step.startedAt.getTime() - step.enqueuedAt.getTime(),
2264
+ durationMs: Date.now() - step.startedAt.getTime()
2265
+ });
2266
+ }
2267
+ /** Checkpoint a step that failed terminally, so the failure point is visible (not just the run). */
2268
+ async failStep(step) {
2269
+ await this.store.saveCheckpoint({
2270
+ runId: step.runId,
2271
+ seq: step.seq,
2272
+ name: step.name,
2273
+ kind: step.kind,
2274
+ stepId: stepId(step.runId, step.seq),
2275
+ status: "failed",
2276
+ input: step.input,
2277
+ error: step.error,
2278
+ events: step.events && step.events.length > 0 ? step.events : void 0,
2279
+ attempts: step.attempts,
2280
+ workerGroup: step.workerGroup,
2281
+ enqueuedAt: step.enqueuedAt,
2282
+ startedAt: step.startedAt,
2283
+ finishedAt: /* @__PURE__ */ new Date()
2284
+ });
2285
+ this.emit({
2286
+ type: "step.failed",
2287
+ runId: step.runId,
2288
+ seq: step.seq,
2289
+ name: step.name,
2290
+ kind: step.kind,
2291
+ error: step.error,
2292
+ queueMs: step.startedAt.getTime() - step.enqueuedAt.getTime(),
2293
+ durationMs: Date.now() - step.startedAt.getTime()
2294
+ });
2295
+ }
2296
+ /**
2297
+ * Whether `run` may run now under its singleton key: it's among the `limit` oldest in-flight runs
2298
+ * (running or suspended) sharing the key, by `(createdAt, id)` order. A consistent store gives every
2299
+ * instance the same ordering, so admission is race-free + FIFO.
2300
+ */
2301
+ async admitSingleton(run, cfg) {
2302
+ const tag = singletonTag(cfg, run.input);
2303
+ const inflight = (await this.store.listRuns({
2304
+ tag,
2305
+ workflow: run.workflow,
2306
+ statuses: [
2307
+ "running",
2308
+ "suspended"
2309
+ ]
2310
+ })).sort((a, b) => a.createdAt.getTime() - b.createdAt.getTime() || a.id.localeCompare(b.id));
2311
+ const idx = inflight.findIndex((r) => r.id === run.id);
2312
+ return idx >= 0 && idx < (cfg.limit ?? 1);
2313
+ }
2314
+ /**
2315
+ * Notify-on-release: a singleton run just settled (freeing a slot), so wake the gated waiters that
2316
+ * can now be admitted — immediately, instead of letting them sleep until their ~1s retry timer.
2317
+ * Dispatches up to `limit` of the oldest gated (`suspended` + same singleton tag) waiters by
2318
+ * `(createdAt, id)`; each re-checks admission in `runExecution` and runs only if it actually wins a
2319
+ * slot, so this never breaks the FIFO + race-free guarantees (the durable timer remains the
2320
+ * cross-instance / crash fallback — this just removes the poll latency on the happy path).
2321
+ */
2322
+ async wakeNextSingletons(settled) {
2323
+ const registered = this.workflows.get(versionKey(settled.workflow, settled.workflowVersion)) ?? this.latest.get(settled.workflow);
2324
+ const cfg = registered?.singleton;
2325
+ if (!cfg) return;
2326
+ const tag = settled.tags?.find((t) => t.startsWith("singleton:"));
2327
+ if (!tag) return;
2328
+ const gated = (await this.store.listRuns({
2329
+ tag,
2330
+ workflow: settled.workflow,
2331
+ statuses: [
2332
+ "suspended"
2333
+ ]
2334
+ })).sort((a, b) => a.createdAt.getTime() - b.createdAt.getTime() || a.id.localeCompare(b.id));
2335
+ for (const next of gated.slice(0, cfg.limit ?? 1)) {
2336
+ if (next.wakeAt != null) {
2337
+ await this.store.updateRun(next.id, {
2338
+ wakeAt: void 0,
2339
+ updatedAt: /* @__PURE__ */ new Date()
2340
+ }).catch(() => void 0);
2341
+ }
2342
+ this.runDispatcher.dispatch(next.id);
2343
+ }
2344
+ }
2345
+ async execute(run, fn) {
2346
+ const registered = this.workflows.get(versionKey(run.workflow, run.workflowVersion));
2347
+ const lockNow = this.clock();
2348
+ if (run.lockedBy !== this.instanceId || (run.lockedUntil ?? 0) <= lockNow) {
2349
+ if (!await this.store.tryLockRun(run.id, this.instanceId, lockNow + this.leaseMs, lockNow)) {
2350
+ return {
2351
+ runId: run.id,
2352
+ status: run.status
2353
+ };
2354
+ }
2355
+ }
2356
+ const renew = setInterval(() => {
2357
+ void this.store.renewRunLock(run.id, this.instanceId, this.clock() + this.leaseMs).catch(() => void 0);
2358
+ }, Math.max(50, Math.floor(this.leaseMs / 2)));
2359
+ renew.unref?.();
2360
+ try {
2361
+ const result = registered?.remote ? await this.runRemoteExecution(run, registered) : await this.runExecution(run, fn, registered);
2362
+ if (registered?.singleton && (result.status === "completed" || result.status === "failed" || result.status === "cancelled" || result.status === "dead")) {
2363
+ void this.wakeNextSingletons(run).catch(() => void 0);
2364
+ }
2365
+ return result;
2366
+ } finally {
2367
+ clearInterval(renew);
2368
+ }
2369
+ }
2370
+ /**
2371
+ * Advance a remote (cross-SDK) workflow one turn: hand its history to the executor (which dispatches
2372
+ * a workflow task to the worker and awaits its replay) and apply the decision. Mirrors
2373
+ * {@link runExecution}'s settle/suspend; the lease is held by {@link execute}. The result that lands
2374
+ * us back here (a remote step finished, a timer fired) goes through `resume` like any TS workflow.
2375
+ */
2376
+ async runRemoteExecution(run, registered) {
2377
+ const remote = registered.remote;
2378
+ if (run.status === "pending") {
2379
+ await this.store.updateRun(run.id, {
2380
+ status: "running",
2381
+ updatedAt: /* @__PURE__ */ new Date()
2382
+ });
2383
+ run.status = "running";
2384
+ this.emit({
2385
+ type: "run.started",
2386
+ runId: run.id,
2387
+ workflow: run.workflow
2388
+ });
2389
+ }
2390
+ if (registered.singleton && !await this.admitSingleton(run, registered.singleton)) {
2391
+ const wakeAt2 = singletonRetryWakeAt(this.clock());
2392
+ await this.store.updateRun(run.id, {
2393
+ status: "suspended",
2394
+ wakeAt: wakeAt2,
2395
+ updatedAt: /* @__PURE__ */ new Date()
2396
+ });
2397
+ this.emit({
2398
+ type: "run.suspended",
2399
+ runId: run.id,
2400
+ workflow: run.workflow
2401
+ });
2402
+ await this.store.releaseRunLock(run.id);
2403
+ return {
2404
+ runId: run.id,
2405
+ status: "suspended"
2406
+ };
2407
+ }
2408
+ const history = await this.remoteHistory(run.id);
2409
+ let decision;
2410
+ try {
2411
+ decision = await remote.executor.advance(run, history);
2412
+ } catch (err) {
2413
+ const error = {
2414
+ message: err instanceof Error ? err.message : String(err),
2415
+ stack: err instanceof Error ? err.stack : void 0
2416
+ };
2417
+ await this.store.updateRun(run.id, {
2418
+ status: "failed",
2419
+ error,
2420
+ updatedAt: /* @__PURE__ */ new Date()
2421
+ });
2422
+ this.emit({
2423
+ type: "run.failed",
2424
+ runId: run.id,
2425
+ workflow: run.workflow,
2426
+ error
2427
+ });
2428
+ return {
2429
+ runId: run.id,
2430
+ status: "failed",
2431
+ error
2432
+ };
2433
+ }
2434
+ if (decision.status === "completed") {
2435
+ await this.applyCommands(run, decision.commands);
2436
+ await this.store.updateRun(run.id, {
2437
+ status: "completed",
2438
+ output: decision.output,
2439
+ error: void 0,
2440
+ updatedAt: /* @__PURE__ */ new Date()
2441
+ });
2442
+ this.emit({
2443
+ type: "run.completed",
2444
+ runId: run.id,
2445
+ workflow: run.workflow,
2446
+ output: decision.output
2447
+ });
2448
+ void this.notifyParent(run.id, {
2449
+ ok: true,
2450
+ value: decision.output
2451
+ });
2452
+ return {
2453
+ runId: run.id,
2454
+ status: "completed",
2455
+ output: decision.output
2456
+ };
2457
+ }
2458
+ if (decision.status === "failed") {
2459
+ await this.applyCommands(run, decision.commands);
2460
+ const error = decision.error ?? {
2461
+ message: "workflow failed"
2462
+ };
2463
+ await this.store.updateRun(run.id, {
2464
+ status: "failed",
2465
+ error,
2466
+ updatedAt: /* @__PURE__ */ new Date()
2467
+ });
2468
+ this.emit({
2469
+ type: "run.failed",
2470
+ runId: run.id,
2471
+ workflow: run.workflow,
2472
+ error
2473
+ });
2474
+ void this.notifyParent(run.id, {
2475
+ ok: false,
2476
+ error: error.message
2477
+ });
2478
+ return {
2479
+ runId: run.id,
2480
+ status: "failed",
2481
+ error
2482
+ };
2483
+ }
2484
+ const wakeAt = await this.applyCommands(run, decision.commands);
2485
+ await this.store.updateRun(run.id, {
2486
+ status: "suspended",
2487
+ wakeAt,
2488
+ updatedAt: /* @__PURE__ */ new Date()
2489
+ });
2490
+ this.emit({
2491
+ type: "run.suspended",
2492
+ runId: run.id,
2493
+ workflow: run.workflow
2494
+ });
2495
+ return {
2496
+ runId: run.id,
2497
+ status: "suspended"
2498
+ };
2499
+ }
2500
+ /** The run's resolved durable ops as replay inputs: completed/failed steps + elapsed timers. */
2501
+ async remoteHistory(runId) {
2502
+ const checkpoints = await this.store.listCheckpoints(runId);
2503
+ const kindOf = {
2504
+ remote: "call",
2505
+ local: "step",
2506
+ sleep: "timer",
2507
+ signal: "signal"
2508
+ };
2509
+ const events = [];
2510
+ for (const cp of checkpoints) {
2511
+ if (cp.status === "completed" || cp.status === "failed") {
2512
+ if (cp.kind === "signal" && cp.name.startsWith("signal:child:")) {
2513
+ const completion = cp.output;
2514
+ events.push({
2515
+ seq: cp.seq,
2516
+ kind: "child",
2517
+ output: completion?.ok ? completion.value : void 0,
2518
+ error: completion && completion.ok === false ? {
2519
+ message: completion.error
2520
+ } : void 0
2521
+ });
2522
+ continue;
2523
+ }
2524
+ events.push({
2525
+ seq: cp.seq,
2526
+ kind: kindOf[cp.kind] ?? "step",
2527
+ // A signal checkpoint's name is the internal `signal:<token>`, not the workflow-level signal
2528
+ // name the replay used — omit it so the replay matches on seq + kind (its determinism anchor).
2529
+ name: cp.kind === "signal" ? void 0 : cp.name,
2530
+ output: cp.status === "completed" ? cp.output : void 0,
2531
+ error: cp.status === "failed" ? cp.error : void 0
2532
+ });
2533
+ } else if (cp.kind === "sleep" && cp.wakeAt != null && cp.wakeAt <= this.clock()) {
2534
+ events.push({
2535
+ seq: cp.seq,
2536
+ kind: "timer",
2537
+ name: cp.name
2538
+ });
2539
+ }
2540
+ }
2541
+ return events.sort((a, b) => a.seq - b.seq);
2542
+ }
2543
+ /** Apply a turn's commands: persist recorded local steps, dispatch remote calls, schedule timers.
2544
+ * Returns the earliest timer deadline to suspend on (or undefined — suspended on a result). */
2545
+ async applyCommands(run, commands) {
2546
+ let wakeAt;
2547
+ for (const cmd of commands) {
2548
+ const at = /* @__PURE__ */ new Date();
2549
+ const id = stepId(run.id, cmd.seq);
2550
+ if (cmd.kind === "recordStep") {
2551
+ const startedAt = cmd.startedAt != null ? new Date(cmd.startedAt) : at;
2552
+ const finishedAt = cmd.finishedAt != null ? new Date(cmd.finishedAt) : at;
2553
+ await this.store.saveCheckpoint({
2554
+ runId: run.id,
2555
+ seq: cmd.seq,
2556
+ name: cmd.name,
2557
+ kind: "local",
2558
+ stepId: id,
2559
+ status: cmd.error ? "failed" : "completed",
2560
+ output: cmd.output,
2561
+ error: cmd.error,
2562
+ events: cmd.events && cmd.events.length > 0 ? cmd.events : void 0,
2563
+ attempts: 1,
2564
+ enqueuedAt: startedAt,
2565
+ startedAt,
2566
+ finishedAt
2567
+ });
2568
+ this.emit({
2569
+ type: cmd.error ? "step.failed" : "step.completed",
2570
+ runId: run.id,
2571
+ seq: cmd.seq,
2572
+ name: cmd.name,
2573
+ kind: "local",
2574
+ output: cmd.output,
2575
+ error: cmd.error
2576
+ });
2577
+ } else if (cmd.kind === "call") {
2578
+ await this.store.saveCheckpoint({
2579
+ runId: run.id,
2580
+ seq: cmd.seq,
2581
+ name: cmd.name,
2582
+ kind: "remote",
2583
+ stepId: id,
2584
+ status: "pending",
2585
+ input: cmd.input,
2586
+ attempts: 1,
2587
+ workerGroup: cmd.group,
2588
+ enqueuedAt: at,
2589
+ startedAt: at,
2590
+ finishedAt: at
2591
+ });
2592
+ await this.pool.dispatch({
2593
+ runId: run.id,
2594
+ seq: cmd.seq,
2595
+ name: cmd.name,
2596
+ stepId: id,
2597
+ group: cmd.group,
2598
+ input: cmd.input,
2599
+ traceparent: this.traceparent?.(),
2600
+ context: this.context?.(),
2601
+ attempt: 1
2602
+ }, void 0);
2603
+ this.emit({
2604
+ type: "step.started",
2605
+ runId: run.id,
2606
+ seq: cmd.seq,
2607
+ name: cmd.name,
2608
+ kind: "remote"
2609
+ });
2610
+ } else if (cmd.kind === "sleep") {
2611
+ const deadline = this.clock() + cmd.ms;
2612
+ await this.store.saveCheckpoint({
2613
+ runId: run.id,
2614
+ seq: cmd.seq,
2615
+ name: `sleep:${cmd.seq}`,
2616
+ kind: "sleep",
2617
+ stepId: id,
2618
+ status: "pending",
2619
+ attempts: 1,
2620
+ wakeAt: deadline,
2621
+ enqueuedAt: at,
2622
+ startedAt: at,
2623
+ finishedAt: at
2624
+ });
2625
+ wakeAt = wakeAt == null ? deadline : Math.min(wakeAt, deadline);
2626
+ } else if (cmd.kind === "waitSignal") {
2627
+ const buffered = await this.store.takeBufferedSignal(cmd.signal);
2628
+ if (buffered) {
2629
+ await this.store.saveCheckpoint(instantCheckpoint({
2630
+ runId: run.id,
2631
+ seq: cmd.seq,
2632
+ name: `signal:${cmd.signal}`,
2633
+ kind: "signal",
2634
+ output: buffered.payload
2635
+ }));
2636
+ setTimeout(() => void this.resume(run.id).catch(() => void 0), 0);
2637
+ } else {
2638
+ await this.store.putSignalWaiter({
2639
+ token: cmd.signal,
2640
+ runId: run.id,
2641
+ seq: cmd.seq
2642
+ });
2643
+ }
2644
+ } else if (cmd.kind === "startChild") {
2645
+ const childId = `${run.id}.child.${cmd.seq}`;
2646
+ await this.store.putSignalWaiter({
2647
+ token: `child:${childId}`,
2648
+ runId: run.id,
2649
+ seq: cmd.seq
2650
+ });
2651
+ if (!await this.store.getRun(childId)) {
2652
+ queueMicrotask(() => void this.start(cmd.workflow, cmd.input, childId).catch(() => void 0));
2653
+ }
2654
+ } else {
2655
+ throw new Error(`remote workflow command '${cmd.kind}' is not supported yet`);
2656
+ }
2657
+ }
2658
+ return wakeAt;
2659
+ }
2660
+ /** The run body, lease held + renewed by {@link execute}. */
2661
+ async runExecution(run, fn, registered) {
2662
+ if (run.status === "pending") {
2663
+ await this.store.updateRun(run.id, {
2664
+ status: "running",
2665
+ updatedAt: /* @__PURE__ */ new Date()
2666
+ });
2667
+ run.status = "running";
2668
+ this.emit({
2669
+ type: "run.started",
2670
+ runId: run.id,
2671
+ workflow: run.workflow
2672
+ });
2673
+ }
2674
+ if (registered?.singleton && !await this.admitSingleton(run, registered.singleton)) {
2675
+ const wakeAt = singletonRetryWakeAt(this.clock());
2676
+ await this.store.updateRun(run.id, {
2677
+ status: "suspended",
2678
+ wakeAt,
2679
+ updatedAt: /* @__PURE__ */ new Date()
2680
+ });
2681
+ this.emit({
2682
+ type: "run.suspended",
2683
+ runId: run.id,
2684
+ workflow: run.workflow
2685
+ });
2686
+ await this.store.releaseRunLock(run.id);
2687
+ return {
2688
+ runId: run.id,
2689
+ status: "suspended"
2690
+ };
2691
+ }
2692
+ const compensations = [];
2693
+ const snapshot = await this.store.listCheckpoints(run.id);
2694
+ const replay = /* @__PURE__ */ new Map();
2695
+ for (const cp of snapshot) replay.set(cp.seq, cp);
2696
+ const ctx = createWorkflowCtx(this.ctxHostFor(replay), run.id, compensations, run.workflow);
2697
+ try {
2698
+ const output = await fn(ctx, run.input);
2699
+ await this.store.updateRun(run.id, {
2700
+ status: "completed",
2701
+ output,
2702
+ error: void 0,
2703
+ updatedAt: /* @__PURE__ */ new Date()
2704
+ });
2705
+ this.emit({
2706
+ type: "run.completed",
2707
+ runId: run.id,
2708
+ workflow: run.workflow,
2709
+ output
2710
+ });
2711
+ void this.notifyParent(run.id, {
2712
+ ok: true,
2713
+ value: output
2714
+ });
2715
+ return {
2716
+ runId: run.id,
2717
+ status: "completed",
2718
+ output
2719
+ };
2720
+ } catch (err) {
2721
+ if (err instanceof ContinueAsNew) {
2722
+ await this.store.updateRun(run.id, {
2723
+ status: "completed",
2724
+ output: void 0,
2725
+ error: void 0,
2726
+ updatedAt: /* @__PURE__ */ new Date()
2727
+ });
2728
+ this.emit({
2729
+ type: "run.completed",
2730
+ runId: run.id,
2731
+ workflow: run.workflow
2732
+ });
2733
+ void this.notifyParent(run.id, {
2734
+ ok: true,
2735
+ value: void 0
2736
+ });
2737
+ const nextId = nextContinuationId(run.id);
2738
+ queueMicrotask(() => void this.start(run.workflow, err.input, nextId).catch(() => void 0));
2739
+ return {
2740
+ runId: run.id,
2741
+ status: "completed"
2742
+ };
2743
+ }
2744
+ if (err instanceof WorkflowSuspended) {
2745
+ if (this.cancelRequested.has(run.id)) {
2746
+ this.cancelRequested.delete(run.id);
2747
+ for (let i = compensations.length - 1; i >= 0; i -= 1) {
2748
+ const comp = compensations[i];
2749
+ if (comp) await this.runCompensation(run, comp);
2750
+ }
2751
+ const error2 = {
2752
+ message: "cancelled"
2753
+ };
2754
+ await this.store.updateRun(run.id, {
2755
+ status: "cancelled",
2756
+ error: error2,
2757
+ updatedAt: /* @__PURE__ */ new Date()
2758
+ });
2759
+ this.emit({
2760
+ type: "run.failed",
2761
+ runId: run.id,
2762
+ workflow: run.workflow,
2763
+ error: error2
2764
+ });
2765
+ return {
2766
+ runId: run.id,
2767
+ status: "cancelled",
2768
+ error: error2
2769
+ };
2770
+ }
2771
+ await this.store.updateRun(run.id, {
2772
+ status: "suspended",
2773
+ wakeAt: err.wakeAt,
2774
+ updatedAt: /* @__PURE__ */ new Date()
2775
+ });
2776
+ this.emit({
2777
+ type: "run.suspended",
2778
+ runId: run.id,
2779
+ workflow: run.workflow
2780
+ });
2781
+ return {
2782
+ runId: run.id,
2783
+ status: "suspended"
2784
+ };
2785
+ }
2786
+ const error = {
2787
+ message: err instanceof Error ? err.message : String(err),
2788
+ stack: err instanceof Error ? err.stack : void 0
2789
+ };
2790
+ for (let i = compensations.length - 1; i >= 0; i -= 1) {
2791
+ const comp = compensations[i];
2792
+ if (!comp) continue;
2793
+ await this.runCompensation(run, comp);
2794
+ }
2795
+ await this.store.updateRun(run.id, {
2796
+ status: "failed",
2797
+ error,
2798
+ updatedAt: /* @__PURE__ */ new Date()
2799
+ });
2800
+ this.emit({
2801
+ type: "run.failed",
2802
+ runId: run.id,
2803
+ workflow: run.workflow,
2804
+ error
2805
+ });
2806
+ void this.notifyParent(run.id, {
2807
+ ok: false,
2808
+ error: error.message
2809
+ });
2810
+ return {
2811
+ runId: run.id,
2812
+ status: "failed",
2813
+ error
2814
+ };
2815
+ } finally {
2816
+ await this.store.releaseRunLock(run.id);
2817
+ }
2818
+ }
2819
+ /**
2820
+ * Run one saga compensation, retried up to `compensationRetries`, emitting a `compensate:<step>`
2821
+ * step event for its outcome so a stranded undo is visible. Never throws — a permanently-failing
2822
+ * compensation is skipped so it can't mask the original failure.
2823
+ */
2824
+ async runCompensation(run, comp) {
2825
+ const name = `compensate:${comp.name}`;
2826
+ for (let attempt = 1; attempt <= this.compensationRetries; attempt += 1) {
2827
+ const startedAt = Date.now();
2828
+ try {
2829
+ await comp.fn();
2830
+ this.emit({
2831
+ type: "step.completed",
2832
+ runId: run.id,
2833
+ workflow: run.workflow,
2834
+ name,
2835
+ kind: "local",
2836
+ durationMs: Date.now() - startedAt
2837
+ });
2838
+ return;
2839
+ } catch (err) {
2840
+ if (attempt >= this.compensationRetries) {
2841
+ this.emit({
2842
+ type: "step.failed",
2843
+ runId: run.id,
2844
+ workflow: run.workflow,
2845
+ name,
2846
+ kind: "local",
2847
+ error: {
2848
+ message: err instanceof Error ? err.message : String(err)
2849
+ },
2850
+ durationMs: Date.now() - startedAt
2851
+ });
2852
+ }
2853
+ }
2854
+ }
2855
+ }
2856
+ /** The seam handed to {@link createWorkflowCtx}: the authoring API reaches durability + lifecycle
2857
+ * (checkpointing, dispatch, child start) through this, so the ctx primitives live in their own
2858
+ * module and the engine stays the orchestrator. */
2859
+ ctxHostFor(replay) {
2860
+ return {
2861
+ store: this.store,
2862
+ replay,
2863
+ clock: this.clock,
2864
+ webhookUrl: this.webhookUrl,
2865
+ startStep: /* @__PURE__ */ __name((s) => this.startStep(s), "startStep"),
2866
+ completeStep: /* @__PURE__ */ __name((s) => this.completeStep(s), "completeStep"),
2867
+ failStep: /* @__PURE__ */ __name((s) => this.failStep(s), "failStep"),
2868
+ callRemote: /* @__PURE__ */ __name((runId, seq, step, input, queue, transport, admission) => this.callRemote(runId, seq, step, input, queue, transport, replay, admission), "callRemote"),
2869
+ // Defer so a fast child can't reentrantly resume a still-running parent.
2870
+ startChild: /* @__PURE__ */ __name((workflow, input, id) => {
2871
+ queueMicrotask(() => void this.start(workflow, input, id).catch(() => void 0));
2872
+ }, "startChild"),
2873
+ signalEntity: /* @__PURE__ */ __name((name, key, op, arg, reply) => {
2874
+ queueMicrotask(() => void this.entities.dispatch(name, key, op, arg, reply).catch(() => void 0));
2875
+ }, "signalEntity"),
2876
+ interceptStep: /* @__PURE__ */ __name((invocation, body) => this.interceptStep(invocation, body), "interceptStep")
2877
+ };
2878
+ }
2879
+ /**
2880
+ * Resume a run paused at a {@link WorkflowCtx.breakpoint} (e.g. the dashboard "continue" button).
2881
+ * Finds the run's pending breakpoint checkpoint and signals it. Returns null if the run isn't
2882
+ * paused at a breakpoint.
2883
+ */
2884
+ async continue(runId) {
2885
+ const checkpoints = await this.store.listCheckpoints(runId);
2886
+ const bp = checkpoints.find(isBreakpoint);
2887
+ if (!bp) return null;
2888
+ return this.signal(breakpointToken(runId, bp.seq), void 0);
2889
+ }
2890
+ /**
2891
+ * Read the latest value a run published for `key` via {@link WorkflowCtx.setEvent} — a
2892
+ * side-effect-free query of a live (or finished) run's state. Returns `undefined` if the run
2893
+ * never published that key. The suspend-model counterpart of a Temporal query.
2894
+ */
2895
+ async getEvent(runId, key) {
2896
+ const name = `event:${key}`;
2897
+ const checkpoints = await this.store.listCheckpoints(runId);
2898
+ let latest;
2899
+ for (const cp of checkpoints) if (cp.name === name) latest = cp.output;
2900
+ return latest;
2901
+ }
2902
+ /**
2903
+ * Register a validator gating `engine.update(runId, name, …)` for runs of `workflow`. The
2904
+ * validator runs BEFORE the update is delivered, so a rejection leaves the run untouched. One
2905
+ * validator per (workflow, update name); registering again replaces it.
2906
+ */
2907
+ registerUpdateValidator(workflow, name, validate) {
2908
+ this.updateValidators.set(`${workflow}:${name}`, validate);
2909
+ }
2910
+ /**
2911
+ * Deliver a validated update to the run waiting at `ctx.onUpdate(name)`. Runs the registered
2912
+ * validator (if any) first: on rejection returns `{ accepted: false, reason }` without disturbing
2913
+ * the run; otherwise delivers `arg` and resumes, returning `{ accepted: true, run }` (`run` is null
2914
+ * if nothing was waiting — a too-early or duplicate update).
2915
+ */
2916
+ async update(runId, name, arg) {
2917
+ const run = await this.store.getRun(runId);
2918
+ if (!run) return {
2919
+ accepted: false,
2920
+ reason: `run ${runId} not found`
2921
+ };
2922
+ const validate = this.updateValidators.get(`${run.workflow}:${name}`);
2923
+ if (validate) {
2924
+ try {
2925
+ const reason = await validate(arg);
2926
+ if (typeof reason === "string" && reason.length > 0) return {
2927
+ accepted: false,
2928
+ reason
2929
+ };
2930
+ } catch (err) {
2931
+ return {
2932
+ accepted: false,
2933
+ reason: err instanceof Error ? err.message : String(err)
2934
+ };
2935
+ }
2936
+ }
2937
+ const result = await this.signal(`update:${runId}:${name}`, arg);
2938
+ return {
2939
+ accepted: true,
2940
+ run: result
2941
+ };
2942
+ }
2943
+ async callRemote(runId, seq, step, input, queue, transport, replay, admission) {
2944
+ const existing = replay?.get(seq) ?? await this.store.getCheckpoint(runId, seq);
2945
+ if (existing && existing.name !== step.name) {
2946
+ throw new NonDeterminismError(runId, seq, step.name, existing.name);
2947
+ }
2948
+ if (existing?.status === "completed") return existing.output;
2949
+ if (this.pool.size === 0) throw new Error("remote steps require a Transport");
2950
+ if (step.timeoutMs) return this.callRemoteInMemory(runId, seq, step, input, transport);
2951
+ if (existing?.status === "pending") throw new WorkflowSuspended();
2952
+ let attempt = 1;
2953
+ if (existing?.status === "failed") {
2954
+ const maxAttempts = Math.max(1, step.retries ?? 1);
2955
+ const retryable = existing.error?.retryable !== false;
2956
+ if (!retryable || existing.attempts >= maxAttempts) throw new RemoteStepError(existing.error);
2957
+ if (existing.wakeAt == null) {
2958
+ const wakeAt = this.clock() + backoffDelay(existing.attempts, step);
2959
+ await this.store.saveCheckpoint({
2960
+ ...existing,
2961
+ wakeAt
2962
+ });
2963
+ throw new WorkflowSuspended(wakeAt);
2964
+ }
2965
+ if (this.clock() < existing.wakeAt) throw new WorkflowSuspended(existing.wakeAt);
2966
+ attempt = existing.attempts + 1;
2967
+ }
2968
+ const id = stepId(runId, seq);
2969
+ const controller = queue ? this.queues.get(queue) : void 0;
2970
+ if (controller) {
2971
+ const decision = controller.tryAdmit({
2972
+ priority: admission?.priority,
2973
+ key: admission?.fairnessKey ?? runId,
2974
+ waiterId: id
2975
+ });
2976
+ if (!decision.ok) throw new WorkflowSuspended(decision.retryAt);
2977
+ this.stepQueue.set(id, queue);
2978
+ }
2979
+ const validInput = step.input.parse(input);
2980
+ const enqueuedAt = /* @__PURE__ */ new Date();
2981
+ await this.store.saveCheckpoint({
2982
+ runId,
2983
+ seq,
2984
+ name: step.name,
2985
+ kind: "remote",
2986
+ stepId: id,
2987
+ status: "pending",
2988
+ input: validInput,
2989
+ attempts: attempt,
2990
+ workerGroup: step.group,
2991
+ enqueuedAt,
2992
+ startedAt: enqueuedAt,
2993
+ finishedAt: enqueuedAt
2994
+ });
2995
+ await this.pool.dispatch({
2996
+ runId,
2997
+ seq,
2998
+ name: step.name,
2999
+ stepId: id,
3000
+ group: step.group,
3001
+ input: validInput,
3002
+ traceparent: this.traceparent?.(),
3003
+ context: this.context?.(),
3004
+ attempt
3005
+ }, transport);
3006
+ this.emit({
3007
+ type: "step.started",
3008
+ runId,
3009
+ seq,
3010
+ name: step.name,
3011
+ kind: "remote"
3012
+ });
3013
+ throw new WorkflowSuspended();
3014
+ }
3015
+ /**
3016
+ * Complete a durable remote step from its worker result and resume the run — runs on whichever
3017
+ * instance receives the result (the dispatching one may be gone), so the run is crash/scale-safe.
3018
+ * A no-op if the checkpoint isn't `pending` (a duplicate or late delivery).
3019
+ */
3020
+ async completeRemoteResult(result) {
3021
+ const cp = await this.store.getCheckpoint(result.runId, result.seq);
3022
+ if (!cp || cp.status !== "pending") return;
3023
+ this.releaseQueueSlot(cp.stepId);
3024
+ const run = await this.store.getRun(result.runId);
3025
+ if (run && (run.status === "cancelled" || run.status === "completed")) return;
3026
+ const finishedAt = /* @__PURE__ */ new Date();
3027
+ const startedAt = result.startedAt ? new Date(result.startedAt) : cp.startedAt;
3028
+ await this.store.saveCheckpoint({
3029
+ ...cp,
3030
+ status: result.status,
3031
+ output: result.status === "completed" ? result.output : cp.output,
3032
+ error: result.error,
3033
+ events: result.events ?? cp.events,
3034
+ startedAt,
3035
+ finishedAt
3036
+ });
3037
+ this.emit({
3038
+ type: result.status === "completed" ? "step.completed" : "step.failed",
3039
+ runId: result.runId,
3040
+ seq: result.seq,
3041
+ name: cp.name,
3042
+ kind: cp.kind,
3043
+ output: result.output,
3044
+ error: result.error,
3045
+ queueMs: startedAt.getTime() - cp.enqueuedAt.getTime(),
3046
+ durationMs: finishedAt.getTime() - startedAt.getTime()
3047
+ });
3048
+ await this.resume(result.runId);
3049
+ }
3050
+ /** Release the flow-control slot a dispatched step held (if any), by its stepId. */
3051
+ releaseQueueSlot(id) {
3052
+ const queue = this.stepQueue.get(id);
3053
+ if (queue === void 0) return;
3054
+ this.stepQueue.delete(id);
3055
+ this.queues.get(queue)?.release();
3056
+ }
3057
+ /** In-memory await path for a remote step with a liveness `timeoutMs` (re-dispatch on timeout). */
3058
+ async callRemoteInMemory(runId, seq, step, input, transport) {
3059
+ if (this.pool.size === 0) throw new Error("remote steps require a Transport");
3060
+ const validInput = step.input.parse(input);
3061
+ const id = stepId(runId, seq);
3062
+ const enqueuedAt = /* @__PURE__ */ new Date();
3063
+ this.emit({
3064
+ type: "step.started",
3065
+ runId,
3066
+ seq,
3067
+ name: step.name,
3068
+ kind: "remote"
3069
+ });
3070
+ const maxAttempts = step.timeoutMs ? Math.max(1, step.retries ?? 1) : 1;
3071
+ for (let attempt = 1; ; attempt += 1) {
3072
+ const resultPromise = new Promise((resolve, reject) => {
3073
+ this.pending.set(id, {
3074
+ resolve,
3075
+ reject
3076
+ });
3077
+ });
3078
+ await this.pool.dispatch({
3079
+ runId,
3080
+ seq,
3081
+ name: step.name,
3082
+ stepId: id,
3083
+ group: step.group,
3084
+ input: validInput,
3085
+ traceparent: this.traceparent?.(),
3086
+ context: this.context?.(),
3087
+ attempt
3088
+ }, transport);
3089
+ try {
3090
+ const resolution = step.timeoutMs ? await this.awaitWithHeartbeat(id, resultPromise, step.timeoutMs) : await resultPromise;
3091
+ const output = step.output.parse(resolution.output);
3092
+ const startedAt = resolution.startedAt ? new Date(resolution.startedAt) : enqueuedAt;
3093
+ await this.completeStep({
3094
+ runId,
3095
+ seq,
3096
+ name: step.name,
3097
+ kind: "remote",
3098
+ input: validInput,
3099
+ output,
3100
+ events: resolution.events,
3101
+ attempts: attempt,
3102
+ workerGroup: step.group,
3103
+ enqueuedAt,
3104
+ startedAt
3105
+ });
3106
+ return output;
3107
+ } catch (err) {
3108
+ this.pending.delete(id);
3109
+ if (err instanceof RemoteStepTimeout && attempt < maxAttempts) continue;
3110
+ throw err;
3111
+ }
3112
+ }
3113
+ }
3114
+ /**
3115
+ * Await a remote result, but reject with `RemoteStepTimeout` if neither the result nor a heartbeat
3116
+ * arrives within `timeoutMs`. Each heartbeat (delivered via `transport.onHeartbeat`) rearms the
3117
+ * window, so a worker that keeps beating stays alive past `timeoutMs`.
3118
+ */
3119
+ awaitWithHeartbeat(id, resultPromise, timeoutMs) {
3120
+ return new Promise((resolve, reject) => {
3121
+ let timer;
3122
+ const cleanup = /* @__PURE__ */ __name(() => {
3123
+ clearTimeout(timer);
3124
+ this.heartbeatResets.delete(id);
3125
+ }, "cleanup");
3126
+ const arm = /* @__PURE__ */ __name(() => {
3127
+ timer = setTimeout(() => {
3128
+ cleanup();
3129
+ this.pending.delete(id);
3130
+ reject(new RemoteStepTimeout(id, timeoutMs));
3131
+ }, timeoutMs);
3132
+ timer.unref?.();
3133
+ }, "arm");
3134
+ this.heartbeatResets.set(id, () => {
3135
+ clearTimeout(timer);
3136
+ arm();
3137
+ });
3138
+ arm();
3139
+ resultPromise.then((value) => {
3140
+ cleanup();
3141
+ resolve(value);
3142
+ }, (err) => {
3143
+ cleanup();
3144
+ reject(err);
3145
+ });
3146
+ });
3147
+ }
3148
+ };
3149
+ var RemoteStepError = class extends Error {
3150
+ static {
3151
+ __name(this, "RemoteStepError");
3152
+ }
3153
+ stepError;
3154
+ constructor(stepError) {
3155
+ super(stepError?.message ?? "remote step failed");
3156
+ this.name = "RemoteStepError";
3157
+ this.stepError = stepError;
3158
+ }
3159
+ };
3160
+
3161
+ // src/remote-step-factory.ts
3162
+ function remoteStep(config) {
3163
+ return {
3164
+ ...config,
3165
+ group: config.group ?? config.name.split(".")[0] ?? config.name,
3166
+ __remote: true
3167
+ };
3168
+ }
3169
+ __name(remoteStep, "remoteStep");
3170
+
3171
+ // src/remote-workflow-executor.ts
3172
+ var taskCounter = 0;
3173
+ var RemoteWorkflowExecutor = class {
3174
+ static {
3175
+ __name(this, "RemoteWorkflowExecutor");
3176
+ }
3177
+ transport;
3178
+ group;
3179
+ opts;
3180
+ pending = /* @__PURE__ */ new Map();
3181
+ subscribed = false;
3182
+ constructor(transport, group, opts = {}) {
3183
+ this.transport = transport;
3184
+ this.group = group;
3185
+ this.opts = opts;
3186
+ }
3187
+ ensureSubscribed() {
3188
+ if (this.subscribed) return;
3189
+ if (!this.transport.onDecision) {
3190
+ throw new Error("transport does not support workflow decisions (onDecision)");
3191
+ }
3192
+ this.subscribed = true;
3193
+ this.transport.onDecision(async (decision) => {
3194
+ const resolve = this.pending.get(decision.taskId);
3195
+ if (resolve) {
3196
+ this.pending.delete(decision.taskId);
3197
+ resolve(decision);
3198
+ }
3199
+ });
3200
+ }
3201
+ async advance(run, history) {
3202
+ if (!this.transport.dispatchWorkflowTask) {
3203
+ throw new Error("transport does not support workflow tasks (dispatchWorkflowTask)");
3204
+ }
3205
+ this.ensureSubscribed();
3206
+ taskCounter += 1;
3207
+ const taskId = `${run.id}:wf:${taskCounter}`;
3208
+ const task = {
3209
+ taskId,
3210
+ runId: run.id,
3211
+ workflow: run.workflow,
3212
+ workflowVersion: run.workflowVersion,
3213
+ input: run.input,
3214
+ history,
3215
+ group: this.group,
3216
+ attempt: 1
3217
+ };
3218
+ const decision = new Promise((resolve, reject) => {
3219
+ this.pending.set(taskId, resolve);
3220
+ if (this.opts.timeoutMs) {
3221
+ const timer = setTimeout(() => {
3222
+ this.pending.delete(taskId);
3223
+ reject(new Error(`workflow task ${taskId} timed out after ${this.opts.timeoutMs}ms`));
3224
+ }, this.opts.timeoutMs);
3225
+ timer.unref?.();
3226
+ }
3227
+ });
3228
+ await this.transport.dispatchWorkflowTask(task);
3229
+ return decision;
3230
+ }
3231
+ };
3232
+
3233
+ // src/codec-state-store.ts
3234
+ var CodecStateStore = class {
3235
+ static {
3236
+ __name(this, "CodecStateStore");
3237
+ }
3238
+ inner;
3239
+ codec;
3240
+ constructor(inner, codec) {
3241
+ this.inner = inner;
3242
+ this.codec = codec;
3243
+ }
3244
+ enc(v) {
3245
+ return v === void 0 ? void 0 : this.codec.encode(v);
3246
+ }
3247
+ dec(v) {
3248
+ return v === void 0 ? void 0 : this.codec.decode(v);
3249
+ }
3250
+ encRun(run) {
3251
+ return {
3252
+ ...run,
3253
+ input: this.enc(run.input),
3254
+ output: this.enc(run.output)
3255
+ };
3256
+ }
3257
+ decRun(run) {
3258
+ return run && {
3259
+ ...run,
3260
+ input: this.dec(run.input),
3261
+ output: this.dec(run.output)
3262
+ };
3263
+ }
3264
+ encCp(cp) {
3265
+ return {
3266
+ ...cp,
3267
+ input: this.enc(cp.input),
3268
+ output: this.enc(cp.output)
3269
+ };
3270
+ }
3271
+ decCp(cp) {
3272
+ return cp && {
3273
+ ...cp,
3274
+ input: this.dec(cp.input),
3275
+ output: this.dec(cp.output)
3276
+ };
3277
+ }
3278
+ ensureSchema() {
3279
+ return this.inner.ensureSchema?.() ?? Promise.resolve();
3280
+ }
3281
+ createRun(run) {
3282
+ return this.inner.createRun(this.encRun(run));
3283
+ }
3284
+ updateRun(runId, patch) {
3285
+ const next = {
3286
+ ...patch
3287
+ };
3288
+ if ("input" in patch) next.input = this.enc(patch.input);
3289
+ if ("output" in patch) next.output = this.enc(patch.output);
3290
+ return this.inner.updateRun(runId, next);
3291
+ }
3292
+ async getRun(runId) {
3293
+ return this.decRun(await this.inner.getRun(runId));
3294
+ }
3295
+ async getCheckpoint(runId, seq) {
3296
+ return this.decCp(await this.inner.getCheckpoint(runId, seq));
3297
+ }
3298
+ saveCheckpoint(checkpoint) {
3299
+ return this.inner.saveCheckpoint(this.encCp(checkpoint));
3300
+ }
3301
+ transaction(work) {
3302
+ if (!this.inner.transaction) {
3303
+ throw new Error("the wrapped store does not support transactions");
3304
+ }
3305
+ return this.inner.transaction((tx) => work({
3306
+ raw: tx.raw,
3307
+ saveCheckpoint: /* @__PURE__ */ __name((cp) => tx.saveCheckpoint(this.encCp(cp)), "saveCheckpoint")
3308
+ }));
3309
+ }
3310
+ async listIncompleteRuns() {
3311
+ return (await this.inner.listIncompleteRuns()).map((r) => this.decRun(r));
3312
+ }
3313
+ async listPendingRuns(limit) {
3314
+ return (await this.inner.listPendingRuns(limit)).map((r) => this.decRun(r));
3315
+ }
3316
+ async listDueTimers(nowMs) {
3317
+ return (await this.inner.listDueTimers(nowMs)).map((r) => this.decRun(r));
3318
+ }
3319
+ tryLockRun(runId, owner, leaseUntilMs, nowMs) {
3320
+ return this.inner.tryLockRun(runId, owner, leaseUntilMs, nowMs);
3321
+ }
3322
+ releaseRunLock(runId) {
3323
+ return this.inner.releaseRunLock(runId);
3324
+ }
3325
+ renewRunLock(runId, owner, leaseUntilMs) {
3326
+ return this.inner.renewRunLock(runId, owner, leaseUntilMs);
3327
+ }
3328
+ putSignalWaiter(waiter) {
3329
+ return this.inner.putSignalWaiter(waiter);
3330
+ }
3331
+ takeSignalWaiter(token) {
3332
+ return this.inner.takeSignalWaiter(token);
3333
+ }
3334
+ listSignalWaiters(prefix) {
3335
+ return this.inner.listSignalWaiters(prefix);
3336
+ }
3337
+ bufferSignal(token, payload) {
3338
+ return this.inner.bufferSignal(token, this.enc(payload));
3339
+ }
3340
+ async takeBufferedSignal(token) {
3341
+ const buffered = await this.inner.takeBufferedSignal(token);
3342
+ return buffered && {
3343
+ payload: this.dec(buffered.payload)
3344
+ };
3345
+ }
3346
+ async listRuns(query) {
3347
+ return (await this.inner.listRuns(query)).map((r) => this.decRun(r));
3348
+ }
3349
+ async listCheckpoints(runId) {
3350
+ return (await this.inner.listCheckpoints(runId)).map((c) => this.decCp(c));
3351
+ }
3352
+ };
3353
+
3354
+ // src/metrics.ts
3355
+ function collectMetrics(engine) {
3356
+ const m = {
3357
+ runs: {
3358
+ started: 0,
3359
+ completed: 0,
3360
+ failed: 0,
3361
+ suspended: 0
3362
+ },
3363
+ steps: {
3364
+ started: 0,
3365
+ completed: 0,
3366
+ failed: 0
3367
+ },
3368
+ byWorkflow: {},
3369
+ stepDuration: {
3370
+ sumMs: 0,
3371
+ count: 0
3372
+ }
3373
+ };
3374
+ const wf = /* @__PURE__ */ __name((name) => {
3375
+ const k = name ?? "unknown";
3376
+ return m.byWorkflow[k] ??= {
3377
+ started: 0,
3378
+ completed: 0,
3379
+ failed: 0
3380
+ };
3381
+ }, "wf");
3382
+ const stop = engine.subscribe((e) => {
3383
+ switch (e.type) {
3384
+ case "run.started":
3385
+ m.runs.started += 1;
3386
+ wf(e.workflow).started += 1;
3387
+ break;
3388
+ case "run.completed":
3389
+ m.runs.completed += 1;
3390
+ wf(e.workflow).completed += 1;
3391
+ break;
3392
+ case "run.failed":
3393
+ m.runs.failed += 1;
3394
+ wf(e.workflow).failed += 1;
3395
+ break;
3396
+ case "run.suspended":
3397
+ m.runs.suspended += 1;
3398
+ break;
3399
+ case "step.started":
3400
+ m.steps.started += 1;
3401
+ break;
3402
+ case "step.completed":
3403
+ m.steps.completed += 1;
3404
+ if (e.durationMs != null) {
3405
+ m.stepDuration.sumMs += e.durationMs;
3406
+ m.stepDuration.count += 1;
3407
+ }
3408
+ break;
3409
+ case "step.failed":
3410
+ m.steps.failed += 1;
3411
+ break;
3412
+ }
3413
+ });
3414
+ return {
3415
+ snapshot: /* @__PURE__ */ __name(() => structuredClone(m), "snapshot"),
3416
+ stop,
3417
+ prometheus: /* @__PURE__ */ __name(() => {
3418
+ const lines = [];
3419
+ lines.push("# TYPE durable_runs_total counter");
3420
+ for (const [k, v] of Object.entries(m.runs)) lines.push(`durable_runs_total{event="${k}"} ${v}`);
3421
+ lines.push("# TYPE durable_steps_total counter");
3422
+ for (const [k, v] of Object.entries(m.steps)) lines.push(`durable_steps_total{event="${k}"} ${v}`);
3423
+ lines.push("# TYPE durable_runs_by_workflow_total counter");
3424
+ for (const [name, v] of Object.entries(m.byWorkflow)) for (const [event, n] of Object.entries(v)) lines.push(`durable_runs_by_workflow_total{workflow="${name}",event="${event}"} ${n}`);
3425
+ lines.push("# TYPE durable_step_duration_ms_sum counter");
3426
+ lines.push(`durable_step_duration_ms_sum ${m.stepDuration.sumMs}`);
3427
+ lines.push("# TYPE durable_step_duration_count counter");
3428
+ lines.push(`durable_step_duration_count ${m.stepDuration.count}`);
3429
+ return `${lines.join("\n")}
3430
+ `;
3431
+ }, "prometheus")
3432
+ };
3433
+ }
3434
+ __name(collectMetrics, "collectMetrics");
3435
+
3436
+ // src/scheduler.ts
3437
+ var import_node_module = require("module");
3438
+ var nodeRequire = (0, import_node_module.createRequire)(__importMetaUrl);
3439
+ var defaultSleep = /* @__PURE__ */ __name((ms) => new Promise((r) => setTimeout(r, ms)), "defaultSleep");
3440
+ function scheduledRunId(key, everyMs, nowMs) {
3441
+ return `sched:${key}:${Math.floor(nowMs / everyMs)}`;
3442
+ }
3443
+ __name(scheduledRunId, "scheduledRunId");
3444
+ var cronParser;
3445
+ function loadCronParser() {
3446
+ if (cronParser) return cronParser;
3447
+ try {
3448
+ cronParser = nodeRequire("cron-parser");
3449
+ } catch {
3450
+ throw new Error('cron schedules need the optional peer dependency "cron-parser" \u2014 install it (e.g. `npm i cron-parser`).');
3451
+ }
3452
+ return cronParser;
3453
+ }
3454
+ __name(loadCronParser, "loadCronParser");
3455
+ function prevCronFireMs(expr, nowMs, timezone = "UTC") {
3456
+ const parser = loadCronParser();
3457
+ const it = parser.parseExpression(expr, {
3458
+ currentDate: new Date(nowMs + 1),
3459
+ tz: timezone
3460
+ });
3461
+ return it.prev().toDate().getTime();
3462
+ }
3463
+ __name(prevCronFireMs, "prevCronFireMs");
3464
+ function scheduleRunIdAt(s, nowMs) {
3465
+ if (s.cron != null) return `sched:${s.key}:${prevCronFireMs(s.cron, nowMs, s.timezone)}`;
3466
+ if (s.everyMs != null) return scheduledRunId(s.key, s.everyMs, nowMs);
3467
+ throw new Error(`schedule "${s.key}" needs either "everyMs" or "cron"`);
3468
+ }
3469
+ __name(scheduleRunIdAt, "scheduleRunIdAt");
3470
+ function scheduleRunIdsAt(s, nowMs) {
3471
+ const current = scheduleRunIdAt(s, nowMs);
3472
+ if (!s.backfill || s.backfill.maxCatchup <= 0) return [
3473
+ current
3474
+ ];
3475
+ const ids = [];
3476
+ if (s.everyMs != null) {
3477
+ const bucket = Math.floor(nowMs / s.everyMs);
3478
+ const oldest = Math.max(0, bucket - s.backfill.maxCatchup);
3479
+ for (let b = oldest; b < bucket; b += 1) ids.push(`sched:${s.key}:${b}`);
3480
+ } else if (s.cron != null) {
3481
+ const prior = [];
3482
+ let cursor = prevCronFireMs(s.cron, nowMs, s.timezone);
3483
+ for (let i = 0; i < s.backfill.maxCatchup; i += 1) {
3484
+ const before = prevCronFireMs(s.cron, cursor - 1, s.timezone);
3485
+ prior.push(`sched:${s.key}:${before}`);
3486
+ cursor = before;
3487
+ }
3488
+ ids.push(...prior.reverse());
3489
+ }
3490
+ ids.push(current);
3491
+ return ids;
3492
+ }
3493
+ __name(scheduleRunIdsAt, "scheduleRunIdsAt");
3494
+ async function runSchedules(engine, schedules, nowMs, opts) {
3495
+ const random = opts?.random ?? Math.random;
3496
+ const sleep = opts?.sleep ?? defaultSleep;
3497
+ const ids = [];
3498
+ for (const s of schedules) {
3499
+ if (s.paused) continue;
3500
+ if (s.overlap === "skip" && s.everyMs) {
3501
+ const prevBucket = Math.floor(nowMs / s.everyMs) - 1;
3502
+ if (prevBucket >= 0) {
3503
+ const prev = await engine.getRun(`sched:${s.key}:${prevBucket}`);
3504
+ if (prev && (prev.status === "running" || prev.status === "suspended")) continue;
3505
+ }
3506
+ }
3507
+ if (s.jitter && s.jitter > 0) await sleep(Math.floor(random() * s.jitter));
3508
+ for (const runId of scheduleRunIdsAt(s, nowMs)) {
3509
+ await engine.start(s.workflow, s.input, runId);
3510
+ ids.push(runId);
3511
+ }
3512
+ }
3513
+ return ids;
3514
+ }
3515
+ __name(runSchedules, "runSchedules");
3516
+
3517
+ // src/search-attributes.ts
3518
+ function compare(actual, op, expected) {
3519
+ switch (op) {
3520
+ case "eq":
3521
+ return actual === expected;
3522
+ case "ne":
3523
+ return actual !== expected;
3524
+ case "gt":
3525
+ return actual != null && actual > expected;
3526
+ case "gte":
3527
+ return actual != null && actual >= expected;
3528
+ case "lt":
3529
+ return actual != null && actual < expected;
3530
+ case "lte":
3531
+ return actual != null && actual <= expected;
3532
+ }
3533
+ }
3534
+ __name(compare, "compare");
3535
+ function matchesAttributes(attributes, filters) {
3536
+ if (!filters?.length) return true;
3537
+ if (!attributes) return false;
3538
+ return filters.every((f) => f.key in attributes && compare(attributes[f.key], f.op, f.value));
3539
+ }
3540
+ __name(matchesAttributes, "matchesAttributes");
3541
+ function applyAttributeQuery(rows, query) {
3542
+ const filtered = rows.filter((r) => matchesAttributes(r.searchAttributes, query.attributes));
3543
+ const offset = query.offset ?? 0;
3544
+ const limit = query.limit ?? filtered.length;
3545
+ return filtered.slice(offset, offset + limit);
3546
+ }
3547
+ __name(applyAttributeQuery, "applyAttributeQuery");
3548
+ function normalizeAttributeRows(runId, attributes) {
3549
+ if (!attributes) return [];
3550
+ const out = [];
3551
+ for (const [key, value] of Object.entries(attributes)) {
3552
+ if (typeof value === "number") {
3553
+ out.push({
3554
+ runId,
3555
+ key,
3556
+ strValue: null,
3557
+ numValue: value
3558
+ });
3559
+ } else if (typeof value === "boolean") {
3560
+ out.push({
3561
+ runId,
3562
+ key,
3563
+ strValue: value ? "true" : "false",
3564
+ numValue: null
3565
+ });
3566
+ } else {
3567
+ out.push({
3568
+ runId,
3569
+ key,
3570
+ strValue: value,
3571
+ numValue: null
3572
+ });
3573
+ }
3574
+ }
3575
+ return out;
3576
+ }
3577
+ __name(normalizeAttributeRows, "normalizeAttributeRows");
3578
+ function sqlComparator(op) {
3579
+ switch (op) {
3580
+ case "eq":
3581
+ return "=";
3582
+ case "ne":
3583
+ return "<>";
3584
+ case "gt":
3585
+ return ">";
3586
+ case "gte":
3587
+ return ">=";
3588
+ case "lt":
3589
+ return "<";
3590
+ case "lte":
3591
+ return "<=";
3592
+ }
3593
+ }
3594
+ __name(sqlComparator, "sqlComparator");
3595
+ function attributeColumnFor(filter) {
3596
+ return typeof filter.value === "number" ? "numValue" : "strValue";
3597
+ }
3598
+ __name(attributeColumnFor, "attributeColumnFor");
3599
+ function attributeOperand(filter) {
3600
+ if (typeof filter.value === "boolean") return filter.value ? "true" : "false";
3601
+ return filter.value;
3602
+ }
3603
+ __name(attributeOperand, "attributeOperand");
3604
+
3605
+ // src/tokens.ts
3606
+ var STATE_STORE = Symbol("nestjs-durable:STATE_STORE");
3607
+ var TRANSPORT = Symbol("nestjs-durable:TRANSPORT");
3608
+ var DURABLE_OPTIONS = Symbol("nestjs-durable:DURABLE_OPTIONS");
3609
+
3610
+ // src/testing/in-memory-state-store.ts
3611
+ var InMemoryStateStore = class {
3612
+ static {
3613
+ __name(this, "InMemoryStateStore");
3614
+ }
3615
+ runs = /* @__PURE__ */ new Map();
3616
+ checkpoints = /* @__PURE__ */ new Map();
3617
+ signalWaiters = /* @__PURE__ */ new Map();
3618
+ /**
3619
+ * Normalized search-attribute side-table, the in-memory analog of the SQL stores'
3620
+ * `durable_run_attributes`: `key -> (runId -> row)`. Maintained on every create/update so an
3621
+ * attribute query can be pushed DOWN to a key-indexed candidate set instead of scanning every run
3622
+ * and comparing in-process. Mirrors {@link normalizeAttributeRows} so behavior matches the SQL path.
3623
+ */
3624
+ attributeIndex = /* @__PURE__ */ new Map();
3625
+ /** Test/observability hook: how many candidate runs the last attribute query's index produced. */
3626
+ lastAttributeCandidates = 0;
3627
+ key(runId, seq) {
3628
+ return `${runId}:${seq}`;
3629
+ }
3630
+ /** Drop then re-add a run's normalized attribute rows so the index tracks its current attributes. */
3631
+ reindexAttributes(runId, attributes) {
3632
+ for (const byRun of this.attributeIndex.values()) byRun.delete(runId);
3633
+ for (const row of normalizeAttributeRows(runId, attributes)) {
3634
+ let byRun = this.attributeIndex.get(row.key);
3635
+ if (!byRun) {
3636
+ byRun = /* @__PURE__ */ new Map();
3637
+ this.attributeIndex.set(row.key, byRun);
3638
+ }
3639
+ byRun.set(runId, row);
3640
+ }
3641
+ }
3642
+ async createRun(run) {
3643
+ this.runs.set(run.id, {
3644
+ ...run
3645
+ });
3646
+ this.reindexAttributes(run.id, run.searchAttributes);
3647
+ }
3648
+ async updateRun(runId, patch) {
3649
+ const existing = this.runs.get(runId);
3650
+ if (!existing) throw new Error(`run ${runId} not found`);
3651
+ const next = {
3652
+ ...existing,
3653
+ ...patch
3654
+ };
3655
+ this.runs.set(runId, next);
3656
+ if ("searchAttributes" in patch) this.reindexAttributes(runId, next.searchAttributes);
3657
+ }
3658
+ async getRun(runId) {
3659
+ const run = this.runs.get(runId);
3660
+ return run ? {
3661
+ ...run
3662
+ } : null;
3663
+ }
3664
+ async getCheckpoint(runId, seq) {
3665
+ const cp = this.checkpoints.get(this.key(runId, seq));
3666
+ return cp ? {
3667
+ ...cp
3668
+ } : null;
3669
+ }
3670
+ async saveCheckpoint(checkpoint) {
3671
+ this.checkpoints.set(this.key(checkpoint.runId, checkpoint.seq), {
3672
+ ...checkpoint
3673
+ });
3674
+ }
3675
+ async listIncompleteRuns() {
3676
+ return [
3677
+ ...this.runs.values()
3678
+ ].filter((r) => r.status === "running").map((r) => ({
3679
+ ...r
3680
+ }));
3681
+ }
3682
+ async listPendingRuns(limit) {
3683
+ return [
3684
+ ...this.runs.values()
3685
+ ].filter((r) => r.status === "pending").sort((a, b) => a.createdAt.getTime() - b.createdAt.getTime() || a.id.localeCompare(b.id)).slice(0, limit).map((r) => ({
3686
+ ...r
3687
+ }));
3688
+ }
3689
+ async listDueTimers(nowMs) {
3690
+ return [
3691
+ ...this.runs.values()
3692
+ ].filter((r) => r.status === "suspended" && r.wakeAt !== void 0 && r.wakeAt <= nowMs).map((r) => ({
3693
+ ...r
3694
+ }));
3695
+ }
3696
+ async tryLockRun(runId, owner, leaseUntilMs, nowMs) {
3697
+ const run = this.runs.get(runId);
3698
+ if (!run) return false;
3699
+ if (run.lockedUntil !== void 0 && run.lockedUntil > nowMs) return false;
3700
+ run.lockedBy = owner;
3701
+ run.lockedUntil = leaseUntilMs;
3702
+ return true;
3703
+ }
3704
+ async releaseRunLock(runId) {
3705
+ const run = this.runs.get(runId);
3706
+ if (run) {
3707
+ run.lockedBy = void 0;
3708
+ run.lockedUntil = void 0;
3709
+ }
3710
+ }
3711
+ async renewRunLock(runId, owner, leaseUntilMs) {
3712
+ const run = this.runs.get(runId);
3713
+ if (!run || run.lockedBy !== owner) return false;
3714
+ run.lockedUntil = leaseUntilMs;
3715
+ return true;
3716
+ }
3717
+ async putSignalWaiter(waiter) {
3718
+ this.signalWaiters.set(waiter.token, {
3719
+ ...waiter
3720
+ });
3721
+ }
3722
+ async takeSignalWaiter(token) {
3723
+ const waiter = this.signalWaiters.get(token);
3724
+ if (!waiter) return null;
3725
+ this.signalWaiters.delete(token);
3726
+ return {
3727
+ ...waiter
3728
+ };
3729
+ }
3730
+ async listSignalWaiters(prefix) {
3731
+ return [
3732
+ ...this.signalWaiters.values()
3733
+ ].filter((w) => w.token.startsWith(prefix)).map((w) => ({
3734
+ ...w
3735
+ }));
3736
+ }
3737
+ // No real DB transaction (there's no DB) — run the work and save the checkpoint. Exactly-once
3738
+ // across a crash needs a SQL store; this keeps `ctx.transaction` usable in tests / local dev.
3739
+ async transaction(work) {
3740
+ return work({
3741
+ raw: this,
3742
+ saveCheckpoint: /* @__PURE__ */ __name((cp) => this.saveCheckpoint(cp), "saveCheckpoint")
3743
+ });
3744
+ }
3745
+ bufferedSignals = /* @__PURE__ */ new Map();
3746
+ async bufferSignal(token, payload) {
3747
+ const queue = this.bufferedSignals.get(token) ?? [];
3748
+ queue.push(payload);
3749
+ this.bufferedSignals.set(token, queue);
3750
+ }
3751
+ async takeBufferedSignal(token) {
3752
+ const queue = this.bufferedSignals.get(token);
3753
+ if (!queue || queue.length === 0) return null;
3754
+ const payload = queue.shift();
3755
+ if (queue.length === 0) this.bufferedSignals.delete(token);
3756
+ return {
3757
+ payload
3758
+ };
3759
+ }
3760
+ /**
3761
+ * Candidate run ids satisfying ONE attribute predicate, served from {@link attributeIndex} — the
3762
+ * pushdown step. Numeric predicates scan only `numValue` rows under the key; string/boolean ones
3763
+ * scan `strValue` rows. This is the in-memory analog of the SQL EXISTS on `durable_run_attributes`.
3764
+ */
3765
+ candidatesForFilter(f) {
3766
+ const byRun = this.attributeIndex.get(f.key);
3767
+ const out = /* @__PURE__ */ new Set();
3768
+ if (!byRun) return out;
3769
+ const numeric = typeof f.value === "number";
3770
+ const operand = typeof f.value === "boolean" ? f.value ? "true" : "false" : f.value;
3771
+ for (const row of byRun.values()) {
3772
+ const actual = numeric ? row.numValue : row.strValue;
3773
+ if (actual == null) continue;
3774
+ let ok = false;
3775
+ switch (f.op) {
3776
+ case "eq":
3777
+ ok = actual === operand;
3778
+ break;
3779
+ case "ne":
3780
+ ok = actual !== operand;
3781
+ break;
3782
+ case "gt":
3783
+ ok = actual > operand;
3784
+ break;
3785
+ case "gte":
3786
+ ok = actual >= operand;
3787
+ break;
3788
+ case "lt":
3789
+ ok = actual < operand;
3790
+ break;
3791
+ case "lte":
3792
+ ok = actual <= operand;
3793
+ break;
3794
+ }
3795
+ if (ok) out.add(row.runId);
3796
+ }
3797
+ return out;
3798
+ }
3799
+ async listRuns(query) {
3800
+ let runs = [
3801
+ ...this.runs.values()
3802
+ ];
3803
+ if (query.workflow) runs = runs.filter((r) => r.workflow === query.workflow);
3804
+ if (query.status) runs = runs.filter((r) => r.status === query.status);
3805
+ if (query.statuses) runs = runs.filter((r) => query.statuses?.includes(r.status));
3806
+ if (query.tag) runs = runs.filter((r) => r.tags?.includes(query.tag));
3807
+ if (query.attributes?.length) {
3808
+ let candidates = null;
3809
+ for (const f of query.attributes) {
3810
+ const next = this.candidatesForFilter(f);
3811
+ if (candidates == null) {
3812
+ candidates = next;
3813
+ } else {
3814
+ const prev = candidates;
3815
+ candidates = new Set([
3816
+ ...prev
3817
+ ].filter((id) => next.has(id)));
3818
+ }
3819
+ if (candidates.size === 0) break;
3820
+ }
3821
+ const ids = candidates ?? /* @__PURE__ */ new Set();
3822
+ this.lastAttributeCandidates = ids.size;
3823
+ runs = runs.filter((r) => ids.has(r.id));
3824
+ }
3825
+ runs.sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime());
3826
+ const offset = query.offset ?? 0;
3827
+ const limit = query.limit ?? runs.length;
3828
+ return runs.slice(offset, offset + limit).map((r) => ({
3829
+ ...r
3830
+ }));
3831
+ }
3832
+ async listCheckpoints(runId) {
3833
+ return [
3834
+ ...this.checkpoints.values()
3835
+ ].filter((cp) => cp.runId === runId).sort((a, b) => a.seq - b.seq).map((cp) => ({
3836
+ ...cp
3837
+ }));
3838
+ }
3839
+ };
3840
+
3841
+ // src/testing/in-memory-transport.ts
3842
+ var InMemoryTransport = class {
3843
+ static {
3844
+ __name(this, "InMemoryTransport");
3845
+ }
3846
+ handlers = /* @__PURE__ */ new Map();
3847
+ resultHandler;
3848
+ stepEventHandler;
3849
+ controlHandlers = /* @__PURE__ */ new Set();
3850
+ /** Register a fake worker handler for a step name. */
3851
+ handle(name, fn) {
3852
+ this.handlers.set(name, fn);
3853
+ }
3854
+ async dispatch(task) {
3855
+ if (!this.resultHandler) throw new Error("no result handler registered");
3856
+ const result = await runStepHandler(task, this.handlers.get(task.name));
3857
+ setImmediate(() => void this.resultHandler?.(result));
3858
+ }
3859
+ onResult(handler) {
3860
+ this.resultHandler = handler;
3861
+ }
3862
+ onHeartbeat(_handler) {
3863
+ }
3864
+ /** Workflow step lifecycle: deliver to the engine's handler (async, mirroring a real broker). */
3865
+ async dispatchStepEvent(event) {
3866
+ await this.stepEventHandler?.(event);
3867
+ }
3868
+ onStepEvent(handler) {
3869
+ this.stepEventHandler = handler;
3870
+ }
3871
+ // Control plane: broadcast to every registered handler (including the publisher's own — the
3872
+ // engine dedupes by `from`), mirroring how a real broker echoes a publish to all subscribers.
3873
+ async publishControl(msg) {
3874
+ for (const handler of this.controlHandlers) handler(msg);
3875
+ }
3876
+ onControl(handler) {
3877
+ this.controlHandlers.add(handler);
3878
+ }
3879
+ };
3880
+ // Annotate the CommonJS export names for ESM import in node:
3881
+ 0 && (module.exports = {
3882
+ CodecStateStore,
3883
+ ContinueAsNew,
3884
+ DURABLE_OPTIONS,
3885
+ Entities,
3886
+ EventAccumulators,
3887
+ FatalError,
3888
+ InMemoryStateStore,
3889
+ InMemoryTransport,
3890
+ NonDeterminismError,
3891
+ QueueController,
3892
+ RemoteStepError,
3893
+ RemoteStepTimeout,
3894
+ RemoteWorkflowExecutor,
3895
+ STATE_STORE,
3896
+ SignalTimeoutError,
3897
+ SingletonQueueFullError,
3898
+ TRANSPORT,
3899
+ WORKFLOW_NAME_KEY,
3900
+ WorkflowEngine,
3901
+ WorkflowSuspended,
3902
+ applyAttributeQuery,
3903
+ attributeColumnFor,
3904
+ attributeOperand,
3905
+ breakpointToken,
3906
+ collectMetrics,
3907
+ eventMatchOf,
3908
+ eventMatches,
3909
+ eventPrefix,
3910
+ eventToken,
3911
+ matchesAttributes,
3912
+ normalizeAttributeRows,
3913
+ parseDuration,
3914
+ prevCronFireMs,
3915
+ remoteStep,
3916
+ runSchedules,
3917
+ runStepHandler,
3918
+ scheduledRunId,
3919
+ sqlComparator,
3920
+ stepId,
3921
+ workflowName
3922
+ });
3923
+ //# sourceMappingURL=index.cjs.map