@dudousxd/nestjs-durable-core 0.93.0 → 0.95.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.cjs CHANGED
@@ -82,6 +82,7 @@ __export(index_exports, {
82
82
  RemoteStepTimeout: () => RemoteStepTimeout,
83
83
  RemoteWorkflowExecutor: () => RemoteWorkflowExecutor,
84
84
  RemoteWorkflowTimeout: () => RemoteWorkflowTimeout,
85
+ RunAppStamper: () => RunAppStamper,
85
86
  RunGateway: () => RunGateway,
86
87
  SINGLETON_ACTIVE_STATUSES: () => SINGLETON_ACTIVE_STATUSES,
87
88
  SINGLETON_ADMITTED_TAG: () => SINGLETON_ADMITTED_TAG,
@@ -138,6 +139,7 @@ __export(index_exports, {
138
139
  codeOf: () => codeOf,
139
140
  collectMetrics: () => collectMetrics,
140
141
  concurrencyTag: () => concurrencyTag,
142
+ controlPlaneAttribution: () => controlPlaneAttribution,
141
143
  controlPlaneDescriptor: () => controlPlaneDescriptor,
142
144
  countRuns: () => countRuns,
143
145
  coveredTokens: () => coveredTokens,
@@ -247,6 +249,7 @@ __export(index_exports, {
247
249
  runOutsideWorkflowCtx: () => runOutsideWorkflowCtx,
248
250
  runSchedules: () => runSchedules,
249
251
  runStepHandler: () => runStepHandler,
252
+ runTraceId: () => runTraceId,
250
253
  runValueFacetsFromRuns: () => runValueFacetsFromRuns,
251
254
  runWaitPrefix: () => runWaitPrefix,
252
255
  runWaitTargetOf: () => runWaitTargetOf,
@@ -272,6 +275,7 @@ __export(index_exports, {
272
275
  summarizeFan: () => summarizeFan,
273
276
  summarizeRunFromStore: () => summarizeRunFromStore,
274
277
  summarizeTimeline: () => summarizeTimeline,
278
+ tenantAppOf: () => tenantAppOf,
275
279
  tenantGroup: () => tenantGroup,
276
280
  tenantIdsFor: () => tenantIdsFor,
277
281
  tenantVisible: () => tenantVisible,
@@ -5311,6 +5315,24 @@ var TransportPool = class {
5311
5315
  return stops;
5312
5316
  }
5313
5317
  /**
5318
+ * The live run-queue consumers ({@link Transport.liveRunConsumers}) across every transport that
5319
+ * reports them, one per instance (its latest beat): an engine consuming the run queue of two
5320
+ * pinned transports is still one process.
5321
+ */
5322
+ async liveRunConsumers(opts) {
5323
+ const byInstance = /* @__PURE__ */ new Map();
5324
+ for (const { transport } of this.transports) {
5325
+ if (!transport.liveRunConsumers) continue;
5326
+ for (const beat of await transport.liveRunConsumers(opts)) {
5327
+ const seen = byInstance.get(beat.instanceId);
5328
+ if (!seen || beat.lastBeatAt > seen.lastBeatAt) byInstance.set(beat.instanceId, beat);
5329
+ }
5330
+ }
5331
+ return [
5332
+ ...byInstance.values()
5333
+ ];
5334
+ }
5335
+ /**
5314
5336
  * The live handshake descriptors advertised for `group` (design §7.2), merged across every transport
5315
5337
  * that can report them. Returns `[]` when NO transport implements
5316
5338
  * {@link Transport.readWorkerDescriptors} (a pure in-process pool) OR when descriptors are supported
@@ -11780,13 +11802,16 @@ var WorkflowEngine = class _WorkflowEngine {
11780
11802
  /** Per-group worker health (queue backlog + live worker heartbeats). Covers {@link knownGroups}
11781
11803
  * (so a registered group with backlog and ZERO workers still reports — the alert case) UNION the
11782
11804
  * groups discovered from live heartbeats (so a local-step group like `pipeline`, not derivable
11783
- * from registrations, shows once its workers beat). Empty when no transport can introspect health
11784
- * (only the BullMQ transport implements `groupHealth`). */
11805
+ * from registrations, shows once its workers beat) UNION this engine's in-process workflows while
11806
+ * the run queue has a live consumer (see {@link inProcessWorkflowHealth}). Empty when no transport
11807
+ * can introspect health (only the BullMQ transport implements `groupHealth`). */
11785
11808
  async workerHealth(extra = []) {
11809
+ const inProcess = await this.inProcessWorkflowHealth();
11786
11810
  const groups = /* @__PURE__ */ new Set([
11787
11811
  ...this.knownGroups(extra),
11788
11812
  ...await this.pool.listWorkerGroups(),
11789
- ...await this.inflightStepGroups()
11813
+ ...await this.inflightStepGroups(),
11814
+ ...inProcess.groups
11790
11815
  ]);
11791
11816
  const workflowTokens = /* @__PURE__ */ new Set();
11792
11817
  for (const def of this.workflows.values()) {
@@ -11799,14 +11824,54 @@ var WorkflowEngine = class _WorkflowEngine {
11799
11824
  if (!health) continue;
11800
11825
  const at = group.lastIndexOf("@");
11801
11826
  const baseToken = at === -1 ? group : group.slice(0, at);
11827
+ const consumers = inProcess.groups.has(group) ? inProcess.consumers.filter((c) => !health.liveWorkers.some((w) => w.instanceId === c.instanceId)).map((c) => ({
11828
+ ...c,
11829
+ group
11830
+ })) : [];
11802
11831
  out.push({
11803
11832
  ...health,
11833
+ liveWorkers: [
11834
+ ...health.liveWorkers,
11835
+ ...consumers
11836
+ ],
11804
11837
  kind: workflowTokens.has(baseToken) ? "workflow" : "step"
11805
11838
  });
11806
11839
  }
11807
11840
  return out;
11808
11841
  }
11809
11842
  /**
11843
+ * The workflows this deployment executes IN-PROCESS — registered here with a body, so a run of one
11844
+ * is driven by whichever engine instance takes it off the run queue — and the instances consuming
11845
+ * that queue right now. Those instances ARE the workers of these workflows, yet they consume no task
11846
+ * queue and so never wrote a worker heartbeat: a control plane running its own workflows showed
11847
+ * "0 live workers" while it completed runs. Each workflow is reported under the token a worker
11848
+ * serving it would use (`tenantGroup(name, namespace)`), with the run-queue consumers as its live
11849
+ * workers. The engine's internal `__` workflows are left out.
11850
+ *
11851
+ * Nothing when no run-queue consumer is live (a transport that cannot report one, or no driving
11852
+ * instance): the workflows are not served by anything the transport can see, and claiming a worker
11853
+ * for them would hide a starved queue.
11854
+ */
11855
+ async inProcessWorkflowHealth() {
11856
+ const groups = /* @__PURE__ */ new Set();
11857
+ for (const def of this.workflows.values()) {
11858
+ if (def.remote || def.hasBody === false || def.name.startsWith("__")) continue;
11859
+ groups.add(tenantGroup(sanitizeQueueToken(def.name), this.namespace));
11860
+ }
11861
+ if (groups.size === 0) return {
11862
+ groups,
11863
+ consumers: []
11864
+ };
11865
+ const consumers = await this.pool.liveRunConsumers({
11866
+ namespace: this.namespace
11867
+ }).catch(() => []);
11868
+ if (consumers.length === 0) groups.clear();
11869
+ return {
11870
+ groups,
11871
+ consumers
11872
+ };
11873
+ }
11874
+ /**
11810
11875
  * What workflows the LIVE fleet announces it can execute — the deployment-wide registry a picker
11811
11876
  * ("call this existing workflow") needs, and the one question this engine's own registry cannot
11812
11877
  * answer. {@link workflowBody} answers only for this process, and a missing body is ambiguous: it
@@ -16407,6 +16472,144 @@ function collectMetrics(engine) {
16407
16472
  }
16408
16473
  __name(collectMetrics, "collectMetrics");
16409
16474
 
16475
+ // src/run-app.ts
16476
+ var import_node_crypto2 = require("crypto");
16477
+ function runTraceId(runId) {
16478
+ const hex = (0, import_node_crypto2.createHash)("sha256").update(`durable:run:${runId}`).digest("hex").slice(0, 32);
16479
+ return /^0+$/.test(hex) ? `${hex.slice(0, 31)}1` : hex;
16480
+ }
16481
+ __name(runTraceId, "runTraceId");
16482
+ function tenantAppOf(namespace, own = []) {
16483
+ if (namespace === void 0 || namespace === "" || namespace === "default") return void 0;
16484
+ const mine = Array.isArray(own) ? own.includes(namespace) : own.has(namespace);
16485
+ return mine ? void 0 : namespace;
16486
+ }
16487
+ __name(tenantAppOf, "tenantAppOf");
16488
+ function positive(value, fallback) {
16489
+ return value !== void 0 && Number.isFinite(value) ? Math.max(1, value) : fallback;
16490
+ }
16491
+ __name(positive, "positive");
16492
+ var FAILED = Symbol("failed");
16493
+ var RunAppStamper = class {
16494
+ static {
16495
+ __name(this, "RunAppStamper");
16496
+ }
16497
+ #lookup;
16498
+ #own;
16499
+ #max;
16500
+ #timeoutMs;
16501
+ /** runId → app (`null`: the control plane's own run). */
16502
+ #apps = /* @__PURE__ */ new Map();
16503
+ /** runId → the last queued event of that run, while one is waiting on a read. */
16504
+ #tails = /* @__PURE__ */ new Map();
16505
+ constructor(options) {
16506
+ this.#lookup = (runId) => options.namespaceOf(runId);
16507
+ this.#own = new Set(options.own ?? []);
16508
+ this.#max = positive(options.cacheSize, 1e4);
16509
+ this.#timeoutMs = positive(options.lookupTimeoutMs, 5e3);
16510
+ }
16511
+ /** Call `forward` with `event` stamped with its app (when it has one), now or once it is known. */
16512
+ stamp(event, forward) {
16513
+ const runId = event.runId;
16514
+ const tail = this.#tails.get(runId);
16515
+ if (tail === void 0) {
16516
+ const known = this.#known(event);
16517
+ if (known !== void 0) {
16518
+ this.#forward(event, known, forward);
16519
+ return;
16520
+ }
16521
+ }
16522
+ const next = (tail ?? Promise.resolve()).then(async () => {
16523
+ try {
16524
+ let app = this.#known(event);
16525
+ if (app === void 0) {
16526
+ const namespace = await this.#read(runId);
16527
+ if (namespace === FAILED) {
16528
+ app = null;
16529
+ } else {
16530
+ app = tenantAppOf(namespace, this.#own) ?? null;
16531
+ this.#remember(runId, app);
16532
+ }
16533
+ }
16534
+ this.#forward(event, app, forward);
16535
+ } catch {
16536
+ }
16537
+ });
16538
+ this.#tails.set(runId, next);
16539
+ void next.then(() => {
16540
+ if (this.#tails.get(runId) === next) this.#tails.delete(runId);
16541
+ });
16542
+ }
16543
+ #forward(event, app, forward) {
16544
+ try {
16545
+ forward(this.#with(event, app));
16546
+ } finally {
16547
+ if (event.type === "run.completed" || event.type === "run.failed") {
16548
+ this.#apps.delete(event.runId);
16549
+ }
16550
+ }
16551
+ }
16552
+ /** The run's namespace, or {@link FAILED} when the read threw or did not answer in time. */
16553
+ async #read(runId) {
16554
+ let timer;
16555
+ const timeout = new Promise((resolve) => {
16556
+ timer = setTimeout(() => resolve(FAILED), this.#timeoutMs);
16557
+ timer.unref?.();
16558
+ });
16559
+ try {
16560
+ return await Promise.race([
16561
+ Promise.resolve().then(() => this.#lookup(runId)).catch(() => FAILED),
16562
+ timeout
16563
+ ]);
16564
+ } finally {
16565
+ clearTimeout(timer);
16566
+ }
16567
+ }
16568
+ /** The app of `event`'s run when it is known without a read (`null`: no app). */
16569
+ #known(event) {
16570
+ if (event.namespace !== void 0) {
16571
+ const app = tenantAppOf(event.namespace, this.#own) ?? null;
16572
+ this.#remember(event.runId, app);
16573
+ return app;
16574
+ }
16575
+ return this.#apps.get(event.runId);
16576
+ }
16577
+ /** A tenant's event carries its app, and its partition as `namespace` (step events have none). */
16578
+ #with(event, app) {
16579
+ if (app === null) return event;
16580
+ return event.namespace === void 0 ? {
16581
+ ...event,
16582
+ namespace: app,
16583
+ app
16584
+ } : {
16585
+ ...event,
16586
+ app
16587
+ };
16588
+ }
16589
+ #remember(runId, app) {
16590
+ if (this.#apps.has(runId)) this.#apps.delete(runId);
16591
+ else if (this.#apps.size >= this.#max) {
16592
+ const oldest = this.#apps.keys().next().value;
16593
+ if (oldest !== void 0) this.#apps.delete(oldest);
16594
+ }
16595
+ this.#apps.set(runId, app);
16596
+ }
16597
+ };
16598
+ function controlPlaneAttribution(options, getRun) {
16599
+ if (options?.topology !== "control-plane") return void 0;
16600
+ const own = [
16601
+ options.namespace,
16602
+ options.partition
16603
+ ].filter((ns) => typeof ns === "string" && ns !== "");
16604
+ return {
16605
+ namespaceOf: /* @__PURE__ */ __name(async (runId) => (await getRun(runId))?.namespace, "namespaceOf"),
16606
+ ...own.length > 0 ? {
16607
+ own
16608
+ } : {}
16609
+ };
16610
+ }
16611
+ __name(controlPlaneAttribution, "controlPlaneAttribution");
16612
+
16410
16613
  // src/search-attributes.ts
16411
16614
  function compare(actual, filter) {
16412
16615
  if (filter.op === "in") return filter.values.some((v) => actual === v);
@@ -17538,6 +17741,7 @@ __name(settleResultsWithPlainCalls, "settleResultsWithPlainCalls");
17538
17741
  RemoteStepTimeout,
17539
17742
  RemoteWorkflowExecutor,
17540
17743
  RemoteWorkflowTimeout,
17744
+ RunAppStamper,
17541
17745
  RunGateway,
17542
17746
  SINGLETON_ACTIVE_STATUSES,
17543
17747
  SINGLETON_ADMITTED_TAG,
@@ -17594,6 +17798,7 @@ __name(settleResultsWithPlainCalls, "settleResultsWithPlainCalls");
17594
17798
  codeOf,
17595
17799
  collectMetrics,
17596
17800
  concurrencyTag,
17801
+ controlPlaneAttribution,
17597
17802
  controlPlaneDescriptor,
17598
17803
  countRuns,
17599
17804
  coveredTokens,
@@ -17703,6 +17908,7 @@ __name(settleResultsWithPlainCalls, "settleResultsWithPlainCalls");
17703
17908
  runOutsideWorkflowCtx,
17704
17909
  runSchedules,
17705
17910
  runStepHandler,
17911
+ runTraceId,
17706
17912
  runValueFacetsFromRuns,
17707
17913
  runWaitPrefix,
17708
17914
  runWaitTargetOf,
@@ -17728,6 +17934,7 @@ __name(settleResultsWithPlainCalls, "settleResultsWithPlainCalls");
17728
17934
  summarizeFan,
17729
17935
  summarizeRunFromStore,
17730
17936
  summarizeTimeline,
17937
+ tenantAppOf,
17731
17938
  tenantGroup,
17732
17939
  tenantIdsFor,
17733
17940
  tenantVisible,