@dudousxd/nestjs-durable-testing 0.23.0 → 0.23.2
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.d.ts +6 -0
- package/dist/index.js +537 -1
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -262,6 +262,12 @@ interface StateStoreContext {
|
|
|
262
262
|
* for a wide batch, not one per row.
|
|
263
263
|
*/
|
|
264
264
|
statements?: () => number;
|
|
265
|
+
/**
|
|
266
|
+
* Every statement the store has sent to its database so far, each as its SQL text followed by its
|
|
267
|
+
* parameters (from the driver's query log). When given, the contract checks that settling a step
|
|
268
|
+
* sends none of the step's input.
|
|
269
|
+
*/
|
|
270
|
+
statementLog?: () => string[];
|
|
265
271
|
}
|
|
266
272
|
/** Builds a fresh, empty store for one test. Called once per `it` (in `beforeEach`). */
|
|
267
273
|
type StateStoreFactory = () => Promise<StateStoreContext>;
|
package/dist/index.js
CHANGED
|
@@ -834,6 +834,15 @@ var checkpoint = /* @__PURE__ */ __name((over = {}) => ({
|
|
|
834
834
|
finishedAt: at,
|
|
835
835
|
...over
|
|
836
836
|
}), "checkpoint");
|
|
837
|
+
async function loggedSince(log, before) {
|
|
838
|
+
let seen = -1;
|
|
839
|
+
for (let i = 0; i < 50 && log().length !== seen; i += 1) {
|
|
840
|
+
seen = log().length;
|
|
841
|
+
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
842
|
+
}
|
|
843
|
+
return log().slice(before);
|
|
844
|
+
}
|
|
845
|
+
__name(loggedSince, "loggedSince");
|
|
837
846
|
function runStateStoreContract(name, makeStore) {
|
|
838
847
|
describe2(`StateStore contract: ${name}`, () => {
|
|
839
848
|
let store;
|
|
@@ -842,6 +851,7 @@ function runStateStoreContract(name, makeStore) {
|
|
|
842
851
|
let supportsAsyncTransaction = true;
|
|
843
852
|
let supportsTagFilter = true;
|
|
844
853
|
let statements;
|
|
854
|
+
let statementLog;
|
|
845
855
|
let skipReason;
|
|
846
856
|
let loggedSkip = false;
|
|
847
857
|
beforeEach(async () => {
|
|
@@ -853,6 +863,7 @@ function runStateStoreContract(name, makeStore) {
|
|
|
853
863
|
supportsAsyncTransaction = ctx.supportsAsyncTransaction ?? true;
|
|
854
864
|
supportsTagFilter = ctx.supportsTagFilter ?? true;
|
|
855
865
|
statements = ctx.statements;
|
|
866
|
+
statementLog = ctx.statementLog;
|
|
856
867
|
skipReason = void 0;
|
|
857
868
|
} catch (err) {
|
|
858
869
|
if (isUnavailable(err) && !process.env.DURABLE_TEST_REQUIRE_BACKENDS) {
|
|
@@ -2306,6 +2317,28 @@ function runStateStoreContract(name, makeStore) {
|
|
|
2306
2317
|
expect3(await store.countCheckpoints?.("r1", 0, n - 1, [
|
|
2307
2318
|
"completed"
|
|
2308
2319
|
])).toBe(n);
|
|
2320
|
+
if (!store.settleCheckpointsIfPending) return;
|
|
2321
|
+
await store.saveCheckpoints?.(rows.map((row) => ({
|
|
2322
|
+
...row,
|
|
2323
|
+
seq: row.seq + n
|
|
2324
|
+
})));
|
|
2325
|
+
const settlements = rows.map((row) => ({
|
|
2326
|
+
runId: "r1",
|
|
2327
|
+
seq: row.seq + n,
|
|
2328
|
+
status: "completed",
|
|
2329
|
+
output: {
|
|
2330
|
+
seq: row.seq
|
|
2331
|
+
},
|
|
2332
|
+
startedAt: row.startedAt,
|
|
2333
|
+
finishedAt: row.finishedAt
|
|
2334
|
+
}));
|
|
2335
|
+
expect3(await sent(async () => {
|
|
2336
|
+
wrote = await store.settleCheckpointsIfPending?.(settlements) ?? [];
|
|
2337
|
+
})).toBeLessThanOrEqual(limit);
|
|
2338
|
+
expect3(wrote.every((w) => w)).toBe(true);
|
|
2339
|
+
expect3(await store.countCheckpoints?.("r1", n, 2 * n - 1, [
|
|
2340
|
+
"completed"
|
|
2341
|
+
])).toBe(n);
|
|
2309
2342
|
});
|
|
2310
2343
|
t("saveCheckpointsIfPending writes a wide batch", async () => {
|
|
2311
2344
|
if (!store.saveCheckpointsIfPending) return;
|
|
@@ -2336,6 +2369,457 @@ function runStateStoreContract(name, makeStore) {
|
|
|
2336
2369
|
seq: n - 1
|
|
2337
2370
|
});
|
|
2338
2371
|
});
|
|
2372
|
+
t("settleCheckpointsIfPending writes only what a result changes, row by row", async () => {
|
|
2373
|
+
expect3(store.settleCheckpointsIfPending).toBeDefined();
|
|
2374
|
+
if (!store.settleCheckpointsIfPending) return;
|
|
2375
|
+
expect3(await store.settleCheckpointsIfPending([])).toEqual([]);
|
|
2376
|
+
await store.createRun(run());
|
|
2377
|
+
await store.createRun(run({
|
|
2378
|
+
id: "r2"
|
|
2379
|
+
}));
|
|
2380
|
+
const pending = /* @__PURE__ */ __name((runId, seq) => checkpoint({
|
|
2381
|
+
runId,
|
|
2382
|
+
seq,
|
|
2383
|
+
name: "ask",
|
|
2384
|
+
kind: "remote",
|
|
2385
|
+
stepId: `${runId}:${seq}`,
|
|
2386
|
+
status: "pending",
|
|
2387
|
+
input: {
|
|
2388
|
+
prompt: `p${seq}`
|
|
2389
|
+
},
|
|
2390
|
+
output: void 0,
|
|
2391
|
+
attempts: 2,
|
|
2392
|
+
workerGroup: "llm",
|
|
2393
|
+
parallelGroup: "all:0:2",
|
|
2394
|
+
wakeAt: 5e3
|
|
2395
|
+
}), "pending");
|
|
2396
|
+
for (const seq of [
|
|
2397
|
+
0,
|
|
2398
|
+
1,
|
|
2399
|
+
2,
|
|
2400
|
+
3
|
|
2401
|
+
]) await store.saveCheckpoint(pending("r1", seq));
|
|
2402
|
+
await store.saveCheckpoint(pending("r2", 0));
|
|
2403
|
+
await store.saveCheckpoint({
|
|
2404
|
+
...pending("r1", 1),
|
|
2405
|
+
status: "completed",
|
|
2406
|
+
output: {
|
|
2407
|
+
first: 1
|
|
2408
|
+
}
|
|
2409
|
+
});
|
|
2410
|
+
const later = new Date(at.getTime() + 6e4);
|
|
2411
|
+
const done = /* @__PURE__ */ __name((runId, seq, output) => ({
|
|
2412
|
+
runId,
|
|
2413
|
+
seq,
|
|
2414
|
+
status: "completed",
|
|
2415
|
+
output,
|
|
2416
|
+
startedAt: new Date(at.getTime() + 1e3),
|
|
2417
|
+
finishedAt: later
|
|
2418
|
+
}), "done");
|
|
2419
|
+
const wrote = await store.settleCheckpointsIfPending([
|
|
2420
|
+
done("r1", 0, {
|
|
2421
|
+
n: 0
|
|
2422
|
+
}),
|
|
2423
|
+
// Already settled: refused.
|
|
2424
|
+
done("r1", 1, {
|
|
2425
|
+
n: 1
|
|
2426
|
+
}),
|
|
2427
|
+
// A second settlement of the same row: the first one settled it, so refused.
|
|
2428
|
+
done("r1", 0, {
|
|
2429
|
+
n: "dup"
|
|
2430
|
+
}),
|
|
2431
|
+
{
|
|
2432
|
+
runId: "r1",
|
|
2433
|
+
seq: 2,
|
|
2434
|
+
status: "failed",
|
|
2435
|
+
error: {
|
|
2436
|
+
message: "boom"
|
|
2437
|
+
},
|
|
2438
|
+
events: [
|
|
2439
|
+
{
|
|
2440
|
+
at: 7,
|
|
2441
|
+
level: "error",
|
|
2442
|
+
message: "boom"
|
|
2443
|
+
}
|
|
2444
|
+
],
|
|
2445
|
+
startedAt: at,
|
|
2446
|
+
finishedAt: later
|
|
2447
|
+
},
|
|
2448
|
+
// No such row: nothing is written.
|
|
2449
|
+
done("r1", 9, {
|
|
2450
|
+
n: 9
|
|
2451
|
+
}),
|
|
2452
|
+
// Another run's row, same seq as one above.
|
|
2453
|
+
done("r2", 0, {
|
|
2454
|
+
n: "r2"
|
|
2455
|
+
})
|
|
2456
|
+
]);
|
|
2457
|
+
expect3(wrote).toEqual([
|
|
2458
|
+
true,
|
|
2459
|
+
false,
|
|
2460
|
+
false,
|
|
2461
|
+
true,
|
|
2462
|
+
false,
|
|
2463
|
+
true
|
|
2464
|
+
]);
|
|
2465
|
+
const r1 = await store.listCheckpoints("r1");
|
|
2466
|
+
expect3(r1.map((c) => [
|
|
2467
|
+
c.seq,
|
|
2468
|
+
c.status,
|
|
2469
|
+
c.output
|
|
2470
|
+
])).toEqual([
|
|
2471
|
+
[
|
|
2472
|
+
0,
|
|
2473
|
+
"completed",
|
|
2474
|
+
{
|
|
2475
|
+
n: 0
|
|
2476
|
+
}
|
|
2477
|
+
],
|
|
2478
|
+
[
|
|
2479
|
+
1,
|
|
2480
|
+
"completed",
|
|
2481
|
+
{
|
|
2482
|
+
first: 1
|
|
2483
|
+
}
|
|
2484
|
+
],
|
|
2485
|
+
[
|
|
2486
|
+
2,
|
|
2487
|
+
"failed",
|
|
2488
|
+
void 0
|
|
2489
|
+
],
|
|
2490
|
+
[
|
|
2491
|
+
3,
|
|
2492
|
+
"pending",
|
|
2493
|
+
void 0
|
|
2494
|
+
]
|
|
2495
|
+
]);
|
|
2496
|
+
const [first, , failed] = r1;
|
|
2497
|
+
expect3(first?.input).toEqual({
|
|
2498
|
+
prompt: "p0"
|
|
2499
|
+
});
|
|
2500
|
+
expect3(first?.name).toBe("ask");
|
|
2501
|
+
expect3(first?.kind).toBe("remote");
|
|
2502
|
+
expect3(first?.stepId).toBe("r1:0");
|
|
2503
|
+
expect3(first?.attempts).toBe(2);
|
|
2504
|
+
expect3(first?.workerGroup).toBe("llm");
|
|
2505
|
+
expect3(first?.parallelGroup).toBe("all:0:2");
|
|
2506
|
+
expect3(first?.wakeAt).toBe(5e3);
|
|
2507
|
+
expect3(first?.enqueuedAt.getTime()).toBe(at.getTime());
|
|
2508
|
+
expect3(first?.startedAt.getTime()).toBe(at.getTime() + 1e3);
|
|
2509
|
+
expect3(first?.finishedAt.getTime()).toBe(later.getTime());
|
|
2510
|
+
expect3(first?.error).toBeUndefined();
|
|
2511
|
+
expect3(failed?.input).toEqual({
|
|
2512
|
+
prompt: "p2"
|
|
2513
|
+
});
|
|
2514
|
+
expect3(failed?.error).toEqual({
|
|
2515
|
+
message: "boom"
|
|
2516
|
+
});
|
|
2517
|
+
expect3(failed?.events).toEqual([
|
|
2518
|
+
{
|
|
2519
|
+
at: 7,
|
|
2520
|
+
level: "error",
|
|
2521
|
+
message: "boom"
|
|
2522
|
+
}
|
|
2523
|
+
]);
|
|
2524
|
+
expect3(await store.getCheckpoint("r1", 9)).toBeNull();
|
|
2525
|
+
expect3((await store.getCheckpoint("r2", 0))?.output).toEqual({
|
|
2526
|
+
n: "r2"
|
|
2527
|
+
});
|
|
2528
|
+
expect3((await store.getCheckpoint("r2", 0))?.input).toEqual({
|
|
2529
|
+
prompt: "p0"
|
|
2530
|
+
});
|
|
2531
|
+
});
|
|
2532
|
+
t("settleCheckpointsIfPending sends none of the input of the steps it settles", async () => {
|
|
2533
|
+
if (!statementLog || !store.settleCheckpointsIfPending) return;
|
|
2534
|
+
await store.createRun(run());
|
|
2535
|
+
const prompt = `prompt-${"x".repeat(1e5)}`;
|
|
2536
|
+
for (const seq of [
|
|
2537
|
+
0,
|
|
2538
|
+
1
|
|
2539
|
+
]) {
|
|
2540
|
+
await store.saveCheckpoint(checkpoint({
|
|
2541
|
+
seq,
|
|
2542
|
+
kind: "remote",
|
|
2543
|
+
stepId: `r1:${seq}`,
|
|
2544
|
+
status: "pending",
|
|
2545
|
+
input: {
|
|
2546
|
+
prompt
|
|
2547
|
+
},
|
|
2548
|
+
output: void 0
|
|
2549
|
+
}));
|
|
2550
|
+
}
|
|
2551
|
+
const before = statementLog().length;
|
|
2552
|
+
const settle2 = /* @__PURE__ */ __name((seqs) => store.settleCheckpointsIfPending?.(seqs.map((seq) => ({
|
|
2553
|
+
runId: "r1",
|
|
2554
|
+
seq,
|
|
2555
|
+
status: "completed",
|
|
2556
|
+
output: {
|
|
2557
|
+
answer: seq
|
|
2558
|
+
},
|
|
2559
|
+
startedAt: at,
|
|
2560
|
+
finishedAt: at
|
|
2561
|
+
}))), "settle");
|
|
2562
|
+
expect3(await settle2([
|
|
2563
|
+
0
|
|
2564
|
+
])).toEqual([
|
|
2565
|
+
true
|
|
2566
|
+
]);
|
|
2567
|
+
expect3(await settle2([
|
|
2568
|
+
1,
|
|
2569
|
+
0
|
|
2570
|
+
])).toEqual([
|
|
2571
|
+
true,
|
|
2572
|
+
false
|
|
2573
|
+
]);
|
|
2574
|
+
const sent = await loggedSince(statementLog, before);
|
|
2575
|
+
expect3(sent.length).toBeGreaterThan(0);
|
|
2576
|
+
expect3(sent.some((statement) => statement.includes("xxxxxxxxxx"))).toBe(false);
|
|
2577
|
+
expect3(sent.join("").length).toBeLessThan(2e4);
|
|
2578
|
+
expect3((await store.getCheckpoint("r1", 1))?.input).toEqual({
|
|
2579
|
+
prompt
|
|
2580
|
+
});
|
|
2581
|
+
});
|
|
2582
|
+
t("getCheckpointsForSettle reads getCheckpoints rows without their input", async () => {
|
|
2583
|
+
expect3(store.getCheckpointsForSettle).toBeDefined();
|
|
2584
|
+
if (!store.getCheckpointsForSettle) return;
|
|
2585
|
+
expect3(await store.getCheckpointsForSettle("r1", [])).toEqual([]);
|
|
2586
|
+
await store.createRun(run());
|
|
2587
|
+
for (const seq of [
|
|
2588
|
+
0,
|
|
2589
|
+
1,
|
|
2590
|
+
2
|
|
2591
|
+
]) {
|
|
2592
|
+
await store.saveCheckpoint(checkpoint({
|
|
2593
|
+
seq,
|
|
2594
|
+
kind: "remote",
|
|
2595
|
+
stepId: `r1:${seq}`,
|
|
2596
|
+
status: seq === 0 ? "completed" : "pending",
|
|
2597
|
+
input: {
|
|
2598
|
+
prompt: `p${seq}`
|
|
2599
|
+
},
|
|
2600
|
+
output: seq === 0 ? {
|
|
2601
|
+
answer: 0
|
|
2602
|
+
} : void 0,
|
|
2603
|
+
events: [
|
|
2604
|
+
{
|
|
2605
|
+
at: seq,
|
|
2606
|
+
level: "info",
|
|
2607
|
+
message: `m${seq}`
|
|
2608
|
+
}
|
|
2609
|
+
],
|
|
2610
|
+
workerGroup: "llm",
|
|
2611
|
+
parallelGroup: "all:0:3",
|
|
2612
|
+
wakeAt: 5e3 + seq
|
|
2613
|
+
}));
|
|
2614
|
+
}
|
|
2615
|
+
const full = await store.getCheckpoints?.("r1", [
|
|
2616
|
+
2,
|
|
2617
|
+
0,
|
|
2618
|
+
9
|
|
2619
|
+
]);
|
|
2620
|
+
const read = await store.getCheckpointsForSettle("r1", [
|
|
2621
|
+
2,
|
|
2622
|
+
0,
|
|
2623
|
+
9
|
|
2624
|
+
]);
|
|
2625
|
+
expect3(read.map((c) => c.seq)).toEqual([
|
|
2626
|
+
0,
|
|
2627
|
+
2
|
|
2628
|
+
]);
|
|
2629
|
+
expect3(read.map((c) => c.input)).toEqual([
|
|
2630
|
+
void 0,
|
|
2631
|
+
void 0
|
|
2632
|
+
]);
|
|
2633
|
+
const withoutInput = /* @__PURE__ */ __name(({ input: _input, ...rest }) => rest, "withoutInput");
|
|
2634
|
+
expect3(read.map(withoutInput)).toEqual((full ?? []).map(withoutInput));
|
|
2635
|
+
expect3((await store.getCheckpoint("r1", 2))?.input).toEqual({
|
|
2636
|
+
prompt: "p2"
|
|
2637
|
+
});
|
|
2638
|
+
});
|
|
2639
|
+
t("getCheckpointsForSettle reads no input column", async () => {
|
|
2640
|
+
if (!statementLog || !store.getCheckpointsForSettle) return;
|
|
2641
|
+
await store.createRun(run());
|
|
2642
|
+
for (const seq of [
|
|
2643
|
+
0,
|
|
2644
|
+
1
|
|
2645
|
+
]) {
|
|
2646
|
+
await store.saveCheckpoint(checkpoint({
|
|
2647
|
+
seq,
|
|
2648
|
+
kind: "remote",
|
|
2649
|
+
stepId: `r1:${seq}`,
|
|
2650
|
+
status: "pending",
|
|
2651
|
+
input: {
|
|
2652
|
+
prompt: "x".repeat(1e5)
|
|
2653
|
+
},
|
|
2654
|
+
output: void 0
|
|
2655
|
+
}));
|
|
2656
|
+
}
|
|
2657
|
+
const before = statementLog().length;
|
|
2658
|
+
const read = await store.getCheckpointsForSettle("r1", [
|
|
2659
|
+
0,
|
|
2660
|
+
1
|
|
2661
|
+
]);
|
|
2662
|
+
const sent = await loggedSince(statementLog, before);
|
|
2663
|
+
expect3(read).toHaveLength(2);
|
|
2664
|
+
expect3(sent.length).toBeGreaterThan(0);
|
|
2665
|
+
expect3(sent.filter((statement) => /\binput\b|\*\s+from/i.test(statement))).toEqual([]);
|
|
2666
|
+
});
|
|
2667
|
+
t("settleCheckpointsIfPending settles a wide batch", async () => {
|
|
2668
|
+
if (!store.settleCheckpointsIfPending) return;
|
|
2669
|
+
await store.createRun(run());
|
|
2670
|
+
const n = 600;
|
|
2671
|
+
const rows = Array.from({
|
|
2672
|
+
length: n
|
|
2673
|
+
}, (_, seq) => checkpoint({
|
|
2674
|
+
seq,
|
|
2675
|
+
stepId: `r1:${seq}`,
|
|
2676
|
+
status: "pending",
|
|
2677
|
+
input: {
|
|
2678
|
+
seq
|
|
2679
|
+
},
|
|
2680
|
+
output: void 0
|
|
2681
|
+
}));
|
|
2682
|
+
if (store.saveCheckpoints) await store.saveCheckpoints(rows);
|
|
2683
|
+
else for (const row of rows) await store.saveCheckpoint(row);
|
|
2684
|
+
const wrote = await store.settleCheckpointsIfPending(rows.map((row) => ({
|
|
2685
|
+
runId: row.runId,
|
|
2686
|
+
seq: row.seq,
|
|
2687
|
+
status: "completed",
|
|
2688
|
+
output: {
|
|
2689
|
+
seq: row.seq
|
|
2690
|
+
},
|
|
2691
|
+
startedAt: at,
|
|
2692
|
+
finishedAt: at
|
|
2693
|
+
})));
|
|
2694
|
+
expect3(wrote).toHaveLength(n);
|
|
2695
|
+
expect3(wrote.every((w) => w === true)).toBe(true);
|
|
2696
|
+
expect3(await store.countCheckpoints?.("r1", 0, n - 1, [
|
|
2697
|
+
"completed"
|
|
2698
|
+
])).toBe(n);
|
|
2699
|
+
const last = await store.getCheckpoint("r1", n - 1);
|
|
2700
|
+
expect3(last?.output).toEqual({
|
|
2701
|
+
seq: n - 1
|
|
2702
|
+
});
|
|
2703
|
+
expect3(last?.input).toEqual({
|
|
2704
|
+
seq: n - 1
|
|
2705
|
+
});
|
|
2706
|
+
});
|
|
2707
|
+
t("listCheckpointsForReplay lists every checkpoint, a completed one without its input", async () => {
|
|
2708
|
+
expect3(store.listCheckpointsForReplay).toBeDefined();
|
|
2709
|
+
if (!store.listCheckpointsForReplay) return;
|
|
2710
|
+
await store.createRun(run());
|
|
2711
|
+
await store.createRun(run({
|
|
2712
|
+
id: "r2"
|
|
2713
|
+
}));
|
|
2714
|
+
const remote = /* @__PURE__ */ __name((seq, over) => checkpoint({
|
|
2715
|
+
seq,
|
|
2716
|
+
name: `ask-${seq}`,
|
|
2717
|
+
kind: "remote",
|
|
2718
|
+
stepId: `r1:${seq}`,
|
|
2719
|
+
input: {
|
|
2720
|
+
prompt: `p${seq}`
|
|
2721
|
+
},
|
|
2722
|
+
workerGroup: "llm",
|
|
2723
|
+
...over
|
|
2724
|
+
}), "remote");
|
|
2725
|
+
await store.saveCheckpoint(remote(0, {
|
|
2726
|
+
output: {
|
|
2727
|
+
answer: 0
|
|
2728
|
+
}
|
|
2729
|
+
}));
|
|
2730
|
+
await store.saveCheckpoint(remote(1, {
|
|
2731
|
+
status: "pending",
|
|
2732
|
+
output: void 0,
|
|
2733
|
+
wakeAt: 9e3
|
|
2734
|
+
}));
|
|
2735
|
+
await store.saveCheckpoint(remote(2, {
|
|
2736
|
+
status: "failed",
|
|
2737
|
+
output: void 0,
|
|
2738
|
+
error: {
|
|
2739
|
+
message: "boom"
|
|
2740
|
+
}
|
|
2741
|
+
}));
|
|
2742
|
+
await store.saveCheckpoint(checkpoint({
|
|
2743
|
+
seq: 3,
|
|
2744
|
+
stepId: "r1:3",
|
|
2745
|
+
status: "running"
|
|
2746
|
+
}));
|
|
2747
|
+
await store.saveCheckpoint(remote(4, {
|
|
2748
|
+
output: {
|
|
2749
|
+
answer: 4
|
|
2750
|
+
},
|
|
2751
|
+
parallelGroup: "all:4:1"
|
|
2752
|
+
}));
|
|
2753
|
+
await store.saveCheckpoint(checkpoint({
|
|
2754
|
+
runId: "r2",
|
|
2755
|
+
stepId: "r2:0",
|
|
2756
|
+
input: {
|
|
2757
|
+
other: true
|
|
2758
|
+
}
|
|
2759
|
+
}));
|
|
2760
|
+
const full = await store.listCheckpoints("r1");
|
|
2761
|
+
const replay = await store.listCheckpointsForReplay("r1");
|
|
2762
|
+
expect3(replay.map((c) => c.seq)).toEqual([
|
|
2763
|
+
0,
|
|
2764
|
+
1,
|
|
2765
|
+
2,
|
|
2766
|
+
3,
|
|
2767
|
+
4
|
|
2768
|
+
]);
|
|
2769
|
+
expect3(replay.map((c) => c.input)).toEqual([
|
|
2770
|
+
void 0,
|
|
2771
|
+
{
|
|
2772
|
+
prompt: "p1"
|
|
2773
|
+
},
|
|
2774
|
+
{
|
|
2775
|
+
prompt: "p2"
|
|
2776
|
+
},
|
|
2777
|
+
void 0,
|
|
2778
|
+
void 0
|
|
2779
|
+
]);
|
|
2780
|
+
const withoutInput = /* @__PURE__ */ __name(({ input: _input, ...rest }) => rest, "withoutInput");
|
|
2781
|
+
expect3(replay.map(withoutInput)).toEqual(full.map(withoutInput));
|
|
2782
|
+
expect3(full[0]?.input).toEqual({
|
|
2783
|
+
prompt: "p0"
|
|
2784
|
+
});
|
|
2785
|
+
expect3((await store.getCheckpoint("r1", 4))?.input).toEqual({
|
|
2786
|
+
prompt: "p4"
|
|
2787
|
+
});
|
|
2788
|
+
expect3(await store.listCheckpointsForReplay("missing")).toEqual([]);
|
|
2789
|
+
});
|
|
2790
|
+
t("listCheckpointsForReplay reads no input column for a run whose steps completed", async () => {
|
|
2791
|
+
if (!statementLog || !store.listCheckpointsForReplay) return;
|
|
2792
|
+
await store.createRun(run());
|
|
2793
|
+
for (const seq of [
|
|
2794
|
+
0,
|
|
2795
|
+
1
|
|
2796
|
+
]) {
|
|
2797
|
+
await store.saveCheckpoint(checkpoint({
|
|
2798
|
+
seq,
|
|
2799
|
+
kind: "remote",
|
|
2800
|
+
stepId: `r1:${seq}`,
|
|
2801
|
+
input: {
|
|
2802
|
+
prompt: "x".repeat(1e5)
|
|
2803
|
+
},
|
|
2804
|
+
output: {
|
|
2805
|
+
answer: seq
|
|
2806
|
+
}
|
|
2807
|
+
}));
|
|
2808
|
+
}
|
|
2809
|
+
const before = statementLog().length;
|
|
2810
|
+
const replay = await store.listCheckpointsForReplay("r1");
|
|
2811
|
+
const sent = await loggedSince(statementLog, before);
|
|
2812
|
+
expect3(replay.map((c) => c.output)).toEqual([
|
|
2813
|
+
{
|
|
2814
|
+
answer: 0
|
|
2815
|
+
},
|
|
2816
|
+
{
|
|
2817
|
+
answer: 1
|
|
2818
|
+
}
|
|
2819
|
+
]);
|
|
2820
|
+
expect3(sent.length).toBeGreaterThan(0);
|
|
2821
|
+
expect3(sent.filter((statement) => /\binput\b|\*\s+from/i.test(statement))).toEqual([]);
|
|
2822
|
+
});
|
|
2339
2823
|
t("upserts checkpoints, reads them by (runId, seq), and lists them ordered by seq", async () => {
|
|
2340
2824
|
await store.createRun(run());
|
|
2341
2825
|
await store.saveCheckpoint(checkpoint());
|
|
@@ -4319,6 +4803,56 @@ function runStateStoreContract(name, makeStore) {
|
|
|
4319
4803
|
expect3(await store.takeBufferedSignal("child:a")).toBeNull();
|
|
4320
4804
|
expect3(await store.takeBufferedSignal("child:b")).toBeNull();
|
|
4321
4805
|
});
|
|
4806
|
+
t("a sweep of buffered child completions sends a few statements per page, not one per row", async () => {
|
|
4807
|
+
if (!statements || !store.listBufferedSignals || !store.deleteBufferedSignals) return;
|
|
4808
|
+
if (!store.saveCheckpoints) return;
|
|
4809
|
+
const parent = "wide";
|
|
4810
|
+
await store.createRun(run({
|
|
4811
|
+
id: parent,
|
|
4812
|
+
status: "suspended"
|
|
4813
|
+
}));
|
|
4814
|
+
const fan = 4e3;
|
|
4815
|
+
for (let from = 0; from < fan; from += 500) {
|
|
4816
|
+
await store.saveCheckpoints(Array.from({
|
|
4817
|
+
length: 500
|
|
4818
|
+
}, (_, k) => {
|
|
4819
|
+
const i = from + k;
|
|
4820
|
+
return checkpoint({
|
|
4821
|
+
runId: parent,
|
|
4822
|
+
seq: i,
|
|
4823
|
+
stepId: `${parent}:${i}`,
|
|
4824
|
+
name: `signal:child:${parent}.all.0.${i}`,
|
|
4825
|
+
kind: "signal",
|
|
4826
|
+
output: {
|
|
4827
|
+
ok: true,
|
|
4828
|
+
value: {
|
|
4829
|
+
i
|
|
4830
|
+
}
|
|
4831
|
+
},
|
|
4832
|
+
parallelGroup: `all:0:n${fan}`
|
|
4833
|
+
});
|
|
4834
|
+
}));
|
|
4835
|
+
}
|
|
4836
|
+
const rows = 500;
|
|
4837
|
+
for (let k = 0; k < rows; k++) {
|
|
4838
|
+
const i = 2e3 + k;
|
|
4839
|
+
await store.bufferSignal(`child:${parent}.all.0.${i}`, {
|
|
4840
|
+
ok: true,
|
|
4841
|
+
value: {
|
|
4842
|
+
i
|
|
4843
|
+
}
|
|
4844
|
+
});
|
|
4845
|
+
}
|
|
4846
|
+
const engine = new WorkflowEngine3({
|
|
4847
|
+
store
|
|
4848
|
+
});
|
|
4849
|
+
const before = statements();
|
|
4850
|
+
const deleted = await engine.sweepBufferedSignals();
|
|
4851
|
+
const sent = statements() - before;
|
|
4852
|
+
expect3(deleted).toBe(rows);
|
|
4853
|
+
expect3(await store.listBufferedSignals("child:", void 0, 10)).toEqual([]);
|
|
4854
|
+
expect3(sent).toBeLessThanOrEqual(10);
|
|
4855
|
+
});
|
|
4322
4856
|
t("buffers signals and takes them FIFO per token", async () => {
|
|
4323
4857
|
await store.bufferSignal("sig", {
|
|
4324
4858
|
n: 1
|
|
@@ -5740,7 +6274,9 @@ function counted(inner, counts) {
|
|
|
5740
6274
|
return (...args) => {
|
|
5741
6275
|
const name = String(prop);
|
|
5742
6276
|
counts.calls.set(name, (counts.calls.get(name) ?? 0) + 1);
|
|
5743
|
-
if (name === "saveCheckpointsIfPending")
|
|
6277
|
+
if (name === "settleCheckpointsIfPending" || name === "saveCheckpointsIfPending") {
|
|
6278
|
+
counts.batches.push(args[0].length);
|
|
6279
|
+
}
|
|
5744
6280
|
if (name === "saveCheckpointIfPending") counts.batches.push(1);
|
|
5745
6281
|
return new Promise((resolve) => setImmediate(resolve)).then(() => value.apply(target, args));
|
|
5746
6282
|
};
|