@dudousxd/nestjs-durable-core 0.103.0 → 0.105.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.js CHANGED
@@ -679,6 +679,27 @@ function backoffDelay(attempt, options) {
679
679
  return options?.jitter ? Math.round(capped * (0.5 + Math.random() * 0.5)) : capped;
680
680
  }
681
681
  __name(backoffDelay, "backoffDelay");
682
+ async function retryLocalStep(options, attempt) {
683
+ const maxAttempts = Math.max(1, options?.retries ?? 1);
684
+ for (let n = 1; ; n += 1) {
685
+ try {
686
+ return {
687
+ ok: true,
688
+ value: await attempt(n),
689
+ attempts: n
690
+ };
691
+ } catch (error2) {
692
+ if (error2 instanceof FatalError || n >= maxAttempts) return {
693
+ ok: false,
694
+ error: error2,
695
+ attempts: n
696
+ };
697
+ const wait = backoffDelay(n, options);
698
+ if (wait > 0) await new Promise((resolve) => setTimeout(resolve, wait));
699
+ }
700
+ }
701
+ }
702
+ __name(retryLocalStep, "retryLocalStep");
682
703
 
683
704
  // src/bounded-runner.ts
684
705
  var BoundedRunner = class {
@@ -4608,6 +4629,14 @@ var LEGACY_SCAN_STATUSES = [
4608
4629
  "cancelling"
4609
4630
  ];
4610
4631
  var byAdmissionOrder = /* @__PURE__ */ __name((a, b) => a.createdAt.getTime() - b.createdAt.getTime() || a.id.localeCompare(b.id), "byAdmissionOrder");
4632
+ function singletonKeyTag(tags) {
4633
+ if (!tags) return void 0;
4634
+ for (let i = tags.length - 1; i >= 0; i -= 1) {
4635
+ if (tags[i].startsWith("singleton:")) return tags[i];
4636
+ }
4637
+ return void 0;
4638
+ }
4639
+ __name(singletonKeyTag, "singletonKeyTag");
4611
4640
  function withoutSingletonMarkers(tags) {
4612
4641
  return tags?.filter((tag) => !SINGLETON_RESERVED_TAGS.includes(tag));
4613
4642
  }
@@ -4636,7 +4665,7 @@ function singletonClaimantIds(rows, tag) {
4636
4665
  __name(singletonClaimantIds, "singletonClaimantIds");
4637
4666
  function singletonAdmissionTags(runs, runId, tag, workflow, limit) {
4638
4667
  if (!Number.isInteger(limit) || limit < 1) throw new Error("singleton limit must be a positive integer");
4639
- const active = runs.filter((run) => run.workflow === workflow && SINGLETON_ACTIVE_STATUSES.includes(run.status) && run.tags?.includes(tag));
4668
+ const active = runs.filter((run) => run.workflow === workflow && SINGLETON_ACTIVE_STATUSES.includes(run.status) && singletonKeyTag(run.tags) === tag);
4640
4669
  const candidate = active.find((run) => run.id === runId);
4641
4670
  if (!candidate) return null;
4642
4671
  if (candidate.tags?.includes(SINGLETON_ADMITTED_TAG)) return candidate.tags;
@@ -4664,7 +4693,7 @@ async function clearTerminalSingletonAdmission(runId, patch, getRun) {
4664
4693
  __name(clearTerminalSingletonAdmission, "clearTerminalSingletonAdmission");
4665
4694
  function singletonAdmissionDenialPatch(runs, runId, tag, workflow, retryWakeAt) {
4666
4695
  if (retryWakeAt === void 0) return void 0;
4667
- const candidate = runs.find((run) => run.id === runId && run.workflow === workflow && run.tags?.includes(tag) && SINGLETON_ACTIVE_STATUSES.includes(run.status));
4696
+ const candidate = runs.find((run) => run.id === runId && run.workflow === workflow && singletonKeyTag(run.tags) === tag && SINGLETON_ACTIVE_STATUSES.includes(run.status));
4668
4697
  if (!candidate || candidate.status === "cancelling") return void 0;
4669
4698
  return {
4670
4699
  status: "suspended",
@@ -4713,6 +4742,28 @@ function isLockConflict(error2) {
4713
4742
  return false;
4714
4743
  }
4715
4744
  __name(isLockConflict, "isLockConflict");
4745
+ var UnparseableStoredInput = class {
4746
+ static {
4747
+ __name(this, "UnparseableStoredInput");
4748
+ }
4749
+ error;
4750
+ constructor(error2) {
4751
+ this.error = error2;
4752
+ }
4753
+ };
4754
+ async function admissionInput(run, parseInput) {
4755
+ if (!parseInput) return run.input;
4756
+ try {
4757
+ return await parseInput(run.input);
4758
+ } catch (err) {
4759
+ return new UnparseableStoredInput(err instanceof SchemaValidationError && err.code === "invalid_input" ? err : new SchemaValidationError(`workflow "${run.workflow}" input`, [
4760
+ {
4761
+ message: err instanceof Error ? err.message : String(err)
4762
+ }
4763
+ ], "invalid_input"));
4764
+ }
4765
+ }
4766
+ __name(admissionInput, "admissionInput");
4716
4767
  var SingletonGate = class {
4717
4768
  static {
4718
4769
  __name(this, "SingletonGate");
@@ -4749,12 +4800,19 @@ var SingletonGate = class {
4749
4800
  throw new SingletonQueueFullError(workflow, cfg.key(input), cfg.maxQueueDepth);
4750
4801
  }
4751
4802
  }
4752
- /** Preserve durable holders and atomically claim one of the remaining slots. */
4753
- async admit(run, cfg) {
4803
+ /**
4804
+ * Preserve durable holders and atomically claim one of the remaining slots. With `parseInput` (the
4805
+ * workflow's input schema), the key reads the parsed input, the value `run(ctx, input)` gets; a
4806
+ * stored input that no longer parses answers {@link UnparseableStoredInput}, which the caller
4807
+ * must fail the turn with (never run it).
4808
+ */
4809
+ async admit(run, cfg, parseInput) {
4754
4810
  if (!this.deps.store.tryAdmitSingleton) {
4755
4811
  throw new Error("Singleton workflows require StateStore.tryAdmitSingleton for atomic durable admission");
4756
4812
  }
4757
- const admitted = await this.claim(run, cfg);
4813
+ const keyInput = await admissionInput(run, parseInput);
4814
+ if (keyInput instanceof UnparseableStoredInput) return keyInput;
4815
+ const admitted = await this.claim(run, cfg, this.admissionTag(run, cfg, keyInput, parseInput));
4758
4816
  if (admitted && !run.tags?.includes(SINGLETON_ADMITTED_TAG)) {
4759
4817
  run.tags = [
4760
4818
  ...(run.tags ?? []).filter((tag) => tag !== SINGLETON_TRACKED_TAG),
@@ -4764,17 +4822,43 @@ var SingletonGate = class {
4764
4822
  return admitted;
4765
4823
  }
4766
4824
  /**
4825
+ * The exact tag a turn claims under: the key of the parsed input, the tag `start` stamps. A run
4826
+ * queued by an engine that predates `parseInput` was stamped with the key of its RAW input
4827
+ * instead; when the run carries that tag and not the parsed one, it claims under it, so an
4828
+ * upgrade does not strand it. Both are computed from this run's own input, never read off a tag.
4829
+ * Only that case calls the key with the raw input (what the older engine always did), and only
4830
+ * for a run that carries a `singleton:` tag at all; a key that throws on it gets the parsed tag.
4831
+ */
4832
+ admissionTag(run, cfg, keyInput, parseInput) {
4833
+ const parsed = this.tag(cfg, keyInput);
4834
+ const stamped = singletonKeyTag(run.tags);
4835
+ if (!parseInput || stamped === void 0 || stamped === parsed) return parsed;
4836
+ let raw;
4837
+ try {
4838
+ raw = this.tag(cfg, run.input);
4839
+ } catch {
4840
+ return parsed;
4841
+ }
4842
+ return stamped === raw ? raw : parsed;
4843
+ }
4844
+ /**
4767
4845
  * One claim, retried when the database aborts it as a lock conflict. A conflict that outlasts the
4768
4846
  * retries answers "not admitted", so the engine releases this turn's lease, instead of stranding
4769
4847
  * the run until the lease lapses. It writes nothing to the run: this turn's lease may have lapsed
4770
4848
  * during the claim, and another instance may now hold the run, its slot and newer tags. A later
4771
4849
  * sweep re-drives the run: recovery once it is unleased, or its timer if it is parked.
4850
+ *
4851
+ * The claim is under the tag the key computes NOW, the one `start` stamped from the same input
4852
+ * (the schema is deterministic). The store admits a run only when it carries that exact tag, so
4853
+ * a run whose stored tags lack it (a caller tag from before callers' `singleton:` tags were
4854
+ * dropped, a run started before the workflow became a singleton) is never admitted under some
4855
+ * other key; it waits instead.
4772
4856
  */
4773
- async claim(run, cfg) {
4857
+ async claim(run, cfg, tag) {
4774
4858
  const admit = this.deps.store.tryAdmitSingleton;
4775
4859
  for (let attempt = 1; ; attempt += 1) {
4776
4860
  try {
4777
- return await admit.call(this.deps.store, run.id, this.tag(cfg, run.input), run.workflow, cfg.limit ?? 1, this.retryWakeAt());
4861
+ return await admit.call(this.deps.store, run.id, tag, run.workflow, cfg.limit ?? 1, this.retryWakeAt());
4778
4862
  } catch (error2) {
4779
4863
  if (!isLockConflict(error2)) throw error2;
4780
4864
  if (attempt >= ADMISSION_ATTEMPTS) return false;
@@ -4792,7 +4876,7 @@ var SingletonGate = class {
4792
4876
  async wakeNext(settled) {
4793
4877
  const cfg = this.deps.configFor(settled);
4794
4878
  if (!cfg) return;
4795
- const tag = settled.tags?.find((t) => t.startsWith("singleton:"));
4879
+ const tag = singletonKeyTag(settled.tags);
4796
4880
  if (!tag) return;
4797
4881
  const gated = (await this.deps.store.listRuns({
4798
4882
  tag,
@@ -5845,7 +5929,6 @@ function createWorkflowCtx(host, runId, compensations, workflow = "", searchAttr
5845
5929
  });
5846
5930
  return existing.output;
5847
5931
  }
5848
- const maxAttempts = Math.max(1, options?.retries ?? 1);
5849
5932
  const startedAt = /* @__PURE__ */ new Date();
5850
5933
  await host.startStep({
5851
5934
  runId,
@@ -5856,60 +5939,59 @@ function createWorkflowCtx(host, runId, compensations, workflow = "", searchAttr
5856
5939
  enqueuedAt: startedAt,
5857
5940
  startedAt
5858
5941
  });
5859
- for (let attempt = 1; ; attempt += 1) {
5860
- const events = [];
5861
- try {
5862
- const invocation = {
5863
- runId,
5864
- workflow,
5865
- stepName: name,
5866
- seq: current,
5867
- attempt
5868
- };
5869
- const body = /* @__PURE__ */ __name(() => {
5870
- const logger = createStepLogger(events, host.clock);
5871
- return runInStepScope(scopeOwner, name, () => runInStepLogger(logger, () => fn(logger)));
5872
- }, "body");
5873
- const output = host.interceptStep ? await host.interceptStep(invocation, body) : await body();
5874
- await host.completeStep({
5875
- runId,
5876
- seq: current,
5877
- name,
5878
- kind: "local",
5879
- output,
5880
- events,
5881
- attempts: attempt,
5882
- enqueuedAt: startedAt,
5883
- startedAt
5884
- });
5885
- if (options?.compensate) compensations.push({
5886
- name,
5887
- fn: options.compensate
5888
- });
5889
- return output;
5890
- } catch (err) {
5891
- if (err instanceof FatalError || attempt >= maxAttempts) {
5892
- await host.failStep({
5893
- runId,
5894
- seq: current,
5895
- name,
5896
- kind: "local",
5897
- error: {
5898
- message: err instanceof Error ? err.message : String(err),
5899
- stack: err instanceof Error ? err.stack : void 0,
5900
- ...codeAndDetails(err)
5901
- },
5902
- events,
5903
- attempts: attempt,
5904
- enqueuedAt: startedAt,
5905
- startedAt
5906
- });
5907
- throw err;
5908
- }
5909
- const wait = backoffDelay(attempt, options);
5910
- if (wait > 0) await new Promise((resolve) => setTimeout(resolve, wait));
5911
- }
5942
+ let events = [];
5943
+ const outcome = await retryLocalStep(options, async (attempt) => {
5944
+ const attemptEvents = [];
5945
+ events = attemptEvents;
5946
+ const invocation = {
5947
+ runId,
5948
+ workflow,
5949
+ stepName: name,
5950
+ seq: current,
5951
+ attempt
5952
+ };
5953
+ const body = /* @__PURE__ */ __name(() => {
5954
+ const logger = createStepLogger(attemptEvents, host.clock);
5955
+ return runInStepScope(scopeOwner, name, () => runInStepLogger(logger, () => fn(logger)));
5956
+ }, "body");
5957
+ const output = host.interceptStep ? await host.interceptStep(invocation, body) : await body();
5958
+ await host.completeStep({
5959
+ runId,
5960
+ seq: current,
5961
+ name,
5962
+ kind: "local",
5963
+ output,
5964
+ events: attemptEvents,
5965
+ attempts: attempt,
5966
+ enqueuedAt: startedAt,
5967
+ startedAt
5968
+ });
5969
+ return output;
5970
+ });
5971
+ if (outcome.ok) {
5972
+ if (options?.compensate) compensations.push({
5973
+ name,
5974
+ fn: options.compensate
5975
+ });
5976
+ return outcome.value;
5912
5977
  }
5978
+ const err = outcome.error;
5979
+ await host.failStep({
5980
+ runId,
5981
+ seq: current,
5982
+ name,
5983
+ kind: "local",
5984
+ error: {
5985
+ message: err instanceof Error ? err.message : String(err),
5986
+ stack: err instanceof Error ? err.stack : void 0,
5987
+ ...codeAndDetails(err)
5988
+ },
5989
+ events,
5990
+ attempts: outcome.attempts,
5991
+ enqueuedAt: startedAt,
5992
+ startedAt
5993
+ });
5994
+ throw err;
5913
5995
  }, "localStep");
5914
5996
  const transaction = /* @__PURE__ */ __name(async (name, fn) => {
5915
5997
  if (!store.transaction) {
@@ -6941,9 +7023,30 @@ function tenantScopedKey(namespace, key) {
6941
7023
  return namespace && namespace !== "default" ? `${namespace}/${key}` : key;
6942
7024
  }
6943
7025
  __name(tenantScopedKey, "tenantScopedKey");
7026
+ function failingBody(error2) {
7027
+ return async () => {
7028
+ throw error2;
7029
+ };
7030
+ }
7031
+ __name(failingBody, "failingBody");
7032
+ function storedInputError(invalid) {
7033
+ return {
7034
+ message: invalid.error.message,
7035
+ code: "invalid_input",
7036
+ retryable: false,
7037
+ details: {
7038
+ issues: invalid.error.issues
7039
+ }
7040
+ };
7041
+ }
7042
+ __name(storedInputError, "storedInputError");
7043
+ function callerTags(tags) {
7044
+ return tags?.filter((tag) => !tag.startsWith(SINGLETON_TAG_PREFIX));
7045
+ }
7046
+ __name(callerTags, "callerTags");
7047
+ var SINGLETON_TAG_PREFIX = "singleton:";
6944
7048
  function singletonKeyOf(run) {
6945
- const tag = run.tags?.find((t) => t.startsWith("singleton:"));
6946
- return tag?.slice("singleton:".length);
7049
+ return singletonKeyTag(run.tags)?.slice("singleton:".length);
6947
7050
  }
6948
7051
  __name(singletonKeyOf, "singletonKeyOf");
6949
7052
  function conventionSingleton(run, announced, wire) {
@@ -7963,6 +8066,7 @@ var WorkflowEngine = class _WorkflowEngine {
7963
8066
  concurrency: opts?.concurrency,
7964
8067
  executionTimeoutMs: opts?.executionTimeout != null ? parseDuration(opts.executionTimeout) : void 0,
7965
8068
  validateInput: opts?.validateInput,
8069
+ parseInput: opts?.parseInput,
7966
8070
  searchAttributesSchema: opts?.searchAttributesSchema,
7967
8071
  onEvent: opts?.onEvent,
7968
8072
  eventBatch: opts?.eventBatch,
@@ -9664,24 +9768,31 @@ var WorkflowEngine = class _WorkflowEngine {
9664
9768
  if (!registered) {
9665
9769
  throw new Error(pinned === void 0 ? `workflow ${name} is not registered` : `workflow ${versionKey(name, pinned)} is not registered \u2014 a pinned start never falls back to the newest version (registered: ${this.registeredVersions(name)})`);
9666
9770
  }
9667
- if (parkUnroutable && registered.validateInput) {
9771
+ const check = registered;
9772
+ const parseAndValidate = /* @__PURE__ */ __name(async () => {
9773
+ const parsed = check.parseInput ? await check.parseInput(input) : input;
9774
+ await check.validateInput?.(parsed);
9775
+ return parsed;
9776
+ }, "parseAndValidate");
9777
+ let keyInput;
9778
+ if (parkUnroutable && (registered.validateInput || registered.parseInput)) {
9668
9779
  try {
9669
- await registered.validateInput(input);
9780
+ keyInput = await parseAndValidate();
9670
9781
  } catch (err) {
9671
9782
  throw new TenantStartInvalid(err);
9672
9783
  }
9673
9784
  } else {
9674
- await registered.validateInput?.(input);
9785
+ keyInput = await parseAndValidate();
9675
9786
  }
9676
9787
  const prior = await this.store.getRun(runId);
9677
9788
  if (prior) return this.existingStartResult(prior);
9678
- const quota = await resolveConcurrency(opts?.concurrency, registered.concurrency, input);
9789
+ const quota = await resolveConcurrency(opts?.concurrency, registered.concurrency, keyInput);
9679
9790
  if (quota) await assertConcurrency(this.store, name, quota);
9680
9791
  const now = /* @__PURE__ */ new Date();
9681
9792
  const stampedTags = [
9682
- ...opts?.tags ?? [],
9793
+ ...callerTags(opts?.tags) ?? [],
9683
9794
  ...registered.singleton ? [
9684
- this.singletons.tag(registered.singleton, input)
9795
+ this.singletons.tag(registered.singleton, keyInput)
9685
9796
  ] : [],
9686
9797
  ...quota ? [
9687
9798
  concurrencyTag(quota.key)
@@ -9690,9 +9801,9 @@ var WorkflowEngine = class _WorkflowEngine {
9690
9801
  VERSION_UNDECLARED_TAG
9691
9802
  ] : []
9692
9803
  ];
9693
- const tags = mergeTags(registered.tags, stampedTags)?.filter((tag) => !SINGLETON_RESERVED_TAGS.includes(tag));
9804
+ const tags = mergeTags(callerTags(registered.tags), stampedTags)?.filter((tag) => !SINGLETON_RESERVED_TAGS.includes(tag));
9694
9805
  if (registered.singleton) {
9695
- await this.singletons.assertCapacity(name, registered.singleton, input);
9806
+ await this.singletons.assertCapacity(name, registered.singleton, keyInput);
9696
9807
  }
9697
9808
  const startAtMs = opts?.startAt instanceof Date ? opts.startAt.getTime() : opts?.startAt;
9698
9809
  const delayed = startAtMs !== void 0 && Number.isFinite(startAtMs) && startAtMs > this.clock();
@@ -9781,7 +9892,7 @@ var WorkflowEngine = class _WorkflowEngine {
9781
9892
  };
9782
9893
  const now = /* @__PURE__ */ new Date();
9783
9894
  const tags = mergeTags(void 0, [
9784
- ...opts?.tags ?? [],
9895
+ ...callerTags(opts?.tags) ?? [],
9785
9896
  ...singleton ? [
9786
9897
  this.singletons.tag(singleton, input)
9787
9898
  ] : [],
@@ -9961,7 +10072,7 @@ var WorkflowEngine = class _WorkflowEngine {
9961
10072
  namespace: poolNamespace(opts.namespace) ?? this.namespace,
9962
10073
  input,
9963
10074
  // The same derived tags an admitted or parked start carries, so quota-key queries find it.
9964
- tags: mergeTags(opts.tags, [
10075
+ tags: mergeTags(callerTags(opts.tags), [
9965
10076
  ...opts.concurrency ? [
9966
10077
  concurrencyTag(opts.concurrency.key)
9967
10078
  ] : [],
@@ -13488,8 +13599,17 @@ ${run.workflowVersion}`;
13488
13599
  ...await this.sourceTraceOf(run)
13489
13600
  });
13490
13601
  }
13491
- if (registered.singleton && !await this.singletons.admit(run, registered.singleton)) {
13492
- return this.singletonWaitResult(run);
13602
+ if (registered.singleton) {
13603
+ const admission = await this.singletons.admit(run, registered.singleton, registered.parseInput);
13604
+ if (admission instanceof UnparseableStoredInput) {
13605
+ const result = await this.settleRun(run, {
13606
+ kind: "failed",
13607
+ error: storedInputError(admission)
13608
+ });
13609
+ await this.releaseLeaseBestEffort(run.id);
13610
+ return result;
13611
+ }
13612
+ if (!admission) return this.singletonWaitResult(run);
13493
13613
  }
13494
13614
  const wfToken = tenantGroup(sanitizeQueueToken(remote.group), void 0);
13495
13615
  let wfPlan = await this.planRoute(wfToken, registered.requires);
@@ -14000,7 +14120,7 @@ ${run.workflowVersion}`;
14000
14120
  output: cmd.output,
14001
14121
  error: cmd.error,
14002
14122
  events: cmd.events,
14003
- attempts: 1,
14123
+ attempts: cmd.attempts ?? 1,
14004
14124
  enqueuedAt: startedAt,
14005
14125
  startedAt,
14006
14126
  finishedAt,
@@ -14526,7 +14646,8 @@ ${run.workflowVersion}`;
14526
14646
  attempt: void 0
14527
14647
  }, () => this.runExecutionTurn(run, fn, registered));
14528
14648
  }
14529
- async runExecutionTurn(run, fn, registered) {
14649
+ async runExecutionTurn(run, registeredFn, registered) {
14650
+ let fn = registeredFn;
14530
14651
  if (run.status === "cancelling") this.cancelRequested.add(run.id);
14531
14652
  if (run.status === "pending") {
14532
14653
  if (!await this.markStarted(run, registered?.singleton !== void 0)) {
@@ -14542,8 +14663,10 @@ ${run.workflowVersion}`;
14542
14663
  });
14543
14664
  }
14544
14665
  await this.noteServedWakes(run.id);
14545
- if (run.status !== "cancelling" && registered?.singleton && !await this.singletons.admit(run, registered.singleton)) {
14546
- return this.singletonWaitResult(run);
14666
+ if (run.status !== "cancelling" && registered?.singleton) {
14667
+ const admission = await this.singletons.admit(run, registered.singleton, registered.parseInput);
14668
+ if (admission instanceof UnparseableStoredInput) fn = failingBody(admission.error);
14669
+ else if (!admission) return this.singletonWaitResult(run);
14547
14670
  }
14548
14671
  const stickyOk = this.stickyEligible(run);
14549
14672
  let cont = stickyOk ? this.takeContinuation(run, fn) : void 0;
@@ -19027,6 +19150,7 @@ export {
19027
19150
  resolveWireTenant,
19028
19151
  retentionKeeps,
19029
19152
  retiredHandoffName,
19153
+ retryLocalStep,
19030
19154
  routeFor,
19031
19155
  rowTenant,
19032
19156
  runCompletedEvent,
@@ -19055,6 +19179,7 @@ export {
19055
19179
  singletonAdmissionDenialPatch,
19056
19180
  singletonAdmissionTags,
19057
19181
  singletonClaimantIds,
19182
+ singletonKeyTag,
19058
19183
  sqlComparator,
19059
19184
  stepConfigOf,
19060
19185
  stepId,