@dudousxd/nestjs-durable-core 0.87.0 → 0.89.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
@@ -23,6 +23,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
23
23
  var index_exports = {};
24
24
  __export(index_exports, {
25
25
  ACCUMULATOR_SEEN_MAX: () => ACCUMULATOR_SEEN_MAX,
26
+ ANY_WAIT_GROUP: () => ANY_WAIT_GROUP,
26
27
  CHILD_EDGE_PREFIXES: () => CHILD_EDGE_PREFIXES,
27
28
  CONTROL_FLOW_SIGNAL: () => CONTROL_FLOW_SIGNAL,
28
29
  CORE_ORIGIN: () => CORE_ORIGIN,
@@ -69,6 +70,7 @@ __export(index_exports, {
69
70
  LIFECYCLE_TRIGGER_FAILED_TAG_PREFIX: () => LIFECYCLE_TRIGGER_FAILED_TAG_PREFIX,
70
71
  NestedWorkflowCallError: () => NestedWorkflowCallError,
71
72
  NonDeterminismError: () => NonDeterminismError,
73
+ PARENT_CLOSE_TAG_PREFIX: () => PARENT_CLOSE_TAG_PREFIX,
72
74
  QueueController: () => QueueController,
73
75
  REMOTE_EVENT: () => REMOTE_EVENT,
74
76
  RUN_HISTORY_FIXTURE_VERSION: () => RUN_HISTORY_FIXTURE_VERSION,
@@ -108,6 +110,7 @@ __export(index_exports, {
108
110
  aggregateAnnouncements: () => aggregateAnnouncements,
109
111
  aggregatesCheckpoints: () => aggregatesCheckpoints,
110
112
  announcementsOf: () => announcementsOf,
113
+ anyCheckpointName: () => anyCheckpointName,
111
114
  applyAttributeQuery: () => applyAttributeQuery,
112
115
  assertConcurrency: () => assertConcurrency,
113
116
  assertRoutes: () => assertRoutes,
@@ -164,6 +167,8 @@ __export(index_exports, {
164
167
  hmacTenantVerifier: () => hmacTenantVerifier,
165
168
  indexWaitersByRun: () => indexWaitersByRun,
166
169
  info: () => info,
170
+ isAnyCheckpoint: () => isAnyCheckpoint,
171
+ isAnyMember: () => isAnyMember,
167
172
  isEngineMintedTag: () => isEngineMintedTag,
168
173
  isEventToken: () => isEventToken,
169
174
  isFailedMember: () => isFailedMember,
@@ -193,6 +198,7 @@ __export(index_exports, {
193
198
  missingCapabilities: () => missingCapabilities,
194
199
  negotiate: () => negotiate,
195
200
  nextCronFireMs: () => nextCronFireMs,
201
+ nextRRuleFireMs: () => nextRRuleFireMs,
196
202
  normalizeAttributeRows: () => normalizeAttributeRows,
197
203
  normalizeDescriptor: () => normalizeDescriptor,
198
204
  observedAnnouncements: () => observedAnnouncements,
@@ -203,11 +209,13 @@ __export(index_exports, {
203
209
  parentRunIdOf: () => parentRunIdOf,
204
210
  parseDuration: () => parseDuration,
205
211
  parseLifecycleEvent: () => parseLifecycleEvent,
212
+ parseRRule: () => parseRRule,
206
213
  parseWebhookToken: () => parseWebhookToken,
207
214
  partitionOfToken: () => partitionOfToken,
208
215
  planDispatch: () => planDispatch,
209
216
  poolNamespace: () => poolNamespace,
210
217
  prevCronFireMs: () => prevCronFireMs,
218
+ prevRRuleFireMs: () => prevRRuleFireMs,
211
219
  queryCheckpointsOf: () => queryCheckpointsOf,
212
220
  readRunsByIds: () => readRunsByIds,
213
221
  readSearchAttributes: () => readSearchAttributes,
@@ -237,6 +245,7 @@ __export(index_exports, {
237
245
  sanitizeQueueToken: () => sanitizeQueueToken,
238
246
  scheduleRunIdAt: () => scheduleRunIdAt,
239
247
  scheduledRunId: () => scheduledRunId,
248
+ settleResultsWithPlainCalls: () => settleResultsWithPlainCalls,
240
249
  signTenantToken: () => signTenantToken,
241
250
  singletonAdmissionDenialPatch: () => singletonAdmissionDenialPatch,
242
251
  singletonAdmissionTags: () => singletonAdmissionTags,
@@ -691,7 +700,10 @@ var WorkflowSuspended = class extends Error {
691
700
  /** Marks this as a control-flow signal — see {@link isWorkflowControlFlowSignal}. */
692
701
  [CONTROL_FLOW_SIGNAL] = true;
693
702
  constructor(wakeAt, fan) {
703
+ const limit = Error.stackTraceLimit;
704
+ Error.stackTraceLimit = 0;
694
705
  super("workflow suspended");
706
+ Error.stackTraceLimit = limit;
695
707
  this.name = "WorkflowSuspended";
696
708
  this.wakeAt = wakeAt;
697
709
  this.fan = fan;
@@ -1035,6 +1047,7 @@ function isNonEmptyRunScope(scope) {
1035
1047
  return scope.workflow !== void 0 || scope.workflows !== void 0 || scope.tag !== void 0 || scope.tags !== void 0 || scope.namespace !== void 0 || scope.namespaces !== void 0 || scope.attributes !== void 0 && scope.attributes.length > 0;
1036
1048
  }
1037
1049
  __name(isNonEmptyRunScope, "isNonEmptyRunScope");
1050
+ var PARENT_CLOSE_TAG_PREFIX = "parent-close:";
1038
1051
 
1039
1052
  // src/events.ts
1040
1053
  var enc = /* @__PURE__ */ __name((s) => Buffer.from(s, "utf8").toString("base64"), "enc");
@@ -2941,9 +2954,9 @@ __name(negotiate, "negotiate");
2941
2954
 
2942
2955
  // src/handshake/routing.ts
2943
2956
  function requiredCapabilities(handler) {
2944
- const list = Array.isArray(handler) ? handler : handler.requires ?? [];
2957
+ const list2 = Array.isArray(handler) ? handler : handler.requires ?? [];
2945
2958
  return [
2946
- ...new Set(list)
2959
+ ...new Set(list2)
2947
2960
  ];
2948
2961
  }
2949
2962
  __name(requiredCapabilities, "requiredCapabilities");
@@ -3262,6 +3275,358 @@ var RunDispatchBatcher = class {
3262
3275
  }
3263
3276
  };
3264
3277
 
3278
+ // src/rrule.ts
3279
+ var FREQS = [
3280
+ "YEARLY",
3281
+ "MONTHLY",
3282
+ "WEEKLY",
3283
+ "DAILY",
3284
+ "HOURLY",
3285
+ "MINUTELY"
3286
+ ];
3287
+ var WEEKDAYS = [
3288
+ "SU",
3289
+ "MO",
3290
+ "TU",
3291
+ "WE",
3292
+ "TH",
3293
+ "FR",
3294
+ "SA"
3295
+ ];
3296
+ var SUPPORTED = /* @__PURE__ */ new Set([
3297
+ "FREQ",
3298
+ "INTERVAL",
3299
+ "COUNT",
3300
+ "UNTIL",
3301
+ "BYMONTH",
3302
+ "BYMONTHDAY",
3303
+ "BYDAY",
3304
+ "BYHOUR",
3305
+ "BYMINUTE",
3306
+ "BYSECOND",
3307
+ "WKST"
3308
+ ]);
3309
+ var DAY_MS = 864e5;
3310
+ var HORIZON_DAYS = 366 * 30;
3311
+ var formatters = /* @__PURE__ */ new Map();
3312
+ function formatter(timezone) {
3313
+ let f = formatters.get(timezone);
3314
+ if (!f) {
3315
+ f = new Intl.DateTimeFormat("en-US", {
3316
+ timeZone: timezone,
3317
+ hourCycle: "h23",
3318
+ year: "numeric",
3319
+ month: "numeric",
3320
+ day: "numeric",
3321
+ hour: "numeric",
3322
+ minute: "numeric",
3323
+ second: "numeric"
3324
+ });
3325
+ formatters.set(timezone, f);
3326
+ }
3327
+ return f;
3328
+ }
3329
+ __name(formatter, "formatter");
3330
+ function civilOf(ms, timezone) {
3331
+ const parts = {};
3332
+ for (const p of formatter(timezone).formatToParts(new Date(ms))) {
3333
+ if (p.type !== "literal") parts[p.type] = Number(p.value);
3334
+ }
3335
+ return {
3336
+ y: parts.year,
3337
+ m: parts.month,
3338
+ d: parts.day,
3339
+ h: parts.hour % 24,
3340
+ mi: parts.minute,
3341
+ s: parts.second
3342
+ };
3343
+ }
3344
+ __name(civilOf, "civilOf");
3345
+ function civilAsUtc(c) {
3346
+ return Date.UTC(c.y, c.m - 1, c.d, c.h, c.mi, c.s);
3347
+ }
3348
+ __name(civilAsUtc, "civilAsUtc");
3349
+ function toInstant(c, timezone) {
3350
+ const wall = civilAsUtc(c);
3351
+ const candidates = /* @__PURE__ */ new Set();
3352
+ for (const probe of [
3353
+ wall - DAY_MS,
3354
+ wall,
3355
+ wall + DAY_MS
3356
+ ]) {
3357
+ const offset = civilAsUtc(civilOf(probe, timezone)) - probe;
3358
+ candidates.add(wall - offset);
3359
+ }
3360
+ const exact = [
3361
+ ...candidates
3362
+ ].filter((t) => civilAsUtc(civilOf(t, timezone)) === wall);
3363
+ return exact.length > 0 ? Math.min(...exact) : null;
3364
+ }
3365
+ __name(toInstant, "toInstant");
3366
+ function dayNumber(y, m, d) {
3367
+ return Math.floor(Date.UTC(y, m - 1, d) / DAY_MS);
3368
+ }
3369
+ __name(dayNumber, "dayNumber");
3370
+ function fromDayNumber(n) {
3371
+ const date = new Date(n * DAY_MS);
3372
+ return {
3373
+ y: date.getUTCFullYear(),
3374
+ m: date.getUTCMonth() + 1,
3375
+ d: date.getUTCDate(),
3376
+ weekday: date.getUTCDay()
3377
+ };
3378
+ }
3379
+ __name(fromDayNumber, "fromDayNumber");
3380
+ function daysInMonth(y, m) {
3381
+ return new Date(Date.UTC(y, m, 0)).getUTCDate();
3382
+ }
3383
+ __name(daysInMonth, "daysInMonth");
3384
+ function list(value, part, min, max, allowNegative = false) {
3385
+ return value.split(",").map((raw) => {
3386
+ const n = Number(raw);
3387
+ const ok = Number.isInteger(n) && (allowNegative ? n !== 0 && Math.abs(n) <= max : n >= min && n <= max);
3388
+ if (!ok) throw new Error(`rrule: invalid ${part} value "${raw}"`);
3389
+ return n;
3390
+ });
3391
+ }
3392
+ __name(list, "list");
3393
+ function parseDateTime(value, timezone, part) {
3394
+ const match = /^(\d{4})(\d{2})(\d{2})(?:T(\d{2})(\d{2})(\d{2})(Z)?)?$/.exec(value);
3395
+ if (!match) throw new Error(`rrule: invalid ${part} "${value}"`);
3396
+ const [, y, m, d, h = "0", mi = "0", s = "0", z] = match;
3397
+ const civil = {
3398
+ y: Number(y),
3399
+ m: Number(m),
3400
+ d: Number(d),
3401
+ h: +h,
3402
+ mi: +mi,
3403
+ s: +s
3404
+ };
3405
+ if (z) return civilAsUtc(civil);
3406
+ const instant = toInstant(civil, timezone);
3407
+ if (instant === null) throw new Error(`rrule: ${part} "${value}" does not exist in ${timezone}`);
3408
+ return instant;
3409
+ }
3410
+ __name(parseDateTime, "parseDateTime");
3411
+ function parseRRule(text, timezone = "UTC") {
3412
+ formatter(timezone);
3413
+ let start = toInstant({
3414
+ y: 1970,
3415
+ m: 1,
3416
+ d: 1,
3417
+ h: 0,
3418
+ mi: 0,
3419
+ s: 0
3420
+ }, timezone) ?? 0;
3421
+ let ruleText;
3422
+ for (const line of text.trim().split(/\r?\n/)) {
3423
+ const l = line.trim();
3424
+ if (!l) continue;
3425
+ if (/^DTSTART[:;]/i.test(l)) {
3426
+ const value = l.slice(l.lastIndexOf(":") + 1);
3427
+ start = parseDateTime(value, timezone, "DTSTART");
3428
+ } else {
3429
+ ruleText = l.replace(/^RRULE:/i, "");
3430
+ }
3431
+ }
3432
+ if (!ruleText) throw new Error('rrule: no rule (expected e.g. "FREQ=WEEKLY;BYDAY=MO")');
3433
+ const parts = /* @__PURE__ */ new Map();
3434
+ for (const pair of ruleText.split(";")) {
3435
+ if (!pair) continue;
3436
+ const [key, value] = pair.split("=");
3437
+ const name = key?.toUpperCase() ?? "";
3438
+ if (!value) throw new Error(`rrule: "${pair}" has no value`);
3439
+ if (!SUPPORTED.has(name)) throw new Error(`rrule: ${name} is not supported`);
3440
+ parts.set(name, value.toUpperCase());
3441
+ }
3442
+ const freq = parts.get("FREQ");
3443
+ if (!freq) throw new Error("rrule: FREQ is required");
3444
+ if (!FREQS.includes(freq)) throw new Error(`rrule: FREQ=${freq} is not supported`);
3445
+ if (parts.has("COUNT") && parts.has("UNTIL")) throw new Error("rrule: COUNT and UNTIL are exclusive");
3446
+ const interval = parts.has("INTERVAL") ? list(parts.get("INTERVAL"), "INTERVAL", 1, 1e4)[0] : 1;
3447
+ const byDay = parts.get("BYDAY")?.split(",").map((raw) => {
3448
+ const match = /^([+-]?\d{1,2})?(SU|MO|TU|WE|TH|FR|SA)$/.exec(raw);
3449
+ if (!match) throw new Error(`rrule: invalid BYDAY value "${raw}"`);
3450
+ const weekday = WEEKDAYS.indexOf(match[2]);
3451
+ if (match[1] === void 0) return {
3452
+ weekday
3453
+ };
3454
+ if (freq !== "MONTHLY" && freq !== "YEARLY") {
3455
+ throw new Error(`rrule: BYDAY with an ordinal ("${raw}") needs FREQ=MONTHLY or YEARLY`);
3456
+ }
3457
+ const nth = Number(match[1]);
3458
+ if (nth === 0 || Math.abs(nth) > 53) throw new Error(`rrule: invalid BYDAY value "${raw}"`);
3459
+ return {
3460
+ weekday,
3461
+ nth
3462
+ };
3463
+ });
3464
+ const wkstText = parts.get("WKST") ?? "MO";
3465
+ const wkst = WEEKDAYS.indexOf(wkstText);
3466
+ if (wkst < 0) throw new Error(`rrule: invalid WKST "${wkstText}"`);
3467
+ const rule = {
3468
+ freq,
3469
+ interval,
3470
+ wkst,
3471
+ start,
3472
+ timezone,
3473
+ ...parts.has("COUNT") ? {
3474
+ count: list(parts.get("COUNT"), "COUNT", 1, 1e6)[0]
3475
+ } : {},
3476
+ ...parts.has("UNTIL") ? {
3477
+ until: parseDateTime(parts.get("UNTIL"), timezone, "UNTIL")
3478
+ } : {},
3479
+ ...parts.has("BYMONTH") ? {
3480
+ byMonth: list(parts.get("BYMONTH"), "BYMONTH", 1, 12)
3481
+ } : {},
3482
+ ...parts.has("BYMONTHDAY") ? {
3483
+ byMonthDay: list(parts.get("BYMONTHDAY"), "BYMONTHDAY", 1, 31, true)
3484
+ } : {},
3485
+ ...byDay ? {
3486
+ byDay
3487
+ } : {},
3488
+ ...parts.has("BYHOUR") ? {
3489
+ byHour: list(parts.get("BYHOUR"), "BYHOUR", 0, 23)
3490
+ } : {},
3491
+ ...parts.has("BYMINUTE") ? {
3492
+ byMinute: list(parts.get("BYMINUTE"), "BYMINUTE", 0, 59)
3493
+ } : {},
3494
+ ...parts.has("BYSECOND") ? {
3495
+ bySecond: list(parts.get("BYSECOND"), "BYSECOND", 0, 59)
3496
+ } : {}
3497
+ };
3498
+ return rule;
3499
+ }
3500
+ __name(parseRRule, "parseRRule");
3501
+ function dayMatches(rule, anchor, day) {
3502
+ const { y, m, d, weekday } = fromDayNumber(day);
3503
+ const anchorDay = dayNumber(anchor.y, anchor.m, anchor.d);
3504
+ if (day < anchorDay) return false;
3505
+ if (rule.freq === "YEARLY" && (y - anchor.y) % rule.interval !== 0) return false;
3506
+ if (rule.freq === "MONTHLY" && (y * 12 + m - (anchor.y * 12 + anchor.m)) % rule.interval !== 0) return false;
3507
+ if (rule.freq === "WEEKLY") {
3508
+ const weekStart = /* @__PURE__ */ __name((n) => n - (fromDayNumber(n).weekday - rule.wkst + 7) % 7, "weekStart");
3509
+ if ((weekStart(day) - weekStart(anchorDay)) / 7 % rule.interval !== 0) return false;
3510
+ }
3511
+ if (rule.freq === "DAILY" && (day - anchorDay) % rule.interval !== 0) return false;
3512
+ const byMonth = rule.byMonth ?? (rule.freq === "YEARLY" && !rule.byMonthDay && !rule.byDay ? [
3513
+ anchor.m
3514
+ ] : void 0);
3515
+ if (byMonth && !byMonth.includes(m)) return false;
3516
+ const dim = daysInMonth(y, m);
3517
+ let byMonthDay = rule.byMonthDay;
3518
+ if (!byMonthDay && !rule.byDay && (rule.freq === "MONTHLY" || rule.freq === "YEARLY")) byMonthDay = [
3519
+ anchor.d
3520
+ ];
3521
+ if (byMonthDay && !byMonthDay.some((md) => (md > 0 ? md : dim + md + 1) === d)) return false;
3522
+ let byDay = rule.byDay;
3523
+ if (!byDay && rule.freq === "WEEKLY") byDay = [
3524
+ {
3525
+ weekday: fromDayNumber(anchorDay).weekday
3526
+ }
3527
+ ];
3528
+ if (byDay) {
3529
+ const hit = byDay.some((bd) => {
3530
+ if (bd.weekday !== weekday) return false;
3531
+ if (bd.nth === void 0) return true;
3532
+ const inMonth = rule.freq === "MONTHLY" || rule.byMonth !== void 0;
3533
+ const firstDay = inMonth ? dayNumber(y, m, 1) : dayNumber(y, 1, 1);
3534
+ const lastDay = inMonth ? dayNumber(y, m, dim) : dayNumber(y, 12, 31);
3535
+ return bd.nth > 0 ? Math.floor((day - firstDay) / 7) + 1 === bd.nth : Math.floor((lastDay - day) / 7) + 1 === -bd.nth;
3536
+ });
3537
+ if (!hit) return false;
3538
+ }
3539
+ return true;
3540
+ }
3541
+ __name(dayMatches, "dayMatches");
3542
+ function occurrencesOn(rule, anchor, day) {
3543
+ if (!dayMatches(rule, anchor, day)) return [];
3544
+ const { y, m, d } = fromDayNumber(day);
3545
+ const anchorDay = dayNumber(anchor.y, anchor.m, anchor.d);
3546
+ const range = /* @__PURE__ */ __name((n) => Array.from({
3547
+ length: n
3548
+ }, (_, i) => i), "range");
3549
+ let hours = rule.byHour ?? (rule.freq === "HOURLY" || rule.freq === "MINUTELY" ? range(24) : [
3550
+ anchor.h
3551
+ ]);
3552
+ if (rule.freq === "HOURLY") {
3553
+ hours = hours.filter((h) => ((day - anchorDay) * 24 + h - anchor.h) % rule.interval === 0);
3554
+ }
3555
+ const minutes = rule.byMinute ?? (rule.freq === "MINUTELY" ? range(60) : [
3556
+ anchor.mi
3557
+ ]);
3558
+ const seconds = rule.bySecond ?? [
3559
+ anchor.s
3560
+ ];
3561
+ const out = [];
3562
+ for (const h of hours) {
3563
+ for (const mi of minutes) {
3564
+ if (rule.freq === "MINUTELY") {
3565
+ const sinceAnchor = ((day - anchorDay) * 24 + h - anchor.h) * 60 + mi - anchor.mi;
3566
+ if (sinceAnchor % rule.interval !== 0) continue;
3567
+ }
3568
+ for (const s of seconds) {
3569
+ const instant = toInstant({
3570
+ y,
3571
+ m,
3572
+ d,
3573
+ h,
3574
+ mi,
3575
+ s
3576
+ }, rule.timezone);
3577
+ if (instant !== null && instant >= rule.start && (rule.until === void 0 || instant <= rule.until)) {
3578
+ out.push(instant);
3579
+ }
3580
+ }
3581
+ }
3582
+ }
3583
+ return [
3584
+ ...new Set(out)
3585
+ ].sort((a, b) => a - b);
3586
+ }
3587
+ __name(occurrencesOn, "occurrencesOn");
3588
+ function countedOccurrences(rule, anchor) {
3589
+ const out = [];
3590
+ const first = dayNumber(anchor.y, anchor.m, anchor.d);
3591
+ for (let day = first; day < first + HORIZON_DAYS && out.length < rule.count; day++) {
3592
+ for (const t of occurrencesOn(rule, anchor, day)) {
3593
+ if (out.length < rule.count) out.push(t);
3594
+ }
3595
+ }
3596
+ return out;
3597
+ }
3598
+ __name(countedOccurrences, "countedOccurrences");
3599
+ function nextRRuleFireMs(rule, afterMs) {
3600
+ const anchor = civilOf(rule.start, rule.timezone);
3601
+ if (rule.count !== void 0) return countedOccurrences(rule, anchor).find((t) => t > afterMs) ?? null;
3602
+ const from = Math.max(afterMs, rule.start - 1);
3603
+ const c = civilOf(from, rule.timezone);
3604
+ const firstDay = dayNumber(c.y, c.m, c.d);
3605
+ for (let day = firstDay; day < firstDay + HORIZON_DAYS; day++) {
3606
+ if (rule.until !== void 0 && day * DAY_MS > rule.until + DAY_MS) return null;
3607
+ const hit = occurrencesOn(rule, anchor, day).find((t) => t > afterMs);
3608
+ if (hit !== void 0) return hit;
3609
+ }
3610
+ return null;
3611
+ }
3612
+ __name(nextRRuleFireMs, "nextRRuleFireMs");
3613
+ function prevRRuleFireMs(rule, atMs) {
3614
+ const anchor = civilOf(rule.start, rule.timezone);
3615
+ if (rule.count !== void 0) {
3616
+ const before = countedOccurrences(rule, anchor).filter((t) => t <= atMs);
3617
+ return before.length > 0 ? before[before.length - 1] : null;
3618
+ }
3619
+ const c = civilOf(atMs, rule.timezone);
3620
+ const lastDay = dayNumber(c.y, c.m, c.d);
3621
+ const anchorDay = dayNumber(anchor.y, anchor.m, anchor.d);
3622
+ for (let day = lastDay; day >= anchorDay && day > lastDay - HORIZON_DAYS; day--) {
3623
+ const hits = occurrencesOn(rule, anchor, day).filter((t) => t <= atMs);
3624
+ if (hits.length > 0) return hits[hits.length - 1];
3625
+ }
3626
+ return null;
3627
+ }
3628
+ __name(prevRRuleFireMs, "prevRRuleFireMs");
3629
+
3265
3630
  // src/scheduler.ts
3266
3631
  var import_node_module = require("module");
3267
3632
 
@@ -3706,9 +4071,15 @@ var ScheduleClient = class {
3706
4071
  }
3707
4072
  };
3708
4073
  function toSpec(opts) {
3709
- if (opts.cron == null === (opts.every == null)) {
3710
- throw new Error(`schedule "${opts.id}" needs exactly one of "cron" or "every"`);
3711
- }
4074
+ const kinds = [
4075
+ opts.cron,
4076
+ opts.every,
4077
+ opts.rrule
4078
+ ].filter((k) => k != null).length;
4079
+ if (kinds !== 1) {
4080
+ throw new Error(`schedule "${opts.id}" needs exactly one of "cron", "every" or "rrule"`);
4081
+ }
4082
+ if (opts.rrule != null) parseRRule(opts.rrule, opts.timezone);
3712
4083
  const everyMs = opts.every != null ? parseDuration(opts.every) : void 0;
3713
4084
  if (everyMs !== void 0 && !(everyMs > 0)) {
3714
4085
  throw new Error(`schedule "${opts.id}": "every" must be a positive duration`);
@@ -3719,6 +4090,7 @@ function toSpec(opts) {
3719
4090
  input: opts.input,
3720
4091
  cron: opts.cron,
3721
4092
  everyMs,
4093
+ rrule: opts.rrule,
3722
4094
  timezone: opts.timezone,
3723
4095
  jitterMs: jitterMs && jitterMs > 0 ? jitterMs : void 0,
3724
4096
  overlap: opts.overlap ?? "allow",
@@ -3736,12 +4108,18 @@ function specOf(record) {
3736
4108
  __name(specOf, "specOf");
3737
4109
  function latestWindowAtOrBefore(spec, atMs) {
3738
4110
  if (spec.cron != null) return prevCronFireMs(spec.cron, atMs, spec.timezone);
4111
+ if (spec.rrule != null) {
4112
+ const prev = prevRRuleFireMs(parseRRule(spec.rrule, spec.timezone), atMs);
4113
+ if (prev === null) throw new Error(`rrule "${spec.rrule}" has no occurrence at or before ${new Date(atMs).toISOString()}`);
4114
+ return prev;
4115
+ }
3739
4116
  const every = spec.everyMs;
3740
4117
  return Math.floor(atMs / every) * every;
3741
4118
  }
3742
4119
  __name(latestWindowAtOrBefore, "latestWindowAtOrBefore");
3743
4120
  function nextWindowAfter(spec, afterMs) {
3744
4121
  if (spec.cron != null) return nextCronFireMs(spec.cron, afterMs, spec.timezone);
4122
+ if (spec.rrule != null) return nextRRuleFireMs(parseRRule(spec.rrule, spec.timezone), afterMs);
3745
4123
  const every = spec.everyMs;
3746
4124
  return (Math.floor(afterMs / every) + 1) * every;
3747
4125
  }
@@ -3781,6 +4159,7 @@ function describe(record) {
3781
4159
  namespace: record.namespace,
3782
4160
  cron: spec.cron,
3783
4161
  everyMs: spec.everyMs,
4162
+ rrule: spec.rrule,
3784
4163
  timezone: spec.timezone,
3785
4164
  jitterMs: spec.jitterMs,
3786
4165
  overlap: spec.overlap ?? "allow",
@@ -4093,6 +4472,240 @@ async function stalledInfo(store, run, now, thresholdMs, ageMs) {
4093
4472
  }
4094
4473
  __name(stalledInfo, "stalledInfo");
4095
4474
 
4475
+ // src/sticky-continuation.ts
4476
+ var StickyContinuation = class {
4477
+ static {
4478
+ __name(this, "StickyContinuation");
4479
+ }
4480
+ runId;
4481
+ fn;
4482
+ workflowVersion;
4483
+ compensations;
4484
+ replay;
4485
+ onPoisoned;
4486
+ /** The body's settled outcome, once it has one. */
4487
+ outcome;
4488
+ /** Primitives started by the body and not yet settled or parked. */
4489
+ active = 0;
4490
+ /** Bumped on every change, so a quiescence check can tell nothing happened in between. */
4491
+ version = 0;
4492
+ checkScheduled = false;
4493
+ waiter;
4494
+ parked = /* @__PURE__ */ new Map();
4495
+ /** A turn currently owns the body (holds the run's lease). */
4496
+ attached = true;
4497
+ /** The body called a primitive while detached, or one suspended the classic way: never reuse it. */
4498
+ unusable = false;
4499
+ /** The run row's `updatedAt` (epoch ms) this process wrote when the body last parked. */
4500
+ stamp;
4501
+ lastUsed = Date.now();
4502
+ /**
4503
+ * Parked steps' `completed` rows, as the instance that settled them read them back: the next turn
4504
+ * takes them instead of reading them again. A completed row never changes, so the hint can only be
4505
+ * missing, never stale.
4506
+ */
4507
+ hints = /* @__PURE__ */ new Map();
4508
+ constructor(runId, fn, workflowVersion, compensations, replay, onPoisoned) {
4509
+ this.runId = runId;
4510
+ this.fn = fn;
4511
+ this.workflowVersion = workflowVersion;
4512
+ this.compensations = compensations;
4513
+ this.replay = replay;
4514
+ this.onPoisoned = onPoisoned;
4515
+ }
4516
+ /** Remember a parked step's settled row for the next turn (see {@link hints}). */
4517
+ hint(cp) {
4518
+ if (cp.status === "completed" && this.parked.has(cp.seq)) this.hints.set(cp.seq, cp);
4519
+ }
4520
+ /** Start the body. `run` invokes it (inside the ambient workflow ctx). */
4521
+ begin(run) {
4522
+ let started;
4523
+ try {
4524
+ started = run();
4525
+ } catch (error2) {
4526
+ started = Promise.reject(error2);
4527
+ }
4528
+ started.then((value) => this.settle({
4529
+ kind: "returned",
4530
+ value
4531
+ }), (error2) => this.settle({
4532
+ kind: "threw",
4533
+ error: error2
4534
+ }));
4535
+ }
4536
+ settle(outcome) {
4537
+ this.outcome = outcome;
4538
+ this.changed();
4539
+ }
4540
+ /**
4541
+ * Called by every primitive the body starts. `false` refuses the call: the body is detached (no
4542
+ * turn holds the lease), and this continuation is poisoned.
4543
+ */
4544
+ enter() {
4545
+ if (!this.attached) {
4546
+ if (!this.unusable) {
4547
+ this.unusable = true;
4548
+ this.onPoisoned(this);
4549
+ }
4550
+ return false;
4551
+ }
4552
+ this.active += 1;
4553
+ this.changed();
4554
+ return true;
4555
+ }
4556
+ /** A primitive the body started settled. A classic suspension makes the body single-use. */
4557
+ exit(error2) {
4558
+ this.active -= 1;
4559
+ if (error2 instanceof WorkflowSuspended) this.unusable = true;
4560
+ this.changed();
4561
+ }
4562
+ /**
4563
+ * Park a dispatched call: it stops counting as active, and its promise settles when a later turn
4564
+ * re-evaluates it to an outcome.
4565
+ */
4566
+ park(seq, wakeAt, reevaluate) {
4567
+ return new Promise((resolve, reject) => {
4568
+ this.parked.set(seq, {
4569
+ seq,
4570
+ wakeAt,
4571
+ reevaluate,
4572
+ resolve,
4573
+ reject
4574
+ });
4575
+ this.active -= 1;
4576
+ this.changed();
4577
+ });
4578
+ }
4579
+ /**
4580
+ * Re-evaluate every parked call (in position order) with its freshly read checkpoint. A call that
4581
+ * has an outcome leaves the parked set and settles; one still waiting stays parked, with its new
4582
+ * deadline. Run by a warm turn after it attached.
4583
+ */
4584
+ async resumeParked(fresh) {
4585
+ const calls = [
4586
+ ...this.parked.values()
4587
+ ].sort((a, b) => a.seq - b.seq);
4588
+ for (const call of calls) {
4589
+ try {
4590
+ const value = await call.reevaluate(fresh.get(call.seq) ?? null);
4591
+ this.parked.delete(call.seq);
4592
+ this.active += 1;
4593
+ call.resolve(value);
4594
+ } catch (error2) {
4595
+ if (error2 instanceof WorkflowSuspended) {
4596
+ call.wakeAt = error2.wakeAt;
4597
+ continue;
4598
+ }
4599
+ this.parked.delete(call.seq);
4600
+ this.active += 1;
4601
+ call.reject(error2);
4602
+ }
4603
+ }
4604
+ this.changed();
4605
+ }
4606
+ /**
4607
+ * Wait for this turn's outcome: the body settled, or it is parked on at least one dispatched call
4608
+ * with nothing else in flight. Quiescence is confirmed a macrotask later with nothing changed in
4609
+ * between, so microtask continuations of the body (a `Promise.all` starting its next call) run
4610
+ * first. Parking detaches the body.
4611
+ */
4612
+ next() {
4613
+ return new Promise((resolve) => {
4614
+ this.waiter = resolve;
4615
+ this.changed();
4616
+ });
4617
+ }
4618
+ changed() {
4619
+ this.version += 1;
4620
+ const waiter = this.waiter;
4621
+ if (!waiter) return;
4622
+ if (this.outcome) {
4623
+ this.waiter = void 0;
4624
+ waiter(this.outcome);
4625
+ return;
4626
+ }
4627
+ if (this.checkScheduled || this.parked.size === 0 || this.active > 0) return;
4628
+ this.checkScheduled = true;
4629
+ const at = this.version;
4630
+ setImmediate(() => {
4631
+ this.checkScheduled = false;
4632
+ if (this.waiter !== waiter) return;
4633
+ if (this.version !== at) {
4634
+ this.changed();
4635
+ return;
4636
+ }
4637
+ if (this.outcome || this.parked.size === 0 || this.active > 0) return;
4638
+ this.waiter = void 0;
4639
+ this.attached = false;
4640
+ let wakeAt;
4641
+ for (const call of this.parked.values()) {
4642
+ if (call.wakeAt !== void 0) wakeAt = Math.min(wakeAt ?? call.wakeAt, call.wakeAt);
4643
+ }
4644
+ waiter({
4645
+ kind: "parked",
4646
+ wakeAt
4647
+ });
4648
+ });
4649
+ }
4650
+ };
4651
+ var StickyCache = class {
4652
+ static {
4653
+ __name(this, "StickyCache");
4654
+ }
4655
+ maxEntries;
4656
+ maxIdleMs;
4657
+ entries = /* @__PURE__ */ new Map();
4658
+ hits = 0;
4659
+ misses = 0;
4660
+ constructor(maxEntries, maxIdleMs) {
4661
+ this.maxEntries = maxEntries;
4662
+ this.maxIdleMs = maxIdleMs;
4663
+ }
4664
+ get enabled() {
4665
+ return this.maxEntries > 0;
4666
+ }
4667
+ get size() {
4668
+ return this.entries.size;
4669
+ }
4670
+ put(c) {
4671
+ if (!this.enabled || c.unusable) return;
4672
+ c.lastUsed = Date.now();
4673
+ this.entries.delete(c.runId);
4674
+ this.entries.set(c.runId, c);
4675
+ this.prune(c.lastUsed);
4676
+ }
4677
+ take(runId) {
4678
+ const c = this.entries.get(runId);
4679
+ if (!c) return void 0;
4680
+ this.entries.delete(runId);
4681
+ if (c.unusable || Date.now() - c.lastUsed > this.maxIdleMs) return void 0;
4682
+ return c;
4683
+ }
4684
+ /** The parked continuation of `runId`, left in place. */
4685
+ peek(runId) {
4686
+ return this.entries.get(runId);
4687
+ }
4688
+ has(runId) {
4689
+ return this.entries.has(runId);
4690
+ }
4691
+ evict(runId) {
4692
+ this.entries.delete(runId);
4693
+ }
4694
+ clear() {
4695
+ this.entries.clear();
4696
+ }
4697
+ prune(now) {
4698
+ while (this.entries.size > this.maxEntries) {
4699
+ const oldest = this.entries.keys().next().value;
4700
+ this.entries.delete(oldest);
4701
+ }
4702
+ for (const [runId, c] of this.entries) {
4703
+ if (now - c.lastUsed <= this.maxIdleMs) break;
4704
+ this.entries.delete(runId);
4705
+ }
4706
+ }
4707
+ };
4708
+
4096
4709
  // src/tenant-auth.ts
4097
4710
  var import_node_crypto = require("crypto");
4098
4711
  var TOKEN_SEP = ".";
@@ -4167,9 +4780,29 @@ var TransportPool = class {
4167
4780
  __name(this, "TransportPool");
4168
4781
  }
4169
4782
  transports;
4783
+ /** Ids of the transports that route replies to {@link replyTo} (see {@link routeReplies}). */
4784
+ replying = /* @__PURE__ */ new Set();
4785
+ replyTo;
4170
4786
  constructor(transports) {
4171
4787
  this.transports = transports;
4172
4788
  }
4789
+ /**
4790
+ * Have every transport that can ({@link Transport.consumeRepliesFor}) send this engine instance
4791
+ * the results of the tasks it dispatches with `replyTo`. Returns whether any transport can.
4792
+ */
4793
+ routeReplies(instanceId) {
4794
+ this.replyTo = instanceId;
4795
+ for (const { id, transport } of this.transports) {
4796
+ if (!transport.consumeRepliesFor) continue;
4797
+ transport.consumeRepliesFor(instanceId);
4798
+ this.replying.add(id);
4799
+ }
4800
+ return this.replying.size > 0;
4801
+ }
4802
+ /** Stop stamping `replyTo` on tasks (a draining engine): their results go to the shared results. */
4803
+ stopReplies() {
4804
+ this.replyTo = void 0;
4805
+ }
4173
4806
  get size() {
4174
4807
  return this.transports.length;
4175
4808
  }
@@ -4210,8 +4843,13 @@ var TransportPool = class {
4210
4843
  let lastErr;
4211
4844
  for (const { id, transport } of this.ordered(preferId)) {
4212
4845
  try {
4846
+ const { replyTo, ...rest } = task;
4847
+ const reply = replyTo !== void 0 && this.replying.has(id) ? {
4848
+ replyTo
4849
+ } : {};
4213
4850
  await transport.dispatch({
4214
- ...task,
4851
+ ...rest,
4852
+ ...reply,
4215
4853
  transport: id
4216
4854
  });
4217
4855
  return;
@@ -4410,6 +5048,266 @@ var TransportPool = class {
4410
5048
  }
4411
5049
  };
4412
5050
 
5051
+ // src/wait-any.ts
5052
+ var ANY_WAIT_GROUP = "$any";
5053
+ var ANY_CHECKPOINT_PREFIX = "any:";
5054
+ function anyCheckpointName(name) {
5055
+ return `${ANY_CHECKPOINT_PREFIX}${name}`;
5056
+ }
5057
+ __name(anyCheckpointName, "anyCheckpointName");
5058
+ function isAnyCheckpoint(cp) {
5059
+ return cp.kind === "signal" && cp.name.startsWith(ANY_CHECKPOINT_PREFIX);
5060
+ }
5061
+ __name(isAnyCheckpoint, "isAnyCheckpoint");
5062
+ function isAnyMember(waiter) {
5063
+ return waiter.parallelGroup === ANY_WAIT_GROUP;
5064
+ }
5065
+ __name(isAnyMember, "isAnyMember");
5066
+ function anyConditionToken(c) {
5067
+ if (c.kind === "signal") return c.signal;
5068
+ if (c.kind === "child") return `child:${c.childId}`;
5069
+ return void 0;
5070
+ }
5071
+ __name(anyConditionToken, "anyConditionToken");
5072
+ function anyConditionsOf(cp) {
5073
+ return cp.input?.conditions ?? [];
5074
+ }
5075
+ __name(anyConditionsOf, "anyConditionsOf");
5076
+ function earliestDeadline(conditions) {
5077
+ let at;
5078
+ for (const c of conditions) if (c.kind === "timer" && (at === void 0 || c.at < at)) at = c.at;
5079
+ return at;
5080
+ }
5081
+ __name(earliestDeadline, "earliestDeadline");
5082
+ function dueTimer(conditions, now) {
5083
+ let due;
5084
+ for (const c of conditions) {
5085
+ if (c.kind !== "timer" || c.at > now) continue;
5086
+ if (!due || c.at < due.at) due = c;
5087
+ }
5088
+ return due;
5089
+ }
5090
+ __name(dueTimer, "dueTimer");
5091
+ function anyOutcomeOf(cp) {
5092
+ const { key, value, childId } = cp.output;
5093
+ if (childId !== void 0) return {
5094
+ key,
5095
+ value: unwrapCompletion(value, `child "${childId}"`)
5096
+ };
5097
+ return {
5098
+ key,
5099
+ value
5100
+ };
5101
+ }
5102
+ __name(anyOutcomeOf, "anyOutcomeOf");
5103
+ async function takeOldestBuffered(store, tokens) {
5104
+ if (tokens.length === 0) return null;
5105
+ if (store.takeOldestBufferedSignal) return store.takeOldestBufferedSignal(tokens);
5106
+ for (const token of tokens) {
5107
+ const taken = await store.takeBufferedSignal(token);
5108
+ if (taken) return {
5109
+ token,
5110
+ payload: taken.payload
5111
+ };
5112
+ }
5113
+ return null;
5114
+ }
5115
+ __name(takeOldestBuffered, "takeOldestBuffered");
5116
+ async function claimAnyCheckpoint(store, cp) {
5117
+ if (store.saveCheckpointIfPending) return store.saveCheckpointIfPending(cp);
5118
+ const current = await store.getCheckpoint(cp.runId, cp.seq);
5119
+ if (current?.status !== "pending") return false;
5120
+ await store.saveCheckpoint(cp);
5121
+ return true;
5122
+ }
5123
+ __name(claimAnyCheckpoint, "claimAnyCheckpoint");
5124
+ function resolvedAnyCheckpoint(cp, condition, value) {
5125
+ const output = condition.kind === "child" ? {
5126
+ key: condition.key,
5127
+ value,
5128
+ childId: condition.childId
5129
+ } : {
5130
+ key: condition.key,
5131
+ value
5132
+ };
5133
+ return {
5134
+ ...cp,
5135
+ status: "completed",
5136
+ output,
5137
+ wakeAt: void 0,
5138
+ finishedAt: /* @__PURE__ */ new Date()
5139
+ };
5140
+ }
5141
+ __name(resolvedAnyCheckpoint, "resolvedAnyCheckpoint");
5142
+ async function removeAnyWaiters(store, cp) {
5143
+ await Promise.all(anyConditionsOf(cp).flatMap((c) => {
5144
+ const token = anyConditionToken(c);
5145
+ return token === void 0 ? [] : [
5146
+ store.removeSignalWaiter({
5147
+ token,
5148
+ runId: cp.runId,
5149
+ seq: cp.seq,
5150
+ parallelGroup: ANY_WAIT_GROUP
5151
+ })
5152
+ ];
5153
+ }));
5154
+ }
5155
+ __name(removeAnyWaiters, "removeAnyWaiters");
5156
+ async function settle(store, cp, ready) {
5157
+ const done = resolvedAnyCheckpoint(cp, ready.condition, ready.value);
5158
+ if (await claimAnyCheckpoint(store, done)) {
5159
+ await removeAnyWaiters(store, done);
5160
+ return done;
5161
+ }
5162
+ if (ready.taken) await store.bufferSignal(ready.taken.token, ready.taken.payload);
5163
+ const winner = await store.getCheckpoint(cp.runId, cp.seq);
5164
+ return winner?.status === "completed" ? winner : void 0;
5165
+ }
5166
+ __name(settle, "settle");
5167
+ async function readyCondition(io, conditions) {
5168
+ const tokens = [];
5169
+ for (const c of conditions) {
5170
+ const token = anyConditionToken(c);
5171
+ if (token !== void 0 && !tokens.includes(token)) tokens.push(token);
5172
+ }
5173
+ const taken = await takeOldestBuffered(io.store, tokens);
5174
+ if (taken) {
5175
+ const condition = conditions.find((c) => anyConditionToken(c) === taken.token);
5176
+ if (condition) return {
5177
+ condition,
5178
+ value: taken.payload,
5179
+ taken
5180
+ };
5181
+ }
5182
+ for (const c of conditions) {
5183
+ if (c.kind !== "child") continue;
5184
+ const child = await io.store.getRun(c.childId);
5185
+ const completion = child ? childCompletion(child) : void 0;
5186
+ if (completion) return {
5187
+ condition: c,
5188
+ value: completion
5189
+ };
5190
+ }
5191
+ const due = dueTimer(conditions, io.clock());
5192
+ return due ? {
5193
+ condition: due,
5194
+ value: null
5195
+ } : void 0;
5196
+ }
5197
+ __name(readyCondition, "readyCondition");
5198
+ async function enterWaitAny(io, runId, seq, name, conditions) {
5199
+ const { store } = io;
5200
+ const cpName = anyCheckpointName(name);
5201
+ let cp = await store.getCheckpoint(runId, seq);
5202
+ if (cp && cp.name !== cpName) throw new NonDeterminismError(runId, seq, cpName, cp.name);
5203
+ if (cp?.status === "completed") return {
5204
+ state: "resolved",
5205
+ checkpoint: cp
5206
+ };
5207
+ if (!cp) {
5208
+ const now = io.clock();
5209
+ const stored2 = conditions.map((c) => c.kind === "timer" ? {
5210
+ key: c.key,
5211
+ kind: "timer",
5212
+ at: c.until ?? now + (c.ms ?? 0)
5213
+ } : c.kind === "signal" ? {
5214
+ key: c.key,
5215
+ kind: "signal",
5216
+ signal: c.signal
5217
+ } : {
5218
+ key: c.key,
5219
+ kind: "child",
5220
+ childId: c.childId
5221
+ });
5222
+ const at = new Date(now);
5223
+ cp = stepCheckpoint({
5224
+ runId,
5225
+ seq,
5226
+ name: cpName,
5227
+ kind: "signal",
5228
+ status: "pending",
5229
+ attempts: 1,
5230
+ input: {
5231
+ conditions: stored2
5232
+ },
5233
+ wakeAt: earliestDeadline(stored2),
5234
+ enqueuedAt: at,
5235
+ startedAt: at,
5236
+ finishedAt: at
5237
+ });
5238
+ await store.saveCheckpoint(cp);
5239
+ }
5240
+ const stored = anyConditionsOf(cp);
5241
+ const ready = await readyCondition(io, stored);
5242
+ if (ready) {
5243
+ const winner = await settle(store, cp, ready);
5244
+ if (winner) return {
5245
+ state: "resolved",
5246
+ checkpoint: winner
5247
+ };
5248
+ }
5249
+ for (const c of stored) {
5250
+ const token = anyConditionToken(c);
5251
+ if (token === void 0) continue;
5252
+ await store.putSignalWaiter({
5253
+ token,
5254
+ runId,
5255
+ seq,
5256
+ parallelGroup: ANY_WAIT_GROUP
5257
+ });
5258
+ }
5259
+ const late = await readyCondition(io, stored);
5260
+ if (late) {
5261
+ const winner = await settle(store, cp, late);
5262
+ if (winner) return {
5263
+ state: "resolved",
5264
+ checkpoint: winner
5265
+ };
5266
+ }
5267
+ return {
5268
+ state: "waiting",
5269
+ wakeAt: earliestDeadline(stored)
5270
+ };
5271
+ }
5272
+ __name(enterWaitAny, "enterWaitAny");
5273
+ function anyHistoryEvent(cp) {
5274
+ const { key, value, childId } = cp.output;
5275
+ const name = cp.name.slice(ANY_CHECKPOINT_PREFIX.length);
5276
+ if (childId !== void 0) {
5277
+ const completion = value;
5278
+ if (completion && completion.ok === false) {
5279
+ return {
5280
+ seq: cp.seq,
5281
+ kind: "any",
5282
+ name,
5283
+ output: {
5284
+ key
5285
+ },
5286
+ error: completionError(completion)
5287
+ };
5288
+ }
5289
+ return {
5290
+ seq: cp.seq,
5291
+ kind: "any",
5292
+ name,
5293
+ output: {
5294
+ key,
5295
+ value: completion?.ok ? completion.value : void 0
5296
+ }
5297
+ };
5298
+ }
5299
+ return {
5300
+ seq: cp.seq,
5301
+ kind: "any",
5302
+ name,
5303
+ output: {
5304
+ key,
5305
+ value
5306
+ }
5307
+ };
5308
+ }
5309
+ __name(anyHistoryEvent, "anyHistoryEvent");
5310
+
4413
5311
  // src/step-name-symbol.ts
4414
5312
  var DURABLE_STEP_NAME = Symbol.for("durable.step.name");
4415
5313
  var DURABLE_STEP_CONFIG = Symbol.for("durable.step.config");
@@ -4829,9 +5727,10 @@ function createWorkflowCtx(host, runId, compensations, workflow = "", searchAttr
4829
5727
  return unwrapCompletion(await waitForSignal(`task:${runId}:${name}`), `task "${name}"`);
4830
5728
  }, "task");
4831
5729
  const child = /* @__PURE__ */ __name(async (workflow2, input, options) => {
4832
- const { childId, priority, version } = normalizeChildOptions(options);
5730
+ const { childId, priority, version, parentClose } = normalizeChildOptions(options);
4833
5731
  const current = pos.next();
4834
5732
  const id = childId ?? `${runId}.child.${current}`;
5733
+ if (parentClose) host.noteParentClose?.(runId, id, parentClose);
4835
5734
  const existing = await readCheckpoint(current);
4836
5735
  if (existing && existing.status === "completed") {
4837
5736
  return unwrapCompletion(existing.output, `child "${id}"`);
@@ -4868,7 +5767,7 @@ function createWorkflowCtx(host, runId, compensations, workflow = "", searchAttr
4868
5767
  }
4869
5768
  const childRun = await store.getRun(id);
4870
5769
  if (!childRun) {
4871
- host.startChild(workflowName(workflow2), input, id, priority, version);
5770
+ host.startChild(workflowName(workflow2), input, id, priority, version, parentClose);
4872
5771
  } else {
4873
5772
  const done = (await settleFinishedChildren([
4874
5773
  {
@@ -4890,12 +5789,12 @@ function createWorkflowCtx(host, runId, compensations, workflow = "", searchAttr
4890
5789
  }
4891
5790
  ]));
4892
5791
  }, "child");
4893
- const fan = /* @__PURE__ */ __name(async (workflow2, inputs, opts, settle) => {
5792
+ const fan = /* @__PURE__ */ __name(async (workflow2, inputs, opts, settle2) => {
4894
5793
  if (inputs.length === 0) return [];
4895
5794
  if (stepNameOf(workflow2) !== void 0) {
4896
- return allSteps(workflow2, inputs, opts, settle);
5795
+ return allSteps(workflow2, inputs, opts, settle2);
4897
5796
  }
4898
- const mode = settle ? "waitAll" : opts?.mode ?? "waitAll";
5797
+ const mode = settle2 ? "waitAll" : opts?.mode ?? "waitAll";
4899
5798
  const name = workflowName(workflow2);
4900
5799
  const waves = allWaves(inputs.length, opts?.concurrency);
4901
5800
  const positions = inputs.map(() => pos.next());
@@ -5067,14 +5966,14 @@ function createWorkflowCtx(host, runId, compensations, workflow = "", searchAttr
5067
5966
  outputs.push(value);
5068
5967
  }
5069
5968
  }
5070
- if (settle) return results;
5969
+ if (settle2) return results;
5071
5970
  if (failures.length > 0) throw new GatherError(failures, inputs.length);
5072
5971
  return outputs;
5073
5972
  }
5074
5973
  }, "fan");
5075
5974
  const all = /* @__PURE__ */ __name((workflow2, inputs, opts) => fan(workflow2, inputs, opts, false), "all");
5076
5975
  const allSettled = /* @__PURE__ */ __name((workflow2, inputs, opts) => fan(workflow2, inputs, opts, true), "allSettled");
5077
- const allSteps = /* @__PURE__ */ __name(async (ref, inputs, opts, settle) => {
5976
+ const allSteps = /* @__PURE__ */ __name(async (ref, inputs, opts, settle2) => {
5078
5977
  if (opts?.queue !== void 0) {
5079
5978
  throw new Error("ctx.all: a flow-control queue is not supported on a step fan");
5080
5979
  }
@@ -5085,8 +5984,8 @@ function createWorkflowCtx(host, runId, compensations, workflow = "", searchAttr
5085
5984
  };
5086
5985
  const positions = inputs.map(() => pos.next());
5087
5986
  return host.callRemoteAll(runId, positions, def, inputs, {
5088
- mode: settle ? "waitAll" : opts?.mode ?? "waitAll",
5089
- settle,
5987
+ mode: settle2 ? "waitAll" : opts?.mode ?? "waitAll",
5988
+ settle: settle2,
5090
5989
  concurrency: opts?.concurrency,
5091
5990
  transport: opts?.transport,
5092
5991
  admission: {
@@ -5190,12 +6089,13 @@ function createWorkflowCtx(host, runId, compensations, workflow = "", searchAttr
5190
6089
  return settled;
5191
6090
  }, "settleFinishedChildren");
5192
6091
  const startChild = /* @__PURE__ */ __name(async (workflow2, input, options) => {
5193
- const { childId, priority, version } = normalizeChildOptions(options);
6092
+ const { childId, priority, version, parentClose } = normalizeChildOptions(options);
5194
6093
  const current = pos.next();
5195
6094
  const id = childId ?? `${runId}.child.${current}`;
6095
+ if (parentClose) host.noteParentClose?.(runId, id, parentClose);
5196
6096
  const existing = await readCheckpoint(current);
5197
6097
  if (existing && existing.status === "completed") return existing.output;
5198
- if (!await store.getRun(id)) host.startChild(workflowName(workflow2), input, id, priority, version);
6098
+ if (!await store.getRun(id)) host.startChild(workflowName(workflow2), input, id, priority, version, parentClose);
5199
6099
  await writeCheckpoint(instantCheckpoint({
5200
6100
  runId,
5201
6101
  seq: current,
@@ -5457,17 +6357,74 @@ function createWorkflowCtx(host, runId, compensations, workflow = "", searchAttr
5457
6357
  });
5458
6358
  }
5459
6359
  __name(step, "step");
6360
+ const waitForAny = /* @__PURE__ */ __name(async (name, conditions) => {
6361
+ const current = pos.next();
6362
+ const cached = replay?.get(current);
6363
+ if (cached?.status === "completed" && cached.name === anyCheckpointName(name)) {
6364
+ return anyOutcomeOf(cached);
6365
+ }
6366
+ const wire = Object.entries(conditions).map(([key, c]) => ({
6367
+ ...c,
6368
+ key
6369
+ }));
6370
+ const state = await enterWaitAny({
6371
+ store,
6372
+ clock: host.clock
6373
+ }, runId, current, name, wire);
6374
+ if (state.state === "resolved") {
6375
+ replay?.set(current, state.checkpoint);
6376
+ return anyOutcomeOf(state.checkpoint);
6377
+ }
6378
+ throw new WorkflowSuspended(state.wakeAt);
6379
+ }, "waitForAny");
6380
+ const signalWait = /* @__PURE__ */ __name((token) => ({
6381
+ kind: "signal",
6382
+ signal: token
6383
+ }), "signalWait");
6384
+ const timer = /* @__PURE__ */ __name((duration) => typeof duration === "object" ? {
6385
+ kind: "timer",
6386
+ until: typeof duration.until === "number" ? duration.until : duration.until.getTime()
6387
+ } : {
6388
+ kind: "timer",
6389
+ ms: parseDuration(duration)
6390
+ }, "timer");
6391
+ const childWait = /* @__PURE__ */ __name((first, second) => ({
6392
+ kind: "child",
6393
+ childId: typeof first === "string" && second === void 0 ? first : second
6394
+ }), "childWait");
5460
6395
  const nestedStep = /* @__PURE__ */ __name(() => {
5461
6396
  const scope = currentStepScope();
5462
6397
  return scope !== void 0 && scope.owner === scopeOwner ? scope.step : void 0;
5463
6398
  }, "nestedStep");
5464
- const guard = /* @__PURE__ */ __name((primitive, fn) => (...args) => {
6399
+ const unguarded = /* @__PURE__ */ __name((primitive, fn) => (...args) => {
5465
6400
  const stepName = nestedStep();
5466
6401
  if (stepName === void 0) return fn(...args);
5467
6402
  return (async () => {
5468
6403
  if (await readCheckpoint(pos.peek())) return fn(...args);
5469
6404
  throw new NestedWorkflowCallError(runId, primitive, stepName);
5470
6405
  })();
6406
+ }, "unguarded");
6407
+ const sticky = host.sticky;
6408
+ const guard = /* @__PURE__ */ __name((primitive, fn) => {
6409
+ const inner = unguarded(primitive, fn);
6410
+ if (!sticky) return inner;
6411
+ return (...args) => {
6412
+ if (!sticky.enter()) return Promise.reject(new WorkflowSuspended());
6413
+ let started;
6414
+ try {
6415
+ started = inner(...args);
6416
+ } catch (err) {
6417
+ sticky.exit(err);
6418
+ throw err;
6419
+ }
6420
+ return started.then((value) => {
6421
+ sticky.exit();
6422
+ return value;
6423
+ }, (err) => {
6424
+ sticky.exit(err);
6425
+ throw err;
6426
+ });
6427
+ };
5471
6428
  }, "guard");
5472
6429
  const guardedWebhook = /* @__PURE__ */ __name(() => {
5473
6430
  const stepName = nestedStep();
@@ -5488,6 +6445,10 @@ function createWorkflowCtx(host, runId, compensations, workflow = "", searchAttr
5488
6445
  sleepUntil: guard("sleepUntil", sleepUntil),
5489
6446
  continueAsNew,
5490
6447
  waitForSignal: guard("waitForSignal", waitForSignal),
6448
+ waitForAny: guard("waitForAny", waitForAny),
6449
+ signalWait,
6450
+ timer,
6451
+ childWait,
5491
6452
  waitForEvent: guard("waitForEvent", waitForEvent),
5492
6453
  waitForRun: guard("waitForRun", waitForRun),
5493
6454
  task: guard("task", task),
@@ -5518,6 +6479,9 @@ function mergeTags(staticTags, runTags) {
5518
6479
  }
5519
6480
  __name(mergeTags, "mergeTags");
5520
6481
  var BREAKPOINT = "breakpoint";
6482
+ var PENDING_POLL_BATCH = 100;
6483
+ var DEFAULT_STICKY_RUNS = 1e3;
6484
+ var DEFAULT_STICKY_IDLE_MS = 3e5;
5521
6485
  var isBreakpoint = /* @__PURE__ */ __name((cp) => cp.status === "pending" && cp.name.startsWith(BREAKPOINT), "isBreakpoint");
5522
6486
  var ANNOUNCED_ROUTES_TTL_MS = 5e3;
5523
6487
  function servedNamespace(namespace, group) {
@@ -5583,6 +6547,8 @@ var freshPendingKey = /* @__PURE__ */ __name(() => `${Date.now()}-${globalThis.c
5583
6547
  var wakeRequestToken = /* @__PURE__ */ __name((runId) => `durable:wake-after-turn:${runId}`, "wakeRequestToken");
5584
6548
  var WAKE_REQUEST_SCAN = 100;
5585
6549
  var CHILD_SIGNAL_PREFIX = "signal:child:";
6550
+ var DEFAULT_SIGNAL_ID_TTL_MS = 7 * 864e5;
6551
+ var SIGNAL_ID_SWEEP_BATCH = 500;
5586
6552
  var BUFFERED_SWEEP_PAGE = 500;
5587
6553
  var BUFFERED_SWEEP_PAGES_PER_CALL = 4;
5588
6554
  var BUFFERED_SWEEP_BUDGET_MS = 250;
@@ -5713,12 +6679,29 @@ var WorkflowEngine = class _WorkflowEngine {
5713
6679
  unregisteredWorkflowGraceMs;
5714
6680
  remoteAdvanceSilenceMs;
5715
6681
  reconcileMs;
6682
+ /** Parked workflow bodies kept between turns (see {@link WorkflowEngineDeps.stickyRuns}). */
6683
+ sticky;
6684
+ /** Pending runs claimed by {@link runPending} for the turn about to run them, as read by the claim. */
6685
+ claimedRuns = /* @__PURE__ */ new Map();
6686
+ /** The continuation each turn executing here runs in, by run id, while the turn runs. */
6687
+ turnContinuations = /* @__PURE__ */ new Map();
6688
+ /** Whether to ask transports to route step results back here (see {@link WorkflowEngineDeps.routeReplies}). */
6689
+ routeReplies;
6690
+ /** Whether a transport routes this instance's step results back to it. */
6691
+ repliesRouted = false;
6692
+ /** The run row stamp (`updatedAt`, epoch ms) a turn here wrote when it settled `suspended`. */
6693
+ suspendStamps = /* @__PURE__ */ new Map();
5716
6694
  timerBatchSize;
5717
6695
  eventBufferTtlMs;
6696
+ signalIdTtlMs;
6697
+ /** The deduplicated {@link signal} sends in flight here, by `token \u0000 id`. */
6698
+ signalIdSends = /* @__PURE__ */ new Map();
5718
6699
  remoteRedispatchMs;
5719
6700
  remoteRedispatchMax;
5720
6701
  /** How long a `blocked` run waits before the recovery poll re-checks the live fleet (design §7.5). */
5721
6702
  blockedPollMs;
6703
+ workerDescriptorCacheMs;
6704
+ descriptorCache = /* @__PURE__ */ new Map();
5722
6705
  /** This engine's control-plane handshake descriptor — negotiated against each worker (design §7.3). */
5723
6706
  cpDescriptor;
5724
6707
  webhookUrl;
@@ -5743,6 +6726,10 @@ var WorkflowEngine = class _WorkflowEngine {
5743
6726
  workflows = /* @__PURE__ */ new Map();
5744
6727
  /** The newest registered version per workflow name — used to `start` new runs. */
5745
6728
  latest = /* @__PURE__ */ new Map();
6729
+ /** Workflows this instance starts but never runs, keyed `name@version` (like {@link workflows}), so every version stays startable. See {@link registerStartOnly}. */
6730
+ startOnly = /* @__PURE__ */ new Map();
6731
+ /** The latest start-only registration per name, for unpinned starts (like {@link latest}). */
6732
+ startOnlyLatest = /* @__PURE__ */ new Map();
5746
6733
  /** Event name → subscribing workflow name → that workflow's subscriptions to it. See `onEvent`. */
5747
6734
  eventTriggers = /* @__PURE__ */ new Map();
5748
6735
  /** Durable-entity subsystem (registers the `__entity` runner; see `registerEntity`). */
@@ -5873,6 +6860,8 @@ var WorkflowEngine = class _WorkflowEngine {
5873
6860
  claimsMade = /* @__PURE__ */ new Map();
5874
6861
  /** Pager hooks for stranded runs (see {@link onStalled}). */
5875
6862
  stalledListeners = /* @__PURE__ */ new Set();
6863
+ /** `parentClose` policies recorded by the turn in flight, per parent run (see {@link settleParentClose}). */
6864
+ parentCloseIntents = /* @__PURE__ */ new Map();
5876
6865
  /** Runs already paged about, by id → the `updatedAt` of the paged episode (see {@link sweepStalled}). */
5877
6866
  notifiedStalled = /* @__PURE__ */ new Map();
5878
6867
  lastStalledSweepAt = Number.NEGATIVE_INFINITY;
@@ -5907,14 +6896,18 @@ var WorkflowEngine = class _WorkflowEngine {
5907
6896
  this.unregisteredWorkflowGraceMs = grace !== void 0 && Number.isFinite(grace) && grace >= 0 ? grace : 24 * 60 * 60 * 1e3;
5908
6897
  this.remoteAdvanceSilenceMs = deps.remoteAdvanceSilenceMs;
5909
6898
  this.reconcileMs = deps.reconcileMs === void 0 ? 3e5 : deps.reconcileMs || void 0;
6899
+ this.sticky = new StickyCache(Math.max(0, deps.stickyRuns ?? DEFAULT_STICKY_RUNS), deps.stickyIdleMs ?? DEFAULT_STICKY_IDLE_MS);
6900
+ this.routeReplies = deps.routeReplies ?? true;
5910
6901
  const stalledAfter = deps.stalledAfterMs;
5911
6902
  this.stalledAfterMs = stalledAfter !== void 0 && Number.isFinite(stalledAfter) ? Math.max(6e4, stalledAfter) : DEFAULT_STALLED_AFTER_MS;
5912
6903
  const batch = deps.timerBatchSize;
5913
6904
  this.timerBatchSize = batch !== void 0 && Number.isFinite(batch) && batch >= 1 ? Math.floor(batch) : 500;
5914
6905
  this.eventBufferTtlMs = deps.eventBufferTtlMs || void 0;
6906
+ this.signalIdTtlMs = deps.signalIdTtl != null ? parseDuration(deps.signalIdTtl) : DEFAULT_SIGNAL_ID_TTL_MS;
5915
6907
  this.remoteRedispatchMs = deps.remoteRedispatchMs || void 0;
5916
6908
  this.remoteRedispatchMax = Math.max(1, deps.remoteRedispatchMax ?? 10);
5917
6909
  this.blockedPollMs = deps.blockedPollMs ?? 5e3;
6910
+ this.workerDescriptorCacheMs = deps.workerDescriptorCacheMs ?? 2e3;
5918
6911
  this.cpDescriptor = controlPlaneDescriptor({
5919
6912
  instanceId: `cp-${this.instanceId}`,
5920
6913
  ...this.namespace !== void 0 ? {
@@ -6012,6 +7005,9 @@ var WorkflowEngine = class _WorkflowEngine {
6012
7005
  await this.completeRemoteDecision(decision);
6013
7006
  }
6014
7007
  );
7008
+ if (this.routeReplies && this.sticky.enabled) {
7009
+ this.repliesRouted = this.pool.routeReplies(this.instanceId);
7010
+ }
6015
7011
  const verifyTenant = deps.verifyTenant;
6016
7012
  this.pool.onStartRun(async (message) => {
6017
7013
  let tenant;
@@ -6254,7 +7250,7 @@ var WorkflowEngine = class _WorkflowEngine {
6254
7250
  if (!this.isRegisteredHere(run) && !await this.canResolveWorkflow(run)) {
6255
7251
  const result = await this.settleUnregistered(run, nowMs);
6256
7252
  if (result) settled?.set(run.id, result);
6257
- else if (result === void 0) this.logUnregisteredSkip(run);
7253
+ else if (result === void 0 && !this.isStartOnlyHere(run)) this.logUnregisteredSkip(run);
6258
7254
  continue;
6259
7255
  }
6260
7256
  try {
@@ -6400,6 +7396,34 @@ var WorkflowEngine = class _WorkflowEngine {
6400
7396
  }
6401
7397
  }
6402
7398
  /**
7399
+ * Let {@link start} create runs of `name@version` on this instance without registering a body here:
7400
+ * the run is stored and left for an instance that {@link register}ed the workflow, and this one
7401
+ * never executes it. For a process that only emits work (an API pod) while workers run it.
7402
+ *
7403
+ * Runs this instance picks up anyway (a driving poll, the run queue) are released for another
7404
+ * instance without a log line, and dead-lettered after `unregisteredWorkflowGraceMs` like any run
7405
+ * whose workflow nobody registered. A {@link register} of the same name on this instance wins.
7406
+ */
7407
+ registerStartOnly(name, version, opts) {
7408
+ const entry = {
7409
+ name,
7410
+ version,
7411
+ fn: /* @__PURE__ */ __name(() => {
7412
+ throw new Error(`workflow ${name} is start-only on this instance \u2014 it has no body here`);
7413
+ }, "fn"),
7414
+ tags: opts?.tags,
7415
+ validateInput: opts?.validateInput,
7416
+ origin: opts?.origin
7417
+ };
7418
+ this.startOnly.set(versionKey(name, version), entry);
7419
+ const current = this.startOnlyLatest.get(name);
7420
+ if (!current || isNewerVersion(version, current.version)) this.startOnlyLatest.set(name, entry);
7421
+ }
7422
+ /** Whether `run` is of a workflow this instance only starts (see {@link registerStartOnly}). */
7423
+ isStartOnlyHere(run) {
7424
+ return !this.isRegisteredHere(run) && this.startOnly.has(versionKey(run.workflow, run.workflowVersion));
7425
+ }
7426
+ /**
6403
7427
  * The retained in-process body for `name@version`, for an IN-APP worker that consumes this engine's
6404
7428
  * own group to SERVE a group-served `register(..., { group, executor })` workflow (uniform dispatch):
6405
7429
  * the worker fetches the body by name when it picks up the dispatched {@link WorkflowTask}, replays
@@ -6588,6 +7612,23 @@ var WorkflowEngine = class _WorkflowEngine {
6588
7612
  return deleted;
6589
7613
  }
6590
7614
  /**
7615
+ * Delete the signal-id records (`signal(..., { id })`) that expired by `now`, in batches of
7616
+ * {@link SIGNAL_ID_SWEEP_BATCH}, at most `maxBatches` per call. Expired records already stop
7617
+ * deduplicating; this reclaims their rows. The NestJS timer poller calls it every tick. Returns how
7618
+ * many it deleted; 0 on a store without `deleteExpiredSignalIds`.
7619
+ */
7620
+ async sweepExpiredSignalIds(now = this.clock(), maxBatches = 4) {
7621
+ const { deleteExpiredSignalIds } = this.store;
7622
+ if (!deleteExpiredSignalIds || this.draining) return 0;
7623
+ let deleted = 0;
7624
+ for (let batch = 0; batch < maxBatches; batch++) {
7625
+ const n = await deleteExpiredSignalIds.call(this.store, now, SIGNAL_ID_SWEEP_BATCH);
7626
+ deleted += n;
7627
+ if (n < SIGNAL_ID_SWEEP_BATCH) break;
7628
+ }
7629
+ return deleted;
7630
+ }
7631
+ /**
6591
7632
  * Register a pager hook for STRANDED runs: an in-flight run untouched past `stalledAfterMs` that is
6592
7633
  * either suspended with NO wake timer (wake-forever: nothing will ever re-drive it) or parked on an
6593
7634
  * old pending remote step with no live lease (dispatched and, most likely, lost). The timer poller
@@ -6665,6 +7706,7 @@ var WorkflowEngine = class _WorkflowEngine {
6665
7706
  ...event,
6666
7707
  at: /* @__PURE__ */ new Date()
6667
7708
  };
7709
+ this.settleParentClose(full);
6668
7710
  this.deliver(full);
6669
7711
  if (opts?.lifecycle !== false) {
6670
7712
  this.publishLifecycle(full);
@@ -7921,7 +8963,8 @@ var WorkflowEngine = class _WorkflowEngine {
7921
8963
  async start(workflow, input, runId, opts) {
7922
8964
  const name = workflowName(workflow);
7923
8965
  const pinned = opts?.version;
7924
- let registered = pinned === void 0 ? this.latest.get(name) : this.workflows.get(versionKey(name, pinned));
8966
+ const startOnly = pinned === void 0 ? this.startOnlyLatest.get(name) : this.startOnly.get(versionKey(name, pinned));
8967
+ let registered = pinned === void 0 ? this.latest.get(name) ?? startOnly : this.workflows.get(versionKey(name, pinned)) ?? startOnly;
7925
8968
  if (!registered && pinned === void 0) {
7926
8969
  const ancestor = await this.findRemoteAncestor(runId);
7927
8970
  if (ancestor) registered = this.synthesizeRemoteChild(name, ancestor.version, ancestor);
@@ -8024,7 +9067,10 @@ var WorkflowEngine = class _WorkflowEngine {
8024
9067
  status: "suspended"
8025
9068
  };
8026
9069
  }
8027
- await this.dispatchRun(runId, run.priority, pendingStateKey(run.updatedAt));
9070
+ const runsLocally = this.customDispatcher === void 0 && !this.dispatchesOverTransport;
9071
+ if (!(runsLocally && registered === startOnly)) {
9072
+ await this.dispatchRun(runId, run.priority, pendingStateKey(run.updatedAt));
9073
+ }
8028
9074
  if (this.controlPlane && !this.queuesRunsOverTransport) {
8029
9075
  void this.controlPlane.publishControl({
8030
9076
  kind: "enqueued",
@@ -8214,8 +9260,31 @@ var WorkflowEngine = class _WorkflowEngine {
8214
9260
  return true;
8215
9261
  }
8216
9262
  async resumeRunNow(runId, allowFailed) {
8217
- const run = await this.store.getRun(runId);
9263
+ const claimed = this.claimedRuns.get(runId);
9264
+ if (claimed) {
9265
+ this.claimedRuns.delete(runId);
9266
+ return this.resumeLoadedRun(claimed, allowFailed);
9267
+ }
9268
+ const lockAndGet = this.executionLeases.has(runId) ? void 0 : this.store.tryLockRunAndGet;
9269
+ let locked = false;
9270
+ let run = null;
9271
+ if (lockAndGet) {
9272
+ const nowMs = this.clock();
9273
+ run = await lockAndGet.call(this.store, runId, this.instanceId, nowMs + this.leaseMs, nowMs);
9274
+ locked = run !== null;
9275
+ }
9276
+ run ??= await this.store.getRun(runId);
8218
9277
  if (!run) throw new Error(`run ${runId} not found`);
9278
+ if (!locked) return this.resumeLoadedRun(run, allowFailed);
9279
+ this.lockedForTurn.add(runId);
9280
+ try {
9281
+ return await this.resumeLoadedRun(run, allowFailed);
9282
+ } finally {
9283
+ if (this.lockedForTurn.delete(runId)) await this.releaseLeaseBestEffort(runId);
9284
+ }
9285
+ }
9286
+ async resumeLoadedRun(run, allowFailed) {
9287
+ const runId = run.id;
8219
9288
  if (run.status === "cancelled" || run.status === "completed" || run.status === "dead" || run.status === "failed" && !allowFailed) {
8220
9289
  return {
8221
9290
  runId,
@@ -8397,6 +9466,8 @@ var WorkflowEngine = class _WorkflowEngine {
8397
9466
  */
8398
9467
  beginDrain(spentMs = 0) {
8399
9468
  this.draining = true;
9469
+ this.sticky.clear();
9470
+ this.pool.stopReplies();
8400
9471
  if (this.runConsumerStops.length > 0) {
8401
9472
  this.runBatcher.flush();
8402
9473
  void this.stopRunConsumers();
@@ -8656,7 +9727,7 @@ var WorkflowEngine = class _WorkflowEngine {
8656
9727
  result: settled2,
8657
9728
  dispatch: false
8658
9729
  };
8659
- if (settled2 === void 0) this.logUnregisteredSkip(run);
9730
+ if (settled2 === void 0 && !this.isStartOnlyHere(run)) this.logUnregisteredSkip(run);
8660
9731
  return void 0;
8661
9732
  }
8662
9733
  const settled = await this.countRecovery(run);
@@ -8937,6 +10008,7 @@ var WorkflowEngine = class _WorkflowEngine {
8937
10008
  const run = await this.store.getRun(runId);
8938
10009
  const settled = run ? await this.settleUnregistered(run, nowMs) : null;
8939
10010
  if (settled !== void 0) return settled;
10011
+ if (run && this.isStartOnlyHere(run)) return null;
8940
10012
  }
8941
10013
  throw err;
8942
10014
  });
@@ -9112,7 +10184,13 @@ var WorkflowEngine = class _WorkflowEngine {
9112
10184
  * {@link resumeDueTimers}.
9113
10185
  */
9114
10186
  async runPending(nowMs = this.clock()) {
9115
- const pending = await this.store.listPendingRuns(100, this.namespace);
10187
+ if (this.store.claimPendingRuns && !this.queuesRunsOverTransport && !this.draining) {
10188
+ const room = Math.min(PENDING_POLL_BATCH, LOCAL_RUN_CONCURRENCY - this.inflight.size);
10189
+ if (room <= 0) return [];
10190
+ const claimed = await this.store.claimPendingRuns(room, this.instanceId, nowMs + this.leaseMs, nowMs, this.namespace);
10191
+ return this.runClaimed(claimed);
10192
+ }
10193
+ const pending = await this.store.listPendingRuns(PENDING_POLL_BATCH, this.namespace);
9116
10194
  if (this.queuesRunsOverTransport && !this.draining && pending.length > 0) {
9117
10195
  if (await this.queueOnTransport(pending)) {
9118
10196
  return pending.map((run) => ({
@@ -9202,6 +10280,35 @@ var WorkflowEngine = class _WorkflowEngine {
9202
10280
  * for a pod that does have the workflow — which is exactly what the skew-protection error asks
9203
10281
  * for. Same posture as `publishEvent`'s per-subscriber isolation.
9204
10282
  */
10283
+ /**
10284
+ * Run `claimed` (pending runs this instance just leased, read as locked): every turn starts at
10285
+ * once, and each re-enters the lease taken for it without reading the run again.
10286
+ */
10287
+ async runClaimed(claimed) {
10288
+ const turns = claimed.map(async (run) => {
10289
+ if (this.draining) {
10290
+ await this.releaseLeaseBestEffort(run.id);
10291
+ return void 0;
10292
+ }
10293
+ this.lockedForTurn.add(run.id);
10294
+ this.claimedRuns.set(run.id, run);
10295
+ try {
10296
+ return await this.resumeImplicit(run.id);
10297
+ } catch (error2) {
10298
+ if (error2 instanceof WorkflowNotRegistered) {
10299
+ const settled = await this.settleUnregistered(run, this.clock()).catch(() => void 0);
10300
+ if (settled) return settled;
10301
+ if (this.isStartOnlyHere(run)) return void 0;
10302
+ }
10303
+ console.error(`[nestjs-durable] could not resume run ${run.id} (${run.workflow}@${run.workflowVersion}); leaving it for another instance:`, error2);
10304
+ return void 0;
10305
+ } finally {
10306
+ this.lockedForTurn.delete(run.id);
10307
+ this.claimedRuns.delete(run.id);
10308
+ }
10309
+ });
10310
+ return (await Promise.all(turns)).filter((r) => r !== void 0);
10311
+ }
9205
10312
  async resumeLeased(runs, nowMs = this.clock(), onLocked) {
9206
10313
  if (this.draining) return [];
9207
10314
  const results = [];
@@ -9221,7 +10328,7 @@ var WorkflowEngine = class _WorkflowEngine {
9221
10328
  if (error2 instanceof WorkflowNotRegistered) {
9222
10329
  const settled = await this.settleUnregistered(run, nowMs).catch(() => void 0);
9223
10330
  if (settled) results.push(settled);
9224
- if (settled !== void 0) continue;
10331
+ if (settled !== void 0 || this.isStartOnlyHere(run)) continue;
9225
10332
  }
9226
10333
  console.error(`[nestjs-durable] could not resume run ${run.id} (${run.workflow}@${run.workflowVersion}); leaving it for another instance:`, error2);
9227
10334
  } finally {
@@ -9301,7 +10408,8 @@ var WorkflowEngine = class _WorkflowEngine {
9301
10408
  * due-timer reconcile sweep ({@link reconcileSignalWaiter}) — all three resolve a waiter the exact
9302
10409
  * same way.
9303
10410
  */
9304
- async deliverSignal(waiter, payload, resumeHere = false) {
10411
+ async deliverSignal(waiter, payload, resumeHere = false, committed) {
10412
+ if (isAnyMember(waiter)) return this.deliverToWaitAny(waiter, payload, resumeHere, committed);
9305
10413
  await this.store.saveCheckpoint(instantCheckpoint({
9306
10414
  runId: waiter.runId,
9307
10415
  seq: waiter.seq,
@@ -9314,6 +10422,7 @@ var WorkflowEngine = class _WorkflowEngine {
9314
10422
  // for an ordinary (non-fan) signal/child await.
9315
10423
  parallelGroup: waiter.parallelGroup
9316
10424
  }));
10425
+ committed?.();
9317
10426
  if (await this.fanStillWaiting(waiter, payload)) {
9318
10427
  return {
9319
10428
  runId: waiter.runId,
@@ -9326,6 +10435,47 @@ var WorkflowEngine = class _WorkflowEngine {
9326
10435
  });
9327
10436
  }
9328
10437
  /**
10438
+ * Deliver to a waiter that is one condition of a `ctx.waitForAny` (see `wait-any.ts`): settle the
10439
+ * wait with this condition through the checkpoint's compare-and-set, then wake the run. A signal
10440
+ * that loses — the wait was settled by another condition first, or a timer of the wait came due
10441
+ * before it arrived (the timer then wins) — is not consumed: it is sent again on its token, where
10442
+ * it is buffered for the next wait (or reaches a waiter registered since).
10443
+ */
10444
+ async deliverToWaitAny(waiter, payload, resumeHere, committed) {
10445
+ const cp = await this.store.getCheckpoint(waiter.runId, waiter.seq);
10446
+ const conditions = cp && cp.status === "pending" && isAnyCheckpoint(cp) ? anyConditionsOf(cp) : [];
10447
+ const condition = conditions.find((c) => anyConditionToken(c) === waiter.token);
10448
+ const run = cp && condition ? await this.store.getRun(waiter.runId) : null;
10449
+ if (!cp || !condition || !run || TERMINAL_RUN_STATUSES.includes(run.status)) {
10450
+ if (cp && condition) await removeAnyWaiters(this.store, cp);
10451
+ await this.redeliverLosingSignal(waiter.token, payload);
10452
+ committed?.();
10453
+ return null;
10454
+ }
10455
+ const due = dueTimer(conditions, this.clock());
10456
+ const done = due ? resolvedAnyCheckpoint(cp, due, null) : resolvedAnyCheckpoint(cp, condition, payload);
10457
+ const won = await claimAnyCheckpoint(this.store, done);
10458
+ if (won) await removeAnyWaiters(this.store, done);
10459
+ if (!won || due) await this.redeliverLosingSignal(waiter.token, payload);
10460
+ committed?.();
10461
+ if (!won) return null;
10462
+ return (resumeHere ? this.resumeImplicit(waiter.runId) : this.wake(waiter.runId)).catch((err) => {
10463
+ if (err instanceof NamespaceMismatch) return null;
10464
+ throw err;
10465
+ });
10466
+ }
10467
+ /**
10468
+ * Put back a signal a `waitForAny` condition took but did not win with. A child's completion is
10469
+ * kept only where a parent can still consume it, as {@link deliverToParent} decides.
10470
+ */
10471
+ async redeliverLosingSignal(token, payload) {
10472
+ if (token.startsWith(CHILD_TOKEN_PREFIX)) {
10473
+ const childId = token.slice(CHILD_TOKEN_PREFIX.length);
10474
+ if (!await childCompletionNeedsBuffer(this.store, childId, payload)) return;
10475
+ }
10476
+ await this.deliverOrBuffer(token, payload);
10477
+ }
10478
+ /**
9329
10479
  * The `ctx.all` join barrier: true when `waiter` was one child of a fan (or of one wave of it)
9330
10480
  * whose siblings are still out, so waking the parent now would only replay the fan and re-suspend.
9331
10481
  * Skipping that wake is what keeps a fan of N children at O(N) work instead of N replays of N items.
@@ -9458,7 +10608,7 @@ var WorkflowEngine = class _WorkflowEngine {
9458
10608
  return;
9459
10609
  }
9460
10610
  const settled = new Set(checkpoints.filter((cp) => cp.status === "completed").map((cp) => cp.seq));
9461
- const awaited = waiters.filter((w) => w.token.startsWith("child:") && !settled.has(w.seq)).map((w) => ({
10611
+ const awaited = waiters.filter((w) => w.token.startsWith("child:") && !settled.has(w.seq) && !isAnyMember(w)).map((w) => ({
9462
10612
  childId: w.token.slice("child:".length),
9463
10613
  seq: w.seq,
9464
10614
  parallelGroup: w.parallelGroup
@@ -9549,10 +10699,51 @@ var WorkflowEngine = class _WorkflowEngine {
9549
10699
  * Either way, the destructive `take*` ops (on the waiter row, on the buffer) arbitrate: whichever
9550
10700
  * side actually removes the row is the one that delivers, so a signal is never delivered twice.
9551
10701
  */
9552
- async signal(token, payload) {
10702
+ async signal(token, payload, opts) {
10703
+ const id = opts?.id;
10704
+ if (id === void 0) return this.deliverOrBuffer(token, payload);
10705
+ if (!this.store.claimSignalId) {
10706
+ throw new Error("engine.signal(..., { id }) needs a store that deduplicates signals (claimSignalId); the bundled stores do");
10707
+ }
10708
+ const key = `${token}\0${id}`;
10709
+ for (let prior = this.signalIdSends.get(key); prior; prior = this.signalIdSends.get(key)) {
10710
+ const sent = await prior.then(() => true, () => false);
10711
+ if (sent) return null;
10712
+ }
10713
+ const send = this.signalOnce(token, payload, id, opts?.idTtl);
10714
+ this.signalIdSends.set(key, send);
10715
+ try {
10716
+ return await send;
10717
+ } finally {
10718
+ if (this.signalIdSends.get(key) === send) this.signalIdSends.delete(key);
10719
+ }
10720
+ }
10721
+ /** One deduplicated {@link signal}: claim `id`, then deliver or buffer; a failure forgets the id. */
10722
+ async signalOnce(token, payload, id, idTtl) {
10723
+ const claimSignalId = this.store.claimSignalId;
10724
+ if (!claimSignalId) return null;
10725
+ const now = this.clock();
10726
+ const ttl = idTtl !== void 0 ? parseDuration(idTtl) : this.signalIdTtlMs;
10727
+ if (!await claimSignalId.call(this.store, token, id, now + ttl, now)) return null;
10728
+ let committed = false;
10729
+ try {
10730
+ return await this.deliverOrBuffer(token, payload, () => {
10731
+ committed = true;
10732
+ });
10733
+ } catch (err) {
10734
+ if (!committed) await this.store.releaseSignalId?.(token, id).catch(() => void 0);
10735
+ throw err;
10736
+ }
10737
+ }
10738
+ /**
10739
+ * {@link signal} without deduplication: deliver to the waiter on `token`, or buffer. `committed`
10740
+ * is called once the signal is durably delivered or buffered.
10741
+ */
10742
+ async deliverOrBuffer(token, payload, committed) {
9553
10743
  const waiter = await this.store.takeSignalWaiter(token);
9554
- if (waiter) return this.deliverSignal(waiter, payload);
10744
+ if (waiter) return this.deliverSignal(waiter, payload, false, committed);
9555
10745
  await this.store.bufferSignal(token, payload);
10746
+ committed?.();
9556
10747
  const lateWaiter = await this.store.takeSignalWaiter(token);
9557
10748
  if (!lateWaiter) return null;
9558
10749
  const reclaimed = await this.store.takeBufferedSignal(token);
@@ -9914,10 +11105,32 @@ var WorkflowEngine = class _WorkflowEngine {
9914
11105
  */
9915
11106
  async cancelChildren(parentRunId, opts) {
9916
11107
  for (const id of await this.getRunChildren(parentRunId)) {
11108
+ const child = await this.store.getRun(id).catch(() => null);
11109
+ if (child?.tags?.includes(`${PARENT_CLOSE_TAG_PREFIX}abandon`)) continue;
9917
11110
  await this.cancel(id, opts).catch(() => void 0);
9918
11111
  }
9919
11112
  }
9920
11113
  /**
11114
+ * Apply the `parentClose` policies a parent's turn recorded (see {@link ChildCallOptions.parentClose})
11115
+ * when that parent completes or fails; forget them when it suspends (its next turn, here or on
11116
+ * another instance, records them again). Local events only: this is the instance that ran the turn.
11117
+ */
11118
+ settleParentClose(event) {
11119
+ if (event.type !== "run.completed" && event.type !== "run.failed" && event.type !== "run.suspended") {
11120
+ return;
11121
+ }
11122
+ const intents = this.parentCloseIntents.get(event.runId);
11123
+ if (!intents) return;
11124
+ this.parentCloseIntents.delete(event.runId);
11125
+ if (event.type === "run.suspended") return;
11126
+ for (const [childId, policy] of intents) {
11127
+ if (policy === "abandon") continue;
11128
+ queueMicrotask(() => void this.cancel(childId, {
11129
+ compensate: policy === "cancel"
11130
+ }).catch(() => void 0));
11131
+ }
11132
+ }
11133
+ /**
9921
11134
  * The ids of the runs a run spawned — both awaited (`ctx.child`) and fire-and-forget
9922
11135
  * (`ctx.startChild`, found via its `spawn:<id>` checkpoint). The canonical parent→children edge,
9923
11136
  * used for both cancellation cascades and the dashboard run-tree.
@@ -9934,7 +11147,7 @@ var WorkflowEngine = class _WorkflowEngine {
9934
11147
  async awaitedChildIds(parentRunId) {
9935
11148
  return (await listSignalWaitersOfRuns(this.store, [
9936
11149
  parentRunId
9937
- ])).filter((w) => w.token.startsWith("child:")).map((w) => w.token.slice("child:".length));
11150
+ ])).filter((w) => w.token.startsWith("child:") && !isAnyMember(w)).map((w) => w.token.slice("child:".length));
9938
11151
  }
9939
11152
  async getRunChildren(parentRunId) {
9940
11153
  const childIds = new Set(await this.awaitedChildIds(parentRunId));
@@ -10222,15 +11435,19 @@ var WorkflowEngine = class _WorkflowEngine {
10222
11435
  void this.announcedSubscriptions();
10223
11436
  const tags = singleton ? withSingletonTracked(run.tags) : run.tags;
10224
11437
  const retag = tags !== run.tags;
10225
- await this.store.updateRun(run.id, {
11438
+ const started = await (this.store.updateRunIf ?? this.updateRunIfFallback).call(this.store, run.id, [
11439
+ "pending"
11440
+ ], {
10226
11441
  status: "running",
10227
11442
  ...retag ? {
10228
11443
  tags
10229
11444
  } : {},
10230
11445
  updatedAt: /* @__PURE__ */ new Date()
10231
11446
  });
11447
+ if (!started) return false;
10232
11448
  run.status = "running";
10233
11449
  if (retag) run.tags = tags;
11450
+ return true;
10234
11451
  }
10235
11452
  async singletonWaitResult(run) {
10236
11453
  const current = await this.store.getRun(run.id);
@@ -10300,7 +11517,7 @@ var WorkflowEngine = class _WorkflowEngine {
10300
11517
  if (result.status === "suspended") {
10301
11518
  this.inBackground(this.honorWakeRequests(run.id, settledFans, served));
10302
11519
  } else if (TERMINAL_RUN_STATUSES.includes(result.status)) {
10303
- this.inBackground(this.listWakeRequests(run.id, true).then((kept) => this.dropWakeRequests(kept)));
11520
+ this.inBackground(this.listWakeRequests(run.id).then((kept) => this.dropWakeRequests(kept)));
10304
11521
  }
10305
11522
  return result;
10306
11523
  } finally {
@@ -10351,7 +11568,7 @@ var WorkflowEngine = class _WorkflowEngine {
10351
11568
  * the timer poll of another instance runs it once this instance's hold on it would have lapsed.
10352
11569
  */
10353
11570
  async honorWakeRequests(runId, settledFans, served) {
10354
- const kept = await this.listWakeRequests(runId, true);
11571
+ const kept = await this.listWakeRequests(runId);
10355
11572
  const seen = /* @__PURE__ */ __name((request) => wakeSeen(request, settledFans, served), "seen");
10356
11573
  const needed = kept.filter((request) => !seen(request));
10357
11574
  await this.dropWakeRequests(kept.filter(seen));
@@ -10361,30 +11578,17 @@ var WorkflowEngine = class _WorkflowEngine {
10361
11578
  if (!(err instanceof NamespaceMismatch)) throw err;
10362
11579
  });
10363
11580
  }
10364
- /**
10365
- * The wake requests kept for `runId`. With `legacy`, also those an older engine kept as buffered
10366
- * signals, which are taken as they are read.
10367
- */
10368
- async listWakeRequests(runId, legacy = false) {
10369
- const token = wakeRequestToken(runId);
10370
- const kept = (await this.store.listBufferedEvents(token, WAKE_REQUEST_SCAN)).map((request) => ({
11581
+ /** The wake requests kept for `runId`. */
11582
+ async listWakeRequests(runId) {
11583
+ const kept = await this.store.listBufferedEvents(wakeRequestToken(runId), WAKE_REQUEST_SCAN);
11584
+ return kept.map((request) => ({
10371
11585
  id: request.id,
10372
11586
  payload: request.payload
10373
11587
  }));
10374
- if (!legacy) return kept;
10375
- for (; ; ) {
10376
- const signal = await this.store.takeBufferedSignal(token);
10377
- if (!signal) return kept;
10378
- kept.push({
10379
- payload: signal.payload
10380
- });
10381
- }
10382
11588
  }
10383
- /** Delete `requests` (the ones kept as buffered events). */
11589
+ /** Delete `requests`. */
10384
11590
  async dropWakeRequests(requests) {
10385
- await Promise.all(requests.flatMap((request) => request.id === void 0 ? [] : [
10386
- this.store.removeBufferedEvent(request.id)
10387
- ]));
11591
+ await Promise.all(requests.map((request) => this.store.removeBufferedEvent(request.id)));
10388
11592
  }
10389
11593
  /**
10390
11594
  * Record, for the turn executing `runId` here, the wakes kept for the run so far. Called before the
@@ -10790,12 +11994,15 @@ ${run.workflowVersion}`;
10790
11994
  };
10791
11995
  }
10792
11996
  if (outcome.kind === "blocked") return this.parkBlocked(run, outcome.blocked);
11997
+ const stamp = new Date(Math.max(updatedAt.getTime(), (run.updatedAt?.getTime() ?? 0) + 1));
10793
11998
  const applied = await (this.store.updateRunIf ?? this.updateRunIfFallback).call(this.store, run.id, SUSPEND_ELIGIBLE_STATUSES, await this.withRunTrace(run, {
10794
11999
  status: "suspended",
10795
12000
  wakeAt: this.reconcileWakeAt(outcome.wakeAt),
10796
- updatedAt
12001
+ updatedAt: stamp
10797
12002
  }));
10798
12003
  if (!applied) return this.echoCurrentStatus(run.id);
12004
+ run.updatedAt = stamp;
12005
+ this.suspendStamps.set(run.id, stamp.getTime());
10799
12006
  await this.keepDueForKeptWakes(run.id);
10800
12007
  this.emit({
10801
12008
  type: "run.suspended",
@@ -10855,7 +12062,10 @@ ${run.workflowVersion}`;
10855
12062
  }
10856
12063
  const remote = registered.remote;
10857
12064
  if (run.status === "pending") {
10858
- await this.markStarted(run, registered?.singleton !== void 0);
12065
+ if (!await this.markStarted(run, registered?.singleton !== void 0)) {
12066
+ await this.releaseLeaseBestEffort(run.id);
12067
+ return await this.echoCurrentStatus(run.id);
12068
+ }
10859
12069
  this.emit({
10860
12070
  type: "run.started",
10861
12071
  runId: run.id,
@@ -10867,7 +12077,9 @@ ${run.workflowVersion}`;
10867
12077
  if (registered.singleton && !await this.singletons.admit(run, registered.singleton)) {
10868
12078
  return this.singletonWaitResult(run);
10869
12079
  }
10870
- const wfPlan = await this.planRoute(tenantGroup(sanitizeQueueToken(remote.group), void 0), registered.requires);
12080
+ const wfToken = tenantGroup(sanitizeQueueToken(remote.group), void 0);
12081
+ let wfPlan = await this.planRoute(wfToken, registered.requires);
12082
+ if (wfPlan?.status === "blocked") wfPlan = await this.planRoute(wfToken, registered.requires, true);
10871
12083
  if (wfPlan?.status === "blocked") return this.parkBlocked(run, wfPlan);
10872
12084
  const checkpoints = await this.replayCheckpoints(run.id);
10873
12085
  await this.deliverFinishedChildren(run.id, checkpoints);
@@ -10996,6 +12208,7 @@ ${run.workflowVersion}`;
10996
12208
  kind: "suspended",
10997
12209
  wakeAt
10998
12210
  });
12211
+ this.suspendStamps.delete(run.id);
10999
12212
  await this.redriveIfTurnSawStaleHistory(run.id, decision.commands);
11000
12213
  return suspended;
11001
12214
  }
@@ -11249,6 +12462,10 @@ ${run.workflowVersion}`;
11249
12462
  for (const cp of checkpoints) {
11250
12463
  if (cp.status === "completed" || cp.status === "failed") {
11251
12464
  if (cp.seq >= HANDOFF_SEQ_BASE) continue;
12465
+ if (isAnyCheckpoint(cp)) {
12466
+ events.push(anyHistoryEvent(cp));
12467
+ continue;
12468
+ }
11252
12469
  if (cp.kind === "signal" && cp.name.startsWith("signal:child:")) {
11253
12470
  const completion = cp.output;
11254
12471
  events.push({
@@ -11416,6 +12633,16 @@ ${run.workflowVersion}`;
11416
12633
  await deliverBuffered(lateBuffered.payload);
11417
12634
  }
11418
12635
  }
12636
+ } else if (cmd.kind === "waitAny") {
12637
+ const state = await enterWaitAny({
12638
+ store: this.store,
12639
+ clock: this.clock
12640
+ }, run.id, cmd.seq, cmd.name, cmd.conditions);
12641
+ if (state.state === "resolved") {
12642
+ setTimeout(() => void this.resumeImplicit(run.id).catch(() => void 0), 0);
12643
+ } else if (state.wakeAt != null) {
12644
+ wakeAt = wakeAt == null ? state.wakeAt : Math.min(wakeAt, state.wakeAt);
12645
+ }
11419
12646
  } else if (cmd.kind === "startChild") {
11420
12647
  const childId = `${run.id}.child.${cmd.seq}`;
11421
12648
  await this.store.putSignalWaiter({
@@ -11514,7 +12741,10 @@ ${run.workflowVersion}`;
11514
12741
  async runExecutionTurn(run, fn, registered) {
11515
12742
  if (run.status === "cancelling") this.cancelRequested.add(run.id);
11516
12743
  if (run.status === "pending") {
11517
- await this.markStarted(run, registered?.singleton !== void 0);
12744
+ if (!await this.markStarted(run, registered?.singleton !== void 0)) {
12745
+ await this.releaseLeaseBestEffort(run.id);
12746
+ return await this.echoCurrentStatus(run.id);
12747
+ }
11518
12748
  this.emit({
11519
12749
  type: "run.started",
11520
12750
  runId: run.id,
@@ -11527,24 +12757,52 @@ ${run.workflowVersion}`;
11527
12757
  if (run.status !== "cancelling" && registered?.singleton && !await this.singletons.admit(run, registered.singleton)) {
11528
12758
  return this.singletonWaitResult(run);
11529
12759
  }
11530
- const compensations = [];
11531
- const snapshot = await this.replayCheckpoints(run.id);
11532
- await this.deliverFinishedChildren(run.id, snapshot);
11533
- const replay = /* @__PURE__ */ new Map();
12760
+ const stickyOk = this.stickyEligible(run);
12761
+ let cont = stickyOk ? this.takeContinuation(run, fn) : void 0;
12762
+ const warm = cont !== void 0;
12763
+ const parkedFresh = cont ? await this.readParked(run.id, cont) : void 0;
12764
+ const compensations = cont ? cont.compensations : [];
12765
+ const snapshot = cont ? [] : await this.replayCheckpoints(run.id);
12766
+ if (!cont) await this.deliverFinishedChildren(run.id, snapshot);
12767
+ const replay = cont ? cont.replay : /* @__PURE__ */ new Map();
11534
12768
  for (const cp of snapshot) replay.set(cp.seq, cp);
12769
+ if (!cont && stickyOk) {
12770
+ cont = new StickyContinuation(run.id, fn, run.workflowVersion, compensations, replay, (c) => this.sticky.evict(c.runId));
12771
+ }
12772
+ if (cont) this.turnContinuations.set(run.id, cont);
12773
+ let parkedTurn = false;
11535
12774
  let suspendedOnFan;
11536
12775
  let suspendedOnSteps = false;
11537
12776
  const awaitedSteps = /* @__PURE__ */ new Set();
11538
12777
  this.awaitedRemoteSteps.set(run.id, awaitedSteps);
11539
12778
  const awaitedFans = [];
11540
12779
  this.awaitedStepFans.set(run.id, awaitedFans);
11541
- const ctx = createWorkflowCtx(this.ctxHostFor(replay, run.namespace), run.id, compensations, run.workflow, registered?.searchAttributesSchema);
12780
+ const ctx = warm ? void 0 : createWorkflowCtx(this.ctxHostFor(replay, run.namespace, cont), run.id, compensations, run.workflow, registered?.searchAttributesSchema);
11542
12781
  try {
11543
12782
  const handoff = liveHandoff(run.id, snapshot);
11544
12783
  if (handoff && !this.cancelRequested.has(run.id)) {
11545
12784
  return await this.settleRun(run, await this.followHandoff(run, handoff));
11546
12785
  }
11547
- const output = await runInWorkflowCtx(ctx, () => fn(ctx, run.input));
12786
+ let output;
12787
+ if (cont) {
12788
+ if (parkedFresh) {
12789
+ cont.attached = true;
12790
+ await cont.resumeParked(parkedFresh);
12791
+ } else {
12792
+ const body = ctx;
12793
+ cont.begin(() => runInWorkflowCtx(body, () => fn(body, run.input)));
12794
+ }
12795
+ const outcome = await cont.next();
12796
+ if (outcome.kind === "parked") {
12797
+ parkedTurn = true;
12798
+ throw new WorkflowSuspended(outcome.wakeAt);
12799
+ }
12800
+ if (outcome.kind === "threw") throw outcome.error;
12801
+ output = outcome.value;
12802
+ } else {
12803
+ const body = ctx;
12804
+ output = await runInWorkflowCtx(body, () => fn(body, run.input));
12805
+ }
11548
12806
  if (this.cancelRequested.has(run.id)) {
11549
12807
  return await this.compensateAndCancel(run, compensations, replay);
11550
12808
  }
@@ -11602,6 +12860,11 @@ ${run.workflowVersion}`;
11602
12860
  });
11603
12861
  suspendedOnFan = err.fan;
11604
12862
  suspendedOnSteps = suspended.status === "suspended";
12863
+ const stamp = this.suspendStamps.get(run.id);
12864
+ this.suspendStamps.delete(run.id);
12865
+ if (parkedTurn && cont && suspendedOnSteps && stamp !== void 0) {
12866
+ this.keepContinuation(cont, stamp);
12867
+ }
11605
12868
  return suspended;
11606
12869
  }
11607
12870
  if (this.cancelRequested.has(run.id)) {
@@ -11626,6 +12889,8 @@ ${run.workflowVersion}`;
11626
12889
  this.awaitedRemoteSteps.delete(run.id);
11627
12890
  }
11628
12891
  if (this.awaitedStepFans.get(run.id) === awaitedFans) this.awaitedStepFans.delete(run.id);
12892
+ if (cont && this.turnContinuations.get(run.id) === cont) this.turnContinuations.delete(run.id);
12893
+ this.suspendStamps.delete(run.id);
11629
12894
  await this.releaseLeaseBestEffort(run.id);
11630
12895
  if (suspendedOnFan) {
11631
12896
  this.inBackground(this.redriveSettledChildren(run.id, suspendedOnFan));
@@ -11843,10 +13108,11 @@ ${run.workflowVersion}`;
11843
13108
  /** The seam handed to {@link createWorkflowCtx}: the authoring API reaches durability + lifecycle
11844
13109
  * (checkpointing, dispatch, child start) through this, so the ctx primitives live in their own
11845
13110
  * module and the engine stays the orchestrator. */
11846
- ctxHostFor(replay, parentNamespace) {
13111
+ ctxHostFor(replay, parentNamespace, sticky) {
11847
13112
  return {
11848
13113
  store: this.store,
11849
13114
  replay,
13115
+ sticky,
11850
13116
  clock: this.clock,
11851
13117
  webhookUrl: this.webhookUrl,
11852
13118
  startStep: /* @__PURE__ */ __name((s) => this.startStep(s), "startStep"),
@@ -11855,9 +13121,27 @@ ${run.workflowVersion}`;
11855
13121
  // Route the dispatch by the RUN's tenant (same reason as `startChild` below): the executing
11856
13122
  // engine may be an operator driving every namespace, so the token must come from the run, not
11857
13123
  // from this engine. See {@link stepGroup}.
11858
- callRemote: /* @__PURE__ */ __name((runId, seq, step, input, queue, transport, admission) => this.callRemote(runId, seq, step, input, queue, transport, replay, admission, {
11859
- namespace: parentNamespace
11860
- }), "callRemote"),
13124
+ callRemote: /* @__PURE__ */ __name((runId, seq, step, input, queue, transport, admission) => {
13125
+ const routing = {
13126
+ namespace: parentNamespace
13127
+ };
13128
+ const call = this.callRemote(runId, seq, step, input, queue, transport, replay, admission, {
13129
+ namespace: parentNamespace
13130
+ });
13131
+ if (!sticky) return call;
13132
+ return call.catch((err) => {
13133
+ if (!(err instanceof WorkflowSuspended) || !sticky.attached) throw err;
13134
+ return sticky.park(seq, err.wakeAt, (fresh) => this.callRemote(runId, seq, step, input, queue, transport, fresh ? /* @__PURE__ */ new Map([
13135
+ [
13136
+ seq,
13137
+ fresh
13138
+ ]
13139
+ ]) : void 0, admission, {
13140
+ ...routing,
13141
+ fresh: true
13142
+ }));
13143
+ });
13144
+ }, "callRemote"),
11861
13145
  callRemoteAll: /* @__PURE__ */ __name((runId, positions, step, inputs, opts) => this.callRemoteAll(runId, positions, step, inputs, opts, {
11862
13146
  namespace: parentNamespace
11863
13147
  }, replay), "callRemoteAll"),
@@ -11867,15 +13151,29 @@ ${run.workflowVersion}`;
11867
13151
  // namespace) is the one executing the parent. Otherwise an operator recovery-resuming a
11868
13152
  // `davi-local` pipeline would stamp its `processing` child `default` and it would leak off the
11869
13153
  // tenant's worker pool onto the shared/dev workers.
11870
- startChild: /* @__PURE__ */ __name((workflow, input, id, priority, version) => {
13154
+ startChild: /* @__PURE__ */ __name((workflow, input, id, priority, version, parentClose) => {
11871
13155
  this.startChildDeferred(workflow, input, id, {
11872
13156
  priority,
11873
13157
  namespace: parentNamespace,
11874
13158
  // Absent unless the body pinned one (`ctx.child(ref, input, { version })`) — an omitted
11875
13159
  // pin must stay omitted, so the child keeps resolving `latest` exactly as before.
11876
- version
13160
+ version,
13161
+ // The policy travels with the child, so a cancel cascade run by ANY instance can read it.
13162
+ ...parentClose ? {
13163
+ tags: [
13164
+ `${PARENT_CLOSE_TAG_PREFIX}${parentClose}`
13165
+ ]
13166
+ } : {}
11877
13167
  });
11878
13168
  }, "startChild"),
13169
+ noteParentClose: /* @__PURE__ */ __name((parentRunId, childId, policy) => {
13170
+ let intents = this.parentCloseIntents.get(parentRunId);
13171
+ if (!intents) {
13172
+ intents = /* @__PURE__ */ new Map();
13173
+ this.parentCloseIntents.set(parentRunId, intents);
13174
+ }
13175
+ intents.set(childId, policy);
13176
+ }, "noteParentClose"),
11879
13177
  // Deferred for the same reentrancy reason as `startChild` above. `cancel()` is already
11880
13178
  // idempotent on a terminal/cancelled run (returns its existing status without side effects), so
11881
13179
  // no extra guard is needed here for the failFast replay case (re-issuing the same cancel calls).
@@ -11985,17 +13283,35 @@ ${run.workflowVersion}`;
11985
13283
  * - a `routable` plan — ≥1 live worker is capability-capable AND protocol-compatible (dispatch);
11986
13284
  * - a `blocked` plan — descriptors exist but none can run it (park the run, never dispatch into it).
11987
13285
  */
11988
- async planRoute(token, requires) {
11989
- const descriptors = await this.pool.readWorkerDescriptors(token);
13286
+ async planRoute(token, requires, fresh = false) {
13287
+ const descriptors = await this.liveWorkerDescriptors(token, fresh);
11990
13288
  if (descriptors.length === 0) return void 0;
11991
13289
  return planDispatch(requires ?? [], descriptors, this.cpDescriptor, token);
11992
13290
  }
13291
+ /** The live descriptors for `token`, reused for {@link workerDescriptorCacheMs}; one read in flight. */
13292
+ liveWorkerDescriptors(token, fresh = false) {
13293
+ if (this.workerDescriptorCacheMs <= 0) return this.pool.readWorkerDescriptors(token);
13294
+ const now = Date.now();
13295
+ const cached = this.descriptorCache.get(token);
13296
+ if (!fresh && cached && now - cached.at < this.workerDescriptorCacheMs) return cached.read;
13297
+ const read = this.pool.readWorkerDescriptors(token);
13298
+ const entry = {
13299
+ at: now,
13300
+ read
13301
+ };
13302
+ this.descriptorCache.set(token, entry);
13303
+ read.catch(() => {
13304
+ if (this.descriptorCache.get(token) === entry) this.descriptorCache.delete(token);
13305
+ });
13306
+ return read;
13307
+ }
11993
13308
  /** Throw {@link WorkflowBlocked} when no live worker can run a dispatch to `token` — so the caller's
11994
13309
  * run-execution catch parks the run `blocked` (with the plan) instead of persisting a `pending`
11995
13310
  * checkpoint + dispatching into a queue nobody consumes (design §7.5). A no-op when the guard does
11996
13311
  * not engage (no descriptors) or a capable+compatible worker exists. */
11997
13312
  async ensureRoutable(token, requires) {
11998
- const plan = await this.planRoute(token, requires);
13313
+ let plan = await this.planRoute(token, requires);
13314
+ if (plan?.status === "blocked") plan = await this.planRoute(token, requires, true);
11999
13315
  if (plan?.status === "blocked") throw new WorkflowBlocked(plan);
12000
13316
  }
12001
13317
  /**
@@ -12053,8 +13369,11 @@ ${run.workflowVersion}`;
12053
13369
  }
12054
13370
  async callRemote(runId, seq, step, input, queue, transport, replay, admission, routing) {
12055
13371
  const group = stepGroup(step, routing?.namespace);
13372
+ if (replay !== void 0 && !replay.has(seq) && this.store.insertCheckpointIfAbsent && !step.timeoutMs && this.pool.size > 0 && !(queue && this.admission.handles(queue)) && await this.dispatchIfAbsent(runId, seq, step, input, group, transport, admission, routing)) {
13373
+ throw new WorkflowSuspended();
13374
+ }
12056
13375
  let existing = replay?.get(seq) ?? await this.store.getCheckpoint(runId, seq);
12057
- if (existing?.status === "pending" && replay?.has(seq)) {
13376
+ if (existing?.status === "pending" && replay?.has(seq) && !routing?.fresh) {
12058
13377
  existing = await this.store.getCheckpoint(runId, seq) ?? existing;
12059
13378
  }
12060
13379
  if (existing && existing.name !== step.name) {
@@ -12114,7 +13433,9 @@ ${run.workflowVersion}`;
12114
13433
  input: existing.input,
12115
13434
  priority: admission?.priority,
12116
13435
  attempt: reAttempt,
12117
- transport
13436
+ transport,
13437
+ // A fan's steps are joined by its barrier, not a parked body: their results stay shared.
13438
+ replyHere: routing?.parallelGroup === void 0
12118
13439
  });
12119
13440
  this.emit({
12120
13441
  type: "step.started",
@@ -12186,7 +13507,8 @@ ${run.workflowVersion}`;
12186
13507
  input,
12187
13508
  priority: admission?.priority,
12188
13509
  attempt,
12189
- transport
13510
+ transport,
13511
+ replyHere: routing?.parallelGroup === void 0
12190
13512
  });
12191
13513
  this.emit({
12192
13514
  type: "step.started",
@@ -12199,6 +13521,61 @@ ${run.workflowVersion}`;
12199
13521
  throw new WorkflowSuspended();
12200
13522
  }
12201
13523
  /**
13524
+ * {@link callRemote}'s first dispatch of a step, written with
13525
+ * {@link StateStore.insertCheckpointIfAbsent}: resolves whether it wrote the `pending` row and
13526
+ * dispatched. `false` when the position already has a row, or when no live worker can take the
13527
+ * step (the read-first path then decides, so a step that is in fact done is never blocked).
13528
+ */
13529
+ async dispatchIfAbsent(runId, seq, step, input, group, transport, admission, routing) {
13530
+ try {
13531
+ await this.ensureRoutable(group, step.requires);
13532
+ } catch (err) {
13533
+ if (err instanceof WorkflowBlocked) return false;
13534
+ throw err;
13535
+ }
13536
+ const id = stepId(runId, seq);
13537
+ const enqueuedAt = /* @__PURE__ */ new Date();
13538
+ const wrote = await this.store.insertCheckpointIfAbsent.call(this.store, {
13539
+ runId,
13540
+ seq,
13541
+ name: step.name,
13542
+ kind: "remote",
13543
+ stepId: id,
13544
+ status: "pending",
13545
+ input,
13546
+ attempts: 1,
13547
+ workerGroup: group,
13548
+ ...routing?.parallelGroup !== void 0 ? {
13549
+ parallelGroup: routing.parallelGroup
13550
+ } : {},
13551
+ enqueuedAt,
13552
+ startedAt: enqueuedAt,
13553
+ finishedAt: enqueuedAt
13554
+ });
13555
+ if (!wrote) return false;
13556
+ await this.dispatchRemoteTask({
13557
+ runId,
13558
+ seq,
13559
+ name: step.name,
13560
+ stepId: id,
13561
+ group,
13562
+ input,
13563
+ priority: admission?.priority,
13564
+ attempt: 1,
13565
+ transport,
13566
+ replyHere: routing?.parallelGroup === void 0
13567
+ });
13568
+ this.emit({
13569
+ type: "step.started",
13570
+ runId,
13571
+ seq,
13572
+ name: step.name,
13573
+ kind: "remote"
13574
+ });
13575
+ this.awaitedRemoteSteps.get(runId)?.add(seq);
13576
+ return true;
13577
+ }
13578
+ /**
12202
13579
  * Whether a `pending` step a turn just read only waits for its result: {@link callRemote} would
12203
13580
  * suspend on it without writing (no redispatch deadline to stamp, the one stamped not yet due).
12204
13581
  */
@@ -12454,6 +13831,69 @@ ${run.workflowVersion}`;
12454
13831
  }
12455
13832
  }
12456
13833
  /**
13834
+ * Whether this turn of `run` may run in a sticky continuation (see `sticky-continuation.ts`). Not
13835
+ * while the engine drains, for a cancel being compensated, nor when execution wrappers are
13836
+ * registered: a resumed body runs in the async scope of the turn that started it, and a wrapper
13837
+ * (a span, a log batch) expects one scope per turn.
13838
+ */
13839
+ stickyEligible(run) {
13840
+ return this.sticky.enabled && !this.draining && run.status !== "cancelling" && !this.cancelRequested.has(run.id) && !hasDurableExecutionWrappers();
13841
+ }
13842
+ /**
13843
+ * The continuation this instance parked for `run`, if it is still the run's latest turn: the run
13844
+ * is `suspended` with the very stamp this instance's suspend wrote, on the same body and version.
13845
+ * Any other settle in between (another instance's turn, a retry, a cancel) changed the stamp, and
13846
+ * the run replays instead. Taking it removes it from the cache.
13847
+ */
13848
+ takeContinuation(run, fn) {
13849
+ const cont = this.sticky.take(run.id);
13850
+ if (!cont) return void 0;
13851
+ const valid = cont.fn === fn && cont.workflowVersion === run.workflowVersion && run.status === "suspended" && cont.stamp !== void 0 && run.updatedAt?.getTime() === cont.stamp;
13852
+ if (!valid) {
13853
+ this.sticky.misses += 1;
13854
+ return void 0;
13855
+ }
13856
+ this.sticky.hits += 1;
13857
+ return cont;
13858
+ }
13859
+ /** Keep `cont` (parked, detached) for the run's next turn here, stamped with its suspend write. */
13860
+ keepContinuation(cont, stamp) {
13861
+ if (cont.unusable) return;
13862
+ cont.stamp = stamp;
13863
+ cont.replay.clear();
13864
+ this.sticky.put(cont);
13865
+ }
13866
+ /**
13867
+ * The checkpoints of the steps `cont` is parked on, as a replay would see them: in one read when
13868
+ * the store batches, and without the input of a step that completed (what
13869
+ * {@link StateStore.getCheckpointsForSettle} leaves out). A step still open or failed is read
13870
+ * whole, since a re-dispatch or a retry sends its input again.
13871
+ */
13872
+ async readParked(runId, cont) {
13873
+ const fresh = /* @__PURE__ */ new Map();
13874
+ for (const seq of cont.parked.keys()) fresh.set(seq, cont.hints.get(seq) ?? null);
13875
+ cont.hints.clear();
13876
+ const seqs = [
13877
+ ...fresh
13878
+ ].flatMap(([seq, cp]) => cp ? [] : [
13879
+ seq
13880
+ ]);
13881
+ if (seqs.length === 0) return fresh;
13882
+ const lean = this.store.getCheckpointsForSettle ?? this.store.getCheckpoints;
13883
+ if (lean) {
13884
+ for (const cp of await lean.call(this.store, runId, seqs)) fresh.set(cp.seq, cp);
13885
+ } else {
13886
+ for (const seq of seqs) fresh.set(seq, await this.store.getCheckpoint(runId, seq));
13887
+ }
13888
+ if (lean === this.store.getCheckpointsForSettle) {
13889
+ for (const seq of seqs) {
13890
+ const cp = fresh.get(seq);
13891
+ if (cp && cp.status !== "completed") fresh.set(seq, await this.store.getCheckpoint(runId, seq));
13892
+ }
13893
+ }
13894
+ return fresh;
13895
+ }
13896
+ /**
12457
13897
  * The run's checkpoints as a turn replays them: {@link StateStore.listCheckpointsForReplay} when
12458
13898
  * the store has it (no input on a completed row), else {@link StateStore.listCheckpoints}.
12459
13899
  */
@@ -12493,8 +13933,8 @@ ${run.workflowVersion}`;
12493
13933
  * whole, the same way.
12494
13934
  */
12495
13935
  async settlePendingCheckpoints(settlements, settled) {
12496
- const settle = this.store.settleCheckpointsIfPending?.bind(this.store);
12497
- if (!settle) {
13936
+ const settle2 = this.store.settleCheckpointsIfPending?.bind(this.store);
13937
+ if (!settle2) {
12498
13938
  return this.savePendingCheckpoints(settlements.map((s, i) => ({
12499
13939
  ...settled[i],
12500
13940
  ...s
@@ -12502,12 +13942,12 @@ ${run.workflowVersion}`;
12502
13942
  }
12503
13943
  if (settlements.length > 1) {
12504
13944
  try {
12505
- const wrote = await settle(settlements);
13945
+ const wrote = await settle2(settlements);
12506
13946
  return settlements.map((_, i) => wrote[i] === true);
12507
13947
  } catch {
12508
13948
  }
12509
13949
  }
12510
- const one = await Promise.allSettled(settlements.map((s) => settle([
13950
+ const one = await Promise.allSettled(settlements.map((s) => settle2([
12511
13951
  s
12512
13952
  ])));
12513
13953
  return one.map((s) => s.status === "fulfilled" ? s.value[0] === true : {
@@ -12568,7 +14008,10 @@ ${run.workflowVersion}`;
12568
14008
  traceparent: this.traceparent?.(),
12569
14009
  context: this.context?.(),
12570
14010
  priority: task.priority,
12571
- attempt: task.attempt
14011
+ attempt: task.attempt,
14012
+ ...task.replyHere && this.repliesRouted && !this.draining ? {
14013
+ replyTo: this.instanceId
14014
+ } : {}
12572
14015
  }, task.transport);
12573
14016
  }
12574
14017
  /**
@@ -12660,6 +14103,9 @@ ${run.workflowVersion}`;
12660
14103
  i
12661
14104
  ];
12662
14105
  });
14106
+ if (this.store.settleResultsIfPending) {
14107
+ return this.settleResultsReturning(runId, results, live, outcomes, queuedIn);
14108
+ }
12663
14109
  const reads = await this.readCheckpoints(runId, live.map((i) => results[i].seq));
12664
14110
  const pending = [];
12665
14111
  const resettled = [];
@@ -12739,6 +14185,110 @@ ${run.workflowVersion}`;
12739
14185
  return wake.applyTo(outcomes);
12740
14186
  }
12741
14187
  /**
14188
+ * {@link completeRemoteResults} on a store with {@link StateStore.settleResultsIfPending}: the
14189
+ * results settle their `pending` rows and read them back in one call, instead of a read of the rows
14190
+ * followed by the write. Only an entry that wrote nothing (a redelivery, or a step gone) is read
14191
+ * afterwards. A settled `completed` row is also handed to the run's parked body, if this instance
14192
+ * holds one, so its next turn does not read it again.
14193
+ */
14194
+ async settleResultsReturning(runId, results, live, outcomes, queuedIn) {
14195
+ const settle2 = this.store.settleResultsIfPending.bind(this.store);
14196
+ const finishedAt = /* @__PURE__ */ new Date();
14197
+ const entries = live.map((i) => {
14198
+ const result = results[i];
14199
+ return {
14200
+ runId,
14201
+ seq: result.seq,
14202
+ status: result.status,
14203
+ output: result.status === "completed" ? result.output : void 0,
14204
+ error: result.error,
14205
+ events: result.events,
14206
+ startedAt: result.startedAt ? new Date(result.startedAt) : void 0,
14207
+ finishedAt
14208
+ };
14209
+ });
14210
+ let rows;
14211
+ try {
14212
+ rows = await settle2(entries);
14213
+ } catch (err) {
14214
+ if (entries.length === 1) {
14215
+ rows = [
14216
+ {
14217
+ error: err
14218
+ }
14219
+ ];
14220
+ } else {
14221
+ const one = await Promise.allSettled(entries.map((e) => settle2([
14222
+ e
14223
+ ])));
14224
+ rows = one.map((o) => o.status === "fulfilled" ? o.value[0] ?? null : {
14225
+ error: o.reason
14226
+ });
14227
+ }
14228
+ }
14229
+ const settled = [];
14230
+ const unsettled = [];
14231
+ rows.forEach((row, k) => {
14232
+ const index = live[k];
14233
+ if (row === null) unsettled.push(index);
14234
+ else if ("error" in row && !("seq" in row)) outcomes[index] = {
14235
+ error: row.error
14236
+ };
14237
+ else settled.push({
14238
+ index,
14239
+ cp: row,
14240
+ result: results[index]
14241
+ });
14242
+ });
14243
+ const parked = this.sticky.peek(runId) ?? this.turnContinuations.get(runId);
14244
+ for (const { cp, result } of settled) {
14245
+ void this.releaseQueueSlot(cp.stepId).catch(() => void 0);
14246
+ if (cp.status === "completed") parked?.hint(cp);
14247
+ if (!parseStepFanGroup(cp.parallelGroup)) this.noteSettledHere(`${runId}:${result.seq}`);
14248
+ this.emit({
14249
+ type: result.status === "completed" ? "step.completed" : "step.failed",
14250
+ runId,
14251
+ seq: result.seq,
14252
+ name: cp.name,
14253
+ kind: cp.kind,
14254
+ output: result.output,
14255
+ error: result.error,
14256
+ queueMs: cp.startedAt.getTime() - cp.enqueuedAt.getTime(),
14257
+ durationMs: finishedAt.getTime() - cp.startedAt.getTime()
14258
+ });
14259
+ }
14260
+ if (settled.length === 0) {
14261
+ if (unsettled.length === 0) return outcomes;
14262
+ const reads = await this.readCheckpoints(runId, unsettled.map((i) => results[i].seq));
14263
+ const resettled = [];
14264
+ reads.forEach((read, k) => {
14265
+ const index = unsettled[k];
14266
+ if (read.status === "rejected") outcomes[index] = {
14267
+ error: read.reason
14268
+ };
14269
+ else if (read.value?.status === "completed" || read.value?.status === "failed") {
14270
+ resettled.push({
14271
+ index,
14272
+ cp: read.value
14273
+ });
14274
+ } else if (read.value?.status === "pending") {
14275
+ void this.releaseQueueSlot(read.value.stepId).catch(() => void 0);
14276
+ }
14277
+ });
14278
+ if (resettled.length === 0) return outcomes;
14279
+ return this.resumeForRedelivered(runId, resettled, outcomes, queuedIn);
14280
+ }
14281
+ const barriers = /* @__PURE__ */ new Map();
14282
+ const wake = new FanWakeCollector();
14283
+ for (const { cp } of settled) wake.note(cp);
14284
+ for (const { index, cp, result } of settled) {
14285
+ if (!await this.stepFanStillRunning(cp, result, barriers, queuedIn)) {
14286
+ wake.add(index, cp, result.status);
14287
+ }
14288
+ }
14289
+ return wake.applyTo(outcomes);
14290
+ }
14291
+ /**
12742
14292
  * The outcomes for a batch of redelivered results (their steps already settled): one of them
12743
14293
  * resumes the run if it is `suspended` and, for a step of a fan, the fan's barrier is open. A
12744
14294
  * resume of a run that had in fact moved on replays to its current wait and re-parks.
@@ -13628,6 +15178,15 @@ var CodecStateStore = class {
13628
15178
  /** Encodes each settlement's payloads like {@link saveCheckpoint}. Bound only when the inner store
13629
15179
  * has it, so the engine's whole-row fallback still kicks in through the wrapper. */
13630
15180
  settleCheckpointsIfPending;
15181
+ /** Encodes each result's payloads, decodes each row read back. Bound only when the inner store
15182
+ * has it. */
15183
+ settleResultsIfPending;
15184
+ /** Encodes like {@link saveCheckpoint}. Bound only when the inner store has it. */
15185
+ insertCheckpointIfAbsent;
15186
+ /** Decodes each run claimed. Bound only when the inner store has it. */
15187
+ claimPendingRuns;
15188
+ /** Decodes the run read. Bound only when the inner store has it. */
15189
+ tryLockRunAndGet;
13631
15190
  /** Encodes each checkpoint written. Bound only when the inner store has it. */
13632
15191
  saveCheckpoints;
13633
15192
  /** Decodes each checkpoint read like {@link listCheckpoints}. Bound only when the inner store has
@@ -13663,6 +15222,14 @@ var CodecStateStore = class {
13663
15222
  listBufferedSignals;
13664
15223
  /** Forwarded verbatim — deletes by row id. */
13665
15224
  deleteBufferedSignals;
15225
+ /** Decodes the payload like {@link takeBufferedSignal}. Bound only when the inner store has it. */
15226
+ takeOldestBufferedSignal;
15227
+ /** Forwarded verbatim — a signal id record carries no payload. */
15228
+ claimSignalId;
15229
+ /** Forwarded verbatim. */
15230
+ releaseSignalId;
15231
+ /** Forwarded verbatim. */
15232
+ deleteExpiredSignalIds;
13666
15233
  /** Forwarded verbatim — a waiter carries no payload. */
13667
15234
  listSignalWaitersByRun;
13668
15235
  /** Forwarded verbatim — a waiter carries no payload. */
@@ -13691,6 +15258,14 @@ var CodecStateStore = class {
13691
15258
  if (saveManyIfPending) this.saveCheckpointsIfPending = (cps) => saveManyIfPending.call(inner, cps.map((cp) => this.encCp(cp)));
13692
15259
  const settleMany = inner.settleCheckpointsIfPending;
13693
15260
  if (settleMany) this.settleCheckpointsIfPending = (settlements) => settleMany.call(inner, settlements.map((s) => this.encSettlement(s)));
15261
+ const settleResults = inner.settleResultsIfPending;
15262
+ if (settleResults) this.settleResultsIfPending = async (results) => (await settleResults.call(inner, results.map((r) => this.encSettlement(r)))).map((cp) => this.decCp(cp));
15263
+ const insertIfAbsent = inner.insertCheckpointIfAbsent;
15264
+ if (insertIfAbsent) this.insertCheckpointIfAbsent = (cp) => insertIfAbsent.call(inner, this.encCp(cp));
15265
+ const claim = inner.claimPendingRuns;
15266
+ if (claim) this.claimPendingRuns = async (limit, owner, leaseUntilMs, nowMs, namespace) => (await claim.call(inner, limit, owner, leaseUntilMs, nowMs, namespace)).map((r) => this.decRun(r));
15267
+ const lockAndGet = inner.tryLockRunAndGet;
15268
+ if (lockAndGet) this.tryLockRunAndGet = async (runId, owner, leaseUntilMs, nowMs) => this.decRun(await lockAndGet.call(inner, runId, owner, leaseUntilMs, nowMs));
13694
15269
  const saveMany = inner.saveCheckpoints;
13695
15270
  if (saveMany) this.saveCheckpoints = (cps) => saveMany.call(inner, cps.map((cp) => this.encCp(cp)));
13696
15271
  const listForReplay = inner.listCheckpointsForReplay;
@@ -13727,6 +15302,18 @@ var CodecStateStore = class {
13727
15302
  }));
13728
15303
  const deleteBuffered = inner.deleteBufferedSignals;
13729
15304
  if (deleteBuffered) this.deleteBufferedSignals = (ids) => deleteBuffered.call(inner, ids);
15305
+ const takeOldest = inner.takeOldestBufferedSignal;
15306
+ if (takeOldest) this.takeOldestBufferedSignal = async (tokens) => {
15307
+ const taken = await takeOldest.call(inner, tokens);
15308
+ return taken ? {
15309
+ token: taken.token,
15310
+ payload: this.dec(taken.payload)
15311
+ } : null;
15312
+ };
15313
+ const { claimSignalId, releaseSignalId, deleteExpiredSignalIds } = inner;
15314
+ if (claimSignalId) this.claimSignalId = (token, id, expiresAt, nowMs) => claimSignalId.call(inner, token, id, expiresAt, nowMs);
15315
+ if (releaseSignalId) this.releaseSignalId = (token, id) => releaseSignalId.call(inner, token, id);
15316
+ if (deleteExpiredSignalIds) this.deleteExpiredSignalIds = (nowMs, limit) => deleteExpiredSignalIds.call(inner, nowMs, limit);
13730
15317
  const byRun = inner.listSignalWaitersByRun;
13731
15318
  if (byRun) this.listSignalWaitersByRun = (runIds) => byRun.call(inner, runIds);
13732
15319
  const hasWaiter = inner.hasSignalWaiter;
@@ -14511,6 +16098,87 @@ var InMemoryStateStore = class {
14511
16098
  return true;
14512
16099
  });
14513
16100
  }
16101
+ // The three round-trip-saving methods below are built on the plain ones, so a test store that
16102
+ // overrides `tryLockRun`, `getRun`, `getCheckpoint` or a settle to stage a race keeps staging it.
16103
+ // They are OFF unless asked for (`roundTripMethods`, or `DURABLE_TEST_ROUND_TRIP_METHODS=1` for a
16104
+ // whole suite): many specs pin the store calls the engine makes without them.
16105
+ settleResultsIfPending;
16106
+ insertCheckpointIfAbsent;
16107
+ tryLockRunAndGet;
16108
+ claimPendingRuns;
16109
+ constructor(opts = {}) {
16110
+ const on = opts.roundTripMethods ?? (typeof process !== "undefined" && process.env.DURABLE_TEST_ROUND_TRIP_METHODS === "1");
16111
+ if (on) {
16112
+ this.settleResultsIfPending = (results) => this.settleResultsFallback(results);
16113
+ this.insertCheckpointIfAbsent = (cp) => this.insertIfAbsentFallback(cp);
16114
+ this.tryLockRunAndGet = (runId, owner, until, now) => this.lockAndGetFallback(runId, owner, until, now);
16115
+ this.claimPendingRuns = async (limit, owner, until, now, namespace) => {
16116
+ const claimed = [];
16117
+ for (const run of await this.listPendingRuns(Number.MAX_SAFE_INTEGER, namespace)) {
16118
+ if (claimed.length >= limit) break;
16119
+ const locked = await this.lockAndGetFallback(run.id, owner, until, now);
16120
+ if (locked) claimed.push(locked);
16121
+ }
16122
+ return claimed;
16123
+ };
16124
+ }
16125
+ }
16126
+ async settleResultsFallback(results) {
16127
+ const out = results.map(() => null);
16128
+ const before = /* @__PURE__ */ new Map();
16129
+ const seqsByRun = /* @__PURE__ */ new Map();
16130
+ for (const r of results) seqsByRun.set(r.runId, [
16131
+ ...seqsByRun.get(r.runId) ?? [],
16132
+ r.seq
16133
+ ]);
16134
+ for (const [runId, seqs] of seqsByRun) {
16135
+ for (const cp of await this.getCheckpointsForSettle(runId, seqs)) {
16136
+ before.set(this.key(runId, cp.seq), cp);
16137
+ }
16138
+ }
16139
+ const claimed = /* @__PURE__ */ new Set();
16140
+ const settlements = [];
16141
+ const at = [];
16142
+ for (const [i, r] of results.entries()) {
16143
+ const key = this.key(r.runId, r.seq);
16144
+ const cp = before.get(key);
16145
+ if (cp?.status !== "pending" || claimed.has(key)) continue;
16146
+ const status = (await this.getRun(r.runId))?.status;
16147
+ if (status === "cancelled" || status === "completed") continue;
16148
+ claimed.add(key);
16149
+ settlements.push({
16150
+ runId: r.runId,
16151
+ seq: r.seq,
16152
+ status: r.status,
16153
+ output: r.status === "completed" ? r.output : cp.output,
16154
+ error: r.error,
16155
+ events: r.events ?? cp.events,
16156
+ startedAt: r.startedAt ?? cp.startedAt,
16157
+ finishedAt: r.finishedAt
16158
+ });
16159
+ at.push(i);
16160
+ }
16161
+ if (settlements.length === 0) return out;
16162
+ const wrote = await this.settleCheckpointsIfPending(settlements);
16163
+ settlements.forEach((settlement, k) => {
16164
+ if (!wrote[k]) return;
16165
+ const cp = before.get(this.key(settlement.runId, settlement.seq));
16166
+ out[at[k]] = {
16167
+ ...cp,
16168
+ ...settlement
16169
+ };
16170
+ });
16171
+ return out;
16172
+ }
16173
+ async insertIfAbsentFallback(checkpoint) {
16174
+ if (await this.getCheckpoint(checkpoint.runId, checkpoint.seq)) return false;
16175
+ await this.saveCheckpoint(checkpoint);
16176
+ return true;
16177
+ }
16178
+ async lockAndGetFallback(runId, owner, leaseUntilMs, nowMs) {
16179
+ if (!await this.tryLockRun(runId, owner, leaseUntilMs, nowMs)) return null;
16180
+ return this.getRun(runId);
16181
+ }
14514
16182
  async listOrphanedRuns(nowMs, limit, namespace, ownedBy) {
14515
16183
  return [
14516
16184
  ...this.runs.values()
@@ -14641,6 +16309,45 @@ var InMemoryStateStore = class {
14641
16309
  payload: head.payload
14642
16310
  };
14643
16311
  }
16312
+ async takeOldestBufferedSignal(tokens) {
16313
+ let oldest;
16314
+ for (const token of new Set(tokens)) {
16315
+ const head = this.bufferedSignals.get(token)?.[0];
16316
+ if (head && (!oldest || head.id < oldest.id)) oldest = {
16317
+ token,
16318
+ id: head.id
16319
+ };
16320
+ }
16321
+ if (!oldest) return null;
16322
+ const taken = await this.takeBufferedSignal(oldest.token);
16323
+ return taken ? {
16324
+ token: oldest.token,
16325
+ payload: taken.payload
16326
+ } : null;
16327
+ }
16328
+ /** `token \u0000 id -> expiresAt` of the signal ids {@link claimSignalId} recorded. */
16329
+ signalIds = /* @__PURE__ */ new Map();
16330
+ async claimSignalId(token, id, expiresAt, nowMs) {
16331
+ const key = `${token}\0${id}`;
16332
+ const existing = this.signalIds.get(key);
16333
+ if (existing !== void 0 && existing > nowMs) return false;
16334
+ this.signalIds.set(key, expiresAt);
16335
+ return true;
16336
+ }
16337
+ async releaseSignalId(token, id) {
16338
+ this.signalIds.delete(`${token}\0${id}`);
16339
+ }
16340
+ async deleteExpiredSignalIds(nowMs, limit) {
16341
+ let deleted = 0;
16342
+ for (const [key, expiresAt] of this.signalIds) {
16343
+ if (deleted >= limit) break;
16344
+ if (expiresAt <= nowMs) {
16345
+ this.signalIds.delete(key);
16346
+ deleted += 1;
16347
+ }
16348
+ }
16349
+ return deleted;
16350
+ }
14644
16351
  async listBufferedSignals(tokenPrefix, afterId, limit) {
14645
16352
  const after = afterId === void 0 ? 0 : Number(afterId);
14646
16353
  const rows = [];
@@ -15028,9 +16735,74 @@ var InMemoryRunQueue = class {
15028
16735
  }
15029
16736
  }
15030
16737
  };
16738
+
16739
+ // src/settle-results.ts
16740
+ async function settleResultsWithPlainCalls(store, results) {
16741
+ const out = results.map(() => null);
16742
+ const key = /* @__PURE__ */ __name((runId, seq) => `${seq}:${runId}`, "key");
16743
+ const seqsByRun = /* @__PURE__ */ new Map();
16744
+ for (const r of results) seqsByRun.set(r.runId, [
16745
+ ...seqsByRun.get(r.runId) ?? [],
16746
+ r.seq
16747
+ ]);
16748
+ const runIds = [
16749
+ ...seqsByRun.keys()
16750
+ ];
16751
+ const runs = store.getRuns ? await store.getRuns(runIds) : (await Promise.all(runIds.map((id) => store.getRun(id)))).filter((r) => r !== null);
16752
+ const finished = new Set(runs.filter((r) => r.status === "cancelled" || r.status === "completed").map((r) => r.id));
16753
+ const read = /* @__PURE__ */ __name(async (runId, seqs) => {
16754
+ if (store.getCheckpointsForSettle) return store.getCheckpointsForSettle(runId, seqs);
16755
+ if (store.getCheckpoints) return store.getCheckpoints(runId, seqs);
16756
+ const rows = await Promise.all(seqs.map((seq) => store.getCheckpoint(runId, seq)));
16757
+ return rows.filter((cp) => cp !== null);
16758
+ }, "read");
16759
+ const before = /* @__PURE__ */ new Map();
16760
+ for (const [runId, seqs] of seqsByRun) {
16761
+ if (finished.has(runId)) continue;
16762
+ for (const cp of await read(runId, seqs)) before.set(key(runId, cp.seq), cp);
16763
+ }
16764
+ const claimed = /* @__PURE__ */ new Set();
16765
+ const settlements = [];
16766
+ const at = [];
16767
+ for (const [i, r] of results.entries()) {
16768
+ const k = key(r.runId, r.seq);
16769
+ const cp = before.get(k);
16770
+ if (cp?.status !== "pending" || claimed.has(k)) continue;
16771
+ claimed.add(k);
16772
+ settlements.push({
16773
+ runId: r.runId,
16774
+ seq: r.seq,
16775
+ status: r.status,
16776
+ output: r.status === "completed" ? r.output : cp.output,
16777
+ error: r.error,
16778
+ events: r.events ?? cp.events,
16779
+ startedAt: r.startedAt ?? cp.startedAt,
16780
+ finishedAt: r.finishedAt
16781
+ });
16782
+ at.push(i);
16783
+ }
16784
+ if (settlements.length === 0 || !store.settleCheckpointsIfPending) return out;
16785
+ const wrote = await store.settleCheckpointsIfPending(settlements);
16786
+ const won = settlements.filter((_, j) => wrote[j] === true);
16787
+ const after = /* @__PURE__ */ new Map();
16788
+ const wonByRun = /* @__PURE__ */ new Map();
16789
+ for (const s of won) wonByRun.set(s.runId, [
16790
+ ...wonByRun.get(s.runId) ?? [],
16791
+ s.seq
16792
+ ]);
16793
+ for (const [runId, seqs] of wonByRun) {
16794
+ for (const cp of await read(runId, seqs)) after.set(key(runId, cp.seq), cp);
16795
+ }
16796
+ settlements.forEach((s, j) => {
16797
+ if (wrote[j] === true) out[at[j]] = after.get(key(s.runId, s.seq)) ?? null;
16798
+ });
16799
+ return out;
16800
+ }
16801
+ __name(settleResultsWithPlainCalls, "settleResultsWithPlainCalls");
15031
16802
  // Annotate the CommonJS export names for ESM import in node:
15032
16803
  0 && (module.exports = {
15033
16804
  ACCUMULATOR_SEEN_MAX,
16805
+ ANY_WAIT_GROUP,
15034
16806
  CHILD_EDGE_PREFIXES,
15035
16807
  CONTROL_FLOW_SIGNAL,
15036
16808
  CORE_ORIGIN,
@@ -15077,6 +16849,7 @@ var InMemoryRunQueue = class {
15077
16849
  LIFECYCLE_TRIGGER_FAILED_TAG_PREFIX,
15078
16850
  NestedWorkflowCallError,
15079
16851
  NonDeterminismError,
16852
+ PARENT_CLOSE_TAG_PREFIX,
15080
16853
  QueueController,
15081
16854
  REMOTE_EVENT,
15082
16855
  RUN_HISTORY_FIXTURE_VERSION,
@@ -15116,6 +16889,7 @@ var InMemoryRunQueue = class {
15116
16889
  aggregateAnnouncements,
15117
16890
  aggregatesCheckpoints,
15118
16891
  announcementsOf,
16892
+ anyCheckpointName,
15119
16893
  applyAttributeQuery,
15120
16894
  assertConcurrency,
15121
16895
  assertRoutes,
@@ -15172,6 +16946,8 @@ var InMemoryRunQueue = class {
15172
16946
  hmacTenantVerifier,
15173
16947
  indexWaitersByRun,
15174
16948
  info,
16949
+ isAnyCheckpoint,
16950
+ isAnyMember,
15175
16951
  isEngineMintedTag,
15176
16952
  isEventToken,
15177
16953
  isFailedMember,
@@ -15201,6 +16977,7 @@ var InMemoryRunQueue = class {
15201
16977
  missingCapabilities,
15202
16978
  negotiate,
15203
16979
  nextCronFireMs,
16980
+ nextRRuleFireMs,
15204
16981
  normalizeAttributeRows,
15205
16982
  normalizeDescriptor,
15206
16983
  observedAnnouncements,
@@ -15211,11 +16988,13 @@ var InMemoryRunQueue = class {
15211
16988
  parentRunIdOf,
15212
16989
  parseDuration,
15213
16990
  parseLifecycleEvent,
16991
+ parseRRule,
15214
16992
  parseWebhookToken,
15215
16993
  partitionOfToken,
15216
16994
  planDispatch,
15217
16995
  poolNamespace,
15218
16996
  prevCronFireMs,
16997
+ prevRRuleFireMs,
15219
16998
  queryCheckpointsOf,
15220
16999
  readRunsByIds,
15221
17000
  readSearchAttributes,
@@ -15245,6 +17024,7 @@ var InMemoryRunQueue = class {
15245
17024
  sanitizeQueueToken,
15246
17025
  scheduleRunIdAt,
15247
17026
  scheduledRunId,
17027
+ settleResultsWithPlainCalls,
15248
17028
  signTenantToken,
15249
17029
  singletonAdmissionDenialPatch,
15250
17030
  singletonAdmissionTags,