@dudousxd/nestjs-durable-core 0.87.0 → 0.88.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;
@@ -5873,6 +6856,8 @@ var WorkflowEngine = class _WorkflowEngine {
5873
6856
  claimsMade = /* @__PURE__ */ new Map();
5874
6857
  /** Pager hooks for stranded runs (see {@link onStalled}). */
5875
6858
  stalledListeners = /* @__PURE__ */ new Set();
6859
+ /** `parentClose` policies recorded by the turn in flight, per parent run (see {@link settleParentClose}). */
6860
+ parentCloseIntents = /* @__PURE__ */ new Map();
5876
6861
  /** Runs already paged about, by id → the `updatedAt` of the paged episode (see {@link sweepStalled}). */
5877
6862
  notifiedStalled = /* @__PURE__ */ new Map();
5878
6863
  lastStalledSweepAt = Number.NEGATIVE_INFINITY;
@@ -5907,14 +6892,18 @@ var WorkflowEngine = class _WorkflowEngine {
5907
6892
  this.unregisteredWorkflowGraceMs = grace !== void 0 && Number.isFinite(grace) && grace >= 0 ? grace : 24 * 60 * 60 * 1e3;
5908
6893
  this.remoteAdvanceSilenceMs = deps.remoteAdvanceSilenceMs;
5909
6894
  this.reconcileMs = deps.reconcileMs === void 0 ? 3e5 : deps.reconcileMs || void 0;
6895
+ this.sticky = new StickyCache(Math.max(0, deps.stickyRuns ?? DEFAULT_STICKY_RUNS), deps.stickyIdleMs ?? DEFAULT_STICKY_IDLE_MS);
6896
+ this.routeReplies = deps.routeReplies ?? true;
5910
6897
  const stalledAfter = deps.stalledAfterMs;
5911
6898
  this.stalledAfterMs = stalledAfter !== void 0 && Number.isFinite(stalledAfter) ? Math.max(6e4, stalledAfter) : DEFAULT_STALLED_AFTER_MS;
5912
6899
  const batch = deps.timerBatchSize;
5913
6900
  this.timerBatchSize = batch !== void 0 && Number.isFinite(batch) && batch >= 1 ? Math.floor(batch) : 500;
5914
6901
  this.eventBufferTtlMs = deps.eventBufferTtlMs || void 0;
6902
+ this.signalIdTtlMs = deps.signalIdTtl != null ? parseDuration(deps.signalIdTtl) : DEFAULT_SIGNAL_ID_TTL_MS;
5915
6903
  this.remoteRedispatchMs = deps.remoteRedispatchMs || void 0;
5916
6904
  this.remoteRedispatchMax = Math.max(1, deps.remoteRedispatchMax ?? 10);
5917
6905
  this.blockedPollMs = deps.blockedPollMs ?? 5e3;
6906
+ this.workerDescriptorCacheMs = deps.workerDescriptorCacheMs ?? 2e3;
5918
6907
  this.cpDescriptor = controlPlaneDescriptor({
5919
6908
  instanceId: `cp-${this.instanceId}`,
5920
6909
  ...this.namespace !== void 0 ? {
@@ -6012,6 +7001,9 @@ var WorkflowEngine = class _WorkflowEngine {
6012
7001
  await this.completeRemoteDecision(decision);
6013
7002
  }
6014
7003
  );
7004
+ if (this.routeReplies && this.sticky.enabled) {
7005
+ this.repliesRouted = this.pool.routeReplies(this.instanceId);
7006
+ }
6015
7007
  const verifyTenant = deps.verifyTenant;
6016
7008
  this.pool.onStartRun(async (message) => {
6017
7009
  let tenant;
@@ -6588,6 +7580,23 @@ var WorkflowEngine = class _WorkflowEngine {
6588
7580
  return deleted;
6589
7581
  }
6590
7582
  /**
7583
+ * Delete the signal-id records (`signal(..., { id })`) that expired by `now`, in batches of
7584
+ * {@link SIGNAL_ID_SWEEP_BATCH}, at most `maxBatches` per call. Expired records already stop
7585
+ * deduplicating; this reclaims their rows. The NestJS timer poller calls it every tick. Returns how
7586
+ * many it deleted; 0 on a store without `deleteExpiredSignalIds`.
7587
+ */
7588
+ async sweepExpiredSignalIds(now = this.clock(), maxBatches = 4) {
7589
+ const { deleteExpiredSignalIds } = this.store;
7590
+ if (!deleteExpiredSignalIds || this.draining) return 0;
7591
+ let deleted = 0;
7592
+ for (let batch = 0; batch < maxBatches; batch++) {
7593
+ const n = await deleteExpiredSignalIds.call(this.store, now, SIGNAL_ID_SWEEP_BATCH);
7594
+ deleted += n;
7595
+ if (n < SIGNAL_ID_SWEEP_BATCH) break;
7596
+ }
7597
+ return deleted;
7598
+ }
7599
+ /**
6591
7600
  * Register a pager hook for STRANDED runs: an in-flight run untouched past `stalledAfterMs` that is
6592
7601
  * either suspended with NO wake timer (wake-forever: nothing will ever re-drive it) or parked on an
6593
7602
  * old pending remote step with no live lease (dispatched and, most likely, lost). The timer poller
@@ -6665,6 +7674,7 @@ var WorkflowEngine = class _WorkflowEngine {
6665
7674
  ...event,
6666
7675
  at: /* @__PURE__ */ new Date()
6667
7676
  };
7677
+ this.settleParentClose(full);
6668
7678
  this.deliver(full);
6669
7679
  if (opts?.lifecycle !== false) {
6670
7680
  this.publishLifecycle(full);
@@ -8214,8 +9224,31 @@ var WorkflowEngine = class _WorkflowEngine {
8214
9224
  return true;
8215
9225
  }
8216
9226
  async resumeRunNow(runId, allowFailed) {
8217
- const run = await this.store.getRun(runId);
9227
+ const claimed = this.claimedRuns.get(runId);
9228
+ if (claimed) {
9229
+ this.claimedRuns.delete(runId);
9230
+ return this.resumeLoadedRun(claimed, allowFailed);
9231
+ }
9232
+ const lockAndGet = this.executionLeases.has(runId) ? void 0 : this.store.tryLockRunAndGet;
9233
+ let locked = false;
9234
+ let run = null;
9235
+ if (lockAndGet) {
9236
+ const nowMs = this.clock();
9237
+ run = await lockAndGet.call(this.store, runId, this.instanceId, nowMs + this.leaseMs, nowMs);
9238
+ locked = run !== null;
9239
+ }
9240
+ run ??= await this.store.getRun(runId);
8218
9241
  if (!run) throw new Error(`run ${runId} not found`);
9242
+ if (!locked) return this.resumeLoadedRun(run, allowFailed);
9243
+ this.lockedForTurn.add(runId);
9244
+ try {
9245
+ return await this.resumeLoadedRun(run, allowFailed);
9246
+ } finally {
9247
+ if (this.lockedForTurn.delete(runId)) await this.releaseLeaseBestEffort(runId);
9248
+ }
9249
+ }
9250
+ async resumeLoadedRun(run, allowFailed) {
9251
+ const runId = run.id;
8219
9252
  if (run.status === "cancelled" || run.status === "completed" || run.status === "dead" || run.status === "failed" && !allowFailed) {
8220
9253
  return {
8221
9254
  runId,
@@ -8397,6 +9430,8 @@ var WorkflowEngine = class _WorkflowEngine {
8397
9430
  */
8398
9431
  beginDrain(spentMs = 0) {
8399
9432
  this.draining = true;
9433
+ this.sticky.clear();
9434
+ this.pool.stopReplies();
8400
9435
  if (this.runConsumerStops.length > 0) {
8401
9436
  this.runBatcher.flush();
8402
9437
  void this.stopRunConsumers();
@@ -9112,7 +10147,13 @@ var WorkflowEngine = class _WorkflowEngine {
9112
10147
  * {@link resumeDueTimers}.
9113
10148
  */
9114
10149
  async runPending(nowMs = this.clock()) {
9115
- const pending = await this.store.listPendingRuns(100, this.namespace);
10150
+ if (this.store.claimPendingRuns && !this.queuesRunsOverTransport && !this.draining) {
10151
+ const room = Math.min(PENDING_POLL_BATCH, LOCAL_RUN_CONCURRENCY - this.inflight.size);
10152
+ if (room <= 0) return [];
10153
+ const claimed = await this.store.claimPendingRuns(room, this.instanceId, nowMs + this.leaseMs, nowMs, this.namespace);
10154
+ return this.runClaimed(claimed);
10155
+ }
10156
+ const pending = await this.store.listPendingRuns(PENDING_POLL_BATCH, this.namespace);
9116
10157
  if (this.queuesRunsOverTransport && !this.draining && pending.length > 0) {
9117
10158
  if (await this.queueOnTransport(pending)) {
9118
10159
  return pending.map((run) => ({
@@ -9202,6 +10243,34 @@ var WorkflowEngine = class _WorkflowEngine {
9202
10243
  * for a pod that does have the workflow — which is exactly what the skew-protection error asks
9203
10244
  * for. Same posture as `publishEvent`'s per-subscriber isolation.
9204
10245
  */
10246
+ /**
10247
+ * Run `claimed` (pending runs this instance just leased, read as locked): every turn starts at
10248
+ * once, and each re-enters the lease taken for it without reading the run again.
10249
+ */
10250
+ async runClaimed(claimed) {
10251
+ const turns = claimed.map(async (run) => {
10252
+ if (this.draining) {
10253
+ await this.releaseLeaseBestEffort(run.id);
10254
+ return void 0;
10255
+ }
10256
+ this.lockedForTurn.add(run.id);
10257
+ this.claimedRuns.set(run.id, run);
10258
+ try {
10259
+ return await this.resumeImplicit(run.id);
10260
+ } catch (error2) {
10261
+ if (error2 instanceof WorkflowNotRegistered) {
10262
+ const settled = await this.settleUnregistered(run, this.clock()).catch(() => void 0);
10263
+ if (settled) return settled;
10264
+ }
10265
+ console.error(`[nestjs-durable] could not resume run ${run.id} (${run.workflow}@${run.workflowVersion}); leaving it for another instance:`, error2);
10266
+ return void 0;
10267
+ } finally {
10268
+ this.lockedForTurn.delete(run.id);
10269
+ this.claimedRuns.delete(run.id);
10270
+ }
10271
+ });
10272
+ return (await Promise.all(turns)).filter((r) => r !== void 0);
10273
+ }
9205
10274
  async resumeLeased(runs, nowMs = this.clock(), onLocked) {
9206
10275
  if (this.draining) return [];
9207
10276
  const results = [];
@@ -9301,7 +10370,8 @@ var WorkflowEngine = class _WorkflowEngine {
9301
10370
  * due-timer reconcile sweep ({@link reconcileSignalWaiter}) — all three resolve a waiter the exact
9302
10371
  * same way.
9303
10372
  */
9304
- async deliverSignal(waiter, payload, resumeHere = false) {
10373
+ async deliverSignal(waiter, payload, resumeHere = false, committed) {
10374
+ if (isAnyMember(waiter)) return this.deliverToWaitAny(waiter, payload, resumeHere, committed);
9305
10375
  await this.store.saveCheckpoint(instantCheckpoint({
9306
10376
  runId: waiter.runId,
9307
10377
  seq: waiter.seq,
@@ -9314,6 +10384,7 @@ var WorkflowEngine = class _WorkflowEngine {
9314
10384
  // for an ordinary (non-fan) signal/child await.
9315
10385
  parallelGroup: waiter.parallelGroup
9316
10386
  }));
10387
+ committed?.();
9317
10388
  if (await this.fanStillWaiting(waiter, payload)) {
9318
10389
  return {
9319
10390
  runId: waiter.runId,
@@ -9326,6 +10397,47 @@ var WorkflowEngine = class _WorkflowEngine {
9326
10397
  });
9327
10398
  }
9328
10399
  /**
10400
+ * Deliver to a waiter that is one condition of a `ctx.waitForAny` (see `wait-any.ts`): settle the
10401
+ * wait with this condition through the checkpoint's compare-and-set, then wake the run. A signal
10402
+ * that loses — the wait was settled by another condition first, or a timer of the wait came due
10403
+ * before it arrived (the timer then wins) — is not consumed: it is sent again on its token, where
10404
+ * it is buffered for the next wait (or reaches a waiter registered since).
10405
+ */
10406
+ async deliverToWaitAny(waiter, payload, resumeHere, committed) {
10407
+ const cp = await this.store.getCheckpoint(waiter.runId, waiter.seq);
10408
+ const conditions = cp && cp.status === "pending" && isAnyCheckpoint(cp) ? anyConditionsOf(cp) : [];
10409
+ const condition = conditions.find((c) => anyConditionToken(c) === waiter.token);
10410
+ const run = cp && condition ? await this.store.getRun(waiter.runId) : null;
10411
+ if (!cp || !condition || !run || TERMINAL_RUN_STATUSES.includes(run.status)) {
10412
+ if (cp && condition) await removeAnyWaiters(this.store, cp);
10413
+ await this.redeliverLosingSignal(waiter.token, payload);
10414
+ committed?.();
10415
+ return null;
10416
+ }
10417
+ const due = dueTimer(conditions, this.clock());
10418
+ const done = due ? resolvedAnyCheckpoint(cp, due, null) : resolvedAnyCheckpoint(cp, condition, payload);
10419
+ const won = await claimAnyCheckpoint(this.store, done);
10420
+ if (won) await removeAnyWaiters(this.store, done);
10421
+ if (!won || due) await this.redeliverLosingSignal(waiter.token, payload);
10422
+ committed?.();
10423
+ if (!won) return null;
10424
+ return (resumeHere ? this.resumeImplicit(waiter.runId) : this.wake(waiter.runId)).catch((err) => {
10425
+ if (err instanceof NamespaceMismatch) return null;
10426
+ throw err;
10427
+ });
10428
+ }
10429
+ /**
10430
+ * Put back a signal a `waitForAny` condition took but did not win with. A child's completion is
10431
+ * kept only where a parent can still consume it, as {@link deliverToParent} decides.
10432
+ */
10433
+ async redeliverLosingSignal(token, payload) {
10434
+ if (token.startsWith(CHILD_TOKEN_PREFIX)) {
10435
+ const childId = token.slice(CHILD_TOKEN_PREFIX.length);
10436
+ if (!await childCompletionNeedsBuffer(this.store, childId, payload)) return;
10437
+ }
10438
+ await this.deliverOrBuffer(token, payload);
10439
+ }
10440
+ /**
9329
10441
  * The `ctx.all` join barrier: true when `waiter` was one child of a fan (or of one wave of it)
9330
10442
  * whose siblings are still out, so waking the parent now would only replay the fan and re-suspend.
9331
10443
  * Skipping that wake is what keeps a fan of N children at O(N) work instead of N replays of N items.
@@ -9458,7 +10570,7 @@ var WorkflowEngine = class _WorkflowEngine {
9458
10570
  return;
9459
10571
  }
9460
10572
  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) => ({
10573
+ const awaited = waiters.filter((w) => w.token.startsWith("child:") && !settled.has(w.seq) && !isAnyMember(w)).map((w) => ({
9462
10574
  childId: w.token.slice("child:".length),
9463
10575
  seq: w.seq,
9464
10576
  parallelGroup: w.parallelGroup
@@ -9549,10 +10661,51 @@ var WorkflowEngine = class _WorkflowEngine {
9549
10661
  * Either way, the destructive `take*` ops (on the waiter row, on the buffer) arbitrate: whichever
9550
10662
  * side actually removes the row is the one that delivers, so a signal is never delivered twice.
9551
10663
  */
9552
- async signal(token, payload) {
10664
+ async signal(token, payload, opts) {
10665
+ const id = opts?.id;
10666
+ if (id === void 0) return this.deliverOrBuffer(token, payload);
10667
+ if (!this.store.claimSignalId) {
10668
+ throw new Error("engine.signal(..., { id }) needs a store that deduplicates signals (claimSignalId); the bundled stores do");
10669
+ }
10670
+ const key = `${token}\0${id}`;
10671
+ for (let prior = this.signalIdSends.get(key); prior; prior = this.signalIdSends.get(key)) {
10672
+ const sent = await prior.then(() => true, () => false);
10673
+ if (sent) return null;
10674
+ }
10675
+ const send = this.signalOnce(token, payload, id, opts?.idTtl);
10676
+ this.signalIdSends.set(key, send);
10677
+ try {
10678
+ return await send;
10679
+ } finally {
10680
+ if (this.signalIdSends.get(key) === send) this.signalIdSends.delete(key);
10681
+ }
10682
+ }
10683
+ /** One deduplicated {@link signal}: claim `id`, then deliver or buffer; a failure forgets the id. */
10684
+ async signalOnce(token, payload, id, idTtl) {
10685
+ const claimSignalId = this.store.claimSignalId;
10686
+ if (!claimSignalId) return null;
10687
+ const now = this.clock();
10688
+ const ttl = idTtl !== void 0 ? parseDuration(idTtl) : this.signalIdTtlMs;
10689
+ if (!await claimSignalId.call(this.store, token, id, now + ttl, now)) return null;
10690
+ let committed = false;
10691
+ try {
10692
+ return await this.deliverOrBuffer(token, payload, () => {
10693
+ committed = true;
10694
+ });
10695
+ } catch (err) {
10696
+ if (!committed) await this.store.releaseSignalId?.(token, id).catch(() => void 0);
10697
+ throw err;
10698
+ }
10699
+ }
10700
+ /**
10701
+ * {@link signal} without deduplication: deliver to the waiter on `token`, or buffer. `committed`
10702
+ * is called once the signal is durably delivered or buffered.
10703
+ */
10704
+ async deliverOrBuffer(token, payload, committed) {
9553
10705
  const waiter = await this.store.takeSignalWaiter(token);
9554
- if (waiter) return this.deliverSignal(waiter, payload);
10706
+ if (waiter) return this.deliverSignal(waiter, payload, false, committed);
9555
10707
  await this.store.bufferSignal(token, payload);
10708
+ committed?.();
9556
10709
  const lateWaiter = await this.store.takeSignalWaiter(token);
9557
10710
  if (!lateWaiter) return null;
9558
10711
  const reclaimed = await this.store.takeBufferedSignal(token);
@@ -9914,10 +11067,32 @@ var WorkflowEngine = class _WorkflowEngine {
9914
11067
  */
9915
11068
  async cancelChildren(parentRunId, opts) {
9916
11069
  for (const id of await this.getRunChildren(parentRunId)) {
11070
+ const child = await this.store.getRun(id).catch(() => null);
11071
+ if (child?.tags?.includes(`${PARENT_CLOSE_TAG_PREFIX}abandon`)) continue;
9917
11072
  await this.cancel(id, opts).catch(() => void 0);
9918
11073
  }
9919
11074
  }
9920
11075
  /**
11076
+ * Apply the `parentClose` policies a parent's turn recorded (see {@link ChildCallOptions.parentClose})
11077
+ * when that parent completes or fails; forget them when it suspends (its next turn, here or on
11078
+ * another instance, records them again). Local events only: this is the instance that ran the turn.
11079
+ */
11080
+ settleParentClose(event) {
11081
+ if (event.type !== "run.completed" && event.type !== "run.failed" && event.type !== "run.suspended") {
11082
+ return;
11083
+ }
11084
+ const intents = this.parentCloseIntents.get(event.runId);
11085
+ if (!intents) return;
11086
+ this.parentCloseIntents.delete(event.runId);
11087
+ if (event.type === "run.suspended") return;
11088
+ for (const [childId, policy] of intents) {
11089
+ if (policy === "abandon") continue;
11090
+ queueMicrotask(() => void this.cancel(childId, {
11091
+ compensate: policy === "cancel"
11092
+ }).catch(() => void 0));
11093
+ }
11094
+ }
11095
+ /**
9921
11096
  * The ids of the runs a run spawned — both awaited (`ctx.child`) and fire-and-forget
9922
11097
  * (`ctx.startChild`, found via its `spawn:<id>` checkpoint). The canonical parent→children edge,
9923
11098
  * used for both cancellation cascades and the dashboard run-tree.
@@ -9934,7 +11109,7 @@ var WorkflowEngine = class _WorkflowEngine {
9934
11109
  async awaitedChildIds(parentRunId) {
9935
11110
  return (await listSignalWaitersOfRuns(this.store, [
9936
11111
  parentRunId
9937
- ])).filter((w) => w.token.startsWith("child:")).map((w) => w.token.slice("child:".length));
11112
+ ])).filter((w) => w.token.startsWith("child:") && !isAnyMember(w)).map((w) => w.token.slice("child:".length));
9938
11113
  }
9939
11114
  async getRunChildren(parentRunId) {
9940
11115
  const childIds = new Set(await this.awaitedChildIds(parentRunId));
@@ -10222,15 +11397,19 @@ var WorkflowEngine = class _WorkflowEngine {
10222
11397
  void this.announcedSubscriptions();
10223
11398
  const tags = singleton ? withSingletonTracked(run.tags) : run.tags;
10224
11399
  const retag = tags !== run.tags;
10225
- await this.store.updateRun(run.id, {
11400
+ const started = await (this.store.updateRunIf ?? this.updateRunIfFallback).call(this.store, run.id, [
11401
+ "pending"
11402
+ ], {
10226
11403
  status: "running",
10227
11404
  ...retag ? {
10228
11405
  tags
10229
11406
  } : {},
10230
11407
  updatedAt: /* @__PURE__ */ new Date()
10231
11408
  });
11409
+ if (!started) return false;
10232
11410
  run.status = "running";
10233
11411
  if (retag) run.tags = tags;
11412
+ return true;
10234
11413
  }
10235
11414
  async singletonWaitResult(run) {
10236
11415
  const current = await this.store.getRun(run.id);
@@ -10300,7 +11479,7 @@ var WorkflowEngine = class _WorkflowEngine {
10300
11479
  if (result.status === "suspended") {
10301
11480
  this.inBackground(this.honorWakeRequests(run.id, settledFans, served));
10302
11481
  } else if (TERMINAL_RUN_STATUSES.includes(result.status)) {
10303
- this.inBackground(this.listWakeRequests(run.id, true).then((kept) => this.dropWakeRequests(kept)));
11482
+ this.inBackground(this.listWakeRequests(run.id).then((kept) => this.dropWakeRequests(kept)));
10304
11483
  }
10305
11484
  return result;
10306
11485
  } finally {
@@ -10351,7 +11530,7 @@ var WorkflowEngine = class _WorkflowEngine {
10351
11530
  * the timer poll of another instance runs it once this instance's hold on it would have lapsed.
10352
11531
  */
10353
11532
  async honorWakeRequests(runId, settledFans, served) {
10354
- const kept = await this.listWakeRequests(runId, true);
11533
+ const kept = await this.listWakeRequests(runId);
10355
11534
  const seen = /* @__PURE__ */ __name((request) => wakeSeen(request, settledFans, served), "seen");
10356
11535
  const needed = kept.filter((request) => !seen(request));
10357
11536
  await this.dropWakeRequests(kept.filter(seen));
@@ -10361,30 +11540,17 @@ var WorkflowEngine = class _WorkflowEngine {
10361
11540
  if (!(err instanceof NamespaceMismatch)) throw err;
10362
11541
  });
10363
11542
  }
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) => ({
11543
+ /** The wake requests kept for `runId`. */
11544
+ async listWakeRequests(runId) {
11545
+ const kept = await this.store.listBufferedEvents(wakeRequestToken(runId), WAKE_REQUEST_SCAN);
11546
+ return kept.map((request) => ({
10371
11547
  id: request.id,
10372
11548
  payload: request.payload
10373
11549
  }));
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
11550
  }
10383
- /** Delete `requests` (the ones kept as buffered events). */
11551
+ /** Delete `requests`. */
10384
11552
  async dropWakeRequests(requests) {
10385
- await Promise.all(requests.flatMap((request) => request.id === void 0 ? [] : [
10386
- this.store.removeBufferedEvent(request.id)
10387
- ]));
11553
+ await Promise.all(requests.map((request) => this.store.removeBufferedEvent(request.id)));
10388
11554
  }
10389
11555
  /**
10390
11556
  * Record, for the turn executing `runId` here, the wakes kept for the run so far. Called before the
@@ -10790,12 +11956,15 @@ ${run.workflowVersion}`;
10790
11956
  };
10791
11957
  }
10792
11958
  if (outcome.kind === "blocked") return this.parkBlocked(run, outcome.blocked);
11959
+ const stamp = new Date(Math.max(updatedAt.getTime(), (run.updatedAt?.getTime() ?? 0) + 1));
10793
11960
  const applied = await (this.store.updateRunIf ?? this.updateRunIfFallback).call(this.store, run.id, SUSPEND_ELIGIBLE_STATUSES, await this.withRunTrace(run, {
10794
11961
  status: "suspended",
10795
11962
  wakeAt: this.reconcileWakeAt(outcome.wakeAt),
10796
- updatedAt
11963
+ updatedAt: stamp
10797
11964
  }));
10798
11965
  if (!applied) return this.echoCurrentStatus(run.id);
11966
+ run.updatedAt = stamp;
11967
+ this.suspendStamps.set(run.id, stamp.getTime());
10799
11968
  await this.keepDueForKeptWakes(run.id);
10800
11969
  this.emit({
10801
11970
  type: "run.suspended",
@@ -10855,7 +12024,10 @@ ${run.workflowVersion}`;
10855
12024
  }
10856
12025
  const remote = registered.remote;
10857
12026
  if (run.status === "pending") {
10858
- await this.markStarted(run, registered?.singleton !== void 0);
12027
+ if (!await this.markStarted(run, registered?.singleton !== void 0)) {
12028
+ await this.releaseLeaseBestEffort(run.id);
12029
+ return await this.echoCurrentStatus(run.id);
12030
+ }
10859
12031
  this.emit({
10860
12032
  type: "run.started",
10861
12033
  runId: run.id,
@@ -10867,7 +12039,9 @@ ${run.workflowVersion}`;
10867
12039
  if (registered.singleton && !await this.singletons.admit(run, registered.singleton)) {
10868
12040
  return this.singletonWaitResult(run);
10869
12041
  }
10870
- const wfPlan = await this.planRoute(tenantGroup(sanitizeQueueToken(remote.group), void 0), registered.requires);
12042
+ const wfToken = tenantGroup(sanitizeQueueToken(remote.group), void 0);
12043
+ let wfPlan = await this.planRoute(wfToken, registered.requires);
12044
+ if (wfPlan?.status === "blocked") wfPlan = await this.planRoute(wfToken, registered.requires, true);
10871
12045
  if (wfPlan?.status === "blocked") return this.parkBlocked(run, wfPlan);
10872
12046
  const checkpoints = await this.replayCheckpoints(run.id);
10873
12047
  await this.deliverFinishedChildren(run.id, checkpoints);
@@ -10996,6 +12170,7 @@ ${run.workflowVersion}`;
10996
12170
  kind: "suspended",
10997
12171
  wakeAt
10998
12172
  });
12173
+ this.suspendStamps.delete(run.id);
10999
12174
  await this.redriveIfTurnSawStaleHistory(run.id, decision.commands);
11000
12175
  return suspended;
11001
12176
  }
@@ -11249,6 +12424,10 @@ ${run.workflowVersion}`;
11249
12424
  for (const cp of checkpoints) {
11250
12425
  if (cp.status === "completed" || cp.status === "failed") {
11251
12426
  if (cp.seq >= HANDOFF_SEQ_BASE) continue;
12427
+ if (isAnyCheckpoint(cp)) {
12428
+ events.push(anyHistoryEvent(cp));
12429
+ continue;
12430
+ }
11252
12431
  if (cp.kind === "signal" && cp.name.startsWith("signal:child:")) {
11253
12432
  const completion = cp.output;
11254
12433
  events.push({
@@ -11416,6 +12595,16 @@ ${run.workflowVersion}`;
11416
12595
  await deliverBuffered(lateBuffered.payload);
11417
12596
  }
11418
12597
  }
12598
+ } else if (cmd.kind === "waitAny") {
12599
+ const state = await enterWaitAny({
12600
+ store: this.store,
12601
+ clock: this.clock
12602
+ }, run.id, cmd.seq, cmd.name, cmd.conditions);
12603
+ if (state.state === "resolved") {
12604
+ setTimeout(() => void this.resumeImplicit(run.id).catch(() => void 0), 0);
12605
+ } else if (state.wakeAt != null) {
12606
+ wakeAt = wakeAt == null ? state.wakeAt : Math.min(wakeAt, state.wakeAt);
12607
+ }
11419
12608
  } else if (cmd.kind === "startChild") {
11420
12609
  const childId = `${run.id}.child.${cmd.seq}`;
11421
12610
  await this.store.putSignalWaiter({
@@ -11514,7 +12703,10 @@ ${run.workflowVersion}`;
11514
12703
  async runExecutionTurn(run, fn, registered) {
11515
12704
  if (run.status === "cancelling") this.cancelRequested.add(run.id);
11516
12705
  if (run.status === "pending") {
11517
- await this.markStarted(run, registered?.singleton !== void 0);
12706
+ if (!await this.markStarted(run, registered?.singleton !== void 0)) {
12707
+ await this.releaseLeaseBestEffort(run.id);
12708
+ return await this.echoCurrentStatus(run.id);
12709
+ }
11518
12710
  this.emit({
11519
12711
  type: "run.started",
11520
12712
  runId: run.id,
@@ -11527,24 +12719,52 @@ ${run.workflowVersion}`;
11527
12719
  if (run.status !== "cancelling" && registered?.singleton && !await this.singletons.admit(run, registered.singleton)) {
11528
12720
  return this.singletonWaitResult(run);
11529
12721
  }
11530
- const compensations = [];
11531
- const snapshot = await this.replayCheckpoints(run.id);
11532
- await this.deliverFinishedChildren(run.id, snapshot);
11533
- const replay = /* @__PURE__ */ new Map();
12722
+ const stickyOk = this.stickyEligible(run);
12723
+ let cont = stickyOk ? this.takeContinuation(run, fn) : void 0;
12724
+ const warm = cont !== void 0;
12725
+ const parkedFresh = cont ? await this.readParked(run.id, cont) : void 0;
12726
+ const compensations = cont ? cont.compensations : [];
12727
+ const snapshot = cont ? [] : await this.replayCheckpoints(run.id);
12728
+ if (!cont) await this.deliverFinishedChildren(run.id, snapshot);
12729
+ const replay = cont ? cont.replay : /* @__PURE__ */ new Map();
11534
12730
  for (const cp of snapshot) replay.set(cp.seq, cp);
12731
+ if (!cont && stickyOk) {
12732
+ cont = new StickyContinuation(run.id, fn, run.workflowVersion, compensations, replay, (c) => this.sticky.evict(c.runId));
12733
+ }
12734
+ if (cont) this.turnContinuations.set(run.id, cont);
12735
+ let parkedTurn = false;
11535
12736
  let suspendedOnFan;
11536
12737
  let suspendedOnSteps = false;
11537
12738
  const awaitedSteps = /* @__PURE__ */ new Set();
11538
12739
  this.awaitedRemoteSteps.set(run.id, awaitedSteps);
11539
12740
  const awaitedFans = [];
11540
12741
  this.awaitedStepFans.set(run.id, awaitedFans);
11541
- const ctx = createWorkflowCtx(this.ctxHostFor(replay, run.namespace), run.id, compensations, run.workflow, registered?.searchAttributesSchema);
12742
+ const ctx = warm ? void 0 : createWorkflowCtx(this.ctxHostFor(replay, run.namespace, cont), run.id, compensations, run.workflow, registered?.searchAttributesSchema);
11542
12743
  try {
11543
12744
  const handoff = liveHandoff(run.id, snapshot);
11544
12745
  if (handoff && !this.cancelRequested.has(run.id)) {
11545
12746
  return await this.settleRun(run, await this.followHandoff(run, handoff));
11546
12747
  }
11547
- const output = await runInWorkflowCtx(ctx, () => fn(ctx, run.input));
12748
+ let output;
12749
+ if (cont) {
12750
+ if (parkedFresh) {
12751
+ cont.attached = true;
12752
+ await cont.resumeParked(parkedFresh);
12753
+ } else {
12754
+ const body = ctx;
12755
+ cont.begin(() => runInWorkflowCtx(body, () => fn(body, run.input)));
12756
+ }
12757
+ const outcome = await cont.next();
12758
+ if (outcome.kind === "parked") {
12759
+ parkedTurn = true;
12760
+ throw new WorkflowSuspended(outcome.wakeAt);
12761
+ }
12762
+ if (outcome.kind === "threw") throw outcome.error;
12763
+ output = outcome.value;
12764
+ } else {
12765
+ const body = ctx;
12766
+ output = await runInWorkflowCtx(body, () => fn(body, run.input));
12767
+ }
11548
12768
  if (this.cancelRequested.has(run.id)) {
11549
12769
  return await this.compensateAndCancel(run, compensations, replay);
11550
12770
  }
@@ -11602,6 +12822,11 @@ ${run.workflowVersion}`;
11602
12822
  });
11603
12823
  suspendedOnFan = err.fan;
11604
12824
  suspendedOnSteps = suspended.status === "suspended";
12825
+ const stamp = this.suspendStamps.get(run.id);
12826
+ this.suspendStamps.delete(run.id);
12827
+ if (parkedTurn && cont && suspendedOnSteps && stamp !== void 0) {
12828
+ this.keepContinuation(cont, stamp);
12829
+ }
11605
12830
  return suspended;
11606
12831
  }
11607
12832
  if (this.cancelRequested.has(run.id)) {
@@ -11626,6 +12851,8 @@ ${run.workflowVersion}`;
11626
12851
  this.awaitedRemoteSteps.delete(run.id);
11627
12852
  }
11628
12853
  if (this.awaitedStepFans.get(run.id) === awaitedFans) this.awaitedStepFans.delete(run.id);
12854
+ if (cont && this.turnContinuations.get(run.id) === cont) this.turnContinuations.delete(run.id);
12855
+ this.suspendStamps.delete(run.id);
11629
12856
  await this.releaseLeaseBestEffort(run.id);
11630
12857
  if (suspendedOnFan) {
11631
12858
  this.inBackground(this.redriveSettledChildren(run.id, suspendedOnFan));
@@ -11843,10 +13070,11 @@ ${run.workflowVersion}`;
11843
13070
  /** The seam handed to {@link createWorkflowCtx}: the authoring API reaches durability + lifecycle
11844
13071
  * (checkpointing, dispatch, child start) through this, so the ctx primitives live in their own
11845
13072
  * module and the engine stays the orchestrator. */
11846
- ctxHostFor(replay, parentNamespace) {
13073
+ ctxHostFor(replay, parentNamespace, sticky) {
11847
13074
  return {
11848
13075
  store: this.store,
11849
13076
  replay,
13077
+ sticky,
11850
13078
  clock: this.clock,
11851
13079
  webhookUrl: this.webhookUrl,
11852
13080
  startStep: /* @__PURE__ */ __name((s) => this.startStep(s), "startStep"),
@@ -11855,9 +13083,27 @@ ${run.workflowVersion}`;
11855
13083
  // Route the dispatch by the RUN's tenant (same reason as `startChild` below): the executing
11856
13084
  // engine may be an operator driving every namespace, so the token must come from the run, not
11857
13085
  // 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"),
13086
+ callRemote: /* @__PURE__ */ __name((runId, seq, step, input, queue, transport, admission) => {
13087
+ const routing = {
13088
+ namespace: parentNamespace
13089
+ };
13090
+ const call = this.callRemote(runId, seq, step, input, queue, transport, replay, admission, {
13091
+ namespace: parentNamespace
13092
+ });
13093
+ if (!sticky) return call;
13094
+ return call.catch((err) => {
13095
+ if (!(err instanceof WorkflowSuspended) || !sticky.attached) throw err;
13096
+ return sticky.park(seq, err.wakeAt, (fresh) => this.callRemote(runId, seq, step, input, queue, transport, fresh ? /* @__PURE__ */ new Map([
13097
+ [
13098
+ seq,
13099
+ fresh
13100
+ ]
13101
+ ]) : void 0, admission, {
13102
+ ...routing,
13103
+ fresh: true
13104
+ }));
13105
+ });
13106
+ }, "callRemote"),
11861
13107
  callRemoteAll: /* @__PURE__ */ __name((runId, positions, step, inputs, opts) => this.callRemoteAll(runId, positions, step, inputs, opts, {
11862
13108
  namespace: parentNamespace
11863
13109
  }, replay), "callRemoteAll"),
@@ -11867,15 +13113,29 @@ ${run.workflowVersion}`;
11867
13113
  // namespace) is the one executing the parent. Otherwise an operator recovery-resuming a
11868
13114
  // `davi-local` pipeline would stamp its `processing` child `default` and it would leak off the
11869
13115
  // tenant's worker pool onto the shared/dev workers.
11870
- startChild: /* @__PURE__ */ __name((workflow, input, id, priority, version) => {
13116
+ startChild: /* @__PURE__ */ __name((workflow, input, id, priority, version, parentClose) => {
11871
13117
  this.startChildDeferred(workflow, input, id, {
11872
13118
  priority,
11873
13119
  namespace: parentNamespace,
11874
13120
  // Absent unless the body pinned one (`ctx.child(ref, input, { version })`) — an omitted
11875
13121
  // pin must stay omitted, so the child keeps resolving `latest` exactly as before.
11876
- version
13122
+ version,
13123
+ // The policy travels with the child, so a cancel cascade run by ANY instance can read it.
13124
+ ...parentClose ? {
13125
+ tags: [
13126
+ `${PARENT_CLOSE_TAG_PREFIX}${parentClose}`
13127
+ ]
13128
+ } : {}
11877
13129
  });
11878
13130
  }, "startChild"),
13131
+ noteParentClose: /* @__PURE__ */ __name((parentRunId, childId, policy) => {
13132
+ let intents = this.parentCloseIntents.get(parentRunId);
13133
+ if (!intents) {
13134
+ intents = /* @__PURE__ */ new Map();
13135
+ this.parentCloseIntents.set(parentRunId, intents);
13136
+ }
13137
+ intents.set(childId, policy);
13138
+ }, "noteParentClose"),
11879
13139
  // Deferred for the same reentrancy reason as `startChild` above. `cancel()` is already
11880
13140
  // idempotent on a terminal/cancelled run (returns its existing status without side effects), so
11881
13141
  // no extra guard is needed here for the failFast replay case (re-issuing the same cancel calls).
@@ -11985,17 +13245,35 @@ ${run.workflowVersion}`;
11985
13245
  * - a `routable` plan — ≥1 live worker is capability-capable AND protocol-compatible (dispatch);
11986
13246
  * - a `blocked` plan — descriptors exist but none can run it (park the run, never dispatch into it).
11987
13247
  */
11988
- async planRoute(token, requires) {
11989
- const descriptors = await this.pool.readWorkerDescriptors(token);
13248
+ async planRoute(token, requires, fresh = false) {
13249
+ const descriptors = await this.liveWorkerDescriptors(token, fresh);
11990
13250
  if (descriptors.length === 0) return void 0;
11991
13251
  return planDispatch(requires ?? [], descriptors, this.cpDescriptor, token);
11992
13252
  }
13253
+ /** The live descriptors for `token`, reused for {@link workerDescriptorCacheMs}; one read in flight. */
13254
+ liveWorkerDescriptors(token, fresh = false) {
13255
+ if (this.workerDescriptorCacheMs <= 0) return this.pool.readWorkerDescriptors(token);
13256
+ const now = Date.now();
13257
+ const cached = this.descriptorCache.get(token);
13258
+ if (!fresh && cached && now - cached.at < this.workerDescriptorCacheMs) return cached.read;
13259
+ const read = this.pool.readWorkerDescriptors(token);
13260
+ const entry = {
13261
+ at: now,
13262
+ read
13263
+ };
13264
+ this.descriptorCache.set(token, entry);
13265
+ read.catch(() => {
13266
+ if (this.descriptorCache.get(token) === entry) this.descriptorCache.delete(token);
13267
+ });
13268
+ return read;
13269
+ }
11993
13270
  /** Throw {@link WorkflowBlocked} when no live worker can run a dispatch to `token` — so the caller's
11994
13271
  * run-execution catch parks the run `blocked` (with the plan) instead of persisting a `pending`
11995
13272
  * checkpoint + dispatching into a queue nobody consumes (design §7.5). A no-op when the guard does
11996
13273
  * not engage (no descriptors) or a capable+compatible worker exists. */
11997
13274
  async ensureRoutable(token, requires) {
11998
- const plan = await this.planRoute(token, requires);
13275
+ let plan = await this.planRoute(token, requires);
13276
+ if (plan?.status === "blocked") plan = await this.planRoute(token, requires, true);
11999
13277
  if (plan?.status === "blocked") throw new WorkflowBlocked(plan);
12000
13278
  }
12001
13279
  /**
@@ -12053,8 +13331,11 @@ ${run.workflowVersion}`;
12053
13331
  }
12054
13332
  async callRemote(runId, seq, step, input, queue, transport, replay, admission, routing) {
12055
13333
  const group = stepGroup(step, routing?.namespace);
13334
+ 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)) {
13335
+ throw new WorkflowSuspended();
13336
+ }
12056
13337
  let existing = replay?.get(seq) ?? await this.store.getCheckpoint(runId, seq);
12057
- if (existing?.status === "pending" && replay?.has(seq)) {
13338
+ if (existing?.status === "pending" && replay?.has(seq) && !routing?.fresh) {
12058
13339
  existing = await this.store.getCheckpoint(runId, seq) ?? existing;
12059
13340
  }
12060
13341
  if (existing && existing.name !== step.name) {
@@ -12114,7 +13395,9 @@ ${run.workflowVersion}`;
12114
13395
  input: existing.input,
12115
13396
  priority: admission?.priority,
12116
13397
  attempt: reAttempt,
12117
- transport
13398
+ transport,
13399
+ // A fan's steps are joined by its barrier, not a parked body: their results stay shared.
13400
+ replyHere: routing?.parallelGroup === void 0
12118
13401
  });
12119
13402
  this.emit({
12120
13403
  type: "step.started",
@@ -12186,7 +13469,8 @@ ${run.workflowVersion}`;
12186
13469
  input,
12187
13470
  priority: admission?.priority,
12188
13471
  attempt,
12189
- transport
13472
+ transport,
13473
+ replyHere: routing?.parallelGroup === void 0
12190
13474
  });
12191
13475
  this.emit({
12192
13476
  type: "step.started",
@@ -12199,6 +13483,61 @@ ${run.workflowVersion}`;
12199
13483
  throw new WorkflowSuspended();
12200
13484
  }
12201
13485
  /**
13486
+ * {@link callRemote}'s first dispatch of a step, written with
13487
+ * {@link StateStore.insertCheckpointIfAbsent}: resolves whether it wrote the `pending` row and
13488
+ * dispatched. `false` when the position already has a row, or when no live worker can take the
13489
+ * step (the read-first path then decides, so a step that is in fact done is never blocked).
13490
+ */
13491
+ async dispatchIfAbsent(runId, seq, step, input, group, transport, admission, routing) {
13492
+ try {
13493
+ await this.ensureRoutable(group, step.requires);
13494
+ } catch (err) {
13495
+ if (err instanceof WorkflowBlocked) return false;
13496
+ throw err;
13497
+ }
13498
+ const id = stepId(runId, seq);
13499
+ const enqueuedAt = /* @__PURE__ */ new Date();
13500
+ const wrote = await this.store.insertCheckpointIfAbsent.call(this.store, {
13501
+ runId,
13502
+ seq,
13503
+ name: step.name,
13504
+ kind: "remote",
13505
+ stepId: id,
13506
+ status: "pending",
13507
+ input,
13508
+ attempts: 1,
13509
+ workerGroup: group,
13510
+ ...routing?.parallelGroup !== void 0 ? {
13511
+ parallelGroup: routing.parallelGroup
13512
+ } : {},
13513
+ enqueuedAt,
13514
+ startedAt: enqueuedAt,
13515
+ finishedAt: enqueuedAt
13516
+ });
13517
+ if (!wrote) return false;
13518
+ await this.dispatchRemoteTask({
13519
+ runId,
13520
+ seq,
13521
+ name: step.name,
13522
+ stepId: id,
13523
+ group,
13524
+ input,
13525
+ priority: admission?.priority,
13526
+ attempt: 1,
13527
+ transport,
13528
+ replyHere: routing?.parallelGroup === void 0
13529
+ });
13530
+ this.emit({
13531
+ type: "step.started",
13532
+ runId,
13533
+ seq,
13534
+ name: step.name,
13535
+ kind: "remote"
13536
+ });
13537
+ this.awaitedRemoteSteps.get(runId)?.add(seq);
13538
+ return true;
13539
+ }
13540
+ /**
12202
13541
  * Whether a `pending` step a turn just read only waits for its result: {@link callRemote} would
12203
13542
  * suspend on it without writing (no redispatch deadline to stamp, the one stamped not yet due).
12204
13543
  */
@@ -12454,6 +13793,69 @@ ${run.workflowVersion}`;
12454
13793
  }
12455
13794
  }
12456
13795
  /**
13796
+ * Whether this turn of `run` may run in a sticky continuation (see `sticky-continuation.ts`). Not
13797
+ * while the engine drains, for a cancel being compensated, nor when execution wrappers are
13798
+ * registered: a resumed body runs in the async scope of the turn that started it, and a wrapper
13799
+ * (a span, a log batch) expects one scope per turn.
13800
+ */
13801
+ stickyEligible(run) {
13802
+ return this.sticky.enabled && !this.draining && run.status !== "cancelling" && !this.cancelRequested.has(run.id) && !hasDurableExecutionWrappers();
13803
+ }
13804
+ /**
13805
+ * The continuation this instance parked for `run`, if it is still the run's latest turn: the run
13806
+ * is `suspended` with the very stamp this instance's suspend wrote, on the same body and version.
13807
+ * Any other settle in between (another instance's turn, a retry, a cancel) changed the stamp, and
13808
+ * the run replays instead. Taking it removes it from the cache.
13809
+ */
13810
+ takeContinuation(run, fn) {
13811
+ const cont = this.sticky.take(run.id);
13812
+ if (!cont) return void 0;
13813
+ const valid = cont.fn === fn && cont.workflowVersion === run.workflowVersion && run.status === "suspended" && cont.stamp !== void 0 && run.updatedAt?.getTime() === cont.stamp;
13814
+ if (!valid) {
13815
+ this.sticky.misses += 1;
13816
+ return void 0;
13817
+ }
13818
+ this.sticky.hits += 1;
13819
+ return cont;
13820
+ }
13821
+ /** Keep `cont` (parked, detached) for the run's next turn here, stamped with its suspend write. */
13822
+ keepContinuation(cont, stamp) {
13823
+ if (cont.unusable) return;
13824
+ cont.stamp = stamp;
13825
+ cont.replay.clear();
13826
+ this.sticky.put(cont);
13827
+ }
13828
+ /**
13829
+ * The checkpoints of the steps `cont` is parked on, as a replay would see them: in one read when
13830
+ * the store batches, and without the input of a step that completed (what
13831
+ * {@link StateStore.getCheckpointsForSettle} leaves out). A step still open or failed is read
13832
+ * whole, since a re-dispatch or a retry sends its input again.
13833
+ */
13834
+ async readParked(runId, cont) {
13835
+ const fresh = /* @__PURE__ */ new Map();
13836
+ for (const seq of cont.parked.keys()) fresh.set(seq, cont.hints.get(seq) ?? null);
13837
+ cont.hints.clear();
13838
+ const seqs = [
13839
+ ...fresh
13840
+ ].flatMap(([seq, cp]) => cp ? [] : [
13841
+ seq
13842
+ ]);
13843
+ if (seqs.length === 0) return fresh;
13844
+ const lean = this.store.getCheckpointsForSettle ?? this.store.getCheckpoints;
13845
+ if (lean) {
13846
+ for (const cp of await lean.call(this.store, runId, seqs)) fresh.set(cp.seq, cp);
13847
+ } else {
13848
+ for (const seq of seqs) fresh.set(seq, await this.store.getCheckpoint(runId, seq));
13849
+ }
13850
+ if (lean === this.store.getCheckpointsForSettle) {
13851
+ for (const seq of seqs) {
13852
+ const cp = fresh.get(seq);
13853
+ if (cp && cp.status !== "completed") fresh.set(seq, await this.store.getCheckpoint(runId, seq));
13854
+ }
13855
+ }
13856
+ return fresh;
13857
+ }
13858
+ /**
12457
13859
  * The run's checkpoints as a turn replays them: {@link StateStore.listCheckpointsForReplay} when
12458
13860
  * the store has it (no input on a completed row), else {@link StateStore.listCheckpoints}.
12459
13861
  */
@@ -12493,8 +13895,8 @@ ${run.workflowVersion}`;
12493
13895
  * whole, the same way.
12494
13896
  */
12495
13897
  async settlePendingCheckpoints(settlements, settled) {
12496
- const settle = this.store.settleCheckpointsIfPending?.bind(this.store);
12497
- if (!settle) {
13898
+ const settle2 = this.store.settleCheckpointsIfPending?.bind(this.store);
13899
+ if (!settle2) {
12498
13900
  return this.savePendingCheckpoints(settlements.map((s, i) => ({
12499
13901
  ...settled[i],
12500
13902
  ...s
@@ -12502,12 +13904,12 @@ ${run.workflowVersion}`;
12502
13904
  }
12503
13905
  if (settlements.length > 1) {
12504
13906
  try {
12505
- const wrote = await settle(settlements);
13907
+ const wrote = await settle2(settlements);
12506
13908
  return settlements.map((_, i) => wrote[i] === true);
12507
13909
  } catch {
12508
13910
  }
12509
13911
  }
12510
- const one = await Promise.allSettled(settlements.map((s) => settle([
13912
+ const one = await Promise.allSettled(settlements.map((s) => settle2([
12511
13913
  s
12512
13914
  ])));
12513
13915
  return one.map((s) => s.status === "fulfilled" ? s.value[0] === true : {
@@ -12568,7 +13970,10 @@ ${run.workflowVersion}`;
12568
13970
  traceparent: this.traceparent?.(),
12569
13971
  context: this.context?.(),
12570
13972
  priority: task.priority,
12571
- attempt: task.attempt
13973
+ attempt: task.attempt,
13974
+ ...task.replyHere && this.repliesRouted && !this.draining ? {
13975
+ replyTo: this.instanceId
13976
+ } : {}
12572
13977
  }, task.transport);
12573
13978
  }
12574
13979
  /**
@@ -12660,6 +14065,9 @@ ${run.workflowVersion}`;
12660
14065
  i
12661
14066
  ];
12662
14067
  });
14068
+ if (this.store.settleResultsIfPending) {
14069
+ return this.settleResultsReturning(runId, results, live, outcomes, queuedIn);
14070
+ }
12663
14071
  const reads = await this.readCheckpoints(runId, live.map((i) => results[i].seq));
12664
14072
  const pending = [];
12665
14073
  const resettled = [];
@@ -12739,6 +14147,110 @@ ${run.workflowVersion}`;
12739
14147
  return wake.applyTo(outcomes);
12740
14148
  }
12741
14149
  /**
14150
+ * {@link completeRemoteResults} on a store with {@link StateStore.settleResultsIfPending}: the
14151
+ * results settle their `pending` rows and read them back in one call, instead of a read of the rows
14152
+ * followed by the write. Only an entry that wrote nothing (a redelivery, or a step gone) is read
14153
+ * afterwards. A settled `completed` row is also handed to the run's parked body, if this instance
14154
+ * holds one, so its next turn does not read it again.
14155
+ */
14156
+ async settleResultsReturning(runId, results, live, outcomes, queuedIn) {
14157
+ const settle2 = this.store.settleResultsIfPending.bind(this.store);
14158
+ const finishedAt = /* @__PURE__ */ new Date();
14159
+ const entries = live.map((i) => {
14160
+ const result = results[i];
14161
+ return {
14162
+ runId,
14163
+ seq: result.seq,
14164
+ status: result.status,
14165
+ output: result.status === "completed" ? result.output : void 0,
14166
+ error: result.error,
14167
+ events: result.events,
14168
+ startedAt: result.startedAt ? new Date(result.startedAt) : void 0,
14169
+ finishedAt
14170
+ };
14171
+ });
14172
+ let rows;
14173
+ try {
14174
+ rows = await settle2(entries);
14175
+ } catch (err) {
14176
+ if (entries.length === 1) {
14177
+ rows = [
14178
+ {
14179
+ error: err
14180
+ }
14181
+ ];
14182
+ } else {
14183
+ const one = await Promise.allSettled(entries.map((e) => settle2([
14184
+ e
14185
+ ])));
14186
+ rows = one.map((o) => o.status === "fulfilled" ? o.value[0] ?? null : {
14187
+ error: o.reason
14188
+ });
14189
+ }
14190
+ }
14191
+ const settled = [];
14192
+ const unsettled = [];
14193
+ rows.forEach((row, k) => {
14194
+ const index = live[k];
14195
+ if (row === null) unsettled.push(index);
14196
+ else if ("error" in row && !("seq" in row)) outcomes[index] = {
14197
+ error: row.error
14198
+ };
14199
+ else settled.push({
14200
+ index,
14201
+ cp: row,
14202
+ result: results[index]
14203
+ });
14204
+ });
14205
+ const parked = this.sticky.peek(runId) ?? this.turnContinuations.get(runId);
14206
+ for (const { cp, result } of settled) {
14207
+ void this.releaseQueueSlot(cp.stepId).catch(() => void 0);
14208
+ if (cp.status === "completed") parked?.hint(cp);
14209
+ if (!parseStepFanGroup(cp.parallelGroup)) this.noteSettledHere(`${runId}:${result.seq}`);
14210
+ this.emit({
14211
+ type: result.status === "completed" ? "step.completed" : "step.failed",
14212
+ runId,
14213
+ seq: result.seq,
14214
+ name: cp.name,
14215
+ kind: cp.kind,
14216
+ output: result.output,
14217
+ error: result.error,
14218
+ queueMs: cp.startedAt.getTime() - cp.enqueuedAt.getTime(),
14219
+ durationMs: finishedAt.getTime() - cp.startedAt.getTime()
14220
+ });
14221
+ }
14222
+ if (settled.length === 0) {
14223
+ if (unsettled.length === 0) return outcomes;
14224
+ const reads = await this.readCheckpoints(runId, unsettled.map((i) => results[i].seq));
14225
+ const resettled = [];
14226
+ reads.forEach((read, k) => {
14227
+ const index = unsettled[k];
14228
+ if (read.status === "rejected") outcomes[index] = {
14229
+ error: read.reason
14230
+ };
14231
+ else if (read.value?.status === "completed" || read.value?.status === "failed") {
14232
+ resettled.push({
14233
+ index,
14234
+ cp: read.value
14235
+ });
14236
+ } else if (read.value?.status === "pending") {
14237
+ void this.releaseQueueSlot(read.value.stepId).catch(() => void 0);
14238
+ }
14239
+ });
14240
+ if (resettled.length === 0) return outcomes;
14241
+ return this.resumeForRedelivered(runId, resettled, outcomes, queuedIn);
14242
+ }
14243
+ const barriers = /* @__PURE__ */ new Map();
14244
+ const wake = new FanWakeCollector();
14245
+ for (const { cp } of settled) wake.note(cp);
14246
+ for (const { index, cp, result } of settled) {
14247
+ if (!await this.stepFanStillRunning(cp, result, barriers, queuedIn)) {
14248
+ wake.add(index, cp, result.status);
14249
+ }
14250
+ }
14251
+ return wake.applyTo(outcomes);
14252
+ }
14253
+ /**
12742
14254
  * The outcomes for a batch of redelivered results (their steps already settled): one of them
12743
14255
  * resumes the run if it is `suspended` and, for a step of a fan, the fan's barrier is open. A
12744
14256
  * resume of a run that had in fact moved on replays to its current wait and re-parks.
@@ -13628,6 +15140,15 @@ var CodecStateStore = class {
13628
15140
  /** Encodes each settlement's payloads like {@link saveCheckpoint}. Bound only when the inner store
13629
15141
  * has it, so the engine's whole-row fallback still kicks in through the wrapper. */
13630
15142
  settleCheckpointsIfPending;
15143
+ /** Encodes each result's payloads, decodes each row read back. Bound only when the inner store
15144
+ * has it. */
15145
+ settleResultsIfPending;
15146
+ /** Encodes like {@link saveCheckpoint}. Bound only when the inner store has it. */
15147
+ insertCheckpointIfAbsent;
15148
+ /** Decodes each run claimed. Bound only when the inner store has it. */
15149
+ claimPendingRuns;
15150
+ /** Decodes the run read. Bound only when the inner store has it. */
15151
+ tryLockRunAndGet;
13631
15152
  /** Encodes each checkpoint written. Bound only when the inner store has it. */
13632
15153
  saveCheckpoints;
13633
15154
  /** Decodes each checkpoint read like {@link listCheckpoints}. Bound only when the inner store has
@@ -13663,6 +15184,14 @@ var CodecStateStore = class {
13663
15184
  listBufferedSignals;
13664
15185
  /** Forwarded verbatim — deletes by row id. */
13665
15186
  deleteBufferedSignals;
15187
+ /** Decodes the payload like {@link takeBufferedSignal}. Bound only when the inner store has it. */
15188
+ takeOldestBufferedSignal;
15189
+ /** Forwarded verbatim — a signal id record carries no payload. */
15190
+ claimSignalId;
15191
+ /** Forwarded verbatim. */
15192
+ releaseSignalId;
15193
+ /** Forwarded verbatim. */
15194
+ deleteExpiredSignalIds;
13666
15195
  /** Forwarded verbatim — a waiter carries no payload. */
13667
15196
  listSignalWaitersByRun;
13668
15197
  /** Forwarded verbatim — a waiter carries no payload. */
@@ -13691,6 +15220,14 @@ var CodecStateStore = class {
13691
15220
  if (saveManyIfPending) this.saveCheckpointsIfPending = (cps) => saveManyIfPending.call(inner, cps.map((cp) => this.encCp(cp)));
13692
15221
  const settleMany = inner.settleCheckpointsIfPending;
13693
15222
  if (settleMany) this.settleCheckpointsIfPending = (settlements) => settleMany.call(inner, settlements.map((s) => this.encSettlement(s)));
15223
+ const settleResults = inner.settleResultsIfPending;
15224
+ if (settleResults) this.settleResultsIfPending = async (results) => (await settleResults.call(inner, results.map((r) => this.encSettlement(r)))).map((cp) => this.decCp(cp));
15225
+ const insertIfAbsent = inner.insertCheckpointIfAbsent;
15226
+ if (insertIfAbsent) this.insertCheckpointIfAbsent = (cp) => insertIfAbsent.call(inner, this.encCp(cp));
15227
+ const claim = inner.claimPendingRuns;
15228
+ if (claim) this.claimPendingRuns = async (limit, owner, leaseUntilMs, nowMs, namespace) => (await claim.call(inner, limit, owner, leaseUntilMs, nowMs, namespace)).map((r) => this.decRun(r));
15229
+ const lockAndGet = inner.tryLockRunAndGet;
15230
+ if (lockAndGet) this.tryLockRunAndGet = async (runId, owner, leaseUntilMs, nowMs) => this.decRun(await lockAndGet.call(inner, runId, owner, leaseUntilMs, nowMs));
13694
15231
  const saveMany = inner.saveCheckpoints;
13695
15232
  if (saveMany) this.saveCheckpoints = (cps) => saveMany.call(inner, cps.map((cp) => this.encCp(cp)));
13696
15233
  const listForReplay = inner.listCheckpointsForReplay;
@@ -13727,6 +15264,18 @@ var CodecStateStore = class {
13727
15264
  }));
13728
15265
  const deleteBuffered = inner.deleteBufferedSignals;
13729
15266
  if (deleteBuffered) this.deleteBufferedSignals = (ids) => deleteBuffered.call(inner, ids);
15267
+ const takeOldest = inner.takeOldestBufferedSignal;
15268
+ if (takeOldest) this.takeOldestBufferedSignal = async (tokens) => {
15269
+ const taken = await takeOldest.call(inner, tokens);
15270
+ return taken ? {
15271
+ token: taken.token,
15272
+ payload: this.dec(taken.payload)
15273
+ } : null;
15274
+ };
15275
+ const { claimSignalId, releaseSignalId, deleteExpiredSignalIds } = inner;
15276
+ if (claimSignalId) this.claimSignalId = (token, id, expiresAt, nowMs) => claimSignalId.call(inner, token, id, expiresAt, nowMs);
15277
+ if (releaseSignalId) this.releaseSignalId = (token, id) => releaseSignalId.call(inner, token, id);
15278
+ if (deleteExpiredSignalIds) this.deleteExpiredSignalIds = (nowMs, limit) => deleteExpiredSignalIds.call(inner, nowMs, limit);
13730
15279
  const byRun = inner.listSignalWaitersByRun;
13731
15280
  if (byRun) this.listSignalWaitersByRun = (runIds) => byRun.call(inner, runIds);
13732
15281
  const hasWaiter = inner.hasSignalWaiter;
@@ -14511,6 +16060,87 @@ var InMemoryStateStore = class {
14511
16060
  return true;
14512
16061
  });
14513
16062
  }
16063
+ // The three round-trip-saving methods below are built on the plain ones, so a test store that
16064
+ // overrides `tryLockRun`, `getRun`, `getCheckpoint` or a settle to stage a race keeps staging it.
16065
+ // They are OFF unless asked for (`roundTripMethods`, or `DURABLE_TEST_ROUND_TRIP_METHODS=1` for a
16066
+ // whole suite): many specs pin the store calls the engine makes without them.
16067
+ settleResultsIfPending;
16068
+ insertCheckpointIfAbsent;
16069
+ tryLockRunAndGet;
16070
+ claimPendingRuns;
16071
+ constructor(opts = {}) {
16072
+ const on = opts.roundTripMethods ?? (typeof process !== "undefined" && process.env.DURABLE_TEST_ROUND_TRIP_METHODS === "1");
16073
+ if (on) {
16074
+ this.settleResultsIfPending = (results) => this.settleResultsFallback(results);
16075
+ this.insertCheckpointIfAbsent = (cp) => this.insertIfAbsentFallback(cp);
16076
+ this.tryLockRunAndGet = (runId, owner, until, now) => this.lockAndGetFallback(runId, owner, until, now);
16077
+ this.claimPendingRuns = async (limit, owner, until, now, namespace) => {
16078
+ const claimed = [];
16079
+ for (const run of await this.listPendingRuns(Number.MAX_SAFE_INTEGER, namespace)) {
16080
+ if (claimed.length >= limit) break;
16081
+ const locked = await this.lockAndGetFallback(run.id, owner, until, now);
16082
+ if (locked) claimed.push(locked);
16083
+ }
16084
+ return claimed;
16085
+ };
16086
+ }
16087
+ }
16088
+ async settleResultsFallback(results) {
16089
+ const out = results.map(() => null);
16090
+ const before = /* @__PURE__ */ new Map();
16091
+ const seqsByRun = /* @__PURE__ */ new Map();
16092
+ for (const r of results) seqsByRun.set(r.runId, [
16093
+ ...seqsByRun.get(r.runId) ?? [],
16094
+ r.seq
16095
+ ]);
16096
+ for (const [runId, seqs] of seqsByRun) {
16097
+ for (const cp of await this.getCheckpointsForSettle(runId, seqs)) {
16098
+ before.set(this.key(runId, cp.seq), cp);
16099
+ }
16100
+ }
16101
+ const claimed = /* @__PURE__ */ new Set();
16102
+ const settlements = [];
16103
+ const at = [];
16104
+ for (const [i, r] of results.entries()) {
16105
+ const key = this.key(r.runId, r.seq);
16106
+ const cp = before.get(key);
16107
+ if (cp?.status !== "pending" || claimed.has(key)) continue;
16108
+ const status = (await this.getRun(r.runId))?.status;
16109
+ if (status === "cancelled" || status === "completed") continue;
16110
+ claimed.add(key);
16111
+ settlements.push({
16112
+ runId: r.runId,
16113
+ seq: r.seq,
16114
+ status: r.status,
16115
+ output: r.status === "completed" ? r.output : cp.output,
16116
+ error: r.error,
16117
+ events: r.events ?? cp.events,
16118
+ startedAt: r.startedAt ?? cp.startedAt,
16119
+ finishedAt: r.finishedAt
16120
+ });
16121
+ at.push(i);
16122
+ }
16123
+ if (settlements.length === 0) return out;
16124
+ const wrote = await this.settleCheckpointsIfPending(settlements);
16125
+ settlements.forEach((settlement, k) => {
16126
+ if (!wrote[k]) return;
16127
+ const cp = before.get(this.key(settlement.runId, settlement.seq));
16128
+ out[at[k]] = {
16129
+ ...cp,
16130
+ ...settlement
16131
+ };
16132
+ });
16133
+ return out;
16134
+ }
16135
+ async insertIfAbsentFallback(checkpoint) {
16136
+ if (await this.getCheckpoint(checkpoint.runId, checkpoint.seq)) return false;
16137
+ await this.saveCheckpoint(checkpoint);
16138
+ return true;
16139
+ }
16140
+ async lockAndGetFallback(runId, owner, leaseUntilMs, nowMs) {
16141
+ if (!await this.tryLockRun(runId, owner, leaseUntilMs, nowMs)) return null;
16142
+ return this.getRun(runId);
16143
+ }
14514
16144
  async listOrphanedRuns(nowMs, limit, namespace, ownedBy) {
14515
16145
  return [
14516
16146
  ...this.runs.values()
@@ -14641,6 +16271,45 @@ var InMemoryStateStore = class {
14641
16271
  payload: head.payload
14642
16272
  };
14643
16273
  }
16274
+ async takeOldestBufferedSignal(tokens) {
16275
+ let oldest;
16276
+ for (const token of new Set(tokens)) {
16277
+ const head = this.bufferedSignals.get(token)?.[0];
16278
+ if (head && (!oldest || head.id < oldest.id)) oldest = {
16279
+ token,
16280
+ id: head.id
16281
+ };
16282
+ }
16283
+ if (!oldest) return null;
16284
+ const taken = await this.takeBufferedSignal(oldest.token);
16285
+ return taken ? {
16286
+ token: oldest.token,
16287
+ payload: taken.payload
16288
+ } : null;
16289
+ }
16290
+ /** `token \u0000 id -> expiresAt` of the signal ids {@link claimSignalId} recorded. */
16291
+ signalIds = /* @__PURE__ */ new Map();
16292
+ async claimSignalId(token, id, expiresAt, nowMs) {
16293
+ const key = `${token}\0${id}`;
16294
+ const existing = this.signalIds.get(key);
16295
+ if (existing !== void 0 && existing > nowMs) return false;
16296
+ this.signalIds.set(key, expiresAt);
16297
+ return true;
16298
+ }
16299
+ async releaseSignalId(token, id) {
16300
+ this.signalIds.delete(`${token}\0${id}`);
16301
+ }
16302
+ async deleteExpiredSignalIds(nowMs, limit) {
16303
+ let deleted = 0;
16304
+ for (const [key, expiresAt] of this.signalIds) {
16305
+ if (deleted >= limit) break;
16306
+ if (expiresAt <= nowMs) {
16307
+ this.signalIds.delete(key);
16308
+ deleted += 1;
16309
+ }
16310
+ }
16311
+ return deleted;
16312
+ }
14644
16313
  async listBufferedSignals(tokenPrefix, afterId, limit) {
14645
16314
  const after = afterId === void 0 ? 0 : Number(afterId);
14646
16315
  const rows = [];
@@ -15028,9 +16697,74 @@ var InMemoryRunQueue = class {
15028
16697
  }
15029
16698
  }
15030
16699
  };
16700
+
16701
+ // src/settle-results.ts
16702
+ async function settleResultsWithPlainCalls(store, results) {
16703
+ const out = results.map(() => null);
16704
+ const key = /* @__PURE__ */ __name((runId, seq) => `${seq}:${runId}`, "key");
16705
+ const seqsByRun = /* @__PURE__ */ new Map();
16706
+ for (const r of results) seqsByRun.set(r.runId, [
16707
+ ...seqsByRun.get(r.runId) ?? [],
16708
+ r.seq
16709
+ ]);
16710
+ const runIds = [
16711
+ ...seqsByRun.keys()
16712
+ ];
16713
+ const runs = store.getRuns ? await store.getRuns(runIds) : (await Promise.all(runIds.map((id) => store.getRun(id)))).filter((r) => r !== null);
16714
+ const finished = new Set(runs.filter((r) => r.status === "cancelled" || r.status === "completed").map((r) => r.id));
16715
+ const read = /* @__PURE__ */ __name(async (runId, seqs) => {
16716
+ if (store.getCheckpointsForSettle) return store.getCheckpointsForSettle(runId, seqs);
16717
+ if (store.getCheckpoints) return store.getCheckpoints(runId, seqs);
16718
+ const rows = await Promise.all(seqs.map((seq) => store.getCheckpoint(runId, seq)));
16719
+ return rows.filter((cp) => cp !== null);
16720
+ }, "read");
16721
+ const before = /* @__PURE__ */ new Map();
16722
+ for (const [runId, seqs] of seqsByRun) {
16723
+ if (finished.has(runId)) continue;
16724
+ for (const cp of await read(runId, seqs)) before.set(key(runId, cp.seq), cp);
16725
+ }
16726
+ const claimed = /* @__PURE__ */ new Set();
16727
+ const settlements = [];
16728
+ const at = [];
16729
+ for (const [i, r] of results.entries()) {
16730
+ const k = key(r.runId, r.seq);
16731
+ const cp = before.get(k);
16732
+ if (cp?.status !== "pending" || claimed.has(k)) continue;
16733
+ claimed.add(k);
16734
+ settlements.push({
16735
+ runId: r.runId,
16736
+ seq: r.seq,
16737
+ status: r.status,
16738
+ output: r.status === "completed" ? r.output : cp.output,
16739
+ error: r.error,
16740
+ events: r.events ?? cp.events,
16741
+ startedAt: r.startedAt ?? cp.startedAt,
16742
+ finishedAt: r.finishedAt
16743
+ });
16744
+ at.push(i);
16745
+ }
16746
+ if (settlements.length === 0 || !store.settleCheckpointsIfPending) return out;
16747
+ const wrote = await store.settleCheckpointsIfPending(settlements);
16748
+ const won = settlements.filter((_, j) => wrote[j] === true);
16749
+ const after = /* @__PURE__ */ new Map();
16750
+ const wonByRun = /* @__PURE__ */ new Map();
16751
+ for (const s of won) wonByRun.set(s.runId, [
16752
+ ...wonByRun.get(s.runId) ?? [],
16753
+ s.seq
16754
+ ]);
16755
+ for (const [runId, seqs] of wonByRun) {
16756
+ for (const cp of await read(runId, seqs)) after.set(key(runId, cp.seq), cp);
16757
+ }
16758
+ settlements.forEach((s, j) => {
16759
+ if (wrote[j] === true) out[at[j]] = after.get(key(s.runId, s.seq)) ?? null;
16760
+ });
16761
+ return out;
16762
+ }
16763
+ __name(settleResultsWithPlainCalls, "settleResultsWithPlainCalls");
15031
16764
  // Annotate the CommonJS export names for ESM import in node:
15032
16765
  0 && (module.exports = {
15033
16766
  ACCUMULATOR_SEEN_MAX,
16767
+ ANY_WAIT_GROUP,
15034
16768
  CHILD_EDGE_PREFIXES,
15035
16769
  CONTROL_FLOW_SIGNAL,
15036
16770
  CORE_ORIGIN,
@@ -15077,6 +16811,7 @@ var InMemoryRunQueue = class {
15077
16811
  LIFECYCLE_TRIGGER_FAILED_TAG_PREFIX,
15078
16812
  NestedWorkflowCallError,
15079
16813
  NonDeterminismError,
16814
+ PARENT_CLOSE_TAG_PREFIX,
15080
16815
  QueueController,
15081
16816
  REMOTE_EVENT,
15082
16817
  RUN_HISTORY_FIXTURE_VERSION,
@@ -15116,6 +16851,7 @@ var InMemoryRunQueue = class {
15116
16851
  aggregateAnnouncements,
15117
16852
  aggregatesCheckpoints,
15118
16853
  announcementsOf,
16854
+ anyCheckpointName,
15119
16855
  applyAttributeQuery,
15120
16856
  assertConcurrency,
15121
16857
  assertRoutes,
@@ -15172,6 +16908,8 @@ var InMemoryRunQueue = class {
15172
16908
  hmacTenantVerifier,
15173
16909
  indexWaitersByRun,
15174
16910
  info,
16911
+ isAnyCheckpoint,
16912
+ isAnyMember,
15175
16913
  isEngineMintedTag,
15176
16914
  isEventToken,
15177
16915
  isFailedMember,
@@ -15201,6 +16939,7 @@ var InMemoryRunQueue = class {
15201
16939
  missingCapabilities,
15202
16940
  negotiate,
15203
16941
  nextCronFireMs,
16942
+ nextRRuleFireMs,
15204
16943
  normalizeAttributeRows,
15205
16944
  normalizeDescriptor,
15206
16945
  observedAnnouncements,
@@ -15211,11 +16950,13 @@ var InMemoryRunQueue = class {
15211
16950
  parentRunIdOf,
15212
16951
  parseDuration,
15213
16952
  parseLifecycleEvent,
16953
+ parseRRule,
15214
16954
  parseWebhookToken,
15215
16955
  partitionOfToken,
15216
16956
  planDispatch,
15217
16957
  poolNamespace,
15218
16958
  prevCronFireMs,
16959
+ prevRRuleFireMs,
15219
16960
  queryCheckpointsOf,
15220
16961
  readRunsByIds,
15221
16962
  readSearchAttributes,
@@ -15245,6 +16986,7 @@ var InMemoryRunQueue = class {
15245
16986
  sanitizeQueueToken,
15246
16987
  scheduleRunIdAt,
15247
16988
  scheduledRunId,
16989
+ settleResultsWithPlainCalls,
15248
16990
  signTenantToken,
15249
16991
  singletonAdmissionDenialPatch,
15250
16992
  singletonAdmissionTags,