@nagi-js/core 0.1.1-rc.20 → 0.1.1-rc.22
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/{chunk-VQTYTI6X.js → chunk-PUAV56FC.js} +922 -519
- package/dist/chunk-PUAV56FC.js.map +1 -0
- package/dist/index.d.ts +90 -115
- package/dist/index.js +693 -1390
- package/dist/index.js.map +1 -1
- package/dist/testing.d.ts +2 -1
- package/dist/testing.js +675 -214
- package/dist/testing.js.map +1 -1
- package/dist/{types-DFSYjJE3.d.ts → types-Ct5xHI4W.d.ts} +119 -125
- package/package.json +1 -1
- package/dist/chunk-VQTYTI6X.js.map +0 -1
package/dist/testing.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Facts, stepStateOf, InMemoryQueue, stepStatusOf, NagiConcurrencyConflictError } from './chunk-
|
|
1
|
+
import { Facts, isRunEnd, stepStateOf, InMemoryQueue, stepStatusOf, NagiConcurrencyConflictError } from './chunk-PUAV56FC.js';
|
|
2
2
|
|
|
3
3
|
// src/testing.ts
|
|
4
4
|
function passthroughSchema() {
|
|
@@ -97,7 +97,7 @@ async function stepStatus(s, runId, stepId) {
|
|
|
97
97
|
}
|
|
98
98
|
async function endRun(s, runId, status, at = /* @__PURE__ */ new Date()) {
|
|
99
99
|
const fact = status === "completed" ? Facts.flowCompleted(runId, null, at) : status === "failed" ? Facts.flowFailed(runId, { name: "E", message: "x" }, at) : Facts.flowCanceled(runId, { cause: "explicit", reason: "test" }, at);
|
|
100
|
-
await s.
|
|
100
|
+
await s.endRun(runId, fact);
|
|
101
101
|
}
|
|
102
102
|
async function stepView(s, runId, stepId) {
|
|
103
103
|
const d = await s.describe(runId);
|
|
@@ -110,6 +110,53 @@ async function claimableAgain(s, runId, stepId, what) {
|
|
|
110
110
|
`${what} must release the lease: claimStep at the same attempt still returns null`
|
|
111
111
|
);
|
|
112
112
|
}
|
|
113
|
+
var TRANSPORT_WAIT_MS = 5e3;
|
|
114
|
+
async function eventually(cond, msg) {
|
|
115
|
+
const deadline = Date.now() + TRANSPORT_WAIT_MS;
|
|
116
|
+
while (!cond()) {
|
|
117
|
+
ok(Date.now() < deadline, msg);
|
|
118
|
+
await sleep(10);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
function recordRuns(s) {
|
|
122
|
+
if (s.events === void 0) return null;
|
|
123
|
+
const seen = [];
|
|
124
|
+
s.events.watchRuns((e) => seen.push(e));
|
|
125
|
+
return seen;
|
|
126
|
+
}
|
|
127
|
+
function typesFor(seen, runId) {
|
|
128
|
+
return seen.filter((e) => e.runId === runId).map((e) => e.type);
|
|
129
|
+
}
|
|
130
|
+
async function drain(stream, what) {
|
|
131
|
+
const out = [];
|
|
132
|
+
const iter = stream[Symbol.asyncIterator]();
|
|
133
|
+
for (; ; ) {
|
|
134
|
+
const next = await Promise.race([
|
|
135
|
+
iter.next(),
|
|
136
|
+
sleep(TRANSPORT_WAIT_MS).then(() => null)
|
|
137
|
+
]);
|
|
138
|
+
if (next === null) {
|
|
139
|
+
await iter.return?.();
|
|
140
|
+
throw new StoreContractViolation(`${what}: the stream never closed`);
|
|
141
|
+
}
|
|
142
|
+
if (next.done === true) return out;
|
|
143
|
+
out.push(next.value);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
function failingQueue() {
|
|
147
|
+
const fail = async () => {
|
|
148
|
+
throw new Error("queue unavailable");
|
|
149
|
+
};
|
|
150
|
+
const q = {
|
|
151
|
+
enqueue: fail,
|
|
152
|
+
dequeue: fail,
|
|
153
|
+
ack: fail,
|
|
154
|
+
nack: fail,
|
|
155
|
+
extend: fail,
|
|
156
|
+
withTx: () => q
|
|
157
|
+
};
|
|
158
|
+
return q;
|
|
159
|
+
}
|
|
113
160
|
var storeContract = [
|
|
114
161
|
{
|
|
115
162
|
name: "tryStartRun: concurrent calls with the same runId produce exactly one flow.started",
|
|
@@ -166,6 +213,162 @@ var storeContract = [
|
|
|
166
213
|
eq((await s.describe(a))?.run.concurrencyKey, "k", "describe(a) key");
|
|
167
214
|
}
|
|
168
215
|
},
|
|
216
|
+
{
|
|
217
|
+
// nagi#29: a phantom superseder is a canceled_by_run_id pointing at a run
|
|
218
|
+
// row that does not exist. The canonical stores cannot produce one because
|
|
219
|
+
// cancel-prior and insert-new share a transaction; this pins that property
|
|
220
|
+
// on the CONTRACT so a custom Store cannot regress into the bug silently.
|
|
221
|
+
name: "tryStartRun: a superseded run's canceledByRunId always references a run that exists",
|
|
222
|
+
async run(h) {
|
|
223
|
+
const s = await h.makeStore({ leaseMs: LEASE_MS });
|
|
224
|
+
const flowId = fid();
|
|
225
|
+
const a = rid();
|
|
226
|
+
const b = rid();
|
|
227
|
+
await startRun(s, a, { flowId, concurrencyKey: "k" });
|
|
228
|
+
await startRun(s, b, { flowId, concurrencyKey: "k" });
|
|
229
|
+
const superseder = (await s.describe(a))?.run.canceledByRunId;
|
|
230
|
+
ok(superseder !== void 0, "a records a superseder");
|
|
231
|
+
ok(
|
|
232
|
+
await s.describe(superseder) !== null,
|
|
233
|
+
"the superseder run row must exist \u2014 a dangling reference is nagi#29"
|
|
234
|
+
);
|
|
235
|
+
eq(
|
|
236
|
+
(await s.loadRunState(superseder)).facts.length > 0,
|
|
237
|
+
true,
|
|
238
|
+
"the superseder must have a durable fact log, not just a reference"
|
|
239
|
+
);
|
|
240
|
+
}
|
|
241
|
+
},
|
|
242
|
+
{
|
|
243
|
+
// The other half of the same atomicity claim: a start that does NOT happen
|
|
244
|
+
// must not cancel anything. An implementation that cancels priors before
|
|
245
|
+
// confirming its own insert leaves exactly the orphaned state nagi#29 saw.
|
|
246
|
+
name: "tryStartRun: a refused start cancels no prior run",
|
|
247
|
+
async run(h) {
|
|
248
|
+
const s = await h.makeStore({ leaseMs: LEASE_MS });
|
|
249
|
+
const flowId = fid();
|
|
250
|
+
const a = rid();
|
|
251
|
+
const b = rid();
|
|
252
|
+
await startRun(s, a, { flowId, concurrencyKey: "k" });
|
|
253
|
+
await startRun(s, b, { flowId, concurrencyKey: "k2" });
|
|
254
|
+
const refused = await startRun(s, b, { flowId, concurrencyKey: "k" });
|
|
255
|
+
eq(refused.started, false, "second start for a known runId is refused");
|
|
256
|
+
eq(refused.canceled.length, 0, "a refused start cancels nothing");
|
|
257
|
+
eq(
|
|
258
|
+
(await s.loadRunState(a)).phase.tag,
|
|
259
|
+
"running",
|
|
260
|
+
"the prior run on that key survives a refused start"
|
|
261
|
+
);
|
|
262
|
+
}
|
|
263
|
+
},
|
|
264
|
+
{
|
|
265
|
+
// The third face of nagi#29, and the one the issue's hypothesis list
|
|
266
|
+
// misses: tryStartRun is atomic, but nothing keeps the superseder ALIVE.
|
|
267
|
+
// Retention that prunes "completed" while keeping "canceled" for audit
|
|
268
|
+
// deletes the superseder and strands the victim's reference.
|
|
269
|
+
name: "pruneFacts: pruning a superseder leaves no dangling canceledByRunId",
|
|
270
|
+
async run(h) {
|
|
271
|
+
const s = await h.makeStore({ leaseMs: LEASE_MS });
|
|
272
|
+
const flowId = fid();
|
|
273
|
+
const a = rid();
|
|
274
|
+
const b = rid();
|
|
275
|
+
await startRun(s, a, { flowId, concurrencyKey: "k" });
|
|
276
|
+
await startRun(s, b, { flowId, concurrencyKey: "k" });
|
|
277
|
+
eq((await s.describe(a))?.run.canceledByRunId, b, "b superseded a");
|
|
278
|
+
await endRun(s, b, "completed");
|
|
279
|
+
await s.pruneFacts({
|
|
280
|
+
olderThan: new Date(Date.now() + 6e4),
|
|
281
|
+
statuses: ["completed"],
|
|
282
|
+
batchSize: 100,
|
|
283
|
+
keepSummary: false
|
|
284
|
+
});
|
|
285
|
+
const victim = await s.describe(a);
|
|
286
|
+
ok(victim !== null, "a canceled run survives a completed-only policy");
|
|
287
|
+
const ref = victim.run.canceledByRunId;
|
|
288
|
+
if (ref !== void 0)
|
|
289
|
+
ok(
|
|
290
|
+
await s.describe(ref) !== null,
|
|
291
|
+
"canceledByRunId must not outlive the run it names \u2014 nagi#29"
|
|
292
|
+
);
|
|
293
|
+
}
|
|
294
|
+
},
|
|
295
|
+
{
|
|
296
|
+
name: "endRun: a run ends once \u2014 of racing ends exactly one is admitted, and a later one (or one on a never-started run) is refused and writes nothing",
|
|
297
|
+
async run(h) {
|
|
298
|
+
const s = await h.makeStore({ leaseMs: LEASE_MS });
|
|
299
|
+
const runId = rid();
|
|
300
|
+
await startRun(s, runId);
|
|
301
|
+
const won = await Promise.all([
|
|
302
|
+
s.endRun(
|
|
303
|
+
runId,
|
|
304
|
+
Facts.flowFailed(runId, { name: "E", message: "x" }, /* @__PURE__ */ new Date())
|
|
305
|
+
),
|
|
306
|
+
s.endRun(
|
|
307
|
+
runId,
|
|
308
|
+
Facts.flowCanceled(
|
|
309
|
+
runId,
|
|
310
|
+
{ cause: "explicit", reason: "race" },
|
|
311
|
+
/* @__PURE__ */ new Date()
|
|
312
|
+
)
|
|
313
|
+
)
|
|
314
|
+
]);
|
|
315
|
+
eq(won.filter(Boolean).length, 1, "exactly one racing end is admitted");
|
|
316
|
+
const winner = won[0] === true ? "failed" : "canceled";
|
|
317
|
+
ok(
|
|
318
|
+
!await s.endRun(runId, Facts.flowCompleted(runId, null, /* @__PURE__ */ new Date())),
|
|
319
|
+
"an end on an already-ended run must resolve false"
|
|
320
|
+
);
|
|
321
|
+
const state = await s.loadRunState(runId);
|
|
322
|
+
eq(
|
|
323
|
+
state.facts.filter(isRunEnd).length,
|
|
324
|
+
1,
|
|
325
|
+
"a refused end is not persisted"
|
|
326
|
+
);
|
|
327
|
+
eq(state.phase.tag, winner, "the fold keeps the winner's end");
|
|
328
|
+
eq((await s.describe(runId))?.run.status, winner, "so does the row");
|
|
329
|
+
const ghost = rid();
|
|
330
|
+
ok(
|
|
331
|
+
!await s.endRun(ghost, Facts.flowCompleted(ghost, null, /* @__PURE__ */ new Date())),
|
|
332
|
+
"a run that never started cannot end"
|
|
333
|
+
);
|
|
334
|
+
eq((await s.loadRunState(ghost)).facts, [], "and nothing is written");
|
|
335
|
+
}
|
|
336
|
+
},
|
|
337
|
+
{
|
|
338
|
+
// NagiCanceledError is public: a handler can name any run as its canceler
|
|
339
|
+
// and classifyFailure turns that claim into a concurrency-cause fact. The
|
|
340
|
+
// fact is the record; the VIEW must not present an unresolvable claim as a
|
|
341
|
+
// reference, or every consumer's audit finds nagi#29's orphan.
|
|
342
|
+
name: "endRun(flow.canceled): a canceler that never existed is not surfaced as a reference",
|
|
343
|
+
async run(h) {
|
|
344
|
+
const s = await h.makeStore({ leaseMs: LEASE_MS });
|
|
345
|
+
const victim = rid();
|
|
346
|
+
await startRun(s, victim);
|
|
347
|
+
await s.endRun(
|
|
348
|
+
victim,
|
|
349
|
+
Facts.flowCanceledByConcurrency({
|
|
350
|
+
runId: victim,
|
|
351
|
+
canceledByRunId: rid(),
|
|
352
|
+
concurrencyKey: "k",
|
|
353
|
+
at: /* @__PURE__ */ new Date()
|
|
354
|
+
})
|
|
355
|
+
);
|
|
356
|
+
const d = await s.describe(victim);
|
|
357
|
+
ok(d !== null, "the canceled run is still describable");
|
|
358
|
+
eq(d.run.status, "canceled", "the cancellation itself stands");
|
|
359
|
+
eq(
|
|
360
|
+
d.run.canceledByRunId,
|
|
361
|
+
void 0,
|
|
362
|
+
"an unresolvable canceler is omitted, not surfaced \u2014 nagi#29"
|
|
363
|
+
);
|
|
364
|
+
const facts = (await s.loadRunState(victim)).facts;
|
|
365
|
+
eq(
|
|
366
|
+
facts.some((f) => f.kind === "flow.canceled"),
|
|
367
|
+
true,
|
|
368
|
+
"the fact log keeps the claim verbatim"
|
|
369
|
+
);
|
|
370
|
+
}
|
|
371
|
+
},
|
|
169
372
|
{
|
|
170
373
|
name: "tryStartRun: a different key or flowId cancels nothing",
|
|
171
374
|
async run(h) {
|
|
@@ -245,7 +448,7 @@ var storeContract = [
|
|
|
245
448
|
}
|
|
246
449
|
},
|
|
247
450
|
{
|
|
248
|
-
name: "
|
|
451
|
+
name: "appendFact(step.completed): releases the lease \u2014 describe() drops lease.expiresAt and claimStep re-acquires",
|
|
249
452
|
async run(h) {
|
|
250
453
|
const s = await h.makeStore({ leaseMs: LEASE_MS });
|
|
251
454
|
const runId = rid();
|
|
@@ -254,29 +457,27 @@ var storeContract = [
|
|
|
254
457
|
ok(await s.claimStep(runId, "s", A1) !== null, "claim");
|
|
255
458
|
const live = await stepView(s, runId, "s");
|
|
256
459
|
ok(live?.lease?.expiresAt instanceof Date, "lease visible while held");
|
|
257
|
-
await s.
|
|
460
|
+
await s.appendFact(
|
|
258
461
|
runId,
|
|
259
|
-
"s",
|
|
260
462
|
Facts.stepCompleted(runId, "s", A1, { ok: 1 }, /* @__PURE__ */ new Date())
|
|
261
463
|
);
|
|
262
464
|
const done = await stepView(s, runId, "s");
|
|
263
465
|
eq(done?.status, "completed", "status");
|
|
264
466
|
eq(done?.output, { ok: 1 }, "output");
|
|
265
467
|
eq(done?.lease, void 0, "lease gone from describe()");
|
|
266
|
-
await claimableAgain(s, runId, "s", "
|
|
468
|
+
await claimableAgain(s, runId, "s", "appendFact(step.completed)");
|
|
267
469
|
}
|
|
268
470
|
},
|
|
269
471
|
{
|
|
270
|
-
name: "
|
|
472
|
+
name: "appendFact(step.failed): releases the lease",
|
|
271
473
|
async run(h) {
|
|
272
474
|
const s = await h.makeStore({ leaseMs: LEASE_MS });
|
|
273
475
|
const runId = rid();
|
|
274
476
|
await startRun(s, runId);
|
|
275
477
|
await startStep(s, runId, "s");
|
|
276
478
|
ok(await s.claimStep(runId, "s", A1) !== null, "claim");
|
|
277
|
-
await s.
|
|
479
|
+
await s.appendFact(
|
|
278
480
|
runId,
|
|
279
|
-
"s",
|
|
280
481
|
Facts.stepFailed(
|
|
281
482
|
runId,
|
|
282
483
|
"s",
|
|
@@ -287,7 +488,7 @@ var storeContract = [
|
|
|
287
488
|
);
|
|
288
489
|
eq(await stepStatus(s, runId, "s"), "failed", "status");
|
|
289
490
|
eq((await stepView(s, runId, "s"))?.lease, void 0, "lease");
|
|
290
|
-
await claimableAgain(s, runId, "s", "
|
|
491
|
+
await claimableAgain(s, runId, "s", "appendFact(step.failed)");
|
|
291
492
|
}
|
|
292
493
|
},
|
|
293
494
|
{
|
|
@@ -454,9 +655,8 @@ var storeContract = [
|
|
|
454
655
|
async run(h) {
|
|
455
656
|
const s = await h.makeStore({ leaseMs: LEASE_MS });
|
|
456
657
|
for (const status of ["completed", "failed"]) {
|
|
457
|
-
const flowId = fid();
|
|
458
658
|
const runId2 = rid();
|
|
459
|
-
await startRun(s, runId2
|
|
659
|
+
await startRun(s, runId2);
|
|
460
660
|
await startStep(s, runId2, "s");
|
|
461
661
|
await endRun(s, runId2, status);
|
|
462
662
|
await s.appendFact(
|
|
@@ -468,12 +668,9 @@ var storeContract = [
|
|
|
468
668
|
"running",
|
|
469
669
|
`fold after flow.${status}`
|
|
470
670
|
);
|
|
471
|
-
const { runs } = await s.queryRuns({
|
|
472
|
-
where: { flowId, status: ["running"] }
|
|
473
|
-
});
|
|
474
671
|
eq(
|
|
475
|
-
|
|
476
|
-
|
|
672
|
+
(await s.describe(runId2))?.run.status,
|
|
673
|
+
"running",
|
|
477
674
|
`read model after flow.${status}`
|
|
478
675
|
);
|
|
479
676
|
}
|
|
@@ -749,8 +946,8 @@ var storeContract = [
|
|
|
749
946
|
eq(msg?.stepId, "s", "re-enqueued stepId");
|
|
750
947
|
eq(msg?.attempt, A2, "re-enqueued attempt");
|
|
751
948
|
eq(msg?.flowId, flowId, "re-enqueued flowId");
|
|
752
|
-
await claimableAgain(s, runId, "s", "sweepLeases");
|
|
753
949
|
eq(await s.sweepLeases({ now: /* @__PURE__ */ new Date(), queue }), [], "restart-safe");
|
|
950
|
+
await claimableAgain(s, runId, "s", "sweepLeases");
|
|
754
951
|
}
|
|
755
952
|
},
|
|
756
953
|
{
|
|
@@ -767,9 +964,8 @@ var storeContract = [
|
|
|
767
964
|
await startRun(s, settled);
|
|
768
965
|
await startStep(s, settled, "s");
|
|
769
966
|
ok(await s.claimStep(settled, "s", A1) !== null, "claim settled");
|
|
770
|
-
await s.
|
|
967
|
+
await s.appendFact(
|
|
771
968
|
settled,
|
|
772
|
-
"s",
|
|
773
969
|
Facts.stepCompleted(settled, "s", A1, null, /* @__PURE__ */ new Date())
|
|
774
970
|
);
|
|
775
971
|
await sleep(PAST_LEASE_MS);
|
|
@@ -854,19 +1050,26 @@ var storeContract = [
|
|
|
854
1050
|
}
|
|
855
1051
|
},
|
|
856
1052
|
{
|
|
857
|
-
name: "recordOnce / getOnce: first write wins;
|
|
1053
|
+
name: "recordOnce / getOnce: first write wins; miss when absent; a recorded null is a hit",
|
|
858
1054
|
async run(h) {
|
|
859
1055
|
const s = await h.makeStore({ leaseMs: LEASE_MS });
|
|
860
1056
|
const runId = rid();
|
|
861
|
-
eq(await s.getOnce(runId, "s", "scope"),
|
|
1057
|
+
eq(await s.getOnce(runId, "s", "scope"), { tag: "miss" }, "absent");
|
|
862
1058
|
await s.recordOnce(runId, "s", "scope", { value: 1 });
|
|
863
1059
|
await s.recordOnce(runId, "s", "scope", { value: 2 });
|
|
864
1060
|
eq(
|
|
865
1061
|
await s.getOnce(runId, "s", "scope"),
|
|
866
|
-
{ value: 1 },
|
|
1062
|
+
{ tag: "hit", value: { value: 1 } },
|
|
867
1063
|
"first write wins"
|
|
868
1064
|
);
|
|
869
|
-
eq(await s.getOnce(runId, "s", "other"),
|
|
1065
|
+
eq(await s.getOnce(runId, "s", "other"), { tag: "miss" }, "scoped");
|
|
1066
|
+
await s.recordOnce(runId, "s", "nil", null);
|
|
1067
|
+
await s.recordOnce(runId, "s", "nil", { value: 3 });
|
|
1068
|
+
eq(
|
|
1069
|
+
await s.getOnce(runId, "s", "nil"),
|
|
1070
|
+
{ tag: "hit", value: null },
|
|
1071
|
+
"recorded null"
|
|
1072
|
+
);
|
|
870
1073
|
}
|
|
871
1074
|
},
|
|
872
1075
|
{
|
|
@@ -921,9 +1124,8 @@ var storeContract = [
|
|
|
921
1124
|
const runId = rid();
|
|
922
1125
|
await startRun(s, runId, { flowId: "f", input: { a: 1 } });
|
|
923
1126
|
await startStep(s, runId, "s");
|
|
924
|
-
await s.
|
|
1127
|
+
await s.appendFact(
|
|
925
1128
|
runId,
|
|
926
|
-
"s",
|
|
927
1129
|
Facts.stepCompleted(runId, "s", A1, 7, /* @__PURE__ */ new Date())
|
|
928
1130
|
);
|
|
929
1131
|
await endRun(s, runId, "completed");
|
|
@@ -938,178 +1140,6 @@ var storeContract = [
|
|
|
938
1140
|
eq(state.phase.tag, "completed", "phase");
|
|
939
1141
|
}
|
|
940
1142
|
},
|
|
941
|
-
{
|
|
942
|
-
name: "queryRuns: orders by (startedAt DESC, runId DESC)",
|
|
943
|
-
async run(h) {
|
|
944
|
-
const s = await h.makeStore({ leaseMs: LEASE_MS });
|
|
945
|
-
const flowId = fid();
|
|
946
|
-
const t0 = /* @__PURE__ */ new Date(17e11);
|
|
947
|
-
const t1 = /* @__PURE__ */ new Date(1700000001e3);
|
|
948
|
-
const base = crypto.randomUUID();
|
|
949
|
-
const a = `run-${base}-a`;
|
|
950
|
-
const b = `run-${base}-b`;
|
|
951
|
-
const c = `run-${base}-c`;
|
|
952
|
-
const later = `run-${base}-0`;
|
|
953
|
-
for (const runId of [b, a, c])
|
|
954
|
-
await startRun(s, runId, { flowId, at: t0 });
|
|
955
|
-
await startRun(s, later, { flowId, at: t1 });
|
|
956
|
-
const r = await s.queryRuns({ where: { flowId } });
|
|
957
|
-
eq(
|
|
958
|
-
r.runs.map((x) => x.runId),
|
|
959
|
-
[later, c, b, a],
|
|
960
|
-
"order"
|
|
961
|
-
);
|
|
962
|
-
eq(r.cursor, null, "single page");
|
|
963
|
-
}
|
|
964
|
-
},
|
|
965
|
-
{
|
|
966
|
-
name: "queryRuns: input containment follows jsonb @> \u2014 subset, nested, arrays, empty object, type mismatch",
|
|
967
|
-
async run(h) {
|
|
968
|
-
const s = await h.makeStore({ leaseMs: LEASE_MS });
|
|
969
|
-
const flowId = fid();
|
|
970
|
-
await startRun(s, rid(), {
|
|
971
|
-
flowId,
|
|
972
|
-
input: { videoId: "abc", userId: 7 }
|
|
973
|
-
});
|
|
974
|
-
await startRun(s, rid(), {
|
|
975
|
-
flowId,
|
|
976
|
-
input: { customer: { id: 1, plan: { seats: 5 } } }
|
|
977
|
-
});
|
|
978
|
-
await startRun(s, rid(), { flowId, input: { tags: ["a", "b", "c"] } });
|
|
979
|
-
await startRun(s, rid(), { flowId, input: { x: "string" } });
|
|
980
|
-
const count = async (input) => (await s.queryRuns({ where: { flowId, input } })).runs.length;
|
|
981
|
-
eq(await count({ videoId: "abc" }), 1, "subset of keys");
|
|
982
|
-
eq(await count({ videoId: "abc", userId: 7 }), 1, "all keys");
|
|
983
|
-
eq(await count({ videoId: "abc", missing: 1 }), 0, "superset of keys");
|
|
984
|
-
eq(await count({ videoId: "MISS" }), 0, "value mismatch");
|
|
985
|
-
eq(await count({ customer: { plan: { seats: 5 } } }), 1, "nested");
|
|
986
|
-
eq(await count({ customer: {} }), 1, "empty object matches any object");
|
|
987
|
-
eq(await count({ tags: ["a", "b"] }), 1, "array subset");
|
|
988
|
-
eq(await count({ tags: ["b", "a"] }), 1, "array order-insensitive");
|
|
989
|
-
eq(await count({ tags: ["a", "z"] }), 0, "array with a missing element");
|
|
990
|
-
eq(await count({ x: 42 }), 0, "type mismatch");
|
|
991
|
-
eq(await count({}), 4, "empty filter matches all");
|
|
992
|
-
}
|
|
993
|
-
},
|
|
994
|
-
{
|
|
995
|
-
name: "queryRuns: filters by flowId and status",
|
|
996
|
-
async run(h) {
|
|
997
|
-
const s = await h.makeStore({ leaseMs: LEASE_MS });
|
|
998
|
-
const flowId = fid();
|
|
999
|
-
const running = rid();
|
|
1000
|
-
const completed = rid();
|
|
1001
|
-
const failed = rid();
|
|
1002
|
-
await startRun(s, running, { flowId });
|
|
1003
|
-
await startRun(s, completed, { flowId });
|
|
1004
|
-
await endRun(s, completed, "completed");
|
|
1005
|
-
await startRun(s, failed, { flowId });
|
|
1006
|
-
await endRun(s, failed, "failed");
|
|
1007
|
-
await startRun(s, rid(), { flowId: fid() });
|
|
1008
|
-
eq((await s.queryRuns({ where: { flowId } })).runs.length, 3, "flowId");
|
|
1009
|
-
const terminal = await s.queryRuns({
|
|
1010
|
-
where: { flowId, status: ["completed", "failed"] }
|
|
1011
|
-
});
|
|
1012
|
-
eq(
|
|
1013
|
-
terminal.runs.map((r) => r.runId).sort(),
|
|
1014
|
-
[completed, failed].sort(),
|
|
1015
|
-
"status array"
|
|
1016
|
-
);
|
|
1017
|
-
const one = await s.queryRuns({
|
|
1018
|
-
where: { flowId, status: ["completed"] }
|
|
1019
|
-
});
|
|
1020
|
-
eq(
|
|
1021
|
-
one.runs.map((r) => r.status),
|
|
1022
|
-
["completed"],
|
|
1023
|
-
"single status"
|
|
1024
|
-
);
|
|
1025
|
-
ok(one.runs[0]?.completedAt instanceof Date, "completedAt populated");
|
|
1026
|
-
}
|
|
1027
|
-
},
|
|
1028
|
-
{
|
|
1029
|
-
name: "queryRuns: latest:true returns the single newest matching run and no cursor",
|
|
1030
|
-
async run(h) {
|
|
1031
|
-
const s = await h.makeStore({ leaseMs: LEASE_MS });
|
|
1032
|
-
const flowId = fid();
|
|
1033
|
-
await startRun(s, rid(), { flowId, at: /* @__PURE__ */ new Date(17e11) });
|
|
1034
|
-
const newest = rid();
|
|
1035
|
-
await startRun(s, newest, { flowId, at: /* @__PURE__ */ new Date(1700000005e3) });
|
|
1036
|
-
await startRun(s, rid(), { flowId, at: /* @__PURE__ */ new Date(1700000002e3) });
|
|
1037
|
-
const r = await s.queryRuns({ where: { flowId }, latest: true });
|
|
1038
|
-
eq(
|
|
1039
|
-
r.runs.map((x) => x.runId),
|
|
1040
|
-
[newest],
|
|
1041
|
-
"newest"
|
|
1042
|
-
);
|
|
1043
|
-
eq(r.cursor, null, "no cursor");
|
|
1044
|
-
eq(
|
|
1045
|
-
await s.queryRuns({ where: { flowId: fid() }, latest: true }),
|
|
1046
|
-
{ runs: [], cursor: null },
|
|
1047
|
-
"no match"
|
|
1048
|
-
);
|
|
1049
|
-
}
|
|
1050
|
-
},
|
|
1051
|
-
{
|
|
1052
|
-
name: "queryRuns: cursor pagination visits every row exactly once and ends with a null cursor",
|
|
1053
|
-
async run(h) {
|
|
1054
|
-
const s = await h.makeStore({ leaseMs: LEASE_MS });
|
|
1055
|
-
const flowId = fid();
|
|
1056
|
-
const seeded = [];
|
|
1057
|
-
for (let i = 0; i < 5; i++) {
|
|
1058
|
-
const runId = rid();
|
|
1059
|
-
seeded.push(runId);
|
|
1060
|
-
await startRun(s, runId, {
|
|
1061
|
-
flowId,
|
|
1062
|
-
at: new Date(17e11 + i * 1e3)
|
|
1063
|
-
});
|
|
1064
|
-
}
|
|
1065
|
-
const seen = [];
|
|
1066
|
-
let cursor = null;
|
|
1067
|
-
let pages = 0;
|
|
1068
|
-
do {
|
|
1069
|
-
const page = await s.queryRuns({
|
|
1070
|
-
where: { flowId },
|
|
1071
|
-
limit: 2,
|
|
1072
|
-
...cursor !== null ? { cursor } : {}
|
|
1073
|
-
});
|
|
1074
|
-
ok(page.runs.length <= 2, "page size");
|
|
1075
|
-
seen.push(...page.runs.map((r) => r.runId));
|
|
1076
|
-
cursor = page.cursor;
|
|
1077
|
-
pages++;
|
|
1078
|
-
} while (cursor !== null && pages < 10);
|
|
1079
|
-
eq(pages, 3, "three pages");
|
|
1080
|
-
eq([...seen].sort(), [...seeded].sort(), "every row once");
|
|
1081
|
-
}
|
|
1082
|
-
},
|
|
1083
|
-
{
|
|
1084
|
-
name: "queryRuns: rejects a malformed cursor",
|
|
1085
|
-
async run(h) {
|
|
1086
|
-
const s = await h.makeStore({ leaseMs: LEASE_MS });
|
|
1087
|
-
await rejects(s.queryRuns({ cursor: "not-a-cursor" }), "throws");
|
|
1088
|
-
}
|
|
1089
|
-
},
|
|
1090
|
-
{
|
|
1091
|
-
name: "queryRuns: a non-positive or non-integer limit falls back to the default",
|
|
1092
|
-
async run(h) {
|
|
1093
|
-
const s = await h.makeStore({ leaseMs: LEASE_MS });
|
|
1094
|
-
const flowId = fid();
|
|
1095
|
-
for (let i = 0; i < 3; i++) await startRun(s, rid(), { flowId });
|
|
1096
|
-
eq(
|
|
1097
|
-
(await s.queryRuns({ where: { flowId }, limit: 0 })).runs.length,
|
|
1098
|
-
3,
|
|
1099
|
-
"0"
|
|
1100
|
-
);
|
|
1101
|
-
eq(
|
|
1102
|
-
(await s.queryRuns({ where: { flowId }, limit: 1.5 })).runs.length,
|
|
1103
|
-
3,
|
|
1104
|
-
"1.5"
|
|
1105
|
-
);
|
|
1106
|
-
eq(
|
|
1107
|
-
(await s.queryRuns({ where: { flowId }, limit: 2 })).runs.length,
|
|
1108
|
-
2,
|
|
1109
|
-
"2"
|
|
1110
|
-
);
|
|
1111
|
-
}
|
|
1112
|
-
},
|
|
1113
1143
|
{
|
|
1114
1144
|
name: "describe: returns null for an unknown runId",
|
|
1115
1145
|
async run(h) {
|
|
@@ -1134,9 +1164,8 @@ var storeContract = [
|
|
|
1134
1164
|
parent: { runId: parent, stepId: "sub" }
|
|
1135
1165
|
});
|
|
1136
1166
|
await startStep(s, parent, "sub", { kind: "subflow" });
|
|
1137
|
-
await s.
|
|
1167
|
+
await s.appendFact(
|
|
1138
1168
|
parent,
|
|
1139
|
-
"sub",
|
|
1140
1169
|
Facts.stepCompleted(
|
|
1141
1170
|
parent,
|
|
1142
1171
|
"sub",
|
|
@@ -1145,7 +1174,7 @@ var storeContract = [
|
|
|
1145
1174
|
/* @__PURE__ */ new Date()
|
|
1146
1175
|
)
|
|
1147
1176
|
);
|
|
1148
|
-
await s.
|
|
1177
|
+
await s.endRun(
|
|
1149
1178
|
parent,
|
|
1150
1179
|
Facts.flowCompleted(parent, { done: true }, /* @__PURE__ */ new Date())
|
|
1151
1180
|
);
|
|
@@ -1175,6 +1204,203 @@ var storeContract = [
|
|
|
1175
1204
|
eq(await s.listChildren(child), [], "listChildren leaf");
|
|
1176
1205
|
}
|
|
1177
1206
|
},
|
|
1207
|
+
{
|
|
1208
|
+
name: "describe: a retried step is ONE view of its latest attempt, timestamped by its facts' `at`",
|
|
1209
|
+
async run(h) {
|
|
1210
|
+
const s = await h.makeStore({ leaseMs: LEASE_MS });
|
|
1211
|
+
const runId = rid();
|
|
1212
|
+
const t = (n) => new Date(17e11 + n * 1e3);
|
|
1213
|
+
const error = { name: "E", message: "boom" };
|
|
1214
|
+
await startRun(s, runId, { at: t(0) });
|
|
1215
|
+
await s.appendFact(
|
|
1216
|
+
runId,
|
|
1217
|
+
Facts.stepStarted(runId, "s", A1, "task", t(1))
|
|
1218
|
+
);
|
|
1219
|
+
await s.appendFact(
|
|
1220
|
+
runId,
|
|
1221
|
+
Facts.stepRetried(runId, "s", A1, t(3), error, t(2))
|
|
1222
|
+
);
|
|
1223
|
+
eq(
|
|
1224
|
+
(await s.describe(runId))?.steps,
|
|
1225
|
+
[
|
|
1226
|
+
{
|
|
1227
|
+
stepId: "s",
|
|
1228
|
+
attempt: A1,
|
|
1229
|
+
status: "running",
|
|
1230
|
+
startedAt: t(1),
|
|
1231
|
+
error
|
|
1232
|
+
}
|
|
1233
|
+
],
|
|
1234
|
+
"backing off: the failed attempt, still running, with its error"
|
|
1235
|
+
);
|
|
1236
|
+
await s.appendFact(
|
|
1237
|
+
runId,
|
|
1238
|
+
Facts.stepStarted(runId, "s", A2, "task", t(3))
|
|
1239
|
+
);
|
|
1240
|
+
await s.appendFact(
|
|
1241
|
+
runId,
|
|
1242
|
+
Facts.stepStarted(runId, "s", A1, "task", t(4))
|
|
1243
|
+
);
|
|
1244
|
+
eq(
|
|
1245
|
+
(await s.describe(runId))?.steps,
|
|
1246
|
+
[{ stepId: "s", attempt: A2, status: "running", startedAt: t(3) }],
|
|
1247
|
+
"attempt 2 supersedes attempt 1; a stale attempt-1 start changes nothing"
|
|
1248
|
+
);
|
|
1249
|
+
eq(
|
|
1250
|
+
(await s.describe(runId))?.run.status,
|
|
1251
|
+
"running",
|
|
1252
|
+
"a retry leaves the run running"
|
|
1253
|
+
);
|
|
1254
|
+
await s.appendFact(
|
|
1255
|
+
runId,
|
|
1256
|
+
Facts.stepCompleted(runId, "s", A2, { v: 2 }, t(5))
|
|
1257
|
+
);
|
|
1258
|
+
await endRun(s, runId, "completed", t(6));
|
|
1259
|
+
const d = await s.describe(runId);
|
|
1260
|
+
eq(
|
|
1261
|
+
d?.steps,
|
|
1262
|
+
[
|
|
1263
|
+
{
|
|
1264
|
+
stepId: "s",
|
|
1265
|
+
attempt: A2,
|
|
1266
|
+
status: "completed",
|
|
1267
|
+
startedAt: t(3),
|
|
1268
|
+
completedAt: t(5),
|
|
1269
|
+
output: { v: 2 }
|
|
1270
|
+
}
|
|
1271
|
+
],
|
|
1272
|
+
"one view per step"
|
|
1273
|
+
);
|
|
1274
|
+
eq(d?.run.startedAt, t(0), "run startedAt");
|
|
1275
|
+
eq(d?.run.completedAt, t(6), "run completedAt");
|
|
1276
|
+
}
|
|
1277
|
+
},
|
|
1278
|
+
{
|
|
1279
|
+
name: "step attempts only move forward, in loadRunState and describe alike: a newer start supersedes a live attempt; a duplicate start, and a retry or abort for an attempt not in flight, change nothing",
|
|
1280
|
+
async run(h) {
|
|
1281
|
+
const s = await h.makeStore({ leaseMs: LEASE_MS });
|
|
1282
|
+
const runId = rid();
|
|
1283
|
+
const t = (n) => new Date(17e11 + n * 1e3);
|
|
1284
|
+
const error = { name: "E", message: "boom" };
|
|
1285
|
+
const both = async (tag, attempt, at) => {
|
|
1286
|
+
eq(
|
|
1287
|
+
stepStateOf(await s.loadRunState(runId), "s"),
|
|
1288
|
+
{ tag, attempt },
|
|
1289
|
+
`loadRunState after ${at.toISOString()}`
|
|
1290
|
+
);
|
|
1291
|
+
eq(
|
|
1292
|
+
await stepView(s, runId, "s"),
|
|
1293
|
+
{ stepId: "s", attempt, status: "running", startedAt: at },
|
|
1294
|
+
`describe after ${at.toISOString()}`
|
|
1295
|
+
);
|
|
1296
|
+
};
|
|
1297
|
+
await startRun(s, runId, { at: t(0) });
|
|
1298
|
+
await s.appendFact(
|
|
1299
|
+
runId,
|
|
1300
|
+
Facts.stepStarted(runId, "s", A1, "task", t(1))
|
|
1301
|
+
);
|
|
1302
|
+
await s.appendFact(
|
|
1303
|
+
runId,
|
|
1304
|
+
Facts.stepStarted(runId, "s", A1, "task", t(2))
|
|
1305
|
+
);
|
|
1306
|
+
await both("running", A1, t(1));
|
|
1307
|
+
await s.appendFact(
|
|
1308
|
+
runId,
|
|
1309
|
+
Facts.stepStarted(runId, "s", A2, "task", t(3))
|
|
1310
|
+
);
|
|
1311
|
+
await both("running", A2, t(3));
|
|
1312
|
+
for (const attempt of [A1, 3]) {
|
|
1313
|
+
await s.appendFact(
|
|
1314
|
+
runId,
|
|
1315
|
+
Facts.stepRetried(runId, "s", attempt, t(9), error, t(4))
|
|
1316
|
+
);
|
|
1317
|
+
}
|
|
1318
|
+
await s.appendFact(
|
|
1319
|
+
runId,
|
|
1320
|
+
Facts.stepAbortRequested({
|
|
1321
|
+
runId,
|
|
1322
|
+
stepId: "s",
|
|
1323
|
+
attempt: A1,
|
|
1324
|
+
at: t(5)
|
|
1325
|
+
})
|
|
1326
|
+
);
|
|
1327
|
+
await both("running", A2, t(3));
|
|
1328
|
+
await s.appendFact(
|
|
1329
|
+
runId,
|
|
1330
|
+
Facts.stepAbortRequested({
|
|
1331
|
+
runId,
|
|
1332
|
+
stepId: "s",
|
|
1333
|
+
attempt: A2,
|
|
1334
|
+
at: t(6)
|
|
1335
|
+
})
|
|
1336
|
+
);
|
|
1337
|
+
eq(
|
|
1338
|
+
stepStateOf(await s.loadRunState(runId), "s"),
|
|
1339
|
+
{ tag: "aborting", attempt: A2 },
|
|
1340
|
+
"an abort for the live attempt lands"
|
|
1341
|
+
);
|
|
1342
|
+
}
|
|
1343
|
+
},
|
|
1344
|
+
{
|
|
1345
|
+
name: "describe: a skipped step settles its view; a settled step changes only by reset",
|
|
1346
|
+
async run(h) {
|
|
1347
|
+
const s = await h.makeStore({ leaseMs: LEASE_MS });
|
|
1348
|
+
const runId = rid();
|
|
1349
|
+
const t = (n) => new Date(17e11 + n * 1e3);
|
|
1350
|
+
const skip = (stepId, at) => s.appendFact(
|
|
1351
|
+
runId,
|
|
1352
|
+
Facts.stepSkipped({ runId, stepId, reason: "when-false", at })
|
|
1353
|
+
);
|
|
1354
|
+
await startRun(s, runId, { at: t(0) });
|
|
1355
|
+
await skip("never", t(1));
|
|
1356
|
+
await s.appendFact(
|
|
1357
|
+
runId,
|
|
1358
|
+
Facts.stepStarted(runId, "inflight", A1, "task", t(2))
|
|
1359
|
+
);
|
|
1360
|
+
await skip("inflight", t(3));
|
|
1361
|
+
await s.appendFact(
|
|
1362
|
+
runId,
|
|
1363
|
+
Facts.stepCompleted(runId, "inflight", A1, { late: true }, t(4))
|
|
1364
|
+
);
|
|
1365
|
+
eq(
|
|
1366
|
+
(await s.describe(runId))?.steps,
|
|
1367
|
+
[
|
|
1368
|
+
{
|
|
1369
|
+
stepId: "inflight",
|
|
1370
|
+
attempt: A1,
|
|
1371
|
+
status: "skipped",
|
|
1372
|
+
startedAt: t(2),
|
|
1373
|
+
completedAt: t(3)
|
|
1374
|
+
},
|
|
1375
|
+
{
|
|
1376
|
+
stepId: "never",
|
|
1377
|
+
attempt: 0,
|
|
1378
|
+
status: "skipped",
|
|
1379
|
+
completedAt: t(1)
|
|
1380
|
+
}
|
|
1381
|
+
],
|
|
1382
|
+
"skipped views; the late completion is ignored"
|
|
1383
|
+
);
|
|
1384
|
+
await s.appendFact(
|
|
1385
|
+
runId,
|
|
1386
|
+
Facts.stepReset({ runId, stepId: "inflight", at: t(5) })
|
|
1387
|
+
);
|
|
1388
|
+
eq(
|
|
1389
|
+
(await s.describe(runId))?.steps.map((st) => st.stepId),
|
|
1390
|
+
["never"],
|
|
1391
|
+
"a reset step has no view until it restarts"
|
|
1392
|
+
);
|
|
1393
|
+
await s.appendFact(
|
|
1394
|
+
runId,
|
|
1395
|
+
Facts.stepStarted(runId, "inflight", A1, "task", t(6))
|
|
1396
|
+
);
|
|
1397
|
+
eq(
|
|
1398
|
+
await stepView(s, runId, "inflight"),
|
|
1399
|
+
{ stepId: "inflight", attempt: A1, status: "running", startedAt: t(6) },
|
|
1400
|
+
"restarted"
|
|
1401
|
+
);
|
|
1402
|
+
}
|
|
1403
|
+
},
|
|
1178
1404
|
{
|
|
1179
1405
|
name: "pruneFacts: never prunes non-terminal runs; respects olderThan and statuses",
|
|
1180
1406
|
async run(h) {
|
|
@@ -1244,7 +1470,7 @@ var storeContract = [
|
|
|
1244
1470
|
}
|
|
1245
1471
|
},
|
|
1246
1472
|
{
|
|
1247
|
-
name: "pruneFacts keepSummary:true: the run stays
|
|
1473
|
+
name: "pruneFacts keepSummary:true: the run stays describable with no steps, and its runId stays reserved",
|
|
1248
1474
|
async run(h) {
|
|
1249
1475
|
const s = await h.makeStore({ leaseMs: LEASE_MS });
|
|
1250
1476
|
const flowId = fid();
|
|
@@ -1252,11 +1478,7 @@ var storeContract = [
|
|
|
1252
1478
|
const old = /* @__PURE__ */ new Date(17e11);
|
|
1253
1479
|
await startRun(s, runId, { flowId, input: { keep: 1 }, at: old });
|
|
1254
1480
|
await startStep(s, runId, "s");
|
|
1255
|
-
await s.
|
|
1256
|
-
runId,
|
|
1257
|
-
"s",
|
|
1258
|
-
Facts.stepCompleted(runId, "s", A1, null, old)
|
|
1259
|
-
);
|
|
1481
|
+
await s.appendFact(runId, Facts.stepCompleted(runId, "s", A1, null, old));
|
|
1260
1482
|
await endRun(s, runId, "completed", old);
|
|
1261
1483
|
await s.pruneFacts({
|
|
1262
1484
|
olderThan: /* @__PURE__ */ new Date(17000001e5),
|
|
@@ -1264,12 +1486,9 @@ var storeContract = [
|
|
|
1264
1486
|
batchSize: 100,
|
|
1265
1487
|
keepSummary: true
|
|
1266
1488
|
});
|
|
1267
|
-
const q = await s.queryRuns({ where: { flowId } });
|
|
1268
|
-
eq(q.runs.length, 1, "still listed");
|
|
1269
|
-
eq(q.runs[0]?.runId, runId, "runId");
|
|
1270
|
-
eq(q.runs[0]?.status, "completed", "status");
|
|
1271
|
-
eq(q.runs[0]?.input, { keep: 1 }, "input");
|
|
1272
1489
|
const d = await s.describe(runId);
|
|
1490
|
+
eq(d?.run.runId, runId, "describe runId");
|
|
1491
|
+
eq(d?.run.flowId, flowId, "describe flowId");
|
|
1273
1492
|
eq(d?.run.status, "completed", "describe status");
|
|
1274
1493
|
eq(d?.run.input, { keep: 1 }, "describe input");
|
|
1275
1494
|
eq(d?.steps, [], "no steps");
|
|
@@ -1296,7 +1515,6 @@ var storeContract = [
|
|
|
1296
1515
|
batchSize: 100,
|
|
1297
1516
|
keepSummary: false
|
|
1298
1517
|
});
|
|
1299
|
-
eq((await s.queryRuns({ where: { flowId } })).runs, [], "not listed");
|
|
1300
1518
|
eq(await s.describe(runId), null, "not describable");
|
|
1301
1519
|
eq(
|
|
1302
1520
|
(await startRun(s, runId, { flowId })).started,
|
|
@@ -1323,10 +1541,253 @@ var storeContract = [
|
|
|
1323
1541
|
batchSize: 100,
|
|
1324
1542
|
keepSummary: false
|
|
1325
1543
|
});
|
|
1326
|
-
eq(await s.getOnce(runId, "gate", "scope"),
|
|
1544
|
+
eq(await s.getOnce(runId, "gate", "scope"), { tag: "miss" }, "once gone");
|
|
1327
1545
|
await claimableAgain(s, runId, "gate", "pruneFacts");
|
|
1328
1546
|
eq(await s.sweepSignalTimeouts({ now: /* @__PURE__ */ new Date() }), [], "timer gone");
|
|
1329
1547
|
}
|
|
1548
|
+
},
|
|
1549
|
+
{
|
|
1550
|
+
name: "tryStartRunOnTx: starts the run on the caller's tx; a second call for the same runId is a no-op",
|
|
1551
|
+
async run(h) {
|
|
1552
|
+
const s = await h.makeStore({ leaseMs: LEASE_MS });
|
|
1553
|
+
const runId = rid();
|
|
1554
|
+
const fact = Facts.flowStarted({
|
|
1555
|
+
runId,
|
|
1556
|
+
flowId: fid(),
|
|
1557
|
+
input: { x: 1 },
|
|
1558
|
+
at: /* @__PURE__ */ new Date()
|
|
1559
|
+
});
|
|
1560
|
+
const first = await h.withTx(
|
|
1561
|
+
s,
|
|
1562
|
+
(tx) => s.tryStartRunOnTx(tx, runId, fact)
|
|
1563
|
+
);
|
|
1564
|
+
eq(first, { started: true, canceled: [] }, "first start");
|
|
1565
|
+
const again = await h.withTx(
|
|
1566
|
+
s,
|
|
1567
|
+
(tx) => s.tryStartRunOnTx(tx, runId, { ...fact, input: { x: 2 } })
|
|
1568
|
+
);
|
|
1569
|
+
eq(again, { started: false, canceled: [] }, "second start");
|
|
1570
|
+
const state = await s.loadRunState(runId);
|
|
1571
|
+
eq(state.phase.tag, "running", "running after commit");
|
|
1572
|
+
eq(state.input, { x: 1 }, "input unchanged");
|
|
1573
|
+
eq((await s.describe(runId))?.run.status, "running", "read model");
|
|
1574
|
+
}
|
|
1575
|
+
},
|
|
1576
|
+
{
|
|
1577
|
+
name: "tryStartRunOnTx: with concurrency, cancels the prior active run on the same (flowId, key) and returns its fact",
|
|
1578
|
+
async run(h) {
|
|
1579
|
+
const s = await h.makeStore({ leaseMs: LEASE_MS });
|
|
1580
|
+
const flowId = fid();
|
|
1581
|
+
const a = rid();
|
|
1582
|
+
const b = rid();
|
|
1583
|
+
await startRun(s, a, { flowId, concurrencyKey: "k" });
|
|
1584
|
+
const res = await h.withTx(
|
|
1585
|
+
s,
|
|
1586
|
+
(tx) => s.tryStartRunOnTx(
|
|
1587
|
+
tx,
|
|
1588
|
+
b,
|
|
1589
|
+
Facts.flowStarted({ runId: b, flowId, input: {}, at: /* @__PURE__ */ new Date() }),
|
|
1590
|
+
{ key: "k", mode: "cancel-in-progress" }
|
|
1591
|
+
)
|
|
1592
|
+
);
|
|
1593
|
+
ok(res.started, "b started");
|
|
1594
|
+
eq(
|
|
1595
|
+
res.canceled.map((c) => [c.runId, c.fact.canceledByRunId]),
|
|
1596
|
+
[[a, b]],
|
|
1597
|
+
"a canceled by b"
|
|
1598
|
+
);
|
|
1599
|
+
eq((await s.loadRunState(a)).phase.tag, "canceled", "a folds canceled");
|
|
1600
|
+
eq((await s.describe(a))?.run.canceledByRunId, b, "describe(a)");
|
|
1601
|
+
}
|
|
1602
|
+
},
|
|
1603
|
+
{
|
|
1604
|
+
name: "events: watchRuns sees flow.started from both tryStartRun and tryStartRunOnTx",
|
|
1605
|
+
async run(h) {
|
|
1606
|
+
const s = await h.makeStore({ leaseMs: LEASE_MS });
|
|
1607
|
+
const seen = recordRuns(s);
|
|
1608
|
+
if (seen === null) return;
|
|
1609
|
+
const own = rid();
|
|
1610
|
+
const onTx = rid();
|
|
1611
|
+
await startRun(s, own);
|
|
1612
|
+
await h.withTx(
|
|
1613
|
+
s,
|
|
1614
|
+
(tx) => s.tryStartRunOnTx(
|
|
1615
|
+
tx,
|
|
1616
|
+
onTx,
|
|
1617
|
+
Facts.flowStarted({
|
|
1618
|
+
runId: onTx,
|
|
1619
|
+
flowId: "f",
|
|
1620
|
+
input: {},
|
|
1621
|
+
at: /* @__PURE__ */ new Date()
|
|
1622
|
+
})
|
|
1623
|
+
)
|
|
1624
|
+
);
|
|
1625
|
+
await eventually(
|
|
1626
|
+
() => typesFor(seen, own).includes("flow.started"),
|
|
1627
|
+
"tryStartRun must announce flow.started"
|
|
1628
|
+
);
|
|
1629
|
+
await eventually(
|
|
1630
|
+
() => typesFor(seen, onTx).includes("flow.started"),
|
|
1631
|
+
"tryStartRunOnTx must announce flow.started once its tx commits"
|
|
1632
|
+
);
|
|
1633
|
+
}
|
|
1634
|
+
},
|
|
1635
|
+
{
|
|
1636
|
+
name: "events: watchRun sees step events in order and ends on the terminal event",
|
|
1637
|
+
async run(h) {
|
|
1638
|
+
const s = await h.makeStore({ leaseMs: LEASE_MS });
|
|
1639
|
+
if (s.events === void 0) return;
|
|
1640
|
+
const runId = rid();
|
|
1641
|
+
const seen = [];
|
|
1642
|
+
s.events.watchRun(runId, (e) => seen.push(e));
|
|
1643
|
+
await startRun(s, runId);
|
|
1644
|
+
await startStep(s, runId, "s");
|
|
1645
|
+
await s.appendFact(
|
|
1646
|
+
runId,
|
|
1647
|
+
Facts.stepCompleted(runId, "s", A1, { ok: 1 }, /* @__PURE__ */ new Date())
|
|
1648
|
+
);
|
|
1649
|
+
await s.appendFact(
|
|
1650
|
+
runId,
|
|
1651
|
+
Facts.stepAbortRequested({
|
|
1652
|
+
runId,
|
|
1653
|
+
stepId: "s",
|
|
1654
|
+
attempt: A1,
|
|
1655
|
+
at: /* @__PURE__ */ new Date()
|
|
1656
|
+
})
|
|
1657
|
+
);
|
|
1658
|
+
await endRun(s, runId, "completed");
|
|
1659
|
+
const want = [
|
|
1660
|
+
"flow.started",
|
|
1661
|
+
"step.started",
|
|
1662
|
+
"step.completed",
|
|
1663
|
+
"flow.completed"
|
|
1664
|
+
];
|
|
1665
|
+
await eventually(() => seen.length >= want.length, "all four events");
|
|
1666
|
+
eq(
|
|
1667
|
+
seen.map((e) => e.type),
|
|
1668
|
+
want,
|
|
1669
|
+
"bookkeeping facts are not events"
|
|
1670
|
+
);
|
|
1671
|
+
}
|
|
1672
|
+
},
|
|
1673
|
+
{
|
|
1674
|
+
name: "events: a superseded run announces flow.canceled naming its superseder",
|
|
1675
|
+
async run(h) {
|
|
1676
|
+
const s = await h.makeStore({ leaseMs: LEASE_MS });
|
|
1677
|
+
const seen = recordRuns(s);
|
|
1678
|
+
if (seen === null) return;
|
|
1679
|
+
const flowId = fid();
|
|
1680
|
+
const a = rid();
|
|
1681
|
+
const b = rid();
|
|
1682
|
+
await startRun(s, a, { flowId, concurrencyKey: "k" });
|
|
1683
|
+
await startRun(s, b, { flowId, concurrencyKey: "k" });
|
|
1684
|
+
await eventually(
|
|
1685
|
+
() => seen.some((e) => e.runId === a && e.type === "flow.canceled"),
|
|
1686
|
+
"a's cancellation is announced"
|
|
1687
|
+
);
|
|
1688
|
+
eq(
|
|
1689
|
+
seen.find((e) => e.runId === a && e.type === "flow.canceled"),
|
|
1690
|
+
{
|
|
1691
|
+
type: "flow.canceled",
|
|
1692
|
+
cause: "concurrency",
|
|
1693
|
+
canceledByRunId: b,
|
|
1694
|
+
runId: a
|
|
1695
|
+
},
|
|
1696
|
+
"cancel event"
|
|
1697
|
+
);
|
|
1698
|
+
}
|
|
1699
|
+
},
|
|
1700
|
+
{
|
|
1701
|
+
name: "stream: a subscriber receives published chunks, then closes on step.completed",
|
|
1702
|
+
async run(h) {
|
|
1703
|
+
const s = await h.makeStore({ leaseMs: LEASE_MS });
|
|
1704
|
+
if (s.stream === void 0) return;
|
|
1705
|
+
const runId = rid();
|
|
1706
|
+
await startRun(s, runId);
|
|
1707
|
+
await startStep(s, runId, "s", { kind: "streaming" });
|
|
1708
|
+
const events = drain(s.stream.subscribeStream(runId, "s"), "completed");
|
|
1709
|
+
s.stream.publishChunk(runId, "s", "a");
|
|
1710
|
+
s.stream.publishChunk(runId, "s", "b");
|
|
1711
|
+
await s.appendFact(
|
|
1712
|
+
runId,
|
|
1713
|
+
Facts.stepCompleted(runId, "s", A1, null, /* @__PURE__ */ new Date())
|
|
1714
|
+
);
|
|
1715
|
+
eq(
|
|
1716
|
+
await events,
|
|
1717
|
+
[
|
|
1718
|
+
{ kind: "chunk", chunk: "a" },
|
|
1719
|
+
{ kind: "chunk", chunk: "b" }
|
|
1720
|
+
],
|
|
1721
|
+
"chunks in order, then closed"
|
|
1722
|
+
);
|
|
1723
|
+
}
|
|
1724
|
+
},
|
|
1725
|
+
{
|
|
1726
|
+
name: "stream: step.failed closes with the error; a run end closes a step that never emitted",
|
|
1727
|
+
async run(h) {
|
|
1728
|
+
const s = await h.makeStore({ leaseMs: LEASE_MS });
|
|
1729
|
+
if (s.stream === void 0) return;
|
|
1730
|
+
const runId = rid();
|
|
1731
|
+
await startRun(s, runId);
|
|
1732
|
+
await startStep(s, runId, "bad", { kind: "streaming" });
|
|
1733
|
+
await startStep(s, runId, "quiet", { kind: "streaming" });
|
|
1734
|
+
const bad = drain(s.stream.subscribeStream(runId, "bad"), "failed");
|
|
1735
|
+
const quiet = drain(s.stream.subscribeStream(runId, "quiet"), "run end");
|
|
1736
|
+
const error = { name: "E", message: "x" };
|
|
1737
|
+
await s.appendFact(
|
|
1738
|
+
runId,
|
|
1739
|
+
Facts.stepFailed(runId, "bad", A1, error, /* @__PURE__ */ new Date())
|
|
1740
|
+
);
|
|
1741
|
+
eq(await bad, [{ kind: "error", error }], "error event, then closed");
|
|
1742
|
+
await endRun(s, runId, "canceled");
|
|
1743
|
+
eq(await quiet, [], "closed by the run end");
|
|
1744
|
+
}
|
|
1745
|
+
},
|
|
1746
|
+
{
|
|
1747
|
+
name: "stream: subscribing to an already-settled step closes instead of hanging",
|
|
1748
|
+
async run(h) {
|
|
1749
|
+
const s = await h.makeStore({ leaseMs: LEASE_MS });
|
|
1750
|
+
if (s.stream === void 0) return;
|
|
1751
|
+
const runId = rid();
|
|
1752
|
+
await startRun(s, runId);
|
|
1753
|
+
await startStep(s, runId, "s", { kind: "streaming" });
|
|
1754
|
+
await s.appendFact(
|
|
1755
|
+
runId,
|
|
1756
|
+
Facts.stepCompleted(runId, "s", A1, null, /* @__PURE__ */ new Date())
|
|
1757
|
+
);
|
|
1758
|
+
eq(
|
|
1759
|
+
await drain(s.stream.subscribeStream(runId, "s"), "settled step"),
|
|
1760
|
+
[],
|
|
1761
|
+
"empty and closed"
|
|
1762
|
+
);
|
|
1763
|
+
}
|
|
1764
|
+
},
|
|
1765
|
+
{
|
|
1766
|
+
name: "sweepLeases: a failing enqueue leaves nothing half-reaped \u2014 the lease stays reapable and no lease.reaped is written",
|
|
1767
|
+
async run(h) {
|
|
1768
|
+
const s = await h.makeStore({ leaseMs: SHORT_LEASE_MS });
|
|
1769
|
+
const runId = rid();
|
|
1770
|
+
await startRun(s, runId);
|
|
1771
|
+
await startStep(s, runId, "s");
|
|
1772
|
+
ok(await s.claimStep(runId, "s", A1) !== null, "claim");
|
|
1773
|
+
await sleep(PAST_LEASE_MS);
|
|
1774
|
+
await rejects(
|
|
1775
|
+
s.sweepLeases({ now: /* @__PURE__ */ new Date(), queue: failingQueue() }),
|
|
1776
|
+
"the enqueue failure surfaces"
|
|
1777
|
+
);
|
|
1778
|
+
eq(
|
|
1779
|
+
(await s.loadRunState(runId)).facts.filter(
|
|
1780
|
+
(f) => f.kind === "lease.reaped"
|
|
1781
|
+
).length,
|
|
1782
|
+
0,
|
|
1783
|
+
"no lease.reaped"
|
|
1784
|
+
);
|
|
1785
|
+
eq(
|
|
1786
|
+
(await s.sweepLeases({ now: /* @__PURE__ */ new Date(), queue: new InMemoryQueue() })).map((r) => r.runId),
|
|
1787
|
+
[runId],
|
|
1788
|
+
"the next sweep still reaps it"
|
|
1789
|
+
);
|
|
1790
|
+
}
|
|
1330
1791
|
}
|
|
1331
1792
|
];
|
|
1332
1793
|
|