@dudousxd/nestjs-durable-testing 0.23.5 → 0.25.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
@@ -288,6 +288,13 @@ var DURABLE_CANONICAL_COLUMNS = {
288
288
  payload: "payload",
289
289
  publishedAt: "published_at"
290
290
  },
291
+ // Signal dedupe ids (`engine.signal(token, payload, { id })`): one row per (token, id) until it
292
+ // expires. `expires_at` is when the id stops deduplicating.
293
+ durable_signal_ids: {
294
+ token: "token",
295
+ signalId: "signal_id",
296
+ expiresAt: "expires_at"
297
+ },
291
298
  // Persisted schedules (`engine.schedules`). `spec`/`state` are engine-owned JSON documents.
292
299
  durable_schedules: {
293
300
  id: "id",
@@ -364,7 +371,9 @@ function createTestEngine() {
364
371
  clock,
365
372
  async tick(ms2) {
366
373
  clock.advance(ms2);
367
- return engine.resumeDueTimers(clock.now());
374
+ const resumed = await engine.resumeDueTimers(clock.now());
375
+ await engine.sweepLifecycleEvents(clock.now());
376
+ return resumed;
368
377
  },
369
378
  async run(workflow, input, runId, opts) {
370
379
  await engine.start(workflow, input, runId, opts);
@@ -442,7 +451,7 @@ async function assertReplayable(register, history) {
442
451
  __name(assertReplayable, "assertReplayable");
443
452
 
444
453
  // src/state-store-conformance.ts
445
- import { SINGLETON_ADMITTED_TAG, SINGLETON_TRACKED_TAG, WorkflowEngine as WorkflowEngine3 } from "@dudousxd/nestjs-durable-core";
454
+ import { LIFECYCLE_PENDING_TAG, LIFECYCLE_REGISTRY_TAG, LIFECYCLE_REGISTRY_WORKFLOW, SINGLETON_ADMITTED_TAG, SINGLETON_TRACKED_TAG, WorkflowEngine as WorkflowEngine3 } from "@dudousxd/nestjs-durable-core";
446
455
  import { beforeEach, describe as describe2, expect as expect3, it as it2 } from "vitest";
447
456
 
448
457
  // src/checkpoint-aggregation-contract.ts
@@ -839,12 +848,22 @@ function sqlOf(logged) {
839
848
  return logged.replace(ANSI_COLOR, "").replace(/^\s*\[query\]\s*/i, "").trim();
840
849
  }
841
850
  __name(sqlOf, "sqlOf");
842
- async function loggedSince(log, before) {
851
+ async function settleLog(log, flush) {
852
+ if (flush) return flush();
843
853
  let seen = -1;
844
854
  for (let i = 0; i < 50 && log().length !== seen; i += 1) {
845
855
  seen = log().length;
846
856
  await new Promise((resolve) => setTimeout(resolve, 10));
847
857
  }
858
+ }
859
+ __name(settleLog, "settleLog");
860
+ async function loggedLength(log, flush) {
861
+ await settleLog(log, flush);
862
+ return log().length;
863
+ }
864
+ __name(loggedLength, "loggedLength");
865
+ async function loggedSince(log, before, flush) {
866
+ await settleLog(log, flush);
848
867
  return log().slice(before);
849
868
  }
850
869
  __name(loggedSince, "loggedSince");
@@ -857,6 +876,7 @@ function runStateStoreContract(name, makeStore) {
857
876
  let supportsTagFilter = true;
858
877
  let statements;
859
878
  let statementLog;
879
+ let flushStatementLog;
860
880
  let implicitTransactions = false;
861
881
  let skipReason;
862
882
  let loggedSkip = false;
@@ -870,6 +890,7 @@ function runStateStoreContract(name, makeStore) {
870
890
  supportsTagFilter = ctx.supportsTagFilter ?? true;
871
891
  statements = ctx.statements;
872
892
  statementLog = ctx.statementLog;
893
+ flushStatementLog = ctx.flushStatementLog;
873
894
  implicitTransactions = ctx.implicitTransactions ?? false;
874
895
  skipReason = void 0;
875
896
  } catch (err) {
@@ -1414,6 +1435,108 @@ function runStateStoreContract(name, makeStore) {
1414
1435
  "c2"
1415
1436
  ]);
1416
1437
  });
1438
+ t("pruneTerminalRuns keeps a run whose lifecycle event is still owed", async () => {
1439
+ await store.createRun(aged("owed", 10, {
1440
+ tags: [
1441
+ "etl",
1442
+ LIFECYCLE_PENDING_TAG
1443
+ ]
1444
+ }));
1445
+ await store.createRun(aged("published", 10, {
1446
+ tags: [
1447
+ "etl",
1448
+ "durable:triggered:refresh"
1449
+ ]
1450
+ }));
1451
+ await store.createRun(aged("owed-over-cap", 2, {
1452
+ tags: [
1453
+ LIFECYCLE_PENDING_TAG
1454
+ ]
1455
+ }));
1456
+ await store.createRun(aged("old", 8));
1457
+ await store.createRun(aged("newest", 1));
1458
+ await store.pruneTerminalRuns?.({
1459
+ statuses: [
1460
+ "completed"
1461
+ ],
1462
+ maxAge: "7d"
1463
+ }, NOW, 100);
1464
+ expect3(await ids()).toEqual([
1465
+ "newest",
1466
+ "owed",
1467
+ "owed-over-cap"
1468
+ ]);
1469
+ await store.createRun(aged("mid", 2));
1470
+ await store.pruneTerminalRuns?.({
1471
+ statuses: [
1472
+ "completed"
1473
+ ],
1474
+ maxCount: 1
1475
+ }, NOW, 100);
1476
+ expect3(await ids()).toEqual([
1477
+ "newest",
1478
+ "owed",
1479
+ "owed-over-cap"
1480
+ ]);
1481
+ });
1482
+ t("pruneTerminalRuns keeps lifecycle registry runs", async () => {
1483
+ await store.createRun(aged("__lifecycle_registry:a", 10, {
1484
+ workflow: LIFECYCLE_REGISTRY_WORKFLOW,
1485
+ tags: [
1486
+ LIFECYCLE_REGISTRY_TAG
1487
+ ]
1488
+ }));
1489
+ await store.createRun(aged("old", 10));
1490
+ await store.pruneTerminalRuns?.({
1491
+ statuses: [
1492
+ "completed"
1493
+ ],
1494
+ maxAge: "7d"
1495
+ }, NOW, 100);
1496
+ expect3(await ids()).toEqual([
1497
+ "__lifecycle_registry:a"
1498
+ ]);
1499
+ await store.pruneTerminalRuns?.({
1500
+ statuses: [
1501
+ "completed"
1502
+ ],
1503
+ maxCount: 0
1504
+ }, NOW, 100);
1505
+ expect3(await ids()).toEqual([
1506
+ "__lifecycle_registry:a"
1507
+ ]);
1508
+ });
1509
+ t("pruneTerminalRuns does not count lifecycle registry runs toward maxCount", async () => {
1510
+ for (const instance of [
1511
+ "a",
1512
+ "b"
1513
+ ]) {
1514
+ await store.createRun(aged(`__lifecycle_registry:${instance}`, 0, {
1515
+ workflow: LIFECYCLE_REGISTRY_WORKFLOW,
1516
+ tags: [
1517
+ LIFECYCLE_REGISTRY_TAG
1518
+ ]
1519
+ }));
1520
+ }
1521
+ await store.createRun(aged("owed", 1, {
1522
+ tags: [
1523
+ LIFECYCLE_PENDING_TAG
1524
+ ]
1525
+ }));
1526
+ for (let i = 2; i <= 4; i++) await store.createRun(aged(`c${i}`, i));
1527
+ await store.pruneTerminalRuns?.({
1528
+ statuses: [
1529
+ "completed"
1530
+ ],
1531
+ maxCount: 2
1532
+ }, NOW, 100);
1533
+ expect3(await ids()).toEqual([
1534
+ "__lifecycle_registry:a",
1535
+ "__lifecycle_registry:b",
1536
+ "c2",
1537
+ "owed"
1538
+ ]);
1539
+ });
1417
1540
  t("a scoped retention policy only prunes (and counts) the runs its scope matches", async () => {
1418
1541
  await store.createRun(aged("acme-old", 10, {
1419
1542
  namespace: "acme"
@@ -2395,8 +2518,10 @@ function runStateStoreContract(name, makeStore) {
2395
2518
  output: void 0
2396
2519
  }));
2397
2520
  const sent = /* @__PURE__ */ __name(async (work) => {
2521
+ await flushStatementLog?.();
2398
2522
  const before = statements();
2399
2523
  await work();
2524
+ await flushStatementLog?.();
2400
2525
  return statements() - before;
2401
2526
  }, "sent");
2402
2527
  expect3(await sent(() => store.saveCheckpoints?.(rows))).toBeLessThanOrEqual(limit);
@@ -2629,6 +2754,326 @@ function runStateStoreContract(name, makeStore) {
2629
2754
  prompt: "p0"
2630
2755
  });
2631
2756
  });
2757
+ t("settleResultsIfPending settles pending rows and reads them back without their input", async () => {
2758
+ if (!store.settleResultsIfPending) return;
2759
+ expect3(await store.settleResultsIfPending([])).toEqual([]);
2760
+ await store.createRun(run());
2761
+ await store.createRun(run({
2762
+ id: "r2"
2763
+ }));
2764
+ await store.createRun(run({
2765
+ id: "gone",
2766
+ status: "cancelled"
2767
+ }));
2768
+ const pending = /* @__PURE__ */ __name((runId, seq) => checkpoint({
2769
+ runId,
2770
+ seq,
2771
+ name: "ask",
2772
+ kind: "remote",
2773
+ stepId: `${runId}:${seq}`,
2774
+ status: "pending",
2775
+ input: {
2776
+ prompt: `p${seq}`
2777
+ },
2778
+ output: {
2779
+ partial: seq
2780
+ },
2781
+ events: [
2782
+ {
2783
+ at: 1,
2784
+ level: "info",
2785
+ message: "queued"
2786
+ }
2787
+ ],
2788
+ attempts: 2,
2789
+ workerGroup: "llm",
2790
+ parallelGroup: "all:0:2",
2791
+ wakeAt: 5e3
2792
+ }), "pending");
2793
+ for (const seq of [
2794
+ 0,
2795
+ 1,
2796
+ 2,
2797
+ 3
2798
+ ]) await store.saveCheckpoint(pending("r1", seq));
2799
+ await store.saveCheckpoint(pending("r2", 0));
2800
+ await store.saveCheckpoint(pending("gone", 0));
2801
+ await store.saveCheckpoint({
2802
+ ...pending("r1", 1),
2803
+ status: "completed",
2804
+ output: {
2805
+ first: 1
2806
+ }
2807
+ });
2808
+ const later = new Date(at.getTime() + 6e4);
2809
+ const settled = await store.settleResultsIfPending([
2810
+ {
2811
+ runId: "r1",
2812
+ seq: 0,
2813
+ status: "completed",
2814
+ output: {
2815
+ n: 0
2816
+ },
2817
+ startedAt: new Date(at.getTime() + 1e3),
2818
+ finishedAt: later
2819
+ },
2820
+ // Already settled: nothing written.
2821
+ {
2822
+ runId: "r1",
2823
+ seq: 1,
2824
+ status: "completed",
2825
+ output: {
2826
+ n: 1
2827
+ },
2828
+ finishedAt: later
2829
+ },
2830
+ // A second result for the row the first one settles: loses.
2831
+ {
2832
+ runId: "r1",
2833
+ seq: 0,
2834
+ status: "completed",
2835
+ output: {
2836
+ n: "dup"
2837
+ },
2838
+ finishedAt: later
2839
+ },
2840
+ // A failure keeps the stored output, events and start; records the error.
2841
+ {
2842
+ runId: "r1",
2843
+ seq: 2,
2844
+ status: "failed",
2845
+ error: {
2846
+ message: "boom"
2847
+ },
2848
+ finishedAt: later
2849
+ },
2850
+ // No such row.
2851
+ {
2852
+ runId: "r1",
2853
+ seq: 9,
2854
+ status: "completed",
2855
+ output: {
2856
+ n: 9
2857
+ },
2858
+ finishedAt: later
2859
+ },
2860
+ // Another run's row at the same seq.
2861
+ {
2862
+ runId: "r2",
2863
+ seq: 0,
2864
+ status: "completed",
2865
+ output: {
2866
+ n: "r2"
2867
+ },
2868
+ finishedAt: later
2869
+ },
2870
+ // A finished run's step settles nothing.
2871
+ {
2872
+ runId: "gone",
2873
+ seq: 0,
2874
+ status: "completed",
2875
+ output: {
2876
+ n: "late"
2877
+ },
2878
+ finishedAt: later
2879
+ }
2880
+ ]);
2881
+ expect3(settled.map((cp2) => cp2 ? [
2882
+ cp2.runId,
2883
+ cp2.seq,
2884
+ cp2.status
2885
+ ] : null)).toEqual([
2886
+ [
2887
+ "r1",
2888
+ 0,
2889
+ "completed"
2890
+ ],
2891
+ null,
2892
+ null,
2893
+ [
2894
+ "r1",
2895
+ 2,
2896
+ "failed"
2897
+ ],
2898
+ null,
2899
+ [
2900
+ "r2",
2901
+ 0,
2902
+ "completed"
2903
+ ],
2904
+ null
2905
+ ]);
2906
+ const [first, , , failed] = settled;
2907
+ expect3(first?.output).toEqual({
2908
+ n: 0
2909
+ });
2910
+ expect3(first?.input).toBeUndefined();
2911
+ expect3(first?.name).toBe("ask");
2912
+ expect3(first?.stepId).toBe("r1:0");
2913
+ expect3(first?.attempts).toBe(2);
2914
+ expect3(first?.parallelGroup).toBe("all:0:2");
2915
+ expect3(first?.events).toEqual([
2916
+ {
2917
+ at: 1,
2918
+ level: "info",
2919
+ message: "queued"
2920
+ }
2921
+ ]);
2922
+ expect3(first?.startedAt.getTime()).toBe(at.getTime() + 1e3);
2923
+ expect3(first?.finishedAt.getTime()).toBe(later.getTime());
2924
+ expect3(first?.enqueuedAt.getTime()).toBe(at.getTime());
2925
+ expect3(failed?.output).toEqual({
2926
+ partial: 2
2927
+ });
2928
+ expect3(failed?.error).toEqual({
2929
+ message: "boom"
2930
+ });
2931
+ expect3(failed?.startedAt.getTime()).toBe(at.getTime());
2932
+ const stored = await store.getCheckpoint("r1", 0);
2933
+ expect3(stored?.output).toEqual({
2934
+ n: 0
2935
+ });
2936
+ expect3(stored?.input).toEqual({
2937
+ prompt: "p0"
2938
+ });
2939
+ expect3((await store.getCheckpoint("r1", 1))?.output).toEqual({
2940
+ first: 1
2941
+ });
2942
+ expect3((await store.getCheckpoint("r1", 3))?.status).toBe("pending");
2943
+ expect3((await store.getCheckpoint("gone", 0))?.status).toBe("pending");
2944
+ expect3(await store.getCheckpoint("r1", 9)).toBeNull();
2945
+ });
2946
+ t("insertCheckpointIfAbsent writes a position only once", async () => {
2947
+ if (!store.insertCheckpointIfAbsent) return;
2948
+ await store.createRun(run());
2949
+ const first = checkpoint({
2950
+ seq: 4,
2951
+ status: "pending",
2952
+ input: {
2953
+ a: 1
2954
+ },
2955
+ output: void 0
2956
+ });
2957
+ expect3(await store.insertCheckpointIfAbsent(first)).toBe(true);
2958
+ expect3(await store.insertCheckpointIfAbsent({
2959
+ ...first,
2960
+ status: "completed",
2961
+ output: {
2962
+ b: 2
2963
+ }
2964
+ })).toBe(false);
2965
+ const stored = await store.getCheckpoint("r1", 4);
2966
+ expect3(stored?.status).toBe("pending");
2967
+ expect3(stored?.input).toEqual({
2968
+ a: 1
2969
+ });
2970
+ expect3(stored?.output).toBeUndefined();
2971
+ });
2972
+ t("claimPendingRuns leases the oldest free pending runs, and concurrent claims never overlap", async () => {
2973
+ if (!store.claimPendingRuns) return;
2974
+ const created2 = /* @__PURE__ */ __name((i) => new Date(at.getTime() + i * 1e3), "created");
2975
+ for (let i = 0; i < 6; i += 1) {
2976
+ await store.createRun(run({
2977
+ id: `p${i}`,
2978
+ status: "pending",
2979
+ createdAt: created2(i)
2980
+ }));
2981
+ }
2982
+ await store.createRun(run({
2983
+ id: "busy",
2984
+ status: "pending",
2985
+ createdAt: created2(-2)
2986
+ }));
2987
+ await store.updateRun("busy", {
2988
+ lockedBy: "other",
2989
+ lockedUntil: 5e3
2990
+ });
2991
+ await store.createRun(run({
2992
+ id: "stale",
2993
+ status: "pending",
2994
+ createdAt: created2(-1)
2995
+ }));
2996
+ await store.updateRun("stale", {
2997
+ lockedBy: "gone",
2998
+ lockedUntil: 500
2999
+ });
3000
+ await store.createRun(run({
3001
+ id: "done",
3002
+ status: "completed",
3003
+ createdAt: created2(-3)
3004
+ }));
3005
+ const first = await store.claimPendingRuns(3, "a", 9e3, 1e3);
3006
+ expect3(first.map((r) => r.id)).toEqual([
3007
+ "stale",
3008
+ "p0",
3009
+ "p1"
3010
+ ]);
3011
+ for (const r of first) {
3012
+ expect3(r.status).toBe("pending");
3013
+ expect3(r.lockedBy).toBe("a");
3014
+ expect3(r.lockedUntil).toBe(9e3);
3015
+ }
3016
+ expect3((await store.getRun("p0"))?.lockedBy).toBe("a");
3017
+ expect3((await store.getRun("busy"))?.lockedBy).toBe("other");
3018
+ expect3(await store.claimPendingRuns(0, "a", 9e3, 1e3)).toEqual([]);
3019
+ const [x, y] = await Promise.all([
3020
+ store.claimPendingRuns(3, "x", 9e3, 1e3),
3021
+ peerStore.claimPendingRuns?.(3, "y", 9e3, 1e3) ?? Promise.resolve([])
3022
+ ]);
3023
+ const ids2 = [
3024
+ ...x,
3025
+ ...y
3026
+ ].map((r) => r.id);
3027
+ expect3(new Set(ids2).size).toBe(ids2.length);
3028
+ expect3(ids2.length).toBeGreaterThan(0);
3029
+ const rest = await store.claimPendingRuns(10, "z", 9e3, 1e3);
3030
+ expect3([
3031
+ ...ids2,
3032
+ ...rest.map((r) => r.id)
3033
+ ].sort()).toEqual([
3034
+ "p2",
3035
+ "p3",
3036
+ "p4",
3037
+ "p5"
3038
+ ]);
3039
+ });
3040
+ t("keeps updatedAt to the millisecond, through createRun, updateRun and updateRunIf", async () => {
3041
+ const created2 = new Date(at.getTime() + 123);
3042
+ await store.createRun(run({
3043
+ updatedAt: created2
3044
+ }));
3045
+ expect3((await store.getRun("r1"))?.updatedAt.getTime()).toBe(created2.getTime());
3046
+ const patched = new Date(at.getTime() + 60456);
3047
+ await store.updateRun("r1", {
3048
+ status: "suspended",
3049
+ updatedAt: patched
3050
+ });
3051
+ expect3((await store.getRun("r1"))?.updatedAt.getTime()).toBe(patched.getTime());
3052
+ if (store.updateRunIf) {
3053
+ const stamped = new Date(at.getTime() + 120789);
3054
+ expect3(await store.updateRunIf("r1", [
3055
+ "suspended"
3056
+ ], {
3057
+ updatedAt: stamped
3058
+ })).toBe(true);
3059
+ expect3((await store.getRun("r1"))?.updatedAt.getTime()).toBe(stamped.getTime());
3060
+ }
3061
+ });
3062
+ t("tryLockRunAndGet takes a free or expired lease and reads the run", async () => {
3063
+ if (!store.tryLockRunAndGet) return;
3064
+ await store.createRun(run({
3065
+ status: "suspended"
3066
+ }));
3067
+ const locked = await store.tryLockRunAndGet("r1", "a", 2e3, 1e3);
3068
+ expect3(locked?.id).toBe("r1");
3069
+ expect3(locked?.status).toBe("suspended");
3070
+ expect3(locked?.lockedBy).toBe("a");
3071
+ expect3(locked?.lockedUntil).toBe(2e3);
3072
+ expect3(await store.tryLockRunAndGet("r1", "b", 3e3, 1500)).toBeNull();
3073
+ expect3((await store.getRun("r1"))?.lockedBy).toBe("a");
3074
+ expect3((await store.tryLockRunAndGet("r1", "b", 4e3, 2e3))?.lockedBy).toBe("b");
3075
+ expect3(await store.tryLockRunAndGet("missing", "a", 2e3, 1e3)).toBeNull();
3076
+ });
2632
3077
  t("settleCheckpointsIfPending sends none of the input of the steps it settles", async () => {
2633
3078
  if (!statementLog || !store.settleCheckpointsIfPending) return;
2634
3079
  await store.createRun(run());
@@ -2648,7 +3093,7 @@ function runStateStoreContract(name, makeStore) {
2648
3093
  output: void 0
2649
3094
  }));
2650
3095
  }
2651
- const before = statementLog().length;
3096
+ const before = await loggedLength(statementLog, flushStatementLog);
2652
3097
  const settle2 = /* @__PURE__ */ __name((seqs) => store.settleCheckpointsIfPending?.(seqs.map((seq) => ({
2653
3098
  runId: "r1",
2654
3099
  seq,
@@ -2671,7 +3116,7 @@ function runStateStoreContract(name, makeStore) {
2671
3116
  true,
2672
3117
  false
2673
3118
  ]);
2674
- const sent = await loggedSince(statementLog, before);
3119
+ const sent = await loggedSince(statementLog, before, flushStatementLog);
2675
3120
  expect3(sent.length).toBeGreaterThan(0);
2676
3121
  expect3(sent.some((statement) => statement.includes("xxxxxxxxxx"))).toBe(false);
2677
3122
  expect3(sent.join("").length).toBeLessThan(2e4);
@@ -2741,9 +3186,9 @@ function runStateStoreContract(name, makeStore) {
2741
3186
  await store.createRun(run({
2742
3187
  id: "warm"
2743
3188
  }));
2744
- const before = statementLog().length;
3189
+ const before = await loggedLength(statementLog, flushStatementLog);
2745
3190
  await store.createRun(run());
2746
- const sent = await loggedSince(statementLog, before);
3191
+ const sent = await loggedSince(statementLog, before, flushStatementLog);
2747
3192
  expect3(sent.length).toBeGreaterThan(0);
2748
3193
  expect3(sent.map(sqlOf).filter((q) => /^select/i.test(q))).toEqual([]);
2749
3194
  if (!implicitTransactions) {
@@ -2758,13 +3203,13 @@ function runStateStoreContract(name, makeStore) {
2758
3203
  status: "running",
2759
3204
  updatedAt: /* @__PURE__ */ new Date()
2760
3205
  });
2761
- const before = statementLog().length;
3206
+ const before = await loggedLength(statementLog, flushStatementLog);
2762
3207
  await store.updateRun("r1", {
2763
3208
  status: "suspended",
2764
3209
  wakeAt: 5e3,
2765
3210
  updatedAt: /* @__PURE__ */ new Date()
2766
3211
  });
2767
- const sent = await loggedSince(statementLog, before);
3212
+ const sent = await loggedSince(statementLog, before, flushStatementLog);
2768
3213
  expect3(sent.map(sqlOf).filter((q) => /^update/i.test(q))).toHaveLength(1);
2769
3214
  expect3(sent.map(sqlOf).filter((q) => /^select/i.test(q))).toEqual([]);
2770
3215
  if (!implicitTransactions) {
@@ -2792,12 +3237,12 @@ function runStateStoreContract(name, makeStore) {
2792
3237
  output: void 0
2793
3238
  }));
2794
3239
  }
2795
- const before = statementLog().length;
3240
+ const before = await loggedLength(statementLog, flushStatementLog);
2796
3241
  const read = await store.getCheckpointsForSettle("r1", [
2797
3242
  0,
2798
3243
  1
2799
3244
  ]);
2800
- const sent = await loggedSince(statementLog, before);
3245
+ const sent = await loggedSince(statementLog, before, flushStatementLog);
2801
3246
  expect3(read).toHaveLength(2);
2802
3247
  expect3(sent.length).toBeGreaterThan(0);
2803
3248
  expect3(sent.filter((statement) => /\binput\b|\*\s+from/i.test(statement))).toEqual([]);
@@ -2944,9 +3389,9 @@ function runStateStoreContract(name, makeStore) {
2944
3389
  }
2945
3390
  }));
2946
3391
  }
2947
- const before = statementLog().length;
3392
+ const before = await loggedLength(statementLog, flushStatementLog);
2948
3393
  const replay = await store.listCheckpointsForReplay("r1");
2949
- const sent = await loggedSince(statementLog, before);
3394
+ const sent = await loggedSince(statementLog, before, flushStatementLog);
2950
3395
  expect3(replay.map((c) => c.output)).toEqual([
2951
3396
  {
2952
3397
  answer: 0
@@ -3529,6 +3974,196 @@ function runStateStoreContract(name, makeStore) {
3529
3974
  "c"
3530
3975
  ]);
3531
3976
  });
3977
+ t("keeps only the runs updated at or after updatedAfter", async () => {
3978
+ const at2 = /* @__PURE__ */ __name((ms2) => new Date(17e11 + ms2), "at");
3979
+ await store.createRun(run({
3980
+ id: "old",
3981
+ status: "completed",
3982
+ createdAt: at2(0),
3983
+ updatedAt: at2(0)
3984
+ }));
3985
+ await store.createRun(run({
3986
+ id: "edge",
3987
+ status: "completed",
3988
+ createdAt: at2(0),
3989
+ updatedAt: at2(5e3)
3990
+ }));
3991
+ await store.createRun(run({
3992
+ id: "new",
3993
+ status: "failed",
3994
+ createdAt: at2(0),
3995
+ updatedAt: at2(9e3)
3996
+ }));
3997
+ const after = at2(5e3).getTime();
3998
+ expect3((await store.listRuns({
3999
+ updatedAfter: after
4000
+ })).map((r) => r.id).sort()).toEqual([
4001
+ "edge",
4002
+ "new"
4003
+ ]);
4004
+ expect3((await store.listRuns({
4005
+ updatedAfter: after,
4006
+ statuses: [
4007
+ "completed"
4008
+ ]
4009
+ })).map((r) => r.id)).toEqual([
4010
+ "edge"
4011
+ ]);
4012
+ if (store.countRuns) expect3(await store.countRuns({
4013
+ updatedAfter: after
4014
+ })).toBe(2);
4015
+ });
4016
+ t("leaves out the runs of excludeWorkflows, in lists and in counts", async () => {
4017
+ await store.createRun(run({
4018
+ id: "a",
4019
+ status: "failed",
4020
+ workflow: "wa"
4021
+ }));
4022
+ await store.createRun(run({
4023
+ id: "b",
4024
+ status: "completed",
4025
+ workflow: "wb"
4026
+ }));
4027
+ await store.createRun(run({
4028
+ id: "c",
4029
+ status: "completed",
4030
+ workflow: "wc"
4031
+ }));
4032
+ expect3((await store.listRuns({
4033
+ excludeWorkflows: [
4034
+ "wb",
4035
+ "wc"
4036
+ ]
4037
+ })).map((r) => r.id).sort()).toEqual([
4038
+ "a"
4039
+ ]);
4040
+ expect3((await store.listRuns({
4041
+ excludeWorkflows: [
4042
+ "wb"
4043
+ ],
4044
+ statuses: [
4045
+ "completed"
4046
+ ]
4047
+ })).map((r) => r.id)).toEqual([
4048
+ "c"
4049
+ ]);
4050
+ expect3((await store.listRuns({
4051
+ excludeWorkflows: []
4052
+ })).map((r) => r.id).sort()).toEqual([
4053
+ "a",
4054
+ "b",
4055
+ "c"
4056
+ ]);
4057
+ if (store.countRuns) expect3(await store.countRuns({
4058
+ excludeWorkflows: [
4059
+ "wa"
4060
+ ]
4061
+ })).toBe(2);
4062
+ if (store.runFacets) {
4063
+ const rows = await store.runFacets({
4064
+ excludeWorkflows: [
4065
+ "wb"
4066
+ ]
4067
+ });
4068
+ expect3(rows.reduce((sum, row) => sum + row.count, 0)).toBe(2);
4069
+ }
4070
+ if (store.runValueFacets) {
4071
+ const rows = await store.runValueFacets({
4072
+ field: "workflow"
4073
+ }, {
4074
+ excludeWorkflows: [
4075
+ "wb"
4076
+ ]
4077
+ });
4078
+ expect3(rows.map((row) => row.value).sort()).toEqual([
4079
+ "wa",
4080
+ "wc"
4081
+ ]);
4082
+ }
4083
+ });
4084
+ t("holds a lifecycle registry run: created, refreshed in place, listed by workflow", async () => {
4085
+ const id = "__lifecycle_registry:instance-1";
4086
+ const subscriptions = [
4087
+ {
4088
+ workflow: "notify",
4089
+ events: [
4090
+ "run.completed:ingest"
4091
+ ]
4092
+ }
4093
+ ];
4094
+ expect3(await store.updateRunIf?.(id, [
4095
+ "completed"
4096
+ ], {
4097
+ updatedAt: new Date(NOW)
4098
+ })).not.toBe(true);
4099
+ await store.createRun(run({
4100
+ id,
4101
+ workflow: LIFECYCLE_REGISTRY_WORKFLOW,
4102
+ status: "completed",
4103
+ input: {
4104
+ instanceId: "instance-1",
4105
+ runtime: "node",
4106
+ subscriptions
4107
+ },
4108
+ tags: [
4109
+ LIFECYCLE_REGISTRY_TAG
4110
+ ],
4111
+ createdAt: new Date(NOW - 2e4),
4112
+ updatedAt: new Date(NOW - 2e4)
4113
+ }));
4114
+ await store.createRun(run({
4115
+ id: "app",
4116
+ workflow: "ingest",
4117
+ status: "completed"
4118
+ }));
4119
+ const more = [
4120
+ ...subscriptions,
4121
+ {
4122
+ workflow: "audit",
4123
+ events: [
4124
+ "run.failed:ingest"
4125
+ ]
4126
+ }
4127
+ ];
4128
+ const input = {
4129
+ instanceId: "instance-1",
4130
+ runtime: "node",
4131
+ subscriptions: more
4132
+ };
4133
+ if (store.updateRunIf) {
4134
+ expect3(await store.updateRunIf(id, [
4135
+ "completed"
4136
+ ], {
4137
+ input,
4138
+ updatedAt: new Date(NOW)
4139
+ })).toBe(true);
4140
+ } else {
4141
+ await store.updateRun(id, {
4142
+ input,
4143
+ updatedAt: new Date(NOW)
4144
+ });
4145
+ }
4146
+ const listed = await store.listRuns({
4147
+ workflow: LIFECYCLE_REGISTRY_WORKFLOW
4148
+ });
4149
+ expect3(listed.map((r) => r.id)).toEqual([
4150
+ id
4151
+ ]);
4152
+ expect3(listed[0]?.input).toEqual(input);
4153
+ expect3(listed[0]?.updatedAt.getTime()).toBe(NOW);
4154
+ expect3(listed[0]?.status).toBe("completed");
4155
+ expect3(listed[0]?.tags).toEqual([
4156
+ LIFECYCLE_REGISTRY_TAG
4157
+ ]);
4158
+ expect3(await store.listIncompleteRuns()).toEqual([]);
4159
+ expect3((await store.listRuns({
4160
+ excludeWorkflows: [
4161
+ LIFECYCLE_REGISTRY_WORKFLOW
4162
+ ]
4163
+ })).map((r) => r.id)).toEqual([
4164
+ "app"
4165
+ ]);
4166
+ });
3532
4167
  t("carries the absent-origin bucket INSIDE a set of origins", async () => {
3533
4168
  await store.createRun(run({
3534
4169
  id: "a",
@@ -4984,8 +5619,10 @@ function runStateStoreContract(name, makeStore) {
4984
5619
  const engine = new WorkflowEngine3({
4985
5620
  store
4986
5621
  });
5622
+ await flushStatementLog?.();
4987
5623
  const before = statements();
4988
5624
  const deleted = await engine.sweepBufferedSignals();
5625
+ await flushStatementLog?.();
4989
5626
  const sent = statements() - before;
4990
5627
  expect3(deleted).toBe(rows);
4991
5628
  expect3(await store.listBufferedSignals("child:", void 0, 10)).toEqual([]);
@@ -5018,6 +5655,137 @@ function runStateStoreContract(name, makeStore) {
5018
5655
  }
5019
5656
  });
5020
5657
  });
5658
+ t("takes the oldest buffered signal among several tokens (waitForAny)", async () => {
5659
+ const take = store.takeOldestBufferedSignal?.bind(store);
5660
+ expect3(take).toBeTypeOf("function");
5661
+ if (!take) return;
5662
+ expect3(await take([])).toBeNull();
5663
+ expect3(await take([
5664
+ "a",
5665
+ "b"
5666
+ ])).toBeNull();
5667
+ await store.bufferSignal("b", {
5668
+ n: 1
5669
+ });
5670
+ await store.bufferSignal("a", {
5671
+ n: 2
5672
+ });
5673
+ await store.bufferSignal("c", {
5674
+ n: 3
5675
+ });
5676
+ await store.bufferSignal("b", {
5677
+ n: 4
5678
+ });
5679
+ await store.bufferSignal("a_", {
5680
+ n: 5
5681
+ });
5682
+ expect3(await take([
5683
+ "a",
5684
+ "b"
5685
+ ])).toEqual({
5686
+ token: "b",
5687
+ payload: {
5688
+ n: 1
5689
+ }
5690
+ });
5691
+ expect3(await take([
5692
+ "a",
5693
+ "b",
5694
+ "a"
5695
+ ])).toEqual({
5696
+ token: "a",
5697
+ payload: {
5698
+ n: 2
5699
+ }
5700
+ });
5701
+ expect3(await take([
5702
+ "a",
5703
+ "b"
5704
+ ])).toEqual({
5705
+ token: "b",
5706
+ payload: {
5707
+ n: 4
5708
+ }
5709
+ });
5710
+ expect3(await take([
5711
+ "a",
5712
+ "b"
5713
+ ])).toBeNull();
5714
+ expect3(await store.takeBufferedSignal("c")).toEqual({
5715
+ payload: {
5716
+ n: 3
5717
+ }
5718
+ });
5719
+ expect3(await store.takeBufferedSignal("a_")).toEqual({
5720
+ payload: {
5721
+ n: 5
5722
+ }
5723
+ });
5724
+ });
5725
+ t("never hands one buffered signal to two concurrent takers among tokens", async () => {
5726
+ const take = store.takeOldestBufferedSignal?.bind(store);
5727
+ const peerTake = peerStore.takeOldestBufferedSignal?.bind(peerStore);
5728
+ if (!take || !peerTake) return;
5729
+ for (let i = 0; i < 6; i += 1) await store.bufferSignal(i % 2 ? "x" : "y", i);
5730
+ const taken = await Promise.all(Array.from({
5731
+ length: 10
5732
+ }, (_, i) => (i % 2 ? take : peerTake)([
5733
+ "x",
5734
+ "y"
5735
+ ])));
5736
+ const payloads = taken.flatMap((t2) => t2 ? [
5737
+ t2.payload
5738
+ ] : []);
5739
+ expect3(payloads.sort()).toEqual([
5740
+ 0,
5741
+ 1,
5742
+ 2,
5743
+ 3,
5744
+ 4,
5745
+ 5
5746
+ ]);
5747
+ });
5748
+ t("records a signal id once per token until it expires (signal dedupe)", async () => {
5749
+ const claim = store.claimSignalId?.bind(store);
5750
+ expect3(claim).toBeTypeOf("function");
5751
+ expect3(store.releaseSignalId).toBeTypeOf("function");
5752
+ expect3(store.deleteExpiredSignalIds).toBeTypeOf("function");
5753
+ if (!claim) return;
5754
+ expect3(await claim("paid:o1", "evt-1", NOW + DAY, NOW)).toBe(true);
5755
+ expect3(await claim("paid:o1", "evt-1", NOW + 2 * DAY, NOW + 1e3)).toBe(false);
5756
+ expect3(await claim("paid:o2", "evt-1", NOW + DAY, NOW)).toBe(true);
5757
+ expect3(await claim("paid:o1", "evt-1", NOW + 3 * DAY, NOW + DAY)).toBe(true);
5758
+ expect3(await claim("paid:o1", "evt-1", NOW + 4 * DAY, NOW + 2 * DAY)).toBe(false);
5759
+ await store.releaseSignalId?.("paid:o1", "evt-1");
5760
+ expect3(await claim("paid:o1", "evt-1", NOW + DAY, NOW)).toBe(true);
5761
+ await store.releaseSignalId?.("never", "claimed");
5762
+ });
5763
+ t("records a signal id for exactly one of several concurrent claims", async () => {
5764
+ const claim = store.claimSignalId?.bind(store);
5765
+ const peerClaim = peerStore.claimSignalId?.bind(peerStore);
5766
+ if (!claim || !peerClaim) return;
5767
+ const fresh = await Promise.all(Array.from({
5768
+ length: 8
5769
+ }, (_, i) => (i % 2 ? claim : peerClaim)("t", "race", NOW + DAY, NOW)));
5770
+ expect3(fresh.filter(Boolean)).toHaveLength(1);
5771
+ const later = NOW + 2 * DAY;
5772
+ const again = await Promise.all(Array.from({
5773
+ length: 8
5774
+ }, (_, i) => (i % 2 ? claim : peerClaim)("t", "race", later + DAY, later)));
5775
+ expect3(again.filter(Boolean)).toHaveLength(1);
5776
+ });
5777
+ t("deletes expired signal ids in bounded batches", async () => {
5778
+ const claim = store.claimSignalId?.bind(store);
5779
+ const sweep = store.deleteExpiredSignalIds?.bind(store);
5780
+ if (!claim || !sweep) return;
5781
+ for (let i = 0; i < 5; i += 1) await claim("t", `old-${i}`, NOW + i, NOW - DAY);
5782
+ await claim("t", "fresh", NOW + DAY, NOW);
5783
+ expect3(await sweep(NOW + 4, 3)).toBe(3);
5784
+ expect3(await sweep(NOW + 4, 3)).toBe(2);
5785
+ expect3(await sweep(NOW + 4, 3)).toBe(0);
5786
+ expect3(await claim("t", "fresh", NOW + DAY, NOW)).toBe(false);
5787
+ expect3(await claim("t", "old-0", NOW + DAY, NOW + 5)).toBe(true);
5788
+ });
5021
5789
  t("buffers events per name, oldest-first, and lists them without consuming", async () => {
5022
5790
  await store.bufferEvent({
5023
5791
  name: "order.paid",