@dudousxd/nestjs-durable-core 0.87.0 → 0.89.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -427,7 +427,10 @@ var WorkflowSuspended = class extends Error {
427
427
  /** Marks this as a control-flow signal — see {@link isWorkflowControlFlowSignal}. */
428
428
  [CONTROL_FLOW_SIGNAL] = true;
429
429
  constructor(wakeAt, fan) {
430
+ const limit = Error.stackTraceLimit;
431
+ Error.stackTraceLimit = 0;
430
432
  super("workflow suspended");
433
+ Error.stackTraceLimit = limit;
431
434
  this.name = "WorkflowSuspended";
432
435
  this.wakeAt = wakeAt;
433
436
  this.fan = fan;
@@ -771,6 +774,7 @@ function isNonEmptyRunScope(scope) {
771
774
  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;
772
775
  }
773
776
  __name(isNonEmptyRunScope, "isNonEmptyRunScope");
777
+ var PARENT_CLOSE_TAG_PREFIX = "parent-close:";
774
778
 
775
779
  // src/events.ts
776
780
  var enc = /* @__PURE__ */ __name((s) => Buffer.from(s, "utf8").toString("base64"), "enc");
@@ -2677,9 +2681,9 @@ __name(negotiate, "negotiate");
2677
2681
 
2678
2682
  // src/handshake/routing.ts
2679
2683
  function requiredCapabilities(handler) {
2680
- const list = Array.isArray(handler) ? handler : handler.requires ?? [];
2684
+ const list2 = Array.isArray(handler) ? handler : handler.requires ?? [];
2681
2685
  return [
2682
- ...new Set(list)
2686
+ ...new Set(list2)
2683
2687
  ];
2684
2688
  }
2685
2689
  __name(requiredCapabilities, "requiredCapabilities");
@@ -2998,6 +3002,358 @@ var RunDispatchBatcher = class {
2998
3002
  }
2999
3003
  };
3000
3004
 
3005
+ // src/rrule.ts
3006
+ var FREQS = [
3007
+ "YEARLY",
3008
+ "MONTHLY",
3009
+ "WEEKLY",
3010
+ "DAILY",
3011
+ "HOURLY",
3012
+ "MINUTELY"
3013
+ ];
3014
+ var WEEKDAYS = [
3015
+ "SU",
3016
+ "MO",
3017
+ "TU",
3018
+ "WE",
3019
+ "TH",
3020
+ "FR",
3021
+ "SA"
3022
+ ];
3023
+ var SUPPORTED = /* @__PURE__ */ new Set([
3024
+ "FREQ",
3025
+ "INTERVAL",
3026
+ "COUNT",
3027
+ "UNTIL",
3028
+ "BYMONTH",
3029
+ "BYMONTHDAY",
3030
+ "BYDAY",
3031
+ "BYHOUR",
3032
+ "BYMINUTE",
3033
+ "BYSECOND",
3034
+ "WKST"
3035
+ ]);
3036
+ var DAY_MS = 864e5;
3037
+ var HORIZON_DAYS = 366 * 30;
3038
+ var formatters = /* @__PURE__ */ new Map();
3039
+ function formatter(timezone) {
3040
+ let f = formatters.get(timezone);
3041
+ if (!f) {
3042
+ f = new Intl.DateTimeFormat("en-US", {
3043
+ timeZone: timezone,
3044
+ hourCycle: "h23",
3045
+ year: "numeric",
3046
+ month: "numeric",
3047
+ day: "numeric",
3048
+ hour: "numeric",
3049
+ minute: "numeric",
3050
+ second: "numeric"
3051
+ });
3052
+ formatters.set(timezone, f);
3053
+ }
3054
+ return f;
3055
+ }
3056
+ __name(formatter, "formatter");
3057
+ function civilOf(ms, timezone) {
3058
+ const parts = {};
3059
+ for (const p of formatter(timezone).formatToParts(new Date(ms))) {
3060
+ if (p.type !== "literal") parts[p.type] = Number(p.value);
3061
+ }
3062
+ return {
3063
+ y: parts.year,
3064
+ m: parts.month,
3065
+ d: parts.day,
3066
+ h: parts.hour % 24,
3067
+ mi: parts.minute,
3068
+ s: parts.second
3069
+ };
3070
+ }
3071
+ __name(civilOf, "civilOf");
3072
+ function civilAsUtc(c) {
3073
+ return Date.UTC(c.y, c.m - 1, c.d, c.h, c.mi, c.s);
3074
+ }
3075
+ __name(civilAsUtc, "civilAsUtc");
3076
+ function toInstant(c, timezone) {
3077
+ const wall = civilAsUtc(c);
3078
+ const candidates = /* @__PURE__ */ new Set();
3079
+ for (const probe of [
3080
+ wall - DAY_MS,
3081
+ wall,
3082
+ wall + DAY_MS
3083
+ ]) {
3084
+ const offset = civilAsUtc(civilOf(probe, timezone)) - probe;
3085
+ candidates.add(wall - offset);
3086
+ }
3087
+ const exact = [
3088
+ ...candidates
3089
+ ].filter((t) => civilAsUtc(civilOf(t, timezone)) === wall);
3090
+ return exact.length > 0 ? Math.min(...exact) : null;
3091
+ }
3092
+ __name(toInstant, "toInstant");
3093
+ function dayNumber(y, m, d) {
3094
+ return Math.floor(Date.UTC(y, m - 1, d) / DAY_MS);
3095
+ }
3096
+ __name(dayNumber, "dayNumber");
3097
+ function fromDayNumber(n) {
3098
+ const date = new Date(n * DAY_MS);
3099
+ return {
3100
+ y: date.getUTCFullYear(),
3101
+ m: date.getUTCMonth() + 1,
3102
+ d: date.getUTCDate(),
3103
+ weekday: date.getUTCDay()
3104
+ };
3105
+ }
3106
+ __name(fromDayNumber, "fromDayNumber");
3107
+ function daysInMonth(y, m) {
3108
+ return new Date(Date.UTC(y, m, 0)).getUTCDate();
3109
+ }
3110
+ __name(daysInMonth, "daysInMonth");
3111
+ function list(value, part, min, max, allowNegative = false) {
3112
+ return value.split(",").map((raw) => {
3113
+ const n = Number(raw);
3114
+ const ok = Number.isInteger(n) && (allowNegative ? n !== 0 && Math.abs(n) <= max : n >= min && n <= max);
3115
+ if (!ok) throw new Error(`rrule: invalid ${part} value "${raw}"`);
3116
+ return n;
3117
+ });
3118
+ }
3119
+ __name(list, "list");
3120
+ function parseDateTime(value, timezone, part) {
3121
+ const match = /^(\d{4})(\d{2})(\d{2})(?:T(\d{2})(\d{2})(\d{2})(Z)?)?$/.exec(value);
3122
+ if (!match) throw new Error(`rrule: invalid ${part} "${value}"`);
3123
+ const [, y, m, d, h = "0", mi = "0", s = "0", z] = match;
3124
+ const civil = {
3125
+ y: Number(y),
3126
+ m: Number(m),
3127
+ d: Number(d),
3128
+ h: +h,
3129
+ mi: +mi,
3130
+ s: +s
3131
+ };
3132
+ if (z) return civilAsUtc(civil);
3133
+ const instant = toInstant(civil, timezone);
3134
+ if (instant === null) throw new Error(`rrule: ${part} "${value}" does not exist in ${timezone}`);
3135
+ return instant;
3136
+ }
3137
+ __name(parseDateTime, "parseDateTime");
3138
+ function parseRRule(text, timezone = "UTC") {
3139
+ formatter(timezone);
3140
+ let start = toInstant({
3141
+ y: 1970,
3142
+ m: 1,
3143
+ d: 1,
3144
+ h: 0,
3145
+ mi: 0,
3146
+ s: 0
3147
+ }, timezone) ?? 0;
3148
+ let ruleText;
3149
+ for (const line of text.trim().split(/\r?\n/)) {
3150
+ const l = line.trim();
3151
+ if (!l) continue;
3152
+ if (/^DTSTART[:;]/i.test(l)) {
3153
+ const value = l.slice(l.lastIndexOf(":") + 1);
3154
+ start = parseDateTime(value, timezone, "DTSTART");
3155
+ } else {
3156
+ ruleText = l.replace(/^RRULE:/i, "");
3157
+ }
3158
+ }
3159
+ if (!ruleText) throw new Error('rrule: no rule (expected e.g. "FREQ=WEEKLY;BYDAY=MO")');
3160
+ const parts = /* @__PURE__ */ new Map();
3161
+ for (const pair of ruleText.split(";")) {
3162
+ if (!pair) continue;
3163
+ const [key, value] = pair.split("=");
3164
+ const name = key?.toUpperCase() ?? "";
3165
+ if (!value) throw new Error(`rrule: "${pair}" has no value`);
3166
+ if (!SUPPORTED.has(name)) throw new Error(`rrule: ${name} is not supported`);
3167
+ parts.set(name, value.toUpperCase());
3168
+ }
3169
+ const freq = parts.get("FREQ");
3170
+ if (!freq) throw new Error("rrule: FREQ is required");
3171
+ if (!FREQS.includes(freq)) throw new Error(`rrule: FREQ=${freq} is not supported`);
3172
+ if (parts.has("COUNT") && parts.has("UNTIL")) throw new Error("rrule: COUNT and UNTIL are exclusive");
3173
+ const interval = parts.has("INTERVAL") ? list(parts.get("INTERVAL"), "INTERVAL", 1, 1e4)[0] : 1;
3174
+ const byDay = parts.get("BYDAY")?.split(",").map((raw) => {
3175
+ const match = /^([+-]?\d{1,2})?(SU|MO|TU|WE|TH|FR|SA)$/.exec(raw);
3176
+ if (!match) throw new Error(`rrule: invalid BYDAY value "${raw}"`);
3177
+ const weekday = WEEKDAYS.indexOf(match[2]);
3178
+ if (match[1] === void 0) return {
3179
+ weekday
3180
+ };
3181
+ if (freq !== "MONTHLY" && freq !== "YEARLY") {
3182
+ throw new Error(`rrule: BYDAY with an ordinal ("${raw}") needs FREQ=MONTHLY or YEARLY`);
3183
+ }
3184
+ const nth = Number(match[1]);
3185
+ if (nth === 0 || Math.abs(nth) > 53) throw new Error(`rrule: invalid BYDAY value "${raw}"`);
3186
+ return {
3187
+ weekday,
3188
+ nth
3189
+ };
3190
+ });
3191
+ const wkstText = parts.get("WKST") ?? "MO";
3192
+ const wkst = WEEKDAYS.indexOf(wkstText);
3193
+ if (wkst < 0) throw new Error(`rrule: invalid WKST "${wkstText}"`);
3194
+ const rule = {
3195
+ freq,
3196
+ interval,
3197
+ wkst,
3198
+ start,
3199
+ timezone,
3200
+ ...parts.has("COUNT") ? {
3201
+ count: list(parts.get("COUNT"), "COUNT", 1, 1e6)[0]
3202
+ } : {},
3203
+ ...parts.has("UNTIL") ? {
3204
+ until: parseDateTime(parts.get("UNTIL"), timezone, "UNTIL")
3205
+ } : {},
3206
+ ...parts.has("BYMONTH") ? {
3207
+ byMonth: list(parts.get("BYMONTH"), "BYMONTH", 1, 12)
3208
+ } : {},
3209
+ ...parts.has("BYMONTHDAY") ? {
3210
+ byMonthDay: list(parts.get("BYMONTHDAY"), "BYMONTHDAY", 1, 31, true)
3211
+ } : {},
3212
+ ...byDay ? {
3213
+ byDay
3214
+ } : {},
3215
+ ...parts.has("BYHOUR") ? {
3216
+ byHour: list(parts.get("BYHOUR"), "BYHOUR", 0, 23)
3217
+ } : {},
3218
+ ...parts.has("BYMINUTE") ? {
3219
+ byMinute: list(parts.get("BYMINUTE"), "BYMINUTE", 0, 59)
3220
+ } : {},
3221
+ ...parts.has("BYSECOND") ? {
3222
+ bySecond: list(parts.get("BYSECOND"), "BYSECOND", 0, 59)
3223
+ } : {}
3224
+ };
3225
+ return rule;
3226
+ }
3227
+ __name(parseRRule, "parseRRule");
3228
+ function dayMatches(rule, anchor, day) {
3229
+ const { y, m, d, weekday } = fromDayNumber(day);
3230
+ const anchorDay = dayNumber(anchor.y, anchor.m, anchor.d);
3231
+ if (day < anchorDay) return false;
3232
+ if (rule.freq === "YEARLY" && (y - anchor.y) % rule.interval !== 0) return false;
3233
+ if (rule.freq === "MONTHLY" && (y * 12 + m - (anchor.y * 12 + anchor.m)) % rule.interval !== 0) return false;
3234
+ if (rule.freq === "WEEKLY") {
3235
+ const weekStart = /* @__PURE__ */ __name((n) => n - (fromDayNumber(n).weekday - rule.wkst + 7) % 7, "weekStart");
3236
+ if ((weekStart(day) - weekStart(anchorDay)) / 7 % rule.interval !== 0) return false;
3237
+ }
3238
+ if (rule.freq === "DAILY" && (day - anchorDay) % rule.interval !== 0) return false;
3239
+ const byMonth = rule.byMonth ?? (rule.freq === "YEARLY" && !rule.byMonthDay && !rule.byDay ? [
3240
+ anchor.m
3241
+ ] : void 0);
3242
+ if (byMonth && !byMonth.includes(m)) return false;
3243
+ const dim = daysInMonth(y, m);
3244
+ let byMonthDay = rule.byMonthDay;
3245
+ if (!byMonthDay && !rule.byDay && (rule.freq === "MONTHLY" || rule.freq === "YEARLY")) byMonthDay = [
3246
+ anchor.d
3247
+ ];
3248
+ if (byMonthDay && !byMonthDay.some((md) => (md > 0 ? md : dim + md + 1) === d)) return false;
3249
+ let byDay = rule.byDay;
3250
+ if (!byDay && rule.freq === "WEEKLY") byDay = [
3251
+ {
3252
+ weekday: fromDayNumber(anchorDay).weekday
3253
+ }
3254
+ ];
3255
+ if (byDay) {
3256
+ const hit = byDay.some((bd) => {
3257
+ if (bd.weekday !== weekday) return false;
3258
+ if (bd.nth === void 0) return true;
3259
+ const inMonth = rule.freq === "MONTHLY" || rule.byMonth !== void 0;
3260
+ const firstDay = inMonth ? dayNumber(y, m, 1) : dayNumber(y, 1, 1);
3261
+ const lastDay = inMonth ? dayNumber(y, m, dim) : dayNumber(y, 12, 31);
3262
+ return bd.nth > 0 ? Math.floor((day - firstDay) / 7) + 1 === bd.nth : Math.floor((lastDay - day) / 7) + 1 === -bd.nth;
3263
+ });
3264
+ if (!hit) return false;
3265
+ }
3266
+ return true;
3267
+ }
3268
+ __name(dayMatches, "dayMatches");
3269
+ function occurrencesOn(rule, anchor, day) {
3270
+ if (!dayMatches(rule, anchor, day)) return [];
3271
+ const { y, m, d } = fromDayNumber(day);
3272
+ const anchorDay = dayNumber(anchor.y, anchor.m, anchor.d);
3273
+ const range = /* @__PURE__ */ __name((n) => Array.from({
3274
+ length: n
3275
+ }, (_, i) => i), "range");
3276
+ let hours = rule.byHour ?? (rule.freq === "HOURLY" || rule.freq === "MINUTELY" ? range(24) : [
3277
+ anchor.h
3278
+ ]);
3279
+ if (rule.freq === "HOURLY") {
3280
+ hours = hours.filter((h) => ((day - anchorDay) * 24 + h - anchor.h) % rule.interval === 0);
3281
+ }
3282
+ const minutes = rule.byMinute ?? (rule.freq === "MINUTELY" ? range(60) : [
3283
+ anchor.mi
3284
+ ]);
3285
+ const seconds = rule.bySecond ?? [
3286
+ anchor.s
3287
+ ];
3288
+ const out = [];
3289
+ for (const h of hours) {
3290
+ for (const mi of minutes) {
3291
+ if (rule.freq === "MINUTELY") {
3292
+ const sinceAnchor = ((day - anchorDay) * 24 + h - anchor.h) * 60 + mi - anchor.mi;
3293
+ if (sinceAnchor % rule.interval !== 0) continue;
3294
+ }
3295
+ for (const s of seconds) {
3296
+ const instant = toInstant({
3297
+ y,
3298
+ m,
3299
+ d,
3300
+ h,
3301
+ mi,
3302
+ s
3303
+ }, rule.timezone);
3304
+ if (instant !== null && instant >= rule.start && (rule.until === void 0 || instant <= rule.until)) {
3305
+ out.push(instant);
3306
+ }
3307
+ }
3308
+ }
3309
+ }
3310
+ return [
3311
+ ...new Set(out)
3312
+ ].sort((a, b) => a - b);
3313
+ }
3314
+ __name(occurrencesOn, "occurrencesOn");
3315
+ function countedOccurrences(rule, anchor) {
3316
+ const out = [];
3317
+ const first = dayNumber(anchor.y, anchor.m, anchor.d);
3318
+ for (let day = first; day < first + HORIZON_DAYS && out.length < rule.count; day++) {
3319
+ for (const t of occurrencesOn(rule, anchor, day)) {
3320
+ if (out.length < rule.count) out.push(t);
3321
+ }
3322
+ }
3323
+ return out;
3324
+ }
3325
+ __name(countedOccurrences, "countedOccurrences");
3326
+ function nextRRuleFireMs(rule, afterMs) {
3327
+ const anchor = civilOf(rule.start, rule.timezone);
3328
+ if (rule.count !== void 0) return countedOccurrences(rule, anchor).find((t) => t > afterMs) ?? null;
3329
+ const from = Math.max(afterMs, rule.start - 1);
3330
+ const c = civilOf(from, rule.timezone);
3331
+ const firstDay = dayNumber(c.y, c.m, c.d);
3332
+ for (let day = firstDay; day < firstDay + HORIZON_DAYS; day++) {
3333
+ if (rule.until !== void 0 && day * DAY_MS > rule.until + DAY_MS) return null;
3334
+ const hit = occurrencesOn(rule, anchor, day).find((t) => t > afterMs);
3335
+ if (hit !== void 0) return hit;
3336
+ }
3337
+ return null;
3338
+ }
3339
+ __name(nextRRuleFireMs, "nextRRuleFireMs");
3340
+ function prevRRuleFireMs(rule, atMs) {
3341
+ const anchor = civilOf(rule.start, rule.timezone);
3342
+ if (rule.count !== void 0) {
3343
+ const before = countedOccurrences(rule, anchor).filter((t) => t <= atMs);
3344
+ return before.length > 0 ? before[before.length - 1] : null;
3345
+ }
3346
+ const c = civilOf(atMs, rule.timezone);
3347
+ const lastDay = dayNumber(c.y, c.m, c.d);
3348
+ const anchorDay = dayNumber(anchor.y, anchor.m, anchor.d);
3349
+ for (let day = lastDay; day >= anchorDay && day > lastDay - HORIZON_DAYS; day--) {
3350
+ const hits = occurrencesOn(rule, anchor, day).filter((t) => t <= atMs);
3351
+ if (hits.length > 0) return hits[hits.length - 1];
3352
+ }
3353
+ return null;
3354
+ }
3355
+ __name(prevRRuleFireMs, "prevRRuleFireMs");
3356
+
3001
3357
  // src/scheduler.ts
3002
3358
  import { createRequire } from "node:module";
3003
3359
 
@@ -3442,9 +3798,15 @@ var ScheduleClient = class {
3442
3798
  }
3443
3799
  };
3444
3800
  function toSpec(opts) {
3445
- if (opts.cron == null === (opts.every == null)) {
3446
- throw new Error(`schedule "${opts.id}" needs exactly one of "cron" or "every"`);
3447
- }
3801
+ const kinds = [
3802
+ opts.cron,
3803
+ opts.every,
3804
+ opts.rrule
3805
+ ].filter((k) => k != null).length;
3806
+ if (kinds !== 1) {
3807
+ throw new Error(`schedule "${opts.id}" needs exactly one of "cron", "every" or "rrule"`);
3808
+ }
3809
+ if (opts.rrule != null) parseRRule(opts.rrule, opts.timezone);
3448
3810
  const everyMs = opts.every != null ? parseDuration(opts.every) : void 0;
3449
3811
  if (everyMs !== void 0 && !(everyMs > 0)) {
3450
3812
  throw new Error(`schedule "${opts.id}": "every" must be a positive duration`);
@@ -3455,6 +3817,7 @@ function toSpec(opts) {
3455
3817
  input: opts.input,
3456
3818
  cron: opts.cron,
3457
3819
  everyMs,
3820
+ rrule: opts.rrule,
3458
3821
  timezone: opts.timezone,
3459
3822
  jitterMs: jitterMs && jitterMs > 0 ? jitterMs : void 0,
3460
3823
  overlap: opts.overlap ?? "allow",
@@ -3472,12 +3835,18 @@ function specOf(record) {
3472
3835
  __name(specOf, "specOf");
3473
3836
  function latestWindowAtOrBefore(spec, atMs) {
3474
3837
  if (spec.cron != null) return prevCronFireMs(spec.cron, atMs, spec.timezone);
3838
+ if (spec.rrule != null) {
3839
+ const prev = prevRRuleFireMs(parseRRule(spec.rrule, spec.timezone), atMs);
3840
+ if (prev === null) throw new Error(`rrule "${spec.rrule}" has no occurrence at or before ${new Date(atMs).toISOString()}`);
3841
+ return prev;
3842
+ }
3475
3843
  const every = spec.everyMs;
3476
3844
  return Math.floor(atMs / every) * every;
3477
3845
  }
3478
3846
  __name(latestWindowAtOrBefore, "latestWindowAtOrBefore");
3479
3847
  function nextWindowAfter(spec, afterMs) {
3480
3848
  if (spec.cron != null) return nextCronFireMs(spec.cron, afterMs, spec.timezone);
3849
+ if (spec.rrule != null) return nextRRuleFireMs(parseRRule(spec.rrule, spec.timezone), afterMs);
3481
3850
  const every = spec.everyMs;
3482
3851
  return (Math.floor(afterMs / every) + 1) * every;
3483
3852
  }
@@ -3517,6 +3886,7 @@ function describe(record) {
3517
3886
  namespace: record.namespace,
3518
3887
  cron: spec.cron,
3519
3888
  everyMs: spec.everyMs,
3889
+ rrule: spec.rrule,
3520
3890
  timezone: spec.timezone,
3521
3891
  jitterMs: spec.jitterMs,
3522
3892
  overlap: spec.overlap ?? "allow",
@@ -3829,6 +4199,240 @@ async function stalledInfo(store, run, now, thresholdMs, ageMs) {
3829
4199
  }
3830
4200
  __name(stalledInfo, "stalledInfo");
3831
4201
 
4202
+ // src/sticky-continuation.ts
4203
+ var StickyContinuation = class {
4204
+ static {
4205
+ __name(this, "StickyContinuation");
4206
+ }
4207
+ runId;
4208
+ fn;
4209
+ workflowVersion;
4210
+ compensations;
4211
+ replay;
4212
+ onPoisoned;
4213
+ /** The body's settled outcome, once it has one. */
4214
+ outcome;
4215
+ /** Primitives started by the body and not yet settled or parked. */
4216
+ active = 0;
4217
+ /** Bumped on every change, so a quiescence check can tell nothing happened in between. */
4218
+ version = 0;
4219
+ checkScheduled = false;
4220
+ waiter;
4221
+ parked = /* @__PURE__ */ new Map();
4222
+ /** A turn currently owns the body (holds the run's lease). */
4223
+ attached = true;
4224
+ /** The body called a primitive while detached, or one suspended the classic way: never reuse it. */
4225
+ unusable = false;
4226
+ /** The run row's `updatedAt` (epoch ms) this process wrote when the body last parked. */
4227
+ stamp;
4228
+ lastUsed = Date.now();
4229
+ /**
4230
+ * Parked steps' `completed` rows, as the instance that settled them read them back: the next turn
4231
+ * takes them instead of reading them again. A completed row never changes, so the hint can only be
4232
+ * missing, never stale.
4233
+ */
4234
+ hints = /* @__PURE__ */ new Map();
4235
+ constructor(runId, fn, workflowVersion, compensations, replay, onPoisoned) {
4236
+ this.runId = runId;
4237
+ this.fn = fn;
4238
+ this.workflowVersion = workflowVersion;
4239
+ this.compensations = compensations;
4240
+ this.replay = replay;
4241
+ this.onPoisoned = onPoisoned;
4242
+ }
4243
+ /** Remember a parked step's settled row for the next turn (see {@link hints}). */
4244
+ hint(cp) {
4245
+ if (cp.status === "completed" && this.parked.has(cp.seq)) this.hints.set(cp.seq, cp);
4246
+ }
4247
+ /** Start the body. `run` invokes it (inside the ambient workflow ctx). */
4248
+ begin(run) {
4249
+ let started;
4250
+ try {
4251
+ started = run();
4252
+ } catch (error2) {
4253
+ started = Promise.reject(error2);
4254
+ }
4255
+ started.then((value) => this.settle({
4256
+ kind: "returned",
4257
+ value
4258
+ }), (error2) => this.settle({
4259
+ kind: "threw",
4260
+ error: error2
4261
+ }));
4262
+ }
4263
+ settle(outcome) {
4264
+ this.outcome = outcome;
4265
+ this.changed();
4266
+ }
4267
+ /**
4268
+ * Called by every primitive the body starts. `false` refuses the call: the body is detached (no
4269
+ * turn holds the lease), and this continuation is poisoned.
4270
+ */
4271
+ enter() {
4272
+ if (!this.attached) {
4273
+ if (!this.unusable) {
4274
+ this.unusable = true;
4275
+ this.onPoisoned(this);
4276
+ }
4277
+ return false;
4278
+ }
4279
+ this.active += 1;
4280
+ this.changed();
4281
+ return true;
4282
+ }
4283
+ /** A primitive the body started settled. A classic suspension makes the body single-use. */
4284
+ exit(error2) {
4285
+ this.active -= 1;
4286
+ if (error2 instanceof WorkflowSuspended) this.unusable = true;
4287
+ this.changed();
4288
+ }
4289
+ /**
4290
+ * Park a dispatched call: it stops counting as active, and its promise settles when a later turn
4291
+ * re-evaluates it to an outcome.
4292
+ */
4293
+ park(seq, wakeAt, reevaluate) {
4294
+ return new Promise((resolve, reject) => {
4295
+ this.parked.set(seq, {
4296
+ seq,
4297
+ wakeAt,
4298
+ reevaluate,
4299
+ resolve,
4300
+ reject
4301
+ });
4302
+ this.active -= 1;
4303
+ this.changed();
4304
+ });
4305
+ }
4306
+ /**
4307
+ * Re-evaluate every parked call (in position order) with its freshly read checkpoint. A call that
4308
+ * has an outcome leaves the parked set and settles; one still waiting stays parked, with its new
4309
+ * deadline. Run by a warm turn after it attached.
4310
+ */
4311
+ async resumeParked(fresh) {
4312
+ const calls = [
4313
+ ...this.parked.values()
4314
+ ].sort((a, b) => a.seq - b.seq);
4315
+ for (const call of calls) {
4316
+ try {
4317
+ const value = await call.reevaluate(fresh.get(call.seq) ?? null);
4318
+ this.parked.delete(call.seq);
4319
+ this.active += 1;
4320
+ call.resolve(value);
4321
+ } catch (error2) {
4322
+ if (error2 instanceof WorkflowSuspended) {
4323
+ call.wakeAt = error2.wakeAt;
4324
+ continue;
4325
+ }
4326
+ this.parked.delete(call.seq);
4327
+ this.active += 1;
4328
+ call.reject(error2);
4329
+ }
4330
+ }
4331
+ this.changed();
4332
+ }
4333
+ /**
4334
+ * Wait for this turn's outcome: the body settled, or it is parked on at least one dispatched call
4335
+ * with nothing else in flight. Quiescence is confirmed a macrotask later with nothing changed in
4336
+ * between, so microtask continuations of the body (a `Promise.all` starting its next call) run
4337
+ * first. Parking detaches the body.
4338
+ */
4339
+ next() {
4340
+ return new Promise((resolve) => {
4341
+ this.waiter = resolve;
4342
+ this.changed();
4343
+ });
4344
+ }
4345
+ changed() {
4346
+ this.version += 1;
4347
+ const waiter = this.waiter;
4348
+ if (!waiter) return;
4349
+ if (this.outcome) {
4350
+ this.waiter = void 0;
4351
+ waiter(this.outcome);
4352
+ return;
4353
+ }
4354
+ if (this.checkScheduled || this.parked.size === 0 || this.active > 0) return;
4355
+ this.checkScheduled = true;
4356
+ const at = this.version;
4357
+ setImmediate(() => {
4358
+ this.checkScheduled = false;
4359
+ if (this.waiter !== waiter) return;
4360
+ if (this.version !== at) {
4361
+ this.changed();
4362
+ return;
4363
+ }
4364
+ if (this.outcome || this.parked.size === 0 || this.active > 0) return;
4365
+ this.waiter = void 0;
4366
+ this.attached = false;
4367
+ let wakeAt;
4368
+ for (const call of this.parked.values()) {
4369
+ if (call.wakeAt !== void 0) wakeAt = Math.min(wakeAt ?? call.wakeAt, call.wakeAt);
4370
+ }
4371
+ waiter({
4372
+ kind: "parked",
4373
+ wakeAt
4374
+ });
4375
+ });
4376
+ }
4377
+ };
4378
+ var StickyCache = class {
4379
+ static {
4380
+ __name(this, "StickyCache");
4381
+ }
4382
+ maxEntries;
4383
+ maxIdleMs;
4384
+ entries = /* @__PURE__ */ new Map();
4385
+ hits = 0;
4386
+ misses = 0;
4387
+ constructor(maxEntries, maxIdleMs) {
4388
+ this.maxEntries = maxEntries;
4389
+ this.maxIdleMs = maxIdleMs;
4390
+ }
4391
+ get enabled() {
4392
+ return this.maxEntries > 0;
4393
+ }
4394
+ get size() {
4395
+ return this.entries.size;
4396
+ }
4397
+ put(c) {
4398
+ if (!this.enabled || c.unusable) return;
4399
+ c.lastUsed = Date.now();
4400
+ this.entries.delete(c.runId);
4401
+ this.entries.set(c.runId, c);
4402
+ this.prune(c.lastUsed);
4403
+ }
4404
+ take(runId) {
4405
+ const c = this.entries.get(runId);
4406
+ if (!c) return void 0;
4407
+ this.entries.delete(runId);
4408
+ if (c.unusable || Date.now() - c.lastUsed > this.maxIdleMs) return void 0;
4409
+ return c;
4410
+ }
4411
+ /** The parked continuation of `runId`, left in place. */
4412
+ peek(runId) {
4413
+ return this.entries.get(runId);
4414
+ }
4415
+ has(runId) {
4416
+ return this.entries.has(runId);
4417
+ }
4418
+ evict(runId) {
4419
+ this.entries.delete(runId);
4420
+ }
4421
+ clear() {
4422
+ this.entries.clear();
4423
+ }
4424
+ prune(now) {
4425
+ while (this.entries.size > this.maxEntries) {
4426
+ const oldest = this.entries.keys().next().value;
4427
+ this.entries.delete(oldest);
4428
+ }
4429
+ for (const [runId, c] of this.entries) {
4430
+ if (now - c.lastUsed <= this.maxIdleMs) break;
4431
+ this.entries.delete(runId);
4432
+ }
4433
+ }
4434
+ };
4435
+
3832
4436
  // src/tenant-auth.ts
3833
4437
  import { createHmac, timingSafeEqual } from "node:crypto";
3834
4438
  var TOKEN_SEP = ".";
@@ -3903,9 +4507,29 @@ var TransportPool = class {
3903
4507
  __name(this, "TransportPool");
3904
4508
  }
3905
4509
  transports;
4510
+ /** Ids of the transports that route replies to {@link replyTo} (see {@link routeReplies}). */
4511
+ replying = /* @__PURE__ */ new Set();
4512
+ replyTo;
3906
4513
  constructor(transports) {
3907
4514
  this.transports = transports;
3908
4515
  }
4516
+ /**
4517
+ * Have every transport that can ({@link Transport.consumeRepliesFor}) send this engine instance
4518
+ * the results of the tasks it dispatches with `replyTo`. Returns whether any transport can.
4519
+ */
4520
+ routeReplies(instanceId) {
4521
+ this.replyTo = instanceId;
4522
+ for (const { id, transport } of this.transports) {
4523
+ if (!transport.consumeRepliesFor) continue;
4524
+ transport.consumeRepliesFor(instanceId);
4525
+ this.replying.add(id);
4526
+ }
4527
+ return this.replying.size > 0;
4528
+ }
4529
+ /** Stop stamping `replyTo` on tasks (a draining engine): their results go to the shared results. */
4530
+ stopReplies() {
4531
+ this.replyTo = void 0;
4532
+ }
3909
4533
  get size() {
3910
4534
  return this.transports.length;
3911
4535
  }
@@ -3946,8 +4570,13 @@ var TransportPool = class {
3946
4570
  let lastErr;
3947
4571
  for (const { id, transport } of this.ordered(preferId)) {
3948
4572
  try {
4573
+ const { replyTo, ...rest } = task;
4574
+ const reply = replyTo !== void 0 && this.replying.has(id) ? {
4575
+ replyTo
4576
+ } : {};
3949
4577
  await transport.dispatch({
3950
- ...task,
4578
+ ...rest,
4579
+ ...reply,
3951
4580
  transport: id
3952
4581
  });
3953
4582
  return;
@@ -4146,6 +4775,266 @@ var TransportPool = class {
4146
4775
  }
4147
4776
  };
4148
4777
 
4778
+ // src/wait-any.ts
4779
+ var ANY_WAIT_GROUP = "$any";
4780
+ var ANY_CHECKPOINT_PREFIX = "any:";
4781
+ function anyCheckpointName(name) {
4782
+ return `${ANY_CHECKPOINT_PREFIX}${name}`;
4783
+ }
4784
+ __name(anyCheckpointName, "anyCheckpointName");
4785
+ function isAnyCheckpoint(cp) {
4786
+ return cp.kind === "signal" && cp.name.startsWith(ANY_CHECKPOINT_PREFIX);
4787
+ }
4788
+ __name(isAnyCheckpoint, "isAnyCheckpoint");
4789
+ function isAnyMember(waiter) {
4790
+ return waiter.parallelGroup === ANY_WAIT_GROUP;
4791
+ }
4792
+ __name(isAnyMember, "isAnyMember");
4793
+ function anyConditionToken(c) {
4794
+ if (c.kind === "signal") return c.signal;
4795
+ if (c.kind === "child") return `child:${c.childId}`;
4796
+ return void 0;
4797
+ }
4798
+ __name(anyConditionToken, "anyConditionToken");
4799
+ function anyConditionsOf(cp) {
4800
+ return cp.input?.conditions ?? [];
4801
+ }
4802
+ __name(anyConditionsOf, "anyConditionsOf");
4803
+ function earliestDeadline(conditions) {
4804
+ let at;
4805
+ for (const c of conditions) if (c.kind === "timer" && (at === void 0 || c.at < at)) at = c.at;
4806
+ return at;
4807
+ }
4808
+ __name(earliestDeadline, "earliestDeadline");
4809
+ function dueTimer(conditions, now) {
4810
+ let due;
4811
+ for (const c of conditions) {
4812
+ if (c.kind !== "timer" || c.at > now) continue;
4813
+ if (!due || c.at < due.at) due = c;
4814
+ }
4815
+ return due;
4816
+ }
4817
+ __name(dueTimer, "dueTimer");
4818
+ function anyOutcomeOf(cp) {
4819
+ const { key, value, childId } = cp.output;
4820
+ if (childId !== void 0) return {
4821
+ key,
4822
+ value: unwrapCompletion(value, `child "${childId}"`)
4823
+ };
4824
+ return {
4825
+ key,
4826
+ value
4827
+ };
4828
+ }
4829
+ __name(anyOutcomeOf, "anyOutcomeOf");
4830
+ async function takeOldestBuffered(store, tokens) {
4831
+ if (tokens.length === 0) return null;
4832
+ if (store.takeOldestBufferedSignal) return store.takeOldestBufferedSignal(tokens);
4833
+ for (const token of tokens) {
4834
+ const taken = await store.takeBufferedSignal(token);
4835
+ if (taken) return {
4836
+ token,
4837
+ payload: taken.payload
4838
+ };
4839
+ }
4840
+ return null;
4841
+ }
4842
+ __name(takeOldestBuffered, "takeOldestBuffered");
4843
+ async function claimAnyCheckpoint(store, cp) {
4844
+ if (store.saveCheckpointIfPending) return store.saveCheckpointIfPending(cp);
4845
+ const current = await store.getCheckpoint(cp.runId, cp.seq);
4846
+ if (current?.status !== "pending") return false;
4847
+ await store.saveCheckpoint(cp);
4848
+ return true;
4849
+ }
4850
+ __name(claimAnyCheckpoint, "claimAnyCheckpoint");
4851
+ function resolvedAnyCheckpoint(cp, condition, value) {
4852
+ const output = condition.kind === "child" ? {
4853
+ key: condition.key,
4854
+ value,
4855
+ childId: condition.childId
4856
+ } : {
4857
+ key: condition.key,
4858
+ value
4859
+ };
4860
+ return {
4861
+ ...cp,
4862
+ status: "completed",
4863
+ output,
4864
+ wakeAt: void 0,
4865
+ finishedAt: /* @__PURE__ */ new Date()
4866
+ };
4867
+ }
4868
+ __name(resolvedAnyCheckpoint, "resolvedAnyCheckpoint");
4869
+ async function removeAnyWaiters(store, cp) {
4870
+ await Promise.all(anyConditionsOf(cp).flatMap((c) => {
4871
+ const token = anyConditionToken(c);
4872
+ return token === void 0 ? [] : [
4873
+ store.removeSignalWaiter({
4874
+ token,
4875
+ runId: cp.runId,
4876
+ seq: cp.seq,
4877
+ parallelGroup: ANY_WAIT_GROUP
4878
+ })
4879
+ ];
4880
+ }));
4881
+ }
4882
+ __name(removeAnyWaiters, "removeAnyWaiters");
4883
+ async function settle(store, cp, ready) {
4884
+ const done = resolvedAnyCheckpoint(cp, ready.condition, ready.value);
4885
+ if (await claimAnyCheckpoint(store, done)) {
4886
+ await removeAnyWaiters(store, done);
4887
+ return done;
4888
+ }
4889
+ if (ready.taken) await store.bufferSignal(ready.taken.token, ready.taken.payload);
4890
+ const winner = await store.getCheckpoint(cp.runId, cp.seq);
4891
+ return winner?.status === "completed" ? winner : void 0;
4892
+ }
4893
+ __name(settle, "settle");
4894
+ async function readyCondition(io, conditions) {
4895
+ const tokens = [];
4896
+ for (const c of conditions) {
4897
+ const token = anyConditionToken(c);
4898
+ if (token !== void 0 && !tokens.includes(token)) tokens.push(token);
4899
+ }
4900
+ const taken = await takeOldestBuffered(io.store, tokens);
4901
+ if (taken) {
4902
+ const condition = conditions.find((c) => anyConditionToken(c) === taken.token);
4903
+ if (condition) return {
4904
+ condition,
4905
+ value: taken.payload,
4906
+ taken
4907
+ };
4908
+ }
4909
+ for (const c of conditions) {
4910
+ if (c.kind !== "child") continue;
4911
+ const child = await io.store.getRun(c.childId);
4912
+ const completion = child ? childCompletion(child) : void 0;
4913
+ if (completion) return {
4914
+ condition: c,
4915
+ value: completion
4916
+ };
4917
+ }
4918
+ const due = dueTimer(conditions, io.clock());
4919
+ return due ? {
4920
+ condition: due,
4921
+ value: null
4922
+ } : void 0;
4923
+ }
4924
+ __name(readyCondition, "readyCondition");
4925
+ async function enterWaitAny(io, runId, seq, name, conditions) {
4926
+ const { store } = io;
4927
+ const cpName = anyCheckpointName(name);
4928
+ let cp = await store.getCheckpoint(runId, seq);
4929
+ if (cp && cp.name !== cpName) throw new NonDeterminismError(runId, seq, cpName, cp.name);
4930
+ if (cp?.status === "completed") return {
4931
+ state: "resolved",
4932
+ checkpoint: cp
4933
+ };
4934
+ if (!cp) {
4935
+ const now = io.clock();
4936
+ const stored2 = conditions.map((c) => c.kind === "timer" ? {
4937
+ key: c.key,
4938
+ kind: "timer",
4939
+ at: c.until ?? now + (c.ms ?? 0)
4940
+ } : c.kind === "signal" ? {
4941
+ key: c.key,
4942
+ kind: "signal",
4943
+ signal: c.signal
4944
+ } : {
4945
+ key: c.key,
4946
+ kind: "child",
4947
+ childId: c.childId
4948
+ });
4949
+ const at = new Date(now);
4950
+ cp = stepCheckpoint({
4951
+ runId,
4952
+ seq,
4953
+ name: cpName,
4954
+ kind: "signal",
4955
+ status: "pending",
4956
+ attempts: 1,
4957
+ input: {
4958
+ conditions: stored2
4959
+ },
4960
+ wakeAt: earliestDeadline(stored2),
4961
+ enqueuedAt: at,
4962
+ startedAt: at,
4963
+ finishedAt: at
4964
+ });
4965
+ await store.saveCheckpoint(cp);
4966
+ }
4967
+ const stored = anyConditionsOf(cp);
4968
+ const ready = await readyCondition(io, stored);
4969
+ if (ready) {
4970
+ const winner = await settle(store, cp, ready);
4971
+ if (winner) return {
4972
+ state: "resolved",
4973
+ checkpoint: winner
4974
+ };
4975
+ }
4976
+ for (const c of stored) {
4977
+ const token = anyConditionToken(c);
4978
+ if (token === void 0) continue;
4979
+ await store.putSignalWaiter({
4980
+ token,
4981
+ runId,
4982
+ seq,
4983
+ parallelGroup: ANY_WAIT_GROUP
4984
+ });
4985
+ }
4986
+ const late = await readyCondition(io, stored);
4987
+ if (late) {
4988
+ const winner = await settle(store, cp, late);
4989
+ if (winner) return {
4990
+ state: "resolved",
4991
+ checkpoint: winner
4992
+ };
4993
+ }
4994
+ return {
4995
+ state: "waiting",
4996
+ wakeAt: earliestDeadline(stored)
4997
+ };
4998
+ }
4999
+ __name(enterWaitAny, "enterWaitAny");
5000
+ function anyHistoryEvent(cp) {
5001
+ const { key, value, childId } = cp.output;
5002
+ const name = cp.name.slice(ANY_CHECKPOINT_PREFIX.length);
5003
+ if (childId !== void 0) {
5004
+ const completion = value;
5005
+ if (completion && completion.ok === false) {
5006
+ return {
5007
+ seq: cp.seq,
5008
+ kind: "any",
5009
+ name,
5010
+ output: {
5011
+ key
5012
+ },
5013
+ error: completionError(completion)
5014
+ };
5015
+ }
5016
+ return {
5017
+ seq: cp.seq,
5018
+ kind: "any",
5019
+ name,
5020
+ output: {
5021
+ key,
5022
+ value: completion?.ok ? completion.value : void 0
5023
+ }
5024
+ };
5025
+ }
5026
+ return {
5027
+ seq: cp.seq,
5028
+ kind: "any",
5029
+ name,
5030
+ output: {
5031
+ key,
5032
+ value
5033
+ }
5034
+ };
5035
+ }
5036
+ __name(anyHistoryEvent, "anyHistoryEvent");
5037
+
4149
5038
  // src/step-name-symbol.ts
4150
5039
  var DURABLE_STEP_NAME = Symbol.for("durable.step.name");
4151
5040
  var DURABLE_STEP_CONFIG = Symbol.for("durable.step.config");
@@ -4565,9 +5454,10 @@ function createWorkflowCtx(host, runId, compensations, workflow = "", searchAttr
4565
5454
  return unwrapCompletion(await waitForSignal(`task:${runId}:${name}`), `task "${name}"`);
4566
5455
  }, "task");
4567
5456
  const child = /* @__PURE__ */ __name(async (workflow2, input, options) => {
4568
- const { childId, priority, version } = normalizeChildOptions(options);
5457
+ const { childId, priority, version, parentClose } = normalizeChildOptions(options);
4569
5458
  const current = pos.next();
4570
5459
  const id = childId ?? `${runId}.child.${current}`;
5460
+ if (parentClose) host.noteParentClose?.(runId, id, parentClose);
4571
5461
  const existing = await readCheckpoint(current);
4572
5462
  if (existing && existing.status === "completed") {
4573
5463
  return unwrapCompletion(existing.output, `child "${id}"`);
@@ -4604,7 +5494,7 @@ function createWorkflowCtx(host, runId, compensations, workflow = "", searchAttr
4604
5494
  }
4605
5495
  const childRun = await store.getRun(id);
4606
5496
  if (!childRun) {
4607
- host.startChild(workflowName(workflow2), input, id, priority, version);
5497
+ host.startChild(workflowName(workflow2), input, id, priority, version, parentClose);
4608
5498
  } else {
4609
5499
  const done = (await settleFinishedChildren([
4610
5500
  {
@@ -4626,12 +5516,12 @@ function createWorkflowCtx(host, runId, compensations, workflow = "", searchAttr
4626
5516
  }
4627
5517
  ]));
4628
5518
  }, "child");
4629
- const fan = /* @__PURE__ */ __name(async (workflow2, inputs, opts, settle) => {
5519
+ const fan = /* @__PURE__ */ __name(async (workflow2, inputs, opts, settle2) => {
4630
5520
  if (inputs.length === 0) return [];
4631
5521
  if (stepNameOf(workflow2) !== void 0) {
4632
- return allSteps(workflow2, inputs, opts, settle);
5522
+ return allSteps(workflow2, inputs, opts, settle2);
4633
5523
  }
4634
- const mode = settle ? "waitAll" : opts?.mode ?? "waitAll";
5524
+ const mode = settle2 ? "waitAll" : opts?.mode ?? "waitAll";
4635
5525
  const name = workflowName(workflow2);
4636
5526
  const waves = allWaves(inputs.length, opts?.concurrency);
4637
5527
  const positions = inputs.map(() => pos.next());
@@ -4803,14 +5693,14 @@ function createWorkflowCtx(host, runId, compensations, workflow = "", searchAttr
4803
5693
  outputs.push(value);
4804
5694
  }
4805
5695
  }
4806
- if (settle) return results;
5696
+ if (settle2) return results;
4807
5697
  if (failures.length > 0) throw new GatherError(failures, inputs.length);
4808
5698
  return outputs;
4809
5699
  }
4810
5700
  }, "fan");
4811
5701
  const all = /* @__PURE__ */ __name((workflow2, inputs, opts) => fan(workflow2, inputs, opts, false), "all");
4812
5702
  const allSettled = /* @__PURE__ */ __name((workflow2, inputs, opts) => fan(workflow2, inputs, opts, true), "allSettled");
4813
- const allSteps = /* @__PURE__ */ __name(async (ref, inputs, opts, settle) => {
5703
+ const allSteps = /* @__PURE__ */ __name(async (ref, inputs, opts, settle2) => {
4814
5704
  if (opts?.queue !== void 0) {
4815
5705
  throw new Error("ctx.all: a flow-control queue is not supported on a step fan");
4816
5706
  }
@@ -4821,8 +5711,8 @@ function createWorkflowCtx(host, runId, compensations, workflow = "", searchAttr
4821
5711
  };
4822
5712
  const positions = inputs.map(() => pos.next());
4823
5713
  return host.callRemoteAll(runId, positions, def, inputs, {
4824
- mode: settle ? "waitAll" : opts?.mode ?? "waitAll",
4825
- settle,
5714
+ mode: settle2 ? "waitAll" : opts?.mode ?? "waitAll",
5715
+ settle: settle2,
4826
5716
  concurrency: opts?.concurrency,
4827
5717
  transport: opts?.transport,
4828
5718
  admission: {
@@ -4926,12 +5816,13 @@ function createWorkflowCtx(host, runId, compensations, workflow = "", searchAttr
4926
5816
  return settled;
4927
5817
  }, "settleFinishedChildren");
4928
5818
  const startChild = /* @__PURE__ */ __name(async (workflow2, input, options) => {
4929
- const { childId, priority, version } = normalizeChildOptions(options);
5819
+ const { childId, priority, version, parentClose } = normalizeChildOptions(options);
4930
5820
  const current = pos.next();
4931
5821
  const id = childId ?? `${runId}.child.${current}`;
5822
+ if (parentClose) host.noteParentClose?.(runId, id, parentClose);
4932
5823
  const existing = await readCheckpoint(current);
4933
5824
  if (existing && existing.status === "completed") return existing.output;
4934
- if (!await store.getRun(id)) host.startChild(workflowName(workflow2), input, id, priority, version);
5825
+ if (!await store.getRun(id)) host.startChild(workflowName(workflow2), input, id, priority, version, parentClose);
4935
5826
  await writeCheckpoint(instantCheckpoint({
4936
5827
  runId,
4937
5828
  seq: current,
@@ -5193,17 +6084,74 @@ function createWorkflowCtx(host, runId, compensations, workflow = "", searchAttr
5193
6084
  });
5194
6085
  }
5195
6086
  __name(step, "step");
6087
+ const waitForAny = /* @__PURE__ */ __name(async (name, conditions) => {
6088
+ const current = pos.next();
6089
+ const cached = replay?.get(current);
6090
+ if (cached?.status === "completed" && cached.name === anyCheckpointName(name)) {
6091
+ return anyOutcomeOf(cached);
6092
+ }
6093
+ const wire = Object.entries(conditions).map(([key, c]) => ({
6094
+ ...c,
6095
+ key
6096
+ }));
6097
+ const state = await enterWaitAny({
6098
+ store,
6099
+ clock: host.clock
6100
+ }, runId, current, name, wire);
6101
+ if (state.state === "resolved") {
6102
+ replay?.set(current, state.checkpoint);
6103
+ return anyOutcomeOf(state.checkpoint);
6104
+ }
6105
+ throw new WorkflowSuspended(state.wakeAt);
6106
+ }, "waitForAny");
6107
+ const signalWait = /* @__PURE__ */ __name((token) => ({
6108
+ kind: "signal",
6109
+ signal: token
6110
+ }), "signalWait");
6111
+ const timer = /* @__PURE__ */ __name((duration) => typeof duration === "object" ? {
6112
+ kind: "timer",
6113
+ until: typeof duration.until === "number" ? duration.until : duration.until.getTime()
6114
+ } : {
6115
+ kind: "timer",
6116
+ ms: parseDuration(duration)
6117
+ }, "timer");
6118
+ const childWait = /* @__PURE__ */ __name((first, second) => ({
6119
+ kind: "child",
6120
+ childId: typeof first === "string" && second === void 0 ? first : second
6121
+ }), "childWait");
5196
6122
  const nestedStep = /* @__PURE__ */ __name(() => {
5197
6123
  const scope = currentStepScope();
5198
6124
  return scope !== void 0 && scope.owner === scopeOwner ? scope.step : void 0;
5199
6125
  }, "nestedStep");
5200
- const guard = /* @__PURE__ */ __name((primitive, fn) => (...args) => {
6126
+ const unguarded = /* @__PURE__ */ __name((primitive, fn) => (...args) => {
5201
6127
  const stepName = nestedStep();
5202
6128
  if (stepName === void 0) return fn(...args);
5203
6129
  return (async () => {
5204
6130
  if (await readCheckpoint(pos.peek())) return fn(...args);
5205
6131
  throw new NestedWorkflowCallError(runId, primitive, stepName);
5206
6132
  })();
6133
+ }, "unguarded");
6134
+ const sticky = host.sticky;
6135
+ const guard = /* @__PURE__ */ __name((primitive, fn) => {
6136
+ const inner = unguarded(primitive, fn);
6137
+ if (!sticky) return inner;
6138
+ return (...args) => {
6139
+ if (!sticky.enter()) return Promise.reject(new WorkflowSuspended());
6140
+ let started;
6141
+ try {
6142
+ started = inner(...args);
6143
+ } catch (err) {
6144
+ sticky.exit(err);
6145
+ throw err;
6146
+ }
6147
+ return started.then((value) => {
6148
+ sticky.exit();
6149
+ return value;
6150
+ }, (err) => {
6151
+ sticky.exit(err);
6152
+ throw err;
6153
+ });
6154
+ };
5207
6155
  }, "guard");
5208
6156
  const guardedWebhook = /* @__PURE__ */ __name(() => {
5209
6157
  const stepName = nestedStep();
@@ -5224,6 +6172,10 @@ function createWorkflowCtx(host, runId, compensations, workflow = "", searchAttr
5224
6172
  sleepUntil: guard("sleepUntil", sleepUntil),
5225
6173
  continueAsNew,
5226
6174
  waitForSignal: guard("waitForSignal", waitForSignal),
6175
+ waitForAny: guard("waitForAny", waitForAny),
6176
+ signalWait,
6177
+ timer,
6178
+ childWait,
5227
6179
  waitForEvent: guard("waitForEvent", waitForEvent),
5228
6180
  waitForRun: guard("waitForRun", waitForRun),
5229
6181
  task: guard("task", task),
@@ -5254,6 +6206,9 @@ function mergeTags(staticTags, runTags) {
5254
6206
  }
5255
6207
  __name(mergeTags, "mergeTags");
5256
6208
  var BREAKPOINT = "breakpoint";
6209
+ var PENDING_POLL_BATCH = 100;
6210
+ var DEFAULT_STICKY_RUNS = 1e3;
6211
+ var DEFAULT_STICKY_IDLE_MS = 3e5;
5257
6212
  var isBreakpoint = /* @__PURE__ */ __name((cp) => cp.status === "pending" && cp.name.startsWith(BREAKPOINT), "isBreakpoint");
5258
6213
  var ANNOUNCED_ROUTES_TTL_MS = 5e3;
5259
6214
  function servedNamespace(namespace, group) {
@@ -5319,6 +6274,8 @@ var freshPendingKey = /* @__PURE__ */ __name(() => `${Date.now()}-${globalThis.c
5319
6274
  var wakeRequestToken = /* @__PURE__ */ __name((runId) => `durable:wake-after-turn:${runId}`, "wakeRequestToken");
5320
6275
  var WAKE_REQUEST_SCAN = 100;
5321
6276
  var CHILD_SIGNAL_PREFIX = "signal:child:";
6277
+ var DEFAULT_SIGNAL_ID_TTL_MS = 7 * 864e5;
6278
+ var SIGNAL_ID_SWEEP_BATCH = 500;
5322
6279
  var BUFFERED_SWEEP_PAGE = 500;
5323
6280
  var BUFFERED_SWEEP_PAGES_PER_CALL = 4;
5324
6281
  var BUFFERED_SWEEP_BUDGET_MS = 250;
@@ -5449,12 +6406,29 @@ var WorkflowEngine = class _WorkflowEngine {
5449
6406
  unregisteredWorkflowGraceMs;
5450
6407
  remoteAdvanceSilenceMs;
5451
6408
  reconcileMs;
6409
+ /** Parked workflow bodies kept between turns (see {@link WorkflowEngineDeps.stickyRuns}). */
6410
+ sticky;
6411
+ /** Pending runs claimed by {@link runPending} for the turn about to run them, as read by the claim. */
6412
+ claimedRuns = /* @__PURE__ */ new Map();
6413
+ /** The continuation each turn executing here runs in, by run id, while the turn runs. */
6414
+ turnContinuations = /* @__PURE__ */ new Map();
6415
+ /** Whether to ask transports to route step results back here (see {@link WorkflowEngineDeps.routeReplies}). */
6416
+ routeReplies;
6417
+ /** Whether a transport routes this instance's step results back to it. */
6418
+ repliesRouted = false;
6419
+ /** The run row stamp (`updatedAt`, epoch ms) a turn here wrote when it settled `suspended`. */
6420
+ suspendStamps = /* @__PURE__ */ new Map();
5452
6421
  timerBatchSize;
5453
6422
  eventBufferTtlMs;
6423
+ signalIdTtlMs;
6424
+ /** The deduplicated {@link signal} sends in flight here, by `token \u0000 id`. */
6425
+ signalIdSends = /* @__PURE__ */ new Map();
5454
6426
  remoteRedispatchMs;
5455
6427
  remoteRedispatchMax;
5456
6428
  /** How long a `blocked` run waits before the recovery poll re-checks the live fleet (design §7.5). */
5457
6429
  blockedPollMs;
6430
+ workerDescriptorCacheMs;
6431
+ descriptorCache = /* @__PURE__ */ new Map();
5458
6432
  /** This engine's control-plane handshake descriptor — negotiated against each worker (design §7.3). */
5459
6433
  cpDescriptor;
5460
6434
  webhookUrl;
@@ -5479,6 +6453,10 @@ var WorkflowEngine = class _WorkflowEngine {
5479
6453
  workflows = /* @__PURE__ */ new Map();
5480
6454
  /** The newest registered version per workflow name — used to `start` new runs. */
5481
6455
  latest = /* @__PURE__ */ new Map();
6456
+ /** Workflows this instance starts but never runs, keyed `name@version` (like {@link workflows}), so every version stays startable. See {@link registerStartOnly}. */
6457
+ startOnly = /* @__PURE__ */ new Map();
6458
+ /** The latest start-only registration per name, for unpinned starts (like {@link latest}). */
6459
+ startOnlyLatest = /* @__PURE__ */ new Map();
5482
6460
  /** Event name → subscribing workflow name → that workflow's subscriptions to it. See `onEvent`. */
5483
6461
  eventTriggers = /* @__PURE__ */ new Map();
5484
6462
  /** Durable-entity subsystem (registers the `__entity` runner; see `registerEntity`). */
@@ -5609,6 +6587,8 @@ var WorkflowEngine = class _WorkflowEngine {
5609
6587
  claimsMade = /* @__PURE__ */ new Map();
5610
6588
  /** Pager hooks for stranded runs (see {@link onStalled}). */
5611
6589
  stalledListeners = /* @__PURE__ */ new Set();
6590
+ /** `parentClose` policies recorded by the turn in flight, per parent run (see {@link settleParentClose}). */
6591
+ parentCloseIntents = /* @__PURE__ */ new Map();
5612
6592
  /** Runs already paged about, by id → the `updatedAt` of the paged episode (see {@link sweepStalled}). */
5613
6593
  notifiedStalled = /* @__PURE__ */ new Map();
5614
6594
  lastStalledSweepAt = Number.NEGATIVE_INFINITY;
@@ -5643,14 +6623,18 @@ var WorkflowEngine = class _WorkflowEngine {
5643
6623
  this.unregisteredWorkflowGraceMs = grace !== void 0 && Number.isFinite(grace) && grace >= 0 ? grace : 24 * 60 * 60 * 1e3;
5644
6624
  this.remoteAdvanceSilenceMs = deps.remoteAdvanceSilenceMs;
5645
6625
  this.reconcileMs = deps.reconcileMs === void 0 ? 3e5 : deps.reconcileMs || void 0;
6626
+ this.sticky = new StickyCache(Math.max(0, deps.stickyRuns ?? DEFAULT_STICKY_RUNS), deps.stickyIdleMs ?? DEFAULT_STICKY_IDLE_MS);
6627
+ this.routeReplies = deps.routeReplies ?? true;
5646
6628
  const stalledAfter = deps.stalledAfterMs;
5647
6629
  this.stalledAfterMs = stalledAfter !== void 0 && Number.isFinite(stalledAfter) ? Math.max(6e4, stalledAfter) : DEFAULT_STALLED_AFTER_MS;
5648
6630
  const batch = deps.timerBatchSize;
5649
6631
  this.timerBatchSize = batch !== void 0 && Number.isFinite(batch) && batch >= 1 ? Math.floor(batch) : 500;
5650
6632
  this.eventBufferTtlMs = deps.eventBufferTtlMs || void 0;
6633
+ this.signalIdTtlMs = deps.signalIdTtl != null ? parseDuration(deps.signalIdTtl) : DEFAULT_SIGNAL_ID_TTL_MS;
5651
6634
  this.remoteRedispatchMs = deps.remoteRedispatchMs || void 0;
5652
6635
  this.remoteRedispatchMax = Math.max(1, deps.remoteRedispatchMax ?? 10);
5653
6636
  this.blockedPollMs = deps.blockedPollMs ?? 5e3;
6637
+ this.workerDescriptorCacheMs = deps.workerDescriptorCacheMs ?? 2e3;
5654
6638
  this.cpDescriptor = controlPlaneDescriptor({
5655
6639
  instanceId: `cp-${this.instanceId}`,
5656
6640
  ...this.namespace !== void 0 ? {
@@ -5748,6 +6732,9 @@ var WorkflowEngine = class _WorkflowEngine {
5748
6732
  await this.completeRemoteDecision(decision);
5749
6733
  }
5750
6734
  );
6735
+ if (this.routeReplies && this.sticky.enabled) {
6736
+ this.repliesRouted = this.pool.routeReplies(this.instanceId);
6737
+ }
5751
6738
  const verifyTenant = deps.verifyTenant;
5752
6739
  this.pool.onStartRun(async (message) => {
5753
6740
  let tenant;
@@ -5990,7 +6977,7 @@ var WorkflowEngine = class _WorkflowEngine {
5990
6977
  if (!this.isRegisteredHere(run) && !await this.canResolveWorkflow(run)) {
5991
6978
  const result = await this.settleUnregistered(run, nowMs);
5992
6979
  if (result) settled?.set(run.id, result);
5993
- else if (result === void 0) this.logUnregisteredSkip(run);
6980
+ else if (result === void 0 && !this.isStartOnlyHere(run)) this.logUnregisteredSkip(run);
5994
6981
  continue;
5995
6982
  }
5996
6983
  try {
@@ -6136,6 +7123,34 @@ var WorkflowEngine = class _WorkflowEngine {
6136
7123
  }
6137
7124
  }
6138
7125
  /**
7126
+ * Let {@link start} create runs of `name@version` on this instance without registering a body here:
7127
+ * the run is stored and left for an instance that {@link register}ed the workflow, and this one
7128
+ * never executes it. For a process that only emits work (an API pod) while workers run it.
7129
+ *
7130
+ * Runs this instance picks up anyway (a driving poll, the run queue) are released for another
7131
+ * instance without a log line, and dead-lettered after `unregisteredWorkflowGraceMs` like any run
7132
+ * whose workflow nobody registered. A {@link register} of the same name on this instance wins.
7133
+ */
7134
+ registerStartOnly(name, version, opts) {
7135
+ const entry = {
7136
+ name,
7137
+ version,
7138
+ fn: /* @__PURE__ */ __name(() => {
7139
+ throw new Error(`workflow ${name} is start-only on this instance \u2014 it has no body here`);
7140
+ }, "fn"),
7141
+ tags: opts?.tags,
7142
+ validateInput: opts?.validateInput,
7143
+ origin: opts?.origin
7144
+ };
7145
+ this.startOnly.set(versionKey(name, version), entry);
7146
+ const current = this.startOnlyLatest.get(name);
7147
+ if (!current || isNewerVersion(version, current.version)) this.startOnlyLatest.set(name, entry);
7148
+ }
7149
+ /** Whether `run` is of a workflow this instance only starts (see {@link registerStartOnly}). */
7150
+ isStartOnlyHere(run) {
7151
+ return !this.isRegisteredHere(run) && this.startOnly.has(versionKey(run.workflow, run.workflowVersion));
7152
+ }
7153
+ /**
6139
7154
  * The retained in-process body for `name@version`, for an IN-APP worker that consumes this engine's
6140
7155
  * own group to SERVE a group-served `register(..., { group, executor })` workflow (uniform dispatch):
6141
7156
  * the worker fetches the body by name when it picks up the dispatched {@link WorkflowTask}, replays
@@ -6324,6 +7339,23 @@ var WorkflowEngine = class _WorkflowEngine {
6324
7339
  return deleted;
6325
7340
  }
6326
7341
  /**
7342
+ * Delete the signal-id records (`signal(..., { id })`) that expired by `now`, in batches of
7343
+ * {@link SIGNAL_ID_SWEEP_BATCH}, at most `maxBatches` per call. Expired records already stop
7344
+ * deduplicating; this reclaims their rows. The NestJS timer poller calls it every tick. Returns how
7345
+ * many it deleted; 0 on a store without `deleteExpiredSignalIds`.
7346
+ */
7347
+ async sweepExpiredSignalIds(now = this.clock(), maxBatches = 4) {
7348
+ const { deleteExpiredSignalIds } = this.store;
7349
+ if (!deleteExpiredSignalIds || this.draining) return 0;
7350
+ let deleted = 0;
7351
+ for (let batch = 0; batch < maxBatches; batch++) {
7352
+ const n = await deleteExpiredSignalIds.call(this.store, now, SIGNAL_ID_SWEEP_BATCH);
7353
+ deleted += n;
7354
+ if (n < SIGNAL_ID_SWEEP_BATCH) break;
7355
+ }
7356
+ return deleted;
7357
+ }
7358
+ /**
6327
7359
  * Register a pager hook for STRANDED runs: an in-flight run untouched past `stalledAfterMs` that is
6328
7360
  * either suspended with NO wake timer (wake-forever: nothing will ever re-drive it) or parked on an
6329
7361
  * old pending remote step with no live lease (dispatched and, most likely, lost). The timer poller
@@ -6401,6 +7433,7 @@ var WorkflowEngine = class _WorkflowEngine {
6401
7433
  ...event,
6402
7434
  at: /* @__PURE__ */ new Date()
6403
7435
  };
7436
+ this.settleParentClose(full);
6404
7437
  this.deliver(full);
6405
7438
  if (opts?.lifecycle !== false) {
6406
7439
  this.publishLifecycle(full);
@@ -7657,7 +8690,8 @@ var WorkflowEngine = class _WorkflowEngine {
7657
8690
  async start(workflow, input, runId, opts) {
7658
8691
  const name = workflowName(workflow);
7659
8692
  const pinned = opts?.version;
7660
- let registered = pinned === void 0 ? this.latest.get(name) : this.workflows.get(versionKey(name, pinned));
8693
+ const startOnly = pinned === void 0 ? this.startOnlyLatest.get(name) : this.startOnly.get(versionKey(name, pinned));
8694
+ let registered = pinned === void 0 ? this.latest.get(name) ?? startOnly : this.workflows.get(versionKey(name, pinned)) ?? startOnly;
7661
8695
  if (!registered && pinned === void 0) {
7662
8696
  const ancestor = await this.findRemoteAncestor(runId);
7663
8697
  if (ancestor) registered = this.synthesizeRemoteChild(name, ancestor.version, ancestor);
@@ -7760,7 +8794,10 @@ var WorkflowEngine = class _WorkflowEngine {
7760
8794
  status: "suspended"
7761
8795
  };
7762
8796
  }
7763
- await this.dispatchRun(runId, run.priority, pendingStateKey(run.updatedAt));
8797
+ const runsLocally = this.customDispatcher === void 0 && !this.dispatchesOverTransport;
8798
+ if (!(runsLocally && registered === startOnly)) {
8799
+ await this.dispatchRun(runId, run.priority, pendingStateKey(run.updatedAt));
8800
+ }
7764
8801
  if (this.controlPlane && !this.queuesRunsOverTransport) {
7765
8802
  void this.controlPlane.publishControl({
7766
8803
  kind: "enqueued",
@@ -7950,8 +8987,31 @@ var WorkflowEngine = class _WorkflowEngine {
7950
8987
  return true;
7951
8988
  }
7952
8989
  async resumeRunNow(runId, allowFailed) {
7953
- const run = await this.store.getRun(runId);
8990
+ const claimed = this.claimedRuns.get(runId);
8991
+ if (claimed) {
8992
+ this.claimedRuns.delete(runId);
8993
+ return this.resumeLoadedRun(claimed, allowFailed);
8994
+ }
8995
+ const lockAndGet = this.executionLeases.has(runId) ? void 0 : this.store.tryLockRunAndGet;
8996
+ let locked = false;
8997
+ let run = null;
8998
+ if (lockAndGet) {
8999
+ const nowMs = this.clock();
9000
+ run = await lockAndGet.call(this.store, runId, this.instanceId, nowMs + this.leaseMs, nowMs);
9001
+ locked = run !== null;
9002
+ }
9003
+ run ??= await this.store.getRun(runId);
7954
9004
  if (!run) throw new Error(`run ${runId} not found`);
9005
+ if (!locked) return this.resumeLoadedRun(run, allowFailed);
9006
+ this.lockedForTurn.add(runId);
9007
+ try {
9008
+ return await this.resumeLoadedRun(run, allowFailed);
9009
+ } finally {
9010
+ if (this.lockedForTurn.delete(runId)) await this.releaseLeaseBestEffort(runId);
9011
+ }
9012
+ }
9013
+ async resumeLoadedRun(run, allowFailed) {
9014
+ const runId = run.id;
7955
9015
  if (run.status === "cancelled" || run.status === "completed" || run.status === "dead" || run.status === "failed" && !allowFailed) {
7956
9016
  return {
7957
9017
  runId,
@@ -8133,6 +9193,8 @@ var WorkflowEngine = class _WorkflowEngine {
8133
9193
  */
8134
9194
  beginDrain(spentMs = 0) {
8135
9195
  this.draining = true;
9196
+ this.sticky.clear();
9197
+ this.pool.stopReplies();
8136
9198
  if (this.runConsumerStops.length > 0) {
8137
9199
  this.runBatcher.flush();
8138
9200
  void this.stopRunConsumers();
@@ -8392,7 +9454,7 @@ var WorkflowEngine = class _WorkflowEngine {
8392
9454
  result: settled2,
8393
9455
  dispatch: false
8394
9456
  };
8395
- if (settled2 === void 0) this.logUnregisteredSkip(run);
9457
+ if (settled2 === void 0 && !this.isStartOnlyHere(run)) this.logUnregisteredSkip(run);
8396
9458
  return void 0;
8397
9459
  }
8398
9460
  const settled = await this.countRecovery(run);
@@ -8673,6 +9735,7 @@ var WorkflowEngine = class _WorkflowEngine {
8673
9735
  const run = await this.store.getRun(runId);
8674
9736
  const settled = run ? await this.settleUnregistered(run, nowMs) : null;
8675
9737
  if (settled !== void 0) return settled;
9738
+ if (run && this.isStartOnlyHere(run)) return null;
8676
9739
  }
8677
9740
  throw err;
8678
9741
  });
@@ -8848,7 +9911,13 @@ var WorkflowEngine = class _WorkflowEngine {
8848
9911
  * {@link resumeDueTimers}.
8849
9912
  */
8850
9913
  async runPending(nowMs = this.clock()) {
8851
- const pending = await this.store.listPendingRuns(100, this.namespace);
9914
+ if (this.store.claimPendingRuns && !this.queuesRunsOverTransport && !this.draining) {
9915
+ const room = Math.min(PENDING_POLL_BATCH, LOCAL_RUN_CONCURRENCY - this.inflight.size);
9916
+ if (room <= 0) return [];
9917
+ const claimed = await this.store.claimPendingRuns(room, this.instanceId, nowMs + this.leaseMs, nowMs, this.namespace);
9918
+ return this.runClaimed(claimed);
9919
+ }
9920
+ const pending = await this.store.listPendingRuns(PENDING_POLL_BATCH, this.namespace);
8852
9921
  if (this.queuesRunsOverTransport && !this.draining && pending.length > 0) {
8853
9922
  if (await this.queueOnTransport(pending)) {
8854
9923
  return pending.map((run) => ({
@@ -8938,6 +10007,35 @@ var WorkflowEngine = class _WorkflowEngine {
8938
10007
  * for a pod that does have the workflow — which is exactly what the skew-protection error asks
8939
10008
  * for. Same posture as `publishEvent`'s per-subscriber isolation.
8940
10009
  */
10010
+ /**
10011
+ * Run `claimed` (pending runs this instance just leased, read as locked): every turn starts at
10012
+ * once, and each re-enters the lease taken for it without reading the run again.
10013
+ */
10014
+ async runClaimed(claimed) {
10015
+ const turns = claimed.map(async (run) => {
10016
+ if (this.draining) {
10017
+ await this.releaseLeaseBestEffort(run.id);
10018
+ return void 0;
10019
+ }
10020
+ this.lockedForTurn.add(run.id);
10021
+ this.claimedRuns.set(run.id, run);
10022
+ try {
10023
+ return await this.resumeImplicit(run.id);
10024
+ } catch (error2) {
10025
+ if (error2 instanceof WorkflowNotRegistered) {
10026
+ const settled = await this.settleUnregistered(run, this.clock()).catch(() => void 0);
10027
+ if (settled) return settled;
10028
+ if (this.isStartOnlyHere(run)) return void 0;
10029
+ }
10030
+ console.error(`[nestjs-durable] could not resume run ${run.id} (${run.workflow}@${run.workflowVersion}); leaving it for another instance:`, error2);
10031
+ return void 0;
10032
+ } finally {
10033
+ this.lockedForTurn.delete(run.id);
10034
+ this.claimedRuns.delete(run.id);
10035
+ }
10036
+ });
10037
+ return (await Promise.all(turns)).filter((r) => r !== void 0);
10038
+ }
8941
10039
  async resumeLeased(runs, nowMs = this.clock(), onLocked) {
8942
10040
  if (this.draining) return [];
8943
10041
  const results = [];
@@ -8957,7 +10055,7 @@ var WorkflowEngine = class _WorkflowEngine {
8957
10055
  if (error2 instanceof WorkflowNotRegistered) {
8958
10056
  const settled = await this.settleUnregistered(run, nowMs).catch(() => void 0);
8959
10057
  if (settled) results.push(settled);
8960
- if (settled !== void 0) continue;
10058
+ if (settled !== void 0 || this.isStartOnlyHere(run)) continue;
8961
10059
  }
8962
10060
  console.error(`[nestjs-durable] could not resume run ${run.id} (${run.workflow}@${run.workflowVersion}); leaving it for another instance:`, error2);
8963
10061
  } finally {
@@ -9037,7 +10135,8 @@ var WorkflowEngine = class _WorkflowEngine {
9037
10135
  * due-timer reconcile sweep ({@link reconcileSignalWaiter}) — all three resolve a waiter the exact
9038
10136
  * same way.
9039
10137
  */
9040
- async deliverSignal(waiter, payload, resumeHere = false) {
10138
+ async deliverSignal(waiter, payload, resumeHere = false, committed) {
10139
+ if (isAnyMember(waiter)) return this.deliverToWaitAny(waiter, payload, resumeHere, committed);
9041
10140
  await this.store.saveCheckpoint(instantCheckpoint({
9042
10141
  runId: waiter.runId,
9043
10142
  seq: waiter.seq,
@@ -9050,6 +10149,7 @@ var WorkflowEngine = class _WorkflowEngine {
9050
10149
  // for an ordinary (non-fan) signal/child await.
9051
10150
  parallelGroup: waiter.parallelGroup
9052
10151
  }));
10152
+ committed?.();
9053
10153
  if (await this.fanStillWaiting(waiter, payload)) {
9054
10154
  return {
9055
10155
  runId: waiter.runId,
@@ -9062,6 +10162,47 @@ var WorkflowEngine = class _WorkflowEngine {
9062
10162
  });
9063
10163
  }
9064
10164
  /**
10165
+ * Deliver to a waiter that is one condition of a `ctx.waitForAny` (see `wait-any.ts`): settle the
10166
+ * wait with this condition through the checkpoint's compare-and-set, then wake the run. A signal
10167
+ * that loses — the wait was settled by another condition first, or a timer of the wait came due
10168
+ * before it arrived (the timer then wins) — is not consumed: it is sent again on its token, where
10169
+ * it is buffered for the next wait (or reaches a waiter registered since).
10170
+ */
10171
+ async deliverToWaitAny(waiter, payload, resumeHere, committed) {
10172
+ const cp = await this.store.getCheckpoint(waiter.runId, waiter.seq);
10173
+ const conditions = cp && cp.status === "pending" && isAnyCheckpoint(cp) ? anyConditionsOf(cp) : [];
10174
+ const condition = conditions.find((c) => anyConditionToken(c) === waiter.token);
10175
+ const run = cp && condition ? await this.store.getRun(waiter.runId) : null;
10176
+ if (!cp || !condition || !run || TERMINAL_RUN_STATUSES.includes(run.status)) {
10177
+ if (cp && condition) await removeAnyWaiters(this.store, cp);
10178
+ await this.redeliverLosingSignal(waiter.token, payload);
10179
+ committed?.();
10180
+ return null;
10181
+ }
10182
+ const due = dueTimer(conditions, this.clock());
10183
+ const done = due ? resolvedAnyCheckpoint(cp, due, null) : resolvedAnyCheckpoint(cp, condition, payload);
10184
+ const won = await claimAnyCheckpoint(this.store, done);
10185
+ if (won) await removeAnyWaiters(this.store, done);
10186
+ if (!won || due) await this.redeliverLosingSignal(waiter.token, payload);
10187
+ committed?.();
10188
+ if (!won) return null;
10189
+ return (resumeHere ? this.resumeImplicit(waiter.runId) : this.wake(waiter.runId)).catch((err) => {
10190
+ if (err instanceof NamespaceMismatch) return null;
10191
+ throw err;
10192
+ });
10193
+ }
10194
+ /**
10195
+ * Put back a signal a `waitForAny` condition took but did not win with. A child's completion is
10196
+ * kept only where a parent can still consume it, as {@link deliverToParent} decides.
10197
+ */
10198
+ async redeliverLosingSignal(token, payload) {
10199
+ if (token.startsWith(CHILD_TOKEN_PREFIX)) {
10200
+ const childId = token.slice(CHILD_TOKEN_PREFIX.length);
10201
+ if (!await childCompletionNeedsBuffer(this.store, childId, payload)) return;
10202
+ }
10203
+ await this.deliverOrBuffer(token, payload);
10204
+ }
10205
+ /**
9065
10206
  * The `ctx.all` join barrier: true when `waiter` was one child of a fan (or of one wave of it)
9066
10207
  * whose siblings are still out, so waking the parent now would only replay the fan and re-suspend.
9067
10208
  * Skipping that wake is what keeps a fan of N children at O(N) work instead of N replays of N items.
@@ -9194,7 +10335,7 @@ var WorkflowEngine = class _WorkflowEngine {
9194
10335
  return;
9195
10336
  }
9196
10337
  const settled = new Set(checkpoints.filter((cp) => cp.status === "completed").map((cp) => cp.seq));
9197
- const awaited = waiters.filter((w) => w.token.startsWith("child:") && !settled.has(w.seq)).map((w) => ({
10338
+ const awaited = waiters.filter((w) => w.token.startsWith("child:") && !settled.has(w.seq) && !isAnyMember(w)).map((w) => ({
9198
10339
  childId: w.token.slice("child:".length),
9199
10340
  seq: w.seq,
9200
10341
  parallelGroup: w.parallelGroup
@@ -9285,10 +10426,51 @@ var WorkflowEngine = class _WorkflowEngine {
9285
10426
  * Either way, the destructive `take*` ops (on the waiter row, on the buffer) arbitrate: whichever
9286
10427
  * side actually removes the row is the one that delivers, so a signal is never delivered twice.
9287
10428
  */
9288
- async signal(token, payload) {
10429
+ async signal(token, payload, opts) {
10430
+ const id = opts?.id;
10431
+ if (id === void 0) return this.deliverOrBuffer(token, payload);
10432
+ if (!this.store.claimSignalId) {
10433
+ throw new Error("engine.signal(..., { id }) needs a store that deduplicates signals (claimSignalId); the bundled stores do");
10434
+ }
10435
+ const key = `${token}\0${id}`;
10436
+ for (let prior = this.signalIdSends.get(key); prior; prior = this.signalIdSends.get(key)) {
10437
+ const sent = await prior.then(() => true, () => false);
10438
+ if (sent) return null;
10439
+ }
10440
+ const send = this.signalOnce(token, payload, id, opts?.idTtl);
10441
+ this.signalIdSends.set(key, send);
10442
+ try {
10443
+ return await send;
10444
+ } finally {
10445
+ if (this.signalIdSends.get(key) === send) this.signalIdSends.delete(key);
10446
+ }
10447
+ }
10448
+ /** One deduplicated {@link signal}: claim `id`, then deliver or buffer; a failure forgets the id. */
10449
+ async signalOnce(token, payload, id, idTtl) {
10450
+ const claimSignalId = this.store.claimSignalId;
10451
+ if (!claimSignalId) return null;
10452
+ const now = this.clock();
10453
+ const ttl = idTtl !== void 0 ? parseDuration(idTtl) : this.signalIdTtlMs;
10454
+ if (!await claimSignalId.call(this.store, token, id, now + ttl, now)) return null;
10455
+ let committed = false;
10456
+ try {
10457
+ return await this.deliverOrBuffer(token, payload, () => {
10458
+ committed = true;
10459
+ });
10460
+ } catch (err) {
10461
+ if (!committed) await this.store.releaseSignalId?.(token, id).catch(() => void 0);
10462
+ throw err;
10463
+ }
10464
+ }
10465
+ /**
10466
+ * {@link signal} without deduplication: deliver to the waiter on `token`, or buffer. `committed`
10467
+ * is called once the signal is durably delivered or buffered.
10468
+ */
10469
+ async deliverOrBuffer(token, payload, committed) {
9289
10470
  const waiter = await this.store.takeSignalWaiter(token);
9290
- if (waiter) return this.deliverSignal(waiter, payload);
10471
+ if (waiter) return this.deliverSignal(waiter, payload, false, committed);
9291
10472
  await this.store.bufferSignal(token, payload);
10473
+ committed?.();
9292
10474
  const lateWaiter = await this.store.takeSignalWaiter(token);
9293
10475
  if (!lateWaiter) return null;
9294
10476
  const reclaimed = await this.store.takeBufferedSignal(token);
@@ -9650,10 +10832,32 @@ var WorkflowEngine = class _WorkflowEngine {
9650
10832
  */
9651
10833
  async cancelChildren(parentRunId, opts) {
9652
10834
  for (const id of await this.getRunChildren(parentRunId)) {
10835
+ const child = await this.store.getRun(id).catch(() => null);
10836
+ if (child?.tags?.includes(`${PARENT_CLOSE_TAG_PREFIX}abandon`)) continue;
9653
10837
  await this.cancel(id, opts).catch(() => void 0);
9654
10838
  }
9655
10839
  }
9656
10840
  /**
10841
+ * Apply the `parentClose` policies a parent's turn recorded (see {@link ChildCallOptions.parentClose})
10842
+ * when that parent completes or fails; forget them when it suspends (its next turn, here or on
10843
+ * another instance, records them again). Local events only: this is the instance that ran the turn.
10844
+ */
10845
+ settleParentClose(event) {
10846
+ if (event.type !== "run.completed" && event.type !== "run.failed" && event.type !== "run.suspended") {
10847
+ return;
10848
+ }
10849
+ const intents = this.parentCloseIntents.get(event.runId);
10850
+ if (!intents) return;
10851
+ this.parentCloseIntents.delete(event.runId);
10852
+ if (event.type === "run.suspended") return;
10853
+ for (const [childId, policy] of intents) {
10854
+ if (policy === "abandon") continue;
10855
+ queueMicrotask(() => void this.cancel(childId, {
10856
+ compensate: policy === "cancel"
10857
+ }).catch(() => void 0));
10858
+ }
10859
+ }
10860
+ /**
9657
10861
  * The ids of the runs a run spawned — both awaited (`ctx.child`) and fire-and-forget
9658
10862
  * (`ctx.startChild`, found via its `spawn:<id>` checkpoint). The canonical parent→children edge,
9659
10863
  * used for both cancellation cascades and the dashboard run-tree.
@@ -9670,7 +10874,7 @@ var WorkflowEngine = class _WorkflowEngine {
9670
10874
  async awaitedChildIds(parentRunId) {
9671
10875
  return (await listSignalWaitersOfRuns(this.store, [
9672
10876
  parentRunId
9673
- ])).filter((w) => w.token.startsWith("child:")).map((w) => w.token.slice("child:".length));
10877
+ ])).filter((w) => w.token.startsWith("child:") && !isAnyMember(w)).map((w) => w.token.slice("child:".length));
9674
10878
  }
9675
10879
  async getRunChildren(parentRunId) {
9676
10880
  const childIds = new Set(await this.awaitedChildIds(parentRunId));
@@ -9958,15 +11162,19 @@ var WorkflowEngine = class _WorkflowEngine {
9958
11162
  void this.announcedSubscriptions();
9959
11163
  const tags = singleton ? withSingletonTracked(run.tags) : run.tags;
9960
11164
  const retag = tags !== run.tags;
9961
- await this.store.updateRun(run.id, {
11165
+ const started = await (this.store.updateRunIf ?? this.updateRunIfFallback).call(this.store, run.id, [
11166
+ "pending"
11167
+ ], {
9962
11168
  status: "running",
9963
11169
  ...retag ? {
9964
11170
  tags
9965
11171
  } : {},
9966
11172
  updatedAt: /* @__PURE__ */ new Date()
9967
11173
  });
11174
+ if (!started) return false;
9968
11175
  run.status = "running";
9969
11176
  if (retag) run.tags = tags;
11177
+ return true;
9970
11178
  }
9971
11179
  async singletonWaitResult(run) {
9972
11180
  const current = await this.store.getRun(run.id);
@@ -10036,7 +11244,7 @@ var WorkflowEngine = class _WorkflowEngine {
10036
11244
  if (result.status === "suspended") {
10037
11245
  this.inBackground(this.honorWakeRequests(run.id, settledFans, served));
10038
11246
  } else if (TERMINAL_RUN_STATUSES.includes(result.status)) {
10039
- this.inBackground(this.listWakeRequests(run.id, true).then((kept) => this.dropWakeRequests(kept)));
11247
+ this.inBackground(this.listWakeRequests(run.id).then((kept) => this.dropWakeRequests(kept)));
10040
11248
  }
10041
11249
  return result;
10042
11250
  } finally {
@@ -10087,7 +11295,7 @@ var WorkflowEngine = class _WorkflowEngine {
10087
11295
  * the timer poll of another instance runs it once this instance's hold on it would have lapsed.
10088
11296
  */
10089
11297
  async honorWakeRequests(runId, settledFans, served) {
10090
- const kept = await this.listWakeRequests(runId, true);
11298
+ const kept = await this.listWakeRequests(runId);
10091
11299
  const seen = /* @__PURE__ */ __name((request) => wakeSeen(request, settledFans, served), "seen");
10092
11300
  const needed = kept.filter((request) => !seen(request));
10093
11301
  await this.dropWakeRequests(kept.filter(seen));
@@ -10097,30 +11305,17 @@ var WorkflowEngine = class _WorkflowEngine {
10097
11305
  if (!(err instanceof NamespaceMismatch)) throw err;
10098
11306
  });
10099
11307
  }
10100
- /**
10101
- * The wake requests kept for `runId`. With `legacy`, also those an older engine kept as buffered
10102
- * signals, which are taken as they are read.
10103
- */
10104
- async listWakeRequests(runId, legacy = false) {
10105
- const token = wakeRequestToken(runId);
10106
- const kept = (await this.store.listBufferedEvents(token, WAKE_REQUEST_SCAN)).map((request) => ({
11308
+ /** The wake requests kept for `runId`. */
11309
+ async listWakeRequests(runId) {
11310
+ const kept = await this.store.listBufferedEvents(wakeRequestToken(runId), WAKE_REQUEST_SCAN);
11311
+ return kept.map((request) => ({
10107
11312
  id: request.id,
10108
11313
  payload: request.payload
10109
11314
  }));
10110
- if (!legacy) return kept;
10111
- for (; ; ) {
10112
- const signal = await this.store.takeBufferedSignal(token);
10113
- if (!signal) return kept;
10114
- kept.push({
10115
- payload: signal.payload
10116
- });
10117
- }
10118
11315
  }
10119
- /** Delete `requests` (the ones kept as buffered events). */
11316
+ /** Delete `requests`. */
10120
11317
  async dropWakeRequests(requests) {
10121
- await Promise.all(requests.flatMap((request) => request.id === void 0 ? [] : [
10122
- this.store.removeBufferedEvent(request.id)
10123
- ]));
11318
+ await Promise.all(requests.map((request) => this.store.removeBufferedEvent(request.id)));
10124
11319
  }
10125
11320
  /**
10126
11321
  * Record, for the turn executing `runId` here, the wakes kept for the run so far. Called before the
@@ -10526,12 +11721,15 @@ ${run.workflowVersion}`;
10526
11721
  };
10527
11722
  }
10528
11723
  if (outcome.kind === "blocked") return this.parkBlocked(run, outcome.blocked);
11724
+ const stamp = new Date(Math.max(updatedAt.getTime(), (run.updatedAt?.getTime() ?? 0) + 1));
10529
11725
  const applied = await (this.store.updateRunIf ?? this.updateRunIfFallback).call(this.store, run.id, SUSPEND_ELIGIBLE_STATUSES, await this.withRunTrace(run, {
10530
11726
  status: "suspended",
10531
11727
  wakeAt: this.reconcileWakeAt(outcome.wakeAt),
10532
- updatedAt
11728
+ updatedAt: stamp
10533
11729
  }));
10534
11730
  if (!applied) return this.echoCurrentStatus(run.id);
11731
+ run.updatedAt = stamp;
11732
+ this.suspendStamps.set(run.id, stamp.getTime());
10535
11733
  await this.keepDueForKeptWakes(run.id);
10536
11734
  this.emit({
10537
11735
  type: "run.suspended",
@@ -10591,7 +11789,10 @@ ${run.workflowVersion}`;
10591
11789
  }
10592
11790
  const remote = registered.remote;
10593
11791
  if (run.status === "pending") {
10594
- await this.markStarted(run, registered?.singleton !== void 0);
11792
+ if (!await this.markStarted(run, registered?.singleton !== void 0)) {
11793
+ await this.releaseLeaseBestEffort(run.id);
11794
+ return await this.echoCurrentStatus(run.id);
11795
+ }
10595
11796
  this.emit({
10596
11797
  type: "run.started",
10597
11798
  runId: run.id,
@@ -10603,7 +11804,9 @@ ${run.workflowVersion}`;
10603
11804
  if (registered.singleton && !await this.singletons.admit(run, registered.singleton)) {
10604
11805
  return this.singletonWaitResult(run);
10605
11806
  }
10606
- const wfPlan = await this.planRoute(tenantGroup(sanitizeQueueToken(remote.group), void 0), registered.requires);
11807
+ const wfToken = tenantGroup(sanitizeQueueToken(remote.group), void 0);
11808
+ let wfPlan = await this.planRoute(wfToken, registered.requires);
11809
+ if (wfPlan?.status === "blocked") wfPlan = await this.planRoute(wfToken, registered.requires, true);
10607
11810
  if (wfPlan?.status === "blocked") return this.parkBlocked(run, wfPlan);
10608
11811
  const checkpoints = await this.replayCheckpoints(run.id);
10609
11812
  await this.deliverFinishedChildren(run.id, checkpoints);
@@ -10732,6 +11935,7 @@ ${run.workflowVersion}`;
10732
11935
  kind: "suspended",
10733
11936
  wakeAt
10734
11937
  });
11938
+ this.suspendStamps.delete(run.id);
10735
11939
  await this.redriveIfTurnSawStaleHistory(run.id, decision.commands);
10736
11940
  return suspended;
10737
11941
  }
@@ -10985,6 +12189,10 @@ ${run.workflowVersion}`;
10985
12189
  for (const cp of checkpoints) {
10986
12190
  if (cp.status === "completed" || cp.status === "failed") {
10987
12191
  if (cp.seq >= HANDOFF_SEQ_BASE) continue;
12192
+ if (isAnyCheckpoint(cp)) {
12193
+ events.push(anyHistoryEvent(cp));
12194
+ continue;
12195
+ }
10988
12196
  if (cp.kind === "signal" && cp.name.startsWith("signal:child:")) {
10989
12197
  const completion = cp.output;
10990
12198
  events.push({
@@ -11152,6 +12360,16 @@ ${run.workflowVersion}`;
11152
12360
  await deliverBuffered(lateBuffered.payload);
11153
12361
  }
11154
12362
  }
12363
+ } else if (cmd.kind === "waitAny") {
12364
+ const state = await enterWaitAny({
12365
+ store: this.store,
12366
+ clock: this.clock
12367
+ }, run.id, cmd.seq, cmd.name, cmd.conditions);
12368
+ if (state.state === "resolved") {
12369
+ setTimeout(() => void this.resumeImplicit(run.id).catch(() => void 0), 0);
12370
+ } else if (state.wakeAt != null) {
12371
+ wakeAt = wakeAt == null ? state.wakeAt : Math.min(wakeAt, state.wakeAt);
12372
+ }
11155
12373
  } else if (cmd.kind === "startChild") {
11156
12374
  const childId = `${run.id}.child.${cmd.seq}`;
11157
12375
  await this.store.putSignalWaiter({
@@ -11250,7 +12468,10 @@ ${run.workflowVersion}`;
11250
12468
  async runExecutionTurn(run, fn, registered) {
11251
12469
  if (run.status === "cancelling") this.cancelRequested.add(run.id);
11252
12470
  if (run.status === "pending") {
11253
- await this.markStarted(run, registered?.singleton !== void 0);
12471
+ if (!await this.markStarted(run, registered?.singleton !== void 0)) {
12472
+ await this.releaseLeaseBestEffort(run.id);
12473
+ return await this.echoCurrentStatus(run.id);
12474
+ }
11254
12475
  this.emit({
11255
12476
  type: "run.started",
11256
12477
  runId: run.id,
@@ -11263,24 +12484,52 @@ ${run.workflowVersion}`;
11263
12484
  if (run.status !== "cancelling" && registered?.singleton && !await this.singletons.admit(run, registered.singleton)) {
11264
12485
  return this.singletonWaitResult(run);
11265
12486
  }
11266
- const compensations = [];
11267
- const snapshot = await this.replayCheckpoints(run.id);
11268
- await this.deliverFinishedChildren(run.id, snapshot);
11269
- const replay = /* @__PURE__ */ new Map();
12487
+ const stickyOk = this.stickyEligible(run);
12488
+ let cont = stickyOk ? this.takeContinuation(run, fn) : void 0;
12489
+ const warm = cont !== void 0;
12490
+ const parkedFresh = cont ? await this.readParked(run.id, cont) : void 0;
12491
+ const compensations = cont ? cont.compensations : [];
12492
+ const snapshot = cont ? [] : await this.replayCheckpoints(run.id);
12493
+ if (!cont) await this.deliverFinishedChildren(run.id, snapshot);
12494
+ const replay = cont ? cont.replay : /* @__PURE__ */ new Map();
11270
12495
  for (const cp of snapshot) replay.set(cp.seq, cp);
12496
+ if (!cont && stickyOk) {
12497
+ cont = new StickyContinuation(run.id, fn, run.workflowVersion, compensations, replay, (c) => this.sticky.evict(c.runId));
12498
+ }
12499
+ if (cont) this.turnContinuations.set(run.id, cont);
12500
+ let parkedTurn = false;
11271
12501
  let suspendedOnFan;
11272
12502
  let suspendedOnSteps = false;
11273
12503
  const awaitedSteps = /* @__PURE__ */ new Set();
11274
12504
  this.awaitedRemoteSteps.set(run.id, awaitedSteps);
11275
12505
  const awaitedFans = [];
11276
12506
  this.awaitedStepFans.set(run.id, awaitedFans);
11277
- const ctx = createWorkflowCtx(this.ctxHostFor(replay, run.namespace), run.id, compensations, run.workflow, registered?.searchAttributesSchema);
12507
+ const ctx = warm ? void 0 : createWorkflowCtx(this.ctxHostFor(replay, run.namespace, cont), run.id, compensations, run.workflow, registered?.searchAttributesSchema);
11278
12508
  try {
11279
12509
  const handoff = liveHandoff(run.id, snapshot);
11280
12510
  if (handoff && !this.cancelRequested.has(run.id)) {
11281
12511
  return await this.settleRun(run, await this.followHandoff(run, handoff));
11282
12512
  }
11283
- const output = await runInWorkflowCtx(ctx, () => fn(ctx, run.input));
12513
+ let output;
12514
+ if (cont) {
12515
+ if (parkedFresh) {
12516
+ cont.attached = true;
12517
+ await cont.resumeParked(parkedFresh);
12518
+ } else {
12519
+ const body = ctx;
12520
+ cont.begin(() => runInWorkflowCtx(body, () => fn(body, run.input)));
12521
+ }
12522
+ const outcome = await cont.next();
12523
+ if (outcome.kind === "parked") {
12524
+ parkedTurn = true;
12525
+ throw new WorkflowSuspended(outcome.wakeAt);
12526
+ }
12527
+ if (outcome.kind === "threw") throw outcome.error;
12528
+ output = outcome.value;
12529
+ } else {
12530
+ const body = ctx;
12531
+ output = await runInWorkflowCtx(body, () => fn(body, run.input));
12532
+ }
11284
12533
  if (this.cancelRequested.has(run.id)) {
11285
12534
  return await this.compensateAndCancel(run, compensations, replay);
11286
12535
  }
@@ -11338,6 +12587,11 @@ ${run.workflowVersion}`;
11338
12587
  });
11339
12588
  suspendedOnFan = err.fan;
11340
12589
  suspendedOnSteps = suspended.status === "suspended";
12590
+ const stamp = this.suspendStamps.get(run.id);
12591
+ this.suspendStamps.delete(run.id);
12592
+ if (parkedTurn && cont && suspendedOnSteps && stamp !== void 0) {
12593
+ this.keepContinuation(cont, stamp);
12594
+ }
11341
12595
  return suspended;
11342
12596
  }
11343
12597
  if (this.cancelRequested.has(run.id)) {
@@ -11362,6 +12616,8 @@ ${run.workflowVersion}`;
11362
12616
  this.awaitedRemoteSteps.delete(run.id);
11363
12617
  }
11364
12618
  if (this.awaitedStepFans.get(run.id) === awaitedFans) this.awaitedStepFans.delete(run.id);
12619
+ if (cont && this.turnContinuations.get(run.id) === cont) this.turnContinuations.delete(run.id);
12620
+ this.suspendStamps.delete(run.id);
11365
12621
  await this.releaseLeaseBestEffort(run.id);
11366
12622
  if (suspendedOnFan) {
11367
12623
  this.inBackground(this.redriveSettledChildren(run.id, suspendedOnFan));
@@ -11579,10 +12835,11 @@ ${run.workflowVersion}`;
11579
12835
  /** The seam handed to {@link createWorkflowCtx}: the authoring API reaches durability + lifecycle
11580
12836
  * (checkpointing, dispatch, child start) through this, so the ctx primitives live in their own
11581
12837
  * module and the engine stays the orchestrator. */
11582
- ctxHostFor(replay, parentNamespace) {
12838
+ ctxHostFor(replay, parentNamespace, sticky) {
11583
12839
  return {
11584
12840
  store: this.store,
11585
12841
  replay,
12842
+ sticky,
11586
12843
  clock: this.clock,
11587
12844
  webhookUrl: this.webhookUrl,
11588
12845
  startStep: /* @__PURE__ */ __name((s) => this.startStep(s), "startStep"),
@@ -11591,9 +12848,27 @@ ${run.workflowVersion}`;
11591
12848
  // Route the dispatch by the RUN's tenant (same reason as `startChild` below): the executing
11592
12849
  // engine may be an operator driving every namespace, so the token must come from the run, not
11593
12850
  // from this engine. See {@link stepGroup}.
11594
- callRemote: /* @__PURE__ */ __name((runId, seq, step, input, queue, transport, admission) => this.callRemote(runId, seq, step, input, queue, transport, replay, admission, {
11595
- namespace: parentNamespace
11596
- }), "callRemote"),
12851
+ callRemote: /* @__PURE__ */ __name((runId, seq, step, input, queue, transport, admission) => {
12852
+ const routing = {
12853
+ namespace: parentNamespace
12854
+ };
12855
+ const call = this.callRemote(runId, seq, step, input, queue, transport, replay, admission, {
12856
+ namespace: parentNamespace
12857
+ });
12858
+ if (!sticky) return call;
12859
+ return call.catch((err) => {
12860
+ if (!(err instanceof WorkflowSuspended) || !sticky.attached) throw err;
12861
+ return sticky.park(seq, err.wakeAt, (fresh) => this.callRemote(runId, seq, step, input, queue, transport, fresh ? /* @__PURE__ */ new Map([
12862
+ [
12863
+ seq,
12864
+ fresh
12865
+ ]
12866
+ ]) : void 0, admission, {
12867
+ ...routing,
12868
+ fresh: true
12869
+ }));
12870
+ });
12871
+ }, "callRemote"),
11597
12872
  callRemoteAll: /* @__PURE__ */ __name((runId, positions, step, inputs, opts) => this.callRemoteAll(runId, positions, step, inputs, opts, {
11598
12873
  namespace: parentNamespace
11599
12874
  }, replay), "callRemoteAll"),
@@ -11603,15 +12878,29 @@ ${run.workflowVersion}`;
11603
12878
  // namespace) is the one executing the parent. Otherwise an operator recovery-resuming a
11604
12879
  // `davi-local` pipeline would stamp its `processing` child `default` and it would leak off the
11605
12880
  // tenant's worker pool onto the shared/dev workers.
11606
- startChild: /* @__PURE__ */ __name((workflow, input, id, priority, version) => {
12881
+ startChild: /* @__PURE__ */ __name((workflow, input, id, priority, version, parentClose) => {
11607
12882
  this.startChildDeferred(workflow, input, id, {
11608
12883
  priority,
11609
12884
  namespace: parentNamespace,
11610
12885
  // Absent unless the body pinned one (`ctx.child(ref, input, { version })`) — an omitted
11611
12886
  // pin must stay omitted, so the child keeps resolving `latest` exactly as before.
11612
- version
12887
+ version,
12888
+ // The policy travels with the child, so a cancel cascade run by ANY instance can read it.
12889
+ ...parentClose ? {
12890
+ tags: [
12891
+ `${PARENT_CLOSE_TAG_PREFIX}${parentClose}`
12892
+ ]
12893
+ } : {}
11613
12894
  });
11614
12895
  }, "startChild"),
12896
+ noteParentClose: /* @__PURE__ */ __name((parentRunId, childId, policy) => {
12897
+ let intents = this.parentCloseIntents.get(parentRunId);
12898
+ if (!intents) {
12899
+ intents = /* @__PURE__ */ new Map();
12900
+ this.parentCloseIntents.set(parentRunId, intents);
12901
+ }
12902
+ intents.set(childId, policy);
12903
+ }, "noteParentClose"),
11615
12904
  // Deferred for the same reentrancy reason as `startChild` above. `cancel()` is already
11616
12905
  // idempotent on a terminal/cancelled run (returns its existing status without side effects), so
11617
12906
  // no extra guard is needed here for the failFast replay case (re-issuing the same cancel calls).
@@ -11721,17 +13010,35 @@ ${run.workflowVersion}`;
11721
13010
  * - a `routable` plan — ≥1 live worker is capability-capable AND protocol-compatible (dispatch);
11722
13011
  * - a `blocked` plan — descriptors exist but none can run it (park the run, never dispatch into it).
11723
13012
  */
11724
- async planRoute(token, requires) {
11725
- const descriptors = await this.pool.readWorkerDescriptors(token);
13013
+ async planRoute(token, requires, fresh = false) {
13014
+ const descriptors = await this.liveWorkerDescriptors(token, fresh);
11726
13015
  if (descriptors.length === 0) return void 0;
11727
13016
  return planDispatch(requires ?? [], descriptors, this.cpDescriptor, token);
11728
13017
  }
13018
+ /** The live descriptors for `token`, reused for {@link workerDescriptorCacheMs}; one read in flight. */
13019
+ liveWorkerDescriptors(token, fresh = false) {
13020
+ if (this.workerDescriptorCacheMs <= 0) return this.pool.readWorkerDescriptors(token);
13021
+ const now = Date.now();
13022
+ const cached = this.descriptorCache.get(token);
13023
+ if (!fresh && cached && now - cached.at < this.workerDescriptorCacheMs) return cached.read;
13024
+ const read = this.pool.readWorkerDescriptors(token);
13025
+ const entry = {
13026
+ at: now,
13027
+ read
13028
+ };
13029
+ this.descriptorCache.set(token, entry);
13030
+ read.catch(() => {
13031
+ if (this.descriptorCache.get(token) === entry) this.descriptorCache.delete(token);
13032
+ });
13033
+ return read;
13034
+ }
11729
13035
  /** Throw {@link WorkflowBlocked} when no live worker can run a dispatch to `token` — so the caller's
11730
13036
  * run-execution catch parks the run `blocked` (with the plan) instead of persisting a `pending`
11731
13037
  * checkpoint + dispatching into a queue nobody consumes (design §7.5). A no-op when the guard does
11732
13038
  * not engage (no descriptors) or a capable+compatible worker exists. */
11733
13039
  async ensureRoutable(token, requires) {
11734
- const plan = await this.planRoute(token, requires);
13040
+ let plan = await this.planRoute(token, requires);
13041
+ if (plan?.status === "blocked") plan = await this.planRoute(token, requires, true);
11735
13042
  if (plan?.status === "blocked") throw new WorkflowBlocked(plan);
11736
13043
  }
11737
13044
  /**
@@ -11789,8 +13096,11 @@ ${run.workflowVersion}`;
11789
13096
  }
11790
13097
  async callRemote(runId, seq, step, input, queue, transport, replay, admission, routing) {
11791
13098
  const group = stepGroup(step, routing?.namespace);
13099
+ 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)) {
13100
+ throw new WorkflowSuspended();
13101
+ }
11792
13102
  let existing = replay?.get(seq) ?? await this.store.getCheckpoint(runId, seq);
11793
- if (existing?.status === "pending" && replay?.has(seq)) {
13103
+ if (existing?.status === "pending" && replay?.has(seq) && !routing?.fresh) {
11794
13104
  existing = await this.store.getCheckpoint(runId, seq) ?? existing;
11795
13105
  }
11796
13106
  if (existing && existing.name !== step.name) {
@@ -11850,7 +13160,9 @@ ${run.workflowVersion}`;
11850
13160
  input: existing.input,
11851
13161
  priority: admission?.priority,
11852
13162
  attempt: reAttempt,
11853
- transport
13163
+ transport,
13164
+ // A fan's steps are joined by its barrier, not a parked body: their results stay shared.
13165
+ replyHere: routing?.parallelGroup === void 0
11854
13166
  });
11855
13167
  this.emit({
11856
13168
  type: "step.started",
@@ -11922,7 +13234,8 @@ ${run.workflowVersion}`;
11922
13234
  input,
11923
13235
  priority: admission?.priority,
11924
13236
  attempt,
11925
- transport
13237
+ transport,
13238
+ replyHere: routing?.parallelGroup === void 0
11926
13239
  });
11927
13240
  this.emit({
11928
13241
  type: "step.started",
@@ -11935,6 +13248,61 @@ ${run.workflowVersion}`;
11935
13248
  throw new WorkflowSuspended();
11936
13249
  }
11937
13250
  /**
13251
+ * {@link callRemote}'s first dispatch of a step, written with
13252
+ * {@link StateStore.insertCheckpointIfAbsent}: resolves whether it wrote the `pending` row and
13253
+ * dispatched. `false` when the position already has a row, or when no live worker can take the
13254
+ * step (the read-first path then decides, so a step that is in fact done is never blocked).
13255
+ */
13256
+ async dispatchIfAbsent(runId, seq, step, input, group, transport, admission, routing) {
13257
+ try {
13258
+ await this.ensureRoutable(group, step.requires);
13259
+ } catch (err) {
13260
+ if (err instanceof WorkflowBlocked) return false;
13261
+ throw err;
13262
+ }
13263
+ const id = stepId(runId, seq);
13264
+ const enqueuedAt = /* @__PURE__ */ new Date();
13265
+ const wrote = await this.store.insertCheckpointIfAbsent.call(this.store, {
13266
+ runId,
13267
+ seq,
13268
+ name: step.name,
13269
+ kind: "remote",
13270
+ stepId: id,
13271
+ status: "pending",
13272
+ input,
13273
+ attempts: 1,
13274
+ workerGroup: group,
13275
+ ...routing?.parallelGroup !== void 0 ? {
13276
+ parallelGroup: routing.parallelGroup
13277
+ } : {},
13278
+ enqueuedAt,
13279
+ startedAt: enqueuedAt,
13280
+ finishedAt: enqueuedAt
13281
+ });
13282
+ if (!wrote) return false;
13283
+ await this.dispatchRemoteTask({
13284
+ runId,
13285
+ seq,
13286
+ name: step.name,
13287
+ stepId: id,
13288
+ group,
13289
+ input,
13290
+ priority: admission?.priority,
13291
+ attempt: 1,
13292
+ transport,
13293
+ replyHere: routing?.parallelGroup === void 0
13294
+ });
13295
+ this.emit({
13296
+ type: "step.started",
13297
+ runId,
13298
+ seq,
13299
+ name: step.name,
13300
+ kind: "remote"
13301
+ });
13302
+ this.awaitedRemoteSteps.get(runId)?.add(seq);
13303
+ return true;
13304
+ }
13305
+ /**
11938
13306
  * Whether a `pending` step a turn just read only waits for its result: {@link callRemote} would
11939
13307
  * suspend on it without writing (no redispatch deadline to stamp, the one stamped not yet due).
11940
13308
  */
@@ -12190,6 +13558,69 @@ ${run.workflowVersion}`;
12190
13558
  }
12191
13559
  }
12192
13560
  /**
13561
+ * Whether this turn of `run` may run in a sticky continuation (see `sticky-continuation.ts`). Not
13562
+ * while the engine drains, for a cancel being compensated, nor when execution wrappers are
13563
+ * registered: a resumed body runs in the async scope of the turn that started it, and a wrapper
13564
+ * (a span, a log batch) expects one scope per turn.
13565
+ */
13566
+ stickyEligible(run) {
13567
+ return this.sticky.enabled && !this.draining && run.status !== "cancelling" && !this.cancelRequested.has(run.id) && !hasDurableExecutionWrappers();
13568
+ }
13569
+ /**
13570
+ * The continuation this instance parked for `run`, if it is still the run's latest turn: the run
13571
+ * is `suspended` with the very stamp this instance's suspend wrote, on the same body and version.
13572
+ * Any other settle in between (another instance's turn, a retry, a cancel) changed the stamp, and
13573
+ * the run replays instead. Taking it removes it from the cache.
13574
+ */
13575
+ takeContinuation(run, fn) {
13576
+ const cont = this.sticky.take(run.id);
13577
+ if (!cont) return void 0;
13578
+ const valid = cont.fn === fn && cont.workflowVersion === run.workflowVersion && run.status === "suspended" && cont.stamp !== void 0 && run.updatedAt?.getTime() === cont.stamp;
13579
+ if (!valid) {
13580
+ this.sticky.misses += 1;
13581
+ return void 0;
13582
+ }
13583
+ this.sticky.hits += 1;
13584
+ return cont;
13585
+ }
13586
+ /** Keep `cont` (parked, detached) for the run's next turn here, stamped with its suspend write. */
13587
+ keepContinuation(cont, stamp) {
13588
+ if (cont.unusable) return;
13589
+ cont.stamp = stamp;
13590
+ cont.replay.clear();
13591
+ this.sticky.put(cont);
13592
+ }
13593
+ /**
13594
+ * The checkpoints of the steps `cont` is parked on, as a replay would see them: in one read when
13595
+ * the store batches, and without the input of a step that completed (what
13596
+ * {@link StateStore.getCheckpointsForSettle} leaves out). A step still open or failed is read
13597
+ * whole, since a re-dispatch or a retry sends its input again.
13598
+ */
13599
+ async readParked(runId, cont) {
13600
+ const fresh = /* @__PURE__ */ new Map();
13601
+ for (const seq of cont.parked.keys()) fresh.set(seq, cont.hints.get(seq) ?? null);
13602
+ cont.hints.clear();
13603
+ const seqs = [
13604
+ ...fresh
13605
+ ].flatMap(([seq, cp]) => cp ? [] : [
13606
+ seq
13607
+ ]);
13608
+ if (seqs.length === 0) return fresh;
13609
+ const lean = this.store.getCheckpointsForSettle ?? this.store.getCheckpoints;
13610
+ if (lean) {
13611
+ for (const cp of await lean.call(this.store, runId, seqs)) fresh.set(cp.seq, cp);
13612
+ } else {
13613
+ for (const seq of seqs) fresh.set(seq, await this.store.getCheckpoint(runId, seq));
13614
+ }
13615
+ if (lean === this.store.getCheckpointsForSettle) {
13616
+ for (const seq of seqs) {
13617
+ const cp = fresh.get(seq);
13618
+ if (cp && cp.status !== "completed") fresh.set(seq, await this.store.getCheckpoint(runId, seq));
13619
+ }
13620
+ }
13621
+ return fresh;
13622
+ }
13623
+ /**
12193
13624
  * The run's checkpoints as a turn replays them: {@link StateStore.listCheckpointsForReplay} when
12194
13625
  * the store has it (no input on a completed row), else {@link StateStore.listCheckpoints}.
12195
13626
  */
@@ -12229,8 +13660,8 @@ ${run.workflowVersion}`;
12229
13660
  * whole, the same way.
12230
13661
  */
12231
13662
  async settlePendingCheckpoints(settlements, settled) {
12232
- const settle = this.store.settleCheckpointsIfPending?.bind(this.store);
12233
- if (!settle) {
13663
+ const settle2 = this.store.settleCheckpointsIfPending?.bind(this.store);
13664
+ if (!settle2) {
12234
13665
  return this.savePendingCheckpoints(settlements.map((s, i) => ({
12235
13666
  ...settled[i],
12236
13667
  ...s
@@ -12238,12 +13669,12 @@ ${run.workflowVersion}`;
12238
13669
  }
12239
13670
  if (settlements.length > 1) {
12240
13671
  try {
12241
- const wrote = await settle(settlements);
13672
+ const wrote = await settle2(settlements);
12242
13673
  return settlements.map((_, i) => wrote[i] === true);
12243
13674
  } catch {
12244
13675
  }
12245
13676
  }
12246
- const one = await Promise.allSettled(settlements.map((s) => settle([
13677
+ const one = await Promise.allSettled(settlements.map((s) => settle2([
12247
13678
  s
12248
13679
  ])));
12249
13680
  return one.map((s) => s.status === "fulfilled" ? s.value[0] === true : {
@@ -12304,7 +13735,10 @@ ${run.workflowVersion}`;
12304
13735
  traceparent: this.traceparent?.(),
12305
13736
  context: this.context?.(),
12306
13737
  priority: task.priority,
12307
- attempt: task.attempt
13738
+ attempt: task.attempt,
13739
+ ...task.replyHere && this.repliesRouted && !this.draining ? {
13740
+ replyTo: this.instanceId
13741
+ } : {}
12308
13742
  }, task.transport);
12309
13743
  }
12310
13744
  /**
@@ -12396,6 +13830,9 @@ ${run.workflowVersion}`;
12396
13830
  i
12397
13831
  ];
12398
13832
  });
13833
+ if (this.store.settleResultsIfPending) {
13834
+ return this.settleResultsReturning(runId, results, live, outcomes, queuedIn);
13835
+ }
12399
13836
  const reads = await this.readCheckpoints(runId, live.map((i) => results[i].seq));
12400
13837
  const pending = [];
12401
13838
  const resettled = [];
@@ -12475,6 +13912,110 @@ ${run.workflowVersion}`;
12475
13912
  return wake.applyTo(outcomes);
12476
13913
  }
12477
13914
  /**
13915
+ * {@link completeRemoteResults} on a store with {@link StateStore.settleResultsIfPending}: the
13916
+ * results settle their `pending` rows and read them back in one call, instead of a read of the rows
13917
+ * followed by the write. Only an entry that wrote nothing (a redelivery, or a step gone) is read
13918
+ * afterwards. A settled `completed` row is also handed to the run's parked body, if this instance
13919
+ * holds one, so its next turn does not read it again.
13920
+ */
13921
+ async settleResultsReturning(runId, results, live, outcomes, queuedIn) {
13922
+ const settle2 = this.store.settleResultsIfPending.bind(this.store);
13923
+ const finishedAt = /* @__PURE__ */ new Date();
13924
+ const entries = live.map((i) => {
13925
+ const result = results[i];
13926
+ return {
13927
+ runId,
13928
+ seq: result.seq,
13929
+ status: result.status,
13930
+ output: result.status === "completed" ? result.output : void 0,
13931
+ error: result.error,
13932
+ events: result.events,
13933
+ startedAt: result.startedAt ? new Date(result.startedAt) : void 0,
13934
+ finishedAt
13935
+ };
13936
+ });
13937
+ let rows;
13938
+ try {
13939
+ rows = await settle2(entries);
13940
+ } catch (err) {
13941
+ if (entries.length === 1) {
13942
+ rows = [
13943
+ {
13944
+ error: err
13945
+ }
13946
+ ];
13947
+ } else {
13948
+ const one = await Promise.allSettled(entries.map((e) => settle2([
13949
+ e
13950
+ ])));
13951
+ rows = one.map((o) => o.status === "fulfilled" ? o.value[0] ?? null : {
13952
+ error: o.reason
13953
+ });
13954
+ }
13955
+ }
13956
+ const settled = [];
13957
+ const unsettled = [];
13958
+ rows.forEach((row, k) => {
13959
+ const index = live[k];
13960
+ if (row === null) unsettled.push(index);
13961
+ else if ("error" in row && !("seq" in row)) outcomes[index] = {
13962
+ error: row.error
13963
+ };
13964
+ else settled.push({
13965
+ index,
13966
+ cp: row,
13967
+ result: results[index]
13968
+ });
13969
+ });
13970
+ const parked = this.sticky.peek(runId) ?? this.turnContinuations.get(runId);
13971
+ for (const { cp, result } of settled) {
13972
+ void this.releaseQueueSlot(cp.stepId).catch(() => void 0);
13973
+ if (cp.status === "completed") parked?.hint(cp);
13974
+ if (!parseStepFanGroup(cp.parallelGroup)) this.noteSettledHere(`${runId}:${result.seq}`);
13975
+ this.emit({
13976
+ type: result.status === "completed" ? "step.completed" : "step.failed",
13977
+ runId,
13978
+ seq: result.seq,
13979
+ name: cp.name,
13980
+ kind: cp.kind,
13981
+ output: result.output,
13982
+ error: result.error,
13983
+ queueMs: cp.startedAt.getTime() - cp.enqueuedAt.getTime(),
13984
+ durationMs: finishedAt.getTime() - cp.startedAt.getTime()
13985
+ });
13986
+ }
13987
+ if (settled.length === 0) {
13988
+ if (unsettled.length === 0) return outcomes;
13989
+ const reads = await this.readCheckpoints(runId, unsettled.map((i) => results[i].seq));
13990
+ const resettled = [];
13991
+ reads.forEach((read, k) => {
13992
+ const index = unsettled[k];
13993
+ if (read.status === "rejected") outcomes[index] = {
13994
+ error: read.reason
13995
+ };
13996
+ else if (read.value?.status === "completed" || read.value?.status === "failed") {
13997
+ resettled.push({
13998
+ index,
13999
+ cp: read.value
14000
+ });
14001
+ } else if (read.value?.status === "pending") {
14002
+ void this.releaseQueueSlot(read.value.stepId).catch(() => void 0);
14003
+ }
14004
+ });
14005
+ if (resettled.length === 0) return outcomes;
14006
+ return this.resumeForRedelivered(runId, resettled, outcomes, queuedIn);
14007
+ }
14008
+ const barriers = /* @__PURE__ */ new Map();
14009
+ const wake = new FanWakeCollector();
14010
+ for (const { cp } of settled) wake.note(cp);
14011
+ for (const { index, cp, result } of settled) {
14012
+ if (!await this.stepFanStillRunning(cp, result, barriers, queuedIn)) {
14013
+ wake.add(index, cp, result.status);
14014
+ }
14015
+ }
14016
+ return wake.applyTo(outcomes);
14017
+ }
14018
+ /**
12478
14019
  * The outcomes for a batch of redelivered results (their steps already settled): one of them
12479
14020
  * resumes the run if it is `suspended` and, for a step of a fan, the fan's barrier is open. A
12480
14021
  * resume of a run that had in fact moved on replays to its current wait and re-parks.
@@ -13364,6 +14905,15 @@ var CodecStateStore = class {
13364
14905
  /** Encodes each settlement's payloads like {@link saveCheckpoint}. Bound only when the inner store
13365
14906
  * has it, so the engine's whole-row fallback still kicks in through the wrapper. */
13366
14907
  settleCheckpointsIfPending;
14908
+ /** Encodes each result's payloads, decodes each row read back. Bound only when the inner store
14909
+ * has it. */
14910
+ settleResultsIfPending;
14911
+ /** Encodes like {@link saveCheckpoint}. Bound only when the inner store has it. */
14912
+ insertCheckpointIfAbsent;
14913
+ /** Decodes each run claimed. Bound only when the inner store has it. */
14914
+ claimPendingRuns;
14915
+ /** Decodes the run read. Bound only when the inner store has it. */
14916
+ tryLockRunAndGet;
13367
14917
  /** Encodes each checkpoint written. Bound only when the inner store has it. */
13368
14918
  saveCheckpoints;
13369
14919
  /** Decodes each checkpoint read like {@link listCheckpoints}. Bound only when the inner store has
@@ -13399,6 +14949,14 @@ var CodecStateStore = class {
13399
14949
  listBufferedSignals;
13400
14950
  /** Forwarded verbatim — deletes by row id. */
13401
14951
  deleteBufferedSignals;
14952
+ /** Decodes the payload like {@link takeBufferedSignal}. Bound only when the inner store has it. */
14953
+ takeOldestBufferedSignal;
14954
+ /** Forwarded verbatim — a signal id record carries no payload. */
14955
+ claimSignalId;
14956
+ /** Forwarded verbatim. */
14957
+ releaseSignalId;
14958
+ /** Forwarded verbatim. */
14959
+ deleteExpiredSignalIds;
13402
14960
  /** Forwarded verbatim — a waiter carries no payload. */
13403
14961
  listSignalWaitersByRun;
13404
14962
  /** Forwarded verbatim — a waiter carries no payload. */
@@ -13427,6 +14985,14 @@ var CodecStateStore = class {
13427
14985
  if (saveManyIfPending) this.saveCheckpointsIfPending = (cps) => saveManyIfPending.call(inner, cps.map((cp) => this.encCp(cp)));
13428
14986
  const settleMany = inner.settleCheckpointsIfPending;
13429
14987
  if (settleMany) this.settleCheckpointsIfPending = (settlements) => settleMany.call(inner, settlements.map((s) => this.encSettlement(s)));
14988
+ const settleResults = inner.settleResultsIfPending;
14989
+ if (settleResults) this.settleResultsIfPending = async (results) => (await settleResults.call(inner, results.map((r) => this.encSettlement(r)))).map((cp) => this.decCp(cp));
14990
+ const insertIfAbsent = inner.insertCheckpointIfAbsent;
14991
+ if (insertIfAbsent) this.insertCheckpointIfAbsent = (cp) => insertIfAbsent.call(inner, this.encCp(cp));
14992
+ const claim = inner.claimPendingRuns;
14993
+ if (claim) this.claimPendingRuns = async (limit, owner, leaseUntilMs, nowMs, namespace) => (await claim.call(inner, limit, owner, leaseUntilMs, nowMs, namespace)).map((r) => this.decRun(r));
14994
+ const lockAndGet = inner.tryLockRunAndGet;
14995
+ if (lockAndGet) this.tryLockRunAndGet = async (runId, owner, leaseUntilMs, nowMs) => this.decRun(await lockAndGet.call(inner, runId, owner, leaseUntilMs, nowMs));
13430
14996
  const saveMany = inner.saveCheckpoints;
13431
14997
  if (saveMany) this.saveCheckpoints = (cps) => saveMany.call(inner, cps.map((cp) => this.encCp(cp)));
13432
14998
  const listForReplay = inner.listCheckpointsForReplay;
@@ -13463,6 +15029,18 @@ var CodecStateStore = class {
13463
15029
  }));
13464
15030
  const deleteBuffered = inner.deleteBufferedSignals;
13465
15031
  if (deleteBuffered) this.deleteBufferedSignals = (ids) => deleteBuffered.call(inner, ids);
15032
+ const takeOldest = inner.takeOldestBufferedSignal;
15033
+ if (takeOldest) this.takeOldestBufferedSignal = async (tokens) => {
15034
+ const taken = await takeOldest.call(inner, tokens);
15035
+ return taken ? {
15036
+ token: taken.token,
15037
+ payload: this.dec(taken.payload)
15038
+ } : null;
15039
+ };
15040
+ const { claimSignalId, releaseSignalId, deleteExpiredSignalIds } = inner;
15041
+ if (claimSignalId) this.claimSignalId = (token, id, expiresAt, nowMs) => claimSignalId.call(inner, token, id, expiresAt, nowMs);
15042
+ if (releaseSignalId) this.releaseSignalId = (token, id) => releaseSignalId.call(inner, token, id);
15043
+ if (deleteExpiredSignalIds) this.deleteExpiredSignalIds = (nowMs, limit) => deleteExpiredSignalIds.call(inner, nowMs, limit);
13466
15044
  const byRun = inner.listSignalWaitersByRun;
13467
15045
  if (byRun) this.listSignalWaitersByRun = (runIds) => byRun.call(inner, runIds);
13468
15046
  const hasWaiter = inner.hasSignalWaiter;
@@ -14247,6 +15825,87 @@ var InMemoryStateStore = class {
14247
15825
  return true;
14248
15826
  });
14249
15827
  }
15828
+ // The three round-trip-saving methods below are built on the plain ones, so a test store that
15829
+ // overrides `tryLockRun`, `getRun`, `getCheckpoint` or a settle to stage a race keeps staging it.
15830
+ // They are OFF unless asked for (`roundTripMethods`, or `DURABLE_TEST_ROUND_TRIP_METHODS=1` for a
15831
+ // whole suite): many specs pin the store calls the engine makes without them.
15832
+ settleResultsIfPending;
15833
+ insertCheckpointIfAbsent;
15834
+ tryLockRunAndGet;
15835
+ claimPendingRuns;
15836
+ constructor(opts = {}) {
15837
+ const on = opts.roundTripMethods ?? (typeof process !== "undefined" && process.env.DURABLE_TEST_ROUND_TRIP_METHODS === "1");
15838
+ if (on) {
15839
+ this.settleResultsIfPending = (results) => this.settleResultsFallback(results);
15840
+ this.insertCheckpointIfAbsent = (cp) => this.insertIfAbsentFallback(cp);
15841
+ this.tryLockRunAndGet = (runId, owner, until, now) => this.lockAndGetFallback(runId, owner, until, now);
15842
+ this.claimPendingRuns = async (limit, owner, until, now, namespace) => {
15843
+ const claimed = [];
15844
+ for (const run of await this.listPendingRuns(Number.MAX_SAFE_INTEGER, namespace)) {
15845
+ if (claimed.length >= limit) break;
15846
+ const locked = await this.lockAndGetFallback(run.id, owner, until, now);
15847
+ if (locked) claimed.push(locked);
15848
+ }
15849
+ return claimed;
15850
+ };
15851
+ }
15852
+ }
15853
+ async settleResultsFallback(results) {
15854
+ const out = results.map(() => null);
15855
+ const before = /* @__PURE__ */ new Map();
15856
+ const seqsByRun = /* @__PURE__ */ new Map();
15857
+ for (const r of results) seqsByRun.set(r.runId, [
15858
+ ...seqsByRun.get(r.runId) ?? [],
15859
+ r.seq
15860
+ ]);
15861
+ for (const [runId, seqs] of seqsByRun) {
15862
+ for (const cp of await this.getCheckpointsForSettle(runId, seqs)) {
15863
+ before.set(this.key(runId, cp.seq), cp);
15864
+ }
15865
+ }
15866
+ const claimed = /* @__PURE__ */ new Set();
15867
+ const settlements = [];
15868
+ const at = [];
15869
+ for (const [i, r] of results.entries()) {
15870
+ const key = this.key(r.runId, r.seq);
15871
+ const cp = before.get(key);
15872
+ if (cp?.status !== "pending" || claimed.has(key)) continue;
15873
+ const status = (await this.getRun(r.runId))?.status;
15874
+ if (status === "cancelled" || status === "completed") continue;
15875
+ claimed.add(key);
15876
+ settlements.push({
15877
+ runId: r.runId,
15878
+ seq: r.seq,
15879
+ status: r.status,
15880
+ output: r.status === "completed" ? r.output : cp.output,
15881
+ error: r.error,
15882
+ events: r.events ?? cp.events,
15883
+ startedAt: r.startedAt ?? cp.startedAt,
15884
+ finishedAt: r.finishedAt
15885
+ });
15886
+ at.push(i);
15887
+ }
15888
+ if (settlements.length === 0) return out;
15889
+ const wrote = await this.settleCheckpointsIfPending(settlements);
15890
+ settlements.forEach((settlement, k) => {
15891
+ if (!wrote[k]) return;
15892
+ const cp = before.get(this.key(settlement.runId, settlement.seq));
15893
+ out[at[k]] = {
15894
+ ...cp,
15895
+ ...settlement
15896
+ };
15897
+ });
15898
+ return out;
15899
+ }
15900
+ async insertIfAbsentFallback(checkpoint) {
15901
+ if (await this.getCheckpoint(checkpoint.runId, checkpoint.seq)) return false;
15902
+ await this.saveCheckpoint(checkpoint);
15903
+ return true;
15904
+ }
15905
+ async lockAndGetFallback(runId, owner, leaseUntilMs, nowMs) {
15906
+ if (!await this.tryLockRun(runId, owner, leaseUntilMs, nowMs)) return null;
15907
+ return this.getRun(runId);
15908
+ }
14250
15909
  async listOrphanedRuns(nowMs, limit, namespace, ownedBy) {
14251
15910
  return [
14252
15911
  ...this.runs.values()
@@ -14377,6 +16036,45 @@ var InMemoryStateStore = class {
14377
16036
  payload: head.payload
14378
16037
  };
14379
16038
  }
16039
+ async takeOldestBufferedSignal(tokens) {
16040
+ let oldest;
16041
+ for (const token of new Set(tokens)) {
16042
+ const head = this.bufferedSignals.get(token)?.[0];
16043
+ if (head && (!oldest || head.id < oldest.id)) oldest = {
16044
+ token,
16045
+ id: head.id
16046
+ };
16047
+ }
16048
+ if (!oldest) return null;
16049
+ const taken = await this.takeBufferedSignal(oldest.token);
16050
+ return taken ? {
16051
+ token: oldest.token,
16052
+ payload: taken.payload
16053
+ } : null;
16054
+ }
16055
+ /** `token \u0000 id -> expiresAt` of the signal ids {@link claimSignalId} recorded. */
16056
+ signalIds = /* @__PURE__ */ new Map();
16057
+ async claimSignalId(token, id, expiresAt, nowMs) {
16058
+ const key = `${token}\0${id}`;
16059
+ const existing = this.signalIds.get(key);
16060
+ if (existing !== void 0 && existing > nowMs) return false;
16061
+ this.signalIds.set(key, expiresAt);
16062
+ return true;
16063
+ }
16064
+ async releaseSignalId(token, id) {
16065
+ this.signalIds.delete(`${token}\0${id}`);
16066
+ }
16067
+ async deleteExpiredSignalIds(nowMs, limit) {
16068
+ let deleted = 0;
16069
+ for (const [key, expiresAt] of this.signalIds) {
16070
+ if (deleted >= limit) break;
16071
+ if (expiresAt <= nowMs) {
16072
+ this.signalIds.delete(key);
16073
+ deleted += 1;
16074
+ }
16075
+ }
16076
+ return deleted;
16077
+ }
14380
16078
  async listBufferedSignals(tokenPrefix, afterId, limit) {
14381
16079
  const after = afterId === void 0 ? 0 : Number(afterId);
14382
16080
  const rows = [];
@@ -14764,8 +16462,73 @@ var InMemoryRunQueue = class {
14764
16462
  }
14765
16463
  }
14766
16464
  };
16465
+
16466
+ // src/settle-results.ts
16467
+ async function settleResultsWithPlainCalls(store, results) {
16468
+ const out = results.map(() => null);
16469
+ const key = /* @__PURE__ */ __name((runId, seq) => `${seq}:${runId}`, "key");
16470
+ const seqsByRun = /* @__PURE__ */ new Map();
16471
+ for (const r of results) seqsByRun.set(r.runId, [
16472
+ ...seqsByRun.get(r.runId) ?? [],
16473
+ r.seq
16474
+ ]);
16475
+ const runIds = [
16476
+ ...seqsByRun.keys()
16477
+ ];
16478
+ const runs = store.getRuns ? await store.getRuns(runIds) : (await Promise.all(runIds.map((id) => store.getRun(id)))).filter((r) => r !== null);
16479
+ const finished = new Set(runs.filter((r) => r.status === "cancelled" || r.status === "completed").map((r) => r.id));
16480
+ const read = /* @__PURE__ */ __name(async (runId, seqs) => {
16481
+ if (store.getCheckpointsForSettle) return store.getCheckpointsForSettle(runId, seqs);
16482
+ if (store.getCheckpoints) return store.getCheckpoints(runId, seqs);
16483
+ const rows = await Promise.all(seqs.map((seq) => store.getCheckpoint(runId, seq)));
16484
+ return rows.filter((cp) => cp !== null);
16485
+ }, "read");
16486
+ const before = /* @__PURE__ */ new Map();
16487
+ for (const [runId, seqs] of seqsByRun) {
16488
+ if (finished.has(runId)) continue;
16489
+ for (const cp of await read(runId, seqs)) before.set(key(runId, cp.seq), cp);
16490
+ }
16491
+ const claimed = /* @__PURE__ */ new Set();
16492
+ const settlements = [];
16493
+ const at = [];
16494
+ for (const [i, r] of results.entries()) {
16495
+ const k = key(r.runId, r.seq);
16496
+ const cp = before.get(k);
16497
+ if (cp?.status !== "pending" || claimed.has(k)) continue;
16498
+ claimed.add(k);
16499
+ settlements.push({
16500
+ runId: r.runId,
16501
+ seq: r.seq,
16502
+ status: r.status,
16503
+ output: r.status === "completed" ? r.output : cp.output,
16504
+ error: r.error,
16505
+ events: r.events ?? cp.events,
16506
+ startedAt: r.startedAt ?? cp.startedAt,
16507
+ finishedAt: r.finishedAt
16508
+ });
16509
+ at.push(i);
16510
+ }
16511
+ if (settlements.length === 0 || !store.settleCheckpointsIfPending) return out;
16512
+ const wrote = await store.settleCheckpointsIfPending(settlements);
16513
+ const won = settlements.filter((_, j) => wrote[j] === true);
16514
+ const after = /* @__PURE__ */ new Map();
16515
+ const wonByRun = /* @__PURE__ */ new Map();
16516
+ for (const s of won) wonByRun.set(s.runId, [
16517
+ ...wonByRun.get(s.runId) ?? [],
16518
+ s.seq
16519
+ ]);
16520
+ for (const [runId, seqs] of wonByRun) {
16521
+ for (const cp of await read(runId, seqs)) after.set(key(runId, cp.seq), cp);
16522
+ }
16523
+ settlements.forEach((s, j) => {
16524
+ if (wrote[j] === true) out[at[j]] = after.get(key(s.runId, s.seq)) ?? null;
16525
+ });
16526
+ return out;
16527
+ }
16528
+ __name(settleResultsWithPlainCalls, "settleResultsWithPlainCalls");
14767
16529
  export {
14768
16530
  ACCUMULATOR_SEEN_MAX,
16531
+ ANY_WAIT_GROUP,
14769
16532
  CHILD_EDGE_PREFIXES,
14770
16533
  CONTROL_FLOW_SIGNAL,
14771
16534
  CORE_ORIGIN,
@@ -14812,6 +16575,7 @@ export {
14812
16575
  LIFECYCLE_TRIGGER_FAILED_TAG_PREFIX,
14813
16576
  NestedWorkflowCallError,
14814
16577
  NonDeterminismError,
16578
+ PARENT_CLOSE_TAG_PREFIX,
14815
16579
  QueueController,
14816
16580
  REMOTE_EVENT,
14817
16581
  RUN_HISTORY_FIXTURE_VERSION,
@@ -14851,6 +16615,7 @@ export {
14851
16615
  aggregateAnnouncements,
14852
16616
  aggregatesCheckpoints,
14853
16617
  announcementsOf,
16618
+ anyCheckpointName,
14854
16619
  applyAttributeQuery,
14855
16620
  assertConcurrency,
14856
16621
  assertRoutes,
@@ -14907,6 +16672,8 @@ export {
14907
16672
  hmacTenantVerifier,
14908
16673
  indexWaitersByRun,
14909
16674
  info,
16675
+ isAnyCheckpoint,
16676
+ isAnyMember,
14910
16677
  isEngineMintedTag,
14911
16678
  isEventToken,
14912
16679
  isFailedMember,
@@ -14936,6 +16703,7 @@ export {
14936
16703
  missingCapabilities,
14937
16704
  negotiate,
14938
16705
  nextCronFireMs,
16706
+ nextRRuleFireMs,
14939
16707
  normalizeAttributeRows,
14940
16708
  normalizeDescriptor,
14941
16709
  observedAnnouncements,
@@ -14946,11 +16714,13 @@ export {
14946
16714
  parentRunIdOf,
14947
16715
  parseDuration,
14948
16716
  parseLifecycleEvent,
16717
+ parseRRule,
14949
16718
  parseWebhookToken,
14950
16719
  partitionOfToken,
14951
16720
  planDispatch,
14952
16721
  poolNamespace,
14953
16722
  prevCronFireMs,
16723
+ prevRRuleFireMs,
14954
16724
  queryCheckpointsOf,
14955
16725
  readRunsByIds,
14956
16726
  readSearchAttributes,
@@ -14980,6 +16750,7 @@ export {
14980
16750
  sanitizeQueueToken,
14981
16751
  scheduleRunIdAt,
14982
16752
  scheduledRunId,
16753
+ settleResultsWithPlainCalls,
14983
16754
  signTenantToken,
14984
16755
  singletonAdmissionDenialPatch,
14985
16756
  singletonAdmissionTags,