durability 5.2.0 → 5.3.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/policy.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { z } from 'zod';
1
+ import * as v from 'valibot';
2
2
  /**
3
3
  * Retry policy shared by all methods or overridden for one method.
4
4
  *
@@ -46,23 +46,23 @@ export type AttemptPolicy = {
46
46
  delay: (attempt: number) => number;
47
47
  maxAttempts: number;
48
48
  };
49
- export declare const methodOptionsSchema: z.ZodObject<{
50
- attemptTimeoutMs: z.ZodOptional<z.ZodNumber>;
51
- retries: z.ZodOptional<z.ZodObject<{
52
- delay: z.ZodOptional<z.ZodCustom<(attempt: number) => number, (attempt: number) => number>>;
53
- maxAttempts: z.ZodOptional<z.ZodNumber>;
54
- }, z.core.$strip>>;
55
- }, z.core.$strip>;
56
- export declare const alarmMethodOptionsSchema: z.ZodObject<{
57
- attemptTimeoutMs: z.ZodOptional<z.ZodNumber>;
58
- retries: z.ZodOptional<z.ZodObject<{
59
- delay: z.ZodOptional<z.ZodCustom<(attempt: number) => number, (attempt: number) => number>>;
60
- maxAttempts: z.ZodOptional<z.ZodNumber>;
61
- }, z.core.$strip>>;
62
- retryTimeouts: z.ZodOptional<z.ZodBoolean>;
63
- }, z.core.$strip>;
64
- export type MethodPolicy = z.infer<typeof methodOptionsSchema>;
65
- export type AlarmMethodPolicy = z.infer<typeof alarmMethodOptionsSchema>;
49
+ export declare const methodOptionsSchema: v.ObjectSchema<{
50
+ readonly attemptTimeoutMs: v.OptionalSchema<v.NumberSchema<undefined>, undefined>;
51
+ readonly retries: v.OptionalSchema<v.ObjectSchema<{
52
+ readonly delay: v.OptionalSchema<v.CustomSchema<(attempt: number) => number, undefined>, undefined>;
53
+ readonly maxAttempts: v.OptionalSchema<v.NumberSchema<undefined>, undefined>;
54
+ }, undefined>, undefined>;
55
+ }, undefined>;
56
+ export declare const alarmMethodOptionsSchema: v.ObjectSchema<{
57
+ readonly retryTimeouts: v.OptionalSchema<v.BooleanSchema<undefined>, undefined>;
58
+ readonly attemptTimeoutMs: v.OptionalSchema<v.NumberSchema<undefined>, undefined>;
59
+ readonly retries: v.OptionalSchema<v.ObjectSchema<{
60
+ readonly delay: v.OptionalSchema<v.CustomSchema<(attempt: number) => number, undefined>, undefined>;
61
+ readonly maxAttempts: v.OptionalSchema<v.NumberSchema<undefined>, undefined>;
62
+ }, undefined>, undefined>;
63
+ }, undefined>;
64
+ export type MethodPolicy = v.InferOutput<typeof methodOptionsSchema>;
65
+ export type AlarmMethodPolicy = v.InferOutput<typeof alarmMethodOptionsSchema>;
66
66
  export declare const assertPositiveInteger: (name: string, value: number) => void;
67
67
  /**
68
68
  * Validates global and per-method policy options once and resolves the
@@ -71,4 +71,4 @@ export declare const assertPositiveInteger: (name: string, value: number) => voi
71
71
  export type PolicyResolver<Method> = (name: string) => AttemptPolicy & {
72
72
  method: Method | undefined;
73
73
  };
74
- export declare const createPolicyResolver: <Method extends MethodPolicy>(options: ExecutionPolicyOptions, methods: Record<string, unknown>, methodSchema: z.ZodType<Method>) => PolicyResolver<Method>;
74
+ export declare const createPolicyResolver: <Method extends MethodPolicy>(options: ExecutionPolicyOptions, methods: Record<string, unknown>, methodSchema: v.GenericSchema<unknown, Method>) => PolicyResolver<Method>;
package/dist/policy.js CHANGED
@@ -1,16 +1,17 @@
1
- import { z } from 'zod';
1
+ import * as v from 'valibot';
2
2
  import { exponential, jitter } from './utils.js';
3
- const functionSchema = () => z.custom((value) => typeof value === 'function');
4
- const retryOptionsSchema = z.object({
5
- delay: functionSchema().optional(),
6
- maxAttempts: z.number().optional(),
3
+ const functionSchema = () => v.custom((value) => typeof value === 'function');
4
+ const retryOptionsSchema = v.object({
5
+ delay: v.optional(functionSchema()),
6
+ maxAttempts: v.optional(v.number()),
7
7
  });
8
- export const methodOptionsSchema = z.object({
9
- attemptTimeoutMs: z.number().optional(),
10
- retries: retryOptionsSchema.optional(),
8
+ export const methodOptionsSchema = v.object({
9
+ attemptTimeoutMs: v.optional(v.number()),
10
+ retries: v.optional(retryOptionsSchema),
11
11
  });
12
- export const alarmMethodOptionsSchema = methodOptionsSchema.extend({
13
- retryTimeouts: z.boolean().optional(),
12
+ export const alarmMethodOptionsSchema = v.object({
13
+ ...methodOptionsSchema.entries,
14
+ retryTimeouts: v.optional(v.boolean()),
14
15
  });
15
16
  export const assertPositiveInteger = (name, value) => {
16
17
  if (!Number.isInteger(value) || value < 1) {
@@ -27,13 +28,13 @@ const assertMethodOptions = (prefix, method) => {
27
28
  };
28
29
  export const createPolicyResolver = (options, methods, methodSchema) => {
29
30
  const attemptTimeoutMs = options.attemptTimeoutMs ?? 5 * 60000;
30
- const retries = retryOptionsSchema.parse(options.retries ?? {});
31
+ const retries = v.parse(retryOptionsSchema, options.retries ?? {});
31
32
  const delay = retries.delay ?? ((attempt) => jitter(exponential(attempt)));
32
33
  const maxAttempts = retries.maxAttempts ?? 5;
33
34
  assertPositiveInteger('attemptTimeoutMs', attemptTimeoutMs);
34
35
  assertPositiveInteger('retries.maxAttempts', maxAttempts);
35
36
  const parsed = new Map(Object.entries(methods).map(([name, method]) => {
36
- const valid = methodSchema.parse(method);
37
+ const valid = v.parse(methodSchema, method);
37
38
  assertMethodOptions(`methods.${name}`, valid);
38
39
  return [name, valid];
39
40
  }));
@@ -111,7 +111,8 @@ export type DurabilityScheduleConfig = SchedulerAttachment & {
111
111
  channel?: string;
112
112
  /**
113
113
  * Returns the first tick strictly after `after`, or `undefined` for none.
114
- * Must be synchronous and deterministic; durability ships no cron parser.
114
+ * Must be synchronous; durability ships no cron parser. When its answers
115
+ * change, call {@link DurabilitySchedule.reschedule}.
115
116
  */
116
117
  nextOccurrence(expression: string, after: number): number | undefined;
117
118
  /** The work of one tick. Throwing or timing out makes the run `errored`. */
@@ -154,6 +155,17 @@ export declare class DurabilitySchedule {
154
155
  * alarm if it was lost.
155
156
  */
156
157
  set(definition: ScheduleDefinition): Promise<boolean>;
158
+ /**
159
+ * Recomputes the stored next tick with `nextOccurrence`, for when its answers
160
+ * changed for a reason durability can't see, and resolves `true` when the
161
+ * stored next tick changed. Resolves `false` when it didn't, when no schedule
162
+ * is live, or when the stored tick is already due: the alarm fires it, and
163
+ * the advance after it asks `nextOccurrence` again. The search starts from
164
+ * `now - jitterMs`, so a tick whose firing time is still ahead is kept and
165
+ * one whose firing time passed is skipped, not run. The version, the stored
166
+ * jitter, cancellations, and runs are unchanged.
167
+ */
168
+ reschedule(): Promise<boolean>;
157
169
  /**
158
170
  * Stops future ticks and resolves `true` when a schedule was live. Runs
159
171
  * already due or running still finish and report. With `version`, the
@@ -10,7 +10,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
10
10
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
11
  };
12
12
  var _DurabilitySchedule_instances, _DurabilitySchedule_engine, _DurabilitySchedule_channel, _DurabilitySchedule_nextOccurrence, _DurabilitySchedule_run, _DurabilitySchedule_onOutcome, _DurabilitySchedule_maxConcurrentRuns, _DurabilitySchedule_runTimeoutMs, _DurabilitySchedule_outcomeTimeoutMs, _DurabilitySchedule_maxOutcomeAttempts, _DurabilitySchedule_delay, _DurabilitySchedule_keyPrefix, _DurabilitySchedule_keySuffix, _DurabilitySchedule_emit, _DurabilitySchedule_active, _DurabilitySchedule_delivering, _DurabilitySchedule_loop, _DurabilitySchedule_woken, _DurabilitySchedule_wakeSleeper, _DurabilitySchedule_nextPendingAt, _DurabilitySchedule_nextAfter, _DurabilitySchedule_idempotencyKey, _DurabilitySchedule_describe, _DurabilitySchedule_entity, _DurabilitySchedule_hold, _DurabilitySchedule_drive, _DurabilitySchedule_pass, _DurabilitySchedule_wake, _DurabilitySchedule_sleep, _DurabilitySchedule_nextWakeAt, _DurabilitySchedule_interruptOrphans, _DurabilitySchedule_tick, _DurabilitySchedule_startQueued, _DurabilitySchedule_reserveSlots, _DurabilitySchedule_start, _DurabilitySchedule_record, _DurabilitySchedule_deliverDue, _DurabilitySchedule_deliver, _DurabilitySchedule_deliverOnce, _DurabilitySchedule_failDelivery, _DurabilitySchedule_retryDelay;
13
- import { z } from 'zod';
13
+ import * as v from 'valibot';
14
14
  import { traceLifecycle, traceMethods } from '../dev.js';
15
15
  import { reportFailure, ScheduledOutcomeTimeoutError, ScheduledRunInterruptedError, ScheduledRunTimeoutError, serializeError, } from '../errors.js';
16
16
  import { durabilityScheduleMigrations } from './migrations.js';
@@ -25,37 +25,37 @@ const runsTable = {
25
25
  // setTimeout overflows past 2^31 - 1 ms.
26
26
  const maxDelayMs = 2 ** 31 - 1;
27
27
  const fallbackRetryMs = 60000;
28
- const scheduleRowSchema = z.object({
29
- version: z.number().int().nonnegative(),
30
- expression: z.string().nullable(),
31
- next_at: z.number().int().nonnegative().nullable(),
32
- jitter_ms: z.number().int().nonnegative(),
28
+ const scheduleRowSchema = v.object({
29
+ version: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
30
+ expression: v.nullable(v.string()),
31
+ next_at: v.nullable(v.pipe(v.number(), v.safeInteger(), v.minValue(0))),
32
+ jitter_ms: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
33
33
  });
34
- const runStateSchema = z.enum(['queued', 'running', 'complete', 'errored']);
35
- const runRowSchema = z.object({
36
- scheduled_time: z.number().int().nonnegative(),
37
- due_at: z.number().int().nonnegative(),
34
+ const runStateSchema = v.picklist(['queued', 'running', 'complete', 'errored']);
35
+ const runRowSchema = v.object({
36
+ scheduled_time: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
37
+ due_at: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
38
38
  state: runStateSchema,
39
- next_at: z.number().int().nonnegative(),
40
- attempt: z.number().int().nonnegative(),
41
- error: z.string().nullable(),
39
+ next_at: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
40
+ attempt: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
41
+ error: v.nullable(v.string()),
42
42
  });
43
- const timeRowSchema = z.object({
44
- scheduled_time: z.number().int().nonnegative(),
43
+ const timeRowSchema = v.object({
44
+ scheduled_time: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
45
45
  });
46
- const dueRowSchema = z.object({
47
- scheduled_time: z.number().int().nonnegative(),
48
- due_at: z.number().int().nonnegative(),
46
+ const dueRowSchema = v.object({
47
+ scheduled_time: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
48
+ due_at: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
49
49
  });
50
- const timedRowSchema = z.object({
51
- scheduled_time: z.number().int().nonnegative(),
52
- next_at: z.number().int().nonnegative(),
50
+ const timedRowSchema = v.object({
51
+ scheduled_time: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
52
+ next_at: v.pipe(v.number(), v.safeInteger(), v.minValue(0)),
53
53
  });
54
- const serializedErrorSchema = z.object({
55
- name: z.string(),
56
- message: z.string(),
54
+ const serializedErrorSchema = v.object({
55
+ name: v.string(),
56
+ message: v.string(),
57
57
  });
58
- const readSchedule = (sql, channel) => scheduleRowSchema.optional().parse(sql
58
+ const readSchedule = (sql, channel) => v.parse(v.optional(scheduleRowSchema), sql
59
59
  .exec(`SELECT
60
60
  version,
61
61
  expression,
@@ -83,14 +83,13 @@ const writeSchedule = (sql, channel, schedule) => {
83
83
  const firesAt = (schedule) => schedule === undefined || schedule.next_at === null
84
84
  ? undefined
85
85
  : schedule.next_at + schedule.jitter_ms;
86
- const scheduledTimes = (cursor) => timeRowSchema
87
- .array()
88
- .parse(cursor.toArray())
86
+ const scheduledTimes = (cursor) => v
87
+ .parse(v.array(timeRowSchema), cursor.toArray())
89
88
  .map((row) => row.scheduled_time);
90
89
  const outcomeOf = (row) => row.state === 'errored'
91
90
  ? {
92
91
  status: 'errored',
93
- error: serializedErrorSchema.parse(JSON.parse(row.error ?? '{}')),
92
+ error: v.parse(serializedErrorSchema, JSON.parse(row.error ?? '{}')),
94
93
  }
95
94
  : { status: 'complete' };
96
95
  const assertNonNegative = (name, value) => {
@@ -122,22 +121,6 @@ const assertDelay = (name, value) => {
122
121
  throw new RangeError(`${name} must be at most ${maxDelayMs}`);
123
122
  }
124
123
  };
125
- const deadline = async (work, timeoutMs, onTimeout) => {
126
- let timer;
127
- try {
128
- await Promise.race([
129
- work,
130
- new Promise((_resolve, reject) => {
131
- timer = setTimeout(() => reject(onTimeout()), timeoutMs);
132
- }),
133
- ]);
134
- }
135
- finally {
136
- if (timer !== undefined) {
137
- clearTimeout(timer);
138
- }
139
- }
140
- };
141
124
  /**
142
125
  * Experimental. One recurring schedule per channel of a Durable Object whose
143
126
  * ticks start individually tracked, overlapping runs and report each run's
@@ -259,6 +242,39 @@ export class DurabilitySchedule {
259
242
  __classPrivateFieldGet(this, _DurabilitySchedule_instances, "m", _DurabilitySchedule_wake).call(this);
260
243
  return applied;
261
244
  }
245
+ /**
246
+ * Recomputes the stored next tick with `nextOccurrence`, for when its answers
247
+ * changed for a reason durability can't see, and resolves `true` when the
248
+ * stored next tick changed. Resolves `false` when it didn't, when no schedule
249
+ * is live, or when the stored tick is already due: the alarm fires it, and
250
+ * the advance after it asks `nextOccurrence` again. The search starts from
251
+ * `now - jitterMs`, so a tick whose firing time is still ahead is kept and
252
+ * one whose firing time passed is skipped, not run. The version, the stored
253
+ * jitter, cancellations, and runs are unchanged.
254
+ */
255
+ async reschedule() {
256
+ const changed = await __classPrivateFieldGet(this, _DurabilitySchedule_engine, "f").transaction((sql) => {
257
+ const schedule = readSchedule(sql, __classPrivateFieldGet(this, _DurabilitySchedule_channel, "f"));
258
+ if (schedule === undefined || schedule.expression === null) {
259
+ return false;
260
+ }
261
+ const now = Date.now();
262
+ const dueAt = firesAt(schedule);
263
+ if (dueAt !== undefined && dueAt <= now) {
264
+ return false;
265
+ }
266
+ const nextAt = __classPrivateFieldGet(this, _DurabilitySchedule_instances, "m", _DurabilitySchedule_nextAfter).call(this, schedule.expression, now - schedule.jitter_ms) ?? null;
267
+ if (nextAt === schedule.next_at) {
268
+ return false;
269
+ }
270
+ sql.exec('UPDATE durability_schedule SET next_at = ? WHERE channel = ? AND id = 1', nextAt, __classPrivateFieldGet(this, _DurabilitySchedule_channel, "f"));
271
+ return true;
272
+ });
273
+ if (changed) {
274
+ __classPrivateFieldGet(this, _DurabilitySchedule_instances, "m", _DurabilitySchedule_wake).call(this);
275
+ }
276
+ return changed;
277
+ }
262
278
  /**
263
279
  * Stops future ticks and resolves `true` when a schedule was live. Runs
264
280
  * already due or running still finish and report. With `version`, the
@@ -291,7 +307,7 @@ export class DurabilitySchedule {
291
307
  }
292
308
  }
293
309
  _DurabilitySchedule_engine = new WeakMap(), _DurabilitySchedule_channel = new WeakMap(), _DurabilitySchedule_nextOccurrence = new WeakMap(), _DurabilitySchedule_run = new WeakMap(), _DurabilitySchedule_onOutcome = new WeakMap(), _DurabilitySchedule_maxConcurrentRuns = new WeakMap(), _DurabilitySchedule_runTimeoutMs = new WeakMap(), _DurabilitySchedule_outcomeTimeoutMs = new WeakMap(), _DurabilitySchedule_maxOutcomeAttempts = new WeakMap(), _DurabilitySchedule_delay = new WeakMap(), _DurabilitySchedule_keyPrefix = new WeakMap(), _DurabilitySchedule_keySuffix = new WeakMap(), _DurabilitySchedule_emit = new WeakMap(), _DurabilitySchedule_active = new WeakMap(), _DurabilitySchedule_delivering = new WeakMap(), _DurabilitySchedule_loop = new WeakMap(), _DurabilitySchedule_woken = new WeakMap(), _DurabilitySchedule_wakeSleeper = new WeakMap(), _DurabilitySchedule_instances = new WeakSet(), _DurabilitySchedule_nextPendingAt = function _DurabilitySchedule_nextPendingAt(sql) {
294
- const rows = timedRowSchema.array().parse(sql
310
+ const rows = v.parse(v.array(timedRowSchema), sql
295
311
  .exec(`SELECT scheduled_time, next_at FROM durability_schedule_runs
296
312
  WHERE channel = ?`, __classPrivateFieldGet(this, _DurabilitySchedule_channel, "f"))
297
313
  .toArray());
@@ -382,27 +398,23 @@ _DurabilitySchedule_engine = new WeakMap(), _DurabilitySchedule_channel = new We
382
398
  if (__classPrivateFieldGet(this, _DurabilitySchedule_woken, "f")) {
383
399
  return;
384
400
  }
385
- let timer;
401
+ let cancelTimeout;
386
402
  try {
387
403
  await new Promise((resolve) => {
388
404
  __classPrivateFieldSet(this, _DurabilitySchedule_wakeSleeper, resolve, "f");
389
405
  if (until !== undefined) {
390
- const delay = Math.min(Math.max(until - Date.now(), 0), maxDelayMs);
391
- timer = setTimeout(resolve, delay);
406
+ cancelTimeout = __classPrivateFieldGet(this, _DurabilitySchedule_engine, "f").timer.schedule(resolve, until - Date.now());
392
407
  }
393
408
  });
394
409
  }
395
410
  finally {
396
- if (timer !== undefined) {
397
- clearTimeout(timer);
398
- }
411
+ cancelTimeout?.();
399
412
  __classPrivateFieldSet(this, _DurabilitySchedule_wakeSleeper, undefined, "f");
400
413
  }
401
414
  }, _DurabilitySchedule_nextWakeAt = function _DurabilitySchedule_nextWakeAt() {
402
415
  const sql = __classPrivateFieldGet(this, _DurabilitySchedule_engine, "f").sql;
403
- const times = runRowSchema
404
- .array()
405
- .parse(sql
416
+ const times = v
417
+ .parse(v.array(runRowSchema), sql
406
418
  .exec(`SELECT * FROM durability_schedule_runs
407
419
  WHERE channel = ? AND state IN ('complete', 'errored')`, __classPrivateFieldGet(this, _DurabilitySchedule_channel, "f"))
408
420
  .toArray())
@@ -463,7 +475,7 @@ async function _DurabilitySchedule_interruptOrphans() {
463
475
  const nextAt = __classPrivateFieldGet(this, _DurabilitySchedule_instances, "m", _DurabilitySchedule_nextAfter).call(this, schedule.expression, Math.max(scheduledTime, now - schedule.jitter_ms));
464
476
  sql.exec('UPDATE durability_schedule SET next_at = ? WHERE channel = ? AND id = 1', nextAt ?? null, __classPrivateFieldGet(this, _DurabilitySchedule_channel, "f"));
465
477
  const queued = free <= 0;
466
- const [row] = dueRowSchema.array().parse(sql
478
+ const [row] = v.parse(v.array(dueRowSchema), sql
467
479
  .exec(`INSERT INTO durability_schedule_runs
468
480
  (channel, scheduled_time, due_at, state, next_at)
469
481
  VALUES (?, ?, ?, ?, ?)
@@ -504,7 +516,7 @@ async function _DurabilitySchedule_interruptOrphans() {
504
516
  const started = await __classPrivateFieldGet(this, _DurabilitySchedule_instances, "m", _DurabilitySchedule_reserveSlots).call(this, (sql, free) => {
505
517
  const rows = free <= 0
506
518
  ? []
507
- : dueRowSchema.array().parse(sql
519
+ : v.parse(v.array(dueRowSchema), sql
508
520
  .exec(`UPDATE durability_schedule_runs
509
521
  SET state = 'running', next_at = ?
510
522
  WHERE channel = ? AND scheduled_time IN (
@@ -554,7 +566,8 @@ async function _DurabilitySchedule_reserveSlots(mark) {
554
566
  __classPrivateFieldGet(this, _DurabilitySchedule_emit, "f").call(this, { ...__classPrivateFieldGet(this, _DurabilitySchedule_instances, "m", _DurabilitySchedule_entity).call(this, scheduledTime), type: 'started' });
555
567
  const controller = new AbortController();
556
568
  const handler = Promise.resolve().then(() => __classPrivateFieldGet(this, _DurabilitySchedule_run, "f").call(this, { ...run, signal: controller.signal }));
557
- const recorded = deadline(handler, __classPrivateFieldGet(this, _DurabilitySchedule_runTimeoutMs, "f"), () => {
569
+ const recorded = __classPrivateFieldGet(this, _DurabilitySchedule_engine, "f").timer
570
+ .withTimeout(handler, __classPrivateFieldGet(this, _DurabilitySchedule_runTimeoutMs, "f"), () => {
558
571
  const error = new ScheduledRunTimeoutError(__classPrivateFieldGet(this, _DurabilitySchedule_runTimeoutMs, "f"));
559
572
  controller.abort(error);
560
573
  return error;
@@ -593,7 +606,7 @@ async function _DurabilitySchedule_reserveSlots(mark) {
593
606
  });
594
607
  __classPrivateFieldGet(this, _DurabilitySchedule_instances, "m", _DurabilitySchedule_deliver).call(this, run, outcome, 1);
595
608
  }, _DurabilitySchedule_deliverDue = function _DurabilitySchedule_deliverDue() {
596
- const due = runRowSchema.array().parse(__classPrivateFieldGet(this, _DurabilitySchedule_engine, "f").sql
609
+ const due = v.parse(v.array(runRowSchema), __classPrivateFieldGet(this, _DurabilitySchedule_engine, "f").sql
597
610
  .exec(`SELECT * FROM durability_schedule_runs
598
611
  WHERE channel = ? AND state IN ('complete', 'errored') AND next_at <= ?`, __classPrivateFieldGet(this, _DurabilitySchedule_channel, "f"), Date.now())
599
612
  .toArray());
@@ -614,7 +627,7 @@ async function _DurabilitySchedule_reserveSlots(mark) {
614
627
  });
615
628
  }, _DurabilitySchedule_deliverOnce = async function _DurabilitySchedule_deliverOnce(run, outcome, attempt) {
616
629
  try {
617
- await deadline(Promise.resolve().then(() => __classPrivateFieldGet(this, _DurabilitySchedule_onOutcome, "f").call(this, { ...run, attempt }, outcome)), __classPrivateFieldGet(this, _DurabilitySchedule_outcomeTimeoutMs, "f"), () => new ScheduledOutcomeTimeoutError(__classPrivateFieldGet(this, _DurabilitySchedule_outcomeTimeoutMs, "f")));
630
+ await __classPrivateFieldGet(this, _DurabilitySchedule_engine, "f").timer.withTimeout(Promise.resolve().then(() => __classPrivateFieldGet(this, _DurabilitySchedule_onOutcome, "f").call(this, { ...run, attempt }, outcome)), __classPrivateFieldGet(this, _DurabilitySchedule_outcomeTimeoutMs, "f"), () => new ScheduledOutcomeTimeoutError(__classPrivateFieldGet(this, _DurabilitySchedule_outcomeTimeoutMs, "f")));
618
631
  }
619
632
  catch (failure) {
620
633
  await __classPrivateFieldGet(this, _DurabilitySchedule_instances, "m", _DurabilitySchedule_failDelivery).call(this, run, attempt, failure);
@@ -2,6 +2,7 @@ import { type SerializedError } from '../errors.js';
2
2
  import { type DurabilityLifecycleEvent, type Emit, type LifecycleEntity } from '../lifecycle.js';
3
3
  import { type MigrationCapability } from '../migrations.js';
4
4
  import { type AttemptPolicy } from '../policy.js';
5
+ import { SharedTimer } from '../shared-timer.js';
5
6
  import { type DurableTable } from '../storage.js';
6
7
  import type { DurableMigrations } from '@durability/storage';
7
8
  /** The Durable Object state durability needs: storage plus optional `waitUntil`. */
@@ -132,6 +133,8 @@ export type ExecutionSpec = {
132
133
  */
133
134
  export declare class Engine {
134
135
  readonly sql: SqlStorage;
136
+ /** Holds the timeouts of every helper on the object behind one native timer. */
137
+ readonly timer: SharedTimer;
135
138
  private context;
136
139
  private readonly storage;
137
140
  private alarmConcurrency;
@@ -250,7 +253,10 @@ export declare class Engine {
250
253
  /**
251
254
  * Registers the execution in its active map synchronously, then claims and
252
255
  * runs one attempt in the background. The returned promise settles when the
253
- * attempt's outcome has been persisted, not when a timed-out handler settles.
256
+ * attempt's outcome has been persisted. The key leaves the active map and
257
+ * its permit is released then too, whether or not a timed-out handler has
258
+ * settled: nothing can stop a handler, and one that never settles would
259
+ * otherwise keep its retry from ever starting.
254
260
  */
255
261
  startExecution(spec: ExecutionSpec, eagerPermit?: () => void): Promise<void>;
256
262
  private failExhausted;
@@ -289,6 +295,18 @@ export declare class DurabilityScheduler {
289
295
  constructor(config: DurabilitySchedulerConfig);
290
296
  /** Runs due work for every attached helper and re-arms the physical alarm. */
291
297
  alarm(info?: AlarmInvocationInfo): Promise<void>;
298
+ /**
299
+ * Runs `callback` once after `delayMs`, like the global `setTimeout`, and
300
+ * returns a function that cancels it. The timeout shares one native timer
301
+ * with every helper on the object, so any number of them can be pending
302
+ * without reaching the runtime's limit of 10,000 native timers.
303
+ *
304
+ * The timeout exists only in memory and an eviction drops it; schedule work
305
+ * that must survive one with `DurabilityAlarms`. Callbacks due at the same
306
+ * time run one after another in the same task, and one that throws is
307
+ * reported as `durability.timer.callback_failed` instead of being thrown.
308
+ */
309
+ setTimeout(callback: () => void, delayMs: number): () => void;
292
310
  }
293
311
  /** Resolves the engine a helper attaches to: the object's one scheduler. */
294
312
  export declare const engineFor: (attachment: SchedulerAttachment) => Engine;