@dudousxd/nestjs-durable-core 0.85.9 → 0.85.11

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.cjs CHANGED
@@ -56,6 +56,7 @@ __export(index_exports, {
56
56
  NestedWorkflowCallError: () => NestedWorkflowCallError,
57
57
  NonDeterminismError: () => NonDeterminismError,
58
58
  QueueController: () => QueueController,
59
+ REMOTE_EVENT: () => REMOTE_EVENT,
59
60
  RUN_HISTORY_FIXTURE_VERSION: () => RUN_HISTORY_FIXTURE_VERSION,
60
61
  RUN_VALUE_FACET_LIMIT: () => RUN_VALUE_FACET_LIMIT,
61
62
  RUN_VALUE_FACET_SCAN: () => RUN_VALUE_FACET_SCAN,
@@ -146,11 +147,13 @@ __export(index_exports, {
146
147
  isFailedMember: () => isFailedMember,
147
148
  isLegacyDescriptor: () => isLegacyDescriptor,
148
149
  isNonEmptyRunScope: () => isNonEmptyRunScope,
150
+ isRemoteEvent: () => isRemoteEvent,
149
151
  isWorkflowControlFlowSignal: () => isWorkflowControlFlowSignal,
150
152
  legacySingletonHolders: () => legacySingletonHolders,
151
153
  listSignalWaitersOfRuns: () => listSignalWaitersOfRuns,
152
154
  liveHandoff: () => liveHandoff,
153
155
  log: () => log,
156
+ markRemoteEvent: () => markRemoteEvent,
154
157
  matchesAttributes: () => matchesAttributes,
155
158
  mergeRunFacetRows: () => mergeRunFacetRows,
156
159
  mergeRunValueFacetRows: () => mergeRunValueFacetRows,
@@ -2775,6 +2778,20 @@ function codeAndDetails(err) {
2775
2778
  }
2776
2779
  __name(codeAndDetails, "codeAndDetails");
2777
2780
 
2781
+ // src/remote-event.ts
2782
+ var REMOTE_EVENT = Symbol.for("@dudousxd/nestjs-durable:remote-event");
2783
+ function markRemoteEvent(event) {
2784
+ Object.defineProperty(event, REMOTE_EVENT, {
2785
+ value: true
2786
+ });
2787
+ return event;
2788
+ }
2789
+ __name(markRemoteEvent, "markRemoteEvent");
2790
+ function isRemoteEvent(event) {
2791
+ return event[REMOTE_EVENT] === true;
2792
+ }
2793
+ __name(isRemoteEvent, "isRemoteEvent");
2794
+
2778
2795
  // src/tenant-group.ts
2779
2796
  function tenantGroup(baseGroup, tenant) {
2780
2797
  return tenant !== void 0 && tenant !== "" && tenant !== "default" ? `${baseGroup}@${tenant}` : baseGroup;
@@ -5015,6 +5032,7 @@ var VERSION_UNDECLARED_TAG = "version:undeclared";
5015
5032
  var versionKey = /* @__PURE__ */ __name((name, version) => `${name}@${version}`, "versionKey");
5016
5033
  var STEP_FAN_CHUNK = 200;
5017
5034
  var FAN_REREAD_CHUNK = 1e3;
5035
+ var STEP_FAN_SEND_ROUND = 2e3;
5018
5036
  var PARENT_NOTIFY_ATTEMPTS = 4;
5019
5037
  var PARENT_NOTIFY_BACKOFF_MS = 250;
5020
5038
  var LEASE_RECHECK_MAX_MS = 2e3;
@@ -5479,10 +5497,10 @@ var WorkflowEngine = class {
5479
5497
  this.controlPlane?.onControl((msg) => {
5480
5498
  if (msg.from === this.instanceId) return;
5481
5499
  if (msg.kind === "event") {
5482
- this.deliver({
5500
+ this.deliver(markRemoteEvent({
5483
5501
  ...msg.event,
5484
5502
  at: new Date(msg.event.at)
5485
- });
5503
+ }));
5486
5504
  } else if (msg.kind === "cancel") {
5487
5505
  this.notifyCancelled(msg.runId);
5488
5506
  } else if (msg.kind === "enqueued") {
@@ -7487,7 +7505,7 @@ var WorkflowEngine = class {
7487
7505
  async redriveSettledStepFans(runId, fans) {
7488
7506
  for (const fan of fans) {
7489
7507
  if (this.stepFansFiredHere.has(`${runId}:${fan.from}`)) continue;
7490
- if (await this.stepFanPending(runId, fan.from, fan.to) > 0) continue;
7508
+ if (await this.stepFanHasPending(runId, fan.from, fan.to)) continue;
7491
7509
  const nowMs = this.clock();
7492
7510
  if (!await this.store.tryLockRun(runId, this.instanceId, nowMs + this.leaseMs, nowMs)) {
7493
7511
  return;
@@ -10425,10 +10443,25 @@ ${run.workflowVersion}`;
10425
10443
  }
10426
10444
  return bySeq;
10427
10445
  }
10446
+ /** Write a round of a step fan's checkpoints: one call where the store batches, else chunked. */
10447
+ async saveFanCheckpoints(checkpoints) {
10448
+ if (this.store.saveCheckpoints) {
10449
+ await this.store.saveCheckpoints(checkpoints);
10450
+ return;
10451
+ }
10452
+ for (let at = 0; at < checkpoints.length; at += STEP_FAN_CHUNK) {
10453
+ await Promise.all(checkpoints.slice(at, at + STEP_FAN_CHUNK).map((cp) => this.store.saveCheckpoint(cp)));
10454
+ }
10455
+ }
10428
10456
  /**
10429
- * Write the `pending` checkpoints of a step fan's new steps, then dispatch them: one
10430
- * {@link StateStore.saveCheckpoints} call and one {@link TransportPool.dispatchMany} for the
10431
- * batch, where the store and transport have them.
10457
+ * Write the `pending` checkpoints of a step fan's new steps and dispatch them, in rounds of
10458
+ * {@link STEP_FAN_SEND_ROUND}: a round's steps are dispatched once its checkpoints commit, while the
10459
+ * next round's are written. One {@link StateStore.saveCheckpoints} call and one
10460
+ * {@link TransportPool.dispatchMany} per round, where the store and transport have them.
10461
+ *
10462
+ * No result can wake the run before every round is written: the run is `running` under this
10463
+ * turn's lease until the turn suspends. A send cut short leaves the unwritten steps without a
10464
+ * checkpoint, and the replay sends them once the written ones settle.
10432
10465
  */
10433
10466
  async sendStepFan(runId, positions, step, inputs, batch, group, opts, routing) {
10434
10467
  const workerGroup = stepGroup(step, routing?.namespace);
@@ -10456,27 +10489,33 @@ ${run.workflowVersion}`;
10456
10489
  } : {}
10457
10490
  };
10458
10491
  });
10459
- if (this.store.saveCheckpoints) {
10460
- await this.store.saveCheckpoints(checkpoints);
10461
- } else {
10462
- for (let at = 0; at < checkpoints.length; at += STEP_FAN_CHUNK) {
10463
- await Promise.all(checkpoints.slice(at, at + STEP_FAN_CHUNK).map((cp) => this.store.saveCheckpoint(cp)));
10464
- }
10465
- }
10466
10492
  const traceparent = this.traceparent?.();
10467
10493
  const context = this.context?.();
10468
- await this.pool.dispatchMany(checkpoints.map((cp, k) => ({
10469
- runId,
10470
- seq: cp.seq,
10471
- name: step.name,
10472
- stepId: cp.stepId,
10473
- group: workerGroup,
10474
- input: inputs[batch[k]],
10475
- traceparent,
10476
- context,
10477
- priority: opts.admission?.priority,
10478
- attempt: 1
10479
- })), opts.transport);
10494
+ const dispatches = [];
10495
+ try {
10496
+ for (let from = 0; from < checkpoints.length; from += STEP_FAN_SEND_ROUND) {
10497
+ const round = checkpoints.slice(from, from + STEP_FAN_SEND_ROUND);
10498
+ await this.saveFanCheckpoints(round);
10499
+ const dispatch = this.pool.dispatchMany(round.map((cp, k) => ({
10500
+ runId,
10501
+ seq: cp.seq,
10502
+ name: step.name,
10503
+ stepId: cp.stepId,
10504
+ group: workerGroup,
10505
+ input: inputs[batch[from + k]],
10506
+ traceparent,
10507
+ context,
10508
+ priority: opts.admission?.priority,
10509
+ attempt: 1
10510
+ })), opts.transport);
10511
+ dispatch.catch(() => {
10512
+ });
10513
+ dispatches.push(dispatch);
10514
+ }
10515
+ } finally {
10516
+ await Promise.allSettled(dispatches);
10517
+ }
10518
+ await Promise.all(dispatches);
10480
10519
  for (const cp of checkpoints) {
10481
10520
  this.emit({
10482
10521
  type: "step.started",
@@ -10835,21 +10874,26 @@ ${run.workflowVersion}`;
10835
10874
  let closed = barriers.get(fan.firstSeq);
10836
10875
  if (!closed) {
10837
10876
  const lastSeq = fan.firstSeq + fan.size - 1;
10838
- closed = queuedIn(fan.firstSeq, lastSeq) ? Promise.resolve(true) : this.stepFanPending(result.runId, fan.firstSeq, lastSeq).then((pending) => pending > 0, () => false);
10877
+ closed = queuedIn(fan.firstSeq, lastSeq) ? Promise.resolve(true) : this.stepFanHasPending(result.runId, fan.firstSeq, lastSeq).catch(() => false);
10839
10878
  barriers.set(fan.firstSeq, closed);
10840
10879
  }
10841
10880
  if (await closed) return true;
10842
10881
  this.noteRecent(this.stepFansFiredHere, `${result.runId}:${fan.firstSeq}`);
10843
10882
  return false;
10844
10883
  }
10845
- /** How many steps of a step fan's seq range are still `pending`. */
10846
- async stepFanPending(runId, from, to) {
10847
- if (this.store.countCheckpoints) {
10848
- return this.store.countCheckpoints(runId, from, to, [
10884
+ /** Whether any step of a step fan's seq range is still `pending`. */
10885
+ async stepFanHasPending(runId, from, to) {
10886
+ if (this.store.hasCheckpoints) {
10887
+ return this.store.hasCheckpoints(runId, from, to, [
10849
10888
  "pending"
10850
10889
  ]);
10851
10890
  }
10852
- return (await this.store.listCheckpoints(runId)).filter((c) => c.seq >= from && c.seq <= to && c.status === "pending").length;
10891
+ if (this.store.countCheckpoints) {
10892
+ return await this.store.countCheckpoints(runId, from, to, [
10893
+ "pending"
10894
+ ]) > 0;
10895
+ }
10896
+ return (await this.store.listCheckpoints(runId)).some((c) => c.seq >= from && c.seq <= to && c.status === "pending");
10853
10897
  }
10854
10898
  /**
10855
10899
  * Apply a remote workflow-turn {@link WorkflowDecision} durably — runs on WHATEVER instance receives
@@ -11681,6 +11725,8 @@ var CodecStateStore = class {
11681
11725
  getCheckpointsForSettle;
11682
11726
  /** Decodes each checkpoint read. Bound only when the inner store has it. */
11683
11727
  getCheckpoints;
11728
+ /** Forwarded verbatim — reads no payload. Bound only when the inner store has it. */
11729
+ hasCheckpoints;
11684
11730
  /** Forwarded verbatim — a count reads no payload. Bound only when the inner store has it. */
11685
11731
  countCheckpoints;
11686
11732
  /** Forwarded verbatim — retention selects by status/updatedAt/scope, never by payload. Bound only
@@ -11741,6 +11787,8 @@ var CodecStateStore = class {
11741
11787
  if (getForSettle) this.getCheckpointsForSettle = async (runId, seqs) => (await getForSettle.call(inner, runId, seqs)).map((cp) => this.decCp(cp));
11742
11788
  const getMany = inner.getCheckpoints;
11743
11789
  if (getMany) this.getCheckpoints = async (runId, seqs) => (await getMany.call(inner, runId, seqs)).map((cp) => this.decCp(cp));
11790
+ const hasCps = inner.hasCheckpoints;
11791
+ if (hasCps) this.hasCheckpoints = (runId, from, to, statuses) => hasCps.call(inner, runId, from, to, statuses);
11744
11792
  const countCps = inner.countCheckpoints;
11745
11793
  if (countCps) this.countCheckpoints = (runId, from, to, statuses) => countCps.call(inner, runId, from, to, statuses);
11746
11794
  const stats = inner.checkpointStats;
@@ -12034,6 +12082,7 @@ function collectMetrics(engine) {
12034
12082
  return entry;
12035
12083
  }, "wf");
12036
12084
  const stop = engine.subscribe((e) => {
12085
+ if (isRemoteEvent(e)) return;
12037
12086
  switch (e.type) {
12038
12087
  case "run.started":
12039
12088
  m.runs.started += 1;
@@ -12808,6 +12857,9 @@ var InMemoryStateStore = class {
12808
12857
  ...latest
12809
12858
  } : void 0;
12810
12859
  }
12860
+ async hasCheckpoints(runId, fromSeq, toSeq, statuses) {
12861
+ return await this.countCheckpoints(runId, fromSeq, toSeq, statuses) > 0;
12862
+ }
12811
12863
  async countCheckpoints(runId, fromSeq, toSeq, statuses) {
12812
12864
  let n = 0;
12813
12865
  for (const cp of this.checkpoints.values()) {
@@ -13001,6 +13053,7 @@ var InMemoryRunQueue = class {
13001
13053
  NestedWorkflowCallError,
13002
13054
  NonDeterminismError,
13003
13055
  QueueController,
13056
+ REMOTE_EVENT,
13004
13057
  RUN_HISTORY_FIXTURE_VERSION,
13005
13058
  RUN_VALUE_FACET_LIMIT,
13006
13059
  RUN_VALUE_FACET_SCAN,
@@ -13091,11 +13144,13 @@ var InMemoryRunQueue = class {
13091
13144
  isFailedMember,
13092
13145
  isLegacyDescriptor,
13093
13146
  isNonEmptyRunScope,
13147
+ isRemoteEvent,
13094
13148
  isWorkflowControlFlowSignal,
13095
13149
  legacySingletonHolders,
13096
13150
  listSignalWaitersOfRuns,
13097
13151
  liveHandoff,
13098
13152
  log,
13153
+ markRemoteEvent,
13099
13154
  matchesAttributes,
13100
13155
  mergeRunFacetRows,
13101
13156
  mergeRunValueFacetRows,