eutaxy 0.6.0 → 0.8.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 +55 -14
- package/dist/aws.d.mts +2 -2
- package/dist/bin.mjs +2 -2
- package/dist/cli-BT7zv5TL.mjs +2 -0
- package/dist/cli.d.mts.map +1 -1
- package/dist/cli.mjs +2 -2
- package/dist/cloudflare.d.mts +9 -9
- package/dist/cloudflare.d.mts.map +1 -1
- package/dist/cloudflare.mjs +1 -1
- package/dist/combinators-Bsd9HKtw.d.mts +42 -0
- package/dist/combinators-Bsd9HKtw.d.mts.map +1 -0
- package/dist/combinators-CVjxodAJ.mjs +27 -0
- package/dist/combinators-CVjxodAJ.mjs.map +1 -0
- package/dist/{delivery-invariants-BraYqcYN.d.mts → delivery-invariants-DfPkJL0Q.d.mts} +2 -2
- package/dist/{delivery-invariants-BraYqcYN.d.mts.map → delivery-invariants-DfPkJL0Q.d.mts.map} +1 -1
- package/dist/explore-Rwe1PPqX.d.mts +233 -0
- package/dist/explore-Rwe1PPqX.d.mts.map +1 -0
- package/dist/index.d.mts +141 -20
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +2 -61
- package/dist/index.mjs.map +1 -1
- package/dist/{platform-DfMwknw1.d.mts → platform-Bpnav0kn.d.mts} +6 -4
- package/dist/{platform-DfMwknw1.d.mts.map → platform-Bpnav0kn.d.mts.map} +1 -1
- package/dist/spec.d.mts +2 -0
- package/dist/spec.mjs +1 -0
- package/dist/src-B9L96rZW.mjs +9521 -0
- package/dist/src-B9L96rZW.mjs.map +1 -0
- package/dist/src-BO7I3f-1.mjs.map +1 -1
- package/dist/src-BZtuuu_j.mjs +1706 -0
- package/dist/src-BZtuuu_j.mjs.map +1 -0
- package/package.json +12 -7
- package/dist/cli-CsiVEsS-.mjs +0 -2
- package/dist/explore-CDpJA9zX.d.mts +0 -342
- package/dist/explore-CDpJA9zX.d.mts.map +0 -1
- package/dist/src-C43iUZgr.mjs +0 -5039
- package/dist/src-C43iUZgr.mjs.map +0 -1
- package/dist/src-Cs4Mg55e.mjs +0 -2672
- package/dist/src-Cs4Mg55e.mjs.map +0 -1
|
@@ -0,0 +1,1706 @@
|
|
|
1
|
+
import { C as nextU32, E as canonicalJson, S as nextInt, w as seedPrng, x as nextFloat } from "./src-BO7I3f-1.mjs";
|
|
2
|
+
import { n as isSpec } from "./combinators-CVjxodAJ.mjs";
|
|
3
|
+
// packages/core/src/verdict.ts
|
|
4
|
+
/** One line per verdict, for error messages; full rendering lives in `report`. */
|
|
5
|
+
function describeVerdict(v) {
|
|
6
|
+
switch (v.kind) {
|
|
7
|
+
case "VIOLATED": return `VIOLATED ${v.scenario}: ${v.violation.message} (seed ${v.schedule.seed}, ${v.reached.runs} run(s))`;
|
|
8
|
+
case "BOUNDED": return `BOUNDED ${v.scenario}: no violation in ${v.reached.runs} run(s), ${v.reached.effectPoints} effect point(s), ${v.reached.faultsInjected} fault(s)`;
|
|
9
|
+
case "UNSUPPORTED": return `UNSUPPORTED ${v.scenario}: ${v.cause}: ${v.reason}`;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
// packages/spec/src/ir.ts
|
|
13
|
+
/** A type, for messages. */
|
|
14
|
+
function describeType(t) {
|
|
15
|
+
switch (t.kind) {
|
|
16
|
+
case "number":
|
|
17
|
+
case "boolean":
|
|
18
|
+
case "string":
|
|
19
|
+
case "bigint":
|
|
20
|
+
case "null":
|
|
21
|
+
case "undefined": return t.kind;
|
|
22
|
+
case "literal": return typeof t.value === "string" ? JSON.stringify(t.value) : String(t.value);
|
|
23
|
+
case "record": return t.name ?? `{ ${t.fields.map((f) => `${f.name}: ${describeType(f.type)}`).join("; ")} }`;
|
|
24
|
+
case "union": return t.members.map(describeType).join(" | ");
|
|
25
|
+
case "array": return t.element.kind === "union" ? `(${describeType(t.element)})[]` : `${describeType(t.element)}[]`;
|
|
26
|
+
case "unknown": return t.text;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
// packages/prover/src/regex-gen.ts
|
|
30
|
+
/**
|
|
31
|
+
* Strings that match a regular expression, drawn with core's seeded
|
|
32
|
+
* generator: the pattern is parsed into alternatives, sequences, classes
|
|
33
|
+
* and quantified terms, and each draw walks that tree. The subset is what
|
|
34
|
+
* guards use: literals, escapes, classes with ranges and negation, groups,
|
|
35
|
+
* alternation, the usual quantifiers and anchors. Lookaround,
|
|
36
|
+
* backreferences and Unicode property escapes are refused with
|
|
37
|
+
* `UnsupportedRegex`, and the caller falls back to sampling. Every draw is
|
|
38
|
+
* checked against the real `RegExp` by the caller, so a gap here can only
|
|
39
|
+
* cost a draw, never admit a value outside the guard.
|
|
40
|
+
*/
|
|
41
|
+
var UnsupportedRegex = class extends Error {};
|
|
42
|
+
const digits$1 = [..."0123456789"];
|
|
43
|
+
const word = [..."abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"];
|
|
44
|
+
const space = [..." \n\r\f\v"];
|
|
45
|
+
/** What `.` and negated classes draw from: printable ASCII, a few others, no newline. */
|
|
46
|
+
const anyChar = [..." !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~üß日"];
|
|
47
|
+
/** A plentiful count for an unbounded quantifier: keeps drawn strings short. */
|
|
48
|
+
const unboundedExtra = 4;
|
|
49
|
+
/** The most a counted quantifier may ask for; a guard wanting more is left to sampling. */
|
|
50
|
+
const maxRepeat = 1e3;
|
|
51
|
+
/** The longest string one draw may build before it is given up. */
|
|
52
|
+
const maxLength = 1e4;
|
|
53
|
+
/** The most nodes one draw may visit: nested repetitions of empty matches multiply work without length. */
|
|
54
|
+
const maxSteps = 1e5;
|
|
55
|
+
/** A class node over the characters it can draw; negation is resolved here, once. */
|
|
56
|
+
function classNode(chars, negated) {
|
|
57
|
+
const pool = negated ? anyChar.filter((c) => !chars.includes(c)) : chars;
|
|
58
|
+
if (pool.length === 0) throw new UnsupportedRegex("an empty class matches nothing");
|
|
59
|
+
return {
|
|
60
|
+
kind: "class",
|
|
61
|
+
pool
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
var Parser = class {
|
|
65
|
+
at = 0;
|
|
66
|
+
src;
|
|
67
|
+
dotAll;
|
|
68
|
+
unicode;
|
|
69
|
+
constructor(src, flags) {
|
|
70
|
+
this.src = src;
|
|
71
|
+
this.dotAll = flags.includes("s");
|
|
72
|
+
this.unicode = flags.includes("u");
|
|
73
|
+
}
|
|
74
|
+
parse() {
|
|
75
|
+
const node = this.alternation();
|
|
76
|
+
if (this.at < this.src.length) throw new UnsupportedRegex(`unexpected ${this.src[this.at]}`);
|
|
77
|
+
return node;
|
|
78
|
+
}
|
|
79
|
+
peek() {
|
|
80
|
+
return this.src[this.at];
|
|
81
|
+
}
|
|
82
|
+
take() {
|
|
83
|
+
const c = this.src[this.at++];
|
|
84
|
+
if (c === void 0) throw new UnsupportedRegex("pattern ends early");
|
|
85
|
+
return c;
|
|
86
|
+
}
|
|
87
|
+
alternation() {
|
|
88
|
+
const options = [this.sequence()];
|
|
89
|
+
while (this.peek() === "|") {
|
|
90
|
+
this.at++;
|
|
91
|
+
options.push(this.sequence());
|
|
92
|
+
}
|
|
93
|
+
return options.length === 1 ? options[0] : {
|
|
94
|
+
kind: "alt",
|
|
95
|
+
options
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
sequence() {
|
|
99
|
+
const items = [];
|
|
100
|
+
while (this.at < this.src.length && this.peek() !== "|" && this.peek() !== ")") {
|
|
101
|
+
const atom = this.atom();
|
|
102
|
+
items.push(this.quantified(atom));
|
|
103
|
+
}
|
|
104
|
+
return items.length === 1 ? items[0] : {
|
|
105
|
+
kind: "seq",
|
|
106
|
+
items
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
quantified(atom) {
|
|
110
|
+
const c = this.peek();
|
|
111
|
+
let min;
|
|
112
|
+
let max;
|
|
113
|
+
if (c === "*") [min, max] = [0, unboundedExtra];
|
|
114
|
+
else if (c === "+") [min, max] = [1, 5];
|
|
115
|
+
else if (c === "?") [min, max] = [0, 1];
|
|
116
|
+
else if (c === "{") {
|
|
117
|
+
const m = /^\{(\d+)(,(\d*))?\}/.exec(this.src.slice(this.at));
|
|
118
|
+
if (!m) return atom;
|
|
119
|
+
min = Number(m[1]);
|
|
120
|
+
max = m[2] === void 0 ? min : m[3] === "" || m[3] === void 0 ? min + unboundedExtra : Number(m[3]);
|
|
121
|
+
if (max < min) throw new UnsupportedRegex(`quantifier {${min},${max}} is empty`);
|
|
122
|
+
if (max > maxRepeat) throw new UnsupportedRegex(`quantifier {${min},${max}} repeats too much to draw`);
|
|
123
|
+
this.at += m[0].length - 1;
|
|
124
|
+
} else return atom;
|
|
125
|
+
this.at++;
|
|
126
|
+
if (this.peek() === "?") this.at++;
|
|
127
|
+
return {
|
|
128
|
+
kind: "repeat",
|
|
129
|
+
item: atom,
|
|
130
|
+
min,
|
|
131
|
+
max
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
atom() {
|
|
135
|
+
const c = this.take();
|
|
136
|
+
switch (c) {
|
|
137
|
+
case "(": {
|
|
138
|
+
if (this.src.startsWith("?:", this.at)) this.at += 2;
|
|
139
|
+
else if (this.src.startsWith("?<", this.at) && !"=!".includes(this.src[this.at + 2] ?? "")) {
|
|
140
|
+
const close = this.src.indexOf(">", this.at);
|
|
141
|
+
if (close < 0) throw new UnsupportedRegex("unterminated group name");
|
|
142
|
+
this.at = close + 1;
|
|
143
|
+
} else if (this.peek() === "?") throw new UnsupportedRegex("lookaround or modifier group");
|
|
144
|
+
const inner = this.alternation();
|
|
145
|
+
if (this.take() !== ")") throw new UnsupportedRegex("unbalanced parenthesis");
|
|
146
|
+
return inner;
|
|
147
|
+
}
|
|
148
|
+
case "[": return this.charClass();
|
|
149
|
+
case ".": return classNode(this.dotAll ? [...anyChar, "\n"] : anyChar, false);
|
|
150
|
+
case "^":
|
|
151
|
+
case "$": return { kind: "empty" };
|
|
152
|
+
case "\\": return this.escape(false);
|
|
153
|
+
case ")":
|
|
154
|
+
case "|":
|
|
155
|
+
case "*":
|
|
156
|
+
case "+":
|
|
157
|
+
case "?": throw new UnsupportedRegex(`unexpected ${c}`);
|
|
158
|
+
default: return {
|
|
159
|
+
kind: "literal",
|
|
160
|
+
text: c
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
/** An escape after `\`; inside a class, `\b` is a backspace. */
|
|
165
|
+
escape(inClass) {
|
|
166
|
+
const e = this.take();
|
|
167
|
+
switch (e) {
|
|
168
|
+
case "d": return classNode(digits$1, false);
|
|
169
|
+
case "D": return classNode(digits$1, true);
|
|
170
|
+
case "w": return classNode(word, false);
|
|
171
|
+
case "W": return classNode(word, true);
|
|
172
|
+
case "s": return classNode(space, false);
|
|
173
|
+
case "S": return classNode(space, true);
|
|
174
|
+
case "n": return {
|
|
175
|
+
kind: "literal",
|
|
176
|
+
text: "\n"
|
|
177
|
+
};
|
|
178
|
+
case "t": return {
|
|
179
|
+
kind: "literal",
|
|
180
|
+
text: " "
|
|
181
|
+
};
|
|
182
|
+
case "r": return {
|
|
183
|
+
kind: "literal",
|
|
184
|
+
text: "\r"
|
|
185
|
+
};
|
|
186
|
+
case "f": return {
|
|
187
|
+
kind: "literal",
|
|
188
|
+
text: "\f"
|
|
189
|
+
};
|
|
190
|
+
case "v": return {
|
|
191
|
+
kind: "literal",
|
|
192
|
+
text: "\v"
|
|
193
|
+
};
|
|
194
|
+
case "0": return {
|
|
195
|
+
kind: "literal",
|
|
196
|
+
text: "\0"
|
|
197
|
+
};
|
|
198
|
+
case "b":
|
|
199
|
+
if (inClass) return {
|
|
200
|
+
kind: "literal",
|
|
201
|
+
text: "\b"
|
|
202
|
+
};
|
|
203
|
+
return { kind: "empty" };
|
|
204
|
+
case "B": return { kind: "empty" };
|
|
205
|
+
case "x": {
|
|
206
|
+
const hex = this.src.slice(this.at, this.at + 2);
|
|
207
|
+
if (!/^[0-9a-fA-F]{2}$/.test(hex)) throw new UnsupportedRegex("bad \\x escape");
|
|
208
|
+
this.at += 2;
|
|
209
|
+
return {
|
|
210
|
+
kind: "literal",
|
|
211
|
+
text: String.fromCharCode(Number.parseInt(hex, 16))
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
case "u": {
|
|
215
|
+
if (this.peek() === "{") {
|
|
216
|
+
if (!this.unicode) throw new UnsupportedRegex("\\u{} without the u flag");
|
|
217
|
+
const close = this.src.indexOf("}", this.at);
|
|
218
|
+
const hex = this.src.slice(this.at + 1, close);
|
|
219
|
+
if (close < 0 || !/^[0-9a-fA-F]{1,6}$/.test(hex)) throw new UnsupportedRegex("bad \\u escape");
|
|
220
|
+
this.at = close + 1;
|
|
221
|
+
return {
|
|
222
|
+
kind: "literal",
|
|
223
|
+
text: String.fromCodePoint(Number.parseInt(hex, 16))
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
const hex = this.src.slice(this.at, this.at + 4);
|
|
227
|
+
if (!/^[0-9a-fA-F]{4}$/.test(hex)) throw new UnsupportedRegex("bad \\u escape");
|
|
228
|
+
this.at += 4;
|
|
229
|
+
return {
|
|
230
|
+
kind: "literal",
|
|
231
|
+
text: String.fromCharCode(Number.parseInt(hex, 16))
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
case "p":
|
|
235
|
+
case "P":
|
|
236
|
+
case "k":
|
|
237
|
+
case "c": throw new UnsupportedRegex(`\\${e} escapes`);
|
|
238
|
+
default:
|
|
239
|
+
if (/[1-9]/.test(e)) throw new UnsupportedRegex("backreferences");
|
|
240
|
+
return {
|
|
241
|
+
kind: "literal",
|
|
242
|
+
text: e
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
/** `[...]` after its opening bracket. `[]` is empty and `[]a]` is an empty class then `a]`, as in JavaScript. */
|
|
247
|
+
charClass() {
|
|
248
|
+
let negated = false;
|
|
249
|
+
if (this.peek() === "^") {
|
|
250
|
+
negated = true;
|
|
251
|
+
this.at++;
|
|
252
|
+
}
|
|
253
|
+
const chars = /* @__PURE__ */ new Set();
|
|
254
|
+
while (this.peek() !== "]") {
|
|
255
|
+
const c = this.take();
|
|
256
|
+
let member;
|
|
257
|
+
if (c === "\\") {
|
|
258
|
+
const node = this.escape(true);
|
|
259
|
+
if (node.kind === "class") {
|
|
260
|
+
for (const x of node.pool) chars.add(x);
|
|
261
|
+
continue;
|
|
262
|
+
}
|
|
263
|
+
if (node.kind !== "literal") throw new UnsupportedRegex("escape in class");
|
|
264
|
+
member = node.text;
|
|
265
|
+
} else member = c;
|
|
266
|
+
if (this.peek() === "-" && this.src[this.at + 1] !== "]" && this.src[this.at + 1] !== void 0) {
|
|
267
|
+
this.at++;
|
|
268
|
+
const to = this.take() === "\\" ? this.escape(true) : {
|
|
269
|
+
kind: "literal",
|
|
270
|
+
text: this.src[this.at - 1]
|
|
271
|
+
};
|
|
272
|
+
if (to.kind !== "literal") throw new UnsupportedRegex("range to a class");
|
|
273
|
+
const lo = member.codePointAt(0);
|
|
274
|
+
const hi = to.text.codePointAt(0);
|
|
275
|
+
if (hi < lo) throw new UnsupportedRegex("range out of order");
|
|
276
|
+
if (hi - lo > 8192) throw new UnsupportedRegex("range too wide to enumerate");
|
|
277
|
+
for (let p = lo; p <= hi; p++) chars.add(String.fromCodePoint(p));
|
|
278
|
+
continue;
|
|
279
|
+
}
|
|
280
|
+
chars.add(member);
|
|
281
|
+
}
|
|
282
|
+
this.at++;
|
|
283
|
+
return classNode([...chars], negated);
|
|
284
|
+
}
|
|
285
|
+
};
|
|
286
|
+
/** The pattern as a tree, or `UnsupportedRegex`. */
|
|
287
|
+
function parseRegex(pattern, flags) {
|
|
288
|
+
if (flags.includes("v")) throw new UnsupportedRegex("the v flag");
|
|
289
|
+
return new Parser(pattern, flags).parse();
|
|
290
|
+
}
|
|
291
|
+
/** One string from the tree, with the state that follows. */
|
|
292
|
+
function drawRegex(node, state) {
|
|
293
|
+
let s = state;
|
|
294
|
+
let steps = 0;
|
|
295
|
+
const go = (n) => {
|
|
296
|
+
if (++steps > maxSteps) throw new UnsupportedRegex("a draw visited more than 100000 nodes");
|
|
297
|
+
switch (n.kind) {
|
|
298
|
+
case "empty": return "";
|
|
299
|
+
case "literal": return n.text;
|
|
300
|
+
case "alt": {
|
|
301
|
+
const [i, next] = nextInt(s, n.options.length);
|
|
302
|
+
s = next;
|
|
303
|
+
return go(n.options[i]);
|
|
304
|
+
}
|
|
305
|
+
case "seq": return n.items.map(go).join("");
|
|
306
|
+
case "repeat": {
|
|
307
|
+
const [extra, next] = nextInt(s, n.max - n.min + 1);
|
|
308
|
+
s = next;
|
|
309
|
+
let out = "";
|
|
310
|
+
for (let i = 0; i < n.min + extra; i++) {
|
|
311
|
+
out += go(n.item);
|
|
312
|
+
if (out.length > maxLength) throw new UnsupportedRegex("a draw grew past 10000 characters");
|
|
313
|
+
}
|
|
314
|
+
return out;
|
|
315
|
+
}
|
|
316
|
+
case "class": {
|
|
317
|
+
const [i, next] = nextInt(s, n.pool.length);
|
|
318
|
+
s = next;
|
|
319
|
+
return n.pool[i];
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
};
|
|
323
|
+
return [go(node), s];
|
|
324
|
+
}
|
|
325
|
+
// packages/prover/src/render.ts
|
|
326
|
+
/**
|
|
327
|
+
* A value as a JavaScript literal: exact for the primitive types (bigints
|
|
328
|
+
* included, `-0` and `NaN` kept), records and arrays spelled as literals,
|
|
329
|
+
* so a counterexample reads back as what it was. Never truncated and never
|
|
330
|
+
* throwing: the rungs key distinct inputs by it, so two values must render
|
|
331
|
+
* alike only when they are alike.
|
|
332
|
+
*/
|
|
333
|
+
function render(value) {
|
|
334
|
+
return renderAt(value, /* @__PURE__ */ new Set());
|
|
335
|
+
}
|
|
336
|
+
function renderAt(value, seen) {
|
|
337
|
+
switch (typeof value) {
|
|
338
|
+
case "bigint": return `${value}n`;
|
|
339
|
+
case "number": return Object.is(value, -0) ? "-0" : String(value);
|
|
340
|
+
case "string": return JSON.stringify(value);
|
|
341
|
+
case "undefined": return "undefined";
|
|
342
|
+
case "boolean": return String(value);
|
|
343
|
+
case "function": return "[function]";
|
|
344
|
+
case "symbol": return String(value);
|
|
345
|
+
}
|
|
346
|
+
if (value === null) return "null";
|
|
347
|
+
const o = value;
|
|
348
|
+
if (seen.has(o)) return "<cycle>";
|
|
349
|
+
seen.add(o);
|
|
350
|
+
try {
|
|
351
|
+
if (Array.isArray(o)) return `[${o.map((x) => renderAt(x, seen)).join(", ")}]`;
|
|
352
|
+
if (o instanceof Date) return Number.isNaN(o.getTime()) ? "Date(NaN)" : `Date(${JSON.stringify(o.toISOString())})`;
|
|
353
|
+
if (o instanceof Map) return `Map([${[...o].map(([k, v]) => `[${renderAt(k, seen)}, ${renderAt(v, seen)}]`).join(", ")}])`;
|
|
354
|
+
if (o instanceof Set) return `Set([${[...o].map((x) => renderAt(x, seen)).join(", ")}])`;
|
|
355
|
+
const entries = Object.entries(o).map(([k, v]) => `${/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(k) ? k : JSON.stringify(k)}: ${renderAt(v, seen)}`);
|
|
356
|
+
return entries.length === 0 ? "{}" : `{ ${entries.join(", ")} }`;
|
|
357
|
+
} catch {
|
|
358
|
+
try {
|
|
359
|
+
return `<${String(o)}>`;
|
|
360
|
+
} catch {
|
|
361
|
+
return "<unrenderable>";
|
|
362
|
+
}
|
|
363
|
+
} finally {
|
|
364
|
+
seen.delete(o);
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
/** Whether two values render alike: structural equality that tells `1n` from `1` and `-0` from `0`. */
|
|
368
|
+
function same(a, b) {
|
|
369
|
+
return typeof a === typeof b && render(a) === render(b);
|
|
370
|
+
}
|
|
371
|
+
/**
|
|
372
|
+
* A deep copy of plain data: arrays and records (plain objects); anything
|
|
373
|
+
* else (a Date the simulator passed, a class instance) is kept as it is.
|
|
374
|
+
* Each evaluation of a property gets its own, so code under test that
|
|
375
|
+
* mutates an input (`xs.sort()`) changes neither the printed counterexample
|
|
376
|
+
* nor the candidates built from it, and no two places in an input share an
|
|
377
|
+
* object, as their printed literals do not.
|
|
378
|
+
*/
|
|
379
|
+
function copy(v) {
|
|
380
|
+
if (Array.isArray(v)) return v.map(copy);
|
|
381
|
+
if (v !== null && typeof v === "object") {
|
|
382
|
+
const proto = Object.getPrototypeOf(v);
|
|
383
|
+
if (proto !== Object.prototype && proto !== null) return v;
|
|
384
|
+
return Object.fromEntries(Object.entries(v).map(([k, x]) => [k, copy(x)]));
|
|
385
|
+
}
|
|
386
|
+
return v;
|
|
387
|
+
}
|
|
388
|
+
// packages/prover/src/generate.ts
|
|
389
|
+
/**
|
|
390
|
+
* Inputs for the `TESTED` rung: values drawn from a binder's type, steered
|
|
391
|
+
* by what the precondition says about it, with core's seeded generator;
|
|
392
|
+
* and the smaller values a shrinker tries in place of one that broke a
|
|
393
|
+
* property. Everything here is pure: the same state draws the same value,
|
|
394
|
+
* so a `TESTED` verdict and its counterexample come out the same on every
|
|
395
|
+
* machine.
|
|
396
|
+
*/
|
|
397
|
+
/** Thrown when no value could be drawn: `unsupported` for a type the rung cannot draw, `gave_up` for a range nothing lies in. */
|
|
398
|
+
var CannotGenerate = class extends Error {
|
|
399
|
+
why;
|
|
400
|
+
constructor(why, message) {
|
|
401
|
+
super(message);
|
|
402
|
+
this.why = why;
|
|
403
|
+
}
|
|
404
|
+
};
|
|
405
|
+
/** Integers are drawn as doubles, so they stay exact. */
|
|
406
|
+
const safe = 2n ** 53n - 1n;
|
|
407
|
+
/** How far an unguarded side of a bigint range reaches. */
|
|
408
|
+
const bigintReach = 1n << 128n;
|
|
409
|
+
/** A finite endpoint's value as a double. */
|
|
410
|
+
function endpointNumber(value) {
|
|
411
|
+
return Number(value.replace(/_/g, "").replace(/n$/, ""));
|
|
412
|
+
}
|
|
413
|
+
/**
|
|
414
|
+
* The integers a bound admits, as the bound itself: `n < 5` admits up to 4,
|
|
415
|
+
* `n <= 5` up to 5, `n < 4.5` up to 4; `0 < n` from 1, `-0.5 <= n` from 0.
|
|
416
|
+
*/
|
|
417
|
+
function integerBound(e, side) {
|
|
418
|
+
const text = e.value.replace(/_/g, "").replace(/n$/, "");
|
|
419
|
+
if (/[.eE]/.test(text)) {
|
|
420
|
+
const n = Number(text);
|
|
421
|
+
if (!Number.isFinite(n)) return void 0;
|
|
422
|
+
if (!Number.isInteger(n)) return BigInt(side === "lower" ? Math.ceil(n) : Math.floor(n));
|
|
423
|
+
return BigInt(n) + (e.inclusive ? 0n : side === "lower" ? 1n : -1n);
|
|
424
|
+
}
|
|
425
|
+
return BigInt(text) + (e.inclusive ? 0n : side === "lower" ? 1n : -1n);
|
|
426
|
+
}
|
|
427
|
+
/** A finite integral endpoint's value, exact: underscores and the `n` suffix dropped. */
|
|
428
|
+
function endpointBigInt(value) {
|
|
429
|
+
return BigInt(value.replace(/_/g, "").replace(/n$/, ""));
|
|
430
|
+
}
|
|
431
|
+
/**
|
|
432
|
+
* The closed integer interval a hint admits: within what a double holds
|
|
433
|
+
* exactly for `number`; for `bigint`, the hint's ends, an unhinted side
|
|
434
|
+
* reaching 2^128 past the other. Undefined when no integer lies in it.
|
|
435
|
+
*/
|
|
436
|
+
function integerRange(kind, hint) {
|
|
437
|
+
const guardLo = hint?.lower === void 0 ? void 0 : integerBound(hint.lower, "lower");
|
|
438
|
+
const guardHi = hint?.upper === void 0 ? void 0 : integerBound(hint.upper, "upper");
|
|
439
|
+
let lo;
|
|
440
|
+
let hi;
|
|
441
|
+
if (kind === "bigint") {
|
|
442
|
+
lo = guardLo ?? (guardHi === void 0 ? -bigintReach : min(-bigintReach, guardHi - bigintReach));
|
|
443
|
+
hi = guardHi ?? (guardLo === void 0 ? bigintReach : max(bigintReach, guardLo + bigintReach));
|
|
444
|
+
} else {
|
|
445
|
+
lo = max(-safe, guardLo ?? -safe);
|
|
446
|
+
hi = min(safe, guardHi ?? safe);
|
|
447
|
+
}
|
|
448
|
+
return lo <= hi ? {
|
|
449
|
+
lo,
|
|
450
|
+
hi
|
|
451
|
+
} : void 0;
|
|
452
|
+
}
|
|
453
|
+
const max = (a, b) => a > b ? a : b;
|
|
454
|
+
const min = (a, b) => a < b ? a : b;
|
|
455
|
+
/** Uniform in [lo, hi], however wide: enough 32-bit words, masked, rejected when past the span. */
|
|
456
|
+
function uniformBigInt(lo, hi, state) {
|
|
457
|
+
const span = hi - lo + 1n;
|
|
458
|
+
if (span <= 0n) throw new RangeError(`empty integer interval [${lo}, ${hi}]`);
|
|
459
|
+
if (span === 1n) return [lo, state];
|
|
460
|
+
let bits = 0;
|
|
461
|
+
for (let x = span - 1n; x > 0n; x >>= 1n) bits++;
|
|
462
|
+
const words = Math.ceil(bits / 32);
|
|
463
|
+
const mask = (1n << BigInt(bits)) - 1n;
|
|
464
|
+
let s = state;
|
|
465
|
+
for (;;) {
|
|
466
|
+
let r = 0n;
|
|
467
|
+
for (let i = 0; i < words; i++) {
|
|
468
|
+
const [u, next] = nextU32(s);
|
|
469
|
+
s = next;
|
|
470
|
+
r = r << 32n | BigInt(u);
|
|
471
|
+
}
|
|
472
|
+
r &= mask;
|
|
473
|
+
if (r < span) return [lo + r, s];
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
/** One of a list, uniformly. */
|
|
477
|
+
function pick(items, state) {
|
|
478
|
+
const [i, s] = nextInt(state, items.length);
|
|
479
|
+
return [items[i], s];
|
|
480
|
+
}
|
|
481
|
+
/**
|
|
482
|
+
* An integer from the range, biased: most draws are small, since
|
|
483
|
+
* properties fail at boundaries and small cases far more often than deep in
|
|
484
|
+
* the range, some are medium or large, and a few are the range's edges.
|
|
485
|
+
*/
|
|
486
|
+
/** An integer range's edges, as the random draws pick from them (repeats kept, so draws stay as they were). */
|
|
487
|
+
function integerEdgeCandidates(lo, hi) {
|
|
488
|
+
return [
|
|
489
|
+
lo,
|
|
490
|
+
hi,
|
|
491
|
+
0n,
|
|
492
|
+
1n,
|
|
493
|
+
-1n,
|
|
494
|
+
lo + 1n,
|
|
495
|
+
hi - 1n
|
|
496
|
+
].filter((v) => v >= lo && v <= hi);
|
|
497
|
+
}
|
|
498
|
+
function drawInteger(name, kind, hint, state) {
|
|
499
|
+
const range = integerRange(kind, hint);
|
|
500
|
+
if (!range) throw new CannotGenerate("gave_up", kind === "bigint" ? `no integer lies inside the precondition's bounds on ${name}` : `no integer lies inside the precondition's bounds on ${name} within ±2^53, which is as far as draws of number go`);
|
|
501
|
+
const { lo, hi } = range;
|
|
502
|
+
const [cls, s] = nextInt(state, 100);
|
|
503
|
+
const within = (a, z) => {
|
|
504
|
+
const l = max(lo, a);
|
|
505
|
+
const h = min(hi, z);
|
|
506
|
+
return l <= h ? [l, h] : void 0;
|
|
507
|
+
};
|
|
508
|
+
if (cls < 5) return pick(integerEdgeCandidates(lo, hi), s);
|
|
509
|
+
const [l, h] = (cls < 55 ? within(-16n, 16n) : cls < 80 ? within(-(1n << 16n), 1n << 16n) : cls < 95 ? within(-(1n << 31n), 1n << 31n) : void 0) ?? [lo, hi];
|
|
510
|
+
return uniformBigInt(l, h, s);
|
|
511
|
+
}
|
|
512
|
+
/** The double interval a hint admits. */
|
|
513
|
+
function numberRange(hint) {
|
|
514
|
+
return {
|
|
515
|
+
lo: hint?.lower === void 0 ? Number.NEGATIVE_INFINITY : endpointNumber(hint.lower.value),
|
|
516
|
+
hi: hint?.upper === void 0 ? Number.POSITIVE_INFINITY : endpointNumber(hint.upper.value),
|
|
517
|
+
guarded: hint?.lower !== void 0 || hint?.upper !== void 0
|
|
518
|
+
};
|
|
519
|
+
}
|
|
520
|
+
/** Whether a double satisfies the hint's bounds (NaN satisfies none). */
|
|
521
|
+
function withinNumberHint(hint, v) {
|
|
522
|
+
if (!hint) return true;
|
|
523
|
+
if (hint.integer && !Number.isInteger(v)) return false;
|
|
524
|
+
if (hint.lower) {
|
|
525
|
+
const lo = endpointNumber(hint.lower.value);
|
|
526
|
+
if (!(hint.lower.inclusive ? v >= lo : v > lo)) return false;
|
|
527
|
+
}
|
|
528
|
+
if (hint.upper) {
|
|
529
|
+
const hi = endpointNumber(hint.upper.value);
|
|
530
|
+
if (!(hint.upper.inclusive ? v <= hi : v < hi)) return false;
|
|
531
|
+
}
|
|
532
|
+
return true;
|
|
533
|
+
}
|
|
534
|
+
/** A double hint's edges, as the random draws pick from them. */
|
|
535
|
+
function doubleEdgeCandidates(hint) {
|
|
536
|
+
const { lo, hi, guarded } = numberRange(hint);
|
|
537
|
+
return [
|
|
538
|
+
0,
|
|
539
|
+
-0,
|
|
540
|
+
1,
|
|
541
|
+
-1,
|
|
542
|
+
.5,
|
|
543
|
+
Number.MIN_VALUE,
|
|
544
|
+
Number.EPSILON,
|
|
545
|
+
Number.MAX_VALUE,
|
|
546
|
+
-Number.MAX_VALUE,
|
|
547
|
+
Number.POSITIVE_INFINITY,
|
|
548
|
+
Number.NEGATIVE_INFINITY,
|
|
549
|
+
...guarded ? [lo, hi] : [NaN]
|
|
550
|
+
].filter((v) => withinNumberHint(hint, v));
|
|
551
|
+
}
|
|
552
|
+
function drawNumber(hint, state) {
|
|
553
|
+
const { lo, hi } = numberRange(hint);
|
|
554
|
+
const [cls, s0] = nextInt(state, 100);
|
|
555
|
+
if (cls < 8) {
|
|
556
|
+
const edges = doubleEdgeCandidates(hint);
|
|
557
|
+
if (edges.length > 0) return pick(edges, s0);
|
|
558
|
+
}
|
|
559
|
+
if (cls < 35) {
|
|
560
|
+
const [k, s1] = nextInt(s0, 33);
|
|
561
|
+
return [k - 16, s1];
|
|
562
|
+
}
|
|
563
|
+
if (cls < 60) {
|
|
564
|
+
const [d, s1] = pick([
|
|
565
|
+
2,
|
|
566
|
+
4,
|
|
567
|
+
8,
|
|
568
|
+
10,
|
|
569
|
+
100
|
|
570
|
+
], s0);
|
|
571
|
+
const [k, s2] = nextInt(s1, 65);
|
|
572
|
+
return [(k - 32) / d, s2];
|
|
573
|
+
}
|
|
574
|
+
if (cls < 80 && Number.isFinite(lo) && Number.isFinite(hi)) {
|
|
575
|
+
const [f, s1] = nextFloat(s0);
|
|
576
|
+
return [lo + f * (hi - lo), s1];
|
|
577
|
+
}
|
|
578
|
+
const [f, s1] = nextFloat(s0);
|
|
579
|
+
const [e, s2] = nextInt(s1, 2100);
|
|
580
|
+
const [neg, s3] = nextInt(s2, 2);
|
|
581
|
+
return [(neg ? -1 : 1) * (1 + f) * 2 ** (e - 1074), s3];
|
|
582
|
+
}
|
|
583
|
+
const letters = "abcdefghijklmnopqrstuvwxyz";
|
|
584
|
+
const digits = "0123456789";
|
|
585
|
+
const plainAlphabet = [...`${letters}${letters.toUpperCase()}${digits} _-.,:/\n'"üß日😀`];
|
|
586
|
+
const stringEdges = [
|
|
587
|
+
"",
|
|
588
|
+
" ",
|
|
589
|
+
"a",
|
|
590
|
+
"0",
|
|
591
|
+
"-1",
|
|
592
|
+
"\n",
|
|
593
|
+
"\0",
|
|
594
|
+
"ü",
|
|
595
|
+
"😀",
|
|
596
|
+
"a".repeat(100)
|
|
597
|
+
];
|
|
598
|
+
/** The longest string sampled over a regex's own characters, and the longest edge a regex is tried on. */
|
|
599
|
+
const regexSampleLength = 12;
|
|
600
|
+
/**
|
|
601
|
+
* The characters a regex names: literals, the ranges and escapes of its
|
|
602
|
+
* classes. Strings drawn from them match the pattern far more often than
|
|
603
|
+
* strings drawn from everything, which is what the rejection below needs.
|
|
604
|
+
*/
|
|
605
|
+
function regexAlphabet(pattern) {
|
|
606
|
+
const chars = /* @__PURE__ */ new Set();
|
|
607
|
+
const add = (s) => {
|
|
608
|
+
for (const c of s) chars.add(c);
|
|
609
|
+
};
|
|
610
|
+
for (let i = 0; i < pattern.length; i++) {
|
|
611
|
+
const c = pattern[i];
|
|
612
|
+
if (c === "\\") {
|
|
613
|
+
const e = pattern[++i];
|
|
614
|
+
if (e === "d") add(digits);
|
|
615
|
+
else if (e === "w") add(`${letters}${letters.toUpperCase()}${digits}_`);
|
|
616
|
+
else if (e === "s") add(" ");
|
|
617
|
+
else if (e === "n") add("\n");
|
|
618
|
+
else if (e === "t") add(" ");
|
|
619
|
+
else if (e !== void 0 && !/[DWSbBuxpP]/.test(e)) add(e);
|
|
620
|
+
continue;
|
|
621
|
+
}
|
|
622
|
+
if (pattern[i + 1] === "-" && i + 2 < pattern.length && pattern[i + 2] !== "]") {
|
|
623
|
+
const from = c.codePointAt(0);
|
|
624
|
+
const to = pattern[i + 2].codePointAt(0);
|
|
625
|
+
if (from < to && to - from <= 4096) for (let p = from; p <= to; p++) chars.add(String.fromCodePoint(p));
|
|
626
|
+
i += 2;
|
|
627
|
+
continue;
|
|
628
|
+
}
|
|
629
|
+
if (!"^$()[]{}|*+?.-".includes(c)) chars.add(c);
|
|
630
|
+
}
|
|
631
|
+
return [...chars];
|
|
632
|
+
}
|
|
633
|
+
/** A string of `length` characters from `alphabet`. */
|
|
634
|
+
function drawChars(alphabet, length, state) {
|
|
635
|
+
let s = state;
|
|
636
|
+
let out = "";
|
|
637
|
+
for (let i = 0; i < length; i++) {
|
|
638
|
+
const [c, next] = pick(alphabet, s);
|
|
639
|
+
s = next;
|
|
640
|
+
out += c;
|
|
641
|
+
}
|
|
642
|
+
return [out, s];
|
|
643
|
+
}
|
|
644
|
+
/** Whether a string passes the hint's regex (the real RegExp has the last word). */
|
|
645
|
+
function matchesRegex(hint, v) {
|
|
646
|
+
return hint?.regex === void 0 || new RegExp(hint.regex.pattern, hint.regex.flags).test(v);
|
|
647
|
+
}
|
|
648
|
+
function drawString(hint, state) {
|
|
649
|
+
const g = hint?.regex;
|
|
650
|
+
if (!g) {
|
|
651
|
+
const [cls, s0] = nextInt(state, 100);
|
|
652
|
+
if (cls < 15) return pick(stringEdges, s0);
|
|
653
|
+
const [length, s1] = nextInt(s0, 25);
|
|
654
|
+
return drawChars(plainAlphabet, length, s1);
|
|
655
|
+
}
|
|
656
|
+
let s = state;
|
|
657
|
+
let tree;
|
|
658
|
+
try {
|
|
659
|
+
tree = parseRegex(g.pattern, g.flags);
|
|
660
|
+
} catch (err) {
|
|
661
|
+
if (!(err instanceof UnsupportedRegex)) throw err;
|
|
662
|
+
}
|
|
663
|
+
for (let attempt = 0; tree && attempt < 16; attempt++) try {
|
|
664
|
+
const [candidate, s1] = drawRegex(tree, s);
|
|
665
|
+
s = s1;
|
|
666
|
+
if (matchesRegex(hint, candidate)) return [candidate, s];
|
|
667
|
+
} catch (err) {
|
|
668
|
+
if (!(err instanceof UnsupportedRegex)) throw err;
|
|
669
|
+
break;
|
|
670
|
+
}
|
|
671
|
+
const fromPattern = regexAlphabet(g.pattern);
|
|
672
|
+
const alphabet = fromPattern.length > 0 ? fromPattern : plainAlphabet;
|
|
673
|
+
for (let attempt = 0; attempt < 64; attempt++) {
|
|
674
|
+
const [length, s1] = nextInt(s, 13);
|
|
675
|
+
const [candidate, s2] = drawChars(alphabet, length, s1);
|
|
676
|
+
s = s2;
|
|
677
|
+
if (matchesRegex(hint, candidate)) return [candidate, s];
|
|
678
|
+
}
|
|
679
|
+
throw new CannotGenerate("gave_up", `no string matching /${g.pattern}/${g.flags} was found in 64 draws over its characters`);
|
|
680
|
+
}
|
|
681
|
+
/** The most shrink candidates one array offers at a step; the shrinker evaluates 500 in all. */
|
|
682
|
+
const shrinkCandidateLimit = 256;
|
|
683
|
+
/** The most edges a record type has; random draws cover its other combinations. */
|
|
684
|
+
const recordEdgeLimit = 64;
|
|
685
|
+
/** Without repeats, in one pass, as `same` compares: 0 and -0 stay apart, two empty arrays do not. */
|
|
686
|
+
function distinct(values) {
|
|
687
|
+
const seen = /* @__PURE__ */ new Set();
|
|
688
|
+
return values.filter((v) => {
|
|
689
|
+
const key = `${typeof v}:${render(v)}`;
|
|
690
|
+
if (seen.has(key)) return false;
|
|
691
|
+
seen.add(key);
|
|
692
|
+
return true;
|
|
693
|
+
});
|
|
694
|
+
}
|
|
695
|
+
function integerEdges(kind, hint) {
|
|
696
|
+
const range = integerRange(kind, hint);
|
|
697
|
+
return range ? distinct(integerEdgeCandidates(range.lo, range.hi)) : [];
|
|
698
|
+
}
|
|
699
|
+
/**
|
|
700
|
+
* How many combinations of these edge lists to take side by side: the
|
|
701
|
+
* longest list's length, twice over when there are two or more lists,
|
|
702
|
+
* since `combined` walks them once in step (equal edges meet, as
|
|
703
|
+
* `Infinity - Infinity` needs) and once rotated (list `k` starting `k`
|
|
704
|
+
* places in, so lists of one type also differ).
|
|
705
|
+
*/
|
|
706
|
+
function combinations(lists) {
|
|
707
|
+
const longest = Math.max(0, ...lists.map((l) => l.length));
|
|
708
|
+
return lists.length > 1 ? 2 * longest : longest;
|
|
709
|
+
}
|
|
710
|
+
/** List `k`'s value in the `i`-th combination. */
|
|
711
|
+
function combined(lists, k, i) {
|
|
712
|
+
const longest = Math.max(...lists.map((l) => l.length));
|
|
713
|
+
const own = lists[k];
|
|
714
|
+
const offset = i < longest ? 0 : k;
|
|
715
|
+
return own[(i % longest + offset) % own.length];
|
|
716
|
+
}
|
|
717
|
+
/**
|
|
718
|
+
* The values of a type, within the hint, that properties fail on far more
|
|
719
|
+
* often than elsewhere: the bounds, zero and one, the double specials (NaN,
|
|
720
|
+
* the infinities, -0), the empty string, both booleans, every member of a
|
|
721
|
+
* union, and records built field by field from those. The `TESTED` rung
|
|
722
|
+
* tries every one before any random draw, so a claim false on one value
|
|
723
|
+
* is found under every seed; a claim false only on a combination of edges
|
|
724
|
+
* across binders or fields is found where the rotation below pairs them,
|
|
725
|
+
* and otherwise rests on the random draws. A type with none (a string
|
|
726
|
+
* whose regex no edge matches) gives none.
|
|
727
|
+
*/
|
|
728
|
+
function edgesOf(type, hint) {
|
|
729
|
+
switch (type.kind) {
|
|
730
|
+
case "boolean": return [false, true];
|
|
731
|
+
case "number":
|
|
732
|
+
if (hint?.integer) return integerEdges("number", hint).flatMap((v) => v === 0n ? [0, -0] : [Number(v)]);
|
|
733
|
+
return distinct(doubleEdgeCandidates(hint));
|
|
734
|
+
case "bigint": return integerEdges("bigint", hint);
|
|
735
|
+
case "string": return stringEdges.filter((v) => hint?.regex === void 0 || v.length <= regexSampleLength && matchesRegex(hint, v));
|
|
736
|
+
case "literal": return [type.value];
|
|
737
|
+
case "null": return [null];
|
|
738
|
+
case "undefined": return [void 0];
|
|
739
|
+
case "union": return distinct(type.members.flatMap((m) => edgesOf(m, void 0)));
|
|
740
|
+
case "record": {
|
|
741
|
+
const lists = type.fields.map((f) => edgesOf(f.type, void 0));
|
|
742
|
+
if (lists.some((l) => l.length === 0)) return [];
|
|
743
|
+
const out = [];
|
|
744
|
+
const seen = /* @__PURE__ */ new Set();
|
|
745
|
+
for (let i = 0; i < Math.max(1, combinations(lists)) && out.length < recordEdgeLimit; i++) {
|
|
746
|
+
const r = Object.fromEntries(type.fields.map((f, k) => [f.name, combined(lists, k, i)]));
|
|
747
|
+
const key = render(r);
|
|
748
|
+
if (seen.has(key)) continue;
|
|
749
|
+
seen.add(key);
|
|
750
|
+
out.push(r);
|
|
751
|
+
}
|
|
752
|
+
return out;
|
|
753
|
+
}
|
|
754
|
+
case "array": {
|
|
755
|
+
if (!drawable(type.element)) return [];
|
|
756
|
+
const element = edgesOf(type.element, void 0);
|
|
757
|
+
return [
|
|
758
|
+
[],
|
|
759
|
+
...element.map((e) => [e]),
|
|
760
|
+
...element.length > 0 ? [[element[0], element[0]]] : []
|
|
761
|
+
];
|
|
762
|
+
}
|
|
763
|
+
case "unknown": return [];
|
|
764
|
+
}
|
|
765
|
+
}
|
|
766
|
+
/** Each binder's edges, computed once for a property's whole edge phase. */
|
|
767
|
+
function edgeLists(binders) {
|
|
768
|
+
return binders.map((b) => edgesOf(b.type, b.hint));
|
|
769
|
+
}
|
|
770
|
+
/** How many edge assignments the binders' `edgeLists` give. */
|
|
771
|
+
function edgeAssignmentCount(edges) {
|
|
772
|
+
return combinations(edges);
|
|
773
|
+
}
|
|
774
|
+
/**
|
|
775
|
+
* The `i`-th edge assignment from the binders' `edgeLists`: the lists
|
|
776
|
+
* walked once in step and once rotated (binder `j` starting `j` places
|
|
777
|
+
* in), cycling, so each edge of each binder appears, equal edges meet and
|
|
778
|
+
* binders of one type also differ; a binder with no edges is drawn at
|
|
779
|
+
* random. Undefined from `edgeAssignmentCount` on.
|
|
780
|
+
*/
|
|
781
|
+
function edgeAssignment(binders, edges, i, state) {
|
|
782
|
+
if (i >= combinations(edges)) return void 0;
|
|
783
|
+
const values = [];
|
|
784
|
+
let s = state;
|
|
785
|
+
binders.forEach((b, j) => {
|
|
786
|
+
if (edges[j].length > 0) {
|
|
787
|
+
values.push(copy(combined(edges, j, i)));
|
|
788
|
+
return;
|
|
789
|
+
}
|
|
790
|
+
const [v, next] = generate(b, s);
|
|
791
|
+
s = next;
|
|
792
|
+
values.push(v);
|
|
793
|
+
});
|
|
794
|
+
return [values, s];
|
|
795
|
+
}
|
|
796
|
+
/** Whether every part of the type is one the rung draws (no `unknown` anywhere in it). */
|
|
797
|
+
function drawable(type) {
|
|
798
|
+
switch (type.kind) {
|
|
799
|
+
case "unknown": return false;
|
|
800
|
+
case "array": return drawable(type.element);
|
|
801
|
+
case "union": return type.members.every(drawable);
|
|
802
|
+
case "record": return type.fields.every((f) => drawable(f.type));
|
|
803
|
+
default: return true;
|
|
804
|
+
}
|
|
805
|
+
}
|
|
806
|
+
/** A value of the type, within the hint (which applies to a top-level binder only). */
|
|
807
|
+
function generateOf(name, type, hint, state) {
|
|
808
|
+
switch (type.kind) {
|
|
809
|
+
case "boolean": {
|
|
810
|
+
const [bit, s] = nextInt(state, 2);
|
|
811
|
+
return [bit === 1, s];
|
|
812
|
+
}
|
|
813
|
+
case "number": {
|
|
814
|
+
if (hint?.integer) {
|
|
815
|
+
const [v, s] = drawInteger(name, "number", hint, state);
|
|
816
|
+
return [Number(v), s];
|
|
817
|
+
}
|
|
818
|
+
let s = state;
|
|
819
|
+
for (let attempt = 0; attempt < 64; attempt++) {
|
|
820
|
+
const [v, next] = drawNumber(hint, s);
|
|
821
|
+
s = next;
|
|
822
|
+
if (withinNumberHint(hint, v)) return [v, s];
|
|
823
|
+
}
|
|
824
|
+
throw new CannotGenerate("gave_up", `no number inside the precondition's bounds on ${name} was found in 64 draws`);
|
|
825
|
+
}
|
|
826
|
+
case "bigint": return drawInteger(name, "bigint", hint, state);
|
|
827
|
+
case "string": return drawString(hint, state);
|
|
828
|
+
case "literal": return [type.value, state];
|
|
829
|
+
case "null": return [null, state];
|
|
830
|
+
case "undefined": return [void 0, state];
|
|
831
|
+
case "union": {
|
|
832
|
+
const [member, s] = pick(type.members, state);
|
|
833
|
+
return generateOf(name, member, void 0, s);
|
|
834
|
+
}
|
|
835
|
+
case "record": {
|
|
836
|
+
let s = state;
|
|
837
|
+
const out = {};
|
|
838
|
+
for (const f of type.fields) {
|
|
839
|
+
const [v, next] = generateOf(`${name}.${f.name}`, f.type, void 0, s);
|
|
840
|
+
s = next;
|
|
841
|
+
out[f.name] = v;
|
|
842
|
+
}
|
|
843
|
+
return [out, s];
|
|
844
|
+
}
|
|
845
|
+
case "array": {
|
|
846
|
+
if (!drawable(type.element)) throw new CannotGenerate("unsupported", `${name} has a type the rung cannot draw from: ${describeType(type)}`);
|
|
847
|
+
const [cls, s0] = nextInt(state, 100);
|
|
848
|
+
const [length, s1] = nextInt(s0, cls < 70 ? 5 : 17);
|
|
849
|
+
let s = s1;
|
|
850
|
+
const out = [];
|
|
851
|
+
for (let i = 0; i < length; i++) {
|
|
852
|
+
if (out.length > 0) {
|
|
853
|
+
const [repeat, s2] = nextInt(s, 4);
|
|
854
|
+
s = s2;
|
|
855
|
+
if (repeat === 0) {
|
|
856
|
+
const [v, s3] = pick(out, s);
|
|
857
|
+
s = s3;
|
|
858
|
+
out.push(copy(v));
|
|
859
|
+
continue;
|
|
860
|
+
}
|
|
861
|
+
}
|
|
862
|
+
const [v, next] = generateOf(`${name}[${i}]`, type.element, void 0, s);
|
|
863
|
+
s = next;
|
|
864
|
+
out.push(v);
|
|
865
|
+
}
|
|
866
|
+
return [out, s];
|
|
867
|
+
}
|
|
868
|
+
case "unknown": throw new CannotGenerate("unsupported", `${name} has a type the rung cannot draw from: ${describeType(type)}`);
|
|
869
|
+
}
|
|
870
|
+
}
|
|
871
|
+
/** A value of the binder's type, within its hint. */
|
|
872
|
+
function generate(b, state) {
|
|
873
|
+
return generateOf(b.name, b.type, b.hint, state);
|
|
874
|
+
}
|
|
875
|
+
/** Whether a binder draws integers (or bigints), which later binders may relate to. */
|
|
876
|
+
function integral(b) {
|
|
877
|
+
return b.type.kind === "bigint" || b.type.kind === "number" && b.hint?.integer === true;
|
|
878
|
+
}
|
|
879
|
+
/**
|
|
880
|
+
* One assignment to every binder. Each is drawn from its own type and
|
|
881
|
+
* hint; a later integer binder is sometimes made to relate to earlier
|
|
882
|
+
* ones instead, equal to one or the sum or difference of two, since
|
|
883
|
+
* preconditions relate binders (`refunded + amount === captured`) and
|
|
884
|
+
* independent draws would meet such an equation once in thousands. A
|
|
885
|
+
* related value outside the binder's hint is dropped for the draw.
|
|
886
|
+
*/
|
|
887
|
+
function generateAssignment(binders, state) {
|
|
888
|
+
const values = [];
|
|
889
|
+
let s = state;
|
|
890
|
+
for (const b of binders) {
|
|
891
|
+
const [drawn, next] = generate(b, s);
|
|
892
|
+
s = next;
|
|
893
|
+
let v = drawn;
|
|
894
|
+
const earlier = values.filter((x, i) => integral(binders[i]) && typeof x === typeof drawn);
|
|
895
|
+
if (integral(b) && earlier.length > 0) {
|
|
896
|
+
const [cls, s1] = nextInt(s, 10);
|
|
897
|
+
s = s1;
|
|
898
|
+
if (cls < 3) {
|
|
899
|
+
const [a, s2] = pick(earlier, s1);
|
|
900
|
+
const [c, s3] = pick(earlier, s2);
|
|
901
|
+
s = s3;
|
|
902
|
+
let related = a;
|
|
903
|
+
if (cls === 1) related = typeof a === "bigint" ? a + c : a + c;
|
|
904
|
+
if (cls === 2) related = typeof a === "bigint" ? a - c : a - c;
|
|
905
|
+
if (inDomain(b, related) && (typeof related !== "number" || Number.isSafeInteger(related))) v = related;
|
|
906
|
+
}
|
|
907
|
+
}
|
|
908
|
+
values.push(v);
|
|
909
|
+
}
|
|
910
|
+
return [values, s];
|
|
911
|
+
}
|
|
912
|
+
/** Whether a value is of the type (hints aside). */
|
|
913
|
+
function conforms(type, v) {
|
|
914
|
+
switch (type.kind) {
|
|
915
|
+
case "number": return typeof v === "number";
|
|
916
|
+
case "boolean": return typeof v === "boolean";
|
|
917
|
+
case "string": return typeof v === "string";
|
|
918
|
+
case "bigint": return typeof v === "bigint";
|
|
919
|
+
case "literal": return v === type.value;
|
|
920
|
+
case "null": return v === null;
|
|
921
|
+
case "undefined": return v === void 0;
|
|
922
|
+
case "union": return type.members.some((m) => conforms(m, v));
|
|
923
|
+
case "record": return typeof v === "object" && v !== null && type.fields.every((f) => conforms(f.type, v[f.name]));
|
|
924
|
+
case "array":
|
|
925
|
+
if (!Array.isArray(v)) return false;
|
|
926
|
+
for (let i = 0; i < v.length; i++) if (!(i in v) || !conforms(type.element, v[i])) return false;
|
|
927
|
+
return true;
|
|
928
|
+
case "unknown": return true;
|
|
929
|
+
}
|
|
930
|
+
}
|
|
931
|
+
/** Whether a top-level value is of the binder's type and within its hint. */
|
|
932
|
+
function inDomain(b, v) {
|
|
933
|
+
if (!conforms(b.type, v)) return false;
|
|
934
|
+
const h = b.hint;
|
|
935
|
+
if (!h) return true;
|
|
936
|
+
if (typeof v === "number") return withinNumberHint(h, v);
|
|
937
|
+
if (typeof v === "bigint") {
|
|
938
|
+
const lo = h.lower && integerBound(h.lower, "lower");
|
|
939
|
+
const hi = h.upper && integerBound(h.upper, "upper");
|
|
940
|
+
if (lo !== void 0 && v < lo) return false;
|
|
941
|
+
if (hi !== void 0 && v > hi) return false;
|
|
942
|
+
return true;
|
|
943
|
+
}
|
|
944
|
+
if (typeof v === "string") return matchesRegex(h, v);
|
|
945
|
+
return true;
|
|
946
|
+
}
|
|
947
|
+
/** The integer nearest to zero in [lo, hi]. */
|
|
948
|
+
function nearestZero(lo, hi) {
|
|
949
|
+
return lo > 0n ? lo : hi < 0n ? hi : 0n;
|
|
950
|
+
}
|
|
951
|
+
/** Steps from `v` toward `target`: the target itself, then the halfway points back toward `v`. */
|
|
952
|
+
function towards(v, target) {
|
|
953
|
+
const out = [];
|
|
954
|
+
if (v === target) return out;
|
|
955
|
+
out.push(target);
|
|
956
|
+
let d = (v - target) / 2n;
|
|
957
|
+
while (d !== 0n) {
|
|
958
|
+
out.push(v - d);
|
|
959
|
+
d /= 2n;
|
|
960
|
+
}
|
|
961
|
+
return out;
|
|
962
|
+
}
|
|
963
|
+
/**
|
|
964
|
+
* Smaller values to try in place of `v`, simplest first, all of the type
|
|
965
|
+
* and within the hint, none equal to `v`. Numbers move toward zero (or the
|
|
966
|
+
* bound nearest it), strings get shorter and plainer, `true` becomes
|
|
967
|
+
* `false`, a union moves to its simplest member, a record shrinks one
|
|
968
|
+
* field at a time.
|
|
969
|
+
*/
|
|
970
|
+
function shrinkCandidates(b, v) {
|
|
971
|
+
return candidatesOf(b.type, b.hint, v).filter((c) => !Object.is(c, v) && inDomain(b, c));
|
|
972
|
+
}
|
|
973
|
+
function candidatesOf(type, hint, v) {
|
|
974
|
+
const dedupe = (xs) => {
|
|
975
|
+
const keys = /* @__PURE__ */ new Set();
|
|
976
|
+
return xs.filter((x) => {
|
|
977
|
+
const key = `${typeof x}:${render(x)}`;
|
|
978
|
+
if (keys.has(key)) return false;
|
|
979
|
+
keys.add(key);
|
|
980
|
+
return true;
|
|
981
|
+
});
|
|
982
|
+
};
|
|
983
|
+
switch (type.kind) {
|
|
984
|
+
case "boolean": return [false];
|
|
985
|
+
case "number": {
|
|
986
|
+
if (typeof v !== "number") return [];
|
|
987
|
+
if (Object.is(v, -0)) return [0];
|
|
988
|
+
if (hint?.integer || Number.isInteger(v)) {
|
|
989
|
+
const range = integerRange("number", hint);
|
|
990
|
+
if (!Number.isInteger(v) || !range) return [];
|
|
991
|
+
return dedupe(towards(BigInt(v), nearestZero(range.lo, range.hi)).map(Number));
|
|
992
|
+
}
|
|
993
|
+
const { lo, hi } = numberRange(hint);
|
|
994
|
+
const target = lo > 0 ? lo : Math.min(hi, 0);
|
|
995
|
+
if (!Number.isFinite(v)) return [
|
|
996
|
+
target,
|
|
997
|
+
1,
|
|
998
|
+
-1,
|
|
999
|
+
Number.MAX_VALUE,
|
|
1000
|
+
-Number.MAX_VALUE
|
|
1001
|
+
];
|
|
1002
|
+
const out = [target, Math.trunc(v)];
|
|
1003
|
+
let d = (v - target) / 2;
|
|
1004
|
+
for (let i = 0; i < 24 && d !== 0; i++) {
|
|
1005
|
+
out.push(v - d);
|
|
1006
|
+
d /= 2;
|
|
1007
|
+
}
|
|
1008
|
+
return dedupe(out);
|
|
1009
|
+
}
|
|
1010
|
+
case "bigint": {
|
|
1011
|
+
const range = integerRange("bigint", hint);
|
|
1012
|
+
if (typeof v !== "bigint" || !range) return [];
|
|
1013
|
+
return towards(v, nearestZero(range.lo, range.hi));
|
|
1014
|
+
}
|
|
1015
|
+
case "string": {
|
|
1016
|
+
if (typeof v !== "string") return [];
|
|
1017
|
+
const chars = [...v];
|
|
1018
|
+
const out = ["", chars.slice(0, Math.floor(chars.length / 2)).join("")];
|
|
1019
|
+
out.push(chars.slice(Math.ceil(chars.length / 2)).join(""));
|
|
1020
|
+
for (const at of [
|
|
1021
|
+
0,
|
|
1022
|
+
chars.length - 1,
|
|
1023
|
+
Math.floor(chars.length / 2)
|
|
1024
|
+
]) out.push([...chars.slice(0, at), ...chars.slice(at + 1)].join(""));
|
|
1025
|
+
const first = chars.findIndex((c) => c !== "a");
|
|
1026
|
+
if (first >= 0) out.push([
|
|
1027
|
+
...chars.slice(0, first),
|
|
1028
|
+
"a",
|
|
1029
|
+
...chars.slice(first + 1)
|
|
1030
|
+
].join(""));
|
|
1031
|
+
return dedupe(out);
|
|
1032
|
+
}
|
|
1033
|
+
case "array": {
|
|
1034
|
+
if (!Array.isArray(v)) return [];
|
|
1035
|
+
const out = v.length > 0 ? [[]] : [];
|
|
1036
|
+
for (let i = 0; i < v.length; i++) out.push(v.filter((_, j) => j !== i));
|
|
1037
|
+
const keys = v.map((x) => `${typeof x}:${render(x)}`);
|
|
1038
|
+
for (let i = 0; i < v.length && out.length < shrinkCandidateLimit; i++) {
|
|
1039
|
+
const own = keys[i];
|
|
1040
|
+
const repeated = keys.some((k, j) => j !== i && k === own);
|
|
1041
|
+
for (const c of candidatesOf(type.element, void 0, v[i])) {
|
|
1042
|
+
if (out.length >= shrinkCandidateLimit) break;
|
|
1043
|
+
if (`${typeof c}:${render(c)}` === own) continue;
|
|
1044
|
+
out.push(v.map((x, j) => j === i ? c : x));
|
|
1045
|
+
if (repeated) out.push(v.map((x, j) => keys[j] === own ? copy(c) : x));
|
|
1046
|
+
}
|
|
1047
|
+
}
|
|
1048
|
+
return dedupe(out).slice(0, shrinkCandidateLimit);
|
|
1049
|
+
}
|
|
1050
|
+
case "literal":
|
|
1051
|
+
case "null":
|
|
1052
|
+
case "undefined":
|
|
1053
|
+
case "unknown": return [];
|
|
1054
|
+
case "union": {
|
|
1055
|
+
const simplest = type.members.flatMap((m) => {
|
|
1056
|
+
switch (m.kind) {
|
|
1057
|
+
case "null": return [null];
|
|
1058
|
+
case "undefined": return [void 0];
|
|
1059
|
+
case "boolean": return [false];
|
|
1060
|
+
case "number": return [0];
|
|
1061
|
+
case "bigint": return [0n];
|
|
1062
|
+
case "string": return [""];
|
|
1063
|
+
case "literal": return [m.value];
|
|
1064
|
+
default: return [];
|
|
1065
|
+
}
|
|
1066
|
+
});
|
|
1067
|
+
const own = type.members.find((m) => conforms(m, v));
|
|
1068
|
+
return dedupe([...simplest, ...own ? candidatesOf(own, void 0, v) : []]);
|
|
1069
|
+
}
|
|
1070
|
+
case "record": {
|
|
1071
|
+
if (typeof v !== "object" || v === null) return [];
|
|
1072
|
+
const o = v;
|
|
1073
|
+
const out = [];
|
|
1074
|
+
for (const f of type.fields) for (const c of candidatesOf(f.type, void 0, o[f.name])) if (!same(c, o[f.name])) out.push({
|
|
1075
|
+
...o,
|
|
1076
|
+
[f.name]: c
|
|
1077
|
+
});
|
|
1078
|
+
return out;
|
|
1079
|
+
}
|
|
1080
|
+
}
|
|
1081
|
+
}
|
|
1082
|
+
// packages/prover/src/checked.ts
|
|
1083
|
+
/**
|
|
1084
|
+
* One assignment through a spec. The precondition decides whether the
|
|
1085
|
+
* assignment is inside the claim; a precondition that throws or does not
|
|
1086
|
+
* answer a boolean is a malformed property, since it cannot tell. The
|
|
1087
|
+
* claim then holds, or is false, or throws: a throw from the claim, the
|
|
1088
|
+
* function's own included, means the claim is not true there. A claim
|
|
1089
|
+
* that returns something other than a boolean is malformed.
|
|
1090
|
+
*/
|
|
1091
|
+
function evaluate(spec, values, copied = false) {
|
|
1092
|
+
const fresh = () => copied ? copy(values) : values;
|
|
1093
|
+
if (spec.when) {
|
|
1094
|
+
let inside;
|
|
1095
|
+
try {
|
|
1096
|
+
inside = spec.when(...fresh());
|
|
1097
|
+
} catch (err) {
|
|
1098
|
+
return { malformed: `the precondition threw ${String(err)}` };
|
|
1099
|
+
}
|
|
1100
|
+
if (typeof inside !== "boolean") return { malformed: `the precondition returned ${render(inside)}, not a boolean` };
|
|
1101
|
+
if (!inside) return { outcome: "discarded" };
|
|
1102
|
+
}
|
|
1103
|
+
let claim;
|
|
1104
|
+
try {
|
|
1105
|
+
claim = spec.ensures(...fresh());
|
|
1106
|
+
} catch (err) {
|
|
1107
|
+
return {
|
|
1108
|
+
outcome: "refuted",
|
|
1109
|
+
error: String(err)
|
|
1110
|
+
};
|
|
1111
|
+
}
|
|
1112
|
+
if (typeof claim !== "boolean") return { malformed: `the claim returned ${render(claim)}, not a boolean` };
|
|
1113
|
+
return { outcome: claim ? "held" : "refuted" };
|
|
1114
|
+
}
|
|
1115
|
+
/** How every rung words an `UNKNOWN` for a property that cannot be evaluated; the ladder recognizes it. */
|
|
1116
|
+
const malformedReason = (detail) => `the property could not be evaluated: ${detail}`;
|
|
1117
|
+
function checkedRung(input) {
|
|
1118
|
+
const { module, fn, property } = input;
|
|
1119
|
+
const base = {
|
|
1120
|
+
module,
|
|
1121
|
+
function: fn.name,
|
|
1122
|
+
property: property.name,
|
|
1123
|
+
text: property.text
|
|
1124
|
+
};
|
|
1125
|
+
const unknown = (cause, reason) => ({
|
|
1126
|
+
...base,
|
|
1127
|
+
kind: "UNKNOWN",
|
|
1128
|
+
cause,
|
|
1129
|
+
reason,
|
|
1130
|
+
assumptions: []
|
|
1131
|
+
});
|
|
1132
|
+
if (property.kind === "total") return unknown("unsupported", "termination is not observable on the inputs the simulator passed; total needs a prover");
|
|
1133
|
+
if (input.unobservable) return unknown("unavailable", `calls to ${fn.name} could not be observed: ${input.unobservable}`);
|
|
1134
|
+
const spec = input.spec;
|
|
1135
|
+
if (!spec) return unknown("unavailable", `the spec file's export ${property.name} was not loaded`);
|
|
1136
|
+
const binders = property.binders;
|
|
1137
|
+
const seen = /* @__PURE__ */ new Set();
|
|
1138
|
+
let held = 0;
|
|
1139
|
+
let discarded = 0;
|
|
1140
|
+
for (const call of input.calls) {
|
|
1141
|
+
const values = binders.map((_, i) => call.args[i]);
|
|
1142
|
+
const key = values.map(render).join(", ");
|
|
1143
|
+
if (seen.has(key)) continue;
|
|
1144
|
+
seen.add(key);
|
|
1145
|
+
if (!binders.every((b, i) => conforms(b.type, values[i]))) {
|
|
1146
|
+
discarded++;
|
|
1147
|
+
continue;
|
|
1148
|
+
}
|
|
1149
|
+
const counterexample = (error) => ({
|
|
1150
|
+
binders: binders.map((b, i) => ({
|
|
1151
|
+
name: b.name,
|
|
1152
|
+
value: render(values[i])
|
|
1153
|
+
})),
|
|
1154
|
+
call: `${fn.name}(${call.args.map(render).join(", ")})`,
|
|
1155
|
+
...error === void 0 ? {} : { error }
|
|
1156
|
+
});
|
|
1157
|
+
const result = evaluate(spec, values);
|
|
1158
|
+
if ("malformed" in result) return unknown("unsupported", malformedReason(result.malformed));
|
|
1159
|
+
if (result.outcome === "refuted") return {
|
|
1160
|
+
...base,
|
|
1161
|
+
kind: "REFUTED",
|
|
1162
|
+
rung: "CHECKED",
|
|
1163
|
+
counterexample: counterexample(result.error),
|
|
1164
|
+
assumptions: []
|
|
1165
|
+
};
|
|
1166
|
+
if (result.outcome === "discarded") discarded++;
|
|
1167
|
+
else held++;
|
|
1168
|
+
}
|
|
1169
|
+
if (held === 0) return unknown("gave_up", input.calls.length === 0 ? `the simulator never called ${fn.name}` : `none of the ${seen.size} distinct input(s) the simulator passed is inside the claim (the precondition)`);
|
|
1170
|
+
return {
|
|
1171
|
+
...base,
|
|
1172
|
+
kind: "CHECKED",
|
|
1173
|
+
inputs: held,
|
|
1174
|
+
discarded,
|
|
1175
|
+
assumptions: [`held on the ${held} distinct input(s) the simulator passed to ${fn.name}; inputs it never passed are not covered`]
|
|
1176
|
+
};
|
|
1177
|
+
}
|
|
1178
|
+
// packages/prover/src/shrink.ts
|
|
1179
|
+
/** How many evaluations shrinking may spend on one counterexample. */
|
|
1180
|
+
const shrinkBudget = 500;
|
|
1181
|
+
function shrinkRefutation(spec, binders, start, startEvaluation) {
|
|
1182
|
+
let values = [...start];
|
|
1183
|
+
let error = refutes(startEvaluation) ?? {};
|
|
1184
|
+
let spent = 0;
|
|
1185
|
+
let progress = true;
|
|
1186
|
+
while (progress && spent < shrinkBudget) {
|
|
1187
|
+
progress = false;
|
|
1188
|
+
for (let i = 0; i < binders.length && spent < shrinkBudget; i++) {
|
|
1189
|
+
const b = binders[i];
|
|
1190
|
+
let moved = true;
|
|
1191
|
+
while (moved && spent < shrinkBudget) {
|
|
1192
|
+
moved = false;
|
|
1193
|
+
for (const candidate of shrinkCandidates(b, values[i])) {
|
|
1194
|
+
if (spent >= shrinkBudget) break;
|
|
1195
|
+
spent++;
|
|
1196
|
+
const trial = values.with(i, candidate);
|
|
1197
|
+
if (spec.sample && !inSample(spec, trial)) continue;
|
|
1198
|
+
const still = refutes(evaluate(spec, trial, true));
|
|
1199
|
+
if (still === void 0) continue;
|
|
1200
|
+
values = trial;
|
|
1201
|
+
error = still;
|
|
1202
|
+
moved = true;
|
|
1203
|
+
progress = true;
|
|
1204
|
+
break;
|
|
1205
|
+
}
|
|
1206
|
+
}
|
|
1207
|
+
}
|
|
1208
|
+
}
|
|
1209
|
+
return error.error === void 0 ? { values } : {
|
|
1210
|
+
values,
|
|
1211
|
+
error: error.error
|
|
1212
|
+
};
|
|
1213
|
+
}
|
|
1214
|
+
/** Whether an evaluation still refutes: an object (holding the error text, if any), or undefined. */
|
|
1215
|
+
function refutes(e) {
|
|
1216
|
+
if ("malformed" in e || e.outcome !== "refuted") return void 0;
|
|
1217
|
+
return "error" in e && e.error !== void 0 ? { error: e.error } : {};
|
|
1218
|
+
}
|
|
1219
|
+
/** Whether the spec's sample holds of the values (a sample that throws or answers other than true does not). */
|
|
1220
|
+
function inSample(spec, values) {
|
|
1221
|
+
try {
|
|
1222
|
+
return spec.sample?.(...copy(values)) === true;
|
|
1223
|
+
} catch {
|
|
1224
|
+
return false;
|
|
1225
|
+
}
|
|
1226
|
+
}
|
|
1227
|
+
// packages/prover/src/tested.ts
|
|
1228
|
+
/**
|
|
1229
|
+
* The `TESTED` rung: a property evaluated on assignments drawn from its
|
|
1230
|
+
* binders' types, steered by the precondition's hints, with a seed, so the
|
|
1231
|
+
* same seed draws the same assignments everywhere. An assignment where the
|
|
1232
|
+
* claim is false is shrunk toward smaller values that still break it and
|
|
1233
|
+
* reported as a counterexample. Holding on every drawn assignment is
|
|
1234
|
+
* `TESTED`: evidence from sampling, which says nothing about the inputs
|
|
1235
|
+
* never drawn.
|
|
1236
|
+
*/
|
|
1237
|
+
function testedRung(input) {
|
|
1238
|
+
const { module, fn, property } = input;
|
|
1239
|
+
const base = {
|
|
1240
|
+
module,
|
|
1241
|
+
function: fn.name,
|
|
1242
|
+
property: property.name,
|
|
1243
|
+
text: property.text
|
|
1244
|
+
};
|
|
1245
|
+
const unknown = (cause, reason) => ({
|
|
1246
|
+
...base,
|
|
1247
|
+
kind: "UNKNOWN",
|
|
1248
|
+
cause,
|
|
1249
|
+
reason,
|
|
1250
|
+
assumptions: []
|
|
1251
|
+
});
|
|
1252
|
+
if (property.kind === "total") return unknown("unsupported", "termination is not observable on generated inputs; total needs a prover");
|
|
1253
|
+
const spec = input.spec;
|
|
1254
|
+
if (!spec) return unknown("unavailable", `the spec file's export ${property.name} was not loaded`);
|
|
1255
|
+
const binders = property.binders;
|
|
1256
|
+
const wanted = input.inputs ?? 100;
|
|
1257
|
+
if (!Number.isInteger(wanted) || wanted < 1) throw new RangeError(`inputs must be a positive integer, got ${String(input.inputs)}`);
|
|
1258
|
+
const seed = `${input.seed}:${module}:${fn.name}:${property.name}`;
|
|
1259
|
+
let state = seedPrng(seed);
|
|
1260
|
+
let held = 0;
|
|
1261
|
+
let discarded = 0;
|
|
1262
|
+
let sampledOut = 0;
|
|
1263
|
+
const seen = /* @__PURE__ */ new Set();
|
|
1264
|
+
const counterexample = (values, error) => ({
|
|
1265
|
+
binders: binders.map((b, i) => ({
|
|
1266
|
+
name: b.name,
|
|
1267
|
+
value: render(values[i])
|
|
1268
|
+
})),
|
|
1269
|
+
...error === void 0 ? {} : { error }
|
|
1270
|
+
});
|
|
1271
|
+
const edges = edgeLists(binders);
|
|
1272
|
+
const edgeTotal = edgeAssignmentCount(edges);
|
|
1273
|
+
const randomWanted = Math.ceil(wanted / 2);
|
|
1274
|
+
let randomHeld = 0;
|
|
1275
|
+
for (let draws = 0; draws < edgeTotal || (held < wanted || randomHeld < randomWanted) && draws < edgeTotal + wanted * 20; draws++) {
|
|
1276
|
+
let values;
|
|
1277
|
+
try {
|
|
1278
|
+
[values, state] = (draws < edgeTotal ? edgeAssignment(binders, edges, draws, state) : void 0) ?? generateAssignment(binders, state);
|
|
1279
|
+
} catch (err) {
|
|
1280
|
+
if (err instanceof CannotGenerate) return unknown(err.why, err.message);
|
|
1281
|
+
throw err;
|
|
1282
|
+
}
|
|
1283
|
+
const key = values.map(render).join(", ");
|
|
1284
|
+
if (seen.has(key)) continue;
|
|
1285
|
+
seen.add(key);
|
|
1286
|
+
if (spec.sample) {
|
|
1287
|
+
let inSample;
|
|
1288
|
+
try {
|
|
1289
|
+
inSample = spec.sample(...copy(values));
|
|
1290
|
+
} catch (err) {
|
|
1291
|
+
return unknown("unsupported", malformedReason(`the sample threw ${String(err)}`));
|
|
1292
|
+
}
|
|
1293
|
+
if (typeof inSample !== "boolean") return unknown("unsupported", malformedReason(`the sample returned ${render(inSample)}, not a boolean`));
|
|
1294
|
+
if (!inSample) {
|
|
1295
|
+
sampledOut++;
|
|
1296
|
+
continue;
|
|
1297
|
+
}
|
|
1298
|
+
}
|
|
1299
|
+
const result = evaluate(spec, values, true);
|
|
1300
|
+
if ("malformed" in result) return unknown("unsupported", malformedReason(result.malformed));
|
|
1301
|
+
if (result.outcome === "discarded") {
|
|
1302
|
+
discarded++;
|
|
1303
|
+
continue;
|
|
1304
|
+
}
|
|
1305
|
+
if (result.outcome === "refuted") {
|
|
1306
|
+
const smallest = shrinkRefutation(spec, binders, values, result);
|
|
1307
|
+
return {
|
|
1308
|
+
...base,
|
|
1309
|
+
kind: "REFUTED",
|
|
1310
|
+
rung: "TESTED",
|
|
1311
|
+
counterexample: counterexample(smallest.values, smallest.error),
|
|
1312
|
+
assumptions: [`found on an input generated with seed ${input.seed}, then shrunk`]
|
|
1313
|
+
};
|
|
1314
|
+
}
|
|
1315
|
+
held++;
|
|
1316
|
+
if (draws >= edgeTotal) randomHeld++;
|
|
1317
|
+
}
|
|
1318
|
+
if (held === 0) return unknown("gave_up", sampledOut > 0 && discarded === 0 ? `all ${sampledOut} distinct generated inputs were outside the sample: it admits too few of the parameter types to draw` : `all ${discarded + sampledOut} distinct generated inputs were outside the claim or the sample: its precondition admits too few of the parameter types to sample`);
|
|
1319
|
+
return {
|
|
1320
|
+
...base,
|
|
1321
|
+
kind: "TESTED",
|
|
1322
|
+
inputs: held,
|
|
1323
|
+
discarded,
|
|
1324
|
+
seed: input.seed,
|
|
1325
|
+
assumptions: [
|
|
1326
|
+
`held on ${held} distinct input(s) generated from the parameter types with seed ${input.seed}; inputs never drawn are not covered`,
|
|
1327
|
+
`${fn.name} was called on generated inputs outside the simulator, so it is assumed pure`,
|
|
1328
|
+
...spec.sample ? [`drawn only where the sample holds${property.sample ? ` (${property.sample.body.replace(/\s+/g, " ").trim()})` : ""}; the claim itself is not limited to it`] : []
|
|
1329
|
+
]
|
|
1330
|
+
};
|
|
1331
|
+
}
|
|
1332
|
+
// packages/prover/src/ladder.ts
|
|
1333
|
+
/** One property through the ladder's rungs. */
|
|
1334
|
+
function specLadder(input, options = {}) {
|
|
1335
|
+
if (options.checked === false) {
|
|
1336
|
+
if (!options.tested) throw new RangeError("a ladder without CHECKED needs the TESTED rung");
|
|
1337
|
+
return testedRung({
|
|
1338
|
+
module: input.module,
|
|
1339
|
+
fn: input.fn,
|
|
1340
|
+
property: input.property,
|
|
1341
|
+
spec: input.spec,
|
|
1342
|
+
...options.tested
|
|
1343
|
+
});
|
|
1344
|
+
}
|
|
1345
|
+
const checked = checkedRung(input);
|
|
1346
|
+
if (checked.kind === "REFUTED" || options.tested === false || options.tested === void 0) return checked;
|
|
1347
|
+
if (input.property.kind === "total") return checked;
|
|
1348
|
+
if (checked.kind === "UNKNOWN" && checked.reason.startsWith(malformedReason(""))) return checked;
|
|
1349
|
+
const tested = testedRung({
|
|
1350
|
+
module: input.module,
|
|
1351
|
+
fn: input.fn,
|
|
1352
|
+
property: input.property,
|
|
1353
|
+
spec: input.spec,
|
|
1354
|
+
...options.tested
|
|
1355
|
+
});
|
|
1356
|
+
if (tested.kind === "REFUTED") return tested;
|
|
1357
|
+
if (tested.kind === "UNKNOWN" && tested.reason.startsWith(malformedReason(""))) return tested;
|
|
1358
|
+
if (tested.kind === "TESTED") {
|
|
1359
|
+
if (checked.kind !== "CHECKED") return {
|
|
1360
|
+
...tested,
|
|
1361
|
+
assumptions: [...tested.assumptions, reason(checked)]
|
|
1362
|
+
};
|
|
1363
|
+
return {
|
|
1364
|
+
...tested,
|
|
1365
|
+
checked: checked.inputs,
|
|
1366
|
+
assumptions: [...tested.assumptions, ...checked.assumptions]
|
|
1367
|
+
};
|
|
1368
|
+
}
|
|
1369
|
+
if (checked.kind !== "UNKNOWN") return {
|
|
1370
|
+
...checked,
|
|
1371
|
+
assumptions: [...checked.assumptions, `not TESTED: ${reason(tested)}`]
|
|
1372
|
+
};
|
|
1373
|
+
if (reason(tested) === checked.reason) return checked;
|
|
1374
|
+
return {
|
|
1375
|
+
...checked,
|
|
1376
|
+
reason: `${checked.reason}; generated inputs: ${reason(tested)}`
|
|
1377
|
+
};
|
|
1378
|
+
}
|
|
1379
|
+
function reason(v) {
|
|
1380
|
+
return v.kind === "UNKNOWN" ? v.reason : v.kind;
|
|
1381
|
+
}
|
|
1382
|
+
/**
|
|
1383
|
+
* Every spec verdict for one module: each property through the ladder.
|
|
1384
|
+
* `props` is the spec file's namespace, `namespace` the module's (after
|
|
1385
|
+
* observation ended, so it holds the functions themselves); a spec whose
|
|
1386
|
+
* function is not the module's export of that name is not evaluated.
|
|
1387
|
+
*/
|
|
1388
|
+
function checkSpecModule(module, specs, props, namespace, observed, options = {}) {
|
|
1389
|
+
return specs.functions.flatMap((fn) => {
|
|
1390
|
+
const calls = observed.calls.filter((c) => c.function === fn.name);
|
|
1391
|
+
const unobservable = Object.hasOwn(observed.unobservable, fn.name) ? observed.unobservable[fn.name] : void 0;
|
|
1392
|
+
return fn.properties.map((property) => {
|
|
1393
|
+
const exported = Object.hasOwn(props, property.name) ? props[property.name] : void 0;
|
|
1394
|
+
let spec;
|
|
1395
|
+
let mismatch;
|
|
1396
|
+
if (!isSpec(exported)) mismatch = `the spec file exports no spec named ${property.name}`;
|
|
1397
|
+
else if (exported.fn !== namespace[fn.name]) mismatch = `${property.name} names a function that is not the module's export ${fn.name}`;
|
|
1398
|
+
else if (exported.kind === "forall") spec = exported;
|
|
1399
|
+
if (mismatch !== void 0) return {
|
|
1400
|
+
module,
|
|
1401
|
+
function: fn.name,
|
|
1402
|
+
property: property.name,
|
|
1403
|
+
text: property.text,
|
|
1404
|
+
kind: "UNKNOWN",
|
|
1405
|
+
cause: "unavailable",
|
|
1406
|
+
reason: mismatch,
|
|
1407
|
+
assumptions: []
|
|
1408
|
+
};
|
|
1409
|
+
return specLadder({
|
|
1410
|
+
module,
|
|
1411
|
+
fn,
|
|
1412
|
+
property,
|
|
1413
|
+
calls,
|
|
1414
|
+
spec,
|
|
1415
|
+
...unobservable === void 0 ? {} : { unobservable }
|
|
1416
|
+
}, options);
|
|
1417
|
+
});
|
|
1418
|
+
});
|
|
1419
|
+
}
|
|
1420
|
+
// packages/prover/src/provers.ts
|
|
1421
|
+
/**
|
|
1422
|
+
* The simulator's verdicts for a module (CHECKED, TESTED, REFUTED, UNKNOWN,
|
|
1423
|
+
* `total` included) combined with what the engines proved. A simulator
|
|
1424
|
+
* counterexample stands. One engine proving is `PROVEN`, the lower rungs'
|
|
1425
|
+
* counts kept beside it; an engine refuting where nothing else did is
|
|
1426
|
+
* `REFUTED` at the PROVEN rung; one proving and another refuting is
|
|
1427
|
+
* `CONFLICT`. An engine that gave up, could not run or does not take the
|
|
1428
|
+
* function leaves the verdict as it was, with why among the assumptions.
|
|
1429
|
+
*/
|
|
1430
|
+
function aggregate(module, verdicts, reports) {
|
|
1431
|
+
return verdicts.map((v) => {
|
|
1432
|
+
if (v.module !== module) return v;
|
|
1433
|
+
const says = reports.map((r) => ({
|
|
1434
|
+
engine: r.engine,
|
|
1435
|
+
outcome: r.proofs.results.find((p) => p.function === v.function && p.property === v.property)?.outcome
|
|
1436
|
+
})).filter((x) => x.outcome !== void 0);
|
|
1437
|
+
if (says.length === 0) return v;
|
|
1438
|
+
const proven = says.find((x) => x.outcome.kind === "PROVEN");
|
|
1439
|
+
const refuted = says.find((x) => x.outcome.kind === "REFUTED");
|
|
1440
|
+
if (v.kind === "REFUTED" || v.kind === "CONFLICT") {
|
|
1441
|
+
if (v.kind === "REFUTED" && proven?.outcome.kind === "PROVEN") return {
|
|
1442
|
+
module: v.module,
|
|
1443
|
+
function: v.function,
|
|
1444
|
+
property: v.property,
|
|
1445
|
+
text: v.text,
|
|
1446
|
+
kind: "CONFLICT",
|
|
1447
|
+
proven: proven.outcome.engine,
|
|
1448
|
+
refuted: {
|
|
1449
|
+
name: "simulator",
|
|
1450
|
+
version: v.rung,
|
|
1451
|
+
counterexample: v.counterexample
|
|
1452
|
+
},
|
|
1453
|
+
assumptions: [...v.assumptions, ...proven.outcome.assumptions]
|
|
1454
|
+
};
|
|
1455
|
+
return v;
|
|
1456
|
+
}
|
|
1457
|
+
if (proven?.outcome.kind === "PROVEN" && refuted?.outcome.kind === "REFUTED") return {
|
|
1458
|
+
module: v.module,
|
|
1459
|
+
function: v.function,
|
|
1460
|
+
property: v.property,
|
|
1461
|
+
text: v.text,
|
|
1462
|
+
kind: "CONFLICT",
|
|
1463
|
+
proven: proven.outcome.engine,
|
|
1464
|
+
refuted: {
|
|
1465
|
+
name: refuted.outcome.engine.name,
|
|
1466
|
+
version: refuted.outcome.engine.version,
|
|
1467
|
+
...refuted.outcome.counterexample ? { counterexample: refuted.outcome.counterexample } : {}
|
|
1468
|
+
},
|
|
1469
|
+
assumptions: [...proven.outcome.assumptions, ...refuted.outcome.assumptions]
|
|
1470
|
+
};
|
|
1471
|
+
if (refuted?.outcome.kind === "REFUTED") return {
|
|
1472
|
+
module: v.module,
|
|
1473
|
+
function: v.function,
|
|
1474
|
+
property: v.property,
|
|
1475
|
+
text: v.text,
|
|
1476
|
+
kind: "REFUTED",
|
|
1477
|
+
rung: "PROVEN",
|
|
1478
|
+
engine: refuted.outcome.engine,
|
|
1479
|
+
counterexample: refuted.outcome.counterexample ?? { binders: [] },
|
|
1480
|
+
assumptions: refuted.outcome.assumptions
|
|
1481
|
+
};
|
|
1482
|
+
if (proven?.outcome.kind === "PROVEN") return {
|
|
1483
|
+
module: v.module,
|
|
1484
|
+
function: v.function,
|
|
1485
|
+
property: v.property,
|
|
1486
|
+
text: v.text,
|
|
1487
|
+
kind: "PROVEN",
|
|
1488
|
+
engine: proven.outcome.engine,
|
|
1489
|
+
...v.kind === "TESTED" ? { tested: v.inputs } : {},
|
|
1490
|
+
...v.kind === "TESTED" && v.checked !== void 0 ? { checked: v.checked } : v.kind === "CHECKED" ? { checked: v.inputs } : {},
|
|
1491
|
+
assumptions: [...proven.outcome.assumptions, ...v.assumptions]
|
|
1492
|
+
};
|
|
1493
|
+
const notes = says.map((x) => x.outcome.kind === "UNKNOWN" ? `not PROVEN by ${x.engine}: ${x.outcome.cause}: ${x.outcome.reason}` : `${x.engine}: ${x.outcome.kind}`);
|
|
1494
|
+
return {
|
|
1495
|
+
...v,
|
|
1496
|
+
assumptions: [...v.assumptions, ...notes]
|
|
1497
|
+
};
|
|
1498
|
+
});
|
|
1499
|
+
}
|
|
1500
|
+
// packages/report/src/regression.ts
|
|
1501
|
+
/**
|
|
1502
|
+
* The Vitest regression emitter. Given a violation, produce a test file that
|
|
1503
|
+
* replays its shrunk schedule on the restricted scenario. By default the
|
|
1504
|
+
* test expects the invariants to hold, so it fails while the bug exists and
|
|
1505
|
+
* passes once it is fixed. With `expect: "violates"` it asserts the recorded
|
|
1506
|
+
* violation instead, which is how Eutaxy's own corpus exercises the emitter.
|
|
1507
|
+
*
|
|
1508
|
+
* The emitter knows nothing about any platform: the caller supplies the
|
|
1509
|
+
* import lines and the expression that builds the platform.
|
|
1510
|
+
*/
|
|
1511
|
+
function emitRegressionTest(verdict, options) {
|
|
1512
|
+
const engineModule = options.engineModule ?? "eutaxy";
|
|
1513
|
+
const mode = options.expect ?? "holds";
|
|
1514
|
+
const schedule = canonicalJson(verdict.schedule);
|
|
1515
|
+
const kept = canonicalJson([...verdict.keptEvents]);
|
|
1516
|
+
const keys = canonicalJson([...verdict.violation.keys]);
|
|
1517
|
+
const title = mode === "holds" ? `${verdict.scenario}: ${verdict.violation.invariant} holds on the recorded schedule` : `${verdict.scenario}: ${verdict.violation.invariant} is still violated on the recorded schedule`;
|
|
1518
|
+
const assertion = mode === "holds" ? ` expect(violations.map((v) => \`\${v.invariant}: \${v.message}\`)).toEqual([]);` : ` expect(violations.map((v) => [v.invariant, v.keys])).toContainEqual([${JSON.stringify(verdict.violation.invariant)}, ${keys}]);`;
|
|
1519
|
+
const observe = options.observe ?? [];
|
|
1520
|
+
const replayLines = observe.length === 0 ? [" const log = await replay(spec, platform, schedule);"] : [
|
|
1521
|
+
` const observers = [${observe.map((o) => `observeCore(${o.namespace}, ${JSON.stringify(o.functions)})`).join(", ")}];`,
|
|
1522
|
+
" const log = await replay(spec, platform, schedule).finally(() => {",
|
|
1523
|
+
" for (const o of observers) o.restore();",
|
|
1524
|
+
" });"
|
|
1525
|
+
];
|
|
1526
|
+
return [
|
|
1527
|
+
"// Generated by Eutaxy. Replays a recorded violation; regenerate rather than edit.",
|
|
1528
|
+
`// Scenario ${verdict.scenario}, seed ${String(verdict.seed)}: ${verdict.violation.message}`,
|
|
1529
|
+
`import { checkInvariants, ${observe.length > 0 ? "observeCore, " : ""}replay, restrictSpec } from ${JSON.stringify(engineModule)};`,
|
|
1530
|
+
...options.imports,
|
|
1531
|
+
"import { expect, test } from \"vitest\";",
|
|
1532
|
+
"",
|
|
1533
|
+
`const schedule = ${schedule} as const;`,
|
|
1534
|
+
`const keptEvents = ${kept} as const;`,
|
|
1535
|
+
"",
|
|
1536
|
+
`test(${JSON.stringify(title)}, async () => {`,
|
|
1537
|
+
` const platform = ${options.platform};`,
|
|
1538
|
+
` const spec = restrictSpec(platform, ${options.scenario}, keptEvents);`,
|
|
1539
|
+
...replayLines,
|
|
1540
|
+
" const violations = checkInvariants(log, spec.invariants);",
|
|
1541
|
+
assertion,
|
|
1542
|
+
"});",
|
|
1543
|
+
""
|
|
1544
|
+
].join("\n");
|
|
1545
|
+
}
|
|
1546
|
+
/**
|
|
1547
|
+
* The JSON report: one object, canonical, with a schema version at the top.
|
|
1548
|
+
* Spec verdicts, when there are any, sit beside the scenario's, never in it.
|
|
1549
|
+
*/
|
|
1550
|
+
function renderJson(verdict, specs = []) {
|
|
1551
|
+
return canonicalJson({
|
|
1552
|
+
schemaVersion: 1,
|
|
1553
|
+
generator: "eutaxy",
|
|
1554
|
+
verdict,
|
|
1555
|
+
...specs.length > 0 ? { specs } : {}
|
|
1556
|
+
});
|
|
1557
|
+
}
|
|
1558
|
+
const specsHeader = "Specs (CHECKED means held on the inputs the simulator passed, TESTED on generated ones too; neither is a proof. PROVEN is one, by the named engine)";
|
|
1559
|
+
/** Spec verdicts as text: one line each, a counterexample under a refutation. */
|
|
1560
|
+
function renderSpecs(specs) {
|
|
1561
|
+
if (specs.length === 0) return "";
|
|
1562
|
+
return [specsHeader, ...specs.flatMap(specLines)].join("\n");
|
|
1563
|
+
}
|
|
1564
|
+
/** The order a report counts verdicts in: strongest evidence first, then what needs attention. */
|
|
1565
|
+
const verdictOrder = [
|
|
1566
|
+
"PROVEN",
|
|
1567
|
+
"TESTED",
|
|
1568
|
+
"CHECKED",
|
|
1569
|
+
"REFUTED",
|
|
1570
|
+
"CONFLICT",
|
|
1571
|
+
"UNKNOWN"
|
|
1572
|
+
];
|
|
1573
|
+
/**
|
|
1574
|
+
* `eutaxy report` as text: the spec verdicts grouped by module, in the
|
|
1575
|
+
* order given, and one closing line counting them by verdict.
|
|
1576
|
+
*/
|
|
1577
|
+
function renderSpecReport(specs) {
|
|
1578
|
+
if (specs.length === 0) return "No specs found.";
|
|
1579
|
+
const modules = [...new Set(specs.map((v) => v.module))];
|
|
1580
|
+
const out = ["Specs (TESTED means held on inputs generated from the parameter types, which is not a proof; PROVEN is one, by the named engine)"];
|
|
1581
|
+
for (const m of modules) {
|
|
1582
|
+
out.push("", m);
|
|
1583
|
+
for (const v of specs) if (v.module === m) out.push(...specLines(v));
|
|
1584
|
+
}
|
|
1585
|
+
const counts = verdictOrder.map((k) => [k, specs.filter((v) => v.kind === k).length]).filter(([, n]) => n > 0).map(([k, n]) => `${n} ${k}`);
|
|
1586
|
+
out.push("", `${specs.length} propert${specs.length === 1 ? "y" : "ies"} in ${modules.length} module${modules.length === 1 ? "" : "s"}: ${counts.join(", ")}`);
|
|
1587
|
+
return out.join("\n");
|
|
1588
|
+
}
|
|
1589
|
+
/** `eutaxy report --json`: canonical, with the schema version, the spec verdicts and nothing else. */
|
|
1590
|
+
function renderSpecReportJson(specs) {
|
|
1591
|
+
return canonicalJson({
|
|
1592
|
+
schemaVersion: 1,
|
|
1593
|
+
generator: "eutaxy",
|
|
1594
|
+
specs
|
|
1595
|
+
});
|
|
1596
|
+
}
|
|
1597
|
+
function specLines(v) {
|
|
1598
|
+
const out = [];
|
|
1599
|
+
const what = `${v.function} ${v.property}`;
|
|
1600
|
+
switch (v.kind) {
|
|
1601
|
+
case "CHECKED":
|
|
1602
|
+
out.push(` CHECKED ${what}: held on ${v.inputs} input(s)${v.discarded > 0 ? `, ${v.discarded} outside the claim` : ""}`);
|
|
1603
|
+
break;
|
|
1604
|
+
case "TESTED":
|
|
1605
|
+
out.push(` TESTED ${what}: held on ${v.inputs} generated input(s)${v.checked === void 0 ? "" : ` and ${v.checked} the simulator passed`}${v.discarded > 0 ? `, ${v.discarded} outside the claim` : ""}`);
|
|
1606
|
+
break;
|
|
1607
|
+
case "PROVEN": {
|
|
1608
|
+
const beside = [...v.tested === void 0 ? [] : [`${v.tested} generated input(s)`], ...v.checked === void 0 ? [] : [`${v.checked} the simulator passed`]];
|
|
1609
|
+
out.push(` PROVEN ${what}: by ${v.engine.name} ${v.engine.version}${beside.length > 0 ? `; also held on ${beside.join(" and ")}` : ""}`);
|
|
1610
|
+
break;
|
|
1611
|
+
}
|
|
1612
|
+
case "CONFLICT":
|
|
1613
|
+
out.push(` CONFLICT ${what}: ${v.proven.name} ${v.proven.version} proves it, ${v.refuted.name} ${v.refuted.version} refutes it; not resolved automatically`);
|
|
1614
|
+
if (v.refuted.counterexample) out.push(` ${v.refuted.counterexample.binders.map((b) => `${b.name} = ${b.value}`).join(", ")}`);
|
|
1615
|
+
break;
|
|
1616
|
+
case "REFUTED": {
|
|
1617
|
+
const c = v.counterexample;
|
|
1618
|
+
const where = v.rung === "PROVEN" ? `by ${v.engine?.name ?? "an engine"}` : c.call === void 0 ? "on a generated input" : `at ${c.call}`;
|
|
1619
|
+
out.push(` REFUTED ${what}: false ${where}${c.error ? `, which threw ${c.error}` : ""}`);
|
|
1620
|
+
if (c.binders.length > 0) out.push(` ${c.binders.map((b) => `${b.name} = ${b.value}`).join(", ")}`);
|
|
1621
|
+
out.push(` ${v.text.replace(/\s+/g, " ")}`);
|
|
1622
|
+
break;
|
|
1623
|
+
}
|
|
1624
|
+
case "UNKNOWN": out.push(` UNKNOWN ${what}: ${v.cause}: ${v.reason}`);
|
|
1625
|
+
}
|
|
1626
|
+
for (const a of v.assumptions) if (v.kind === "PROVEN" || a.startsWith("not PROVEN by ")) out.push(` ${a}`);
|
|
1627
|
+
return out;
|
|
1628
|
+
}
|
|
1629
|
+
function clip(s, max) {
|
|
1630
|
+
return s.length <= max ? s : `${s.slice(0, max - 1)}…`;
|
|
1631
|
+
}
|
|
1632
|
+
function pad(s, width) {
|
|
1633
|
+
return s.length >= width ? s : s + " ".repeat(width - s.length);
|
|
1634
|
+
}
|
|
1635
|
+
/** The trace as a fixed-width table; evidence rows are marked with `*`. */
|
|
1636
|
+
function renderTrace(log, evidence = []) {
|
|
1637
|
+
const rows = log.entries.map((e) => [
|
|
1638
|
+
evidence.includes(e.seq) ? "*" : " ",
|
|
1639
|
+
String(e.seq),
|
|
1640
|
+
e.invocation,
|
|
1641
|
+
e.kind,
|
|
1642
|
+
e.target,
|
|
1643
|
+
e.op,
|
|
1644
|
+
clip(canonicalJson(e.payload), 72)
|
|
1645
|
+
]);
|
|
1646
|
+
const header = [
|
|
1647
|
+
" ",
|
|
1648
|
+
"seq",
|
|
1649
|
+
"invocation",
|
|
1650
|
+
"kind",
|
|
1651
|
+
"target",
|
|
1652
|
+
"op",
|
|
1653
|
+
"payload"
|
|
1654
|
+
];
|
|
1655
|
+
const widths = header.map((h, i) => Math.max(h.length, ...rows.map((r) => r[i].length)));
|
|
1656
|
+
const line = (cells) => cells.map((c, i) => i === cells.length - 1 ? c : pad(c, widths[i])).join(" ").trimEnd();
|
|
1657
|
+
return [line(header), ...rows.map(line)].join("\n");
|
|
1658
|
+
}
|
|
1659
|
+
/** A readable report: verdict, what it rests on, and for a violation the schedule and trace. */
|
|
1660
|
+
function renderText(verdict) {
|
|
1661
|
+
const out = [];
|
|
1662
|
+
const r = verdict.reached;
|
|
1663
|
+
out.push(`${verdict.kind} ${verdict.scenario} (seed ${String(verdict.seed)})`);
|
|
1664
|
+
switch (verdict.kind) {
|
|
1665
|
+
case "VIOLATED":
|
|
1666
|
+
out.push("");
|
|
1667
|
+
out.push(`Invariant ${verdict.violation.invariant} is violated: ${verdict.violation.message}`);
|
|
1668
|
+
out.push(`Keys: ${verdict.violation.keys.join(", ")}`);
|
|
1669
|
+
out.push("");
|
|
1670
|
+
out.push(`Schedule (run seed ${verdict.schedule.seed}), events kept: ${verdict.keptEvents.join(", ")}`);
|
|
1671
|
+
if (verdict.schedule.faults.length === 0) out.push(" no injected faults");
|
|
1672
|
+
for (const f of verdict.schedule.faults) out.push(` ${f.fault} at ${f.invocation} effect #${f.ordinal} (${f.target} ${f.op})`);
|
|
1673
|
+
out.push(`Shrunk: ${verdict.shrunk.eventsRemoved} event(s) and ${verdict.shrunk.faultsRemoved} fault(s) removed in ${verdict.shrunk.runs} run(s)`);
|
|
1674
|
+
out.push("");
|
|
1675
|
+
out.push("Trace:");
|
|
1676
|
+
out.push(renderTrace(verdict.trace, verdict.violation.evidence));
|
|
1677
|
+
break;
|
|
1678
|
+
case "BOUNDED": {
|
|
1679
|
+
const b = verdict.bounds;
|
|
1680
|
+
out.push("");
|
|
1681
|
+
out.push("No violation found within the stated bounds. This is not a proof.");
|
|
1682
|
+
out.push(`Bounds: faults per run ≤ ${b.maxFaultsPerRun}, redeliveries ≤ ${b.maxRedeliveries}, concurrent invocations ≤ ${b.maxConcurrentInvocations}, budget ${b.budgetMs} ms`);
|
|
1683
|
+
break;
|
|
1684
|
+
}
|
|
1685
|
+
case "UNSUPPORTED":
|
|
1686
|
+
out.push("");
|
|
1687
|
+
out.push(`Eutaxy could not decide this scenario (${verdict.cause}): ${verdict.reason}`);
|
|
1688
|
+
}
|
|
1689
|
+
out.push("");
|
|
1690
|
+
out.push(`Explored: ${r.runs} run(s), ${r.effectPoints} effect point(s), ${r.faultsInjected} fault(s) injected, ${r.elapsedMs} ms`);
|
|
1691
|
+
const e = r.exhaustive;
|
|
1692
|
+
if (e) {
|
|
1693
|
+
const stopped = {
|
|
1694
|
+
VIOLATED: "stopped at the first violation",
|
|
1695
|
+
BOUNDED: "stopped at the budget before every schedule ran",
|
|
1696
|
+
UNSUPPORTED: "stopped where the scenario could not be decided"
|
|
1697
|
+
}[verdict.kind];
|
|
1698
|
+
out.push(`Exhaustive: ${e.complete ? "every schedule within the bounds ran" : stopped} (${e.schedules} schedules${e.reduction ? ", partial-order reduction" : ""})`);
|
|
1699
|
+
}
|
|
1700
|
+
out.push("Assumptions:");
|
|
1701
|
+
for (const a of verdict.assumptions) out.push(` - ${a}`);
|
|
1702
|
+
return out.join("\n");
|
|
1703
|
+
}
|
|
1704
|
+
export { endpointNumber as _, renderText as a, describeVerdict as b, aggregate as c, testedRung as d, shrinkRefutation as f, endpointBigInt as g, conforms as h, renderSpecs as i, checkSpecModule as l, evaluate as m, renderSpecReport as n, renderTrace as o, checkedRung as p, renderSpecReportJson as r, emitRegressionTest as s, renderJson as t, specLadder as u, render as v, describeType as y };
|
|
1705
|
+
|
|
1706
|
+
//# sourceMappingURL=src-BZtuuu_j.mjs.map
|