eutaxy 0.15.0 → 0.16.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/README.md +6 -6
- package/dist/aws.d.mts +2 -2
- package/dist/aws.d.mts.map +1 -1
- package/dist/aws.mjs +53 -79
- package/dist/aws.mjs.map +1 -1
- package/dist/bin.mjs +1 -1
- package/dist/{cli-CzGCMudc.mjs → cli-DbLlYHTY.mjs} +406 -196
- package/dist/cli-DbLlYHTY.mjs.map +1 -0
- package/dist/cli.mjs +1 -1
- package/dist/cloudflare.d.mts +7 -7
- package/dist/cloudflare.d.mts.map +1 -1
- package/dist/cloudflare.mjs +2 -2
- package/dist/delivery-invariants-90c5xBcd.mjs +44 -0
- package/dist/delivery-invariants-90c5xBcd.mjs.map +1 -0
- package/dist/{delivery-invariants-DzMy3iPs.d.mts → delivery-invariants-B8ZZnCZg.d.mts} +2 -2
- package/dist/{delivery-invariants-DzMy3iPs.d.mts.map → delivery-invariants-B8ZZnCZg.d.mts.map} +1 -1
- package/dist/{explore-B52m-wmk.d.mts → explore-DlwIUowm.d.mts} +2 -2
- package/dist/{explore-B52m-wmk.d.mts.map → explore-DlwIUowm.d.mts.map} +1 -1
- package/dist/index.d.mts +2 -2
- package/dist/index.mjs +2 -2
- package/dist/{platform-Ba1Kvdza.d.mts → platform-DPJTjpSr.d.mts} +56 -4
- package/dist/{platform-Ba1Kvdza.d.mts.map → platform-DPJTjpSr.d.mts.map} +1 -1
- package/dist/{src-ud5wEfnx.mjs → src-CmHdPsZy.mjs} +1297 -388
- package/dist/src-CmHdPsZy.mjs.map +1 -0
- package/dist/{src-nwQA9Xwr.mjs → src-Dz9Wn2vL.mjs} +2 -2
- package/dist/{src-nwQA9Xwr.mjs.map → src-Dz9Wn2vL.mjs.map} +1 -1
- package/dist/{workerd-Be43_PZo.mjs → workerd-D9cUGjYv.mjs} +571 -483
- package/dist/workerd-D9cUGjYv.mjs.map +1 -0
- package/package.json +3 -3
- package/dist/cli-CzGCMudc.mjs.map +0 -1
- package/dist/delivery-invariants-BCtvzAfW.mjs +0 -67
- package/dist/delivery-invariants-BCtvzAfW.mjs.map +0 -1
- package/dist/src-ud5wEfnx.mjs.map +0 -1
- package/dist/workerd-Be43_PZo.mjs.map +0 -1
|
@@ -29,18 +29,54 @@ function canonicalJson(value) {
|
|
|
29
29
|
default: throw new TypeError(`not JSON data: ${typeof value}`);
|
|
30
30
|
}
|
|
31
31
|
}
|
|
32
|
+
// packages/core/src/delivery.ts
|
|
33
|
+
/** The invocation one attempt of an event runs as: `<event>#<attempt>`, so a schedule's faults stay keyed when other events are removed. */
|
|
34
|
+
function invocationOf(event) {
|
|
35
|
+
return `${event.id}#${event.attempt}`;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* The pending event to deliver next, as the engine and the raw sides pick
|
|
39
|
+
* it: the earliest by `at` (an event without one is due now), and the first
|
|
40
|
+
* queued among equals. The index into `pending`. Pure.
|
|
41
|
+
*/
|
|
42
|
+
function earliestEvent(pending) {
|
|
43
|
+
let best = 0;
|
|
44
|
+
for (let i = 1; i < pending.length; i++) if ((pending[i]?.at ?? 0) < (pending[best]?.at ?? 0)) best = i;
|
|
45
|
+
return best;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* The next attempt of an event that was not handled (a message not
|
|
49
|
+
* acknowledged or deleted, a request its caller resends, an alarm that
|
|
50
|
+
* failed), or null when it is dropped (dead-lettered, given up on). It is
|
|
51
|
+
* delivered again while both the platform's own limit on redeliveries
|
|
52
|
+
* (Queues' `max_retries`, the caller's `retries`, SQS's `maxReceiveCount`
|
|
53
|
+
* less the first delivery, an alarm's six retries) and the scenario's
|
|
54
|
+
* `maxRedeliveries` leave room; the redeliveries so far are `attempt - 1`.
|
|
55
|
+
* Pure.
|
|
56
|
+
*/
|
|
57
|
+
function redelivery(event, limit, bounds) {
|
|
58
|
+
const redeliveries = event.attempt - 1;
|
|
59
|
+
if (redeliveries >= limit || redeliveries >= bounds.maxRedeliveries) return null;
|
|
60
|
+
return {
|
|
61
|
+
...event,
|
|
62
|
+
attempt: event.attempt + 1
|
|
63
|
+
};
|
|
64
|
+
}
|
|
32
65
|
// packages/core/src/selectors.ts
|
|
33
66
|
/** Picking effect records out of the log, and reading paths out of their payloads. */
|
|
67
|
+
const partsOf = (path) => path === "" ? [] : path.split(".");
|
|
68
|
+
const indexPattern = /^(0|[1-9]\d*)$/;
|
|
34
69
|
/** Read a dot path such as "args.params.0" out of JSON data; undefined when absent. */
|
|
35
70
|
function getPath(value, path) {
|
|
71
|
+
return readParts(value, partsOf(path));
|
|
72
|
+
}
|
|
73
|
+
function readParts(value, parts) {
|
|
36
74
|
let cur = value;
|
|
37
|
-
|
|
38
|
-
for (const part of path.split(".")) {
|
|
75
|
+
for (const part of parts) {
|
|
39
76
|
if (cur === null || typeof cur !== "object") return void 0;
|
|
40
77
|
if (Array.isArray(cur)) {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
cur = cur[i];
|
|
78
|
+
if (!indexPattern.test(part)) return void 0;
|
|
79
|
+
cur = cur[Number(part)];
|
|
44
80
|
} else cur = cur[part];
|
|
45
81
|
if (cur === void 0) return void 0;
|
|
46
82
|
}
|
|
@@ -58,61 +94,30 @@ function jsonEqual(a, b) {
|
|
|
58
94
|
const bk = Object.keys(bo).sort();
|
|
59
95
|
return ak.length === bk.length && ak.every((k, i) => k === bk[i] && jsonEqual(ao[k], bo[k]));
|
|
60
96
|
}
|
|
61
|
-
function
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
const
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
97
|
+
function compileSelector(sel) {
|
|
98
|
+
const where = Object.entries(sel.where ?? {}).map(([p, want]) => [partsOf(p), want]);
|
|
99
|
+
const atLeast = Object.entries(sel.atLeast ?? {}).map(([p, min]) => [partsOf(p), min]);
|
|
100
|
+
return (record) => {
|
|
101
|
+
if (record.target !== sel.target) return false;
|
|
102
|
+
if (sel.op !== void 0 && record.op !== sel.op) return false;
|
|
103
|
+
if (sel.kind !== void 0 && record.kind !== sel.kind) return false;
|
|
104
|
+
for (const [parts, want] of where) if (!jsonEqual(readParts(record.payload, parts), want)) return false;
|
|
105
|
+
for (const [parts, min] of atLeast) {
|
|
106
|
+
const v = readParts(record.payload, parts);
|
|
107
|
+
if (typeof v !== "number" || v < min) return false;
|
|
108
|
+
}
|
|
109
|
+
return true;
|
|
110
|
+
};
|
|
73
111
|
}
|
|
74
112
|
function selectEffects(log, sel) {
|
|
75
|
-
return log.entries.filter((
|
|
113
|
+
return log.entries.filter(compileSelector(sel));
|
|
76
114
|
}
|
|
77
115
|
/** Canonical form of a key, so equal JSON values compare equal whatever their key order. */
|
|
78
116
|
function keyOf(record, path) {
|
|
79
117
|
const v = getPath(record.payload, path);
|
|
80
118
|
return v === void 0 ? void 0 : canonicalJson(v);
|
|
81
119
|
}
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* UTF-8 encode a string, with no runtime dependency on TextEncoder. An unpaired
|
|
85
|
-
* surrogate becomes U+FFFD, exactly as TextEncoder does, so the output is
|
|
86
|
-
* always valid UTF-8.
|
|
87
|
-
*/
|
|
88
|
-
function utf8Encode(s) {
|
|
89
|
-
const out = [];
|
|
90
|
-
for (const ch of s) {
|
|
91
|
-
let cp = ch.codePointAt(0) ?? 0;
|
|
92
|
-
if (cp >= 55296 && cp <= 57343) cp = 65533;
|
|
93
|
-
if (cp < 128) out.push(cp);
|
|
94
|
-
else if (cp < 2048) out.push(192 | cp >> 6, 128 | cp & 63);
|
|
95
|
-
else if (cp < 65536) out.push(224 | cp >> 12, 128 | cp >> 6 & 63, 128 | cp & 63);
|
|
96
|
-
else out.push(240 | cp >> 18, 128 | cp >> 12 & 63, 128 | cp >> 6 & 63, 128 | cp & 63);
|
|
97
|
-
}
|
|
98
|
-
return Uint8Array.from(out);
|
|
99
|
-
}
|
|
100
|
-
// packages/core/src/effect-log.ts
|
|
101
|
-
/**
|
|
102
|
-
* The effect log: the complete, ordered record of what a simulated run did at
|
|
103
|
-
* its effect boundaries. It is plain data, appended immutably, and has one
|
|
104
|
-
* canonical serialisation, so two runs are identical exactly when their
|
|
105
|
-
* serialised logs are byte-identical.
|
|
106
|
-
*/
|
|
107
|
-
const emptyLog = Object.freeze({ entries: Object.freeze([]) });
|
|
108
|
-
/** Append one effect, assigning its sequence number. The input log is unchanged. */
|
|
109
|
-
function appendEffect(log, effect) {
|
|
110
|
-
const record = {
|
|
111
|
-
...effect,
|
|
112
|
-
seq: log.entries.length
|
|
113
|
-
};
|
|
114
|
-
return { entries: [...log.entries, record] };
|
|
115
|
-
}
|
|
120
|
+
Object.freeze({ entries: Object.freeze([]) });
|
|
116
121
|
/** One canonical JSON object per line, in sequence order. */
|
|
117
122
|
function serializeLog(log) {
|
|
118
123
|
return log.entries.map((e) => canonicalJson({
|
|
@@ -124,16 +129,6 @@ function serializeLog(log) {
|
|
|
124
129
|
payload: e.payload
|
|
125
130
|
})).join("\n");
|
|
126
131
|
}
|
|
127
|
-
/** A 64-bit FNV-1a digest of the serialised log, as 16 hex digits. Not cryptographic. */
|
|
128
|
-
function digestLog(log) {
|
|
129
|
-
const bytes = utf8Encode(serializeLog(log));
|
|
130
|
-
let h = 14695981039346656037n;
|
|
131
|
-
for (const b of bytes) {
|
|
132
|
-
h ^= BigInt(b);
|
|
133
|
-
h = h * 1099511628211n & 18446744073709551615n;
|
|
134
|
-
}
|
|
135
|
-
return h.toString(16).padStart(16, "0");
|
|
136
|
-
}
|
|
137
132
|
// packages/core/src/prng.ts
|
|
138
133
|
function rotl(x, k) {
|
|
139
134
|
return (x << k | x >>> 32 - k) >>> 0;
|
|
@@ -279,14 +274,27 @@ function decisionTarget(fn) {
|
|
|
279
274
|
}
|
|
280
275
|
function checkRefines(log, inv) {
|
|
281
276
|
const target = decisionTarget(inv.decision);
|
|
277
|
+
const entries = log.entries;
|
|
278
|
+
const lastBelow = (seq) => {
|
|
279
|
+
if (entries[seq - 1]?.seq === seq - 1) return seq - 1;
|
|
280
|
+
let i = entries.length - 1;
|
|
281
|
+
while (i >= 0 && entries[i].seq >= seq) i--;
|
|
282
|
+
return i;
|
|
283
|
+
};
|
|
284
|
+
const firstAbove = (seq) => {
|
|
285
|
+
if (entries[seq + 1]?.seq === seq + 1) return seq + 1;
|
|
286
|
+
const i = entries.findIndex((e) => e.seq > seq);
|
|
287
|
+
return i === -1 ? entries.length : i;
|
|
288
|
+
};
|
|
289
|
+
/** The last record before `seq` in `invocation` that `pick` accepts. */
|
|
282
290
|
const before = (seq, invocation, pick) => {
|
|
283
|
-
let
|
|
284
|
-
|
|
285
|
-
if (e.
|
|
286
|
-
if (e.invocation === invocation && pick(e)) found = e;
|
|
291
|
+
for (let i = lastBelow(seq); i >= 0; i--) {
|
|
292
|
+
const e = entries[i];
|
|
293
|
+
if (e.invocation === invocation && pick(e)) return e;
|
|
287
294
|
}
|
|
288
|
-
return found;
|
|
289
295
|
};
|
|
296
|
+
const isRead = inv.state ? compileSelector(inv.state.read) : null;
|
|
297
|
+
const isWrite = inv.state ? compileSelector(inv.state.write) : null;
|
|
290
298
|
for (const effect of selectEffects(log, inv.effect)) {
|
|
291
299
|
const what = `${effect.target} ${effect.op} (seq ${effect.seq}) in ${effect.invocation}`;
|
|
292
300
|
const decision = before(effect.seq, effect.invocation, (e) => e.target === target && e.kind === "result" && getPath(e.payload, "error") === void 0);
|
|
@@ -310,8 +318,8 @@ function checkRefines(log, inv) {
|
|
|
310
318
|
}
|
|
311
319
|
}
|
|
312
320
|
const state = inv.state;
|
|
313
|
-
if (!state) continue;
|
|
314
|
-
const read = before(decision.seq, effect.invocation,
|
|
321
|
+
if (!state || !isRead || !isWrite) continue;
|
|
322
|
+
const read = before(decision.seq, effect.invocation, isRead);
|
|
315
323
|
if (!read) return {
|
|
316
324
|
invariant: inv.name,
|
|
317
325
|
message: `${what} carries out the decision at seq ${decision.seq}, which rests on no read of the state: ${state.read.target} ${state.read.op ?? "*"} was not read before it in that invocation`,
|
|
@@ -319,7 +327,15 @@ function checkRefines(log, inv) {
|
|
|
319
327
|
evidence: [decision.seq, effect.seq]
|
|
320
328
|
};
|
|
321
329
|
const key = state.key === void 0 ? void 0 : keyOf(read, state.key);
|
|
322
|
-
|
|
330
|
+
let write;
|
|
331
|
+
for (let i = firstAbove(read.seq); i < entries.length; i++) {
|
|
332
|
+
const e = entries[i];
|
|
333
|
+
if (e.seq >= effect.seq) break;
|
|
334
|
+
if (e.invocation !== effect.invocation && isWrite(e) && (state.key === void 0 || keyOf(e, state.key) === key)) {
|
|
335
|
+
write = e;
|
|
336
|
+
break;
|
|
337
|
+
}
|
|
338
|
+
}
|
|
323
339
|
if (write) return {
|
|
324
340
|
invariant: inv.name,
|
|
325
341
|
message: `${what} carries out a decision made on a stale read: the decision at seq ${decision.seq} rests on the read at seq ${read.seq}${key === void 0 ? "" : ` of ${key}`}, and ${write.invocation} changed it at seq ${write.seq} before the effect committed`,
|
|
@@ -338,13 +354,17 @@ function checkRefines(log, inv) {
|
|
|
338
354
|
/** State invariants: conditions on the rows of the end-of-run snapshots recorded in the log. */
|
|
339
355
|
/** The tables in the last snapshot recorded for a target, or undefined when none was. */
|
|
340
356
|
function lastSnapshot(log, target) {
|
|
357
|
+
const e = findSnapshot(log, target);
|
|
358
|
+
if (!e) return void 0;
|
|
359
|
+
const tables = getPath(e.payload, "tables");
|
|
360
|
+
if (tables && typeof tables === "object" && !Array.isArray(tables)) return tables;
|
|
361
|
+
return {};
|
|
362
|
+
}
|
|
363
|
+
/** The last snapshot recorded for a target, found from the end of the log. */
|
|
364
|
+
function findSnapshot(log, target) {
|
|
341
365
|
for (let i = log.entries.length - 1; i >= 0; i--) {
|
|
342
366
|
const e = log.entries[i];
|
|
343
|
-
if (e.kind === "snapshot" && e.target === target)
|
|
344
|
-
const tables = getPath(e.payload, "tables");
|
|
345
|
-
if (tables && typeof tables === "object" && !Array.isArray(tables)) return tables;
|
|
346
|
-
return {};
|
|
347
|
-
}
|
|
367
|
+
if (e.kind === "snapshot" && e.target === target) return e;
|
|
348
368
|
}
|
|
349
369
|
}
|
|
350
370
|
function rowMatches(row, condition) {
|
|
@@ -369,18 +389,21 @@ function queryRows(log, query) {
|
|
|
369
389
|
return rows.filter((r) => where.every((c) => rowMatches(r, c)));
|
|
370
390
|
}
|
|
371
391
|
function snapshotSeq(log, target) {
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
return last === void 0 ? [] : [last];
|
|
392
|
+
const last = findSnapshot(log, target);
|
|
393
|
+
return last === void 0 ? [] : [last.seq];
|
|
375
394
|
}
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
invariant
|
|
380
|
-
message: `no snapshot of ${
|
|
395
|
+
/** The violation a state invariant reports when the target it reads was never snapshotted. */
|
|
396
|
+
function noSnapshot(invariant, target) {
|
|
397
|
+
return {
|
|
398
|
+
invariant,
|
|
399
|
+
message: `no snapshot of ${target} was recorded`,
|
|
381
400
|
keys: [],
|
|
382
401
|
evidence: []
|
|
383
402
|
};
|
|
403
|
+
}
|
|
404
|
+
function checkEveryRow(log, inv) {
|
|
405
|
+
const rows = queryRows(log, inv.query);
|
|
406
|
+
if (rows === void 0) return noSnapshot(inv.name, inv.query.target);
|
|
384
407
|
const failing = rows.filter((r) => !inv.holds.every((c) => rowMatches(r, c)));
|
|
385
408
|
if (failing.length === 0) return null;
|
|
386
409
|
const keys = failing.map((r) => canonicalJson(r));
|
|
@@ -394,12 +417,7 @@ function checkEveryRow(log, inv) {
|
|
|
394
417
|
}
|
|
395
418
|
function checkUnique(log, inv) {
|
|
396
419
|
const rows = queryRows(log, inv.query);
|
|
397
|
-
if (rows === void 0) return
|
|
398
|
-
invariant: inv.name,
|
|
399
|
-
message: `no snapshot of ${inv.query.target} was recorded`,
|
|
400
|
-
keys: [],
|
|
401
|
-
evidence: []
|
|
402
|
-
};
|
|
420
|
+
if (rows === void 0) return noSnapshot(inv.name, inv.query.target);
|
|
403
421
|
const counts = /* @__PURE__ */ new Map();
|
|
404
422
|
for (const r of rows) {
|
|
405
423
|
const v = getPath(r, inv.path);
|
|
@@ -419,42 +437,32 @@ function checkUnique(log, inv) {
|
|
|
419
437
|
}
|
|
420
438
|
function checkImplies(log, inv) {
|
|
421
439
|
const rows = queryRows(log, inv.state);
|
|
422
|
-
if (rows === void 0) return
|
|
423
|
-
invariant: inv.name,
|
|
424
|
-
message: `no snapshot of ${inv.state.target} was recorded`,
|
|
425
|
-
keys: [],
|
|
426
|
-
evidence: []
|
|
427
|
-
};
|
|
440
|
+
if (rows === void 0) return noSnapshot(inv.name, inv.state.target);
|
|
428
441
|
const present = /* @__PURE__ */ new Set();
|
|
429
442
|
for (const r of rows) {
|
|
430
443
|
const v = r[inv.column];
|
|
431
444
|
if (v !== void 0) present.add(canonicalJson(v));
|
|
432
445
|
}
|
|
433
|
-
const missing =
|
|
446
|
+
const missing = /* @__PURE__ */ new Set();
|
|
434
447
|
const evidence = [];
|
|
435
448
|
for (const e of selectEffects(log, inv.when)) {
|
|
436
449
|
const k = keyOf(e, inv.key);
|
|
437
450
|
if (k === void 0 || present.has(k)) continue;
|
|
438
|
-
|
|
451
|
+
missing.add(k);
|
|
439
452
|
evidence.push(e.seq);
|
|
440
453
|
}
|
|
441
|
-
if (missing.
|
|
454
|
+
if (missing.size === 0) return null;
|
|
442
455
|
return {
|
|
443
456
|
invariant: inv.name,
|
|
444
|
-
message: `${inv.when.target} ${inv.when.op ?? "*"} happened for key(s) ${missing.join(", ")} but ${inv.state.table}.${inv.column} in ${inv.state.target} has no such row`,
|
|
445
|
-
keys: missing,
|
|
457
|
+
message: `${inv.when.target} ${inv.when.op ?? "*"} happened for key(s) ${[...missing].join(", ")} but ${inv.state.table}.${inv.column} in ${inv.state.target} has no such row`,
|
|
458
|
+
keys: [...missing],
|
|
446
459
|
evidence
|
|
447
460
|
};
|
|
448
461
|
}
|
|
449
462
|
function checkReferences(log, inv) {
|
|
450
463
|
const rows = queryRows(log, inv.query);
|
|
451
464
|
const targets = queryRows(log, inv.in);
|
|
452
|
-
for (const [q, r] of [[inv.query, rows], [inv.in, targets]]) if (r === void 0) return
|
|
453
|
-
invariant: inv.name,
|
|
454
|
-
message: `no snapshot of ${q.target} was recorded`,
|
|
455
|
-
keys: [],
|
|
456
|
-
evidence: []
|
|
457
|
-
};
|
|
465
|
+
for (const [q, r] of [[inv.query, rows], [inv.in, targets]]) if (r === void 0) return noSnapshot(inv.name, q.target);
|
|
458
466
|
const pointedAt = /* @__PURE__ */ new Set();
|
|
459
467
|
for (const t of targets ?? []) {
|
|
460
468
|
const v = t[inv.column];
|
|
@@ -568,8 +576,8 @@ function validateScenario(spec) {
|
|
|
568
576
|
const errors = [];
|
|
569
577
|
if (spec.version !== 1) errors.push(`version must be 1, got ${String(spec.version)}`);
|
|
570
578
|
if (!spec.name.trim()) errors.push("name must not be empty");
|
|
579
|
+
else if (!/^[a-z0-9][a-z0-9-]*$/.test(spec.name)) errors.push(`name must be lowercase letters, digits and dashes: ${JSON.stringify(spec.name)}`);
|
|
571
580
|
if (spec.platform !== void 0 && !spec.platform) errors.push("platform must not be empty when given");
|
|
572
|
-
if (!/^[a-z0-9][a-z0-9-]*$/.test(spec.name)) errors.push(`name must be lowercase letters, digits and dashes: ${JSON.stringify(spec.name)}`);
|
|
573
581
|
if (!spec.worker.entry) errors.push("worker.entry must not be empty");
|
|
574
582
|
if (!spec.worker.config) errors.push("worker.config must not be empty");
|
|
575
583
|
if (!spec.drive.kind) errors.push("drive.kind must not be empty");
|
|
@@ -592,7 +600,7 @@ function validateScenario(spec) {
|
|
|
592
600
|
const ext = /* @__PURE__ */ new Set();
|
|
593
601
|
for (const e of spec.externals) {
|
|
594
602
|
if (!e.name) errors.push("every external needs a name");
|
|
595
|
-
if (ext.has(e.name)) errors.push(`duplicate external ${JSON.stringify(e.name)}`);
|
|
603
|
+
else if (ext.has(e.name)) errors.push(`duplicate external ${JSON.stringify(e.name)}`);
|
|
596
604
|
ext.add(e.name);
|
|
597
605
|
if (!e.host) errors.push(`external ${JSON.stringify(e.name)} needs a host`);
|
|
598
606
|
(e.responses ?? []).forEach((r, i) => {
|
|
@@ -831,12 +839,14 @@ function patchProperty(target, key, patch) {
|
|
|
831
839
|
* functions are replaced by deterministic versions driven by the run's seed
|
|
832
840
|
* and by a clock that starts at a fixed epoch and advances only when the
|
|
833
841
|
* engine says so: one millisecond per effect, and to each timer's due time
|
|
834
|
-
* when the timer fires. Timers fire
|
|
835
|
-
*
|
|
842
|
+
* when the timer fires. Timers fire in due order and never race real I/O:
|
|
843
|
+
* in a sequential run only while the invocation is idle (no effect in
|
|
844
|
+
* flight); in a concurrent run one already due is a turn like a parked
|
|
845
|
+
* effect, and one not yet due fires only when nothing else can move.
|
|
836
846
|
*/
|
|
837
847
|
/** 2026-01-01T00:00:00.000Z. Every run starts here. */
|
|
838
848
|
const virtualEpoch = Date.UTC(2026, 0, 1);
|
|
839
|
-
const realSetTimeout = globalThis.setTimeout.bind(globalThis);
|
|
849
|
+
const realSetTimeout$1 = globalThis.setTimeout.bind(globalThis);
|
|
840
850
|
const realSetInterval = globalThis.setInterval.bind(globalThis);
|
|
841
851
|
const realClearTimeout = globalThis.clearTimeout.bind(globalThis);
|
|
842
852
|
const realClearInterval = globalThis.clearInterval.bind(globalThis);
|
|
@@ -852,11 +862,19 @@ const libraryScope = asyncScope();
|
|
|
852
862
|
var VirtualClock = class {
|
|
853
863
|
time = virtualEpoch;
|
|
854
864
|
timers = [];
|
|
865
|
+
/** Deferred timers fired and not yet run by their pollers: work on its way, inside a host. */
|
|
866
|
+
unrun = /* @__PURE__ */ new Set();
|
|
867
|
+
/** Set by `stop`: the run is over, so no timer is pending and every deferred poller ends. */
|
|
868
|
+
stopped = false;
|
|
855
869
|
nextId = 1;
|
|
856
870
|
/** The invocation whose code is running; timers scheduled now belong to it. */
|
|
857
871
|
owner = null;
|
|
858
872
|
/** Set by a concurrent run: who is running, asked when a timer is scheduled (overrides `owner`). */
|
|
859
873
|
ownerNow = null;
|
|
874
|
+
/** Set by the run: the effect being performed by the code scheduling a timer, kept on the timer. */
|
|
875
|
+
scopeNow = null;
|
|
876
|
+
/** Set by the run: told whose timer fired (or was marked fired), whichever path fired it. */
|
|
877
|
+
afterFire = null;
|
|
860
878
|
/** Set by a concurrent run: binds a callback to the async context it was scheduled in. */
|
|
861
879
|
bind = null;
|
|
862
880
|
now() {
|
|
@@ -865,7 +883,7 @@ var VirtualClock = class {
|
|
|
865
883
|
advance(ms) {
|
|
866
884
|
if (ms > 0) this.time += ms;
|
|
867
885
|
}
|
|
868
|
-
schedule(callback, delay, args, interval) {
|
|
886
|
+
schedule(callback, delay, args, interval, owner) {
|
|
869
887
|
const ms = Number.isFinite(delay) && delay > 0 ? Math.floor(delay) : 0;
|
|
870
888
|
const timer = {
|
|
871
889
|
id: this.nextId++,
|
|
@@ -873,16 +891,53 @@ var VirtualClock = class {
|
|
|
873
891
|
callback: this.bind ? this.bind(callback) : callback,
|
|
874
892
|
args,
|
|
875
893
|
interval,
|
|
876
|
-
owner: this.ownerNow ? this.ownerNow() : this.owner
|
|
894
|
+
owner: owner ?? (this.ownerNow ? this.ownerNow() : this.owner),
|
|
895
|
+
scope: owner === void 0 ? this.scopeNow?.() : void 0,
|
|
896
|
+
deferred: false,
|
|
897
|
+
deadline: owner !== void 0,
|
|
898
|
+
fired: 0
|
|
877
899
|
};
|
|
900
|
+
if (this.stopped) return timer.id;
|
|
901
|
+
if (timer.scope !== void 0 && inWorkerd) {
|
|
902
|
+
timer.deferred = true;
|
|
903
|
+
this.pollDeferred(timer);
|
|
904
|
+
}
|
|
878
905
|
this.timers.push(timer);
|
|
879
906
|
return timer.id;
|
|
880
907
|
}
|
|
908
|
+
/**
|
|
909
|
+
* Run a deferred timer's callback from where it was scheduled: a real zero
|
|
910
|
+
* timer made here, inside the host's code, polls until `fireNext` has
|
|
911
|
+
* fired it (as often as it fired, for an interval) or it was cleared.
|
|
912
|
+
*/
|
|
913
|
+
pollDeferred(timer) {
|
|
914
|
+
const tick = () => {
|
|
915
|
+
while (timer.fired > 0) {
|
|
916
|
+
timer.fired--;
|
|
917
|
+
if (timer.fired === 0) this.unrun.delete(timer);
|
|
918
|
+
try {
|
|
919
|
+
timer.callback(...timer.args);
|
|
920
|
+
} catch {}
|
|
921
|
+
}
|
|
922
|
+
if (timer.fired > 0 || this.timers.includes(timer)) realSetTimeout$1(tick, 0);
|
|
923
|
+
};
|
|
924
|
+
realSetTimeout$1(tick, 0);
|
|
925
|
+
}
|
|
881
926
|
setTimeout(callback, delay = 0, ...args) {
|
|
882
927
|
return this.schedule(callback, delay, args, null);
|
|
883
928
|
}
|
|
929
|
+
/**
|
|
930
|
+
* A timer of `owner`'s set by the run itself, whoever is running (an
|
|
931
|
+
* attempt's deadline): cancelled with the owner's others, and fired
|
|
932
|
+
* whenever time reaches it, whoever's timers may fire then (an object
|
|
933
|
+
* napping inside the attempt's call does not hold the engine's clock).
|
|
934
|
+
*/
|
|
935
|
+
setTimeoutAs(owner, callback, delay) {
|
|
936
|
+
return this.schedule(callback, delay, [], null, owner);
|
|
937
|
+
}
|
|
884
938
|
setInterval(callback, delay = 0, ...args) {
|
|
885
|
-
|
|
939
|
+
const every = Number.isFinite(delay) && delay >= 1 ? Math.floor(delay) : 1;
|
|
940
|
+
return this.schedule(callback, delay, args, every);
|
|
886
941
|
}
|
|
887
942
|
clear(id) {
|
|
888
943
|
this.timers = this.timers.filter((t) => t.id !== id);
|
|
@@ -894,9 +949,32 @@ var VirtualClock = class {
|
|
|
894
949
|
pendingTimers() {
|
|
895
950
|
return this.timers.length;
|
|
896
951
|
}
|
|
952
|
+
/**
|
|
953
|
+
* The run is over: drop every timer, fired or not, so the pollers armed
|
|
954
|
+
* inside hosts' code end (one left polling would keep an object referenced
|
|
955
|
+
* past the run) and nothing of the run's fires later.
|
|
956
|
+
*/
|
|
957
|
+
stop() {
|
|
958
|
+
this.stopped = true;
|
|
959
|
+
for (const t of this.unrun) t.fired = 0;
|
|
960
|
+
this.unrun.clear();
|
|
961
|
+
this.timers = [];
|
|
962
|
+
}
|
|
963
|
+
/** Deferred timers fired and not yet run: what waits on nothing visible is still about to move. */
|
|
964
|
+
firedPending() {
|
|
965
|
+
return this.unrun.size;
|
|
966
|
+
}
|
|
967
|
+
/** Whether a timer scheduled inside the effect `scope` is still pending. */
|
|
968
|
+
hasTimerIn(scope) {
|
|
969
|
+
return scope !== void 0 && this.timers.some((t) => t.scope === scope);
|
|
970
|
+
}
|
|
897
971
|
/** Drop every timer an invocation scheduled: a crashed invocation's code must never run again. */
|
|
898
972
|
cancelOwner(owner) {
|
|
899
973
|
this.timers = this.timers.filter((t) => t.owner !== owner);
|
|
974
|
+
for (const t of this.unrun) if (t.owner === owner) {
|
|
975
|
+
t.fired = 0;
|
|
976
|
+
this.unrun.delete(t);
|
|
977
|
+
}
|
|
900
978
|
}
|
|
901
979
|
earliest() {
|
|
902
980
|
let earliest = this.timers[0];
|
|
@@ -907,20 +985,47 @@ var VirtualClock = class {
|
|
|
907
985
|
nextDue() {
|
|
908
986
|
return this.earliest()?.due;
|
|
909
987
|
}
|
|
910
|
-
/**
|
|
911
|
-
|
|
912
|
-
|
|
988
|
+
/** Advance to the earliest due timer whose owner `owned` accepts and run it. False when none is pending. */
|
|
989
|
+
fireNextOwned(owned) {
|
|
990
|
+
const earliest = this.earliestOwned(owned);
|
|
991
|
+
if (!earliest) return false;
|
|
992
|
+
return this.fire(earliest);
|
|
993
|
+
}
|
|
994
|
+
/**
|
|
995
|
+
* When the earliest timer whose owner `owned` accepts is due, or undefined
|
|
996
|
+
* when none is pending; a deadline (`setTimeoutAs`) counts, as it fires,
|
|
997
|
+
* unless `deadlines` is false: whether an owner has a timer of its own to
|
|
998
|
+
* advance to (an object sleeping before it answers) is asked without them.
|
|
999
|
+
*/
|
|
1000
|
+
nextDueOwned(owned, deadlines = true) {
|
|
1001
|
+
return this.earliestOwned(owned, deadlines)?.due;
|
|
1002
|
+
}
|
|
1003
|
+
earliestOwned(owned, deadlines = true) {
|
|
1004
|
+
let earliest;
|
|
1005
|
+
for (const t of this.timers) if ((deadlines && t.deadline || owned(t.owner)) && (!earliest || t.due < earliest.due || t.due === earliest.due && t.id < earliest.id)) earliest = t;
|
|
1006
|
+
return earliest;
|
|
1007
|
+
}
|
|
1008
|
+
/** Who set the timer `fireNextOwned` would run for `owned`; undefined when none is pending. */
|
|
1009
|
+
nextOwnerOwned(owned) {
|
|
1010
|
+
return this.earliestOwned(owned)?.owner;
|
|
913
1011
|
}
|
|
914
1012
|
/** Advance to the earliest due timer and run it. False when none is pending. */
|
|
915
1013
|
fireNext() {
|
|
916
1014
|
const earliest = this.earliest();
|
|
917
1015
|
if (!earliest) return false;
|
|
1016
|
+
return this.fire(earliest);
|
|
1017
|
+
}
|
|
1018
|
+
fire(earliest) {
|
|
918
1019
|
if (earliest.due > this.time) this.time = earliest.due;
|
|
919
1020
|
if (earliest.interval === null) this.timers = this.timers.filter((t) => t !== earliest);
|
|
920
1021
|
else earliest.due = this.time + earliest.interval;
|
|
921
|
-
|
|
1022
|
+
if (earliest.deferred) {
|
|
1023
|
+
earliest.fired++;
|
|
1024
|
+
this.unrun.add(earliest);
|
|
1025
|
+
} else try {
|
|
922
1026
|
earliest.callback(...earliest.args);
|
|
923
1027
|
} catch {}
|
|
1028
|
+
this.afterFire?.(earliest.owner);
|
|
924
1029
|
return true;
|
|
925
1030
|
}
|
|
926
1031
|
};
|
|
@@ -1013,7 +1118,7 @@ function patchGlobals(seed, options = {}) {
|
|
|
1013
1118
|
return random.float();
|
|
1014
1119
|
});
|
|
1015
1120
|
define(g, "setTimeout", (cb, ms, ...args) => {
|
|
1016
|
-
if (inLibrary()) return realSetTimeout(cb, ms, ...args);
|
|
1121
|
+
if (inLibrary()) return realSetTimeout$1(cb, ms, ...args);
|
|
1017
1122
|
read();
|
|
1018
1123
|
return clock.setTimeout(cb, ms, ...args);
|
|
1019
1124
|
});
|
|
@@ -1069,6 +1174,7 @@ function patchGlobals(seed, options = {}) {
|
|
|
1069
1174
|
restore() {
|
|
1070
1175
|
if (restored) return;
|
|
1071
1176
|
restored = true;
|
|
1177
|
+
clock.stop();
|
|
1072
1178
|
for (const u of undo.reverse()) u();
|
|
1073
1179
|
}
|
|
1074
1180
|
};
|
|
@@ -1080,14 +1186,14 @@ function realDeadline(ms) {
|
|
|
1080
1186
|
let handle;
|
|
1081
1187
|
return {
|
|
1082
1188
|
expired: new Promise((resolve) => {
|
|
1083
|
-
handle = realSetTimeout(() => resolve("expired"), Math.min(ms, maxTimeoutMs));
|
|
1189
|
+
handle = realSetTimeout$1(() => resolve("expired"), Math.min(ms, maxTimeoutMs));
|
|
1084
1190
|
}),
|
|
1085
1191
|
cancel: () => realClearTimeout(handle)
|
|
1086
1192
|
};
|
|
1087
1193
|
}
|
|
1088
1194
|
/** Yield to the real event loop so microtasks and pending I/O can make progress. */
|
|
1089
1195
|
function realTick() {
|
|
1090
|
-
return new Promise((resolve) => realSetTimeout(() => resolve(), 0));
|
|
1196
|
+
return new Promise((resolve) => realSetTimeout$1(() => resolve(), 0));
|
|
1091
1197
|
}
|
|
1092
1198
|
const hostSetImmediate = globalThis.setImmediate;
|
|
1093
1199
|
const inWorkerd = globalThis.navigator?.userAgent === "Cloudflare-Workers" || typeof globalThis.WebSocketPair === "function";
|
|
@@ -1125,15 +1231,34 @@ async function settleOn(promise, ctx) {
|
|
|
1125
1231
|
failed = true;
|
|
1126
1232
|
error = e;
|
|
1127
1233
|
});
|
|
1234
|
+
let afterTimer = false;
|
|
1235
|
+
let idleBefore = false;
|
|
1128
1236
|
for (;;) {
|
|
1129
|
-
await Promise.race([tracked, realTick()]);
|
|
1237
|
+
await Promise.race([tracked, afterTimer ? eventLoopTurn() : realTick()]);
|
|
1238
|
+
const cheap = afterTimer;
|
|
1239
|
+
afterTimer = false;
|
|
1130
1240
|
if (done) {
|
|
1131
1241
|
if (failed) throw error;
|
|
1132
1242
|
return value;
|
|
1133
1243
|
}
|
|
1134
|
-
if (ctx.crashed())
|
|
1135
|
-
|
|
1136
|
-
|
|
1244
|
+
if (ctx.crashed()) {
|
|
1245
|
+
if (ctx.crashError) throw ctx.crashError();
|
|
1246
|
+
return new Promise(() => {});
|
|
1247
|
+
}
|
|
1248
|
+
if (ctx.inFlight() > 0) {
|
|
1249
|
+
const owned = ctx.idle?.() ?? null;
|
|
1250
|
+
if (owned && idleBefore && ctx.clock.fireNextOwned(owned)) {
|
|
1251
|
+
idleBefore = false;
|
|
1252
|
+
afterTimer = true;
|
|
1253
|
+
} else idleBefore = owned !== null;
|
|
1254
|
+
continue;
|
|
1255
|
+
}
|
|
1256
|
+
idleBefore = false;
|
|
1257
|
+
if (ctx.clock.fireNext()) {
|
|
1258
|
+
afterTimer = true;
|
|
1259
|
+
continue;
|
|
1260
|
+
}
|
|
1261
|
+
if (cheap) continue;
|
|
1137
1262
|
ctx.stuck();
|
|
1138
1263
|
return new Promise(() => {});
|
|
1139
1264
|
}
|
|
@@ -1199,11 +1324,32 @@ function replyOf(r) {
|
|
|
1199
1324
|
body
|
|
1200
1325
|
};
|
|
1201
1326
|
}
|
|
1327
|
+
/**
|
|
1328
|
+
* The scripted reply as a `Response`. Its body is known, so `text()` and
|
|
1329
|
+
* `json()` answer from it within a microtask: the model assumes body reads
|
|
1330
|
+
* settle within one turn of the event loop, and a stream read inside a
|
|
1331
|
+
* Durable Object can take a turn or more, during which the object's code
|
|
1332
|
+
* looks idle to the run.
|
|
1333
|
+
*/
|
|
1202
1334
|
function toResponse(reply) {
|
|
1203
|
-
|
|
1335
|
+
const response = new Response(reply.body === "" ? null : reply.body, {
|
|
1204
1336
|
status: reply.status,
|
|
1205
1337
|
headers: reply.headers
|
|
1206
1338
|
});
|
|
1339
|
+
const text = reply.body;
|
|
1340
|
+
Object.defineProperties(response, {
|
|
1341
|
+
text: {
|
|
1342
|
+
value: () => Promise.resolve(text),
|
|
1343
|
+
configurable: true,
|
|
1344
|
+
writable: true
|
|
1345
|
+
},
|
|
1346
|
+
json: {
|
|
1347
|
+
value: () => Promise.resolve().then(() => JSON.parse(text)),
|
|
1348
|
+
configurable: true,
|
|
1349
|
+
writable: true
|
|
1350
|
+
}
|
|
1351
|
+
});
|
|
1352
|
+
return response;
|
|
1207
1353
|
}
|
|
1208
1354
|
/** A request or reply body as effect-log data: JSON when it parses, the text otherwise, null when empty. */
|
|
1209
1355
|
function parseBody(text) {
|
|
@@ -1236,7 +1382,8 @@ function createScriptedFetch(externals, hooks, unsupported, afterRequest = () =>
|
|
|
1236
1382
|
});
|
|
1237
1383
|
const dedupeHeader = external.guarantees.dedupeBy?.toLowerCase();
|
|
1238
1384
|
const dedupeKey = dedupeHeader ? headers[dedupeHeader] ?? null : null;
|
|
1239
|
-
const
|
|
1385
|
+
const given = init?.body;
|
|
1386
|
+
const body = parseBody(typeof given === "string" ? given : given === null || given === void 0 && !(input instanceof Request) ? "" : await request.text());
|
|
1240
1387
|
const args = {
|
|
1241
1388
|
url: request.url,
|
|
1242
1389
|
method,
|
|
@@ -1328,18 +1475,37 @@ async function withoutNetwork(work) {
|
|
|
1328
1475
|
var Frame = class {
|
|
1329
1476
|
/** The invocation whose effect this is. */
|
|
1330
1477
|
invocation;
|
|
1478
|
+
target;
|
|
1479
|
+
op;
|
|
1331
1480
|
/** Whether the effect hosts user code that makes effects of its own (`hostsEffects`). */
|
|
1332
1481
|
hosts;
|
|
1482
|
+
/** Whose code the effect hosts, when it stays alive after the effect (`EffectOptions.host`). */
|
|
1483
|
+
host;
|
|
1333
1484
|
/** False once the effect's perform has ended. */
|
|
1334
1485
|
active = true;
|
|
1335
1486
|
/** The effect's `onAbandon`, called where the crash happens (inside the hosted code's context). */
|
|
1336
1487
|
onAbandon;
|
|
1488
|
+
/** Set by the run: told, after `onAbandon` reset the host, which host it was. */
|
|
1489
|
+
onHostAbandoned;
|
|
1337
1490
|
/** The effect's `mayPark`: whether an effect its hosted code makes may wait for a turn now. */
|
|
1338
1491
|
mayPark;
|
|
1339
1492
|
/** The hosting effect this one was made inside of and waited for a turn under, if any. */
|
|
1340
1493
|
parent;
|
|
1341
1494
|
/** Effects the hosted code made that are parked or running now. */
|
|
1342
1495
|
children = 0;
|
|
1496
|
+
/** Effects nested in this one (made inside its perform, not as turns) being performed now. */
|
|
1497
|
+
performing = 0;
|
|
1498
|
+
/** A host's effect whose call the host's code has started (`Frames.claimHost`). */
|
|
1499
|
+
claimed = false;
|
|
1500
|
+
/** The host's code this effect is the call of has started running (`Run.hostedCode`); until then the call is in transit. */
|
|
1501
|
+
entered = false;
|
|
1502
|
+
/** The host's code this effect is the call of has returned (`Frames.answered`): its answer is in transit. */
|
|
1503
|
+
answered = false;
|
|
1504
|
+
/**
|
|
1505
|
+
* The attempt (`RunHooks.attempt`) whose code made this effect, if any: what
|
|
1506
|
+
* the effect hosts is that attempt's code too, late once the attempt is.
|
|
1507
|
+
*/
|
|
1508
|
+
attempt;
|
|
1343
1509
|
/** Of those, the ones parked, or running but only waiting on parked effects of their own. */
|
|
1344
1510
|
waitingChildren = 0;
|
|
1345
1511
|
abandoned = false;
|
|
@@ -1347,9 +1513,12 @@ var Frame = class {
|
|
|
1347
1513
|
crashed = new Promise((resolve) => {
|
|
1348
1514
|
this.wake = () => resolve("nested_crash");
|
|
1349
1515
|
});
|
|
1350
|
-
constructor(
|
|
1351
|
-
this.invocation = invocation;
|
|
1352
|
-
this.
|
|
1516
|
+
constructor(point, options) {
|
|
1517
|
+
this.invocation = point.invocation;
|
|
1518
|
+
this.target = point.target;
|
|
1519
|
+
this.op = point.op;
|
|
1520
|
+
this.hosts = point.hostsEffects === true;
|
|
1521
|
+
this.host = options.host;
|
|
1353
1522
|
this.onAbandon = options.onAbandon;
|
|
1354
1523
|
this.mayPark = options.mayPark ?? (() => true);
|
|
1355
1524
|
}
|
|
@@ -1357,13 +1526,32 @@ var Frame = class {
|
|
|
1357
1526
|
isWaiting() {
|
|
1358
1527
|
return this.children > 0 && this.waitingChildren === this.children;
|
|
1359
1528
|
}
|
|
1529
|
+
/**
|
|
1530
|
+
* Whether it counts as real I/O pending: unless its hosted code is only
|
|
1531
|
+
* waiting on parked effects of its own; once that code has answered, the
|
|
1532
|
+
* answer is on its way whatever it left parked, so the effect counts again
|
|
1533
|
+
* until it ends.
|
|
1534
|
+
*/
|
|
1535
|
+
inFlight() {
|
|
1536
|
+
return this.answered || !this.isWaiting();
|
|
1537
|
+
}
|
|
1360
1538
|
crash() {
|
|
1361
|
-
this.abandon();
|
|
1362
1539
|
this.wake();
|
|
1540
|
+
this.abandon();
|
|
1541
|
+
}
|
|
1542
|
+
/** Whether the effect was abandoned: a crash in its hosted code, or its I/O past the budget; the host was reset. */
|
|
1543
|
+
get isAbandoned() {
|
|
1544
|
+
return this.abandoned;
|
|
1363
1545
|
}
|
|
1546
|
+
/**
|
|
1547
|
+
* The run's bookkeeping first, the platform's reset last: a Durable
|
|
1548
|
+
* Object's `abort()` tears down the task it is called in, so nothing after
|
|
1549
|
+
* it may be counted on to run.
|
|
1550
|
+
*/
|
|
1364
1551
|
abandon() {
|
|
1365
1552
|
if (this.abandoned) return;
|
|
1366
1553
|
this.abandoned = true;
|
|
1554
|
+
if (this.host !== void 0) this.onHostAbandoned?.(this.host);
|
|
1367
1555
|
this.onAbandon?.();
|
|
1368
1556
|
}
|
|
1369
1557
|
};
|
|
@@ -1379,23 +1567,75 @@ var Frames = class {
|
|
|
1379
1567
|
hosting = /* @__PURE__ */ new Map();
|
|
1380
1568
|
/** Effects with `hostsEffects` being performed now, across invocations. */
|
|
1381
1569
|
hostingCount = 0;
|
|
1570
|
+
/** Per host (`EffectOptions.host`), its effects being performed, innermost last. */
|
|
1571
|
+
byHost = /* @__PURE__ */ new Map();
|
|
1382
1572
|
/** How many effects with `hostsEffects` are being performed now: above 0, hosted code may be running. */
|
|
1383
1573
|
get hostingNow() {
|
|
1384
1574
|
return this.hostingCount;
|
|
1385
1575
|
}
|
|
1576
|
+
/** The innermost effect of `host`'s being performed now, if any: code of the host's runs inside it. */
|
|
1577
|
+
liveHost(host) {
|
|
1578
|
+
return this.byHost.get(host)?.at(-1);
|
|
1579
|
+
}
|
|
1580
|
+
/** The effects of `invocation`'s being performed now that host an object's code (`EffectOptions.host`). */
|
|
1581
|
+
hostingOf(invocation) {
|
|
1582
|
+
return this.stack.filter((f) => f.invocation === invocation && f.host !== void 0);
|
|
1583
|
+
}
|
|
1584
|
+
/**
|
|
1585
|
+
* The oldest effect of `host`'s being performed, with `op` when given, whose
|
|
1586
|
+
* call the host's code has not started yet, now claimed: an object's input
|
|
1587
|
+
* gate delivers calls in the order they arrived, which is the order their
|
|
1588
|
+
* effects entered. Without one of that op, the oldest unclaimed of any.
|
|
1589
|
+
*/
|
|
1590
|
+
claimHost(host, op) {
|
|
1591
|
+
const live = this.byHost.get(host) ?? [];
|
|
1592
|
+
const frame = (op === void 0 ? void 0 : live.find((f) => !f.claimed && f.op === op)) ?? live.find((f) => !f.claimed);
|
|
1593
|
+
if (frame) frame.claimed = true;
|
|
1594
|
+
return frame;
|
|
1595
|
+
}
|
|
1596
|
+
/**
|
|
1597
|
+
* The hosts of the effects in flight, when every one of them is a hosting
|
|
1598
|
+
* effect whose hosted code is idle: running (entered, its answer not yet
|
|
1599
|
+
* on its way), with no effect of it performing or waiting for a turn,
|
|
1600
|
+
* nothing hosted inside it and no real work tracked. Such code can only
|
|
1601
|
+
* be awaiting a timer of its own (an object sleeping before it answers),
|
|
1602
|
+
* which the run may fire for it. Null while anything else is in flight,
|
|
1603
|
+
* or nothing is: a call still crossing into its object is on its way, and
|
|
1604
|
+
* waiting for it keeps the order in which calls enter the object the
|
|
1605
|
+
* schedule's, not real time's (two timers due together fire by the order
|
|
1606
|
+
* they were set).
|
|
1607
|
+
*/
|
|
1608
|
+
idleHosts() {
|
|
1609
|
+
let counted = 0;
|
|
1610
|
+
const hosts = /* @__PURE__ */ new Set();
|
|
1611
|
+
for (const frame of this.stack) {
|
|
1612
|
+
if (frame.isWaiting()) continue;
|
|
1613
|
+
counted++;
|
|
1614
|
+
if (frame.host === void 0 || !frame.entered || frame.answered || frame.performing > 0 || frame.children > 0) return null;
|
|
1615
|
+
hosts.add(frame.host);
|
|
1616
|
+
}
|
|
1617
|
+
return counted > 0 && counted === this.inFlight ? hosts : null;
|
|
1618
|
+
}
|
|
1386
1619
|
/**
|
|
1387
1620
|
* The effect an invocation's new effect is made inside of, if any. Async
|
|
1388
1621
|
* context tells exactly which code is inside; an invocation merely woken
|
|
1389
1622
|
* meanwhile is not. A hosting effect's own effects are attributed to its
|
|
1390
1623
|
* invocation: only those count as nested. Async context can be stale (code
|
|
1391
1624
|
* resumed in the context of an effect that has ended, as an object's code
|
|
1392
|
-
* may run in the context it was built in)
|
|
1393
|
-
*
|
|
1625
|
+
* may run in the context it was built in) or absent (workerd carries none
|
|
1626
|
+
* into an object's code): trust only a live frame of the same invocation
|
|
1627
|
+
* (the innermost); else, for code in a stale context or hosted code
|
|
1628
|
+
* (`hosted`: an object's, entered by the platform), the invocation's own
|
|
1629
|
+
* hosting effect. Code in no context at all that is not hosted is
|
|
1630
|
+
* outside every effect: a second strand of the invocation (a step
|
|
1631
|
+
* attempt the engine gave up on, see `Run.attempt`) running while a call
|
|
1632
|
+
* of the first strand is in flight.
|
|
1394
1633
|
*/
|
|
1395
|
-
enclosing(invocation) {
|
|
1396
|
-
|
|
1397
|
-
const
|
|
1398
|
-
|
|
1634
|
+
enclosing(invocation, hosted = true) {
|
|
1635
|
+
if (!effectScope) return this.stack.at(-1);
|
|
1636
|
+
const store = effectScope.getStore();
|
|
1637
|
+
if (store?.active && store.invocation === invocation) return store;
|
|
1638
|
+
return hosted || store !== void 0 ? this.hosting.get(invocation)?.at(-1) : void 0;
|
|
1399
1639
|
}
|
|
1400
1640
|
/**
|
|
1401
1641
|
* The effect whose perform the current code runs inside, whatever
|
|
@@ -1415,6 +1655,23 @@ var Frames = class {
|
|
|
1415
1655
|
this.hostingFrames(frame.invocation).push(frame);
|
|
1416
1656
|
this.hostingCount++;
|
|
1417
1657
|
}
|
|
1658
|
+
if (frame.host !== void 0) {
|
|
1659
|
+
const frames = this.byHost.get(frame.host) ?? [];
|
|
1660
|
+
frames.push(frame);
|
|
1661
|
+
this.byHost.set(frame.host, frames);
|
|
1662
|
+
}
|
|
1663
|
+
}
|
|
1664
|
+
/**
|
|
1665
|
+
* The host's code `frame` is the call of has returned: its answer is in
|
|
1666
|
+
* transit, so the effect is in flight until it ends, whatever that code
|
|
1667
|
+
* left parked (the run waits for the answer before its next turn, so what
|
|
1668
|
+
* the code left outstanding is handed over before anything else moves).
|
|
1669
|
+
*/
|
|
1670
|
+
answered(frame) {
|
|
1671
|
+
if (frame.answered) return;
|
|
1672
|
+
const before = frame.inFlight();
|
|
1673
|
+
frame.answered = true;
|
|
1674
|
+
if (frame.active && !before) this.inFlight++;
|
|
1418
1675
|
}
|
|
1419
1676
|
/** An effect's perform has ended. */
|
|
1420
1677
|
leave(frame) {
|
|
@@ -1424,8 +1681,12 @@ var Frames = class {
|
|
|
1424
1681
|
this.hostingCount--;
|
|
1425
1682
|
}
|
|
1426
1683
|
this.stack.splice(this.stack.lastIndexOf(frame), 1);
|
|
1427
|
-
if (
|
|
1428
|
-
|
|
1684
|
+
if (frame.host !== void 0) {
|
|
1685
|
+
const frames = this.byHost.get(frame.host);
|
|
1686
|
+
frames?.splice(frames.lastIndexOf(frame), 1);
|
|
1687
|
+
}
|
|
1688
|
+
if (frame.inFlight()) this.inFlight--;
|
|
1689
|
+
if (frame.isWaiting() && frame.parent) {
|
|
1429
1690
|
const parent = frame.parent;
|
|
1430
1691
|
this.adjust(parent, () => parent.waitingChildren--);
|
|
1431
1692
|
}
|
|
@@ -1438,13 +1699,14 @@ var Frames = class {
|
|
|
1438
1699
|
* and a frame that starts or stops waiting does so for its parent too.
|
|
1439
1700
|
*/
|
|
1440
1701
|
adjust(frame, change) {
|
|
1441
|
-
const
|
|
1702
|
+
const waitingBefore = frame.isWaiting();
|
|
1703
|
+
const inFlightBefore = frame.inFlight();
|
|
1442
1704
|
change();
|
|
1443
|
-
const
|
|
1444
|
-
if (
|
|
1445
|
-
if (
|
|
1705
|
+
const waiting = frame.isWaiting();
|
|
1706
|
+
if (frame.active && inFlightBefore !== frame.inFlight()) this.inFlight += waiting ? -1 : 1;
|
|
1707
|
+
if (waitingBefore === waiting) return;
|
|
1446
1708
|
const parent = frame.parent;
|
|
1447
|
-
if (frame.active && parent) this.adjust(parent, () => parent.waitingChildren +=
|
|
1709
|
+
if (frame.active && parent) this.adjust(parent, () => parent.waitingChildren += waiting ? 1 : -1);
|
|
1448
1710
|
}
|
|
1449
1711
|
hostingFrames(invocation) {
|
|
1450
1712
|
let frames = this.hosting.get(invocation);
|
|
@@ -1455,7 +1717,6 @@ var Frames = class {
|
|
|
1455
1717
|
return frames;
|
|
1456
1718
|
}
|
|
1457
1719
|
};
|
|
1458
|
-
// packages/engine/src/turns.ts
|
|
1459
1720
|
var Turns = class {
|
|
1460
1721
|
chooser;
|
|
1461
1722
|
frames;
|
|
@@ -1463,6 +1724,12 @@ var Turns = class {
|
|
|
1463
1724
|
runAs;
|
|
1464
1725
|
/** The virtual globals' read counter, when they are installed. */
|
|
1465
1726
|
reads;
|
|
1727
|
+
/** Work on its way that no frame counts: a host's timer fired and not yet run by its poller. */
|
|
1728
|
+
pending;
|
|
1729
|
+
/** Whether an object's work after an answer may be running: a wake could then come from inside it. */
|
|
1730
|
+
inObjects;
|
|
1731
|
+
/** Whether everything in flight is a hosting effect whose code is idle (see `Frames.idleHosts`). */
|
|
1732
|
+
idleHosted;
|
|
1466
1733
|
/** Invocations waiting at an effect for their turn. */
|
|
1467
1734
|
parked = [];
|
|
1468
1735
|
parkSeq = 0;
|
|
@@ -1470,6 +1737,8 @@ var Turns = class {
|
|
|
1470
1737
|
picked = [];
|
|
1471
1738
|
/** Events started so far, to name the next start the same in every run. */
|
|
1472
1739
|
starts = 0;
|
|
1740
|
+
/** Times the run let time pass over late turns (see `takeTurn`). */
|
|
1741
|
+
waits = 0;
|
|
1473
1742
|
/** The target of the turn running since the last decision; null for an event's start or none. */
|
|
1474
1743
|
turnTarget = null;
|
|
1475
1744
|
commuteNot = false;
|
|
@@ -1483,11 +1752,14 @@ var Turns = class {
|
|
|
1483
1752
|
restsAtStep = 0;
|
|
1484
1753
|
/** The order invocations started in, which orders the parked ones. */
|
|
1485
1754
|
startOrder = /* @__PURE__ */ new Map();
|
|
1486
|
-
constructor(chooser, frames, runAs, reads) {
|
|
1755
|
+
constructor(chooser, frames, runAs, reads, pending = () => 0, inObjects = () => false, idleHosted = () => false) {
|
|
1487
1756
|
this.chooser = chooser;
|
|
1488
1757
|
this.frames = frames;
|
|
1489
1758
|
this.runAs = runAs;
|
|
1490
1759
|
this.reads = reads;
|
|
1760
|
+
this.pending = pending;
|
|
1761
|
+
this.inObjects = inObjects;
|
|
1762
|
+
this.idleHosted = idleHosted;
|
|
1491
1763
|
}
|
|
1492
1764
|
/** The turns picked so far, as a copy. */
|
|
1493
1765
|
get picks() {
|
|
@@ -1498,7 +1770,7 @@ var Turns = class {
|
|
|
1498
1770
|
this.startOrder.set(id, this.startOrder.size);
|
|
1499
1771
|
}
|
|
1500
1772
|
/** Park an invocation at an effect until `takeTurn` releases it with `go`. */
|
|
1501
|
-
park(inv, target, go, host) {
|
|
1773
|
+
park(inv, target, go, host, attempt) {
|
|
1502
1774
|
const option = {
|
|
1503
1775
|
label: `${inv.id}@${inv.parks++}`,
|
|
1504
1776
|
invocation: inv.id,
|
|
@@ -1509,10 +1781,26 @@ var Turns = class {
|
|
|
1509
1781
|
seq: this.parkSeq++,
|
|
1510
1782
|
go,
|
|
1511
1783
|
option,
|
|
1784
|
+
attempt,
|
|
1512
1785
|
...host ? { host } : {}
|
|
1513
1786
|
});
|
|
1514
1787
|
this.rested();
|
|
1515
1788
|
}
|
|
1789
|
+
/**
|
|
1790
|
+
* The hosting effect `host` ended with effects of its hosted code parked:
|
|
1791
|
+
* they are `owner`'s turns now (the host's work after its answer), so the
|
|
1792
|
+
* caller's crash does not drop them and a fault in them is the owner's.
|
|
1793
|
+
*/
|
|
1794
|
+
adopt(host, owner) {
|
|
1795
|
+
this.parked = this.parked.map((p) => p.host === host ? {
|
|
1796
|
+
...p,
|
|
1797
|
+
inv: owner,
|
|
1798
|
+
option: {
|
|
1799
|
+
...p.option,
|
|
1800
|
+
invocation: owner.id
|
|
1801
|
+
}
|
|
1802
|
+
} : p);
|
|
1803
|
+
}
|
|
1516
1804
|
/** See `Run.rested`. */
|
|
1517
1805
|
rested() {
|
|
1518
1806
|
this.rests++;
|
|
@@ -1550,7 +1838,7 @@ var Turns = class {
|
|
|
1550
1838
|
/** See `Run.progressed`. */
|
|
1551
1839
|
progressed() {
|
|
1552
1840
|
this.moves++;
|
|
1553
|
-
if (this.frames.hostingNow > 0) return;
|
|
1841
|
+
if (this.frames.hostingNow > 0 || this.inObjects()) return;
|
|
1554
1842
|
const waiters = this.progressWaiters;
|
|
1555
1843
|
this.progressWaiters = [];
|
|
1556
1844
|
for (const wake of waiters) wake();
|
|
@@ -1559,7 +1847,11 @@ var Turns = class {
|
|
|
1559
1847
|
async quiesce() {
|
|
1560
1848
|
for (;;) {
|
|
1561
1849
|
const moved = await this.wait();
|
|
1562
|
-
if (this.
|
|
1850
|
+
if (this.pending() > 0) continue;
|
|
1851
|
+
if (this.frames.inFlight > 0) {
|
|
1852
|
+
if (!moved && this.idleHosted()) return;
|
|
1853
|
+
continue;
|
|
1854
|
+
}
|
|
1563
1855
|
if (this.rests !== this.restsAtStep) return;
|
|
1564
1856
|
if (!moved) return;
|
|
1565
1857
|
}
|
|
@@ -1593,20 +1885,37 @@ var Turns = class {
|
|
|
1593
1885
|
this.seenMoves = this.moves;
|
|
1594
1886
|
return this.moves !== before;
|
|
1595
1887
|
}
|
|
1596
|
-
/**
|
|
1597
|
-
takeTurn
|
|
1888
|
+
/**
|
|
1889
|
+
* See `Run.takeTurn`. `canWait`: whether, with nothing else to do, the run
|
|
1890
|
+
* could let time pass (a timer is pending or a delayed event is due later).
|
|
1891
|
+
* While every parked turn is late (`RunHooks.attempt`) and nothing else is
|
|
1892
|
+
* on offer, waiting is an option too, so the late code's next move lands
|
|
1893
|
+
* before or after what the time brings; "none" then means the run should
|
|
1894
|
+
* let time pass as when nothing is parked.
|
|
1895
|
+
*/
|
|
1896
|
+
takeTurn(canStart, timer, canWait = false) {
|
|
1598
1897
|
const order = (inv) => this.startOrder.get(inv.id) ?? 0;
|
|
1599
1898
|
this.parked = this.parked.filter((p) => !p.inv.isCrashed()).sort((a, b) => order(a.inv) - order(b.inv) || a.seq - b.seq);
|
|
1600
|
-
const
|
|
1899
|
+
const waiting = canWait && !canStart && !timer && this.waits < 100 && this.parked.length > 0 && this.parked.every((p) => p.attempt?.late === true);
|
|
1900
|
+
const offered = this.turnOptions(canStart, timer, waiting);
|
|
1901
|
+
const options = offered.length;
|
|
1601
1902
|
this.endSegment();
|
|
1602
1903
|
this.turnTarget = null;
|
|
1603
1904
|
this.step();
|
|
1604
1905
|
if (options === 0) return "none";
|
|
1605
|
-
const answer = this.chooser.choose("turn",
|
|
1906
|
+
const answer = this.chooser.choose("turn", offered);
|
|
1606
1907
|
const pick = answer < options ? answer : 0;
|
|
1607
1908
|
if (options > 1) this.picked.push(pick);
|
|
1608
1909
|
const [chosen] = pick < this.parked.length ? this.parked.splice(pick, 1) : [];
|
|
1609
1910
|
if (!chosen) {
|
|
1911
|
+
if (waiting) {
|
|
1912
|
+
this.waits++;
|
|
1913
|
+
return "none";
|
|
1914
|
+
}
|
|
1915
|
+
if (timer && pick === options - 1) {
|
|
1916
|
+
timer.fire();
|
|
1917
|
+
return "timer";
|
|
1918
|
+
}
|
|
1610
1919
|
this.starts++;
|
|
1611
1920
|
return "start";
|
|
1612
1921
|
}
|
|
@@ -1617,9 +1926,26 @@ var Turns = class {
|
|
|
1617
1926
|
chosen.go();
|
|
1618
1927
|
return "turn";
|
|
1619
1928
|
}
|
|
1620
|
-
/**
|
|
1621
|
-
|
|
1622
|
-
|
|
1929
|
+
/**
|
|
1930
|
+
* The turns on offer, as choices: the parked invocations in order, then
|
|
1931
|
+
* starting the next event, then the due timer (last, so a recorded pick
|
|
1932
|
+
* of the others means the same with or without it), or, when every parked
|
|
1933
|
+
* turn is late and nothing else is offered, letting time pass. A timer's
|
|
1934
|
+
* option names its owner and how many of that owner's timers have fired,
|
|
1935
|
+
* and has no target: the callback may do anything, and firing it reads
|
|
1936
|
+
* virtual time, so it commutes with nothing; so does waiting, which fires
|
|
1937
|
+
* the next timer.
|
|
1938
|
+
*/
|
|
1939
|
+
turnOptions(canStart, timer, waiting = false) {
|
|
1940
|
+
return [
|
|
1941
|
+
...this.parked.map((p) => p.option),
|
|
1942
|
+
...canStart ? [{ label: `start:${this.starts}` }] : [],
|
|
1943
|
+
...timer ? [{
|
|
1944
|
+
label: `timer:${timer.owner ?? ""}@${timer.fired}`,
|
|
1945
|
+
...timer.owner !== null ? { invocation: timer.owner } : {}
|
|
1946
|
+
}] : [],
|
|
1947
|
+
...waiting ? [{ label: `wait:${this.waits}` }] : []
|
|
1948
|
+
];
|
|
1623
1949
|
}
|
|
1624
1950
|
};
|
|
1625
1951
|
// packages/engine/src/run.ts
|
|
@@ -1636,6 +1962,8 @@ var Turns = class {
|
|
|
1636
1962
|
* attributed to the invocation whose code made it even when a promise shared
|
|
1637
1963
|
* between invocations resumes one during another's turn.
|
|
1638
1964
|
*/
|
|
1965
|
+
/** The host's own timer, captured before any patching: a host poller's ticks are real. */
|
|
1966
|
+
const realSetTimeout = globalThis.setTimeout.bind(globalThis);
|
|
1639
1967
|
/** Thrown into user code by a synchronous effect that crashes the invocation. */
|
|
1640
1968
|
var CrashSignal = class extends Error {
|
|
1641
1969
|
name = "CrashSignal";
|
|
@@ -1676,14 +2004,17 @@ var InvocationState = class {
|
|
|
1676
2004
|
isCrashed() {
|
|
1677
2005
|
return this.crashedFlag;
|
|
1678
2006
|
}
|
|
1679
|
-
crash()
|
|
2007
|
+
/** `inside`: the effect the crash happened inside of, when one did (a nested effect's fault). */
|
|
2008
|
+
crash(inside) {
|
|
1680
2009
|
if (this.crashedFlag) return;
|
|
1681
2010
|
this.crashedFlag = true;
|
|
1682
|
-
this.onCrash();
|
|
2011
|
+
this.onCrash(inside);
|
|
1683
2012
|
this.resolveCrashed();
|
|
1684
2013
|
}
|
|
1685
2014
|
};
|
|
1686
2015
|
const deliveryScope = asyncScope();
|
|
2016
|
+
const hostScope = asyncScope();
|
|
2017
|
+
const attemptScope = asyncScope();
|
|
1687
2018
|
/** The run whose globals are installed, if any: where code observed from outside records into. */
|
|
1688
2019
|
let installed = null;
|
|
1689
2020
|
function installedRun() {
|
|
@@ -1701,8 +2032,18 @@ function waitingUnseen(invocation) {
|
|
|
1701
2032
|
}
|
|
1702
2033
|
/** Delivered invocations are `<event>#<attempt>`, so this id is never one of theirs. */
|
|
1703
2034
|
const bookkeepingId = "platform";
|
|
2035
|
+
function isThenable(value) {
|
|
2036
|
+
return value !== null && (typeof value === "object" || typeof value === "function") && typeof value.then === "function";
|
|
2037
|
+
}
|
|
1704
2038
|
var Run = class {
|
|
1705
|
-
|
|
2039
|
+
/** The records, appended in place. */
|
|
2040
|
+
entries = [];
|
|
2041
|
+
/**
|
|
2042
|
+
* The log last handed out, a snapshot of the records then: what a run returned never changes
|
|
2043
|
+
* under its reader, however late an abandoned effect's I/O records its result. Rebuilt on the
|
|
2044
|
+
* next read after a record, so a run reads it once at its end and a differential once per event.
|
|
2045
|
+
*/
|
|
2046
|
+
logView = null;
|
|
1706
2047
|
faults = [];
|
|
1707
2048
|
effectIndex = 0;
|
|
1708
2049
|
eligibleSeen = 0;
|
|
@@ -1715,6 +2056,30 @@ var Run = class {
|
|
|
1715
2056
|
* `hosted`.
|
|
1716
2057
|
*/
|
|
1717
2058
|
hosts = /* @__PURE__ */ new Map();
|
|
2059
|
+
/** Per host (`EffectOptions.host`), the invocation its work after an answer runs as; see `continuationOf`. */
|
|
2060
|
+
continuations = /* @__PURE__ */ new Map();
|
|
2061
|
+
/** Per host, how many such invocations it has had: one more after each reset of the object. */
|
|
2062
|
+
continuationCounts = /* @__PURE__ */ new Map();
|
|
2063
|
+
/** Per host, the last of its effects performed: its `onAbandon` resets the object when its later work crashes. */
|
|
2064
|
+
hostFrames = /* @__PURE__ */ new Map();
|
|
2065
|
+
/** Continuations with work that may still move: the run does not end while one is alive. */
|
|
2066
|
+
afterWork = /* @__PURE__ */ new Set();
|
|
2067
|
+
/** Per continuation, timers of its fired since its work last settled (see `afterWorkTimerLimit`). */
|
|
2068
|
+
afterWorkTimers = /* @__PURE__ */ new Map();
|
|
2069
|
+
/** Per owner, timers fired so far (any path): names a timer turn (see `dueTimer`). */
|
|
2070
|
+
timersFired = /* @__PURE__ */ new Map();
|
|
2071
|
+
/** Per host, real work its code started and the run tracks (Web Crypto) still running; see `leftHost`. */
|
|
2072
|
+
hostIo = /* @__PURE__ */ new Map();
|
|
2073
|
+
/**
|
|
2074
|
+
* Hosting effects that ended with effects of their hosted code outstanding
|
|
2075
|
+
* (parked or performing), and the host's work after its answer, which owns
|
|
2076
|
+
* those effects from then on; see `ownerOf`.
|
|
2077
|
+
*/
|
|
2078
|
+
adopted = /* @__PURE__ */ new WeakMap();
|
|
2079
|
+
/** Per host, its poller (concurrent runs; see `pollHost`). */
|
|
2080
|
+
hostPollers = /* @__PURE__ */ new Map();
|
|
2081
|
+
/** Concurrent runs: settles waiting on invocations, released with a `CrashSignal` once theirs crashed; see `settle`. */
|
|
2082
|
+
crashWatch = [];
|
|
1718
2083
|
patched = null;
|
|
1719
2084
|
restoreFetch = null;
|
|
1720
2085
|
/** The invocation whose code is running (one at a time); outbound fetches are attributed to it. */
|
|
@@ -1728,11 +2093,12 @@ var Run = class {
|
|
|
1728
2093
|
options;
|
|
1729
2094
|
constructor(options) {
|
|
1730
2095
|
this.options = options;
|
|
1731
|
-
this.turns = new Turns(options.chooser, this.frames, (id) => this.runAs(id), () => this.patched?.reads);
|
|
2096
|
+
this.turns = new Turns(options.chooser, this.frames, (id) => this.runAs(id), () => this.patched?.reads, () => this.firedPending(), () => this.hasAfterWork(), () => this.timersAllowed() !== null);
|
|
1732
2097
|
this.concurrent = options.bounds.maxConcurrentInvocations > 1;
|
|
1733
2098
|
}
|
|
1734
2099
|
get log() {
|
|
1735
|
-
|
|
2100
|
+
this.logView ??= { entries: [...this.entries] };
|
|
2101
|
+
return this.logView;
|
|
1736
2102
|
}
|
|
1737
2103
|
/** The schedule as actually executed: the faults taken and, when they do not follow the seed, the turns. */
|
|
1738
2104
|
get schedule() {
|
|
@@ -1777,18 +2143,40 @@ var Run = class {
|
|
|
1777
2143
|
if (!this.patched) {
|
|
1778
2144
|
this.patched = patchGlobals(this.options.chooser.seed, { trackIo: (work) => {
|
|
1779
2145
|
this.frames.inFlight++;
|
|
2146
|
+
const host = hostScope?.getStore()?.host;
|
|
2147
|
+
if (host !== void 0) this.hostIo.set(host, (this.hostIo.get(host) ?? 0) + 1);
|
|
1780
2148
|
return work.finally(() => {
|
|
1781
2149
|
this.frames.inFlight--;
|
|
2150
|
+
if (host !== void 0) {
|
|
2151
|
+
const left = (this.hostIo.get(host) ?? 1) - 1;
|
|
2152
|
+
if (left > 0) this.hostIo.set(host, left);
|
|
2153
|
+
else this.hostIo.delete(host);
|
|
2154
|
+
}
|
|
1782
2155
|
if (this.concurrent) this.progressed();
|
|
1783
2156
|
});
|
|
1784
2157
|
} });
|
|
1785
2158
|
const clock = this.patched.clock;
|
|
1786
|
-
clock.ownerNow = () => this.
|
|
2159
|
+
clock.ownerNow = () => this.timerOwner();
|
|
2160
|
+
clock.scopeNow = () => hostScope?.getStore()?.host;
|
|
2161
|
+
clock.afterFire = (owner) => {
|
|
2162
|
+
this.timersFired.set(owner, (this.timersFired.get(owner) ?? 0) + 1);
|
|
2163
|
+
if (owner !== null) this.timerFired(owner);
|
|
2164
|
+
};
|
|
1787
2165
|
clock.bind = (callback) => {
|
|
1788
2166
|
if (!deliveryScope) return callback;
|
|
1789
2167
|
const scope = deliveryScope;
|
|
1790
2168
|
const cell = scope.getStore() ?? { id: this.running() };
|
|
1791
|
-
|
|
2169
|
+
const inHostCtx = hostScope?.getStore();
|
|
2170
|
+
const inHost = (fn) => {
|
|
2171
|
+
if (hostScope && inHostCtx !== void 0) hostScope.run(inHostCtx, fn);
|
|
2172
|
+
else fn();
|
|
2173
|
+
};
|
|
2174
|
+
const inAttemptCell = attemptScope?.getStore();
|
|
2175
|
+
const inAttempt = (fn) => {
|
|
2176
|
+
if (attemptScope && inAttemptCell !== void 0) attemptScope.run(inAttemptCell, fn);
|
|
2177
|
+
else fn();
|
|
2178
|
+
};
|
|
2179
|
+
return (...args) => scope.run(cell, () => inHost(() => inAttempt(() => callback(...args))));
|
|
1792
2180
|
};
|
|
1793
2181
|
}
|
|
1794
2182
|
if (!this.restoreFetch) this.restoreFetch = installScriptedFetch(externals, {
|
|
@@ -1807,10 +2195,6 @@ var Run = class {
|
|
|
1807
2195
|
now() {
|
|
1808
2196
|
return this.patched ? this.patched.clock.now() : Date.now();
|
|
1809
2197
|
}
|
|
1810
|
-
/** Timers scheduled and not yet fired or cancelled (0 when the globals are not installed). */
|
|
1811
|
-
pendingTimers() {
|
|
1812
|
-
return this.patched?.clock.pendingTimers() ?? 0;
|
|
1813
|
-
}
|
|
1814
2198
|
startInvocation(id, options = {}) {
|
|
1815
2199
|
const inv = this.track(id);
|
|
1816
2200
|
if (options.bookkeeping) return inv;
|
|
@@ -1837,8 +2221,282 @@ var Run = class {
|
|
|
1837
2221
|
return queued;
|
|
1838
2222
|
}
|
|
1839
2223
|
running() {
|
|
2224
|
+
const host = this.afterWorkHost();
|
|
2225
|
+
if (host !== void 0) return this.continuationOf(host).id;
|
|
1840
2226
|
return deliveryScope?.getStore()?.id ?? this.current;
|
|
1841
2227
|
}
|
|
2228
|
+
hostedCode(host, code, op) {
|
|
2229
|
+
if (hostScope?.getStore()?.host === host) return code();
|
|
2230
|
+
if (this.concurrent) this.pollHost(host);
|
|
2231
|
+
const current = this.frames.current();
|
|
2232
|
+
const frame = current?.host === host && current.active ? current : this.frames.claimHost(host, op);
|
|
2233
|
+
if (frame) frame.entered = true;
|
|
2234
|
+
const run = () => this.patched ? this.patched.handlerCode(code) : code();
|
|
2235
|
+
const result = hostScope ? hostScope.run({
|
|
2236
|
+
host,
|
|
2237
|
+
frame
|
|
2238
|
+
}, run) : run();
|
|
2239
|
+
if (frame) {
|
|
2240
|
+
const answered = () => this.frames.answered(frame);
|
|
2241
|
+
if (isThenable(result)) Promise.resolve(result).then(answered, answered);
|
|
2242
|
+
else answered();
|
|
2243
|
+
}
|
|
2244
|
+
return result;
|
|
2245
|
+
}
|
|
2246
|
+
/**
|
|
2247
|
+
* Concurrent runs: the host's poller, a real zero timer armed here, inside
|
|
2248
|
+
* the host's code during its request, and re-armed from its own ticks while
|
|
2249
|
+
* the host may still move (an effect of its being performed, work of its
|
|
2250
|
+
* own alive, a wait pending). Each tick wakes the host's waits (`hostTick`):
|
|
2251
|
+
* the host's work after an answer waits for its turns on them, since a
|
|
2252
|
+
* timer that work armed itself, after its request completed, would keep the
|
|
2253
|
+
* object referenced in workerd.
|
|
2254
|
+
*/
|
|
2255
|
+
pollHost(host) {
|
|
2256
|
+
if (this.hostPollers.has(host)) return;
|
|
2257
|
+
const poller = { waiters: [] };
|
|
2258
|
+
this.hostPollers.set(host, poller);
|
|
2259
|
+
const tick = () => {
|
|
2260
|
+
const waiters = poller.waiters;
|
|
2261
|
+
poller.waiters = [];
|
|
2262
|
+
for (const wake of waiters) wake();
|
|
2263
|
+
if (poller.waiters.length > 0 || this.hostMayMove(host)) realSetTimeout(tick, 0);
|
|
2264
|
+
else this.hostPollers.delete(host);
|
|
2265
|
+
};
|
|
2266
|
+
realSetTimeout(tick, 0);
|
|
2267
|
+
}
|
|
2268
|
+
hostMayMove(host) {
|
|
2269
|
+
const cont = this.continuations.get(host);
|
|
2270
|
+
return cont !== void 0 && !cont.isCrashed() && this.afterWork.has(cont) || this.frames.liveHost(host) !== void 0;
|
|
2271
|
+
}
|
|
2272
|
+
/** One tick of the host's poller, from inside the host's code; a real tick where no poller is armed. */
|
|
2273
|
+
hostTick(host) {
|
|
2274
|
+
const poller = this.hostPollers.get(host);
|
|
2275
|
+
if (!poller) return realTick();
|
|
2276
|
+
return new Promise((resolve) => poller.waiters.push(resolve));
|
|
2277
|
+
}
|
|
2278
|
+
/**
|
|
2279
|
+
* The invocation a timer set now belongs to: a host's timers are the
|
|
2280
|
+
* host's, its continuation's, inside a call or after it, so the caller's
|
|
2281
|
+
* crash does not cancel them and a crash of the host's work does.
|
|
2282
|
+
*/
|
|
2283
|
+
timerOwner() {
|
|
2284
|
+
const ctx = hostScope?.getStore();
|
|
2285
|
+
if (ctx !== void 0) return this.continuationOf(ctx.host).id;
|
|
2286
|
+
return this.running();
|
|
2287
|
+
}
|
|
2288
|
+
/**
|
|
2289
|
+
* The host whose code is running after the effect it was entered by has
|
|
2290
|
+
* ended (or, where that effect is not known, while none of its effects is
|
|
2291
|
+
* being performed), if any: work after an answer.
|
|
2292
|
+
*/
|
|
2293
|
+
afterWorkHost() {
|
|
2294
|
+
const ctx = hostScope?.getStore();
|
|
2295
|
+
if (ctx === void 0) return void 0;
|
|
2296
|
+
return (ctx.frame ? ctx.frame.active : this.frames.liveHost(ctx.host) !== void 0) ? void 0 : ctx.host;
|
|
2297
|
+
}
|
|
2298
|
+
/**
|
|
2299
|
+
* The invocation a host's work after an answer runs as, `<host>/after`
|
|
2300
|
+
* (`#<n>` once the object was reset: a new instance's work is a new
|
|
2301
|
+
* invocation's), started on first use and alive until `settleAfterWork`;
|
|
2302
|
+
* work woken later revives it.
|
|
2303
|
+
*/
|
|
2304
|
+
continuationOf(host) {
|
|
2305
|
+
let cont = this.continuations.get(host);
|
|
2306
|
+
if (!cont) {
|
|
2307
|
+
const n = (this.continuationCounts.get(host) ?? 0) + 1;
|
|
2308
|
+
this.continuationCounts.set(host, n);
|
|
2309
|
+
cont = this.track(`${host}/after${n > 1 ? `#${n}` : ""}`);
|
|
2310
|
+
this.continuations.set(host, cont);
|
|
2311
|
+
if (this.concurrent) this.turns.started(cont.id);
|
|
2312
|
+
}
|
|
2313
|
+
if (!cont.isCrashed()) this.afterWork.add(cont);
|
|
2314
|
+
return cont;
|
|
2315
|
+
}
|
|
2316
|
+
hostReset(host) {
|
|
2317
|
+
this.hostAbandoned(host);
|
|
2318
|
+
}
|
|
2319
|
+
/**
|
|
2320
|
+
* The host was reset (`onAbandon` ran: a crash inside its code, an effect
|
|
2321
|
+
* of its that hung; or the platform dropped the instance, `hostReset`): its
|
|
2322
|
+
* work after an answer is gone with the instance, timers included, and its
|
|
2323
|
+
* poller with it.
|
|
2324
|
+
*/
|
|
2325
|
+
hostAbandoned(host) {
|
|
2326
|
+
this.hostPollers.delete(host);
|
|
2327
|
+
const cont = this.continuations.get(host);
|
|
2328
|
+
if (!cont) return;
|
|
2329
|
+
this.continuations.delete(host);
|
|
2330
|
+
this.afterWork.delete(cont);
|
|
2331
|
+
cont.crash();
|
|
2332
|
+
}
|
|
2333
|
+
/**
|
|
2334
|
+
* A host's effect ended: work its code left outstanding (effects parked or
|
|
2335
|
+
* performing, timers pending, real work tracked) is the host's own now,
|
|
2336
|
+
* its continuation's, which owns the outstanding effects from here on
|
|
2337
|
+
* (see `ownerOf`): the caller's crash does not end them, and a fault in
|
|
2338
|
+
* them is the object's.
|
|
2339
|
+
*/
|
|
2340
|
+
leftHost(frame) {
|
|
2341
|
+
if (frame.host === void 0 || frame.isAbandoned) return;
|
|
2342
|
+
this.hostFrames.set(frame.host, frame);
|
|
2343
|
+
const effects = frame.children > 0 || frame.performing > 0;
|
|
2344
|
+
if (!effects && !this.patched?.clock.hasTimerIn(frame.host) && (this.hostIo.get(frame.host) ?? 0) === 0) {
|
|
2345
|
+
const cont = this.continuations.get(frame.host);
|
|
2346
|
+
if (cont) this.afterWorkTimers.delete(cont.id);
|
|
2347
|
+
return;
|
|
2348
|
+
}
|
|
2349
|
+
const cont = this.continuationOf(frame.host);
|
|
2350
|
+
if (!effects) return;
|
|
2351
|
+
this.adopted.set(frame, cont);
|
|
2352
|
+
if (this.concurrent) this.turns.adopt(frame, cont);
|
|
2353
|
+
}
|
|
2354
|
+
/**
|
|
2355
|
+
* Who owns an effect of `inv`'s made inside `enclosing`: `inv`, unless the
|
|
2356
|
+
* enclosing effect was a host's, ended while this one was outstanding and
|
|
2357
|
+
* handed it to the host's work after its answer (`leftHost`). The call's
|
|
2358
|
+
* invocation may be long finished or crashed then; the object goes on.
|
|
2359
|
+
*/
|
|
2360
|
+
ownerOf(inv, enclosing) {
|
|
2361
|
+
const adopted = enclosing && this.adopted.get(enclosing);
|
|
2362
|
+
return adopted && enclosing.invocation === inv.id ? adopted : inv;
|
|
2363
|
+
}
|
|
2364
|
+
/**
|
|
2365
|
+
* A timer of a continuation's fired, by whichever path: its count against
|
|
2366
|
+
* `afterWorkTimerLimit` since its work last settled; past it the object's
|
|
2367
|
+
* work does not settle, so the run is UNSUPPORTED and the work ends (its
|
|
2368
|
+
* timers with it).
|
|
2369
|
+
*/
|
|
2370
|
+
timerFired(owner) {
|
|
2371
|
+
let cont;
|
|
2372
|
+
for (const c of this.continuations.values()) if (c.id === owner) cont = c;
|
|
2373
|
+
if (!cont || cont.isCrashed()) return;
|
|
2374
|
+
const n = (this.afterWorkTimers.get(owner) ?? 0) + 1;
|
|
2375
|
+
this.afterWorkTimers.set(owner, n);
|
|
2376
|
+
if (n <= 100) return;
|
|
2377
|
+
this.unsupported(`${owner}: the object's work after its answer fired its timers ${n} times and does not settle (an interval or a timer that re-arms itself); the model waits for such work only up to 100 firings without it settling`);
|
|
2378
|
+
this.afterWork.delete(cont);
|
|
2379
|
+
cont.crash();
|
|
2380
|
+
}
|
|
2381
|
+
/** A host's timers fired and not yet run by their pollers inside it: work on its way. */
|
|
2382
|
+
firedPending() {
|
|
2383
|
+
return this.patched?.clock.firedPending() ?? 0;
|
|
2384
|
+
}
|
|
2385
|
+
/** Whether an object's work after an answer may still move; the run does not end while it may. */
|
|
2386
|
+
hasAfterWork() {
|
|
2387
|
+
for (const cont of this.afterWork) if (!cont.isCrashed()) return true;
|
|
2388
|
+
return false;
|
|
2389
|
+
}
|
|
2390
|
+
/**
|
|
2391
|
+
* Nothing can move (no effect in flight, no turn to take, no timer due):
|
|
2392
|
+
* every continuation alive has settled, as an object with nothing pending
|
|
2393
|
+
* goes idle. How many did; work of theirs woken later revives them.
|
|
2394
|
+
*/
|
|
2395
|
+
settleAfterWork() {
|
|
2396
|
+
const n = this.afterWork.size;
|
|
2397
|
+
for (const cont of this.afterWork) this.afterWorkTimers.delete(cont.id);
|
|
2398
|
+
this.afterWork.clear();
|
|
2399
|
+
return n;
|
|
2400
|
+
}
|
|
2401
|
+
/**
|
|
2402
|
+
* Sequential runs: between deliveries, let objects' work after their
|
|
2403
|
+
* answers run to rest, as it does in the runtime while nothing else comes
|
|
2404
|
+
* in: real I/O completes, timers fire in due order, and once nothing of it
|
|
2405
|
+
* can move it has settled.
|
|
2406
|
+
*/
|
|
2407
|
+
async driveAfterWork() {
|
|
2408
|
+
let idleBefore = false;
|
|
2409
|
+
while (this.hasAfterWork()) {
|
|
2410
|
+
if (this.firedPending() > 0) {
|
|
2411
|
+
idleBefore = false;
|
|
2412
|
+
await realTick();
|
|
2413
|
+
continue;
|
|
2414
|
+
}
|
|
2415
|
+
if (this.frames.inFlight > 0) {
|
|
2416
|
+
const allowed = this.timersAllowed();
|
|
2417
|
+
if (allowed && idleBefore && this.fireOwned(allowed) !== void 0) {
|
|
2418
|
+
idleBefore = false;
|
|
2419
|
+
await eventLoopTurn();
|
|
2420
|
+
} else {
|
|
2421
|
+
idleBefore = allowed !== null;
|
|
2422
|
+
await realTick();
|
|
2423
|
+
}
|
|
2424
|
+
continue;
|
|
2425
|
+
}
|
|
2426
|
+
idleBefore = false;
|
|
2427
|
+
if (this.fireAfterWorkTimer()) {
|
|
2428
|
+
await eventLoopTurn();
|
|
2429
|
+
continue;
|
|
2430
|
+
}
|
|
2431
|
+
await realTick();
|
|
2432
|
+
if (this.frames.inFlight > 0 || this.firedPending() > 0 || this.fireAfterWorkTimer()) continue;
|
|
2433
|
+
this.settleAfterWork();
|
|
2434
|
+
}
|
|
2435
|
+
}
|
|
2436
|
+
/** Fire the next due timer of an object's work after an answer, as that work. False when none is pending. */
|
|
2437
|
+
fireAfterWorkTimer() {
|
|
2438
|
+
const alive = this.aliveAfterWork();
|
|
2439
|
+
if (alive.size === 0) return false;
|
|
2440
|
+
return this.fireOwned((owner) => owner !== null && alive.has(owner)) !== void 0;
|
|
2441
|
+
}
|
|
2442
|
+
/**
|
|
2443
|
+
* Whose timers may fire now: anyone's while nothing is in flight; while
|
|
2444
|
+
* only hosting effects whose hosted code is idle are, and one of those
|
|
2445
|
+
* hosts has a timer pending (an object awaiting a timer of its own before
|
|
2446
|
+
* it answers, see `Frames.idleHosts`), the timers of those hosts' work;
|
|
2447
|
+
* nobody's otherwise: a fired timer not yet run, or idle hosted code with
|
|
2448
|
+
* no timer, is work on its way that the run cannot see yet (a call
|
|
2449
|
+
* crossing into the object, storage I/O).
|
|
2450
|
+
*/
|
|
2451
|
+
timersAllowed() {
|
|
2452
|
+
const clock = this.patched?.clock;
|
|
2453
|
+
if (!clock || this.firedPending() > 0) return null;
|
|
2454
|
+
if (this.frames.inFlight === 0) return () => true;
|
|
2455
|
+
const hosts = this.frames.idleHosts();
|
|
2456
|
+
if (!hosts) return null;
|
|
2457
|
+
const owners = /* @__PURE__ */ new Set();
|
|
2458
|
+
for (const host of hosts) {
|
|
2459
|
+
const cont = this.continuations.get(host);
|
|
2460
|
+
if (cont) owners.add(cont.id);
|
|
2461
|
+
}
|
|
2462
|
+
const allowed = (owner) => owner !== null && owners.has(owner);
|
|
2463
|
+
return clock.nextDueOwned(allowed, false) === void 0 ? null : allowed;
|
|
2464
|
+
}
|
|
2465
|
+
/**
|
|
2466
|
+
* Fire the earliest due timer whose owner `allowed` accepts, as that owner
|
|
2467
|
+
* (a step of a concurrent run). The owner it fired as (null for none);
|
|
2468
|
+
* undefined when no such timer is pending.
|
|
2469
|
+
*/
|
|
2470
|
+
fireOwned(allowed) {
|
|
2471
|
+
const clock = this.patched?.clock;
|
|
2472
|
+
if (!clock) return void 0;
|
|
2473
|
+
const owner = clock.nextOwnerOwned(allowed);
|
|
2474
|
+
if (owner === void 0) return void 0;
|
|
2475
|
+
this.runAs(owner);
|
|
2476
|
+
if (this.concurrent) this.turns.step();
|
|
2477
|
+
clock.fireNextOwned(allowed);
|
|
2478
|
+
return owner;
|
|
2479
|
+
}
|
|
2480
|
+
/**
|
|
2481
|
+
* Wait until what is running has come to rest: fired timers run and real
|
|
2482
|
+
* I/O done; or, two turns of the event loop running, only hosting effects
|
|
2483
|
+
* whose code is idle in flight (an object awaiting a timer of its own
|
|
2484
|
+
* before it answers), which the next timer to fire is for.
|
|
2485
|
+
*/
|
|
2486
|
+
async rest() {
|
|
2487
|
+
let idleBefore = false;
|
|
2488
|
+
for (;;) {
|
|
2489
|
+
await eventLoopTurn();
|
|
2490
|
+
if (this.firedPending() > 0) {
|
|
2491
|
+
idleBefore = false;
|
|
2492
|
+
continue;
|
|
2493
|
+
}
|
|
2494
|
+
if (this.frames.inFlight === 0) return;
|
|
2495
|
+
const idle = this.timersAllowed() !== null;
|
|
2496
|
+
if (idle && idleBefore) return;
|
|
2497
|
+
idleBefore = idle;
|
|
2498
|
+
}
|
|
2499
|
+
}
|
|
1842
2500
|
runAs(id) {
|
|
1843
2501
|
if (this.patched) this.patched.clock.owner = id;
|
|
1844
2502
|
if (id !== null) this.current = id;
|
|
@@ -1849,10 +2507,30 @@ var Run = class {
|
|
|
1849
2507
|
}
|
|
1850
2508
|
track(id) {
|
|
1851
2509
|
if (this.invocations.has(id)) throw new Error(`invocation ${id} already started`);
|
|
1852
|
-
const inv = new InvocationState(id, () =>
|
|
2510
|
+
const inv = new InvocationState(id, (inside) => {
|
|
2511
|
+
this.patched?.clock.cancelOwner(id);
|
|
2512
|
+
this.adoptInFlight(id, inside);
|
|
2513
|
+
});
|
|
1853
2514
|
this.invocations.set(id, inv);
|
|
1854
2515
|
return inv;
|
|
1855
2516
|
}
|
|
2517
|
+
/**
|
|
2518
|
+
* The invocation crashed with calls into objects still in flight (a late
|
|
2519
|
+
* attempt's call while the retry's effect took the fault): the objects go
|
|
2520
|
+
* on, as the runtime's do, so what their code still does is their own work
|
|
2521
|
+
* (`ownerOf`), an answer to a caller that is gone. The call the crash
|
|
2522
|
+
* happened inside (`inside`) is abandoned by the crash instead and resets
|
|
2523
|
+
* its object.
|
|
2524
|
+
*/
|
|
2525
|
+
adoptInFlight(id, inside) {
|
|
2526
|
+
for (const frame of this.frames.hostingOf(id)) {
|
|
2527
|
+
if (frame === inside || frame.isAbandoned || frame.host === void 0) continue;
|
|
2528
|
+
if (this.adopted.has(frame)) continue;
|
|
2529
|
+
const cont = this.continuationOf(frame.host);
|
|
2530
|
+
this.adopted.set(frame, cont);
|
|
2531
|
+
if (this.concurrent) this.turns.adopt(frame, cont);
|
|
2532
|
+
}
|
|
2533
|
+
}
|
|
1856
2534
|
invocation(id) {
|
|
1857
2535
|
const inv = this.invocations.get(id);
|
|
1858
2536
|
if (!inv) throw new Error(`effect from unknown invocation ${id}`);
|
|
@@ -1879,9 +2557,17 @@ var Run = class {
|
|
|
1879
2557
|
}))]);
|
|
1880
2558
|
return i === 0 ? null : point.allowed[i - 1] ?? null;
|
|
1881
2559
|
}
|
|
2560
|
+
/** Append one record, with the next sequence number: O(1), so a long run's log stays cheap. */
|
|
2561
|
+
append(effect) {
|
|
2562
|
+
this.entries.push({
|
|
2563
|
+
...effect,
|
|
2564
|
+
seq: this.entries.length
|
|
2565
|
+
});
|
|
2566
|
+
this.logView = null;
|
|
2567
|
+
}
|
|
1882
2568
|
record(point, kind, payload) {
|
|
1883
2569
|
if (kind === "result") this.turns.performed(point.target);
|
|
1884
|
-
this.
|
|
2570
|
+
this.append({
|
|
1885
2571
|
kind,
|
|
1886
2572
|
invocation: point.invocation,
|
|
1887
2573
|
target: point.target,
|
|
@@ -1905,77 +2591,111 @@ var Run = class {
|
|
|
1905
2591
|
return fault;
|
|
1906
2592
|
}
|
|
1907
2593
|
async effect(point, perform, summarize, options = {}) {
|
|
2594
|
+
const ctx = hostScope?.getStore();
|
|
2595
|
+
if (ctx !== void 0) {
|
|
2596
|
+
const { host } = ctx;
|
|
2597
|
+
const live = ctx.frame ? ctx.frame.active ? ctx.frame : void 0 : this.frames.liveHost(host);
|
|
2598
|
+
if (live) {
|
|
2599
|
+
if (live.invocation !== point.invocation) return this.effect({
|
|
2600
|
+
...point,
|
|
2601
|
+
invocation: live.invocation
|
|
2602
|
+
}, perform, summarize, options);
|
|
2603
|
+
} else {
|
|
2604
|
+
const cont = this.continuationOf(host);
|
|
2605
|
+
const own = {
|
|
2606
|
+
...point,
|
|
2607
|
+
invocation: cont.id
|
|
2608
|
+
};
|
|
2609
|
+
if (cont.isCrashed()) return hang();
|
|
2610
|
+
const ended = ctx.frame ?? this.hostFrames.get(host);
|
|
2611
|
+
if (this.concurrent && (ended?.mayPark() ?? true)) {
|
|
2612
|
+
let released = false;
|
|
2613
|
+
this.turns.park(cont, own.target, () => {
|
|
2614
|
+
released = true;
|
|
2615
|
+
});
|
|
2616
|
+
while (!released && !cont.isCrashed()) await this.hostTick(host);
|
|
2617
|
+
if (!released) return hang();
|
|
2618
|
+
}
|
|
2619
|
+
return this.performEffect(own, perform, summarize, options, cont, ended);
|
|
2620
|
+
}
|
|
2621
|
+
}
|
|
1908
2622
|
const inv = this.invocation(point.invocation);
|
|
1909
|
-
|
|
1910
|
-
|
|
2623
|
+
const enclosing = this.frames.enclosing(point.invocation, hostScope?.getStore() !== void 0);
|
|
2624
|
+
if (this.ownerOf(inv, enclosing).isCrashed()) return hang();
|
|
1911
2625
|
const nested = enclosing !== void 0;
|
|
1912
2626
|
if (this.concurrent && !nested) {
|
|
1913
2627
|
const host = this.hosts.get(point.invocation);
|
|
1914
|
-
if (host?.active)
|
|
1915
|
-
|
|
1916
|
-
host.children++;
|
|
1917
|
-
host.waitingChildren++;
|
|
1918
|
-
});
|
|
1919
|
-
await new Promise((go) => this.turns.park(inv, point.target, go, host));
|
|
1920
|
-
try {
|
|
1921
|
-
return await this.performEffect(point, perform, summarize, options, inv, void 0, host);
|
|
1922
|
-
} finally {
|
|
1923
|
-
this.frames.adjust(host, () => host.children--);
|
|
1924
|
-
}
|
|
1925
|
-
}
|
|
1926
|
-
await new Promise((go) => this.turns.park(inv, point.target, go));
|
|
2628
|
+
if (host?.active) return this.hostedTurn(point, perform, summarize, options, inv, void 0, host, (park) => new Promise((go) => park(() => go(true))));
|
|
2629
|
+
await new Promise((go) => this.turns.park(inv, point.target, go, void 0, this.attemptNow()));
|
|
1927
2630
|
if (inv.isCrashed()) return hang();
|
|
1928
|
-
} else if (this.concurrent && enclosing?.hosts && enclosing.mayPark()) {
|
|
1929
|
-
const host = enclosing;
|
|
2631
|
+
} else if (this.concurrent && enclosing?.hosts && enclosing.mayPark()) return this.hostedTurn(point, perform, summarize, options, inv, enclosing, enclosing, async (park, crashed) => {
|
|
1930
2632
|
let released = false;
|
|
1931
|
-
|
|
1932
|
-
|
|
1933
|
-
|
|
1934
|
-
|
|
1935
|
-
|
|
1936
|
-
while (!released && !inv.isCrashed()) await realTick();
|
|
1937
|
-
if (!released) {
|
|
1938
|
-
this.frames.adjust(host, () => {
|
|
1939
|
-
host.children--;
|
|
1940
|
-
host.waitingChildren--;
|
|
1941
|
-
});
|
|
1942
|
-
return hang();
|
|
1943
|
-
}
|
|
1944
|
-
try {
|
|
1945
|
-
return await this.performEffect(point, perform, summarize, options, inv, enclosing, host);
|
|
1946
|
-
} finally {
|
|
1947
|
-
this.frames.adjust(host, () => host.children--);
|
|
1948
|
-
}
|
|
1949
|
-
}
|
|
2633
|
+
park(() => released = true);
|
|
2634
|
+
const host = enclosing.host;
|
|
2635
|
+
while (!released && !crashed()) await (host === void 0 ? realTick() : this.hostTick(host));
|
|
2636
|
+
return released;
|
|
2637
|
+
});
|
|
1950
2638
|
return this.performEffect(point, perform, summarize, options, inv, enclosing);
|
|
1951
2639
|
}
|
|
2640
|
+
/**
|
|
2641
|
+
* An effect that is a turn under `host` (the effect hosting the invocation, or the hosted code's
|
|
2642
|
+
* own enclosing effect): the host waits on it until `wait` gives it the turn, and is in flight
|
|
2643
|
+
* again while it performs. `wait` parks the effect through `park` and answers whether the turn
|
|
2644
|
+
* came; when it did not (the invocation crashed meanwhile) the effect hangs.
|
|
2645
|
+
*/
|
|
2646
|
+
async hostedTurn(point, perform, summarize, options, inv, enclosing, host, wait) {
|
|
2647
|
+
this.childWaits(host);
|
|
2648
|
+
const owner = this.ownerOf(inv, enclosing);
|
|
2649
|
+
const crashed = () => this.ownerOf(inv, enclosing).isCrashed();
|
|
2650
|
+
const attempt = this.attemptNow(enclosing, host);
|
|
2651
|
+
if (!await wait((go) => this.turns.park(owner, point.target, go, host, attempt), crashed)) {
|
|
2652
|
+
this.childLeaves(host, true);
|
|
2653
|
+
return hang();
|
|
2654
|
+
}
|
|
2655
|
+
try {
|
|
2656
|
+
return await this.performEffect(point, perform, summarize, options, inv, enclosing, host);
|
|
2657
|
+
} finally {
|
|
2658
|
+
this.childLeaves(host, false);
|
|
2659
|
+
}
|
|
2660
|
+
}
|
|
2661
|
+
/** A child of `host` started and waits for its turn (the scheduler clears the wait when it comes). */
|
|
2662
|
+
childWaits(host) {
|
|
2663
|
+
this.frames.adjust(host, () => {
|
|
2664
|
+
host.children++;
|
|
2665
|
+
host.waitingChildren++;
|
|
2666
|
+
});
|
|
2667
|
+
}
|
|
2668
|
+
/** A child of `host` ended; `stillWaiting` when its turn never came. */
|
|
2669
|
+
childLeaves(host, stillWaiting) {
|
|
2670
|
+
this.frames.adjust(host, () => {
|
|
2671
|
+
host.children--;
|
|
2672
|
+
if (stillWaiting) host.waitingChildren--;
|
|
2673
|
+
});
|
|
2674
|
+
}
|
|
1952
2675
|
/** The effect once it has its turn: the fault, the perform under its deadline, the record. */
|
|
1953
|
-
async performEffect(
|
|
2676
|
+
async performEffect(made, perform, summarize, options, inv, enclosing, host) {
|
|
1954
2677
|
const dead = () => {
|
|
1955
2678
|
enclosing?.crash();
|
|
1956
2679
|
return hang();
|
|
1957
2680
|
};
|
|
1958
|
-
const
|
|
1959
|
-
|
|
1960
|
-
|
|
1961
|
-
|
|
1962
|
-
|
|
1963
|
-
|
|
1964
|
-
|
|
1965
|
-
|
|
1966
|
-
|
|
1967
|
-
if (
|
|
1968
|
-
this.record(point, "fault", {
|
|
1969
|
-
args: point.args,
|
|
1970
|
-
fault
|
|
1971
|
-
});
|
|
1972
|
-
throw new InjectedEffectError(point, fault);
|
|
1973
|
-
}
|
|
2681
|
+
const owner = () => this.ownerOf(inv, enclosing);
|
|
2682
|
+
const opener = owner();
|
|
2683
|
+
const point = opener === inv ? made : {
|
|
2684
|
+
...made,
|
|
2685
|
+
invocation: opener.id
|
|
2686
|
+
};
|
|
2687
|
+
const fault = this.open(point, opener);
|
|
2688
|
+
const before = this.faultBefore(point, opener, fault, enclosing);
|
|
2689
|
+
if (before === "crashed") return dead();
|
|
2690
|
+
if (before) throw before;
|
|
1974
2691
|
let settled;
|
|
1975
2692
|
const deadline = realDeadline(this.options.bounds.budgetMs);
|
|
1976
|
-
const frame = new Frame(point
|
|
2693
|
+
const frame = new Frame(point, options);
|
|
2694
|
+
frame.onHostAbandoned = (h) => this.hostAbandoned(h);
|
|
1977
2695
|
frame.parent = host;
|
|
2696
|
+
frame.attempt = this.attemptNow(enclosing, host);
|
|
1978
2697
|
this.frames.enter(frame);
|
|
2698
|
+
if (enclosing) enclosing.performing++;
|
|
1979
2699
|
try {
|
|
1980
2700
|
const inside = () => effectScope ? effectScope.run(frame, perform) : perform();
|
|
1981
2701
|
const io = this.patched ? this.patched.libraryIo(inside) : inside();
|
|
@@ -1997,13 +2717,15 @@ var Run = class {
|
|
|
1997
2717
|
};
|
|
1998
2718
|
} finally {
|
|
1999
2719
|
deadline.cancel();
|
|
2720
|
+
if (enclosing) enclosing.performing--;
|
|
2000
2721
|
this.frames.leave(frame);
|
|
2722
|
+
this.leftHost(frame);
|
|
2001
2723
|
this.patched?.clock.advance(1);
|
|
2002
2724
|
if (this.concurrent) this.progressed();
|
|
2003
2725
|
}
|
|
2004
2726
|
if (settled === "expired") {
|
|
2005
2727
|
this.unsupported(`${point.target} ${point.op} did not complete within ${this.options.bounds.budgetMs} ms of real time`);
|
|
2006
|
-
|
|
2728
|
+
owner().crash();
|
|
2007
2729
|
frame.abandon();
|
|
2008
2730
|
return hang();
|
|
2009
2731
|
}
|
|
@@ -2021,43 +2743,57 @@ var Run = class {
|
|
|
2021
2743
|
args: point.args,
|
|
2022
2744
|
result: summarize(value)
|
|
2023
2745
|
});
|
|
2024
|
-
if (
|
|
2746
|
+
if (owner().isCrashed()) return hang();
|
|
2747
|
+
const after = this.faultAfter(point, owner(), fault, enclosing);
|
|
2748
|
+
if (after === "crashed") return dead();
|
|
2749
|
+
if (after) throw after;
|
|
2750
|
+
return value;
|
|
2751
|
+
}
|
|
2752
|
+
/** A fault decided before the effect performs: the crash it is, the error to throw, or nothing. */
|
|
2753
|
+
/**
|
|
2754
|
+
* A fault decided before the effect performs: the crash it is, the error to
|
|
2755
|
+
* throw, or nothing. The crash names the effect the point is nested in
|
|
2756
|
+
* (an object's call the crash happened inside, which `dead` then abandons,
|
|
2757
|
+
* resetting the object), so it is not handed to the object (`adoptInFlight`).
|
|
2758
|
+
*/
|
|
2759
|
+
faultBefore(point, inv, fault, enclosing) {
|
|
2760
|
+
if (fault === "crash_before" || fault === "cut_off") {
|
|
2761
|
+
this.recordFault(point, fault);
|
|
2762
|
+
inv.crash(enclosing);
|
|
2763
|
+
return "crashed";
|
|
2764
|
+
}
|
|
2765
|
+
if (fault === "error") {
|
|
2766
|
+
this.recordFault(point, fault);
|
|
2767
|
+
return new InjectedEffectError(point, fault);
|
|
2768
|
+
}
|
|
2769
|
+
return null;
|
|
2770
|
+
}
|
|
2771
|
+
/** A fault decided after the effect committed: the crash it is, the error to throw, or nothing. */
|
|
2772
|
+
faultAfter(point, inv, fault, enclosing) {
|
|
2025
2773
|
if (fault === "crash_after") {
|
|
2026
|
-
this.
|
|
2027
|
-
|
|
2028
|
-
|
|
2029
|
-
});
|
|
2030
|
-
inv.crash();
|
|
2031
|
-
return dead();
|
|
2774
|
+
this.recordFault(point, fault);
|
|
2775
|
+
inv.crash(enclosing);
|
|
2776
|
+
return "crashed";
|
|
2032
2777
|
}
|
|
2033
2778
|
if (fault === "ambiguous") {
|
|
2034
|
-
this.
|
|
2035
|
-
|
|
2036
|
-
fault
|
|
2037
|
-
});
|
|
2038
|
-
throw new InjectedEffectError(point, fault);
|
|
2779
|
+
this.recordFault(point, fault);
|
|
2780
|
+
return new InjectedEffectError(point, fault);
|
|
2039
2781
|
}
|
|
2040
|
-
return
|
|
2782
|
+
return null;
|
|
2783
|
+
}
|
|
2784
|
+
recordFault(point, fault) {
|
|
2785
|
+
this.record(point, "fault", {
|
|
2786
|
+
args: point.args,
|
|
2787
|
+
fault
|
|
2788
|
+
});
|
|
2041
2789
|
}
|
|
2042
2790
|
effectSync(point, perform, summarize) {
|
|
2043
2791
|
const inv = this.invocation(point.invocation);
|
|
2044
2792
|
if (inv.isCrashed()) throw new CrashSignal(inv.id);
|
|
2045
2793
|
const fault = this.open(point, inv);
|
|
2046
|
-
|
|
2047
|
-
|
|
2048
|
-
|
|
2049
|
-
fault
|
|
2050
|
-
});
|
|
2051
|
-
inv.crash();
|
|
2052
|
-
throw new CrashSignal(inv.id);
|
|
2053
|
-
}
|
|
2054
|
-
if (fault === "error") {
|
|
2055
|
-
this.record(point, "fault", {
|
|
2056
|
-
args: point.args,
|
|
2057
|
-
fault
|
|
2058
|
-
});
|
|
2059
|
-
throw new InjectedEffectError(point, fault);
|
|
2060
|
-
}
|
|
2794
|
+
const before = this.faultBefore(point, inv, fault);
|
|
2795
|
+
if (before === "crashed") throw new CrashSignal(inv.id);
|
|
2796
|
+
if (before) throw before;
|
|
2061
2797
|
let value;
|
|
2062
2798
|
try {
|
|
2063
2799
|
value = perform();
|
|
@@ -2073,39 +2809,64 @@ var Run = class {
|
|
|
2073
2809
|
args: point.args,
|
|
2074
2810
|
result: summarize(value)
|
|
2075
2811
|
});
|
|
2076
|
-
|
|
2077
|
-
|
|
2078
|
-
|
|
2079
|
-
fault
|
|
2080
|
-
});
|
|
2081
|
-
inv.crash();
|
|
2082
|
-
throw new CrashSignal(inv.id);
|
|
2083
|
-
}
|
|
2084
|
-
if (fault === "ambiguous") {
|
|
2085
|
-
this.record(point, "fault", {
|
|
2086
|
-
args: point.args,
|
|
2087
|
-
fault
|
|
2088
|
-
});
|
|
2089
|
-
throw new InjectedEffectError(point, fault);
|
|
2090
|
-
}
|
|
2812
|
+
const after = this.faultAfter(point, inv, fault);
|
|
2813
|
+
if (after === "crashed") throw new CrashSignal(inv.id);
|
|
2814
|
+
if (after) throw after;
|
|
2091
2815
|
return value;
|
|
2092
2816
|
}
|
|
2093
2817
|
unsupported(reason) {
|
|
2094
2818
|
this.reasons.push(reason);
|
|
2095
2819
|
}
|
|
2820
|
+
attempt(invocation, code, deadlineMs) {
|
|
2821
|
+
const cell = { late: deadlineMs <= 0 };
|
|
2822
|
+
let giveUp = () => {};
|
|
2823
|
+
const late = new Promise((resolve) => {
|
|
2824
|
+
giveUp = () => {
|
|
2825
|
+
cell.late = true;
|
|
2826
|
+
resolve();
|
|
2827
|
+
};
|
|
2828
|
+
});
|
|
2829
|
+
if (cell.late) giveUp();
|
|
2830
|
+
const value = attemptScope ? attemptScope.run(cell, code) : code();
|
|
2831
|
+
const clock = this.patched?.clock;
|
|
2832
|
+
if (!cell.late && clock) {
|
|
2833
|
+
const timer = clock.setTimeoutAs(invocation, giveUp, deadlineMs);
|
|
2834
|
+
const clear = () => clock.clear(timer);
|
|
2835
|
+
if (isThenable(value)) Promise.resolve(value).then(clear, clear);
|
|
2836
|
+
else clear();
|
|
2837
|
+
}
|
|
2838
|
+
return {
|
|
2839
|
+
value,
|
|
2840
|
+
late
|
|
2841
|
+
};
|
|
2842
|
+
}
|
|
2843
|
+
/**
|
|
2844
|
+
* The attempt whose code is running now, by its async context or, where the
|
|
2845
|
+
* runtime carries none (an object's code), by the effect it runs inside of.
|
|
2846
|
+
* Read live where it is kept: an attempt may become late after its effect
|
|
2847
|
+
* parked or entered an object.
|
|
2848
|
+
*/
|
|
2849
|
+
attemptNow(...frames) {
|
|
2850
|
+
return attemptScope?.getStore() ?? frames.find((f) => f?.attempt)?.attempt;
|
|
2851
|
+
}
|
|
2096
2852
|
settle(promise, invocation) {
|
|
2097
2853
|
const inv = this.invocation(invocation);
|
|
2098
|
-
if (this.concurrent) return Promise.race([promise,
|
|
2854
|
+
if (this.concurrent) return Promise.race([promise, new Promise((_, reject) => {
|
|
2855
|
+
if (inv.isCrashed()) reject(new CrashSignal(inv.id));
|
|
2856
|
+
else this.crashWatch.push({
|
|
2857
|
+
inv,
|
|
2858
|
+
reject
|
|
2859
|
+
});
|
|
2860
|
+
})]);
|
|
2099
2861
|
const patched = this.patched;
|
|
2100
|
-
if (!this.hosts.has(invocation))
|
|
2101
|
-
if (patched) patched.clock.owner = invocation;
|
|
2102
|
-
this.current = invocation;
|
|
2103
|
-
}
|
|
2862
|
+
if (!this.hosts.has(invocation)) this.runAs(invocation);
|
|
2104
2863
|
if (!patched) return Promise.race([promise, inv.crashed.then(() => hang())]);
|
|
2105
2864
|
return settleOn(promise, {
|
|
2106
2865
|
clock: patched.clock,
|
|
2107
|
-
inFlight: () => this.frames.inFlight + this.hostedBy(invocation),
|
|
2866
|
+
inFlight: () => this.frames.inFlight + this.hostedBy(invocation) + this.firedPending(),
|
|
2867
|
+
idle: () => this.hostedBy(invocation) > 0 ? null : this.timersAllowed(),
|
|
2108
2868
|
crashed: () => inv.isCrashed(),
|
|
2869
|
+
crashError: () => new CrashSignal(invocation),
|
|
2109
2870
|
stuck: () => {
|
|
2110
2871
|
this.unsupported(waitingUnseen(invocation));
|
|
2111
2872
|
inv.crash();
|
|
@@ -2130,22 +2891,17 @@ var Run = class {
|
|
|
2130
2891
|
const inv = this.track(id);
|
|
2131
2892
|
if (this.concurrent) this.turns.started(id);
|
|
2132
2893
|
this.hosts.set(id, frame);
|
|
2133
|
-
this.
|
|
2134
|
-
frame.children++;
|
|
2135
|
-
frame.waitingChildren++;
|
|
2136
|
-
});
|
|
2894
|
+
this.childWaits(frame);
|
|
2137
2895
|
try {
|
|
2138
2896
|
return await work(inv);
|
|
2139
2897
|
} finally {
|
|
2140
|
-
this.
|
|
2141
|
-
frame.children--;
|
|
2142
|
-
frame.waitingChildren--;
|
|
2143
|
-
});
|
|
2898
|
+
this.childLeaves(frame, true);
|
|
2144
2899
|
this.hosts.delete(id);
|
|
2145
2900
|
}
|
|
2146
2901
|
};
|
|
2147
2902
|
const asHandler = () => this.patched ? this.patched.handlerCode(run) : run();
|
|
2148
|
-
|
|
2903
|
+
const unhosted = () => hostScope ? hostScope.run(void 0, asHandler) : asHandler();
|
|
2904
|
+
return deliveryScope.run({ id }, unhosted);
|
|
2149
2905
|
}
|
|
2150
2906
|
/** How many invocations are hosted inside effects of `invocation` now. */
|
|
2151
2907
|
hostedBy(invocation) {
|
|
@@ -2183,16 +2939,57 @@ var Run = class {
|
|
|
2183
2939
|
* path gives, before it counts as idle.
|
|
2184
2940
|
*/
|
|
2185
2941
|
quiesce() {
|
|
2942
|
+
this.releaseCrashed();
|
|
2186
2943
|
return this.turns.quiesce();
|
|
2187
2944
|
}
|
|
2188
2945
|
/**
|
|
2189
|
-
* Concurrent runs:
|
|
2190
|
-
*
|
|
2191
|
-
* by start, then by when they parked, so the pick does not depend on
|
|
2192
|
-
* real-time arrival. Returns what was picked; "turn" has already released it.
|
|
2946
|
+
* Concurrent runs: settles waiting on invocations that have crashed since
|
|
2947
|
+
* are released, on the run's own step. Returns the invocations released.
|
|
2193
2948
|
*/
|
|
2194
|
-
|
|
2195
|
-
|
|
2949
|
+
releaseCrashed() {
|
|
2950
|
+
const released = /* @__PURE__ */ new Set();
|
|
2951
|
+
if (this.crashWatch.length === 0) return released;
|
|
2952
|
+
const waiting = this.crashWatch;
|
|
2953
|
+
this.crashWatch = [];
|
|
2954
|
+
for (const w of waiting) if (w.inv.isCrashed()) {
|
|
2955
|
+
w.reject(new CrashSignal(w.inv.id));
|
|
2956
|
+
released.add(w.inv.id);
|
|
2957
|
+
} else this.crashWatch.push(w);
|
|
2958
|
+
return released;
|
|
2959
|
+
}
|
|
2960
|
+
/**
|
|
2961
|
+
* Concurrent runs: pick the next turn among the parked invocations, when
|
|
2962
|
+
* `canStart` starting the next event, and the earliest virtual timer
|
|
2963
|
+
* already due that may fire now (`timersAllowed`; with `afterWorkOnly`,
|
|
2964
|
+
* only an object's work after its answer's, as `fireAfterWorkTimer`).
|
|
2965
|
+
* Parked invocations are ordered by start, then by when they parked, so
|
|
2966
|
+
* the pick does not depend on real-time arrival; timers keep their due
|
|
2967
|
+
* order among themselves. `canWait`: whether the loop could let time pass
|
|
2968
|
+
* if nothing were parked; while only late turns are, it is an option
|
|
2969
|
+
* (`Turns.takeTurn`). Returns what was picked; "turn" has already
|
|
2970
|
+
* released it, "timer" fired it, and "none" leaves the loop to let time
|
|
2971
|
+
* pass or give up.
|
|
2972
|
+
*/
|
|
2973
|
+
takeTurn(canStart, afterWorkOnly = false, canWait = false) {
|
|
2974
|
+
return this.turns.takeTurn(canStart, this.dueTimer(afterWorkOnly), canWait);
|
|
2975
|
+
}
|
|
2976
|
+
/** The earliest timer already due (at or before the virtual now) that may fire now, as a turn. */
|
|
2977
|
+
dueTimer(afterWorkOnly) {
|
|
2978
|
+
const clock = this.patched?.clock;
|
|
2979
|
+
const allowed = this.timersAllowed();
|
|
2980
|
+
if (!clock || !allowed) return void 0;
|
|
2981
|
+
const alive = afterWorkOnly ? this.aliveAfterWork() : null;
|
|
2982
|
+
const owned = alive ? (owner) => owner !== null && alive.has(owner) && allowed(owner) : allowed;
|
|
2983
|
+
const due = clock.nextDueOwned(owned);
|
|
2984
|
+
if (due === void 0 || due > clock.now()) return void 0;
|
|
2985
|
+
const owner = clock.nextOwnerOwned(owned) ?? null;
|
|
2986
|
+
return {
|
|
2987
|
+
owner,
|
|
2988
|
+
fired: this.timersFired.get(owner) ?? 0,
|
|
2989
|
+
fire: () => {
|
|
2990
|
+
this.fireOwned(owned);
|
|
2991
|
+
}
|
|
2992
|
+
};
|
|
2196
2993
|
}
|
|
2197
2994
|
/**
|
|
2198
2995
|
* Let virtual time pass until `at`: timers due by then fire first, in due
|
|
@@ -2203,41 +3000,63 @@ var Run = class {
|
|
|
2203
3000
|
const clock = this.patched?.clock;
|
|
2204
3001
|
if (!clock) return false;
|
|
2205
3002
|
let fired = false;
|
|
2206
|
-
for (
|
|
2207
|
-
this.
|
|
3003
|
+
for (;;) {
|
|
3004
|
+
const allowed = this.timersAllowed();
|
|
3005
|
+
const due = allowed ? clock.nextDueOwned(allowed) : void 0;
|
|
3006
|
+
if (!allowed || due === void 0 || due > at) break;
|
|
3007
|
+
this.fireOwned(allowed);
|
|
2208
3008
|
fired = true;
|
|
2209
|
-
|
|
2210
|
-
await eventLoopTurn();
|
|
2211
|
-
while (this.frames.inFlight > 0);
|
|
3009
|
+
await this.rest();
|
|
2212
3010
|
}
|
|
2213
3011
|
clock.advance(at - clock.now());
|
|
2214
3012
|
return fired;
|
|
2215
3013
|
}
|
|
2216
|
-
/**
|
|
2217
|
-
|
|
2218
|
-
|
|
2219
|
-
|
|
2220
|
-
|
|
2221
|
-
|
|
3014
|
+
/**
|
|
3015
|
+
* When the next virtual timer is due, or undefined when none is pending;
|
|
3016
|
+
* `afterWorkOnly` counts the timers of objects' work after answers alone,
|
|
3017
|
+
* the ones that fire while only such work is alive.
|
|
3018
|
+
*/
|
|
3019
|
+
nextTimerDue(afterWorkOnly = false) {
|
|
2222
3020
|
const clock = this.patched?.clock;
|
|
2223
|
-
if (!clock
|
|
2224
|
-
|
|
3021
|
+
if (!clock) return void 0;
|
|
3022
|
+
if (!afterWorkOnly) {
|
|
3023
|
+
const allowed = this.timersAllowed();
|
|
3024
|
+
return allowed ? clock.nextDueOwned(allowed) : void 0;
|
|
3025
|
+
}
|
|
3026
|
+
const alive = this.aliveAfterWork();
|
|
3027
|
+
return clock.nextDueOwned((owner) => owner !== null && alive.has(owner));
|
|
2225
3028
|
}
|
|
2226
|
-
/**
|
|
2227
|
-
|
|
2228
|
-
|
|
2229
|
-
|
|
2230
|
-
return
|
|
3029
|
+
/** The ids of continuations alive now. */
|
|
3030
|
+
aliveAfterWork() {
|
|
3031
|
+
const alive = /* @__PURE__ */ new Set();
|
|
3032
|
+
for (const cont of this.afterWork) if (!cont.isCrashed()) alive.add(cont.id);
|
|
3033
|
+
return alive;
|
|
3034
|
+
}
|
|
3035
|
+
/**
|
|
3036
|
+
* Concurrent runs: fire the next due timer that may fire now
|
|
3037
|
+
* (`timersAllowed`) as the invocation that set it. False when none is.
|
|
3038
|
+
*/
|
|
3039
|
+
fireTimer() {
|
|
3040
|
+
const allowed = this.timersAllowed();
|
|
3041
|
+
return allowed !== null && this.fireOwned(allowed) !== void 0;
|
|
2231
3042
|
}
|
|
2232
3043
|
/**
|
|
2233
3044
|
* Concurrent runs: nothing can make progress; abandon these invocations so
|
|
2234
|
-
* the run can end. Returns how many were still alive
|
|
3045
|
+
* the run can end. Returns how many were still alive, or whose delivery
|
|
3046
|
+
* ends now: one that crashed inside an object (the object's reset tears
|
|
3047
|
+
* down the task the crash settled its promise in, so the delivery never
|
|
3048
|
+
* saw it) has its settle released here, on the run's own step.
|
|
2235
3049
|
*/
|
|
2236
3050
|
abandon(ids) {
|
|
2237
3051
|
let abandoned = 0;
|
|
3052
|
+
const released = this.releaseCrashed();
|
|
2238
3053
|
for (const id of ids) {
|
|
2239
3054
|
const inv = this.invocations.get(id);
|
|
2240
|
-
if (!inv
|
|
3055
|
+
if (!inv) continue;
|
|
3056
|
+
if (inv.isCrashed()) {
|
|
3057
|
+
if (released.has(id)) abandoned++;
|
|
3058
|
+
continue;
|
|
3059
|
+
}
|
|
2241
3060
|
this.unsupported(waitingUnseen(id));
|
|
2242
3061
|
inv.crash();
|
|
2243
3062
|
abandoned++;
|
|
@@ -2246,7 +3065,7 @@ var Run = class {
|
|
|
2246
3065
|
}
|
|
2247
3066
|
/** A call of a pure core function, recorded as a decision of the invocation running now (see `observeCore`). */
|
|
2248
3067
|
decision(fn, payload) {
|
|
2249
|
-
this.
|
|
3068
|
+
this.append({
|
|
2250
3069
|
kind: "result",
|
|
2251
3070
|
invocation: this.running(),
|
|
2252
3071
|
target: decisionTarget(fn),
|
|
@@ -2255,7 +3074,7 @@ var Run = class {
|
|
|
2255
3074
|
});
|
|
2256
3075
|
}
|
|
2257
3076
|
snapshot(invocation, target, payload) {
|
|
2258
|
-
this.
|
|
3077
|
+
this.append({
|
|
2259
3078
|
kind: "snapshot",
|
|
2260
3079
|
invocation,
|
|
2261
3080
|
target,
|
|
@@ -2269,18 +3088,22 @@ var Run = class {
|
|
|
2269
3088
|
* Run user code as an invocation: under `RunHooks.settle` (real I/O
|
|
2270
3089
|
* completes, timers fire while it is idle) and ended as "crashed" when an
|
|
2271
3090
|
* injected fault crashes the invocation, whether the code saw the crash as
|
|
2272
|
-
* a `CrashSignal
|
|
2273
|
-
*
|
|
3091
|
+
* a `CrashSignal`, an effect of its hung (the settle then rejects with the
|
|
3092
|
+
* signal, on the run's own tick), the invocation's `crashed` settled, or
|
|
3093
|
+
* the code returned or threw anything else after the crash (an object's
|
|
3094
|
+
* reset rejecting the call into it with an error of the runtime's); as
|
|
3095
|
+
* "errored" when it threw anything else.
|
|
2274
3096
|
*/
|
|
2275
3097
|
async function runInvocation(hooks, inv, work) {
|
|
2276
|
-
const
|
|
3098
|
+
const crashed = { status: "crashed" };
|
|
3099
|
+
const outcome = Promise.resolve().then(work).then((value) => inv.isCrashed() ? crashed : {
|
|
2277
3100
|
status: "completed",
|
|
2278
3101
|
value
|
|
2279
|
-
}
|
|
3102
|
+
}, (error) => error instanceof CrashSignal || inv.isCrashed() ? crashed : {
|
|
2280
3103
|
status: "errored",
|
|
2281
3104
|
error
|
|
2282
3105
|
});
|
|
2283
|
-
return Promise.race([hooks.settle(outcome, inv.id), inv.crashed.then(() =>
|
|
3106
|
+
return Promise.race([hooks.settle(outcome, inv.id).catch((error) => error instanceof CrashSignal ? crashed : Promise.reject(error)), inv.crashed.then(() => crashed)]);
|
|
2284
3107
|
}
|
|
2285
3108
|
/**
|
|
2286
3109
|
* Run user code as an invocation of its own inside the effect being performed
|
|
@@ -2297,6 +3120,28 @@ function hostInvocation(hooks, id, work) {
|
|
|
2297
3120
|
if (!(hooks instanceof Run)) throw new Error("hostInvocation needs the engine's run");
|
|
2298
3121
|
return hooks.hosted(id, work);
|
|
2299
3122
|
}
|
|
3123
|
+
/**
|
|
3124
|
+
* Record a platform action that performs nothing: a marker in the log (a
|
|
3125
|
+
* message dead-lettered, a caller that gave up, a Workflow step's result).
|
|
3126
|
+
* Under faults it can crash the invocation like any effect; most admit none.
|
|
3127
|
+
*/
|
|
3128
|
+
function note(hooks, point) {
|
|
3129
|
+
hooks.effectSync(point, () => null, () => null);
|
|
3130
|
+
}
|
|
3131
|
+
/**
|
|
3132
|
+
* Perform a decision of the platform's own (an implicit ack, a delete)
|
|
3133
|
+
* under the faults around a decision: it stands, or an injected crash
|
|
3134
|
+
* ended the invocation at that point.
|
|
3135
|
+
*/
|
|
3136
|
+
function decide(hooks, point, perform, summarize) {
|
|
3137
|
+
try {
|
|
3138
|
+
hooks.effectSync(point, perform, summarize);
|
|
3139
|
+
return "decided";
|
|
3140
|
+
} catch (err) {
|
|
3141
|
+
if (err instanceof CrashSignal) return "crashed";
|
|
3142
|
+
throw err;
|
|
3143
|
+
}
|
|
3144
|
+
}
|
|
2300
3145
|
// packages/engine/src/exhaustive.ts
|
|
2301
3146
|
/**
|
|
2302
3147
|
* Exhaustive exploration: every schedule within the bounds, each once. A
|
|
@@ -2310,8 +3155,9 @@ function hostInvocation(hooks, id, work) {
|
|
|
2310
3155
|
* Reduction, when on, uses sleep sets (Godefroid, Partial-Order Methods for
|
|
2311
3156
|
* the Verification of Concurrent Systems, 1996): two turns of different
|
|
2312
3157
|
* invocations whose effects have different targets commute, so after one
|
|
2313
|
-
* order of them has been explored the other is not. Starting an event
|
|
2314
|
-
*
|
|
3158
|
+
* order of them has been explored the other is not. Starting an event and
|
|
3159
|
+
* firing a timer already due are dependent on everything (their options
|
|
3160
|
+
* have no target), so what they run is never reordered. A run whose every option is asleep repeats an
|
|
2315
3161
|
* explored order; it still runs (the choices are forced up to it) but is not
|
|
2316
3162
|
* counted as a schedule and nothing below it is explored.
|
|
2317
3163
|
*/
|
|
@@ -2532,7 +3378,7 @@ const engineAssumptions = [
|
|
|
2532
3378
|
"schedule points are effect boundaries: proxied bindings, patched timers and fetch; other awaits run in program order",
|
|
2533
3379
|
"faults: crash_before, crash_after, ambiguous, error at any effect point; redelivery per the platform's queue model",
|
|
2534
3380
|
"Date, Math.random, crypto.randomUUID, crypto.getRandomValues, performance.now and timers are virtualised per run from the seed; time starts at 2026-01-01T00:00:00Z and advances 1 ms per effect and to each timer's due time",
|
|
2535
|
-
"timers fire only while the invocation is idle (no effect in flight)
|
|
3381
|
+
"timers fire in due order; one at a time, only while the invocation is idle (no effect in flight); with several in flight, as the concurrency assumption says; an invocation waiting on something outside Eutaxy makes the run UNSUPPORTED",
|
|
2536
3382
|
"an effect whose real I/O takes longer than the time budget (the scenario's budgetMs, or the budgetMs option replacing it) is treated as hung: the run is UNSUPPORTED and names the effect",
|
|
2537
3383
|
"while an effect performs real I/O, timers the library schedules inside that I/O are real (backoff, timeouts) and timers the handler schedules meanwhile stay virtual, told apart by async context; without async context support every timer stays virtual",
|
|
2538
3384
|
"module-level state cannot be reset between runs; before BOUNDED the first runs are replayed in reverse order and any difference makes the run UNSUPPORTED (module_state_leak)",
|
|
@@ -2541,34 +3387,48 @@ const engineAssumptions = [
|
|
|
2541
3387
|
/** What a verdict rests on about concurrency, for these bounds. */
|
|
2542
3388
|
function concurrencyAssumption(bounds) {
|
|
2543
3389
|
if (bounds.maxConcurrentInvocations === 1) return "one invocation at a time (maxConcurrentInvocations = 1)";
|
|
2544
|
-
return `up to ${bounds.maxConcurrentInvocations} invocations in flight, interleaved only at asynchronous effects (proxied bindings, fetch): code between two such effects runs without interruption (except continuations of a promise it settles for another invocation, which run then and are still attributed to their own invocation), Web Crypto calls are waited for like I/O, other non-effect awaits (body reads included) settle within one turn of the event loop, synchronous effects are not interleaving points, an effect made by code another effect hosts (an object calling out during a call) is a turn of its own unless that code cannot be interrupted (an object inside blockConcurrencyWhile), when it runs within the hosting effect's turn, the next turn is a seeded pick among parked invocations,
|
|
3390
|
+
return `up to ${bounds.maxConcurrentInvocations} invocations in flight, interleaved only at asynchronous effects (proxied bindings, fetch): code between two such effects runs without interruption (except continuations of a promise it settles for another invocation, which run then and are still attributed to their own invocation), Web Crypto calls are waited for like I/O, other non-effect awaits (body reads included) settle within one turn of the event loop, synchronous effects are not interleaving points, an effect made by code another effect hosts (an object calling out during a call) is a turn of its own unless that code cannot be interrupted (an object inside blockConcurrencyWhile), when it runs within the hosting effect's turn, the next turn is a seeded pick among parked invocations, starting a new event while a slot is free, and firing the earliest timer already due (at or before the virtual now, which effects advance), timers firing in due order among themselves, and a timer not yet due fires (virtual time passing to it) only when nothing else can move`;
|
|
3391
|
+
}
|
|
3392
|
+
/** The first record a replay recorded differently from the run, or how their lengths differ; null when the logs match. */
|
|
3393
|
+
function whatDiffered(run, replay) {
|
|
3394
|
+
const show = (e) => {
|
|
3395
|
+
const payload = canonicalJson(e.payload);
|
|
3396
|
+
const shown = payload.length > 120 ? `${payload.slice(0, 117)}...` : payload;
|
|
3397
|
+
return `${e.kind} ${e.target} ${e.op} in ${e.invocation} ${shown}`;
|
|
3398
|
+
};
|
|
3399
|
+
const a = serializeLog(run).split("\n");
|
|
3400
|
+
const b = serializeLog(replay).split("\n");
|
|
3401
|
+
const n = Math.min(run.entries.length, replay.entries.length);
|
|
3402
|
+
for (let i = 0; i < n; i++) if (a[i] !== b[i]) return `at seq ${i} the run recorded ${show(run.entries[i])} and the replay ${show(replay.entries[i])}`;
|
|
3403
|
+
if (run.entries.length === replay.entries.length) return null;
|
|
3404
|
+
return `the run recorded ${run.entries.length} entries and the replay ${replay.entries.length}`;
|
|
2545
3405
|
}
|
|
2546
|
-
/**
|
|
2547
|
-
function
|
|
2548
|
-
|
|
2549
|
-
for (let i = 1; i < pending.length; i++) if ((pending[i]?.at ?? 0) < (pending[best]?.at ?? 0)) best = i;
|
|
2550
|
-
return best;
|
|
3406
|
+
/** Whether exhaustive exploration reduces by partial order (it does unless told not to). */
|
|
3407
|
+
function reductionOf(options) {
|
|
3408
|
+
return typeof options.exhaustive === "object" ? options.exhaustive.reduction ?? true : true;
|
|
2551
3409
|
}
|
|
2552
3410
|
/** What a verdict rests on about how schedules were chosen, when exploration was exhaustive. */
|
|
2553
3411
|
function explorationAssumption(options) {
|
|
2554
3412
|
if (!options.exhaustive) return [];
|
|
2555
|
-
return ["exhaustive: every placement of faults within maxFaultsPerRun and every order of turns, each run once, until complete or the budget ends (reached.exhaustive says which)", ...(
|
|
3413
|
+
return ["exhaustive: every placement of faults within maxFaultsPerRun and every order of turns, each run once, until complete or the budget ends (reached.exhaustive says which)", ...reductionOf(options) ? ["partial-order reduction: turns of different invocations whose effects have different targets commute, so one order of them is run; this assumes code between an invocation's effects shares no state with other invocations except through effects and the virtual globals, and once a turn other than an event's start performs an effect on another target than its own, queues an event, or reads virtual time or randomness, or schedules a timer, the reduction is off and every order is run (reached.exhaustive.reduction says whether it stayed on)"] : []];
|
|
2556
3414
|
}
|
|
2557
3415
|
/** Deliver events one at a time, each to completion; time passes until a delayed one is due. */
|
|
2558
3416
|
async function deliverInOrder(spec, platform, run) {
|
|
2559
3417
|
const pending = [...platform.initialEvents(spec)];
|
|
2560
3418
|
while (pending.length > 0) {
|
|
2561
|
-
const [event] = pending.splice(
|
|
3419
|
+
const [event] = pending.splice(earliestEvent(pending), 1);
|
|
2562
3420
|
if (event.at !== void 0) await run.advanceTo(event.at);
|
|
2563
3421
|
const result = await platform.deliver(event, run, spec);
|
|
3422
|
+
await run.driveAfterWork();
|
|
2564
3423
|
pending.push(...run.takeQueued(), ...result.followUps);
|
|
2565
3424
|
}
|
|
2566
3425
|
}
|
|
2567
3426
|
/**
|
|
2568
3427
|
* Deliver events with up to `maxConcurrentInvocations` in flight. Each turn
|
|
2569
3428
|
* is a seeded pick: one parked invocation performs its effect and runs to
|
|
2570
|
-
* its next one,
|
|
2571
|
-
* nothing is parked
|
|
3429
|
+
* its next one, while a slot is free the next event starts, or a timer
|
|
3430
|
+
* already due fires. When nothing is parked, no event can start and no
|
|
3431
|
+
* timer is due, the next timer fires (time passes to it), and when nothing
|
|
2572
3432
|
* can move the remaining invocations are abandoned.
|
|
2573
3433
|
*/
|
|
2574
3434
|
async function deliverConcurrently(spec, platform, run) {
|
|
@@ -2584,11 +3444,15 @@ async function deliverConcurrently(spec, platform, run) {
|
|
|
2584
3444
|
active = active.filter((d) => !d.outcome);
|
|
2585
3445
|
pending.push(...run.takeQueued());
|
|
2586
3446
|
if (pending.length > queuedBefore) run.queuedEvents();
|
|
2587
|
-
if (active.length === 0 && pending.length === 0) return;
|
|
2588
|
-
const next = pending.length > 0 ?
|
|
2589
|
-
const
|
|
3447
|
+
if (active.length === 0 && pending.length === 0 && !run.hasAfterWork()) return;
|
|
3448
|
+
const next = pending.length > 0 ? earliestEvent(pending) : -1;
|
|
3449
|
+
const eventAt = pending[next]?.at ?? 0;
|
|
3450
|
+
const due = next >= 0 && eventAt <= run.now();
|
|
2590
3451
|
const canStart = active.length < spec.bounds.maxConcurrentInvocations && due;
|
|
2591
|
-
const
|
|
3452
|
+
const afterWorkOnly = active.length === 0 && run.hasAfterWork();
|
|
3453
|
+
const timerDue = run.nextTimerDue(afterWorkOnly);
|
|
3454
|
+
const untilEvent = next >= 0 && !due && active.length < spec.bounds.maxConcurrentInvocations && (timerDue ?? Number.POSITIVE_INFINITY) > eventAt;
|
|
3455
|
+
const turn = run.takeTurn(canStart, afterWorkOnly, untilEvent || timerDue !== void 0);
|
|
2592
3456
|
if (turn === "start") {
|
|
2593
3457
|
const [event] = pending.splice(next, 1);
|
|
2594
3458
|
const d = {
|
|
@@ -2603,9 +3467,9 @@ async function deliverConcurrently(spec, platform, run) {
|
|
|
2603
3467
|
run.rested();
|
|
2604
3468
|
});
|
|
2605
3469
|
active.push(d);
|
|
2606
|
-
} else if (turn === "none" &&
|
|
2607
|
-
if (!await run.advanceTo(
|
|
2608
|
-
} else if (turn === "none" && !run.fireTimer()) {
|
|
3470
|
+
} else if (turn === "none" && untilEvent) {
|
|
3471
|
+
if (!await run.advanceTo(eventAt)) continue;
|
|
3472
|
+
} else if (turn === "none" && !(active.length > 0 || !run.hasAfterWork() ? run.fireTimer() : run.fireAfterWorkTimer()) && run.settleAfterWork() === 0) {
|
|
2609
3473
|
const stuck = active.flatMap((d) => d.started.invocation() ?? []);
|
|
2610
3474
|
if (run.abandon(stuck) === 0) throw new Error(`the ${platform.name} platform's delivery made no progress and cannot be abandoned`);
|
|
2611
3475
|
}
|
|
@@ -2682,20 +3546,20 @@ async function explore(scenario, platform, options) {
|
|
|
2682
3546
|
seed: options.seed,
|
|
2683
3547
|
assumptions
|
|
2684
3548
|
};
|
|
2685
|
-
|
|
2686
|
-
|
|
2687
|
-
|
|
2688
|
-
|
|
2689
|
-
|
|
2690
|
-
|
|
2691
|
-
};
|
|
2692
|
-
if (spec.bounds.maxConcurrentInvocations > 1 && !platform.concurrent) return {
|
|
3549
|
+
const exhaustive = options.exhaustive ? new ExhaustiveDriver(reductionOf(options), spec.bounds.maxFaultsPerRun) : null;
|
|
3550
|
+
const doneNow = () => ({
|
|
3551
|
+
...done(),
|
|
3552
|
+
...exhaustive ? { exhaustive: exhaustive.reached } : {}
|
|
3553
|
+
});
|
|
3554
|
+
const unsupported = (cause, reason) => ({
|
|
2693
3555
|
...base,
|
|
2694
3556
|
kind: "UNSUPPORTED",
|
|
2695
|
-
cause
|
|
2696
|
-
reason
|
|
2697
|
-
reached:
|
|
2698
|
-
};
|
|
3557
|
+
cause,
|
|
3558
|
+
reason,
|
|
3559
|
+
reached: doneNow()
|
|
3560
|
+
});
|
|
3561
|
+
if (spec.bounds.maxConcurrentInvocations > 1 && !concurrencySupported) return unsupported("unsupported_bounds", "maxConcurrentInvocations above 1 needs AsyncLocalStorage (Node, or workerd with nodejs_compat or nodejs_als)");
|
|
3562
|
+
if (spec.bounds.maxConcurrentInvocations > 1 && !platform.concurrent) return unsupported("unsupported_bounds", `maxConcurrentInvocations above 1 is not supported on the ${platform.name} platform yet`);
|
|
2699
3563
|
const leakCheckRuns = options.leakCheckRuns ?? 2;
|
|
2700
3564
|
/** The first runs' schedules and digests, replayed in reverse order before BOUNDED. */
|
|
2701
3565
|
const witnesses = [];
|
|
@@ -2703,11 +3567,6 @@ async function explore(scenario, platform, options) {
|
|
|
2703
3567
|
/** Eligible points of a run without faults, and the most any run has had: seeded fault positions. */
|
|
2704
3568
|
let baseline = 0;
|
|
2705
3569
|
let longest = 0;
|
|
2706
|
-
const exhaustive = options.exhaustive ? new ExhaustiveDriver(typeof options.exhaustive === "object" ? options.exhaustive.reduction ?? true : true, spec.bounds.maxFaultsPerRun) : null;
|
|
2707
|
-
const doneNow = () => ({
|
|
2708
|
-
...done(),
|
|
2709
|
-
...exhaustive ? { exhaustive: exhaustive.reached } : {}
|
|
2710
|
-
});
|
|
2711
3570
|
while ((exhaustive?.hasMore ?? true) && reached.runs < maxRuns && now() - start < spec.bounds.budgetMs) {
|
|
2712
3571
|
const [runSeed, next] = nextU32(master);
|
|
2713
3572
|
master = next;
|
|
@@ -2726,41 +3585,25 @@ async function explore(scenario, platform, options) {
|
|
|
2726
3585
|
if (run.faultsInjected === 0) baseline = Math.max(baseline, run.eligiblePoints);
|
|
2727
3586
|
longest = Math.max(longest, run.eligiblePoints);
|
|
2728
3587
|
if (exhaustive) {
|
|
2729
|
-
if (exhaustive.mismatch) return {
|
|
2730
|
-
...base,
|
|
2731
|
-
kind: "UNSUPPORTED",
|
|
2732
|
-
cause: "not_replayable",
|
|
2733
|
-
reason: `exhaustive exploration needs deterministic runs: ${exhaustive.mismatch}`,
|
|
2734
|
-
reached: doneNow()
|
|
2735
|
-
};
|
|
3588
|
+
if (exhaustive.mismatch) return unsupported("not_replayable", `exhaustive exploration needs deterministic runs: ${exhaustive.mismatch}`);
|
|
2736
3589
|
if (!exhaustive.ran(() => run.turnsMayNotCommute)) continue;
|
|
2737
3590
|
}
|
|
2738
3591
|
reached.effectPoints += run.effectPoints;
|
|
2739
3592
|
reached.faultsInjected += run.faultsInjected;
|
|
2740
3593
|
if (witnesses.length < leakCheckRuns) witnesses.push({
|
|
2741
3594
|
schedule: run.schedule,
|
|
2742
|
-
|
|
3595
|
+
log
|
|
2743
3596
|
});
|
|
2744
|
-
if (run.unsupportedReasons.length > 0) return
|
|
2745
|
-
...base,
|
|
2746
|
-
kind: "UNSUPPORTED",
|
|
2747
|
-
cause: "unsupported_api",
|
|
2748
|
-
reason: run.unsupportedReasons.join("; "),
|
|
2749
|
-
reached: doneNow()
|
|
2750
|
-
};
|
|
3597
|
+
if (run.unsupportedReasons.length > 0) return unsupported("unsupported_api", run.unsupportedReasons.join("; "));
|
|
2751
3598
|
const violation = checkInvariants(log, spec.invariants)[0];
|
|
2752
3599
|
if (violation) {
|
|
2753
3600
|
const schedule = run.schedule;
|
|
2754
3601
|
const replayedRun = await replayRun(spec, platform, schedule);
|
|
2755
3602
|
const replayed = replayedRun.log;
|
|
2756
3603
|
reached.runs++;
|
|
2757
|
-
if (replayedRun.unsupportedReasons.length > 0
|
|
2758
|
-
|
|
2759
|
-
|
|
2760
|
-
cause: "not_replayable",
|
|
2761
|
-
reason: `the violating run of seed ${schedule.seed} did not reproduce on replay`,
|
|
2762
|
-
reached: doneNow()
|
|
2763
|
-
};
|
|
3604
|
+
if (replayedRun.unsupportedReasons.length > 0) return unsupported("unsupported_api", replayedRun.unsupportedReasons.join("; "));
|
|
3605
|
+
const differed = whatDiffered(log, replayed);
|
|
3606
|
+
if (differed) return unsupported("not_replayable", `the violating run of seed ${schedule.seed} did not reproduce on replay: ${differed}`);
|
|
2764
3607
|
const shrunk = await shrink(spec, platform, schedule, violation, replayed, {
|
|
2765
3608
|
maxRuns: options.maxShrinkRuns ?? 200,
|
|
2766
3609
|
deadline: now() + spec.bounds.budgetMs,
|
|
@@ -2787,13 +3630,9 @@ async function explore(scenario, platform, options) {
|
|
|
2787
3630
|
for (const witness of [...witnesses].reverse()) {
|
|
2788
3631
|
const again = await replayRun(spec, platform, witness.schedule);
|
|
2789
3632
|
reached.runs++;
|
|
2790
|
-
if (again.unsupportedReasons.length > 0
|
|
2791
|
-
|
|
2792
|
-
|
|
2793
|
-
cause: "module_state_leak",
|
|
2794
|
-
reason: `run seed ${witness.schedule.seed} produced a different log when replayed after other runs: state is leaking between runs (module-level variables, or a binding Eutaxy does not reset)`,
|
|
2795
|
-
reached: doneNow()
|
|
2796
|
-
};
|
|
3633
|
+
if (again.unsupportedReasons.length > 0) return unsupported("unsupported_api", again.unsupportedReasons.join("; "));
|
|
3634
|
+
const differed = whatDiffered(witness.log, again.log);
|
|
3635
|
+
if (differed) return unsupported("module_state_leak", `run seed ${witness.schedule.seed} produced a different log when replayed after other runs: state is leaking between runs (module-level variables, or a binding Eutaxy does not reset); ${differed}`);
|
|
2797
3636
|
}
|
|
2798
3637
|
return {
|
|
2799
3638
|
...base,
|
|
@@ -2804,11 +3643,14 @@ async function explore(scenario, platform, options) {
|
|
|
2804
3643
|
}
|
|
2805
3644
|
// packages/engine/src/differential.ts
|
|
2806
3645
|
/**
|
|
2807
|
-
* The platform-neutral
|
|
2808
|
-
* scenario once through Eutaxy
|
|
2809
|
-
*
|
|
2810
|
-
*
|
|
2811
|
-
*
|
|
3646
|
+
* The platform-neutral part of a differential check: the same fault-free
|
|
3647
|
+
* scenario once through Eutaxy and once against the raw bindings, then a
|
|
3648
|
+
* comparison of what each attempt decided and of the final state. Any
|
|
3649
|
+
* difference is a bug in the proxies or the platform model, not in the
|
|
3650
|
+
* handler. The raw side's loop is here too (the same virtual globals and
|
|
3651
|
+
* scripted externals as the proxied side, the same pick of the next event
|
|
3652
|
+
* and the same redelivery rule); a platform writes only how one raw attempt
|
|
3653
|
+
* calls its handler, sharing no proxy code.
|
|
2812
3654
|
*/
|
|
2813
3655
|
/** The raw side cannot abandon an invocation the way the engine does; a stuck handler ends the comparison. */
|
|
2814
3656
|
var DifferentialStuck = class extends Error {
|
|
@@ -2856,6 +3698,73 @@ async function proxiedSide(spec, platform, attempt) {
|
|
|
2856
3698
|
unsupported: run.unsupportedReasons
|
|
2857
3699
|
};
|
|
2858
3700
|
}
|
|
3701
|
+
/**
|
|
3702
|
+
* The raw side: every event delivered as the engine would deliver it with
|
|
3703
|
+
* no faults and one invocation at a time (the earliest due first, time
|
|
3704
|
+
* moved to a delayed one, an unhandled one delivered again under core's
|
|
3705
|
+
* redelivery rule), each attempt performed by the platform. The attempts'
|
|
3706
|
+
* records, in order.
|
|
3707
|
+
*/
|
|
3708
|
+
async function rawRun(spec, side) {
|
|
3709
|
+
const patched = patchGlobals(0);
|
|
3710
|
+
const reasons = [];
|
|
3711
|
+
const unsupported = (reason) => {
|
|
3712
|
+
reasons.push(reason);
|
|
3713
|
+
};
|
|
3714
|
+
const restoreFetch = installScriptedFetch(spec.externals, null, unsupported, () => patched.clock.advance(1));
|
|
3715
|
+
const io = { n: 0 };
|
|
3716
|
+
const settle = (promise) => settleOn(promise, {
|
|
3717
|
+
clock: patched.clock,
|
|
3718
|
+
inFlight: () => io.n,
|
|
3719
|
+
crashed: () => false,
|
|
3720
|
+
stuck: () => {
|
|
3721
|
+
throw new DifferentialStuck();
|
|
3722
|
+
}
|
|
3723
|
+
});
|
|
3724
|
+
const call = async (code) => {
|
|
3725
|
+
try {
|
|
3726
|
+
return {
|
|
3727
|
+
status: "completed",
|
|
3728
|
+
value: await settle(Promise.resolve().then(code))
|
|
3729
|
+
};
|
|
3730
|
+
} catch (error) {
|
|
3731
|
+
if (error instanceof DifferentialStuck) throw error;
|
|
3732
|
+
return {
|
|
3733
|
+
status: "errored",
|
|
3734
|
+
error
|
|
3735
|
+
};
|
|
3736
|
+
}
|
|
3737
|
+
};
|
|
3738
|
+
const attempts = [];
|
|
3739
|
+
const pending = [...side.events];
|
|
3740
|
+
let restore;
|
|
3741
|
+
try {
|
|
3742
|
+
const installed = side.install({
|
|
3743
|
+
clock: patched.clock,
|
|
3744
|
+
io,
|
|
3745
|
+
libraryIo: (perform) => patched.libraryIo(perform),
|
|
3746
|
+
unsupported,
|
|
3747
|
+
settle,
|
|
3748
|
+
call
|
|
3749
|
+
});
|
|
3750
|
+
restore = installed.restore;
|
|
3751
|
+
const { attempt } = installed;
|
|
3752
|
+
while (pending.length > 0) {
|
|
3753
|
+
const [event] = pending.splice(earliestEvent(pending), 1);
|
|
3754
|
+
if (event.at !== void 0 && event.at > patched.clock.now()) patched.clock.advance(event.at - patched.clock.now());
|
|
3755
|
+
const { record, handled } = await attempt(event);
|
|
3756
|
+
attempts.push(record);
|
|
3757
|
+
const next = handled ? null : redelivery(event, side.limit, spec.bounds);
|
|
3758
|
+
if (next) pending.push(next);
|
|
3759
|
+
}
|
|
3760
|
+
} finally {
|
|
3761
|
+
restore?.();
|
|
3762
|
+
restoreFetch();
|
|
3763
|
+
patched.restore();
|
|
3764
|
+
}
|
|
3765
|
+
if (reasons.length > 0) throw new DifferentialStuck(`raw side: ${reasons.join("; ")}`);
|
|
3766
|
+
return attempts;
|
|
3767
|
+
}
|
|
2859
3768
|
/** Every difference between the two sides, one readable entry each; none means they agree. */
|
|
2860
3769
|
function compareSides(eutaxy, raw, unsupported) {
|
|
2861
3770
|
const differences = [];
|
|
@@ -2950,6 +3859,6 @@ function observeCore(namespace, functions) {
|
|
|
2950
3859
|
}
|
|
2951
3860
|
};
|
|
2952
3861
|
}
|
|
2953
|
-
export {
|
|
3862
|
+
export { writeFaults as A, toJson as C, checkInvariants as D, checkInvariant as E, getPath as F, invocationOf as I, redelivery as L, nextInt as M, nextU32 as N, decisionFaults as O, seedPrng as P, canonicalJson as R, patchProperty as S, validateScenario as T, replyOf as _, explore as a, realTick as b, decide as c, runInvocation as d, CrashSignal as f, parseBody as g, isRedirect as h, rawRun as i, nextFloat as j, readFaults as k, hostInvocation as l, findResponse as m, compareSides as n, replay as o, InjectedEffectError as p, proxiedSide as r, restrictSpec as s, observeCore as t, note as u, toResponse as v, defaultBounds as w, virtualEpoch as x, withoutNetwork as y };
|
|
2954
3863
|
|
|
2955
|
-
//# sourceMappingURL=src-
|
|
3864
|
+
//# sourceMappingURL=src-CmHdPsZy.mjs.map
|