@dudousxd/nestjs-durable-core 0.42.0 → 0.44.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.
package/dist/index.d.cts CHANGED
@@ -260,6 +260,16 @@ interface WorkflowRun {
260
260
  * durable state required for correctness — a transport without priority support ignores it.
261
261
  */
262
262
  priority?: number | undefined;
263
+ /**
264
+ * REMOTE workflow turn awaiting a decision: the `taskId` of the turn the engine dispatched to a
265
+ * workflow worker and then SUSPENDED on. Set by the dispatch-and-suspend path; matched by
266
+ * {@link WorkflowEngine.completeRemoteDecision} so ONLY the decision for the currently-awaited turn
267
+ * is applied (a stale/duplicate/foreign decision is dropped), then cleared the moment it is applied.
268
+ * This is what makes a workflow-turn decision multi-instance safe: any engine instance that receives
269
+ * the decision looks the run up by `decision.runId` and applies it durably, instead of resolving an
270
+ * in-memory promise that only the dispatching instance held. Absent on non-remote / not-awaiting runs.
271
+ */
272
+ awaitingDecisionTaskId?: string | undefined;
263
273
  createdAt: Date;
264
274
  updatedAt: Date;
265
275
  }
@@ -784,7 +794,25 @@ interface WorkflowStepEvent {
784
794
  * concerns, identical for in-process and remote workflows.
785
795
  */
786
796
  interface WorkflowExecutor {
787
- advance(run: WorkflowRun, history: HistoryEvent[], pendingSignals?: WorkflowTask['pendingSignals']): Promise<WorkflowDecision>;
797
+ /**
798
+ * INLINE replay: advance the turn and RETURN its decision (the in-process executor runs the
799
+ * registered TS function synchronously). The engine awaits this under the run's lease. Provide this
800
+ * OR {@link dispatch} — exactly one. An executor with only `advance` is single-instance by nature
801
+ * (the decision is produced in-process, not delivered over a broker).
802
+ */
803
+ advance?(run: WorkflowRun, history: HistoryEvent[], pendingSignals?: WorkflowTask['pendingSignals']): Promise<WorkflowDecision>;
804
+ /**
805
+ * DISPATCH-AND-SUSPEND (broker-backed, multi-instance safe): dispatch the turn to a worker and
806
+ * return the dispatched `taskId` WITHOUT awaiting the decision. The engine then suspends the run,
807
+ * recording the `taskId` on it; the worker's {@link WorkflowDecision} comes back over the
808
+ * transport's {@link Transport.onDecision} and is applied DURABLY by whatever engine instance
809
+ * receives it ({@link WorkflowEngine.completeRemoteDecision} looks the run up by `decision.runId`).
810
+ * No in-memory await, so the decision is never lost to a point-to-point broker handing it to a
811
+ * non-dispatching instance. Provide this OR {@link advance} — exactly one.
812
+ */
813
+ dispatch?(run: WorkflowRun, history: HistoryEvent[], pendingSignals?: WorkflowTask['pendingSignals']): Promise<{
814
+ taskId: string;
815
+ }>;
788
816
  }
789
817
  /**
790
818
  * A transport in an ordered pool, identified by `id`. The engine dispatches on the first by default
@@ -1636,6 +1664,29 @@ declare class WorkflowEngine {
1636
1664
  executionTimeout?: string | number;
1637
1665
  validateInput?: (input: unknown) => void | Promise<void>;
1638
1666
  }): void;
1667
+ /**
1668
+ * The convenience form of {@link registerRemote}: register a workflow whose body runs on a worker
1669
+ * `group` (e.g. the Python `durable-worker`, or a separate TS worker), without hand-building a
1670
+ * {@link RemoteWorkflowExecutor}. It wires the standard broker executor — a `RemoteWorkflowExecutor`
1671
+ * over the engine's own transport, dispatching to `opts.group` — for you, so you write
1672
+ * `engine.remote('processing', { group: 'processing' })` instead of
1673
+ * `engine.registerRemote('processing', '1', { group: 'processing', executor: new RemoteWorkflowExecutor(transport, 'processing') })`.
1674
+ *
1675
+ * Defaults the version to `'1'`. All the other {@link registerRemote} options
1676
+ * (`tags` / `singleton` / `executionTimeout` / `validateInput`) pass straight through. Reach for the
1677
+ * low-level {@link registerRemote} when you need a custom {@link WorkflowExecutor} (a different
1678
+ * transport, a fixed `timeoutMs`, a polyglot stub) — `remote()` is the standard wiring on the rails.
1679
+ */
1680
+ remote(name: string, opts: {
1681
+ /** Worker group this workflow's turns are dispatched to (also the executor's target group). */
1682
+ group: string;
1683
+ /** Workflow version. Defaults to `'1'`. */
1684
+ version?: string;
1685
+ tags?: string[];
1686
+ singleton?: SingletonConfig;
1687
+ executionTimeout?: string | number;
1688
+ validateInput?: (input: unknown) => void | Promise<void>;
1689
+ }): void;
1639
1690
  /**
1640
1691
  * Register a flow-control queue referenced by `ctx.call(step, input, { queue })`. Caps concurrent
1641
1692
  * in-flight steps and/or the admission rate; blocked calls re-suspend and retry, so the limit is
@@ -1946,6 +1997,14 @@ declare class WorkflowEngine {
1946
1997
  */
1947
1998
  private settleRun;
1948
1999
  private runRemoteExecution;
2000
+ /**
2001
+ * Apply a remote workflow turn's {@link WorkflowDecision}: persist the local steps it ran, then
2002
+ * settle (completed/failed/cancelled) or dispatch its blocking ops and suspend (continue). Shared by
2003
+ * the INLINE-advance path ({@link runRemoteExecution}, under the held lease) and the durable
2004
+ * DISPATCH path ({@link completeRemoteDecision}, which holds the lease itself). Idempotent transitions
2005
+ * — the caller has already ensured this is the decision for the currently-awaited turn.
2006
+ */
2007
+ private applyDecision;
1949
2008
  /** The run's resolved durable ops as replay inputs: completed/failed steps + elapsed timers. */
1950
2009
  private remoteHistory;
1951
2010
  /** Apply a turn's commands: persist recorded local steps, dispatch remote calls, schedule timers.
@@ -2002,6 +2061,21 @@ declare class WorkflowEngine {
2002
2061
  * A no-op if the checkpoint isn't `pending` (a duplicate or late delivery).
2003
2062
  */
2004
2063
  private completeRemoteResult;
2064
+ /**
2065
+ * Apply a remote workflow-turn {@link WorkflowDecision} durably — runs on WHATEVER instance receives
2066
+ * it over the broker (the dispatching one may be gone, or a point-to-point broker handed it to a
2067
+ * non-dispatcher). The durable, multi-instance-safe twin of {@link completeRemoteResult} for the
2068
+ * workflow TURN: look the run up by `decision.runId`, verify the decision matches the run's
2069
+ * CURRENTLY-awaited turn (`awaitingDecisionTaskId`) — so a stale / duplicate / foreign / re-driven
2070
+ * decision is dropped (applied AT MOST ONCE) — lease it, clear the marker, and apply the decision.
2071
+ */
2072
+ private completeRemoteDecision;
2073
+ /**
2074
+ * A run-scoped heartbeat for a run suspended awaiting a workflow-turn decision (the dispatch path):
2075
+ * push its durable re-drive deadline (`wakeAt`) forward so a still-working worker's turn is not
2076
+ * re-dispatched. The durable counterpart of an inline turn's in-memory `heartbeatResets` rearm.
2077
+ */
2078
+ private rearmDecisionDeadline;
2005
2079
  /**
2006
2080
  * A slot freed on `queue` (a fleet-wide signal): resume this instance's runs blocked on it so they
2007
2081
  * re-contend now instead of at their retry tick. Snapshot-and-clear — a run still blocked after the
@@ -2170,25 +2244,36 @@ declare function remoteStep<TInput, TOutput>(config: RemoteStepConfig<TInput, TO
2170
2244
 
2171
2245
  /**
2172
2246
  * A {@link WorkflowExecutor} backed by a {@link Transport}: it advances a remote workflow by
2173
- * dispatching a {@link WorkflowTask} over the broker and awaiting the matching {@link WorkflowDecision}
2174
- * (correlated by `taskId`). Pass one to `engine.registerRemote(name, version, { group, executor })` so
2175
- * a workflow authored in another SDK (e.g. the Python `durable-worker`) is driven over Redis/BullMQ.
2247
+ * DISPATCHING a {@link WorkflowTask} over the broker and returning the dispatched `taskId` WITHOUT
2248
+ * awaiting the decision. Pass one to `engine.registerRemote(name, version, { group, executor })` so a
2249
+ * workflow authored in another SDK (e.g. the Python `durable-worker`) is driven over Redis/BullMQ.
2250
+ *
2251
+ * Multi-instance safe by construction: the executor holds NO in-memory state correlating a dispatched
2252
+ * turn to its decision. The engine suspends the run recording the dispatched `taskId`, and the
2253
+ * worker's {@link import('./interfaces').WorkflowDecision} is delivered over the transport's
2254
+ * `onDecision` and applied DURABLY (by run id) on whatever engine instance consumes it — even if that
2255
+ * is not the instance that dispatched the turn. A point-to-point broker can therefore hand the
2256
+ * decision to any instance without it being dropped (the bug this design fixes).
2176
2257
  *
2177
- * Recovery-safe: if the engine crashes awaiting a decision, the re-drive dispatches a fresh task with
2178
- * the same history — the worker's replay is deterministic, so it returns the same decision; a late
2179
- * decision for the old `taskId` simply finds no waiter and is dropped.
2258
+ * Recovery-safe: if the engine crashes (or a decision is genuinely lost because the worker died), the
2259
+ * suspended turn's heartbeat-rearmed window (`remoteAdvanceSilenceMs`) lapses and the timer poller
2260
+ * re-dispatches a fresh task with the same history — the worker's replay is deterministic, so it
2261
+ * returns the same decision; a late decision for the old `taskId` no longer matches the run's
2262
+ * currently-awaited turn and is dropped.
2180
2263
  */
2181
2264
  declare class RemoteWorkflowExecutor implements WorkflowExecutor {
2182
2265
  private readonly transport;
2183
2266
  private readonly group;
2184
- private readonly opts;
2185
- private readonly pending;
2186
- private subscribed;
2187
- constructor(transport: Transport, group: string, opts?: {
2267
+ constructor(transport: Transport, group: string, _opts?: {
2188
2268
  timeoutMs?: number;
2189
2269
  });
2190
- private ensureSubscribed;
2191
- advance(run: WorkflowRun, history: HistoryEvent[]): Promise<WorkflowDecision>;
2270
+ /**
2271
+ * Dispatch one workflow turn and return its `taskId`. Does NOT await the decision — the engine
2272
+ * suspends the run on the returned `taskId` and applies the decision durably via `onDecision`.
2273
+ */
2274
+ dispatch(run: WorkflowRun, history: HistoryEvent[], pendingSignals?: WorkflowTask['pendingSignals']): Promise<{
2275
+ taskId: string;
2276
+ }>;
2192
2277
  }
2193
2278
 
2194
2279
  /**
package/dist/index.d.ts CHANGED
@@ -260,6 +260,16 @@ interface WorkflowRun {
260
260
  * durable state required for correctness — a transport without priority support ignores it.
261
261
  */
262
262
  priority?: number | undefined;
263
+ /**
264
+ * REMOTE workflow turn awaiting a decision: the `taskId` of the turn the engine dispatched to a
265
+ * workflow worker and then SUSPENDED on. Set by the dispatch-and-suspend path; matched by
266
+ * {@link WorkflowEngine.completeRemoteDecision} so ONLY the decision for the currently-awaited turn
267
+ * is applied (a stale/duplicate/foreign decision is dropped), then cleared the moment it is applied.
268
+ * This is what makes a workflow-turn decision multi-instance safe: any engine instance that receives
269
+ * the decision looks the run up by `decision.runId` and applies it durably, instead of resolving an
270
+ * in-memory promise that only the dispatching instance held. Absent on non-remote / not-awaiting runs.
271
+ */
272
+ awaitingDecisionTaskId?: string | undefined;
263
273
  createdAt: Date;
264
274
  updatedAt: Date;
265
275
  }
@@ -784,7 +794,25 @@ interface WorkflowStepEvent {
784
794
  * concerns, identical for in-process and remote workflows.
785
795
  */
786
796
  interface WorkflowExecutor {
787
- advance(run: WorkflowRun, history: HistoryEvent[], pendingSignals?: WorkflowTask['pendingSignals']): Promise<WorkflowDecision>;
797
+ /**
798
+ * INLINE replay: advance the turn and RETURN its decision (the in-process executor runs the
799
+ * registered TS function synchronously). The engine awaits this under the run's lease. Provide this
800
+ * OR {@link dispatch} — exactly one. An executor with only `advance` is single-instance by nature
801
+ * (the decision is produced in-process, not delivered over a broker).
802
+ */
803
+ advance?(run: WorkflowRun, history: HistoryEvent[], pendingSignals?: WorkflowTask['pendingSignals']): Promise<WorkflowDecision>;
804
+ /**
805
+ * DISPATCH-AND-SUSPEND (broker-backed, multi-instance safe): dispatch the turn to a worker and
806
+ * return the dispatched `taskId` WITHOUT awaiting the decision. The engine then suspends the run,
807
+ * recording the `taskId` on it; the worker's {@link WorkflowDecision} comes back over the
808
+ * transport's {@link Transport.onDecision} and is applied DURABLY by whatever engine instance
809
+ * receives it ({@link WorkflowEngine.completeRemoteDecision} looks the run up by `decision.runId`).
810
+ * No in-memory await, so the decision is never lost to a point-to-point broker handing it to a
811
+ * non-dispatching instance. Provide this OR {@link advance} — exactly one.
812
+ */
813
+ dispatch?(run: WorkflowRun, history: HistoryEvent[], pendingSignals?: WorkflowTask['pendingSignals']): Promise<{
814
+ taskId: string;
815
+ }>;
788
816
  }
789
817
  /**
790
818
  * A transport in an ordered pool, identified by `id`. The engine dispatches on the first by default
@@ -1636,6 +1664,29 @@ declare class WorkflowEngine {
1636
1664
  executionTimeout?: string | number;
1637
1665
  validateInput?: (input: unknown) => void | Promise<void>;
1638
1666
  }): void;
1667
+ /**
1668
+ * The convenience form of {@link registerRemote}: register a workflow whose body runs on a worker
1669
+ * `group` (e.g. the Python `durable-worker`, or a separate TS worker), without hand-building a
1670
+ * {@link RemoteWorkflowExecutor}. It wires the standard broker executor — a `RemoteWorkflowExecutor`
1671
+ * over the engine's own transport, dispatching to `opts.group` — for you, so you write
1672
+ * `engine.remote('processing', { group: 'processing' })` instead of
1673
+ * `engine.registerRemote('processing', '1', { group: 'processing', executor: new RemoteWorkflowExecutor(transport, 'processing') })`.
1674
+ *
1675
+ * Defaults the version to `'1'`. All the other {@link registerRemote} options
1676
+ * (`tags` / `singleton` / `executionTimeout` / `validateInput`) pass straight through. Reach for the
1677
+ * low-level {@link registerRemote} when you need a custom {@link WorkflowExecutor} (a different
1678
+ * transport, a fixed `timeoutMs`, a polyglot stub) — `remote()` is the standard wiring on the rails.
1679
+ */
1680
+ remote(name: string, opts: {
1681
+ /** Worker group this workflow's turns are dispatched to (also the executor's target group). */
1682
+ group: string;
1683
+ /** Workflow version. Defaults to `'1'`. */
1684
+ version?: string;
1685
+ tags?: string[];
1686
+ singleton?: SingletonConfig;
1687
+ executionTimeout?: string | number;
1688
+ validateInput?: (input: unknown) => void | Promise<void>;
1689
+ }): void;
1639
1690
  /**
1640
1691
  * Register a flow-control queue referenced by `ctx.call(step, input, { queue })`. Caps concurrent
1641
1692
  * in-flight steps and/or the admission rate; blocked calls re-suspend and retry, so the limit is
@@ -1946,6 +1997,14 @@ declare class WorkflowEngine {
1946
1997
  */
1947
1998
  private settleRun;
1948
1999
  private runRemoteExecution;
2000
+ /**
2001
+ * Apply a remote workflow turn's {@link WorkflowDecision}: persist the local steps it ran, then
2002
+ * settle (completed/failed/cancelled) or dispatch its blocking ops and suspend (continue). Shared by
2003
+ * the INLINE-advance path ({@link runRemoteExecution}, under the held lease) and the durable
2004
+ * DISPATCH path ({@link completeRemoteDecision}, which holds the lease itself). Idempotent transitions
2005
+ * — the caller has already ensured this is the decision for the currently-awaited turn.
2006
+ */
2007
+ private applyDecision;
1949
2008
  /** The run's resolved durable ops as replay inputs: completed/failed steps + elapsed timers. */
1950
2009
  private remoteHistory;
1951
2010
  /** Apply a turn's commands: persist recorded local steps, dispatch remote calls, schedule timers.
@@ -2002,6 +2061,21 @@ declare class WorkflowEngine {
2002
2061
  * A no-op if the checkpoint isn't `pending` (a duplicate or late delivery).
2003
2062
  */
2004
2063
  private completeRemoteResult;
2064
+ /**
2065
+ * Apply a remote workflow-turn {@link WorkflowDecision} durably — runs on WHATEVER instance receives
2066
+ * it over the broker (the dispatching one may be gone, or a point-to-point broker handed it to a
2067
+ * non-dispatcher). The durable, multi-instance-safe twin of {@link completeRemoteResult} for the
2068
+ * workflow TURN: look the run up by `decision.runId`, verify the decision matches the run's
2069
+ * CURRENTLY-awaited turn (`awaitingDecisionTaskId`) — so a stale / duplicate / foreign / re-driven
2070
+ * decision is dropped (applied AT MOST ONCE) — lease it, clear the marker, and apply the decision.
2071
+ */
2072
+ private completeRemoteDecision;
2073
+ /**
2074
+ * A run-scoped heartbeat for a run suspended awaiting a workflow-turn decision (the dispatch path):
2075
+ * push its durable re-drive deadline (`wakeAt`) forward so a still-working worker's turn is not
2076
+ * re-dispatched. The durable counterpart of an inline turn's in-memory `heartbeatResets` rearm.
2077
+ */
2078
+ private rearmDecisionDeadline;
2005
2079
  /**
2006
2080
  * A slot freed on `queue` (a fleet-wide signal): resume this instance's runs blocked on it so they
2007
2081
  * re-contend now instead of at their retry tick. Snapshot-and-clear — a run still blocked after the
@@ -2170,25 +2244,36 @@ declare function remoteStep<TInput, TOutput>(config: RemoteStepConfig<TInput, TO
2170
2244
 
2171
2245
  /**
2172
2246
  * A {@link WorkflowExecutor} backed by a {@link Transport}: it advances a remote workflow by
2173
- * dispatching a {@link WorkflowTask} over the broker and awaiting the matching {@link WorkflowDecision}
2174
- * (correlated by `taskId`). Pass one to `engine.registerRemote(name, version, { group, executor })` so
2175
- * a workflow authored in another SDK (e.g. the Python `durable-worker`) is driven over Redis/BullMQ.
2247
+ * DISPATCHING a {@link WorkflowTask} over the broker and returning the dispatched `taskId` WITHOUT
2248
+ * awaiting the decision. Pass one to `engine.registerRemote(name, version, { group, executor })` so a
2249
+ * workflow authored in another SDK (e.g. the Python `durable-worker`) is driven over Redis/BullMQ.
2250
+ *
2251
+ * Multi-instance safe by construction: the executor holds NO in-memory state correlating a dispatched
2252
+ * turn to its decision. The engine suspends the run recording the dispatched `taskId`, and the
2253
+ * worker's {@link import('./interfaces').WorkflowDecision} is delivered over the transport's
2254
+ * `onDecision` and applied DURABLY (by run id) on whatever engine instance consumes it — even if that
2255
+ * is not the instance that dispatched the turn. A point-to-point broker can therefore hand the
2256
+ * decision to any instance without it being dropped (the bug this design fixes).
2176
2257
  *
2177
- * Recovery-safe: if the engine crashes awaiting a decision, the re-drive dispatches a fresh task with
2178
- * the same history — the worker's replay is deterministic, so it returns the same decision; a late
2179
- * decision for the old `taskId` simply finds no waiter and is dropped.
2258
+ * Recovery-safe: if the engine crashes (or a decision is genuinely lost because the worker died), the
2259
+ * suspended turn's heartbeat-rearmed window (`remoteAdvanceSilenceMs`) lapses and the timer poller
2260
+ * re-dispatches a fresh task with the same history — the worker's replay is deterministic, so it
2261
+ * returns the same decision; a late decision for the old `taskId` no longer matches the run's
2262
+ * currently-awaited turn and is dropped.
2180
2263
  */
2181
2264
  declare class RemoteWorkflowExecutor implements WorkflowExecutor {
2182
2265
  private readonly transport;
2183
2266
  private readonly group;
2184
- private readonly opts;
2185
- private readonly pending;
2186
- private subscribed;
2187
- constructor(transport: Transport, group: string, opts?: {
2267
+ constructor(transport: Transport, group: string, _opts?: {
2188
2268
  timeoutMs?: number;
2189
2269
  });
2190
- private ensureSubscribed;
2191
- advance(run: WorkflowRun, history: HistoryEvent[]): Promise<WorkflowDecision>;
2270
+ /**
2271
+ * Dispatch one workflow turn and return its `taskId`. Does NOT await the decision — the engine
2272
+ * suspends the run on the returned `taskId` and applies the decision durably via `onDecision`.
2273
+ */
2274
+ dispatch(run: WorkflowRun, history: HistoryEvent[], pendingSignals?: WorkflowTask['pendingSignals']): Promise<{
2275
+ taskId: string;
2276
+ }>;
2192
2277
  }
2193
2278
 
2194
2279
  /**