@dudousxd/nestjs-durable-store-drizzle 0.24.7 → 0.26.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/pg/index.js CHANGED
@@ -102,6 +102,12 @@ var DURABLE_PG_DDL = [
102
102
  "name" text NOT NULL,
103
103
  "payload" jsonb,
104
104
  "published_at" timestamp with time zone NOT NULL
105
+ )`,
106
+ `CREATE TABLE IF NOT EXISTS "durable_signal_ids" (
107
+ "token" text NOT NULL,
108
+ "signal_id" text NOT NULL,
109
+ "expires_at" timestamp with time zone NOT NULL,
110
+ CONSTRAINT "durable_signal_ids_token_signal_id_pk" PRIMARY KEY("token","signal_id")
105
111
  )`,
106
112
  `CREATE TABLE IF NOT EXISTS "durable_schedules" (
107
113
  "id" text PRIMARY KEY NOT NULL,
@@ -126,7 +132,8 @@ var DURABLE_PG_DDL = [
126
132
  `CREATE INDEX IF NOT EXISTS "durable_signal_waiters_run_id_idx" ON "durable_signal_waiters" USING btree ("run_id")`,
127
133
  `CREATE INDEX IF NOT EXISTS "durable_buffered_signals_token_idx" ON "durable_buffered_signals" USING btree ("token","id")`,
128
134
  `CREATE INDEX IF NOT EXISTS "durable_buffered_events_name_published_at_idx" ON "durable_buffered_events" USING btree ("name","published_at")`,
129
- `CREATE INDEX IF NOT EXISTS "durable_schedules_due_idx" ON "durable_schedules" USING btree ("paused","next_fire_at")`
135
+ `CREATE INDEX IF NOT EXISTS "durable_schedules_due_idx" ON "durable_schedules" USING btree ("paused","next_fire_at")`,
136
+ `CREATE INDEX IF NOT EXISTS "durable_signal_ids_expires_at_idx" ON "durable_signal_ids" USING btree ("expires_at")`
130
137
  ];
131
138
  var SCHEMA_LOCK_KEY = "durable_schema";
132
139
  function drizzlePgSchemaRepairWarning(repairs) {
@@ -198,8 +205,120 @@ async function ensureDrizzlePgDurableSchema(db, options = {}) {
198
205
  __name(ensureDrizzlePgDurableSchema, "ensureDrizzlePgDurableSchema");
199
206
 
200
207
  // src/pg/drizzle-pg-state-store.ts
201
- import { RUN_VALUE_FACET_SCAN, SINGLETON_ACTIVE_STATUSES, TERMINAL_RUN_STATUSES, attributePredicateOperands, axisIsRunColumn, clearTerminalSingletonAdmission, mergeRunFacetRows, mergeRunValueFacetRows, normalizeAttributeRows, parseDuration, runValueFacetsFromRuns, singletonAdmissionDenialPatch, singletonAdmissionTags } from "@dudousxd/nestjs-durable-core";
202
- import { and, asc, desc, eq, exists, getTableColumns, gt, gte, inArray, isNotNull, isNull, like, lt, lte, notInArray, or, sql as sql3 } from "drizzle-orm";
208
+ import { LIFECYCLE_REGISTRY_WORKFLOW, RUN_VALUE_FACET_SCAN, SINGLETON_ACTIVE_STATUSES, TERMINAL_RUN_STATUSES, attributePredicateOperands, axisIsRunColumn, clearTerminalSingletonAdmission, mergeRunFacetRows, mergeRunValueFacetRows, normalizeAttributeRows, parseDuration, retentionKeeps, runValueFacetsFromRuns, singletonAdmissionDenialPatch, singletonAdmissionTags } from "@dudousxd/nestjs-durable-core";
209
+ import { and, asc, desc, eq, exists, getTableColumns as getTableColumns2, getTableName as getTableName2, gt, gte, inArray, isNotNull, isNull, like, lt, lte, notInArray, or, sql as sql3 } from "drizzle-orm";
210
+
211
+ // src/pg/hot-path.ts
212
+ import * as drizzleOrm from "drizzle-orm";
213
+ import { getTableColumns } from "drizzle-orm";
214
+ function nodePgClient(db) {
215
+ const client = db.$client;
216
+ if (!client || typeof client !== "object") return void 0;
217
+ if (typeof client.query !== "function") return void 0;
218
+ const isPool = "totalCount" in client && "idleCount" in client;
219
+ const isClient = "connectionParameters" in client;
220
+ return isPool || isClient ? client : void 0;
221
+ }
222
+ __name(nodePgClient, "nodePgClient");
223
+ function drizzleLogger(db) {
224
+ const logger = db.session?.logger;
225
+ if (!logger || typeof logger.logQuery !== "function") return void 0;
226
+ if (logger.constructor?.name === "NoopLogger") {
227
+ return void 0;
228
+ }
229
+ return logger;
230
+ }
231
+ __name(drizzleLogger, "drizzleLogger");
232
+ var QueryError = drizzleOrm.DrizzleQueryError;
233
+ function hash(text2) {
234
+ let h = 2166136261;
235
+ for (let i = 0; i < text2.length; i += 1) {
236
+ h ^= text2.charCodeAt(i);
237
+ h = Math.imul(h, 16777619);
238
+ }
239
+ return (h >>> 0).toString(36);
240
+ }
241
+ __name(hash, "hash");
242
+ var PgHotPath = class _PgHotPath {
243
+ static {
244
+ __name(this, "PgHotPath");
245
+ }
246
+ client;
247
+ logger;
248
+ prepared;
249
+ constructor(client, logger, prepared) {
250
+ this.client = client;
251
+ this.logger = logger;
252
+ this.prepared = prepared;
253
+ }
254
+ /** The hot path for `db`, or `undefined` when `db` does not run on node-postgres. */
255
+ static for(db, options = {}) {
256
+ const client = nodePgClient(db);
257
+ if (!client) return void 0;
258
+ return new _PgHotPath(client, drizzleLogger(db), options.preparedStatements !== false);
259
+ }
260
+ /** Run one statement; resolves its rows as the driver decoded them (column names as stored). */
261
+ async query(text2, values) {
262
+ this.logger?.logQuery(text2, values);
263
+ const config = {
264
+ text: text2,
265
+ values
266
+ };
267
+ if (this.prepared) config.name = `durable_${hash(text2)}`;
268
+ try {
269
+ return (await this.client.query(config)).rows;
270
+ } catch (err) {
271
+ throw QueryError ? new QueryError(text2, values, err) : err;
272
+ }
273
+ }
274
+ };
275
+ var TableCodec = class {
276
+ static {
277
+ __name(this, "TableCodec");
278
+ }
279
+ columns;
280
+ /** `"a", "b", …` in column order. */
281
+ list;
282
+ constructor(table, omit = []) {
283
+ this.columns = Object.entries(getTableColumns(table)).filter(([key]) => !omit.includes(key));
284
+ this.list = this.columns.map(([, c]) => quote(c.name)).join(", ");
285
+ }
286
+ /** The quoted column name of `property`. */
287
+ name(property) {
288
+ const found = this.columns.find(([key]) => key === property);
289
+ if (!found) throw new Error(`no column for ${property}`);
290
+ return quote(found[1].name);
291
+ }
292
+ /** A value of `property` as drizzle's builder would bind it. */
293
+ encode(property, value) {
294
+ if (value == null) return null;
295
+ const found = this.columns.find(([key]) => key === property);
296
+ return found ? found[1].mapToDriverValue(value) : value;
297
+ }
298
+ /** A row's values, in column order, as drizzle's builder would bind them. */
299
+ encodeRow(row) {
300
+ return this.columns.map(([key, column]) => {
301
+ const value = row[key];
302
+ return value == null ? null : column.mapToDriverValue(value);
303
+ });
304
+ }
305
+ /** A row a statement returned, as drizzle would map it (absent / omitted columns read `null`). */
306
+ decode(raw) {
307
+ const row = {};
308
+ for (const [key, column] of this.columns) {
309
+ const value = raw[column.name];
310
+ row[key] = value == null ? null : column.mapFromDriverValue(value);
311
+ }
312
+ return row;
313
+ }
314
+ };
315
+ var quote = /* @__PURE__ */ __name((name) => `"${name.replace(/"/g, '""')}"`, "quote");
316
+ function placeholders(from, count) {
317
+ return Array.from({
318
+ length: count
319
+ }, (_, i) => `$${from + i}`).join(", ");
320
+ }
321
+ __name(placeholders, "placeholders");
203
322
 
204
323
  // src/pg/schema.ts
205
324
  import { getTableName } from "drizzle-orm";
@@ -332,6 +451,19 @@ var durableBufferedEvents = pgTable("durable_buffered_events", {
332
451
  }, (t) => [
333
452
  index("durable_buffered_events_name_published_at_idx").on(t.name, t.publishedAt)
334
453
  ]);
454
+ var durableSignalIds = pgTable("durable_signal_ids", {
455
+ token: text("token").notNull(),
456
+ signalId: text("signal_id").notNull(),
457
+ expiresAt: tstz("expires_at").notNull()
458
+ }, (t) => [
459
+ primaryKey({
460
+ columns: [
461
+ t.token,
462
+ t.signalId
463
+ ]
464
+ }),
465
+ index("durable_signal_ids_expires_at_idx").on(t.expiresAt)
466
+ ]);
335
467
  var durableSchedules = pgTable(
336
468
  "durable_schedules",
337
469
  {
@@ -358,6 +490,7 @@ var durablePgSchema = {
358
490
  durableSignalWaiters,
359
491
  durableBufferedSignals,
360
492
  durableBufferedEvents,
493
+ durableSignalIds,
361
494
  durableSchedules
362
495
  };
363
496
  function durablePgManagedTables() {
@@ -366,6 +499,9 @@ function durablePgManagedTables() {
366
499
  __name(durablePgManagedTables, "durablePgManagedTables");
367
500
 
368
501
  // src/pg/drizzle-pg-state-store.ts
502
+ var RETENTION_EXCLUDED = [
503
+ LIFECYCLE_REGISTRY_WORKFLOW
504
+ ];
369
505
  function anyOf(conditions) {
370
506
  const present = conditions.filter((c) => c !== void 0);
371
507
  return present.length ? or(...present) : sql3`1 = 0`;
@@ -397,10 +533,59 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
397
533
  db;
398
534
  options;
399
535
  scopeNamespace;
536
+ /** Constant-SQL path for the per-step statements, on node-postgres (see `hot-path.ts`). */
537
+ hot;
538
+ /** The run read-scope predicate as SQL text from `$2` on, and its values (hot path). */
539
+ hotScope;
540
+ /** Hot-path UPDATE texts by patch shape. */
541
+ hotUpdates = /* @__PURE__ */ new Map();
400
542
  constructor(db, options = {}) {
401
543
  this.db = db;
402
544
  this.options = options;
403
545
  this.scopeNamespace = options.scope?.namespace;
546
+ this.hot = PgHotPath.for(db, {
547
+ preparedStatements: options.preparedStatements
548
+ });
549
+ const ns = this.scopeNamespace;
550
+ this.hotScope = ns === void 0 ? {
551
+ text: "",
552
+ values: []
553
+ } : ns === DEFAULT_NAMESPACE || ns === "" ? {
554
+ text: ' AND "namespace" IN ($2, $3)',
555
+ values: [
556
+ DEFAULT_NAMESPACE,
557
+ ""
558
+ ]
559
+ } : {
560
+ text: ' AND "namespace" = $2',
561
+ values: [
562
+ ns
563
+ ]
564
+ };
565
+ }
566
+ /**
567
+ * A hot-path `UPDATE` of the runs table setting `row`'s columns on run `$n`, optionally only while
568
+ * its status is one of `expected` (then `RETURNING "id"`). Text cached per shape.
569
+ */
570
+ hotRunUpdate(runId, row, expected) {
571
+ const keys = Object.keys(row);
572
+ const shape = `${keys.join(",")}|${expected?.length ?? -1}`;
573
+ let text2 = this.hotUpdates.get(shape);
574
+ if (text2 === void 0) {
575
+ const set = keys.map((key, i) => `${RUNS.name(key)} = $${i + 1}`).join(", ");
576
+ const id = keys.length + 1;
577
+ text2 = `UPDATE ${T_RUNS} SET ${set} WHERE "id" = $${id}`;
578
+ if (expected) {
579
+ text2 += ` AND "status" IN (${placeholders(id + 1, expected.length)}) RETURNING "id"`;
580
+ }
581
+ this.hotUpdates.set(shape, text2);
582
+ }
583
+ const values = keys.map((key) => RUNS.encode(key, row[key]));
584
+ values.push(runId, ...expected ?? []);
585
+ return {
586
+ text: text2,
587
+ values
588
+ };
404
589
  }
405
590
  /** A store confined to `scope.namespace`, sharing this store's db (no new connection). Used by the
406
591
  * NestJS module's `scopeReads`; `{ namespace: undefined }` is the unscoped operator view. */
@@ -443,6 +628,13 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
443
628
  }
444
629
  async createRun(run) {
445
630
  if (normalizeAttributeRows(run.id, run.searchAttributes).length === 0) {
631
+ if (this.hot) {
632
+ await this.hot.query(INSERT_RUN, RUNS.encodeRow(toRunRow(run)));
633
+ await this.hot.query(DELETE_ATTRIBUTES, [
634
+ run.id
635
+ ]);
636
+ return;
637
+ }
446
638
  await this.db.insert(durableWorkflowRuns).values(toRunRow(run));
447
639
  await this.db.delete(durableRunAttributes).where(eq(durableRunAttributes.runId, run.id));
448
640
  return;
@@ -458,6 +650,11 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
458
650
  const reindex = "searchAttributes" in patch;
459
651
  if (!Object.keys(row).length && !reindex) return;
460
652
  if (!reindex) {
653
+ if (this.hot) {
654
+ const { text: text2, values } = this.hotRunUpdate(runId, row);
655
+ await this.hot.query(text2, values);
656
+ return;
657
+ }
461
658
  await this.db.update(durableWorkflowRuns).set(row).where(eq(durableWorkflowRuns.id, runId));
462
659
  return;
463
660
  }
@@ -472,6 +669,16 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
472
669
  const row = toRunPatch(patch);
473
670
  const reindex = "searchAttributes" in patch;
474
671
  const matches = and(eq(durableWorkflowRuns.id, runId), inArray(durableWorkflowRuns.status, expectedStatuses));
672
+ if (!reindex && Object.keys(row).length) {
673
+ if (this.hot) {
674
+ const { text: text2, values } = this.hotRunUpdate(runId, row, expectedStatuses);
675
+ return (await this.hot.query(text2, values)).length === 1;
676
+ }
677
+ const applied = await this.db.update(durableWorkflowRuns).set(row).where(matches).returning({
678
+ id: durableWorkflowRuns.id
679
+ });
680
+ return applied.length === 1;
681
+ }
475
682
  return this.db.transaction(async (tx) => {
476
683
  const applied = Object.keys(row).length ? (await tx.update(durableWorkflowRuns).set(row).where(matches).returning({
477
684
  id: durableWorkflowRuns.id
@@ -483,6 +690,13 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
483
690
  });
484
691
  }
485
692
  async getRun(runId) {
693
+ if (this.hot) {
694
+ const rows2 = await this.hot.query(`${SELECT_RUN}${this.hotScope.text} LIMIT 1`, [
695
+ runId,
696
+ ...this.hotScope.values
697
+ ]);
698
+ return rows2[0] ? fromRunRow(RUNS.decode(rows2[0])) : null;
699
+ }
486
700
  const rows = await this.db.select().from(durableWorkflowRuns).where(and(eq(durableWorkflowRuns.id, runId), this.scope())).limit(1);
487
701
  return rows[0] ? fromRunRow(rows[0]) : null;
488
702
  }
@@ -512,28 +726,31 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
512
726
  if (policy.maxAge == null && policy.maxCount == null) return 0;
513
727
  const scoped = this.runFilters({
514
728
  ...policy.scope ?? {},
515
- statuses
729
+ statuses,
730
+ excludeWorkflows: RETENTION_EXCLUDED
516
731
  });
517
732
  return this.db.transaction(async (tx) => {
518
733
  const ids = /* @__PURE__ */ new Set();
519
734
  if (policy.maxAge != null) {
520
735
  const cutoff = new Date(nowMs - parseDuration(policy.maxAge));
521
736
  const rows = await tx.select({
522
- id: durableWorkflowRuns.id
737
+ id: durableWorkflowRuns.id,
738
+ tags: durableWorkflowRuns.tags
523
739
  }).from(durableWorkflowRuns).where(and(...scoped, lt(durableWorkflowRuns.updatedAt, cutoff))).orderBy(asc(durableWorkflowRuns.updatedAt)).limit(limit).for("update", {
524
740
  skipLocked: true
525
741
  });
526
- for (const r of rows) ids.add(r.id);
742
+ for (const r of rows) if (!retentionKeeps(r.tags)) ids.add(r.id);
527
743
  }
528
744
  if (policy.maxCount != null && ids.size < limit) {
529
745
  const rows = await tx.select({
530
- id: durableWorkflowRuns.id
746
+ id: durableWorkflowRuns.id,
747
+ tags: durableWorkflowRuns.tags
531
748
  }).from(durableWorkflowRuns).where(and(...scoped)).orderBy(desc(durableWorkflowRuns.updatedAt), desc(durableWorkflowRuns.id)).limit(limit).offset(policy.maxCount).for("update", {
532
749
  skipLocked: true
533
750
  });
534
751
  for (const r of rows) {
535
752
  if (ids.size >= limit) break;
536
- ids.add(r.id);
753
+ if (!retentionKeeps(r.tags)) ids.add(r.id);
537
754
  }
538
755
  }
539
756
  if (ids.size === 0) return 0;
@@ -545,6 +762,13 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
545
762
  });
546
763
  }
547
764
  async getCheckpoint(runId, seq) {
765
+ if (this.hot) {
766
+ const rows2 = await this.hot.query(SELECT_CHECKPOINT, [
767
+ runId,
768
+ seq
769
+ ]);
770
+ return rows2[0] ? fromCheckpointRow(CPS.decode(rows2[0])) : null;
771
+ }
548
772
  const rows = await this.db.select().from(durableStepCheckpoints).where(and(eq(durableStepCheckpoints.runId, runId), eq(durableStepCheckpoints.seq, seq))).limit(1);
549
773
  return rows[0] ? fromCheckpointRow(rows[0]) : null;
550
774
  }
@@ -562,7 +786,15 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
562
786
  const unique = [
563
787
  ...new Set(seqs)
564
788
  ];
565
- const { input: _input, ...columns } = getTableColumns(durableStepCheckpoints);
789
+ if (this.hot) {
790
+ if (unique.length === 0) return [];
791
+ const rows2 = await this.hot.query(SELECT_CHECKPOINTS_FOR_SETTLE, [
792
+ runId,
793
+ unique
794
+ ]);
795
+ return rows2.map((raw) => fromCheckpointRow(CPS_NO_INPUT.decode(raw)));
796
+ }
797
+ const { input: _input, ...columns } = getTableColumns2(durableStepCheckpoints);
566
798
  const rows = [];
567
799
  for (let at = 0; at < unique.length; at += 500) {
568
800
  rows.push(...await this.db.select(columns).from(durableStepCheckpoints).where(and(eq(durableStepCheckpoints.runId, runId), inArray(durableStepCheckpoints.seq, unique.slice(at, at + 500)))));
@@ -573,6 +805,10 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
573
805
  }));
574
806
  }
575
807
  async saveCheckpoint(checkpoint) {
808
+ if (this.hot) {
809
+ await this.hot.query(UPSERT_CHECKPOINT, CPS.encodeRow(toCheckpointRow(checkpoint)));
810
+ return;
811
+ }
576
812
  await upsertCheckpoint(this.db, checkpoint);
577
813
  }
578
814
  async saveCheckpoints(checkpoints) {
@@ -599,7 +835,7 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
599
835
  async saveCheckpointsIfPending(checkpoints) {
600
836
  const wrote = checkpoints.map(() => false);
601
837
  if (checkpoints.length === 0) return wrote;
602
- await this.db.transaction(async (tx) => {
838
+ await this.inTransactionIf(oneStatement(checkpoints), async (tx) => {
603
839
  for (const round of distinctRounds(checkpoints)) {
604
840
  for (let at = 0; at < round.length; at += CHECKPOINT_BATCH_ROWS) {
605
841
  const chunk = round.slice(at, at + CHECKPOINT_BATCH_ROWS);
@@ -623,7 +859,7 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
623
859
  async settleCheckpointsIfPending(settlements) {
624
860
  const wrote = settlements.map(() => false);
625
861
  if (settlements.length === 0) return wrote;
626
- await this.db.transaction(async (tx) => {
862
+ await this.inTransactionIf(oneStatement(settlements), async (tx) => {
627
863
  for (const round of distinctRounds(settlements)) {
628
864
  for (let at = 0; at < round.length; at += CHECKPOINT_BATCH_ROWS) {
629
865
  const chunk = round.slice(at, at + CHECKPOINT_BATCH_ROWS);
@@ -644,6 +880,84 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
644
880
  });
645
881
  return wrote;
646
882
  }
883
+ async settleResultsIfPending(results) {
884
+ const out = results.map(() => null);
885
+ const seen = /* @__PURE__ */ new Set();
886
+ const sent = results.flatMap((r, i) => {
887
+ const key = rowKey(r);
888
+ if (seen.has(key)) return [];
889
+ seen.add(key);
890
+ return [
891
+ i
892
+ ];
893
+ });
894
+ if (sent.length === 0) return out;
895
+ if (this.hot && sent.length <= HOT_SETTLE_MAX_ROWS) {
896
+ const rows = await this.hot.query(settleResultsText(sent.length), sent.flatMap((i) => settleResultValues(results[i])));
897
+ const byKey = new Map(sent.map((i) => [
898
+ rowKey(results[i]),
899
+ i
900
+ ]));
901
+ for (const raw of rows) {
902
+ const cp = fromCheckpointRow(CPS_NO_INPUT.decode(raw));
903
+ const i = byKey.get(rowKey(cp));
904
+ if (i !== void 0) out[i] = cp;
905
+ }
906
+ return out;
907
+ }
908
+ await this.inTransactionIf(sent.length <= CHECKPOINT_BATCH_ROWS, async (tx) => {
909
+ for (let at = 0; at < sent.length; at += CHECKPOINT_BATCH_ROWS) {
910
+ const chunk = sent.slice(at, at + CHECKPOINT_BATCH_ROWS);
911
+ const rows = rowsOf(await tx.execute(settleResultsFrom(chunk.map((i) => results[i]))));
912
+ const byKey = new Map(chunk.map((i) => [
913
+ rowKey(results[i]),
914
+ i
915
+ ]));
916
+ for (const raw of rows) {
917
+ const cp = fromCheckpointRow(settledRowFromDriver(raw));
918
+ const i = byKey.get(rowKey(cp));
919
+ if (i !== void 0) out[i] = cp;
920
+ }
921
+ }
922
+ });
923
+ return out;
924
+ }
925
+ async insertCheckpointIfAbsent(checkpoint) {
926
+ if (this.hot) {
927
+ const values = CPS.encodeRow(toCheckpointRow(checkpoint));
928
+ return (await this.hot.query(INSERT_CHECKPOINT_IF_ABSENT, values)).length === 1;
929
+ }
930
+ const rows = await this.db.insert(durableStepCheckpoints).values(toCheckpointRow(checkpoint)).onConflictDoNothing({
931
+ target: [
932
+ durableStepCheckpoints.runId,
933
+ durableStepCheckpoints.seq
934
+ ]
935
+ }).returning({
936
+ seq: durableStepCheckpoints.seq
937
+ });
938
+ return rows.length === 1;
939
+ }
940
+ async tryLockRunAndGet(runId, owner, leaseUntilMs, nowMs) {
941
+ if (this.hot) {
942
+ const rows2 = await this.hot.query(`${LOCK_RUN}${this.hotScope.text.replace(/\$(\d+)/g, (_, n) => `$${Number(n) + 3}`)} RETURNING ${RUNS.list}`, [
943
+ runId,
944
+ owner,
945
+ lockTime(leaseUntilMs),
946
+ lockTime(nowMs),
947
+ ...this.hotScope.values
948
+ ]);
949
+ return rows2[0] ? fromRunRow(RUNS.decode(rows2[0])) : null;
950
+ }
951
+ const rows = await this.db.update(durableWorkflowRuns).set({
952
+ lockedBy: owner,
953
+ lockedUntil: new Date(leaseUntilMs)
954
+ }).where(and(eq(durableWorkflowRuns.id, runId), or(isNull(durableWorkflowRuns.lockedUntil), lte(durableWorkflowRuns.lockedUntil, new Date(nowMs))), this.scope())).returning();
955
+ return rows[0] ? fromRunRow(rows[0]) : null;
956
+ }
957
+ /** Run `work` on the db as is when it is one statement (atomic by itself), else in a transaction. */
958
+ inTransactionIf(single, work) {
959
+ return single ? work(this.db) : this.db.transaction((tx) => work(tx));
960
+ }
647
961
  async transaction(work) {
648
962
  return this.db.transaction(async (tx) => work({
649
963
  raw: tx,
@@ -677,12 +991,45 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
677
991
  const rows = await this.db.select().from(durableWorkflowRuns).where(and(eq(durableWorkflowRuns.status, "pending"), namespaceFilter(namespace), this.scope())).orderBy(asc(durableWorkflowRuns.createdAt)).limit(limit);
678
992
  return rows.map(fromRunRow);
679
993
  }
994
+ async claimPendingRuns(limit, owner, leaseUntilMs, nowMs, namespace) {
995
+ if (limit <= 0) return [];
996
+ const filters = and(eq(durableWorkflowRuns.status, "pending"), or(isNull(durableWorkflowRuns.lockedUntil), lte(durableWorkflowRuns.lockedUntil, new Date(nowMs))), namespaceFilter(namespace), this.scope());
997
+ const returning = sql3.join(RUNS.columns.map(([, column]) => sql3`r.${sql3.identifier(column.name)}`), sql3`, `);
998
+ const rows = rowsOf(
999
+ // A CTE, MATERIALIZED, picks the rows once. As a subquery in UPDATE ... FROM, Postgres may
1000
+ // rescan it, and each rescan skips the rows this statement already updated and takes the
1001
+ // next ones, so a claim of `limit` rows could lease every free pending run.
1002
+ await this.db.execute(sql3`WITH c AS MATERIALIZED (
1003
+ SELECT ${durableWorkflowRuns.id} AS claimed_id FROM ${durableWorkflowRuns}
1004
+ WHERE ${filters}
1005
+ ORDER BY ${durableWorkflowRuns.createdAt}, ${durableWorkflowRuns.id}
1006
+ LIMIT ${limit}
1007
+ FOR UPDATE SKIP LOCKED
1008
+ )
1009
+ UPDATE ${durableWorkflowRuns} AS r
1010
+ SET ${sql3.identifier(getTableColumns2(durableWorkflowRuns).lockedBy.name)} = ${owner},
1011
+ ${sql3.identifier(getTableColumns2(durableWorkflowRuns).lockedUntil.name)} = ${new Date(leaseUntilMs).toISOString()}::timestamptz
1012
+ FROM c
1013
+ WHERE r.${sql3.identifier(getTableColumns2(durableWorkflowRuns).id.name)} = c.claimed_id
1014
+ RETURNING ${returning}`)
1015
+ );
1016
+ return rows.map((raw) => fromRunRow(RUNS.decode(raw))).sort((a, b) => a.createdAt.getTime() - b.createdAt.getTime() || a.id.localeCompare(b.id));
1017
+ }
680
1018
  async listDueTimers(nowMs, namespace, limit) {
681
1019
  const query = this.db.select().from(durableWorkflowRuns).where(and(eq(durableWorkflowRuns.status, "suspended"), isNotNull(durableWorkflowRuns.wakeAt), lte(durableWorkflowRuns.wakeAt, new Date(nowMs)), namespaceFilter(namespace), this.scope())).orderBy(asc(durableWorkflowRuns.wakeAt));
682
1020
  const rows = limit !== void 0 ? await query.limit(limit) : await query;
683
1021
  return rows.map(fromRunRow);
684
1022
  }
685
1023
  async tryLockRun(runId, owner, leaseUntilMs, nowMs) {
1024
+ if (this.hot) {
1025
+ const rows2 = await this.hot.query(`${LOCK_RUN} RETURNING "id"`, [
1026
+ runId,
1027
+ owner,
1028
+ lockTime(leaseUntilMs),
1029
+ lockTime(nowMs)
1030
+ ]);
1031
+ return rows2.length === 1;
1032
+ }
686
1033
  const rows = await this.db.update(durableWorkflowRuns).set({
687
1034
  lockedBy: owner,
688
1035
  lockedUntil: new Date(leaseUntilMs)
@@ -692,12 +1039,29 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
692
1039
  return rows.length === 1;
693
1040
  }
694
1041
  async releaseRunLock(runId, owner) {
1042
+ if (this.hot) {
1043
+ await (owner === void 0 ? this.hot.query(RELEASE_LOCK, [
1044
+ runId
1045
+ ]) : this.hot.query(`${RELEASE_LOCK} AND "locked_by" = $2`, [
1046
+ runId,
1047
+ owner
1048
+ ]));
1049
+ return;
1050
+ }
695
1051
  await this.db.update(durableWorkflowRuns).set({
696
1052
  lockedBy: null,
697
1053
  lockedUntil: null
698
1054
  }).where(owner === void 0 ? eq(durableWorkflowRuns.id, runId) : and(eq(durableWorkflowRuns.id, runId), eq(durableWorkflowRuns.lockedBy, owner)));
699
1055
  }
700
1056
  async renewRunLock(runId, owner, leaseUntilMs) {
1057
+ if (this.hot) {
1058
+ const rows2 = await this.hot.query(RENEW_LOCK, [
1059
+ runId,
1060
+ owner,
1061
+ lockTime(leaseUntilMs)
1062
+ ]);
1063
+ return rows2.length === 1;
1064
+ }
701
1065
  const rows = await this.db.update(durableWorkflowRuns).set({
702
1066
  lockedUntil: new Date(leaseUntilMs)
703
1067
  }).where(and(eq(durableWorkflowRuns.id, runId), eq(durableWorkflowRuns.lockedBy, owner))).returning({
@@ -711,6 +1075,8 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
711
1075
  this.scope(),
712
1076
  query.workflow ? eq(durableWorkflowRuns.workflow, query.workflow) : void 0,
713
1077
  query.workflows ? query.workflows.length ? inArray(durableWorkflowRuns.workflow, query.workflows) : sql3`1 = 0` : void 0,
1078
+ query.excludeWorkflows?.length ? notInArray(durableWorkflowRuns.workflow, query.excludeWorkflows) : void 0,
1079
+ query.updatedAfter !== void 0 ? gte(durableWorkflowRuns.updatedAt, new Date(query.updatedAfter)) : void 0,
714
1080
  // The tenant boundary. Also the engine's execution-timeout sweep path
715
1081
  // (`listRuns({ workflow, status, namespace })`) — unscoped, it would cancel another tenant's runs.
716
1082
  namespaceFilter(query.namespace),
@@ -824,7 +1190,26 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
824
1190
  return rows.map(fromCheckpointRow);
825
1191
  }
826
1192
  async listCheckpointsForReplay(runId) {
827
- const { input: _input, ...columns } = getTableColumns(durableStepCheckpoints);
1193
+ if (this.hot) {
1194
+ const rows2 = (await this.hot.query(SELECT_REPLAY, [
1195
+ runId
1196
+ ])).map((raw) => CPS_NO_INPUT.decode(raw));
1197
+ const open2 = rows2.filter((row) => row.status !== "completed").map((row) => row.seq);
1198
+ const inputs2 = /* @__PURE__ */ new Map();
1199
+ if (open2.length > 0) {
1200
+ for (const raw of await this.hot.query(SELECT_INPUTS, [
1201
+ runId,
1202
+ open2
1203
+ ])) {
1204
+ inputs2.set(Number(raw.seq), CPS.decode(raw).input);
1205
+ }
1206
+ }
1207
+ return rows2.map((row) => fromCheckpointRow({
1208
+ ...row,
1209
+ input: inputs2.get(row.seq) ?? null
1210
+ }));
1211
+ }
1212
+ const { input: _input, ...columns } = getTableColumns2(durableStepCheckpoints);
828
1213
  const rows = await this.db.select(columns).from(durableStepCheckpoints).where(eq(durableStepCheckpoints.runId, runId)).orderBy(asc(durableStepCheckpoints.seq));
829
1214
  const open = rows.filter((row) => row.status !== "completed").map((row) => row.seq);
830
1215
  const inputs = /* @__PURE__ */ new Map();
@@ -846,6 +1231,15 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
846
1231
  }
847
1232
  async hasCheckpoints(runId, fromSeq, toSeq, statuses) {
848
1233
  if (statuses.length === 0) return false;
1234
+ if (this.hot) {
1235
+ const rows2 = await this.hot.query(HAS_CHECKPOINTS, [
1236
+ runId,
1237
+ fromSeq,
1238
+ toSeq,
1239
+ statuses
1240
+ ]);
1241
+ return rows2.length > 0;
1242
+ }
849
1243
  const rows = await this.db.select({
850
1244
  seq: durableStepCheckpoints.seq
851
1245
  }).from(durableStepCheckpoints).where(and(eq(durableStepCheckpoints.runId, runId), gte(durableStepCheckpoints.seq, fromSeq), lte(durableStepCheckpoints.seq, toSeq), inArray(durableStepCheckpoints.status, statuses))).orderBy(desc(durableStepCheckpoints.seq)).limit(1);
@@ -853,6 +1247,15 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
853
1247
  }
854
1248
  async countCheckpoints(runId, fromSeq, toSeq, statuses) {
855
1249
  if (statuses.length === 0) return 0;
1250
+ if (this.hot) {
1251
+ const rows2 = await this.hot.query(COUNT_CHECKPOINTS, [
1252
+ runId,
1253
+ fromSeq,
1254
+ toSeq,
1255
+ statuses
1256
+ ]);
1257
+ return Number(rows2[0]?.n ?? 0);
1258
+ }
856
1259
  const rows = await this.db.select({
857
1260
  count: sql3`count(*)`.mapWith(Number)
858
1261
  }).from(durableStepCheckpoints).where(and(eq(durableStepCheckpoints.runId, runId), gte(durableStepCheckpoints.seq, fromSeq), lte(durableStepCheckpoints.seq, toSeq), inArray(durableStepCheckpoints.status, statuses)));
@@ -985,6 +1388,16 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
985
1388
  });
986
1389
  }
987
1390
  async takeBufferedSignal(token) {
1391
+ if (this.hot) {
1392
+ const [row2] = await this.hot.query(TAKE_BUFFERED_SIGNAL, [
1393
+ token
1394
+ ]);
1395
+ if (!row2) return null;
1396
+ const payload = row2.payload == null ? null : SIGNAL_PAYLOAD.mapFromDriverValue(row2.payload);
1397
+ return {
1398
+ payload: payload ?? void 0
1399
+ };
1400
+ }
988
1401
  const oldest = this.db.select({
989
1402
  id: durableBufferedSignals.id
990
1403
  }).from(durableBufferedSignals).where(eq(durableBufferedSignals.token, token)).orderBy(asc(durableBufferedSignals.id)).limit(1).for("update", {
@@ -998,6 +1411,59 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
998
1411
  payload: row.payload ?? void 0
999
1412
  } : null;
1000
1413
  }
1414
+ async takeOldestBufferedSignal(tokens) {
1415
+ if (tokens.length === 0) return null;
1416
+ const oldest = this.db.select({
1417
+ id: durableBufferedSignals.id
1418
+ }).from(durableBufferedSignals).where(inArray(durableBufferedSignals.token, [
1419
+ ...new Set(tokens)
1420
+ ])).orderBy(asc(durableBufferedSignals.id)).limit(1).for("update", {
1421
+ skipLocked: true
1422
+ });
1423
+ const rows = await this.db.delete(durableBufferedSignals).where(inArray(durableBufferedSignals.id, oldest)).returning({
1424
+ token: durableBufferedSignals.token,
1425
+ payload: durableBufferedSignals.payload
1426
+ });
1427
+ const row = rows[0];
1428
+ return row ? {
1429
+ token: row.token,
1430
+ payload: row.payload ?? void 0
1431
+ } : null;
1432
+ }
1433
+ async claimSignalId(token, id, expiresAt, nowMs) {
1434
+ const rows = await this.db.insert(durableSignalIds).values({
1435
+ token,
1436
+ signalId: id,
1437
+ expiresAt: new Date(expiresAt)
1438
+ }).onConflictDoUpdate({
1439
+ target: [
1440
+ durableSignalIds.token,
1441
+ durableSignalIds.signalId
1442
+ ],
1443
+ set: {
1444
+ expiresAt: new Date(expiresAt)
1445
+ },
1446
+ setWhere: lte(durableSignalIds.expiresAt, new Date(nowMs))
1447
+ }).returning({
1448
+ token: durableSignalIds.token
1449
+ });
1450
+ return rows.length > 0;
1451
+ }
1452
+ async releaseSignalId(token, id) {
1453
+ await this.db.delete(durableSignalIds).where(and(eq(durableSignalIds.token, token), eq(durableSignalIds.signalId, id)));
1454
+ }
1455
+ async deleteExpiredSignalIds(nowMs, limit) {
1456
+ const expired = this.db.select({
1457
+ token: durableSignalIds.token,
1458
+ signalId: durableSignalIds.signalId
1459
+ }).from(durableSignalIds).where(lte(durableSignalIds.expiresAt, new Date(nowMs))).limit(limit).for("update", {
1460
+ skipLocked: true
1461
+ });
1462
+ const rows = await this.db.delete(durableSignalIds).where(sql3`(${durableSignalIds.token}, ${durableSignalIds.signalId}) in (${expired})`).returning({
1463
+ token: durableSignalIds.token
1464
+ });
1465
+ return rows.length;
1466
+ }
1001
1467
  async listBufferedSignals(tokenPrefix, afterId, limit) {
1002
1468
  const rows = await this.db.select().from(durableBufferedSignals).where(and(like(durableBufferedSignals.token, `${escapeLike(tokenPrefix)}%`), afterId !== void 0 ? gt(durableBufferedSignals.id, Number(afterId)) : void 0)).orderBy(asc(durableBufferedSignals.id)).limit(limit);
1003
1469
  return rows.filter((r) => r.token.startsWith(tokenPrefix)).map((r) => ({
@@ -1019,6 +1485,19 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
1019
1485
  });
1020
1486
  }
1021
1487
  async listBufferedEvents(name, limit) {
1488
+ if (this.hot) {
1489
+ return (await this.hot.query(LIST_BUFFERED_EVENTS, [
1490
+ name,
1491
+ limit
1492
+ ])).map((raw) => {
1493
+ const r = EVENTS.decode(raw);
1494
+ return {
1495
+ id: r.id,
1496
+ payload: r.payload ?? void 0,
1497
+ publishedAt: r.publishedAt.getTime()
1498
+ };
1499
+ });
1500
+ }
1022
1501
  const rows = await this.db.select().from(durableBufferedEvents).where(eq(durableBufferedEvents.name, name)).orderBy(asc(durableBufferedEvents.publishedAt), asc(durableBufferedEvents.id)).limit(limit);
1023
1502
  return rows.map((r) => ({
1024
1503
  id: r.id,
@@ -1027,6 +1506,9 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
1027
1506
  }));
1028
1507
  }
1029
1508
  async removeBufferedEvent(id) {
1509
+ if (this.hot) return (await this.hot.query(REMOVE_BUFFERED_EVENT, [
1510
+ id
1511
+ ])).length === 1;
1030
1512
  const rows = await this.db.delete(durableBufferedEvents).where(eq(durableBufferedEvents.id, id)).returning({
1031
1513
  id: durableBufferedEvents.id
1032
1514
  });
@@ -1038,6 +1520,68 @@ function escapeLike(value) {
1038
1520
  }
1039
1521
  __name(escapeLike, "escapeLike");
1040
1522
  var CHECKPOINT_BATCH_ROWS = 500;
1523
+ var RUNS = new TableCodec(durableWorkflowRuns);
1524
+ var CPS = new TableCodec(durableStepCheckpoints);
1525
+ var CPS_NO_INPUT = new TableCodec(durableStepCheckpoints, [
1526
+ "input"
1527
+ ]);
1528
+ var EVENTS = new TableCodec(durableBufferedEvents);
1529
+ var SIGNAL_PAYLOAD = getTableColumns2(durableBufferedSignals).payload;
1530
+ var T_RUNS = quote(getTableName2(durableWorkflowRuns));
1531
+ var T_CPS = quote(getTableName2(durableStepCheckpoints));
1532
+ var T_EVENTS = quote(getTableName2(durableBufferedEvents));
1533
+ var T_SIGNALS = quote(getTableName2(durableBufferedSignals));
1534
+ var T_ATTRIBUTES = quote(getTableName2(durableRunAttributes));
1535
+ var SELECT_RUN = `SELECT ${RUNS.list} FROM ${T_RUNS} WHERE "id" = $1`;
1536
+ var INSERT_RUN = `INSERT INTO ${T_RUNS} (${RUNS.list}) VALUES (${placeholders(1, RUNS.columns.length)})`;
1537
+ var DELETE_ATTRIBUTES = `DELETE FROM ${T_ATTRIBUTES} WHERE "run_id" = $1`;
1538
+ var LOCK_RUN = `UPDATE ${T_RUNS} SET "locked_by" = $2, "locked_until" = $3 WHERE "id" = $1 AND ("locked_until" IS NULL OR "locked_until" <= $4)`;
1539
+ var RELEASE_LOCK = `UPDATE ${T_RUNS} SET "locked_by" = NULL, "locked_until" = NULL WHERE "id" = $1`;
1540
+ var RENEW_LOCK = `UPDATE ${T_RUNS} SET "locked_until" = $3 WHERE "id" = $1 AND "locked_by" = $2 RETURNING "id"`;
1541
+ var SELECT_CHECKPOINT = `SELECT ${CPS.list} FROM ${T_CPS} WHERE "run_id" = $1 AND "seq" = $2 LIMIT 1`;
1542
+ var SELECT_CHECKPOINTS_FOR_SETTLE = `SELECT ${CPS_NO_INPUT.list} FROM ${T_CPS} WHERE "run_id" = $1 AND "seq" = ANY($2::integer[]) ORDER BY "seq"`;
1543
+ var SELECT_REPLAY = `SELECT ${CPS_NO_INPUT.list} FROM ${T_CPS} WHERE "run_id" = $1 ORDER BY "seq"`;
1544
+ var SELECT_INPUTS = `SELECT "seq", "input" FROM ${T_CPS} WHERE "run_id" = $1 AND "seq" = ANY($2::integer[])`;
1545
+ var CHECKPOINT_VALUES = `(${CPS.list}) VALUES (${placeholders(1, CPS.columns.length)})`;
1546
+ var UPSERT_CHECKPOINT = `INSERT INTO ${T_CPS} ${CHECKPOINT_VALUES} ON CONFLICT ("run_id", "seq") DO UPDATE SET ${CPS.columns.map(([, c]) => `${quote(c.name)} = EXCLUDED.${quote(c.name)}`).join(", ")}`;
1547
+ var INSERT_CHECKPOINT_IF_ABSENT = `INSERT INTO ${T_CPS} ${CHECKPOINT_VALUES} ON CONFLICT ("run_id", "seq") DO NOTHING RETURNING "seq"`;
1548
+ var CHECKPOINT_RANGE = `FROM ${T_CPS} WHERE "run_id" = $1 AND "seq" >= $2 AND "seq" <= $3 AND "status" = ANY($4::text[])`;
1549
+ var HAS_CHECKPOINTS = `SELECT "seq" ${CHECKPOINT_RANGE} ORDER BY "seq" DESC LIMIT 1`;
1550
+ var COUNT_CHECKPOINTS = `SELECT count(*)::integer AS "n" ${CHECKPOINT_RANGE}`;
1551
+ var LIST_BUFFERED_EVENTS = `SELECT ${EVENTS.list} FROM ${T_EVENTS} WHERE "name" = $1 ORDER BY "published_at", "id" LIMIT $2`;
1552
+ var REMOVE_BUFFERED_EVENT = `DELETE FROM ${T_EVENTS} WHERE "id" = $1 RETURNING "id"`;
1553
+ var TAKE_BUFFERED_SIGNAL = `WITH taken AS MATERIALIZED (SELECT "id" FROM ${T_SIGNALS} WHERE "token" = $1 ORDER BY "id" LIMIT 1 FOR UPDATE SKIP LOCKED) DELETE FROM ${T_SIGNALS} WHERE "id" IN (SELECT "id" FROM taken) RETURNING "payload"`;
1554
+ var HOT_SETTLE_MAX_ROWS = 64;
1555
+ var settleTexts = /* @__PURE__ */ new Map();
1556
+ var lockTime = /* @__PURE__ */ __name((ms2) => RUNS.encode("lockedUntil", new Date(ms2)), "lockTime");
1557
+ function settleResultsText(n) {
1558
+ let text2 = settleTexts.get(n);
1559
+ if (text2 !== void 0) return text2;
1560
+ const rows = Array.from({
1561
+ length: n
1562
+ }, (_, r) => {
1563
+ const p = /* @__PURE__ */ __name((k) => `$${r * 8 + k}`, "p");
1564
+ return `(${p(1)}::text, ${p(2)}::integer, ${p(3)}::text, ${p(4)}::jsonb, ${p(5)}::jsonb, ${p(6)}::jsonb, ${p(7)}::timestamptz, ${p(8)}::timestamptz)`;
1565
+ });
1566
+ text2 = `UPDATE ${T_CPS} AS c SET status = v.status, output = CASE WHEN v.status = 'completed' THEN v.output ELSE c.output END, error = v.error, events = COALESCE(v.events, c.events), started_at = COALESCE(v.started_at, c.started_at), finished_at = v.finished_at FROM (VALUES ${rows.join(", ")}) AS v(run_id, seq, status, output, error, events, started_at, finished_at) WHERE c.run_id = v.run_id AND c.seq = v.seq AND c.status = 'pending' AND NOT EXISTS (SELECT 1 FROM ${T_RUNS} AS r WHERE r.id = c.run_id AND r.status IN ('cancelled', 'completed')) RETURNING ${CPS_NO_INPUT.columns.map(([, c]) => `c.${quote(c.name)}`).join(", ")}`;
1567
+ settleTexts.set(n, text2);
1568
+ return text2;
1569
+ }
1570
+ __name(settleResultsText, "settleResultsText");
1571
+ function settleResultValues(r) {
1572
+ const json = /* @__PURE__ */ __name((v) => v == null ? null : JSON.stringify(v), "json");
1573
+ return [
1574
+ r.runId,
1575
+ r.seq,
1576
+ r.status,
1577
+ json(r.status === "completed" ? r.output : null),
1578
+ json(r.error),
1579
+ json(r.events),
1580
+ r.startedAt ? r.startedAt.toISOString() : null,
1581
+ r.finishedAt.toISOString()
1582
+ ];
1583
+ }
1584
+ __name(settleResultValues, "settleResultValues");
1041
1585
  var rowKey = /* @__PURE__ */ __name((cp) => `${cp.seq}:${cp.runId}`, "rowKey");
1042
1586
  function distinctRounds(checkpoints) {
1043
1587
  const rounds = [];
@@ -1084,6 +1628,48 @@ function settleIfPendingFrom(settlements) {
1084
1628
  RETURNING c.run_id, c.seq`;
1085
1629
  }
1086
1630
  __name(settleIfPendingFrom, "settleIfPendingFrom");
1631
+ function oneStatement(rows) {
1632
+ if (rows.length > CHECKPOINT_BATCH_ROWS) return false;
1633
+ const keys = new Set(rows.map(rowKey));
1634
+ return keys.size === rows.length;
1635
+ }
1636
+ __name(oneStatement, "oneStatement");
1637
+ var SETTLED_COLUMNS = (() => {
1638
+ const { input: _input, ...columns } = getTableColumns2(durableStepCheckpoints);
1639
+ return columns;
1640
+ })();
1641
+ function settledRowFromDriver(raw) {
1642
+ const row = {
1643
+ input: null
1644
+ };
1645
+ for (const [key, column] of Object.entries(SETTLED_COLUMNS)) {
1646
+ const value = raw[column.name];
1647
+ row[key] = value == null ? null : column.mapFromDriverValue(value);
1648
+ }
1649
+ return row;
1650
+ }
1651
+ __name(settledRowFromDriver, "settledRowFromDriver");
1652
+ function settleResultsFrom(results) {
1653
+ const json = /* @__PURE__ */ __name((v) => v == null ? null : JSON.stringify(v), "json");
1654
+ const values = results.map((r) => sql3`(${r.runId}::text, ${r.seq}::integer, ${r.status}::text, ${json(r.status === "completed" ? r.output : null)}::jsonb, ${json(r.error)}::jsonb, ${json(r.events)}::jsonb, ${r.startedAt ? r.startedAt.toISOString() : null}::timestamptz, ${r.finishedAt.toISOString()}::timestamptz)`);
1655
+ const returning = sql3.join(Object.values(SETTLED_COLUMNS).map((column) => sql3`c.${sql3.identifier(column.name)}`), sql3`, `);
1656
+ return sql3`UPDATE ${durableStepCheckpoints} AS c SET
1657
+ status = v.status,
1658
+ output = CASE WHEN v.status = 'completed' THEN v.output ELSE c.output END,
1659
+ error = v.error,
1660
+ events = COALESCE(v.events, c.events),
1661
+ started_at = COALESCE(v.started_at, c.started_at),
1662
+ finished_at = v.finished_at
1663
+ FROM (VALUES ${sql3.join(values, sql3`, `)}) AS v(run_id, seq, status, output, error, events,
1664
+ started_at, finished_at)
1665
+ WHERE c.run_id = v.run_id AND c.seq = v.seq AND c.status = 'pending'
1666
+ AND NOT EXISTS (
1667
+ SELECT 1 FROM ${durableWorkflowRuns} AS r
1668
+ WHERE r.id = c.run_id AND r.status IN ('cancelled', 'completed')
1669
+ )
1670
+ RETURNING ${returning}`;
1671
+ }
1672
+ __name(settleResultsFrom, "settleResultsFrom");
1087
1673
  async function upsertCheckpoint(db, cp) {
1088
1674
  const row = toCheckpointRow(cp);
1089
1675
  await db.insert(durableStepCheckpoints).values(row).onConflictDoUpdate({
@@ -1282,7 +1868,7 @@ function fromScheduleRow(row) {
1282
1868
  }
1283
1869
  __name(fromScheduleRow, "fromScheduleRow");
1284
1870
  function excludedSet(table, only) {
1285
- return Object.fromEntries(Object.entries(getTableColumns(table)).filter(([key]) => !only || only.includes(key)).map(([key, column]) => [
1871
+ return Object.fromEntries(Object.entries(getTableColumns2(table)).filter(([key]) => !only || only.includes(key)).map(([key, column]) => [
1286
1872
  key,
1287
1873
  sql3.raw(`excluded."${column.name}"`)
1288
1874
  ]));
@@ -1300,6 +1886,7 @@ export {
1300
1886
  durablePgSchema,
1301
1887
  durableRunAttributes,
1302
1888
  durableSchedules,
1889
+ durableSignalIds,
1303
1890
  durableSignalWaiters,
1304
1891
  durableStepCheckpoints,
1305
1892
  durableWorkflowRuns,