@multiplier-labs/stepflow 0.3.1 → 0.3.4

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 (112) hide show
  1. package/.codekin/reports/code-review/2026-04-11_code-review-daily.md +146 -0
  2. package/.codekin/reports/code-review/2026-04-13_code-review-daily.md +74 -0
  3. package/.codekin/reports/comments/2026-04-04_comment-assessment.md +179 -0
  4. package/.codekin/reports/comments/2026-04-11_comment-assessment.md +161 -0
  5. package/.codekin/reports/comments/2026-04-14_comment-assessment.md +34 -0
  6. package/.codekin/reports/complexity/2026-04-03_complexity-report.md +152 -0
  7. package/.codekin/reports/complexity/2026-04-11_complexity-report.md +152 -0
  8. package/.codekin/reports/complexity/2026-04-14_complexity-report.md +34 -0
  9. package/.codekin/reports/dependencies/2026-04-04_dependency-health.md +94 -0
  10. package/.codekin/reports/dependencies/2026-04-11_dependency-health.md +93 -0
  11. package/.codekin/reports/docs-audit/2026-04-04_docs-audit.md +10 -0
  12. package/.codekin/reports/docs-audit/2026-04-11_docs-audit.md +138 -0
  13. package/.codekin/reports/docs-audit/2026-04-13_docs-audit.md +121 -0
  14. package/.codekin/reports/security/2026-04-03_security-audit.md +208 -0
  15. package/.codekin/reports/security/2026-04-11_security-audit.md +297 -0
  16. package/.github/CODEOWNERS +15 -0
  17. package/.github/dependabot.yml +23 -0
  18. package/.github/workflows/ci.yml +38 -0
  19. package/.github/workflows/publish.yml +17 -5
  20. package/.github/workflows/security.yml +29 -0
  21. package/CHANGELOG.md +34 -0
  22. package/README.md +24 -0
  23. package/SECURITY.md +40 -0
  24. package/dist/{chunk-FQCT3I2A.js → chunk-5XJBAPXF.js} +527 -551
  25. package/dist/chunk-5XJBAPXF.js.map +1 -0
  26. package/dist/{chunk-U5QGN3RT.js → chunk-FMPJBK2N.js} +67 -55
  27. package/dist/chunk-FMPJBK2N.js.map +1 -0
  28. package/dist/{chunk-GXY66V7Q.js → chunk-JPXPB7U5.js} +52 -2
  29. package/dist/chunk-JPXPB7U5.js.map +1 -0
  30. package/dist/{chunk-XCF4PLN7.js → chunk-U3PCVK6O.js} +276 -61
  31. package/dist/chunk-U3PCVK6O.js.map +1 -0
  32. package/dist/events/index.d.ts +60 -7
  33. package/dist/events/index.js +1 -1
  34. package/dist/{index-CPfGsmrp.d.ts → index-Cd0IZ40L.d.ts} +56 -17
  35. package/dist/index.d.ts +15 -23
  36. package/dist/index.js +784 -547
  37. package/dist/index.js.map +1 -1
  38. package/dist/scheduler/index.d.ts +4 -4
  39. package/dist/scheduler/index.js +2 -2
  40. package/dist/storage/index.d.ts +322 -56
  41. package/dist/storage/index.js +4 -2
  42. package/dist/{types-BlwFEGzj.d.ts → types-DSEjiLMj.d.ts} +16 -15
  43. package/dist/{types-DzkEqnQJ.d.ts → types-DZUdWmCc.d.ts} +2 -2
  44. package/dist/{types-BVShq7zT.d.ts → types-K5Gjk3H_.d.ts} +4 -4
  45. package/docs/stepflow-api-reference.md +5 -4
  46. package/package.json +12 -8
  47. package/src/core/engine/concurrency.ts +229 -0
  48. package/src/core/engine/lifecycle.ts +176 -0
  49. package/src/core/engine.test.ts +536 -522
  50. package/src/core/engine.ts +116 -235
  51. package/src/core/index.ts +3 -3
  52. package/src/core/orchestrator/persistence.ts +165 -0
  53. package/src/core/orchestrator/plan-resolution.ts +140 -0
  54. package/src/core/orchestrator/state-transitions.ts +109 -0
  55. package/src/core/orchestrator/step-runner.ts +427 -0
  56. package/src/core/orchestrator.ts +57 -395
  57. package/src/core/types.ts +7 -29
  58. package/src/events/index.ts +4 -4
  59. package/src/events/memory.test.ts +39 -41
  60. package/src/events/memory.ts +7 -10
  61. package/src/events/socketio.test.ts +112 -162
  62. package/src/events/socketio.ts +20 -27
  63. package/src/events/types.ts +16 -19
  64. package/src/events/webhook.test.ts +489 -235
  65. package/src/events/webhook.ts +355 -139
  66. package/src/index.ts +19 -19
  67. package/src/planning/evaluate-condition.test.ts +153 -0
  68. package/src/planning/evaluate-condition.ts +105 -0
  69. package/src/planning/index.ts +6 -3
  70. package/src/planning/planner.ts +45 -152
  71. package/src/planning/planning.test.ts +303 -366
  72. package/src/planning/registry.test.ts +204 -251
  73. package/src/planning/registry.ts +20 -106
  74. package/src/planning/types.ts +23 -27
  75. package/src/scheduler/cron.test.ts +149 -150
  76. package/src/scheduler/cron.ts +58 -69
  77. package/src/scheduler/index.ts +4 -14
  78. package/src/scheduler/postgres-persistence.ts +89 -105
  79. package/src/scheduler/sqlite-persistence.test.ts +96 -96
  80. package/src/scheduler/sqlite-persistence.ts +40 -22
  81. package/src/scheduler/types.ts +4 -9
  82. package/src/storage/index.ts +5 -5
  83. package/src/storage/memory.test.ts +133 -183
  84. package/src/storage/memory.ts +21 -42
  85. package/src/storage/postgres-core.ts +550 -0
  86. package/src/storage/postgres-migration.test.ts +69 -0
  87. package/src/storage/postgres-transaction-pagination.test.ts +107 -0
  88. package/src/storage/postgres.ts +222 -937
  89. package/src/storage/sqlite-core.ts +151 -0
  90. package/src/storage/sqlite.test.ts +330 -281
  91. package/src/storage/sqlite.ts +225 -170
  92. package/src/storage/types.ts +33 -58
  93. package/src/utils/errors.test.ts +78 -80
  94. package/src/utils/errors.ts +27 -57
  95. package/src/utils/id.ts +3 -3
  96. package/src/utils/index.ts +6 -4
  97. package/src/utils/logger.test.ts +52 -58
  98. package/src/utils/logger.ts +4 -4
  99. package/src/utils/pinned-fetch.test.ts +84 -0
  100. package/src/utils/pinned-fetch.ts +118 -0
  101. package/src/utils/postgres-deps.ts +20 -4
  102. package/src/utils/retry.test.ts +47 -67
  103. package/src/utils/retry.ts +9 -9
  104. package/src/utils/safe-json.test.ts +166 -0
  105. package/src/utils/safe-json.ts +107 -0
  106. package/src/utils/webhook-signature.test.ts +111 -0
  107. package/src/utils/webhook-signature.ts +74 -0
  108. package/dist/chunk-FQCT3I2A.js.map +0 -1
  109. package/dist/chunk-GXY66V7Q.js.map +0 -1
  110. package/dist/chunk-U5QGN3RT.js.map +0 -1
  111. package/dist/chunk-XCF4PLN7.js.map +0 -1
  112. package/src/scheduler/postgres-persistence.test.ts +0 -906
@@ -0,0 +1,427 @@
1
+ /**
2
+ * Step runner — executes a single workflow step with hooks, retries,
3
+ * cancellation, and timeout handling.
4
+ *
5
+ * The shape of the previous monolithic `executeStep` was a `while (true)`
6
+ * loop containing a `try`/`catch` with multiple nested `if` branches per
7
+ * error path (cancel, hook, skip, retry, fail). This file flattens that:
8
+ *
9
+ * - Each error path is its own intent-named helper
10
+ * (handleSkipAction, handleRetryAction, runOnStepErrorHook, etc.)
11
+ * - The main loop nesting is at most 3 levels (while → try → switch)
12
+ *
13
+ * State-transition rules covered here (see also state-transitions.ts):
14
+ *
15
+ * pre-attempt:
16
+ * - abort signal already raised → throw WorkflowCanceledError
17
+ *
18
+ * per-attempt:
19
+ * - handler resolves → markStepSucceeded, emit step.completed, return
20
+ * - handler rejects + canceled → markStepFailed(canceled), emit step.failed, rethrow
21
+ * - handler rejects + policy=skip → emit step.skipped, return undefined
22
+ * - handler rejects + policy=retry → emit step.retry, sleep, continue
23
+ * - handler rejects + policy=fail → throw StepError
24
+ */
25
+ import type {
26
+ WorkflowDefinition,
27
+ WorkflowStep,
28
+ WorkflowContext,
29
+ Logger,
30
+ } from '../types';
31
+ import type { StorageAdapter, WorkflowRunStepRecord } from '../../storage/types';
32
+ import type { EventTransport } from '../../events/types';
33
+ import {
34
+ StepError,
35
+ StepTimeoutError,
36
+ WorkflowCanceledError,
37
+ } from '../../utils/errors';
38
+ import { sleep, calculateRetryDelay } from '../../utils/retry';
39
+ import {
40
+ createStepRecord,
41
+ markStepSucceeded,
42
+ markStepFailed,
43
+ markStepCanceled,
44
+ safeEmitEvent,
45
+ } from './persistence';
46
+ import {
47
+ resolveStepErrorPolicy,
48
+ decideStepFailureAction,
49
+ type StepErrorPolicy,
50
+ } from './state-transitions';
51
+
52
+ /**
53
+ * Inputs to `executeStep` beyond the step + context themselves.
54
+ */
55
+ export interface StepExecutionOptions<TInput = Record<string, unknown>> {
56
+ definition: WorkflowDefinition<TInput>;
57
+ storage: StorageAdapter;
58
+ events: EventTransport;
59
+ logger: Logger;
60
+ abortController: AbortController;
61
+ }
62
+
63
+ /**
64
+ * Execute a single workflow step with error handling and retries.
65
+ *
66
+ * Returns the step's result on success, or `undefined` if the resolved
67
+ * error policy is `skip` and the step failed. Throws on cancellation,
68
+ * timeout, or `fail` policy.
69
+ */
70
+ export async function executeStep<TInput>(
71
+ step: WorkflowStep<TInput>,
72
+ context: WorkflowContext<TInput>,
73
+ options: StepExecutionOptions<TInput>
74
+ ): Promise<unknown> {
75
+ const { definition, storage, events, logger, abortController } = options;
76
+ const policy = resolveStepErrorPolicy(step, definition);
77
+
78
+ let attempt = 0;
79
+
80
+ while (true) {
81
+ attempt++;
82
+ throwIfCanceled(abortController, context.runId);
83
+
84
+ const stepRecord = await beginStepAttempt(
85
+ storage,
86
+ events,
87
+ logger,
88
+ context,
89
+ step,
90
+ attempt
91
+ );
92
+
93
+ if (definition.hooks?.beforeStep) {
94
+ await definition.hooks.beforeStep(context, step);
95
+ }
96
+
97
+ try {
98
+ const result = await runStepHandler(step, context, abortController);
99
+ await completeStepAttempt(
100
+ storage,
101
+ events,
102
+ logger,
103
+ stepRecord,
104
+ context,
105
+ step,
106
+ result,
107
+ definition
108
+ );
109
+ return result;
110
+ } catch (error) {
111
+ const lastError = toError(error);
112
+ const isCanceled =
113
+ error instanceof WorkflowCanceledError || abortController.signal.aborted;
114
+
115
+ await markStepFailed(storage, stepRecord.id, error, isCanceled);
116
+
117
+ if (isCanceled) {
118
+ emitStepFailedEvent(events, logger, context, step, lastError, attempt);
119
+ throw error;
120
+ }
121
+
122
+ await runOnStepErrorHook(definition, context, step, lastError, logger);
123
+ emitStepFailedEvent(events, logger, context, step, lastError, attempt);
124
+
125
+ const action = decideStepFailureAction(policy, attempt);
126
+
127
+ if (action === 'skip') {
128
+ handleSkipAction(events, logger, context, step, lastError);
129
+ return undefined;
130
+ }
131
+
132
+ if (action === 'retry') {
133
+ await handleRetryAction(
134
+ storage,
135
+ events,
136
+ logger,
137
+ abortController,
138
+ context,
139
+ step,
140
+ attempt,
141
+ policy,
142
+ lastError,
143
+ stepRecord.id
144
+ );
145
+ continue;
146
+ }
147
+
148
+ // action === 'fail' — explicit fail strategy or retries exhausted
149
+ throw new StepError(step.key, lastError.message, attempt, lastError);
150
+ }
151
+ }
152
+ }
153
+
154
+ /**
155
+ * Throw a WorkflowCanceledError if the abort signal has already been raised.
156
+ * Called at the top of each attempt so we don't even create a step record
157
+ * after cancellation.
158
+ */
159
+ function throwIfCanceled(abortController: AbortController, runId: string): void {
160
+ if (abortController.signal.aborted) {
161
+ throw new WorkflowCanceledError(runId);
162
+ }
163
+ }
164
+
165
+ /**
166
+ * Persist a fresh step record for this attempt, set it as the current step
167
+ * id on the context (so handlers can attach granular telemetry), and emit
168
+ * `step.started`.
169
+ */
170
+ async function beginStepAttempt<TInput>(
171
+ storage: StorageAdapter,
172
+ events: EventTransport,
173
+ logger: Logger,
174
+ context: WorkflowContext<TInput>,
175
+ step: WorkflowStep<TInput>,
176
+ attempt: number
177
+ ): Promise<WorkflowRunStepRecord> {
178
+ const stepRecord = await createStepRecord(
179
+ storage,
180
+ context.runId,
181
+ step.key,
182
+ step.name,
183
+ attempt
184
+ );
185
+ context.stepId = stepRecord.id;
186
+ safeEmitEvent(events, logger, {
187
+ runId: context.runId,
188
+ kind: context.kind,
189
+ eventType: 'step.started',
190
+ stepKey: step.key,
191
+ timestamp: new Date(),
192
+ payload: { attempt },
193
+ });
194
+ return stepRecord;
195
+ }
196
+
197
+ /**
198
+ * Invoke the step handler. If the step has its own timeout, race against it
199
+ * AND the abort signal; otherwise just race against the abort signal so a
200
+ * workflow-level timeout/cancellation still interrupts the step.
201
+ */
202
+ async function runStepHandler<TInput>(
203
+ step: WorkflowStep<TInput>,
204
+ context: WorkflowContext<TInput>,
205
+ abortController: AbortController
206
+ ): Promise<unknown> {
207
+ if (step.timeout) {
208
+ return executeWithTimeout(
209
+ () => step.handler(context),
210
+ step.timeout,
211
+ abortController.signal,
212
+ step.key
213
+ );
214
+ }
215
+ return raceWithAbort(step.handler(context), abortController.signal);
216
+ }
217
+
218
+ /**
219
+ * Mark the step as succeeded, run the afterStep hook (if any), and emit
220
+ * `step.completed`.
221
+ */
222
+ async function completeStepAttempt<TInput>(
223
+ storage: StorageAdapter,
224
+ events: EventTransport,
225
+ logger: Logger,
226
+ stepRecord: WorkflowRunStepRecord,
227
+ context: WorkflowContext<TInput>,
228
+ step: WorkflowStep<TInput>,
229
+ result: unknown,
230
+ definition: WorkflowDefinition<TInput>
231
+ ): Promise<void> {
232
+ await markStepSucceeded(storage, stepRecord.id, result);
233
+ if (definition.hooks?.afterStep) {
234
+ await definition.hooks.afterStep(context, step, result);
235
+ }
236
+ safeEmitEvent(events, logger, {
237
+ runId: context.runId,
238
+ kind: context.kind,
239
+ eventType: 'step.completed',
240
+ stepKey: step.key,
241
+ timestamp: new Date(),
242
+ payload: { result },
243
+ });
244
+ }
245
+
246
+ /**
247
+ * Coerce an unknown thrown value into a real Error, preserving message
248
+ * text for non-Error throws.
249
+ */
250
+ function toError(error: unknown): Error {
251
+ return error instanceof Error ? error : new Error(String(error));
252
+ }
253
+
254
+ /**
255
+ * Run the user's onStepError hook (if defined). Hook failures are logged
256
+ * but never propagated — they must not mask the original step error.
257
+ */
258
+ async function runOnStepErrorHook<TInput>(
259
+ definition: WorkflowDefinition<TInput>,
260
+ context: WorkflowContext<TInput>,
261
+ step: WorkflowStep<TInput>,
262
+ lastError: Error,
263
+ logger: Logger
264
+ ): Promise<void> {
265
+ if (!definition.hooks?.onStepError) return;
266
+ try {
267
+ await definition.hooks.onStepError(context, step, lastError);
268
+ } catch (hookError) {
269
+ logger.error('onStepError hook failed for run ' + context.runId + ':', hookError);
270
+ }
271
+ }
272
+
273
+ /**
274
+ * Emit a `step.failed` event with the canonical payload.
275
+ */
276
+ function emitStepFailedEvent<TInput>(
277
+ events: EventTransport,
278
+ logger: Logger,
279
+ context: WorkflowContext<TInput>,
280
+ step: WorkflowStep<TInput>,
281
+ lastError: Error,
282
+ attempt: number
283
+ ): void {
284
+ safeEmitEvent(events, logger, {
285
+ runId: context.runId,
286
+ kind: context.kind,
287
+ eventType: 'step.failed',
288
+ stepKey: step.key,
289
+ timestamp: new Date(),
290
+ payload: { error: lastError.message, attempt },
291
+ });
292
+ }
293
+
294
+ /**
295
+ * Apply the `skip` failure action: warn, emit `step.skipped`, then the
296
+ * caller returns `undefined` from the step.
297
+ */
298
+ function handleSkipAction<TInput>(
299
+ events: EventTransport,
300
+ logger: Logger,
301
+ context: WorkflowContext<TInput>,
302
+ step: WorkflowStep<TInput>,
303
+ lastError: Error
304
+ ): void {
305
+ logger.warn(`Step "${step.key}" failed, skipping (strategy: skip)`);
306
+ safeEmitEvent(events, logger, {
307
+ runId: context.runId,
308
+ kind: context.kind,
309
+ eventType: 'step.skipped',
310
+ stepKey: step.key,
311
+ timestamp: new Date(),
312
+ payload: { reason: 'error', error: lastError.message },
313
+ });
314
+ }
315
+
316
+ /**
317
+ * Apply the `retry` failure action: emit `step.retry`, sleep with backoff,
318
+ * and let the loop fall through to the next attempt. If the sleep itself
319
+ * is interrupted by cancellation, mark the step record canceled before
320
+ * rethrowing.
321
+ */
322
+ async function handleRetryAction<TInput>(
323
+ storage: StorageAdapter,
324
+ events: EventTransport,
325
+ logger: Logger,
326
+ abortController: AbortController,
327
+ context: WorkflowContext<TInput>,
328
+ step: WorkflowStep<TInput>,
329
+ attempt: number,
330
+ policy: StepErrorPolicy,
331
+ lastError: Error,
332
+ stepRecordId: string
333
+ ): Promise<void> {
334
+ const delay = calculateRetryDelay(attempt, policy.retryDelay, policy.retryBackoff);
335
+ logger.warn(
336
+ `Step "${step.key}" failed, retrying in ${delay}ms (attempt ${attempt}/${policy.maxRetries})`
337
+ );
338
+ safeEmitEvent(events, logger, {
339
+ runId: context.runId,
340
+ kind: context.kind,
341
+ eventType: 'step.retry',
342
+ stepKey: step.key,
343
+ timestamp: new Date(),
344
+ payload: { attempt, maxRetries: policy.maxRetries, delay, error: lastError.message },
345
+ });
346
+
347
+ try {
348
+ await sleep(delay, abortController.signal);
349
+ } catch (sleepError) {
350
+ if (sleepError instanceof WorkflowCanceledError) {
351
+ await markStepCanceled(storage, stepRecordId);
352
+ }
353
+ throw sleepError;
354
+ }
355
+ }
356
+
357
+ /**
358
+ * Execute a function with a timeout.
359
+ *
360
+ * Uses Promise.race to resolve as soon as either the function completes, the
361
+ * timeout fires, or the abort signal triggers. The `finally` block is critical:
362
+ * without it, the losing promise's timer/listener would remain active, leaking
363
+ * memory — especially problematic in long-running engines processing many steps.
364
+ */
365
+ export async function executeWithTimeout<T>(
366
+ fn: () => Promise<T>,
367
+ timeoutMs: number,
368
+ signal: AbortSignal,
369
+ stepKey: string
370
+ ): Promise<T> {
371
+ let timeoutId: ReturnType<typeof setTimeout> | undefined;
372
+ let onAbort: (() => void) | undefined;
373
+
374
+ try {
375
+ return await Promise.race([
376
+ fn(),
377
+ new Promise<never>((_, reject) => {
378
+ timeoutId = setTimeout(() => {
379
+ reject(new StepTimeoutError(stepKey, timeoutMs));
380
+ }, timeoutMs);
381
+
382
+ onAbort = () => {
383
+ clearTimeout(timeoutId);
384
+ reject(new WorkflowCanceledError('run'));
385
+ };
386
+ signal.addEventListener('abort', onAbort, { once: true });
387
+ }),
388
+ ]);
389
+ } finally {
390
+ if (timeoutId) clearTimeout(timeoutId);
391
+ if (onAbort) signal.removeEventListener('abort', onAbort);
392
+ }
393
+ }
394
+
395
+ /**
396
+ * Race a promise against an abort signal.
397
+ *
398
+ * Steps without their own timeout still need to respect workflow-level timeouts
399
+ * and cancellation. This wraps the step handler in a Promise.race so that an
400
+ * abort signal (from workflow timeout or user cancellation) can interrupt it.
401
+ *
402
+ * The `finally` cleanup removes the abort listener to prevent accumulating
403
+ * listeners on the signal across many steps — without it, each step would
404
+ * leave a dangling listener even after completing successfully.
405
+ */
406
+ export async function raceWithAbort<T>(
407
+ promise: Promise<T>,
408
+ signal: AbortSignal
409
+ ): Promise<T> {
410
+ if (signal.aborted) {
411
+ throw new WorkflowCanceledError('run');
412
+ }
413
+
414
+ let onAbort: (() => void) | undefined;
415
+
416
+ try {
417
+ return await Promise.race([
418
+ promise,
419
+ new Promise<never>((_, reject) => {
420
+ onAbort = () => reject(new WorkflowCanceledError('run'));
421
+ signal.addEventListener('abort', onAbort, { once: true });
422
+ }),
423
+ ]);
424
+ } finally {
425
+ if (onAbort) signal.removeEventListener('abort', onAbort);
426
+ }
427
+ }