@dudousxd/nestjs-durable-core 0.87.0 → 0.88.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.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;
@@ -5609,6 +6583,8 @@ var WorkflowEngine = class _WorkflowEngine {
5609
6583
  claimsMade = /* @__PURE__ */ new Map();
5610
6584
  /** Pager hooks for stranded runs (see {@link onStalled}). */
5611
6585
  stalledListeners = /* @__PURE__ */ new Set();
6586
+ /** `parentClose` policies recorded by the turn in flight, per parent run (see {@link settleParentClose}). */
6587
+ parentCloseIntents = /* @__PURE__ */ new Map();
5612
6588
  /** Runs already paged about, by id → the `updatedAt` of the paged episode (see {@link sweepStalled}). */
5613
6589
  notifiedStalled = /* @__PURE__ */ new Map();
5614
6590
  lastStalledSweepAt = Number.NEGATIVE_INFINITY;
@@ -5643,14 +6619,18 @@ var WorkflowEngine = class _WorkflowEngine {
5643
6619
  this.unregisteredWorkflowGraceMs = grace !== void 0 && Number.isFinite(grace) && grace >= 0 ? grace : 24 * 60 * 60 * 1e3;
5644
6620
  this.remoteAdvanceSilenceMs = deps.remoteAdvanceSilenceMs;
5645
6621
  this.reconcileMs = deps.reconcileMs === void 0 ? 3e5 : deps.reconcileMs || void 0;
6622
+ this.sticky = new StickyCache(Math.max(0, deps.stickyRuns ?? DEFAULT_STICKY_RUNS), deps.stickyIdleMs ?? DEFAULT_STICKY_IDLE_MS);
6623
+ this.routeReplies = deps.routeReplies ?? true;
5646
6624
  const stalledAfter = deps.stalledAfterMs;
5647
6625
  this.stalledAfterMs = stalledAfter !== void 0 && Number.isFinite(stalledAfter) ? Math.max(6e4, stalledAfter) : DEFAULT_STALLED_AFTER_MS;
5648
6626
  const batch = deps.timerBatchSize;
5649
6627
  this.timerBatchSize = batch !== void 0 && Number.isFinite(batch) && batch >= 1 ? Math.floor(batch) : 500;
5650
6628
  this.eventBufferTtlMs = deps.eventBufferTtlMs || void 0;
6629
+ this.signalIdTtlMs = deps.signalIdTtl != null ? parseDuration(deps.signalIdTtl) : DEFAULT_SIGNAL_ID_TTL_MS;
5651
6630
  this.remoteRedispatchMs = deps.remoteRedispatchMs || void 0;
5652
6631
  this.remoteRedispatchMax = Math.max(1, deps.remoteRedispatchMax ?? 10);
5653
6632
  this.blockedPollMs = deps.blockedPollMs ?? 5e3;
6633
+ this.workerDescriptorCacheMs = deps.workerDescriptorCacheMs ?? 2e3;
5654
6634
  this.cpDescriptor = controlPlaneDescriptor({
5655
6635
  instanceId: `cp-${this.instanceId}`,
5656
6636
  ...this.namespace !== void 0 ? {
@@ -5748,6 +6728,9 @@ var WorkflowEngine = class _WorkflowEngine {
5748
6728
  await this.completeRemoteDecision(decision);
5749
6729
  }
5750
6730
  );
6731
+ if (this.routeReplies && this.sticky.enabled) {
6732
+ this.repliesRouted = this.pool.routeReplies(this.instanceId);
6733
+ }
5751
6734
  const verifyTenant = deps.verifyTenant;
5752
6735
  this.pool.onStartRun(async (message) => {
5753
6736
  let tenant;
@@ -6324,6 +7307,23 @@ var WorkflowEngine = class _WorkflowEngine {
6324
7307
  return deleted;
6325
7308
  }
6326
7309
  /**
7310
+ * Delete the signal-id records (`signal(..., { id })`) that expired by `now`, in batches of
7311
+ * {@link SIGNAL_ID_SWEEP_BATCH}, at most `maxBatches` per call. Expired records already stop
7312
+ * deduplicating; this reclaims their rows. The NestJS timer poller calls it every tick. Returns how
7313
+ * many it deleted; 0 on a store without `deleteExpiredSignalIds`.
7314
+ */
7315
+ async sweepExpiredSignalIds(now = this.clock(), maxBatches = 4) {
7316
+ const { deleteExpiredSignalIds } = this.store;
7317
+ if (!deleteExpiredSignalIds || this.draining) return 0;
7318
+ let deleted = 0;
7319
+ for (let batch = 0; batch < maxBatches; batch++) {
7320
+ const n = await deleteExpiredSignalIds.call(this.store, now, SIGNAL_ID_SWEEP_BATCH);
7321
+ deleted += n;
7322
+ if (n < SIGNAL_ID_SWEEP_BATCH) break;
7323
+ }
7324
+ return deleted;
7325
+ }
7326
+ /**
6327
7327
  * Register a pager hook for STRANDED runs: an in-flight run untouched past `stalledAfterMs` that is
6328
7328
  * either suspended with NO wake timer (wake-forever: nothing will ever re-drive it) or parked on an
6329
7329
  * old pending remote step with no live lease (dispatched and, most likely, lost). The timer poller
@@ -6401,6 +7401,7 @@ var WorkflowEngine = class _WorkflowEngine {
6401
7401
  ...event,
6402
7402
  at: /* @__PURE__ */ new Date()
6403
7403
  };
7404
+ this.settleParentClose(full);
6404
7405
  this.deliver(full);
6405
7406
  if (opts?.lifecycle !== false) {
6406
7407
  this.publishLifecycle(full);
@@ -7950,8 +8951,31 @@ var WorkflowEngine = class _WorkflowEngine {
7950
8951
  return true;
7951
8952
  }
7952
8953
  async resumeRunNow(runId, allowFailed) {
7953
- const run = await this.store.getRun(runId);
8954
+ const claimed = this.claimedRuns.get(runId);
8955
+ if (claimed) {
8956
+ this.claimedRuns.delete(runId);
8957
+ return this.resumeLoadedRun(claimed, allowFailed);
8958
+ }
8959
+ const lockAndGet = this.executionLeases.has(runId) ? void 0 : this.store.tryLockRunAndGet;
8960
+ let locked = false;
8961
+ let run = null;
8962
+ if (lockAndGet) {
8963
+ const nowMs = this.clock();
8964
+ run = await lockAndGet.call(this.store, runId, this.instanceId, nowMs + this.leaseMs, nowMs);
8965
+ locked = run !== null;
8966
+ }
8967
+ run ??= await this.store.getRun(runId);
7954
8968
  if (!run) throw new Error(`run ${runId} not found`);
8969
+ if (!locked) return this.resumeLoadedRun(run, allowFailed);
8970
+ this.lockedForTurn.add(runId);
8971
+ try {
8972
+ return await this.resumeLoadedRun(run, allowFailed);
8973
+ } finally {
8974
+ if (this.lockedForTurn.delete(runId)) await this.releaseLeaseBestEffort(runId);
8975
+ }
8976
+ }
8977
+ async resumeLoadedRun(run, allowFailed) {
8978
+ const runId = run.id;
7955
8979
  if (run.status === "cancelled" || run.status === "completed" || run.status === "dead" || run.status === "failed" && !allowFailed) {
7956
8980
  return {
7957
8981
  runId,
@@ -8133,6 +9157,8 @@ var WorkflowEngine = class _WorkflowEngine {
8133
9157
  */
8134
9158
  beginDrain(spentMs = 0) {
8135
9159
  this.draining = true;
9160
+ this.sticky.clear();
9161
+ this.pool.stopReplies();
8136
9162
  if (this.runConsumerStops.length > 0) {
8137
9163
  this.runBatcher.flush();
8138
9164
  void this.stopRunConsumers();
@@ -8848,7 +9874,13 @@ var WorkflowEngine = class _WorkflowEngine {
8848
9874
  * {@link resumeDueTimers}.
8849
9875
  */
8850
9876
  async runPending(nowMs = this.clock()) {
8851
- const pending = await this.store.listPendingRuns(100, this.namespace);
9877
+ if (this.store.claimPendingRuns && !this.queuesRunsOverTransport && !this.draining) {
9878
+ const room = Math.min(PENDING_POLL_BATCH, LOCAL_RUN_CONCURRENCY - this.inflight.size);
9879
+ if (room <= 0) return [];
9880
+ const claimed = await this.store.claimPendingRuns(room, this.instanceId, nowMs + this.leaseMs, nowMs, this.namespace);
9881
+ return this.runClaimed(claimed);
9882
+ }
9883
+ const pending = await this.store.listPendingRuns(PENDING_POLL_BATCH, this.namespace);
8852
9884
  if (this.queuesRunsOverTransport && !this.draining && pending.length > 0) {
8853
9885
  if (await this.queueOnTransport(pending)) {
8854
9886
  return pending.map((run) => ({
@@ -8938,6 +9970,34 @@ var WorkflowEngine = class _WorkflowEngine {
8938
9970
  * for a pod that does have the workflow — which is exactly what the skew-protection error asks
8939
9971
  * for. Same posture as `publishEvent`'s per-subscriber isolation.
8940
9972
  */
9973
+ /**
9974
+ * Run `claimed` (pending runs this instance just leased, read as locked): every turn starts at
9975
+ * once, and each re-enters the lease taken for it without reading the run again.
9976
+ */
9977
+ async runClaimed(claimed) {
9978
+ const turns = claimed.map(async (run) => {
9979
+ if (this.draining) {
9980
+ await this.releaseLeaseBestEffort(run.id);
9981
+ return void 0;
9982
+ }
9983
+ this.lockedForTurn.add(run.id);
9984
+ this.claimedRuns.set(run.id, run);
9985
+ try {
9986
+ return await this.resumeImplicit(run.id);
9987
+ } catch (error2) {
9988
+ if (error2 instanceof WorkflowNotRegistered) {
9989
+ const settled = await this.settleUnregistered(run, this.clock()).catch(() => void 0);
9990
+ if (settled) return settled;
9991
+ }
9992
+ console.error(`[nestjs-durable] could not resume run ${run.id} (${run.workflow}@${run.workflowVersion}); leaving it for another instance:`, error2);
9993
+ return void 0;
9994
+ } finally {
9995
+ this.lockedForTurn.delete(run.id);
9996
+ this.claimedRuns.delete(run.id);
9997
+ }
9998
+ });
9999
+ return (await Promise.all(turns)).filter((r) => r !== void 0);
10000
+ }
8941
10001
  async resumeLeased(runs, nowMs = this.clock(), onLocked) {
8942
10002
  if (this.draining) return [];
8943
10003
  const results = [];
@@ -9037,7 +10097,8 @@ var WorkflowEngine = class _WorkflowEngine {
9037
10097
  * due-timer reconcile sweep ({@link reconcileSignalWaiter}) — all three resolve a waiter the exact
9038
10098
  * same way.
9039
10099
  */
9040
- async deliverSignal(waiter, payload, resumeHere = false) {
10100
+ async deliverSignal(waiter, payload, resumeHere = false, committed) {
10101
+ if (isAnyMember(waiter)) return this.deliverToWaitAny(waiter, payload, resumeHere, committed);
9041
10102
  await this.store.saveCheckpoint(instantCheckpoint({
9042
10103
  runId: waiter.runId,
9043
10104
  seq: waiter.seq,
@@ -9050,6 +10111,7 @@ var WorkflowEngine = class _WorkflowEngine {
9050
10111
  // for an ordinary (non-fan) signal/child await.
9051
10112
  parallelGroup: waiter.parallelGroup
9052
10113
  }));
10114
+ committed?.();
9053
10115
  if (await this.fanStillWaiting(waiter, payload)) {
9054
10116
  return {
9055
10117
  runId: waiter.runId,
@@ -9062,6 +10124,47 @@ var WorkflowEngine = class _WorkflowEngine {
9062
10124
  });
9063
10125
  }
9064
10126
  /**
10127
+ * Deliver to a waiter that is one condition of a `ctx.waitForAny` (see `wait-any.ts`): settle the
10128
+ * wait with this condition through the checkpoint's compare-and-set, then wake the run. A signal
10129
+ * that loses — the wait was settled by another condition first, or a timer of the wait came due
10130
+ * before it arrived (the timer then wins) — is not consumed: it is sent again on its token, where
10131
+ * it is buffered for the next wait (or reaches a waiter registered since).
10132
+ */
10133
+ async deliverToWaitAny(waiter, payload, resumeHere, committed) {
10134
+ const cp = await this.store.getCheckpoint(waiter.runId, waiter.seq);
10135
+ const conditions = cp && cp.status === "pending" && isAnyCheckpoint(cp) ? anyConditionsOf(cp) : [];
10136
+ const condition = conditions.find((c) => anyConditionToken(c) === waiter.token);
10137
+ const run = cp && condition ? await this.store.getRun(waiter.runId) : null;
10138
+ if (!cp || !condition || !run || TERMINAL_RUN_STATUSES.includes(run.status)) {
10139
+ if (cp && condition) await removeAnyWaiters(this.store, cp);
10140
+ await this.redeliverLosingSignal(waiter.token, payload);
10141
+ committed?.();
10142
+ return null;
10143
+ }
10144
+ const due = dueTimer(conditions, this.clock());
10145
+ const done = due ? resolvedAnyCheckpoint(cp, due, null) : resolvedAnyCheckpoint(cp, condition, payload);
10146
+ const won = await claimAnyCheckpoint(this.store, done);
10147
+ if (won) await removeAnyWaiters(this.store, done);
10148
+ if (!won || due) await this.redeliverLosingSignal(waiter.token, payload);
10149
+ committed?.();
10150
+ if (!won) return null;
10151
+ return (resumeHere ? this.resumeImplicit(waiter.runId) : this.wake(waiter.runId)).catch((err) => {
10152
+ if (err instanceof NamespaceMismatch) return null;
10153
+ throw err;
10154
+ });
10155
+ }
10156
+ /**
10157
+ * Put back a signal a `waitForAny` condition took but did not win with. A child's completion is
10158
+ * kept only where a parent can still consume it, as {@link deliverToParent} decides.
10159
+ */
10160
+ async redeliverLosingSignal(token, payload) {
10161
+ if (token.startsWith(CHILD_TOKEN_PREFIX)) {
10162
+ const childId = token.slice(CHILD_TOKEN_PREFIX.length);
10163
+ if (!await childCompletionNeedsBuffer(this.store, childId, payload)) return;
10164
+ }
10165
+ await this.deliverOrBuffer(token, payload);
10166
+ }
10167
+ /**
9065
10168
  * The `ctx.all` join barrier: true when `waiter` was one child of a fan (or of one wave of it)
9066
10169
  * whose siblings are still out, so waking the parent now would only replay the fan and re-suspend.
9067
10170
  * Skipping that wake is what keeps a fan of N children at O(N) work instead of N replays of N items.
@@ -9194,7 +10297,7 @@ var WorkflowEngine = class _WorkflowEngine {
9194
10297
  return;
9195
10298
  }
9196
10299
  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) => ({
10300
+ const awaited = waiters.filter((w) => w.token.startsWith("child:") && !settled.has(w.seq) && !isAnyMember(w)).map((w) => ({
9198
10301
  childId: w.token.slice("child:".length),
9199
10302
  seq: w.seq,
9200
10303
  parallelGroup: w.parallelGroup
@@ -9285,10 +10388,51 @@ var WorkflowEngine = class _WorkflowEngine {
9285
10388
  * Either way, the destructive `take*` ops (on the waiter row, on the buffer) arbitrate: whichever
9286
10389
  * side actually removes the row is the one that delivers, so a signal is never delivered twice.
9287
10390
  */
9288
- async signal(token, payload) {
10391
+ async signal(token, payload, opts) {
10392
+ const id = opts?.id;
10393
+ if (id === void 0) return this.deliverOrBuffer(token, payload);
10394
+ if (!this.store.claimSignalId) {
10395
+ throw new Error("engine.signal(..., { id }) needs a store that deduplicates signals (claimSignalId); the bundled stores do");
10396
+ }
10397
+ const key = `${token}\0${id}`;
10398
+ for (let prior = this.signalIdSends.get(key); prior; prior = this.signalIdSends.get(key)) {
10399
+ const sent = await prior.then(() => true, () => false);
10400
+ if (sent) return null;
10401
+ }
10402
+ const send = this.signalOnce(token, payload, id, opts?.idTtl);
10403
+ this.signalIdSends.set(key, send);
10404
+ try {
10405
+ return await send;
10406
+ } finally {
10407
+ if (this.signalIdSends.get(key) === send) this.signalIdSends.delete(key);
10408
+ }
10409
+ }
10410
+ /** One deduplicated {@link signal}: claim `id`, then deliver or buffer; a failure forgets the id. */
10411
+ async signalOnce(token, payload, id, idTtl) {
10412
+ const claimSignalId = this.store.claimSignalId;
10413
+ if (!claimSignalId) return null;
10414
+ const now = this.clock();
10415
+ const ttl = idTtl !== void 0 ? parseDuration(idTtl) : this.signalIdTtlMs;
10416
+ if (!await claimSignalId.call(this.store, token, id, now + ttl, now)) return null;
10417
+ let committed = false;
10418
+ try {
10419
+ return await this.deliverOrBuffer(token, payload, () => {
10420
+ committed = true;
10421
+ });
10422
+ } catch (err) {
10423
+ if (!committed) await this.store.releaseSignalId?.(token, id).catch(() => void 0);
10424
+ throw err;
10425
+ }
10426
+ }
10427
+ /**
10428
+ * {@link signal} without deduplication: deliver to the waiter on `token`, or buffer. `committed`
10429
+ * is called once the signal is durably delivered or buffered.
10430
+ */
10431
+ async deliverOrBuffer(token, payload, committed) {
9289
10432
  const waiter = await this.store.takeSignalWaiter(token);
9290
- if (waiter) return this.deliverSignal(waiter, payload);
10433
+ if (waiter) return this.deliverSignal(waiter, payload, false, committed);
9291
10434
  await this.store.bufferSignal(token, payload);
10435
+ committed?.();
9292
10436
  const lateWaiter = await this.store.takeSignalWaiter(token);
9293
10437
  if (!lateWaiter) return null;
9294
10438
  const reclaimed = await this.store.takeBufferedSignal(token);
@@ -9650,10 +10794,32 @@ var WorkflowEngine = class _WorkflowEngine {
9650
10794
  */
9651
10795
  async cancelChildren(parentRunId, opts) {
9652
10796
  for (const id of await this.getRunChildren(parentRunId)) {
10797
+ const child = await this.store.getRun(id).catch(() => null);
10798
+ if (child?.tags?.includes(`${PARENT_CLOSE_TAG_PREFIX}abandon`)) continue;
9653
10799
  await this.cancel(id, opts).catch(() => void 0);
9654
10800
  }
9655
10801
  }
9656
10802
  /**
10803
+ * Apply the `parentClose` policies a parent's turn recorded (see {@link ChildCallOptions.parentClose})
10804
+ * when that parent completes or fails; forget them when it suspends (its next turn, here or on
10805
+ * another instance, records them again). Local events only: this is the instance that ran the turn.
10806
+ */
10807
+ settleParentClose(event) {
10808
+ if (event.type !== "run.completed" && event.type !== "run.failed" && event.type !== "run.suspended") {
10809
+ return;
10810
+ }
10811
+ const intents = this.parentCloseIntents.get(event.runId);
10812
+ if (!intents) return;
10813
+ this.parentCloseIntents.delete(event.runId);
10814
+ if (event.type === "run.suspended") return;
10815
+ for (const [childId, policy] of intents) {
10816
+ if (policy === "abandon") continue;
10817
+ queueMicrotask(() => void this.cancel(childId, {
10818
+ compensate: policy === "cancel"
10819
+ }).catch(() => void 0));
10820
+ }
10821
+ }
10822
+ /**
9657
10823
  * The ids of the runs a run spawned — both awaited (`ctx.child`) and fire-and-forget
9658
10824
  * (`ctx.startChild`, found via its `spawn:<id>` checkpoint). The canonical parent→children edge,
9659
10825
  * used for both cancellation cascades and the dashboard run-tree.
@@ -9670,7 +10836,7 @@ var WorkflowEngine = class _WorkflowEngine {
9670
10836
  async awaitedChildIds(parentRunId) {
9671
10837
  return (await listSignalWaitersOfRuns(this.store, [
9672
10838
  parentRunId
9673
- ])).filter((w) => w.token.startsWith("child:")).map((w) => w.token.slice("child:".length));
10839
+ ])).filter((w) => w.token.startsWith("child:") && !isAnyMember(w)).map((w) => w.token.slice("child:".length));
9674
10840
  }
9675
10841
  async getRunChildren(parentRunId) {
9676
10842
  const childIds = new Set(await this.awaitedChildIds(parentRunId));
@@ -9958,15 +11124,19 @@ var WorkflowEngine = class _WorkflowEngine {
9958
11124
  void this.announcedSubscriptions();
9959
11125
  const tags = singleton ? withSingletonTracked(run.tags) : run.tags;
9960
11126
  const retag = tags !== run.tags;
9961
- await this.store.updateRun(run.id, {
11127
+ const started = await (this.store.updateRunIf ?? this.updateRunIfFallback).call(this.store, run.id, [
11128
+ "pending"
11129
+ ], {
9962
11130
  status: "running",
9963
11131
  ...retag ? {
9964
11132
  tags
9965
11133
  } : {},
9966
11134
  updatedAt: /* @__PURE__ */ new Date()
9967
11135
  });
11136
+ if (!started) return false;
9968
11137
  run.status = "running";
9969
11138
  if (retag) run.tags = tags;
11139
+ return true;
9970
11140
  }
9971
11141
  async singletonWaitResult(run) {
9972
11142
  const current = await this.store.getRun(run.id);
@@ -10036,7 +11206,7 @@ var WorkflowEngine = class _WorkflowEngine {
10036
11206
  if (result.status === "suspended") {
10037
11207
  this.inBackground(this.honorWakeRequests(run.id, settledFans, served));
10038
11208
  } else if (TERMINAL_RUN_STATUSES.includes(result.status)) {
10039
- this.inBackground(this.listWakeRequests(run.id, true).then((kept) => this.dropWakeRequests(kept)));
11209
+ this.inBackground(this.listWakeRequests(run.id).then((kept) => this.dropWakeRequests(kept)));
10040
11210
  }
10041
11211
  return result;
10042
11212
  } finally {
@@ -10087,7 +11257,7 @@ var WorkflowEngine = class _WorkflowEngine {
10087
11257
  * the timer poll of another instance runs it once this instance's hold on it would have lapsed.
10088
11258
  */
10089
11259
  async honorWakeRequests(runId, settledFans, served) {
10090
- const kept = await this.listWakeRequests(runId, true);
11260
+ const kept = await this.listWakeRequests(runId);
10091
11261
  const seen = /* @__PURE__ */ __name((request) => wakeSeen(request, settledFans, served), "seen");
10092
11262
  const needed = kept.filter((request) => !seen(request));
10093
11263
  await this.dropWakeRequests(kept.filter(seen));
@@ -10097,30 +11267,17 @@ var WorkflowEngine = class _WorkflowEngine {
10097
11267
  if (!(err instanceof NamespaceMismatch)) throw err;
10098
11268
  });
10099
11269
  }
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) => ({
11270
+ /** The wake requests kept for `runId`. */
11271
+ async listWakeRequests(runId) {
11272
+ const kept = await this.store.listBufferedEvents(wakeRequestToken(runId), WAKE_REQUEST_SCAN);
11273
+ return kept.map((request) => ({
10107
11274
  id: request.id,
10108
11275
  payload: request.payload
10109
11276
  }));
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
11277
  }
10119
- /** Delete `requests` (the ones kept as buffered events). */
11278
+ /** Delete `requests`. */
10120
11279
  async dropWakeRequests(requests) {
10121
- await Promise.all(requests.flatMap((request) => request.id === void 0 ? [] : [
10122
- this.store.removeBufferedEvent(request.id)
10123
- ]));
11280
+ await Promise.all(requests.map((request) => this.store.removeBufferedEvent(request.id)));
10124
11281
  }
10125
11282
  /**
10126
11283
  * Record, for the turn executing `runId` here, the wakes kept for the run so far. Called before the
@@ -10526,12 +11683,15 @@ ${run.workflowVersion}`;
10526
11683
  };
10527
11684
  }
10528
11685
  if (outcome.kind === "blocked") return this.parkBlocked(run, outcome.blocked);
11686
+ const stamp = new Date(Math.max(updatedAt.getTime(), (run.updatedAt?.getTime() ?? 0) + 1));
10529
11687
  const applied = await (this.store.updateRunIf ?? this.updateRunIfFallback).call(this.store, run.id, SUSPEND_ELIGIBLE_STATUSES, await this.withRunTrace(run, {
10530
11688
  status: "suspended",
10531
11689
  wakeAt: this.reconcileWakeAt(outcome.wakeAt),
10532
- updatedAt
11690
+ updatedAt: stamp
10533
11691
  }));
10534
11692
  if (!applied) return this.echoCurrentStatus(run.id);
11693
+ run.updatedAt = stamp;
11694
+ this.suspendStamps.set(run.id, stamp.getTime());
10535
11695
  await this.keepDueForKeptWakes(run.id);
10536
11696
  this.emit({
10537
11697
  type: "run.suspended",
@@ -10591,7 +11751,10 @@ ${run.workflowVersion}`;
10591
11751
  }
10592
11752
  const remote = registered.remote;
10593
11753
  if (run.status === "pending") {
10594
- await this.markStarted(run, registered?.singleton !== void 0);
11754
+ if (!await this.markStarted(run, registered?.singleton !== void 0)) {
11755
+ await this.releaseLeaseBestEffort(run.id);
11756
+ return await this.echoCurrentStatus(run.id);
11757
+ }
10595
11758
  this.emit({
10596
11759
  type: "run.started",
10597
11760
  runId: run.id,
@@ -10603,7 +11766,9 @@ ${run.workflowVersion}`;
10603
11766
  if (registered.singleton && !await this.singletons.admit(run, registered.singleton)) {
10604
11767
  return this.singletonWaitResult(run);
10605
11768
  }
10606
- const wfPlan = await this.planRoute(tenantGroup(sanitizeQueueToken(remote.group), void 0), registered.requires);
11769
+ const wfToken = tenantGroup(sanitizeQueueToken(remote.group), void 0);
11770
+ let wfPlan = await this.planRoute(wfToken, registered.requires);
11771
+ if (wfPlan?.status === "blocked") wfPlan = await this.planRoute(wfToken, registered.requires, true);
10607
11772
  if (wfPlan?.status === "blocked") return this.parkBlocked(run, wfPlan);
10608
11773
  const checkpoints = await this.replayCheckpoints(run.id);
10609
11774
  await this.deliverFinishedChildren(run.id, checkpoints);
@@ -10732,6 +11897,7 @@ ${run.workflowVersion}`;
10732
11897
  kind: "suspended",
10733
11898
  wakeAt
10734
11899
  });
11900
+ this.suspendStamps.delete(run.id);
10735
11901
  await this.redriveIfTurnSawStaleHistory(run.id, decision.commands);
10736
11902
  return suspended;
10737
11903
  }
@@ -10985,6 +12151,10 @@ ${run.workflowVersion}`;
10985
12151
  for (const cp of checkpoints) {
10986
12152
  if (cp.status === "completed" || cp.status === "failed") {
10987
12153
  if (cp.seq >= HANDOFF_SEQ_BASE) continue;
12154
+ if (isAnyCheckpoint(cp)) {
12155
+ events.push(anyHistoryEvent(cp));
12156
+ continue;
12157
+ }
10988
12158
  if (cp.kind === "signal" && cp.name.startsWith("signal:child:")) {
10989
12159
  const completion = cp.output;
10990
12160
  events.push({
@@ -11152,6 +12322,16 @@ ${run.workflowVersion}`;
11152
12322
  await deliverBuffered(lateBuffered.payload);
11153
12323
  }
11154
12324
  }
12325
+ } else if (cmd.kind === "waitAny") {
12326
+ const state = await enterWaitAny({
12327
+ store: this.store,
12328
+ clock: this.clock
12329
+ }, run.id, cmd.seq, cmd.name, cmd.conditions);
12330
+ if (state.state === "resolved") {
12331
+ setTimeout(() => void this.resumeImplicit(run.id).catch(() => void 0), 0);
12332
+ } else if (state.wakeAt != null) {
12333
+ wakeAt = wakeAt == null ? state.wakeAt : Math.min(wakeAt, state.wakeAt);
12334
+ }
11155
12335
  } else if (cmd.kind === "startChild") {
11156
12336
  const childId = `${run.id}.child.${cmd.seq}`;
11157
12337
  await this.store.putSignalWaiter({
@@ -11250,7 +12430,10 @@ ${run.workflowVersion}`;
11250
12430
  async runExecutionTurn(run, fn, registered) {
11251
12431
  if (run.status === "cancelling") this.cancelRequested.add(run.id);
11252
12432
  if (run.status === "pending") {
11253
- await this.markStarted(run, registered?.singleton !== void 0);
12433
+ if (!await this.markStarted(run, registered?.singleton !== void 0)) {
12434
+ await this.releaseLeaseBestEffort(run.id);
12435
+ return await this.echoCurrentStatus(run.id);
12436
+ }
11254
12437
  this.emit({
11255
12438
  type: "run.started",
11256
12439
  runId: run.id,
@@ -11263,24 +12446,52 @@ ${run.workflowVersion}`;
11263
12446
  if (run.status !== "cancelling" && registered?.singleton && !await this.singletons.admit(run, registered.singleton)) {
11264
12447
  return this.singletonWaitResult(run);
11265
12448
  }
11266
- const compensations = [];
11267
- const snapshot = await this.replayCheckpoints(run.id);
11268
- await this.deliverFinishedChildren(run.id, snapshot);
11269
- const replay = /* @__PURE__ */ new Map();
12449
+ const stickyOk = this.stickyEligible(run);
12450
+ let cont = stickyOk ? this.takeContinuation(run, fn) : void 0;
12451
+ const warm = cont !== void 0;
12452
+ const parkedFresh = cont ? await this.readParked(run.id, cont) : void 0;
12453
+ const compensations = cont ? cont.compensations : [];
12454
+ const snapshot = cont ? [] : await this.replayCheckpoints(run.id);
12455
+ if (!cont) await this.deliverFinishedChildren(run.id, snapshot);
12456
+ const replay = cont ? cont.replay : /* @__PURE__ */ new Map();
11270
12457
  for (const cp of snapshot) replay.set(cp.seq, cp);
12458
+ if (!cont && stickyOk) {
12459
+ cont = new StickyContinuation(run.id, fn, run.workflowVersion, compensations, replay, (c) => this.sticky.evict(c.runId));
12460
+ }
12461
+ if (cont) this.turnContinuations.set(run.id, cont);
12462
+ let parkedTurn = false;
11271
12463
  let suspendedOnFan;
11272
12464
  let suspendedOnSteps = false;
11273
12465
  const awaitedSteps = /* @__PURE__ */ new Set();
11274
12466
  this.awaitedRemoteSteps.set(run.id, awaitedSteps);
11275
12467
  const awaitedFans = [];
11276
12468
  this.awaitedStepFans.set(run.id, awaitedFans);
11277
- const ctx = createWorkflowCtx(this.ctxHostFor(replay, run.namespace), run.id, compensations, run.workflow, registered?.searchAttributesSchema);
12469
+ const ctx = warm ? void 0 : createWorkflowCtx(this.ctxHostFor(replay, run.namespace, cont), run.id, compensations, run.workflow, registered?.searchAttributesSchema);
11278
12470
  try {
11279
12471
  const handoff = liveHandoff(run.id, snapshot);
11280
12472
  if (handoff && !this.cancelRequested.has(run.id)) {
11281
12473
  return await this.settleRun(run, await this.followHandoff(run, handoff));
11282
12474
  }
11283
- const output = await runInWorkflowCtx(ctx, () => fn(ctx, run.input));
12475
+ let output;
12476
+ if (cont) {
12477
+ if (parkedFresh) {
12478
+ cont.attached = true;
12479
+ await cont.resumeParked(parkedFresh);
12480
+ } else {
12481
+ const body = ctx;
12482
+ cont.begin(() => runInWorkflowCtx(body, () => fn(body, run.input)));
12483
+ }
12484
+ const outcome = await cont.next();
12485
+ if (outcome.kind === "parked") {
12486
+ parkedTurn = true;
12487
+ throw new WorkflowSuspended(outcome.wakeAt);
12488
+ }
12489
+ if (outcome.kind === "threw") throw outcome.error;
12490
+ output = outcome.value;
12491
+ } else {
12492
+ const body = ctx;
12493
+ output = await runInWorkflowCtx(body, () => fn(body, run.input));
12494
+ }
11284
12495
  if (this.cancelRequested.has(run.id)) {
11285
12496
  return await this.compensateAndCancel(run, compensations, replay);
11286
12497
  }
@@ -11338,6 +12549,11 @@ ${run.workflowVersion}`;
11338
12549
  });
11339
12550
  suspendedOnFan = err.fan;
11340
12551
  suspendedOnSteps = suspended.status === "suspended";
12552
+ const stamp = this.suspendStamps.get(run.id);
12553
+ this.suspendStamps.delete(run.id);
12554
+ if (parkedTurn && cont && suspendedOnSteps && stamp !== void 0) {
12555
+ this.keepContinuation(cont, stamp);
12556
+ }
11341
12557
  return suspended;
11342
12558
  }
11343
12559
  if (this.cancelRequested.has(run.id)) {
@@ -11362,6 +12578,8 @@ ${run.workflowVersion}`;
11362
12578
  this.awaitedRemoteSteps.delete(run.id);
11363
12579
  }
11364
12580
  if (this.awaitedStepFans.get(run.id) === awaitedFans) this.awaitedStepFans.delete(run.id);
12581
+ if (cont && this.turnContinuations.get(run.id) === cont) this.turnContinuations.delete(run.id);
12582
+ this.suspendStamps.delete(run.id);
11365
12583
  await this.releaseLeaseBestEffort(run.id);
11366
12584
  if (suspendedOnFan) {
11367
12585
  this.inBackground(this.redriveSettledChildren(run.id, suspendedOnFan));
@@ -11579,10 +12797,11 @@ ${run.workflowVersion}`;
11579
12797
  /** The seam handed to {@link createWorkflowCtx}: the authoring API reaches durability + lifecycle
11580
12798
  * (checkpointing, dispatch, child start) through this, so the ctx primitives live in their own
11581
12799
  * module and the engine stays the orchestrator. */
11582
- ctxHostFor(replay, parentNamespace) {
12800
+ ctxHostFor(replay, parentNamespace, sticky) {
11583
12801
  return {
11584
12802
  store: this.store,
11585
12803
  replay,
12804
+ sticky,
11586
12805
  clock: this.clock,
11587
12806
  webhookUrl: this.webhookUrl,
11588
12807
  startStep: /* @__PURE__ */ __name((s) => this.startStep(s), "startStep"),
@@ -11591,9 +12810,27 @@ ${run.workflowVersion}`;
11591
12810
  // Route the dispatch by the RUN's tenant (same reason as `startChild` below): the executing
11592
12811
  // engine may be an operator driving every namespace, so the token must come from the run, not
11593
12812
  // 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"),
12813
+ callRemote: /* @__PURE__ */ __name((runId, seq, step, input, queue, transport, admission) => {
12814
+ const routing = {
12815
+ namespace: parentNamespace
12816
+ };
12817
+ const call = this.callRemote(runId, seq, step, input, queue, transport, replay, admission, {
12818
+ namespace: parentNamespace
12819
+ });
12820
+ if (!sticky) return call;
12821
+ return call.catch((err) => {
12822
+ if (!(err instanceof WorkflowSuspended) || !sticky.attached) throw err;
12823
+ return sticky.park(seq, err.wakeAt, (fresh) => this.callRemote(runId, seq, step, input, queue, transport, fresh ? /* @__PURE__ */ new Map([
12824
+ [
12825
+ seq,
12826
+ fresh
12827
+ ]
12828
+ ]) : void 0, admission, {
12829
+ ...routing,
12830
+ fresh: true
12831
+ }));
12832
+ });
12833
+ }, "callRemote"),
11597
12834
  callRemoteAll: /* @__PURE__ */ __name((runId, positions, step, inputs, opts) => this.callRemoteAll(runId, positions, step, inputs, opts, {
11598
12835
  namespace: parentNamespace
11599
12836
  }, replay), "callRemoteAll"),
@@ -11603,15 +12840,29 @@ ${run.workflowVersion}`;
11603
12840
  // namespace) is the one executing the parent. Otherwise an operator recovery-resuming a
11604
12841
  // `davi-local` pipeline would stamp its `processing` child `default` and it would leak off the
11605
12842
  // tenant's worker pool onto the shared/dev workers.
11606
- startChild: /* @__PURE__ */ __name((workflow, input, id, priority, version) => {
12843
+ startChild: /* @__PURE__ */ __name((workflow, input, id, priority, version, parentClose) => {
11607
12844
  this.startChildDeferred(workflow, input, id, {
11608
12845
  priority,
11609
12846
  namespace: parentNamespace,
11610
12847
  // Absent unless the body pinned one (`ctx.child(ref, input, { version })`) — an omitted
11611
12848
  // pin must stay omitted, so the child keeps resolving `latest` exactly as before.
11612
- version
12849
+ version,
12850
+ // The policy travels with the child, so a cancel cascade run by ANY instance can read it.
12851
+ ...parentClose ? {
12852
+ tags: [
12853
+ `${PARENT_CLOSE_TAG_PREFIX}${parentClose}`
12854
+ ]
12855
+ } : {}
11613
12856
  });
11614
12857
  }, "startChild"),
12858
+ noteParentClose: /* @__PURE__ */ __name((parentRunId, childId, policy) => {
12859
+ let intents = this.parentCloseIntents.get(parentRunId);
12860
+ if (!intents) {
12861
+ intents = /* @__PURE__ */ new Map();
12862
+ this.parentCloseIntents.set(parentRunId, intents);
12863
+ }
12864
+ intents.set(childId, policy);
12865
+ }, "noteParentClose"),
11615
12866
  // Deferred for the same reentrancy reason as `startChild` above. `cancel()` is already
11616
12867
  // idempotent on a terminal/cancelled run (returns its existing status without side effects), so
11617
12868
  // no extra guard is needed here for the failFast replay case (re-issuing the same cancel calls).
@@ -11721,17 +12972,35 @@ ${run.workflowVersion}`;
11721
12972
  * - a `routable` plan — ≥1 live worker is capability-capable AND protocol-compatible (dispatch);
11722
12973
  * - a `blocked` plan — descriptors exist but none can run it (park the run, never dispatch into it).
11723
12974
  */
11724
- async planRoute(token, requires) {
11725
- const descriptors = await this.pool.readWorkerDescriptors(token);
12975
+ async planRoute(token, requires, fresh = false) {
12976
+ const descriptors = await this.liveWorkerDescriptors(token, fresh);
11726
12977
  if (descriptors.length === 0) return void 0;
11727
12978
  return planDispatch(requires ?? [], descriptors, this.cpDescriptor, token);
11728
12979
  }
12980
+ /** The live descriptors for `token`, reused for {@link workerDescriptorCacheMs}; one read in flight. */
12981
+ liveWorkerDescriptors(token, fresh = false) {
12982
+ if (this.workerDescriptorCacheMs <= 0) return this.pool.readWorkerDescriptors(token);
12983
+ const now = Date.now();
12984
+ const cached = this.descriptorCache.get(token);
12985
+ if (!fresh && cached && now - cached.at < this.workerDescriptorCacheMs) return cached.read;
12986
+ const read = this.pool.readWorkerDescriptors(token);
12987
+ const entry = {
12988
+ at: now,
12989
+ read
12990
+ };
12991
+ this.descriptorCache.set(token, entry);
12992
+ read.catch(() => {
12993
+ if (this.descriptorCache.get(token) === entry) this.descriptorCache.delete(token);
12994
+ });
12995
+ return read;
12996
+ }
11729
12997
  /** Throw {@link WorkflowBlocked} when no live worker can run a dispatch to `token` — so the caller's
11730
12998
  * run-execution catch parks the run `blocked` (with the plan) instead of persisting a `pending`
11731
12999
  * checkpoint + dispatching into a queue nobody consumes (design §7.5). A no-op when the guard does
11732
13000
  * not engage (no descriptors) or a capable+compatible worker exists. */
11733
13001
  async ensureRoutable(token, requires) {
11734
- const plan = await this.planRoute(token, requires);
13002
+ let plan = await this.planRoute(token, requires);
13003
+ if (plan?.status === "blocked") plan = await this.planRoute(token, requires, true);
11735
13004
  if (plan?.status === "blocked") throw new WorkflowBlocked(plan);
11736
13005
  }
11737
13006
  /**
@@ -11789,8 +13058,11 @@ ${run.workflowVersion}`;
11789
13058
  }
11790
13059
  async callRemote(runId, seq, step, input, queue, transport, replay, admission, routing) {
11791
13060
  const group = stepGroup(step, routing?.namespace);
13061
+ 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)) {
13062
+ throw new WorkflowSuspended();
13063
+ }
11792
13064
  let existing = replay?.get(seq) ?? await this.store.getCheckpoint(runId, seq);
11793
- if (existing?.status === "pending" && replay?.has(seq)) {
13065
+ if (existing?.status === "pending" && replay?.has(seq) && !routing?.fresh) {
11794
13066
  existing = await this.store.getCheckpoint(runId, seq) ?? existing;
11795
13067
  }
11796
13068
  if (existing && existing.name !== step.name) {
@@ -11850,7 +13122,9 @@ ${run.workflowVersion}`;
11850
13122
  input: existing.input,
11851
13123
  priority: admission?.priority,
11852
13124
  attempt: reAttempt,
11853
- transport
13125
+ transport,
13126
+ // A fan's steps are joined by its barrier, not a parked body: their results stay shared.
13127
+ replyHere: routing?.parallelGroup === void 0
11854
13128
  });
11855
13129
  this.emit({
11856
13130
  type: "step.started",
@@ -11922,7 +13196,8 @@ ${run.workflowVersion}`;
11922
13196
  input,
11923
13197
  priority: admission?.priority,
11924
13198
  attempt,
11925
- transport
13199
+ transport,
13200
+ replyHere: routing?.parallelGroup === void 0
11926
13201
  });
11927
13202
  this.emit({
11928
13203
  type: "step.started",
@@ -11935,6 +13210,61 @@ ${run.workflowVersion}`;
11935
13210
  throw new WorkflowSuspended();
11936
13211
  }
11937
13212
  /**
13213
+ * {@link callRemote}'s first dispatch of a step, written with
13214
+ * {@link StateStore.insertCheckpointIfAbsent}: resolves whether it wrote the `pending` row and
13215
+ * dispatched. `false` when the position already has a row, or when no live worker can take the
13216
+ * step (the read-first path then decides, so a step that is in fact done is never blocked).
13217
+ */
13218
+ async dispatchIfAbsent(runId, seq, step, input, group, transport, admission, routing) {
13219
+ try {
13220
+ await this.ensureRoutable(group, step.requires);
13221
+ } catch (err) {
13222
+ if (err instanceof WorkflowBlocked) return false;
13223
+ throw err;
13224
+ }
13225
+ const id = stepId(runId, seq);
13226
+ const enqueuedAt = /* @__PURE__ */ new Date();
13227
+ const wrote = await this.store.insertCheckpointIfAbsent.call(this.store, {
13228
+ runId,
13229
+ seq,
13230
+ name: step.name,
13231
+ kind: "remote",
13232
+ stepId: id,
13233
+ status: "pending",
13234
+ input,
13235
+ attempts: 1,
13236
+ workerGroup: group,
13237
+ ...routing?.parallelGroup !== void 0 ? {
13238
+ parallelGroup: routing.parallelGroup
13239
+ } : {},
13240
+ enqueuedAt,
13241
+ startedAt: enqueuedAt,
13242
+ finishedAt: enqueuedAt
13243
+ });
13244
+ if (!wrote) return false;
13245
+ await this.dispatchRemoteTask({
13246
+ runId,
13247
+ seq,
13248
+ name: step.name,
13249
+ stepId: id,
13250
+ group,
13251
+ input,
13252
+ priority: admission?.priority,
13253
+ attempt: 1,
13254
+ transport,
13255
+ replyHere: routing?.parallelGroup === void 0
13256
+ });
13257
+ this.emit({
13258
+ type: "step.started",
13259
+ runId,
13260
+ seq,
13261
+ name: step.name,
13262
+ kind: "remote"
13263
+ });
13264
+ this.awaitedRemoteSteps.get(runId)?.add(seq);
13265
+ return true;
13266
+ }
13267
+ /**
11938
13268
  * Whether a `pending` step a turn just read only waits for its result: {@link callRemote} would
11939
13269
  * suspend on it without writing (no redispatch deadline to stamp, the one stamped not yet due).
11940
13270
  */
@@ -12190,6 +13520,69 @@ ${run.workflowVersion}`;
12190
13520
  }
12191
13521
  }
12192
13522
  /**
13523
+ * Whether this turn of `run` may run in a sticky continuation (see `sticky-continuation.ts`). Not
13524
+ * while the engine drains, for a cancel being compensated, nor when execution wrappers are
13525
+ * registered: a resumed body runs in the async scope of the turn that started it, and a wrapper
13526
+ * (a span, a log batch) expects one scope per turn.
13527
+ */
13528
+ stickyEligible(run) {
13529
+ return this.sticky.enabled && !this.draining && run.status !== "cancelling" && !this.cancelRequested.has(run.id) && !hasDurableExecutionWrappers();
13530
+ }
13531
+ /**
13532
+ * The continuation this instance parked for `run`, if it is still the run's latest turn: the run
13533
+ * is `suspended` with the very stamp this instance's suspend wrote, on the same body and version.
13534
+ * Any other settle in between (another instance's turn, a retry, a cancel) changed the stamp, and
13535
+ * the run replays instead. Taking it removes it from the cache.
13536
+ */
13537
+ takeContinuation(run, fn) {
13538
+ const cont = this.sticky.take(run.id);
13539
+ if (!cont) return void 0;
13540
+ const valid = cont.fn === fn && cont.workflowVersion === run.workflowVersion && run.status === "suspended" && cont.stamp !== void 0 && run.updatedAt?.getTime() === cont.stamp;
13541
+ if (!valid) {
13542
+ this.sticky.misses += 1;
13543
+ return void 0;
13544
+ }
13545
+ this.sticky.hits += 1;
13546
+ return cont;
13547
+ }
13548
+ /** Keep `cont` (parked, detached) for the run's next turn here, stamped with its suspend write. */
13549
+ keepContinuation(cont, stamp) {
13550
+ if (cont.unusable) return;
13551
+ cont.stamp = stamp;
13552
+ cont.replay.clear();
13553
+ this.sticky.put(cont);
13554
+ }
13555
+ /**
13556
+ * The checkpoints of the steps `cont` is parked on, as a replay would see them: in one read when
13557
+ * the store batches, and without the input of a step that completed (what
13558
+ * {@link StateStore.getCheckpointsForSettle} leaves out). A step still open or failed is read
13559
+ * whole, since a re-dispatch or a retry sends its input again.
13560
+ */
13561
+ async readParked(runId, cont) {
13562
+ const fresh = /* @__PURE__ */ new Map();
13563
+ for (const seq of cont.parked.keys()) fresh.set(seq, cont.hints.get(seq) ?? null);
13564
+ cont.hints.clear();
13565
+ const seqs = [
13566
+ ...fresh
13567
+ ].flatMap(([seq, cp]) => cp ? [] : [
13568
+ seq
13569
+ ]);
13570
+ if (seqs.length === 0) return fresh;
13571
+ const lean = this.store.getCheckpointsForSettle ?? this.store.getCheckpoints;
13572
+ if (lean) {
13573
+ for (const cp of await lean.call(this.store, runId, seqs)) fresh.set(cp.seq, cp);
13574
+ } else {
13575
+ for (const seq of seqs) fresh.set(seq, await this.store.getCheckpoint(runId, seq));
13576
+ }
13577
+ if (lean === this.store.getCheckpointsForSettle) {
13578
+ for (const seq of seqs) {
13579
+ const cp = fresh.get(seq);
13580
+ if (cp && cp.status !== "completed") fresh.set(seq, await this.store.getCheckpoint(runId, seq));
13581
+ }
13582
+ }
13583
+ return fresh;
13584
+ }
13585
+ /**
12193
13586
  * The run's checkpoints as a turn replays them: {@link StateStore.listCheckpointsForReplay} when
12194
13587
  * the store has it (no input on a completed row), else {@link StateStore.listCheckpoints}.
12195
13588
  */
@@ -12229,8 +13622,8 @@ ${run.workflowVersion}`;
12229
13622
  * whole, the same way.
12230
13623
  */
12231
13624
  async settlePendingCheckpoints(settlements, settled) {
12232
- const settle = this.store.settleCheckpointsIfPending?.bind(this.store);
12233
- if (!settle) {
13625
+ const settle2 = this.store.settleCheckpointsIfPending?.bind(this.store);
13626
+ if (!settle2) {
12234
13627
  return this.savePendingCheckpoints(settlements.map((s, i) => ({
12235
13628
  ...settled[i],
12236
13629
  ...s
@@ -12238,12 +13631,12 @@ ${run.workflowVersion}`;
12238
13631
  }
12239
13632
  if (settlements.length > 1) {
12240
13633
  try {
12241
- const wrote = await settle(settlements);
13634
+ const wrote = await settle2(settlements);
12242
13635
  return settlements.map((_, i) => wrote[i] === true);
12243
13636
  } catch {
12244
13637
  }
12245
13638
  }
12246
- const one = await Promise.allSettled(settlements.map((s) => settle([
13639
+ const one = await Promise.allSettled(settlements.map((s) => settle2([
12247
13640
  s
12248
13641
  ])));
12249
13642
  return one.map((s) => s.status === "fulfilled" ? s.value[0] === true : {
@@ -12304,7 +13697,10 @@ ${run.workflowVersion}`;
12304
13697
  traceparent: this.traceparent?.(),
12305
13698
  context: this.context?.(),
12306
13699
  priority: task.priority,
12307
- attempt: task.attempt
13700
+ attempt: task.attempt,
13701
+ ...task.replyHere && this.repliesRouted && !this.draining ? {
13702
+ replyTo: this.instanceId
13703
+ } : {}
12308
13704
  }, task.transport);
12309
13705
  }
12310
13706
  /**
@@ -12396,6 +13792,9 @@ ${run.workflowVersion}`;
12396
13792
  i
12397
13793
  ];
12398
13794
  });
13795
+ if (this.store.settleResultsIfPending) {
13796
+ return this.settleResultsReturning(runId, results, live, outcomes, queuedIn);
13797
+ }
12399
13798
  const reads = await this.readCheckpoints(runId, live.map((i) => results[i].seq));
12400
13799
  const pending = [];
12401
13800
  const resettled = [];
@@ -12475,6 +13874,110 @@ ${run.workflowVersion}`;
12475
13874
  return wake.applyTo(outcomes);
12476
13875
  }
12477
13876
  /**
13877
+ * {@link completeRemoteResults} on a store with {@link StateStore.settleResultsIfPending}: the
13878
+ * results settle their `pending` rows and read them back in one call, instead of a read of the rows
13879
+ * followed by the write. Only an entry that wrote nothing (a redelivery, or a step gone) is read
13880
+ * afterwards. A settled `completed` row is also handed to the run's parked body, if this instance
13881
+ * holds one, so its next turn does not read it again.
13882
+ */
13883
+ async settleResultsReturning(runId, results, live, outcomes, queuedIn) {
13884
+ const settle2 = this.store.settleResultsIfPending.bind(this.store);
13885
+ const finishedAt = /* @__PURE__ */ new Date();
13886
+ const entries = live.map((i) => {
13887
+ const result = results[i];
13888
+ return {
13889
+ runId,
13890
+ seq: result.seq,
13891
+ status: result.status,
13892
+ output: result.status === "completed" ? result.output : void 0,
13893
+ error: result.error,
13894
+ events: result.events,
13895
+ startedAt: result.startedAt ? new Date(result.startedAt) : void 0,
13896
+ finishedAt
13897
+ };
13898
+ });
13899
+ let rows;
13900
+ try {
13901
+ rows = await settle2(entries);
13902
+ } catch (err) {
13903
+ if (entries.length === 1) {
13904
+ rows = [
13905
+ {
13906
+ error: err
13907
+ }
13908
+ ];
13909
+ } else {
13910
+ const one = await Promise.allSettled(entries.map((e) => settle2([
13911
+ e
13912
+ ])));
13913
+ rows = one.map((o) => o.status === "fulfilled" ? o.value[0] ?? null : {
13914
+ error: o.reason
13915
+ });
13916
+ }
13917
+ }
13918
+ const settled = [];
13919
+ const unsettled = [];
13920
+ rows.forEach((row, k) => {
13921
+ const index = live[k];
13922
+ if (row === null) unsettled.push(index);
13923
+ else if ("error" in row && !("seq" in row)) outcomes[index] = {
13924
+ error: row.error
13925
+ };
13926
+ else settled.push({
13927
+ index,
13928
+ cp: row,
13929
+ result: results[index]
13930
+ });
13931
+ });
13932
+ const parked = this.sticky.peek(runId) ?? this.turnContinuations.get(runId);
13933
+ for (const { cp, result } of settled) {
13934
+ void this.releaseQueueSlot(cp.stepId).catch(() => void 0);
13935
+ if (cp.status === "completed") parked?.hint(cp);
13936
+ if (!parseStepFanGroup(cp.parallelGroup)) this.noteSettledHere(`${runId}:${result.seq}`);
13937
+ this.emit({
13938
+ type: result.status === "completed" ? "step.completed" : "step.failed",
13939
+ runId,
13940
+ seq: result.seq,
13941
+ name: cp.name,
13942
+ kind: cp.kind,
13943
+ output: result.output,
13944
+ error: result.error,
13945
+ queueMs: cp.startedAt.getTime() - cp.enqueuedAt.getTime(),
13946
+ durationMs: finishedAt.getTime() - cp.startedAt.getTime()
13947
+ });
13948
+ }
13949
+ if (settled.length === 0) {
13950
+ if (unsettled.length === 0) return outcomes;
13951
+ const reads = await this.readCheckpoints(runId, unsettled.map((i) => results[i].seq));
13952
+ const resettled = [];
13953
+ reads.forEach((read, k) => {
13954
+ const index = unsettled[k];
13955
+ if (read.status === "rejected") outcomes[index] = {
13956
+ error: read.reason
13957
+ };
13958
+ else if (read.value?.status === "completed" || read.value?.status === "failed") {
13959
+ resettled.push({
13960
+ index,
13961
+ cp: read.value
13962
+ });
13963
+ } else if (read.value?.status === "pending") {
13964
+ void this.releaseQueueSlot(read.value.stepId).catch(() => void 0);
13965
+ }
13966
+ });
13967
+ if (resettled.length === 0) return outcomes;
13968
+ return this.resumeForRedelivered(runId, resettled, outcomes, queuedIn);
13969
+ }
13970
+ const barriers = /* @__PURE__ */ new Map();
13971
+ const wake = new FanWakeCollector();
13972
+ for (const { cp } of settled) wake.note(cp);
13973
+ for (const { index, cp, result } of settled) {
13974
+ if (!await this.stepFanStillRunning(cp, result, barriers, queuedIn)) {
13975
+ wake.add(index, cp, result.status);
13976
+ }
13977
+ }
13978
+ return wake.applyTo(outcomes);
13979
+ }
13980
+ /**
12478
13981
  * The outcomes for a batch of redelivered results (their steps already settled): one of them
12479
13982
  * resumes the run if it is `suspended` and, for a step of a fan, the fan's barrier is open. A
12480
13983
  * resume of a run that had in fact moved on replays to its current wait and re-parks.
@@ -13364,6 +14867,15 @@ var CodecStateStore = class {
13364
14867
  /** Encodes each settlement's payloads like {@link saveCheckpoint}. Bound only when the inner store
13365
14868
  * has it, so the engine's whole-row fallback still kicks in through the wrapper. */
13366
14869
  settleCheckpointsIfPending;
14870
+ /** Encodes each result's payloads, decodes each row read back. Bound only when the inner store
14871
+ * has it. */
14872
+ settleResultsIfPending;
14873
+ /** Encodes like {@link saveCheckpoint}. Bound only when the inner store has it. */
14874
+ insertCheckpointIfAbsent;
14875
+ /** Decodes each run claimed. Bound only when the inner store has it. */
14876
+ claimPendingRuns;
14877
+ /** Decodes the run read. Bound only when the inner store has it. */
14878
+ tryLockRunAndGet;
13367
14879
  /** Encodes each checkpoint written. Bound only when the inner store has it. */
13368
14880
  saveCheckpoints;
13369
14881
  /** Decodes each checkpoint read like {@link listCheckpoints}. Bound only when the inner store has
@@ -13399,6 +14911,14 @@ var CodecStateStore = class {
13399
14911
  listBufferedSignals;
13400
14912
  /** Forwarded verbatim — deletes by row id. */
13401
14913
  deleteBufferedSignals;
14914
+ /** Decodes the payload like {@link takeBufferedSignal}. Bound only when the inner store has it. */
14915
+ takeOldestBufferedSignal;
14916
+ /** Forwarded verbatim — a signal id record carries no payload. */
14917
+ claimSignalId;
14918
+ /** Forwarded verbatim. */
14919
+ releaseSignalId;
14920
+ /** Forwarded verbatim. */
14921
+ deleteExpiredSignalIds;
13402
14922
  /** Forwarded verbatim — a waiter carries no payload. */
13403
14923
  listSignalWaitersByRun;
13404
14924
  /** Forwarded verbatim — a waiter carries no payload. */
@@ -13427,6 +14947,14 @@ var CodecStateStore = class {
13427
14947
  if (saveManyIfPending) this.saveCheckpointsIfPending = (cps) => saveManyIfPending.call(inner, cps.map((cp) => this.encCp(cp)));
13428
14948
  const settleMany = inner.settleCheckpointsIfPending;
13429
14949
  if (settleMany) this.settleCheckpointsIfPending = (settlements) => settleMany.call(inner, settlements.map((s) => this.encSettlement(s)));
14950
+ const settleResults = inner.settleResultsIfPending;
14951
+ if (settleResults) this.settleResultsIfPending = async (results) => (await settleResults.call(inner, results.map((r) => this.encSettlement(r)))).map((cp) => this.decCp(cp));
14952
+ const insertIfAbsent = inner.insertCheckpointIfAbsent;
14953
+ if (insertIfAbsent) this.insertCheckpointIfAbsent = (cp) => insertIfAbsent.call(inner, this.encCp(cp));
14954
+ const claim = inner.claimPendingRuns;
14955
+ if (claim) this.claimPendingRuns = async (limit, owner, leaseUntilMs, nowMs, namespace) => (await claim.call(inner, limit, owner, leaseUntilMs, nowMs, namespace)).map((r) => this.decRun(r));
14956
+ const lockAndGet = inner.tryLockRunAndGet;
14957
+ if (lockAndGet) this.tryLockRunAndGet = async (runId, owner, leaseUntilMs, nowMs) => this.decRun(await lockAndGet.call(inner, runId, owner, leaseUntilMs, nowMs));
13430
14958
  const saveMany = inner.saveCheckpoints;
13431
14959
  if (saveMany) this.saveCheckpoints = (cps) => saveMany.call(inner, cps.map((cp) => this.encCp(cp)));
13432
14960
  const listForReplay = inner.listCheckpointsForReplay;
@@ -13463,6 +14991,18 @@ var CodecStateStore = class {
13463
14991
  }));
13464
14992
  const deleteBuffered = inner.deleteBufferedSignals;
13465
14993
  if (deleteBuffered) this.deleteBufferedSignals = (ids) => deleteBuffered.call(inner, ids);
14994
+ const takeOldest = inner.takeOldestBufferedSignal;
14995
+ if (takeOldest) this.takeOldestBufferedSignal = async (tokens) => {
14996
+ const taken = await takeOldest.call(inner, tokens);
14997
+ return taken ? {
14998
+ token: taken.token,
14999
+ payload: this.dec(taken.payload)
15000
+ } : null;
15001
+ };
15002
+ const { claimSignalId, releaseSignalId, deleteExpiredSignalIds } = inner;
15003
+ if (claimSignalId) this.claimSignalId = (token, id, expiresAt, nowMs) => claimSignalId.call(inner, token, id, expiresAt, nowMs);
15004
+ if (releaseSignalId) this.releaseSignalId = (token, id) => releaseSignalId.call(inner, token, id);
15005
+ if (deleteExpiredSignalIds) this.deleteExpiredSignalIds = (nowMs, limit) => deleteExpiredSignalIds.call(inner, nowMs, limit);
13466
15006
  const byRun = inner.listSignalWaitersByRun;
13467
15007
  if (byRun) this.listSignalWaitersByRun = (runIds) => byRun.call(inner, runIds);
13468
15008
  const hasWaiter = inner.hasSignalWaiter;
@@ -14247,6 +15787,87 @@ var InMemoryStateStore = class {
14247
15787
  return true;
14248
15788
  });
14249
15789
  }
15790
+ // The three round-trip-saving methods below are built on the plain ones, so a test store that
15791
+ // overrides `tryLockRun`, `getRun`, `getCheckpoint` or a settle to stage a race keeps staging it.
15792
+ // They are OFF unless asked for (`roundTripMethods`, or `DURABLE_TEST_ROUND_TRIP_METHODS=1` for a
15793
+ // whole suite): many specs pin the store calls the engine makes without them.
15794
+ settleResultsIfPending;
15795
+ insertCheckpointIfAbsent;
15796
+ tryLockRunAndGet;
15797
+ claimPendingRuns;
15798
+ constructor(opts = {}) {
15799
+ const on = opts.roundTripMethods ?? (typeof process !== "undefined" && process.env.DURABLE_TEST_ROUND_TRIP_METHODS === "1");
15800
+ if (on) {
15801
+ this.settleResultsIfPending = (results) => this.settleResultsFallback(results);
15802
+ this.insertCheckpointIfAbsent = (cp) => this.insertIfAbsentFallback(cp);
15803
+ this.tryLockRunAndGet = (runId, owner, until, now) => this.lockAndGetFallback(runId, owner, until, now);
15804
+ this.claimPendingRuns = async (limit, owner, until, now, namespace) => {
15805
+ const claimed = [];
15806
+ for (const run of await this.listPendingRuns(Number.MAX_SAFE_INTEGER, namespace)) {
15807
+ if (claimed.length >= limit) break;
15808
+ const locked = await this.lockAndGetFallback(run.id, owner, until, now);
15809
+ if (locked) claimed.push(locked);
15810
+ }
15811
+ return claimed;
15812
+ };
15813
+ }
15814
+ }
15815
+ async settleResultsFallback(results) {
15816
+ const out = results.map(() => null);
15817
+ const before = /* @__PURE__ */ new Map();
15818
+ const seqsByRun = /* @__PURE__ */ new Map();
15819
+ for (const r of results) seqsByRun.set(r.runId, [
15820
+ ...seqsByRun.get(r.runId) ?? [],
15821
+ r.seq
15822
+ ]);
15823
+ for (const [runId, seqs] of seqsByRun) {
15824
+ for (const cp of await this.getCheckpointsForSettle(runId, seqs)) {
15825
+ before.set(this.key(runId, cp.seq), cp);
15826
+ }
15827
+ }
15828
+ const claimed = /* @__PURE__ */ new Set();
15829
+ const settlements = [];
15830
+ const at = [];
15831
+ for (const [i, r] of results.entries()) {
15832
+ const key = this.key(r.runId, r.seq);
15833
+ const cp = before.get(key);
15834
+ if (cp?.status !== "pending" || claimed.has(key)) continue;
15835
+ const status = (await this.getRun(r.runId))?.status;
15836
+ if (status === "cancelled" || status === "completed") continue;
15837
+ claimed.add(key);
15838
+ settlements.push({
15839
+ runId: r.runId,
15840
+ seq: r.seq,
15841
+ status: r.status,
15842
+ output: r.status === "completed" ? r.output : cp.output,
15843
+ error: r.error,
15844
+ events: r.events ?? cp.events,
15845
+ startedAt: r.startedAt ?? cp.startedAt,
15846
+ finishedAt: r.finishedAt
15847
+ });
15848
+ at.push(i);
15849
+ }
15850
+ if (settlements.length === 0) return out;
15851
+ const wrote = await this.settleCheckpointsIfPending(settlements);
15852
+ settlements.forEach((settlement, k) => {
15853
+ if (!wrote[k]) return;
15854
+ const cp = before.get(this.key(settlement.runId, settlement.seq));
15855
+ out[at[k]] = {
15856
+ ...cp,
15857
+ ...settlement
15858
+ };
15859
+ });
15860
+ return out;
15861
+ }
15862
+ async insertIfAbsentFallback(checkpoint) {
15863
+ if (await this.getCheckpoint(checkpoint.runId, checkpoint.seq)) return false;
15864
+ await this.saveCheckpoint(checkpoint);
15865
+ return true;
15866
+ }
15867
+ async lockAndGetFallback(runId, owner, leaseUntilMs, nowMs) {
15868
+ if (!await this.tryLockRun(runId, owner, leaseUntilMs, nowMs)) return null;
15869
+ return this.getRun(runId);
15870
+ }
14250
15871
  async listOrphanedRuns(nowMs, limit, namespace, ownedBy) {
14251
15872
  return [
14252
15873
  ...this.runs.values()
@@ -14377,6 +15998,45 @@ var InMemoryStateStore = class {
14377
15998
  payload: head.payload
14378
15999
  };
14379
16000
  }
16001
+ async takeOldestBufferedSignal(tokens) {
16002
+ let oldest;
16003
+ for (const token of new Set(tokens)) {
16004
+ const head = this.bufferedSignals.get(token)?.[0];
16005
+ if (head && (!oldest || head.id < oldest.id)) oldest = {
16006
+ token,
16007
+ id: head.id
16008
+ };
16009
+ }
16010
+ if (!oldest) return null;
16011
+ const taken = await this.takeBufferedSignal(oldest.token);
16012
+ return taken ? {
16013
+ token: oldest.token,
16014
+ payload: taken.payload
16015
+ } : null;
16016
+ }
16017
+ /** `token \u0000 id -> expiresAt` of the signal ids {@link claimSignalId} recorded. */
16018
+ signalIds = /* @__PURE__ */ new Map();
16019
+ async claimSignalId(token, id, expiresAt, nowMs) {
16020
+ const key = `${token}\0${id}`;
16021
+ const existing = this.signalIds.get(key);
16022
+ if (existing !== void 0 && existing > nowMs) return false;
16023
+ this.signalIds.set(key, expiresAt);
16024
+ return true;
16025
+ }
16026
+ async releaseSignalId(token, id) {
16027
+ this.signalIds.delete(`${token}\0${id}`);
16028
+ }
16029
+ async deleteExpiredSignalIds(nowMs, limit) {
16030
+ let deleted = 0;
16031
+ for (const [key, expiresAt] of this.signalIds) {
16032
+ if (deleted >= limit) break;
16033
+ if (expiresAt <= nowMs) {
16034
+ this.signalIds.delete(key);
16035
+ deleted += 1;
16036
+ }
16037
+ }
16038
+ return deleted;
16039
+ }
14380
16040
  async listBufferedSignals(tokenPrefix, afterId, limit) {
14381
16041
  const after = afterId === void 0 ? 0 : Number(afterId);
14382
16042
  const rows = [];
@@ -14764,8 +16424,73 @@ var InMemoryRunQueue = class {
14764
16424
  }
14765
16425
  }
14766
16426
  };
16427
+
16428
+ // src/settle-results.ts
16429
+ async function settleResultsWithPlainCalls(store, results) {
16430
+ const out = results.map(() => null);
16431
+ const key = /* @__PURE__ */ __name((runId, seq) => `${seq}:${runId}`, "key");
16432
+ const seqsByRun = /* @__PURE__ */ new Map();
16433
+ for (const r of results) seqsByRun.set(r.runId, [
16434
+ ...seqsByRun.get(r.runId) ?? [],
16435
+ r.seq
16436
+ ]);
16437
+ const runIds = [
16438
+ ...seqsByRun.keys()
16439
+ ];
16440
+ const runs = store.getRuns ? await store.getRuns(runIds) : (await Promise.all(runIds.map((id) => store.getRun(id)))).filter((r) => r !== null);
16441
+ const finished = new Set(runs.filter((r) => r.status === "cancelled" || r.status === "completed").map((r) => r.id));
16442
+ const read = /* @__PURE__ */ __name(async (runId, seqs) => {
16443
+ if (store.getCheckpointsForSettle) return store.getCheckpointsForSettle(runId, seqs);
16444
+ if (store.getCheckpoints) return store.getCheckpoints(runId, seqs);
16445
+ const rows = await Promise.all(seqs.map((seq) => store.getCheckpoint(runId, seq)));
16446
+ return rows.filter((cp) => cp !== null);
16447
+ }, "read");
16448
+ const before = /* @__PURE__ */ new Map();
16449
+ for (const [runId, seqs] of seqsByRun) {
16450
+ if (finished.has(runId)) continue;
16451
+ for (const cp of await read(runId, seqs)) before.set(key(runId, cp.seq), cp);
16452
+ }
16453
+ const claimed = /* @__PURE__ */ new Set();
16454
+ const settlements = [];
16455
+ const at = [];
16456
+ for (const [i, r] of results.entries()) {
16457
+ const k = key(r.runId, r.seq);
16458
+ const cp = before.get(k);
16459
+ if (cp?.status !== "pending" || claimed.has(k)) continue;
16460
+ claimed.add(k);
16461
+ settlements.push({
16462
+ runId: r.runId,
16463
+ seq: r.seq,
16464
+ status: r.status,
16465
+ output: r.status === "completed" ? r.output : cp.output,
16466
+ error: r.error,
16467
+ events: r.events ?? cp.events,
16468
+ startedAt: r.startedAt ?? cp.startedAt,
16469
+ finishedAt: r.finishedAt
16470
+ });
16471
+ at.push(i);
16472
+ }
16473
+ if (settlements.length === 0 || !store.settleCheckpointsIfPending) return out;
16474
+ const wrote = await store.settleCheckpointsIfPending(settlements);
16475
+ const won = settlements.filter((_, j) => wrote[j] === true);
16476
+ const after = /* @__PURE__ */ new Map();
16477
+ const wonByRun = /* @__PURE__ */ new Map();
16478
+ for (const s of won) wonByRun.set(s.runId, [
16479
+ ...wonByRun.get(s.runId) ?? [],
16480
+ s.seq
16481
+ ]);
16482
+ for (const [runId, seqs] of wonByRun) {
16483
+ for (const cp of await read(runId, seqs)) after.set(key(runId, cp.seq), cp);
16484
+ }
16485
+ settlements.forEach((s, j) => {
16486
+ if (wrote[j] === true) out[at[j]] = after.get(key(s.runId, s.seq)) ?? null;
16487
+ });
16488
+ return out;
16489
+ }
16490
+ __name(settleResultsWithPlainCalls, "settleResultsWithPlainCalls");
14767
16491
  export {
14768
16492
  ACCUMULATOR_SEEN_MAX,
16493
+ ANY_WAIT_GROUP,
14769
16494
  CHILD_EDGE_PREFIXES,
14770
16495
  CONTROL_FLOW_SIGNAL,
14771
16496
  CORE_ORIGIN,
@@ -14812,6 +16537,7 @@ export {
14812
16537
  LIFECYCLE_TRIGGER_FAILED_TAG_PREFIX,
14813
16538
  NestedWorkflowCallError,
14814
16539
  NonDeterminismError,
16540
+ PARENT_CLOSE_TAG_PREFIX,
14815
16541
  QueueController,
14816
16542
  REMOTE_EVENT,
14817
16543
  RUN_HISTORY_FIXTURE_VERSION,
@@ -14851,6 +16577,7 @@ export {
14851
16577
  aggregateAnnouncements,
14852
16578
  aggregatesCheckpoints,
14853
16579
  announcementsOf,
16580
+ anyCheckpointName,
14854
16581
  applyAttributeQuery,
14855
16582
  assertConcurrency,
14856
16583
  assertRoutes,
@@ -14907,6 +16634,8 @@ export {
14907
16634
  hmacTenantVerifier,
14908
16635
  indexWaitersByRun,
14909
16636
  info,
16637
+ isAnyCheckpoint,
16638
+ isAnyMember,
14910
16639
  isEngineMintedTag,
14911
16640
  isEventToken,
14912
16641
  isFailedMember,
@@ -14936,6 +16665,7 @@ export {
14936
16665
  missingCapabilities,
14937
16666
  negotiate,
14938
16667
  nextCronFireMs,
16668
+ nextRRuleFireMs,
14939
16669
  normalizeAttributeRows,
14940
16670
  normalizeDescriptor,
14941
16671
  observedAnnouncements,
@@ -14946,11 +16676,13 @@ export {
14946
16676
  parentRunIdOf,
14947
16677
  parseDuration,
14948
16678
  parseLifecycleEvent,
16679
+ parseRRule,
14949
16680
  parseWebhookToken,
14950
16681
  partitionOfToken,
14951
16682
  planDispatch,
14952
16683
  poolNamespace,
14953
16684
  prevCronFireMs,
16685
+ prevRRuleFireMs,
14954
16686
  queryCheckpointsOf,
14955
16687
  readRunsByIds,
14956
16688
  readSearchAttributes,
@@ -14980,6 +16712,7 @@ export {
14980
16712
  sanitizeQueueToken,
14981
16713
  scheduleRunIdAt,
14982
16714
  scheduledRunId,
16715
+ settleResultsWithPlainCalls,
14983
16716
  signTenantToken,
14984
16717
  singletonAdmissionDenialPatch,
14985
16718
  singletonAdmissionTags,