@milkenjoyer/toolchain 0.11.0 → 0.12.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 +98 -14
- package/dist/conformance.d.mts +18 -0
- package/dist/conformance.mjs +31 -0
- package/dist/effect-plugin.mjs +1 -1
- package/dist/exceptions-Dybl9xpc.mjs +11 -0
- package/dist/exceptions.mjs +1 -1
- package/dist/foldkit-plugin.mjs +1 -1
- package/dist/formal.d.mts +131 -3
- package/dist/formal.mjs +716 -65
- package/dist/guard.mjs +2 -2
- package/dist/{lint-JSnPsGO1.mjs → lint-IhmtnBbx.mjs} +3 -3
- package/dist/lint.mjs +1 -1
- package/dist/plugin.mjs +1 -1
- package/dist/proof.mjs +1 -1
- package/dist/trace-file-KuPRWgwt.mjs +17 -0
- package/dist/verdict.d.mts +1 -1
- package/dist/verdict.mjs +1 -1
- package/dist/{verification-p2rSx5lc.d.mts → verification-FgQFU1YN.d.mts} +1 -1
- package/dist/verification.d.mts +1 -1
- package/dist/verification.mjs +1 -1
- package/package.json +6 -2
- package/src/formal/cfg.ts +58 -1
- package/src/formal/checks.ts +39 -52
- package/src/formal/coverage.ts +79 -0
- package/src/formal/gate.ts +74 -0
- package/src/formal/git.ts +38 -0
- package/src/formal/proofs.ts +217 -0
- package/src/formal/semantics.ts +110 -0
- package/src/formal/specs.ts +80 -34
- package/src/formal/tla.ts +24 -2
- package/src/formal/trace-file.ts +35 -0
- package/src/formal/traces.ts +293 -0
- package/src/formal/violations.ts +76 -0
- package/src/formal.ts +9 -2
- package/dist/exceptions-Bq-9MxoR.mjs +0 -3
- package/dist/{effect-plugin-kYxB-5LJ.mjs → effect-plugin-BXUx9PPu.mjs} +1 -1
- package/dist/{plugin-BoBRGtGf.mjs → plugin-CwxhPtui.mjs} +1 -1
- package/dist/{verification-6s2chr4X.mjs → verification-oAc1Q6I9.mjs} +1 -1
package/dist/formal.mjs
CHANGED
|
@@ -1,22 +1,25 @@
|
|
|
1
1
|
#!/usr/bin/env bun
|
|
2
|
+
import { i as sorted, r as receiptPath, t as FORMAL_TRACES } from "./trace-file-KuPRWgwt.mjs";
|
|
2
3
|
import * as Data from "effect/Data";
|
|
3
4
|
import * as Option from "effect/Option";
|
|
4
5
|
import * as Schema from "effect/Schema";
|
|
6
|
+
import * as Config from "effect/Config";
|
|
5
7
|
import * as Effect from "effect/Effect";
|
|
6
8
|
import * as Result from "effect/Result";
|
|
9
|
+
import { basename, dirname, join, relative } from "node:path";
|
|
7
10
|
import { NodeServices } from "@effect/platform-node";
|
|
8
11
|
import * as Cause from "effect/Cause";
|
|
9
12
|
import * as Console from "effect/Console";
|
|
10
13
|
import * as Exit from "effect/Exit";
|
|
14
|
+
import * as Layer from "effect/Layer";
|
|
11
15
|
import { ChildProcess, ChildProcessSpawner } from "effect/process";
|
|
12
16
|
import * as Stream from "effect/Stream";
|
|
13
17
|
import { FileSystem } from "effect/FileSystem";
|
|
18
|
+
import * as Cache from "effect/Cache";
|
|
19
|
+
import * as Context from "effect/Context";
|
|
14
20
|
import { createHash } from "node:crypto";
|
|
15
21
|
import * as Clock from "effect/Clock";
|
|
16
|
-
import * as Config from "effect/Config";
|
|
17
|
-
import * as Context from "effect/Context";
|
|
18
22
|
import { FetchHttpClient, HttpClient } from "effect/http";
|
|
19
|
-
import * as Layer from "effect/Layer";
|
|
20
23
|
import { Path } from "effect/Path";
|
|
21
24
|
import * as SchemaIssue from "effect/SchemaIssue";
|
|
22
25
|
// src/formal/cfg.ts
|
|
@@ -42,6 +45,8 @@ const keywords = /* @__PURE__ */ new Set([
|
|
|
42
45
|
]);
|
|
43
46
|
const parseCfg = (source) => {
|
|
44
47
|
const tokens = source.replaceAll(/\(\*[\s\S]*?\*\)|\\\*[^\n]*/gu, " ").match(/"(?:\\.|[^"\\])*"|<-|[A-Za-z_][A-Za-z0-9_!]*|-?\d+|[^\s]/gu) ?? [];
|
|
48
|
+
const sections = [];
|
|
49
|
+
const properties = [];
|
|
45
50
|
const constants = [];
|
|
46
51
|
const invariants = [];
|
|
47
52
|
const checkDeadlock = [];
|
|
@@ -55,6 +60,10 @@ const parseCfg = (source) => {
|
|
|
55
60
|
if (keyword === void 0 || !keywords.has(keyword)) return fail(`unknown config keyword ${keyword}`);
|
|
56
61
|
const operands = [];
|
|
57
62
|
while (i < tokens.length && !keywords.has(tokens[i] ?? "")) operands.push(tokens[i++] ?? "");
|
|
63
|
+
sections.push({
|
|
64
|
+
keyword,
|
|
65
|
+
text: operands.join(" ")
|
|
66
|
+
});
|
|
58
67
|
if (operands.length === 0) return fail(`missing operand for ${keyword}`);
|
|
59
68
|
if (keyword === "CONSTANT" || keyword === "CONSTANTS") {
|
|
60
69
|
let j = 0;
|
|
@@ -89,16 +98,104 @@ const parseCfg = (source) => {
|
|
|
89
98
|
checkDeadlock.push(operands[0] === "TRUE");
|
|
90
99
|
} else {
|
|
91
100
|
if (!operands.every((token) => /^[A-Za-z_][A-Za-z0-9_!]*$/u.test(token))) return fail(`invalid operands for ${keyword}`);
|
|
101
|
+
if (keyword.startsWith("PROPERT")) properties.push(...operands);
|
|
92
102
|
if (keyword.startsWith("INVARIANT")) invariants.push(...operands);
|
|
93
103
|
}
|
|
94
104
|
}
|
|
95
105
|
return {
|
|
106
|
+
sections,
|
|
96
107
|
constants,
|
|
97
108
|
invariants,
|
|
109
|
+
properties,
|
|
98
110
|
checkDeadlock
|
|
99
111
|
};
|
|
100
112
|
};
|
|
101
113
|
const integer = (constant) => constant._tag === "Value" && /^-?\d+$/u.test(constant.value) ? BigInt(constant.value) : void 0;
|
|
114
|
+
const derive = (cfg, plan) => {
|
|
115
|
+
const removed = /* @__PURE__ */ new Set([
|
|
116
|
+
"INVARIANT",
|
|
117
|
+
"INVARIANTS",
|
|
118
|
+
"PROPERTY",
|
|
119
|
+
"PROPERTIES"
|
|
120
|
+
]);
|
|
121
|
+
if (plan.simulation) for (const k of [
|
|
122
|
+
"INIT",
|
|
123
|
+
"NEXT",
|
|
124
|
+
"SPECIFICATION",
|
|
125
|
+
"SYMMETRY",
|
|
126
|
+
"VIEW",
|
|
127
|
+
"ALIAS",
|
|
128
|
+
"POSTCONDITION",
|
|
129
|
+
"CHECK_DEADLOCK"
|
|
130
|
+
]) removed.add(k);
|
|
131
|
+
const lines = cfg.sections.filter((s) => !removed.has(s.keyword)).map((s) => s.keyword + " " + s.text);
|
|
132
|
+
if (plan.target) lines.push((plan.target.level < 2 ? "INVARIANT " : "PROPERTY ") + plan.target.name);
|
|
133
|
+
if (plan.simulation) {
|
|
134
|
+
const s = plan.simulation;
|
|
135
|
+
lines.push("CONSTANTS", ...s.overrides, "INIT " + s.init, "NEXT " + s.next, "INVARIANT Formal_Export", "CHECK_DEADLOCK FALSE");
|
|
136
|
+
}
|
|
137
|
+
return lines.join("\n") + "\n";
|
|
138
|
+
};
|
|
139
|
+
// src/formal/coverage.ts
|
|
140
|
+
const span = (text) => {
|
|
141
|
+
const m = /line (\d+), col (\d+) to line (\d+), col (\d+) of module (\w+)/u.exec(text);
|
|
142
|
+
return m ? {
|
|
143
|
+
line: Number(m[1]),
|
|
144
|
+
col: Number(m[2]),
|
|
145
|
+
endLine: Number(m[3]),
|
|
146
|
+
endCol: Number(m[4]),
|
|
147
|
+
module: m[5] ?? ""
|
|
148
|
+
} : void 0;
|
|
149
|
+
};
|
|
150
|
+
const coverage = (messages) => {
|
|
151
|
+
const start = messages.findLastIndex((m) => m.code === 2201);
|
|
152
|
+
const end = messages.findIndex((m, i) => i > start && (m.code === 2202 || m.code === 2777));
|
|
153
|
+
if (start < 0 || end < 0) return "missing final coverage report";
|
|
154
|
+
const actions = [];
|
|
155
|
+
const zeros = [];
|
|
156
|
+
let scope;
|
|
157
|
+
let zeroDepth;
|
|
158
|
+
for (const m of messages.slice(start + 1, end)) {
|
|
159
|
+
if (m.code === 2779) continue;
|
|
160
|
+
if (m.code === 2772 || m.code === 2773) {
|
|
161
|
+
const loc = span(m.text);
|
|
162
|
+
const match = /^<(\w+) .*: ([\d,]+):([\d,]+)$/u.exec(m.text);
|
|
163
|
+
if (!loc || !match) return `unparseable coverage ${m.code}: ${m.text}`;
|
|
164
|
+
const name = match[1] ?? "";
|
|
165
|
+
const generated = BigInt((match[3] ?? "0").replaceAll(",", ""));
|
|
166
|
+
scope = {
|
|
167
|
+
name,
|
|
168
|
+
enabled: generated > 0n
|
|
169
|
+
};
|
|
170
|
+
zeroDepth = void 0;
|
|
171
|
+
if (m.code === 2772) actions.push({
|
|
172
|
+
name,
|
|
173
|
+
span: loc,
|
|
174
|
+
generated
|
|
175
|
+
});
|
|
176
|
+
} else if (m.code === 2221 || m.code === 2775) {
|
|
177
|
+
const loc = span(m.text);
|
|
178
|
+
const count = /: ([\d,]+)(?::[\d,]+)?$/u.exec(m.text);
|
|
179
|
+
if (!loc || !count) return `unparseable coverage ${m.code}: ${m.text}`;
|
|
180
|
+
const depth = m.text.indexOf("line");
|
|
181
|
+
if (zeroDepth !== void 0 && depth <= zeroDepth) zeroDepth = void 0;
|
|
182
|
+
if (scope?.enabled === true && BigInt((count[1] ?? "0").replaceAll(",", "")) === 0n && zeroDepth === void 0) {
|
|
183
|
+
zeros.push({
|
|
184
|
+
scope: scope.name,
|
|
185
|
+
span: loc
|
|
186
|
+
});
|
|
187
|
+
zeroDepth = depth;
|
|
188
|
+
}
|
|
189
|
+
} else if (m.code === 2774 || m.code === 2778) {
|
|
190
|
+
scope = void 0;
|
|
191
|
+
zeroDepth = void 0;
|
|
192
|
+
} else return `unparseable coverage ${m.code}: ${m.text}`;
|
|
193
|
+
}
|
|
194
|
+
return {
|
|
195
|
+
actions,
|
|
196
|
+
zeros
|
|
197
|
+
};
|
|
198
|
+
};
|
|
102
199
|
// src/formal/semantics.ts
|
|
103
200
|
const child = (node, tag) => node.children.find((n) => n.tag === tag);
|
|
104
201
|
const value = (node, tag) => child(node, tag)?.text.trim() ?? "";
|
|
@@ -195,7 +292,67 @@ const fromXml = (xml) => {
|
|
|
195
292
|
};
|
|
196
293
|
const typeOK = module.children.filter((n) => n.tag === "UserDefinedOpKindRef").map((ref) => entries.get(value(ref, "UID"))).find((node) => node !== void 0 && value(node, "uniquename") === "TypeOK");
|
|
197
294
|
const body = typeOK && child(typeOK, "body")?.children[0];
|
|
295
|
+
const definitions = [...entries.values()].filter((n) => n?.tag === "UserDefinedOpKind" && closure.has(value(child(n, "location") ?? n, "filename")));
|
|
296
|
+
const operators = /* @__PURE__ */ new Map();
|
|
297
|
+
const fairness = [];
|
|
298
|
+
const specifications = /* @__PURE__ */ new Map();
|
|
299
|
+
const opName = (n) => {
|
|
300
|
+
const t = target(n);
|
|
301
|
+
return t ? value(entries.get(t.uid) ?? n, "uniquename") : "";
|
|
302
|
+
};
|
|
303
|
+
for (const n of definitions) {
|
|
304
|
+
const name = value(n, "uniquename");
|
|
305
|
+
const params = (child(n, "params")?.children ?? []).map((ref) => entries.get(value(child(ref, "FormalParamNodeRef") ?? ref, "UID")) ?? ref).map((p) => ({
|
|
306
|
+
name: value(p, "uniquename"),
|
|
307
|
+
arity: Number(value(p, "arity"))
|
|
308
|
+
}));
|
|
309
|
+
operators.set(name, {
|
|
310
|
+
name,
|
|
311
|
+
level: Number(value(n, "level")),
|
|
312
|
+
params,
|
|
313
|
+
module: value(child(n, "location") ?? n, "filename")
|
|
314
|
+
});
|
|
315
|
+
if (value(child(n, "location") ?? n, "filename") === value(modules, "RootModule")) for (const a of descendants(child(n, "body") ?? n)) {
|
|
316
|
+
const kind = opName(a);
|
|
317
|
+
if (!["$WF", "$SF"].includes(kind)) continue;
|
|
318
|
+
const sub = operands(a)[0];
|
|
319
|
+
fairness.push({
|
|
320
|
+
kind: `${kind.slice(1)}_${sub && opName(sub) === "$Tuple" ? `<< ${operands(sub).map(opName).join(", ")} >>` : sub ? opName(sub) : "?"}`,
|
|
321
|
+
line: value(child(child(a, "location") ?? a, "line") ?? a, "begin"),
|
|
322
|
+
valid: sub !== void 0 && opName(sub) === "vars" && operands(sub).length === 0
|
|
323
|
+
});
|
|
324
|
+
}
|
|
325
|
+
const flatten = (a, seen = /* @__PURE__ */ new Set()) => {
|
|
326
|
+
const t = target(a);
|
|
327
|
+
if (["$ConjList", "\\land"].includes(opName(a))) return operands(a).flatMap((x) => flatten(x, new Set(seen)));
|
|
328
|
+
if (t && t.ref.tag === "UserDefinedOpKindRef" && Number(value(a, "level")) === 3 && operands(a).length === 0 && !seen.has(t.uid)) {
|
|
329
|
+
const b = child(entries.get(t.uid) ?? a, "body")?.children[0];
|
|
330
|
+
if (b) {
|
|
331
|
+
seen.add(t.uid);
|
|
332
|
+
return flatten(b, seen);
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
return [a];
|
|
336
|
+
};
|
|
337
|
+
const body = child(n, "body")?.children[0];
|
|
338
|
+
if (!body) continue;
|
|
339
|
+
const conjuncts = flatten(body);
|
|
340
|
+
const state = conjuncts.filter((a) => Number(value(a, "level")) === 1);
|
|
341
|
+
const boxes = conjuncts.filter((a) => opName(a) === "[]");
|
|
342
|
+
const box = boxes[0] && operands(boxes[0])[0];
|
|
343
|
+
const next = box && opName(box) === "$SquareAct" ? operands(box)[0] : void 0;
|
|
344
|
+
if (state.length === 1 && boxes.length === 1 && next && operands(next).length === 0) {
|
|
345
|
+
const init = state[0] && operands(state[0]).length === 0 ? opName(state[0]) : void 0;
|
|
346
|
+
if (init !== void 0 && init !== "") specifications.set(name, {
|
|
347
|
+
init,
|
|
348
|
+
next: opName(next)
|
|
349
|
+
});
|
|
350
|
+
}
|
|
351
|
+
}
|
|
198
352
|
return {
|
|
353
|
+
operators,
|
|
354
|
+
fairness,
|
|
355
|
+
specifications,
|
|
199
356
|
variables: [...new Set(variables.values())],
|
|
200
357
|
typeOK: typeOK && new Set(body ? typed(body, /* @__PURE__ */ new Set()) : [])
|
|
201
358
|
};
|
|
@@ -219,7 +376,7 @@ const messages = (output) => [...output.matchAll(/@!@!@STARTMSG (\d+):([0-4]) @!
|
|
|
219
376
|
text: (match[3] ?? "").trim()
|
|
220
377
|
}));
|
|
221
378
|
const sanyReport = (output) => output.split("\n").filter((line) => !/^(?:\*{6} SANY2 Version|Parsing file |Semantic processing of module |Linting of module )/u.test(line)).join("\n").trim();
|
|
222
|
-
const make = Effect.gen(function* () {
|
|
379
|
+
const make$1 = Effect.gen(function* () {
|
|
223
380
|
const fs = yield* FileSystem;
|
|
224
381
|
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
|
|
225
382
|
const path = yield* Path;
|
|
@@ -344,7 +501,19 @@ const make = Effect.gen(function* () {
|
|
|
344
501
|
"tlc2.TLC",
|
|
345
502
|
"-tool",
|
|
346
503
|
"-workers",
|
|
347
|
-
"auto",
|
|
504
|
+
input.mode._tag === "Simulate" ? "1" : "auto",
|
|
505
|
+
...input.mode._tag === "Simulate" ? [
|
|
506
|
+
"-simulate",
|
|
507
|
+
`num=${input.mode.num}`,
|
|
508
|
+
"-depth",
|
|
509
|
+
String(input.mode.depth),
|
|
510
|
+
"-seed",
|
|
511
|
+
String(input.mode.seed)
|
|
512
|
+
] : [
|
|
513
|
+
"-lncheck",
|
|
514
|
+
"final",
|
|
515
|
+
...input.mode.coverage ? ["-coverage", "60"] : []
|
|
516
|
+
],
|
|
348
517
|
"-metadir",
|
|
349
518
|
temp,
|
|
350
519
|
"-noGenerateSpecTE",
|
|
@@ -365,8 +534,453 @@ const make = Effect.gen(function* () {
|
|
|
365
534
|
};
|
|
366
535
|
}).pipe(Effect.mapError(error));
|
|
367
536
|
var Tla = class Tla extends Context.Service()("formal/Tla") {
|
|
368
|
-
static layer = Layer.effect(Tla, make);
|
|
537
|
+
static layer = Layer.effect(Tla, make$1);
|
|
369
538
|
};
|
|
539
|
+
// src/formal/gate.ts
|
|
540
|
+
const make = Effect.gen(function* () {
|
|
541
|
+
const tla = yield* Tla;
|
|
542
|
+
const run = yield* Cache.make({
|
|
543
|
+
capacity: 1e4,
|
|
544
|
+
lookup: Effect.fn("formal.gate")(function* (m) {
|
|
545
|
+
const r = yield* tla.tlc({
|
|
546
|
+
spec: `${m.root}/${m.path}`,
|
|
547
|
+
config: `${m.root}/${m.config.path}`,
|
|
548
|
+
timeoutSeconds: m.entry.timeoutSeconds,
|
|
549
|
+
mode: {
|
|
550
|
+
_tag: "Check",
|
|
551
|
+
coverage: true
|
|
552
|
+
}
|
|
553
|
+
});
|
|
554
|
+
if (r._tag === "TimedOut") return {
|
|
555
|
+
_tag: "Failed",
|
|
556
|
+
message: `TLC timed out after ${m.entry.timeoutSeconds} s`
|
|
557
|
+
};
|
|
558
|
+
const failures = r.messages.filter((x) => x.severity >= 1 && x.severity <= 3);
|
|
559
|
+
const states = r.messages.find((x) => x.code === 2199)?.text.match(/([\d,]+) distinct states/u)?.[1]?.replaceAll(",", "");
|
|
560
|
+
if (r.code !== 0 || failures.length || !r.messages.some((x) => x.code === 2193) || states === void 0 || BigInt(states) === 0n) {
|
|
561
|
+
const meaning = /* @__PURE__ */ new Map([
|
|
562
|
+
[11, "deadlock"],
|
|
563
|
+
[12, "safety violation"],
|
|
564
|
+
[13, "liveness violation"],
|
|
565
|
+
[150, "parse failure"]
|
|
566
|
+
]);
|
|
567
|
+
return {
|
|
568
|
+
_tag: "Failed",
|
|
569
|
+
message: `TLC exited ${r.code}${meaning.has(r.code) ? ` (${meaning.get(r.code)})` : ""}; ${failures.length ? failures.map((x) => `${x.severity === 3 ? "warning" : "error"} ${x.code}: ${x.text}`).join("; ") : "requires 2193 success and nonzero distinct states (2199)"}`
|
|
570
|
+
};
|
|
571
|
+
}
|
|
572
|
+
const c = coverage(r.messages);
|
|
573
|
+
return typeof c === "string" ? {
|
|
574
|
+
_tag: "Failed",
|
|
575
|
+
message: c
|
|
576
|
+
} : {
|
|
577
|
+
_tag: "Passed",
|
|
578
|
+
states,
|
|
579
|
+
seconds: r.seconds,
|
|
580
|
+
coverage: c
|
|
581
|
+
};
|
|
582
|
+
})
|
|
583
|
+
});
|
|
584
|
+
return { run: (model) => Cache.get(run, model) };
|
|
585
|
+
});
|
|
586
|
+
var Gate = class Gate extends Context.Service()("formal/Gate") {
|
|
587
|
+
static layer = Layer.effect(Gate, make);
|
|
588
|
+
};
|
|
589
|
+
// src/formal/git.ts
|
|
590
|
+
const changes = Effect.fn("formal.changes")(function* (root) {
|
|
591
|
+
const base = yield* Config.String("BASE").pipe(Config.withDefault("origin/main"));
|
|
592
|
+
const run = (args) => Effect.scoped(Effect.gen(function* () {
|
|
593
|
+
const p = yield* ChildProcess.make("git", args, {
|
|
594
|
+
cwd: root,
|
|
595
|
+
stdin: "ignore"
|
|
596
|
+
});
|
|
597
|
+
const [out, , code] = yield* Effect.all([
|
|
598
|
+
p.stdout.pipe(Stream.decodeText, Stream.mkString),
|
|
599
|
+
p.stderr.pipe(Stream.decodeText, Stream.mkString),
|
|
600
|
+
p.exitCode
|
|
601
|
+
], { concurrency: "unbounded" });
|
|
602
|
+
return {
|
|
603
|
+
out,
|
|
604
|
+
code: Number(code)
|
|
605
|
+
};
|
|
606
|
+
}));
|
|
607
|
+
const merge = yield* run([
|
|
608
|
+
"merge-base",
|
|
609
|
+
base,
|
|
610
|
+
"HEAD"
|
|
611
|
+
]);
|
|
612
|
+
if (merge.code !== 0) return {
|
|
613
|
+
_tag: "All",
|
|
614
|
+
base
|
|
615
|
+
};
|
|
616
|
+
const sha = merge.out.trim();
|
|
617
|
+
const diff = yield* run([
|
|
618
|
+
"diff",
|
|
619
|
+
"--name-only",
|
|
620
|
+
"-z",
|
|
621
|
+
sha
|
|
622
|
+
]);
|
|
623
|
+
return diff.code !== 0 ? {
|
|
624
|
+
_tag: "All",
|
|
625
|
+
base
|
|
626
|
+
} : {
|
|
627
|
+
_tag: "Changed",
|
|
628
|
+
sha,
|
|
629
|
+
files: diff.out.split("\0").filter(Boolean)
|
|
630
|
+
};
|
|
631
|
+
});
|
|
632
|
+
// src/formal/violations.ts
|
|
633
|
+
const sandbox = Effect.fn("formal.sandbox")(function* (repo, model) {
|
|
634
|
+
const fs = yield* FileSystem;
|
|
635
|
+
const dir = yield* fs.makeTempDirectoryScoped({ prefix: "formal-model-" });
|
|
636
|
+
for (const file of repo.tracked.filter((f) => dirname(f) === dirname(model.path) && f.endsWith(".tla"))) yield* fs.copyFile(`${repo.root}/${file}`, `${dir}/${basename(file)}`);
|
|
637
|
+
return dir;
|
|
638
|
+
});
|
|
639
|
+
const held = Symbol("held");
|
|
640
|
+
const violate = Effect.fn("formal.violate")(function* (repo, m, target, edit) {
|
|
641
|
+
return yield* Effect.scoped(Effect.gen(function* () {
|
|
642
|
+
const fs = yield* FileSystem;
|
|
643
|
+
const tla = yield* Tla;
|
|
644
|
+
const dir = yield* sandbox(repo, m);
|
|
645
|
+
let cfg = m.config.cfg;
|
|
646
|
+
if (edit) {
|
|
647
|
+
const file = edit.file ?? m.path;
|
|
648
|
+
const changed = (yield* fs.readFileString(`${repo.root}/${file}`)).replace(edit.find, edit.replace);
|
|
649
|
+
if (file === m.config.path) {
|
|
650
|
+
const parsed = parseCfg(changed);
|
|
651
|
+
if ("_tag" in parsed) return parsed.message;
|
|
652
|
+
cfg = parsed;
|
|
653
|
+
} else yield* fs.writeFileString(`${dir}/${basename(file)}`, changed);
|
|
654
|
+
}
|
|
655
|
+
const level = m.semantics.operators.get(target)?.level ?? (cfg.properties.includes(target) ? 3 : 1);
|
|
656
|
+
const config = `${dir}/Check.cfg`;
|
|
657
|
+
yield* fs.writeFileString(config, derive(cfg, { target: {
|
|
658
|
+
name: target,
|
|
659
|
+
level
|
|
660
|
+
} }));
|
|
661
|
+
const run = yield* tla.tlc({
|
|
662
|
+
spec: `${dir}/${basename(m.path)}`,
|
|
663
|
+
config,
|
|
664
|
+
timeoutSeconds: m.entry.timeoutSeconds,
|
|
665
|
+
mode: {
|
|
666
|
+
_tag: "Check",
|
|
667
|
+
coverage: false
|
|
668
|
+
}
|
|
669
|
+
});
|
|
670
|
+
if (run._tag === "TimedOut") return `TLC timed out after ${m.entry.timeoutSeconds} s`;
|
|
671
|
+
if (run.code === 0) return held;
|
|
672
|
+
const violated = [
|
|
673
|
+
2107,
|
|
674
|
+
2108,
|
|
675
|
+
2110,
|
|
676
|
+
2112,
|
|
677
|
+
2116
|
|
678
|
+
];
|
|
679
|
+
if ([12, 13].includes(run.code) && run.messages.some((x) => violated.includes(x.code)) && !run.messages.some((x) => x.severity >= 1 && x.severity <= 3 && ![
|
|
680
|
+
...violated,
|
|
681
|
+
2121,
|
|
682
|
+
2264
|
|
683
|
+
].includes(x.code))) return void 0;
|
|
684
|
+
return `TLC exited ${run.code}; ${run.messages.find((x) => x.severity >= 1 && x.severity <= 3)?.text ?? "expected a violation of " + target}`;
|
|
685
|
+
}));
|
|
686
|
+
});
|
|
687
|
+
// src/formal/proofs.ts
|
|
688
|
+
const matches = (glob, file) => new Bun.Glob(glob).match(file);
|
|
689
|
+
const critical = (repo) => Effect.succeed([
|
|
690
|
+
...repo.file.critical.flatMap((glob) => repo.tracked.some((f) => matches(glob, f)) ? [] : [{
|
|
691
|
+
file: "specs.json",
|
|
692
|
+
message: `critical glob ${glob} matches no tracked file`
|
|
693
|
+
}]),
|
|
694
|
+
...Object.entries({
|
|
695
|
+
...repo.file.specs,
|
|
696
|
+
...repo.file.kani
|
|
697
|
+
}).flatMap(([file, e]) => e.covers.flatMap((glob) => repo.tracked.some((f) => matches(glob, f)) ? [] : [{
|
|
698
|
+
file,
|
|
699
|
+
message: `covers glob ${glob} matches no tracked file`
|
|
700
|
+
}])),
|
|
701
|
+
...repo.tracked.flatMap((file) => {
|
|
702
|
+
const glob = repo.file.critical.find((g) => matches(g, file));
|
|
703
|
+
return glob !== void 0 && !Object.values({
|
|
704
|
+
...repo.file.specs,
|
|
705
|
+
...repo.file.kani
|
|
706
|
+
}).some((e) => e.mode !== "library" && e.covers.some((g) => matches(g, file))) ? [{
|
|
707
|
+
file,
|
|
708
|
+
message: `matches critical glob ${glob} but no spec covers it`
|
|
709
|
+
}] : [];
|
|
710
|
+
})
|
|
711
|
+
]);
|
|
712
|
+
const fairness = (repo) => Effect.succeed(repo.specs.flatMap((s) => s.analysis._tag === "Parsed" ? s.analysis.semantics.fairness.filter((f) => !f.valid).map((f) => ({
|
|
713
|
+
file: s.path,
|
|
714
|
+
message: `${f.kind} at line ${f.line}; fairness must be WF_vars or SF_vars`
|
|
715
|
+
})) : []));
|
|
716
|
+
const coverageCheck = (repo) => Effect.gen(function* () {
|
|
717
|
+
const gate = yield* Gate;
|
|
718
|
+
const fs = yield* FileSystem;
|
|
719
|
+
const problems = [];
|
|
720
|
+
for (const m of repo.models.filter((m) => m.entry.mode === "gate")) {
|
|
721
|
+
const result = yield* gate.run(m);
|
|
722
|
+
if (result._tag === "Failed") continue;
|
|
723
|
+
const start = problems.length;
|
|
724
|
+
for (const a of result.coverage.actions.filter((a) => a.generated === 0n)) problems.push({
|
|
725
|
+
file: m.path,
|
|
726
|
+
message: `action ${a.name} (line ${a.span.line}) is never enabled`
|
|
727
|
+
});
|
|
728
|
+
for (const z of result.coverage.zeros) {
|
|
729
|
+
const candidate = join(dirname(m.path), `${z.span.module}.tla`);
|
|
730
|
+
const file = repo.tracked.includes(candidate) ? candidate : m.path;
|
|
731
|
+
const source = (yield* fs.readFileString(`${repo.root}/${file}`)).split("\n").slice(z.span.line - 1, z.span.endLine).join("\n").slice(z.span.col - 1, z.span.endLine === z.span.line ? z.span.endCol : void 0).slice(0, 60);
|
|
732
|
+
problems.push({
|
|
733
|
+
file,
|
|
734
|
+
message: `line ${z.span.line}, col ${z.span.col} to line ${z.span.endLine}, col ${z.span.endCol} never runs in ${z.scope}: ${source}`
|
|
735
|
+
});
|
|
736
|
+
}
|
|
737
|
+
if (problems.length === start) yield* Console.log(`coverage: ${m.path}: no dead logic`);
|
|
738
|
+
}
|
|
739
|
+
return problems;
|
|
740
|
+
}).pipe(Effect.mapError((e) => new FormalError({ message: String(e) })));
|
|
741
|
+
const witnesses = (repo) => Effect.gen(function* () {
|
|
742
|
+
const gate = yield* Gate;
|
|
743
|
+
const problems = [];
|
|
744
|
+
for (const m of repo.models) for (const name of m.entry.witnesses) {
|
|
745
|
+
const op = m.semantics.operators.get(name);
|
|
746
|
+
const message = !op ? `${name} is not defined` : op.level === 2 ? `${name} is an action; check [][${name}]_vars instead` : op.params.length ? `${name} must have zero arguments` : [...m.config.cfg.invariants, ...m.config.cfg.properties].includes(name) ? `${name} is already checked by ${m.config.path}` : void 0;
|
|
747
|
+
if (message !== void 0) {
|
|
748
|
+
problems.push({
|
|
749
|
+
file: m.path,
|
|
750
|
+
message
|
|
751
|
+
});
|
|
752
|
+
continue;
|
|
753
|
+
}
|
|
754
|
+
if (m.entry.mode !== "gate" || (yield* gate.run(m))._tag === "Failed") continue;
|
|
755
|
+
const result = yield* violate(repo, m, name);
|
|
756
|
+
if (result !== void 0) problems.push({
|
|
757
|
+
file: m.path,
|
|
758
|
+
message: result === held ? `${name} holds; a witness must be violated` : result
|
|
759
|
+
});
|
|
760
|
+
else yield* Console.log(`witnesses: ${m.path}: ${name} violated`);
|
|
761
|
+
}
|
|
762
|
+
return problems;
|
|
763
|
+
}).pipe(Effect.mapError((e) => new FormalError({ message: String(e) })));
|
|
764
|
+
const breaks = (repo) => Effect.gen(function* () {
|
|
765
|
+
const gate = yield* Gate;
|
|
766
|
+
const fs = yield* FileSystem;
|
|
767
|
+
const changed = yield* changes(repo.root);
|
|
768
|
+
const problems = [];
|
|
769
|
+
if (changed._tag === "All") yield* Console.log(`breaks: BASE ${changed.base} does not resolve; running every break`);
|
|
770
|
+
for (const m of repo.models) {
|
|
771
|
+
const targets = [...m.config.cfg.invariants, ...m.config.cfg.properties];
|
|
772
|
+
for (const target of targets) if (!m.entry.breaks.some((b) => b.violates === target)) problems.push({
|
|
773
|
+
file: m.config.path,
|
|
774
|
+
message: `${target} has no break`
|
|
775
|
+
});
|
|
776
|
+
const dirty = changed._tag === "All" || changed.files.some((f) => f === "specs.json" || dirname(f) === dirname(m.path) && /\.(?:tla|cfg)$/u.test(f) || m.entry.breaks.some((b) => b.file === f));
|
|
777
|
+
if (!dirty && m.entry.mode === "gate") yield* Console.log(`breaks: ${m.path}: unchanged since ${changed.sha.slice(0, 7)}, not run`);
|
|
778
|
+
for (const [index, b] of m.entry.breaks.entries()) {
|
|
779
|
+
const label = `break ${index + 1}`;
|
|
780
|
+
const file = b.file ?? m.path;
|
|
781
|
+
if (!targets.includes(b.violates)) {
|
|
782
|
+
problems.push({
|
|
783
|
+
file: m.path,
|
|
784
|
+
message: `${label}: ${b.violates} is not checked by ${m.config.path}`
|
|
785
|
+
});
|
|
786
|
+
continue;
|
|
787
|
+
}
|
|
788
|
+
if (!repo.tracked.includes(file) || !(file === m.config.path || dirname(file) === dirname(m.path) && file.endsWith(".tla"))) {
|
|
789
|
+
problems.push({
|
|
790
|
+
file: m.path,
|
|
791
|
+
message: `${label}: ${file} must be a tracked module in the spec directory or its config`
|
|
792
|
+
});
|
|
793
|
+
continue;
|
|
794
|
+
}
|
|
795
|
+
const count = (yield* fs.readFileString(`${repo.root}/${file}`)).split(b.find).length - 1;
|
|
796
|
+
if (count !== 1) {
|
|
797
|
+
problems.push({
|
|
798
|
+
file: m.path,
|
|
799
|
+
message: `${label} find matches ${count} times in ${file}`
|
|
800
|
+
});
|
|
801
|
+
continue;
|
|
802
|
+
}
|
|
803
|
+
if (!dirty || m.entry.mode !== "gate" || (yield* gate.run(m))._tag === "Failed") continue;
|
|
804
|
+
const result = yield* violate(repo, m, b.violates, b);
|
|
805
|
+
if (result !== void 0) problems.push({
|
|
806
|
+
file: m.path,
|
|
807
|
+
message: `${label} (${b.violates}): ${result === held ? `${b.violates} still holds after the edit` : result}`
|
|
808
|
+
});
|
|
809
|
+
else yield* Console.log(`breaks: ${m.path}: ${label} violated ${b.violates}`);
|
|
810
|
+
}
|
|
811
|
+
}
|
|
812
|
+
return problems;
|
|
813
|
+
}).pipe(Effect.mapError((e) => new FormalError({ message: String(e) })));
|
|
814
|
+
// src/formal/traces.ts
|
|
815
|
+
const quote = (s) => JSON.stringify(s);
|
|
816
|
+
const RawState = Schema.Record(Schema.String, Schema.Json);
|
|
817
|
+
const Raw = Schema.Array(RawState);
|
|
818
|
+
const decodeAction = Schema.decodeUnknownSync(Schema.Struct({
|
|
819
|
+
name: Schema.String,
|
|
820
|
+
context: Schema.optionalKey(Schema.NullOr(Schema.Record(Schema.String, Schema.Json))),
|
|
821
|
+
location: Schema.optionalKey(Schema.Json)
|
|
822
|
+
}));
|
|
823
|
+
const normalize = (text, plan) => {
|
|
824
|
+
const lines = text === "" ? [] : (text.endsWith("\n") ? text.slice(0, -1) : text).split("\n");
|
|
825
|
+
if (lines.length !== plan.count) throw new Error(`only ${lines.length} of ${plan.count} behaviors reached depth ${plan.depth}`);
|
|
826
|
+
return lines.map((line, index) => {
|
|
827
|
+
const states = Schema.decodeSync(Schema.fromJsonString(Raw))(line);
|
|
828
|
+
if (states.length !== plan.depth) throw new Error(`trace ${index} has ${states.length} states; expected ${plan.depth}`);
|
|
829
|
+
const steps = states.map((s, i) => {
|
|
830
|
+
if (s.formal_tick !== i) throw new Error(`trace ${index} state ${i} has wrong formal_tick`);
|
|
831
|
+
const action = decodeAction(s._action);
|
|
832
|
+
const name = action.name.replace(/^Formal_/u, "");
|
|
833
|
+
if (action.name !== `Formal_${name}` || (i === 0 ? name !== plan.init : !plan.actions.has(name))) throw new Error(`trace ${index} state ${i} has unknown action ${action.name}`);
|
|
834
|
+
const args = action.context ?? {};
|
|
835
|
+
if (Object.keys(args).length !== (i === 0 ? 0 : plan.actions.get(name))) throw new Error(`trace ${index} state ${i} has wrong action arity`);
|
|
836
|
+
const keys = Object.keys(s).toSorted();
|
|
837
|
+
const expected = [
|
|
838
|
+
...plan.variables,
|
|
839
|
+
"_action",
|
|
840
|
+
"formal_tick"
|
|
841
|
+
].toSorted();
|
|
842
|
+
if (JSON.stringify(keys) !== JSON.stringify(expected)) throw new Error(`trace ${index} state ${i} has wrong variables`);
|
|
843
|
+
return {
|
|
844
|
+
action: name,
|
|
845
|
+
args,
|
|
846
|
+
state: Object.fromEntries(Object.entries(s).filter(([k]) => k !== "_action" && k !== "formal_tick"))
|
|
847
|
+
};
|
|
848
|
+
});
|
|
849
|
+
return JSON.stringify(sorted({
|
|
850
|
+
index,
|
|
851
|
+
constants: plan.constants,
|
|
852
|
+
steps
|
|
853
|
+
}));
|
|
854
|
+
}).join("\n") + "\n";
|
|
855
|
+
};
|
|
856
|
+
const traces = (repo) => Effect.gen(function* () {
|
|
857
|
+
const fs = yield* FileSystem;
|
|
858
|
+
const tla = yield* Tla;
|
|
859
|
+
const gate = yield* Gate;
|
|
860
|
+
const problems = [];
|
|
861
|
+
for (const m of repo.models) {
|
|
862
|
+
if (m.entry.mode !== "gate" || !m.entry.traces) continue;
|
|
863
|
+
const t = m.entry.traces;
|
|
864
|
+
const result = yield* gate.run(m);
|
|
865
|
+
if (result._tag === "Failed") continue;
|
|
866
|
+
const finding = yield* Effect.scoped(Effect.gen(function* () {
|
|
867
|
+
const section = (k) => m.config.cfg.sections.find((s) => s.keyword === k)?.text;
|
|
868
|
+
const spec = section("SPECIFICATION");
|
|
869
|
+
const pair = spec !== void 0 ? m.semantics.specifications.get(spec) : {
|
|
870
|
+
init: section("INIT"),
|
|
871
|
+
next: section("NEXT")
|
|
872
|
+
};
|
|
873
|
+
if (pair?.init === void 0 || pair.next === void 0) return "cannot derive zero-argument INIT/NEXT from config";
|
|
874
|
+
const { init, next } = pair;
|
|
875
|
+
const names = [...new Set(result.coverage.actions.map((a) => a.name))];
|
|
876
|
+
const actions = /* @__PURE__ */ new Map();
|
|
877
|
+
for (const name of names) {
|
|
878
|
+
const op = m.semantics.operators.get(name);
|
|
879
|
+
if (name === next || !op || op.params.some((p) => p.arity !== 0)) return `cannot wrap action ${name}: unnamed, INSTANCE'd, or higher-order action`;
|
|
880
|
+
actions.set(name, op.params.length);
|
|
881
|
+
}
|
|
882
|
+
for (const name of Object.keys(t.steer ?? {})) if (!actions.has(name)) return `steer key ${name} is not an action`;
|
|
883
|
+
if ([...m.semantics.operators.keys(), ...m.semantics.variables].some((n) => n === "FormalTraces" || n === "formal_tick" || n.startsWith("Formal_"))) return "generated trace name collides with spec";
|
|
884
|
+
const dir = yield* sandbox(repo, m);
|
|
885
|
+
const raw = `${dir}/raw.ndjson`;
|
|
886
|
+
const module = basename(m.path, ".tla");
|
|
887
|
+
const slash = "\\";
|
|
888
|
+
const defs = names.map((name) => {
|
|
889
|
+
const params = m.semantics.operators.get(name)?.params.map((p) => p.name) ?? [];
|
|
890
|
+
const args = params.length ? `(${params.join(", ")})` : "";
|
|
891
|
+
const keep = t.steer?.[name];
|
|
892
|
+
return `Formal_${name}${args} == Formal_M!${name}${args} /${slash} formal_tick' = formal_tick + 1${keep === void 0 ? "" : ` /${slash} Formal_TLC!RandomElement(1 .. ${keep}) = 1`}`;
|
|
893
|
+
});
|
|
894
|
+
const wrapper = [
|
|
895
|
+
"---- MODULE FormalTraces ----",
|
|
896
|
+
`EXTENDS ${module}`,
|
|
897
|
+
"VARIABLE formal_tick",
|
|
898
|
+
`Formal_M == INSTANCE ${module}`,
|
|
899
|
+
"Formal_TLC == INSTANCE TLC",
|
|
900
|
+
"Formal_Ext == INSTANCE TLCExt",
|
|
901
|
+
"Formal_IO == INSTANCE IOUtils",
|
|
902
|
+
`Formal_${init} == Formal_M!${init} /${slash} formal_tick = 0`,
|
|
903
|
+
...defs,
|
|
904
|
+
"Formal_Count == Formal_TLC!TLCGet(\"stats\").traces",
|
|
905
|
+
`Formal_Fresh == 1 ${slash}notin DOMAIN Formal_TLC!TLCGet("all") ${slash}/ Formal_TLC!TLCGet(1) # Formal_Count`,
|
|
906
|
+
`Formal_Export == (Formal_TLC!TLCGet("level") = ${t.depth} /${slash} Formal_Fresh) =>`,
|
|
907
|
+
` /${slash} Formal_TLC!TLCSet(1, Formal_Count)`,
|
|
908
|
+
` /${slash} Formal_IO!Serialize(<<Formal_Ext!Trace>>, ${quote(raw)}, [format |-> "NDJSON", charset |-> "UTF-8", openOptions |-> <<"WRITE", "CREATE", "APPEND">>])`,
|
|
909
|
+
"====",
|
|
910
|
+
""
|
|
911
|
+
].join("\n");
|
|
912
|
+
yield* fs.writeFileString(`${dir}/FormalTraces.tla`, wrapper);
|
|
913
|
+
const cfg = derive(m.config.cfg, { simulation: {
|
|
914
|
+
init,
|
|
915
|
+
next,
|
|
916
|
+
overrides: [`${init} <- Formal_${init}`, ...names.map((n) => `${n} <- Formal_${n}`)]
|
|
917
|
+
} });
|
|
918
|
+
yield* fs.writeFileString(`${dir}/FormalTraces.cfg`, cfg);
|
|
919
|
+
const run = yield* tla.tlc({
|
|
920
|
+
spec: `${dir}/FormalTraces.tla`,
|
|
921
|
+
config: `${dir}/FormalTraces.cfg`,
|
|
922
|
+
timeoutSeconds: m.entry.timeoutSeconds,
|
|
923
|
+
mode: {
|
|
924
|
+
_tag: "Simulate",
|
|
925
|
+
num: t.count,
|
|
926
|
+
depth: t.depth,
|
|
927
|
+
seed: t.seed
|
|
928
|
+
}
|
|
929
|
+
});
|
|
930
|
+
if (run._tag === "TimedOut") return `simulation timed out after ${m.entry.timeoutSeconds} s`;
|
|
931
|
+
if (run.code !== 0 || run.messages.some((x) => x.severity >= 1 && x.severity <= 3)) return `simulation exited ${run.code}: ${run.messages.filter((x) => x.severity >= 1 && x.severity <= 3).map((x) => x.text).join("; ")}`;
|
|
932
|
+
const text = (yield* fs.exists(raw)) ? yield* fs.readFileString(raw) : "";
|
|
933
|
+
const normalized = yield* Effect.try({
|
|
934
|
+
try: () => normalize(text, {
|
|
935
|
+
count: t.count,
|
|
936
|
+
depth: t.depth,
|
|
937
|
+
init,
|
|
938
|
+
actions,
|
|
939
|
+
variables: m.semantics.variables,
|
|
940
|
+
constants: Object.fromEntries(m.config.cfg.constants.flatMap((c) => c._tag === "Value" ? [[c.name, /^-?\d+$/u.test(c.value) ? Number(c.value) : c.value]] : []))
|
|
941
|
+
}),
|
|
942
|
+
catch: (e) => new FormalError({ message: e instanceof Error ? e.message : String(e) })
|
|
943
|
+
});
|
|
944
|
+
const file = `${dir}/traces.ndjson`;
|
|
945
|
+
yield* fs.writeFileString(file, normalized);
|
|
946
|
+
let owner = dirname(t.conformance);
|
|
947
|
+
while (owner !== "." && !repo.tracked.includes(`${owner}/package.json`)) owner = dirname(owner);
|
|
948
|
+
const started = yield* Clock.currentTimeMillis;
|
|
949
|
+
const conformance = yield* Effect.scoped(Effect.gen(function* () {
|
|
950
|
+
const p = yield* ChildProcess.make("vp", [
|
|
951
|
+
"test",
|
|
952
|
+
"run",
|
|
953
|
+
relative(owner, t.conformance)
|
|
954
|
+
], {
|
|
955
|
+
cwd: `${repo.root}/${owner}`,
|
|
956
|
+
env: { [FORMAL_TRACES]: file },
|
|
957
|
+
extendEnv: true,
|
|
958
|
+
stdin: "ignore",
|
|
959
|
+
killSignal: "SIGKILL"
|
|
960
|
+
});
|
|
961
|
+
return yield* Effect.all([
|
|
962
|
+
p.stdout.pipe(Stream.decodeText, Stream.mkString),
|
|
963
|
+
p.stderr.pipe(Stream.decodeText, Stream.mkString),
|
|
964
|
+
p.exitCode
|
|
965
|
+
], { concurrency: "unbounded" });
|
|
966
|
+
}).pipe(Effect.timeoutOption(`${m.entry.timeoutSeconds} seconds`)));
|
|
967
|
+
if (Option.isNone(conformance)) return `conformance test ${t.conformance} timed out`;
|
|
968
|
+
if (Number(conformance.value[2]) !== 0) {
|
|
969
|
+
yield* Console.log((conformance.value[0] + conformance.value[1]).slice(-4e3));
|
|
970
|
+
return `conformance test ${t.conformance} failed (vp test exited ${conformance.value[2]})`;
|
|
971
|
+
}
|
|
972
|
+
const receipt = receiptPath(file);
|
|
973
|
+
const replayed = (yield* fs.exists(receipt)) ? (yield* fs.readFileString(receipt)).trim() : "0";
|
|
974
|
+
if (replayed !== String(t.count)) return `conformance test replayed ${replayed} of ${t.count} traces; register them with replayTraces`;
|
|
975
|
+
yield* Console.log(`traces: ${m.path}: ${t.count} traces of ${t.depth} states (sha256 ${createHash("sha256").update(normalized).digest("hex")}), conformance passed in ${((yield* Clock.currentTimeMillis) - started) / 1e3} s`);
|
|
976
|
+
})).pipe(Effect.catch((e) => Effect.succeed(e.message)));
|
|
977
|
+
if (finding !== void 0) problems.push({
|
|
978
|
+
file: m.path,
|
|
979
|
+
message: finding
|
|
980
|
+
});
|
|
981
|
+
}
|
|
982
|
+
return problems;
|
|
983
|
+
});
|
|
370
984
|
// src/formal/checks.ts
|
|
371
985
|
const configs = (repo) => repo.specs.filter((s) => s.analysis._tag === "Parsed").flatMap((s) => s.configs).flatMap((c) => "_tag" in c.cfg ? [] : [{
|
|
372
986
|
path: c.path,
|
|
@@ -382,22 +996,29 @@ const specs = (repo) => Effect.succeed([
|
|
|
382
996
|
file: s.path,
|
|
383
997
|
message: "tracked spec has no specs.json entry"
|
|
384
998
|
}] : [],
|
|
385
|
-
...s.entry
|
|
999
|
+
...s.entry && s.entry.mode !== "library" && !s.configs.some((c) => c.path === s.path.replace(/\.tla$/u, ".cfg")) ? [{
|
|
1000
|
+
file: s.path,
|
|
1001
|
+
message: "runnable spec has no matching .cfg"
|
|
1002
|
+
}] : [],
|
|
1003
|
+
...s.entry?.mode === "library" && s.configs.length ? [{
|
|
386
1004
|
file: s.path,
|
|
387
|
-
message: "
|
|
1005
|
+
message: "library spec must not have a config"
|
|
388
1006
|
}] : [],
|
|
389
1007
|
...s.configs.flatMap((c) => "_tag" in c.cfg ? [{
|
|
390
1008
|
file: c.path,
|
|
391
1009
|
message: c.cfg.message
|
|
392
1010
|
}] : [])
|
|
393
1011
|
]),
|
|
394
|
-
...Object.keys(
|
|
1012
|
+
...Object.keys({
|
|
1013
|
+
...repo.file.specs,
|
|
1014
|
+
...repo.file.kani
|
|
1015
|
+
}).filter((f) => !repo.tracked.includes(f)).map((file) => ({
|
|
395
1016
|
file,
|
|
396
1017
|
message: "entry names no tracked file"
|
|
397
1018
|
})),
|
|
398
1019
|
...repo.orphans.map((file) => ({
|
|
399
1020
|
file,
|
|
400
|
-
message: "
|
|
1021
|
+
message: "config pairs with no module; give a variant its own module that EXTENDS X"
|
|
401
1022
|
}))
|
|
402
1023
|
]);
|
|
403
1024
|
const sany = (repo) => Effect.succeed(repo.specs.flatMap((s) => s.analysis._tag === "Unparsed" ? [{
|
|
@@ -458,82 +1079,95 @@ const format = (repo) => Effect.gen(function* () {
|
|
|
458
1079
|
}), { concurrency: 4 })).flat();
|
|
459
1080
|
}).pipe(Effect.mapError((cause) => new FormalError({ message: String(cause) })));
|
|
460
1081
|
const tlc = (repo) => Effect.gen(function* () {
|
|
461
|
-
const
|
|
1082
|
+
const gate = yield* Gate;
|
|
462
1083
|
const problems = [];
|
|
463
|
-
for (const
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
spec: `${repo.root}/${s.path}`,
|
|
469
|
-
config: `${repo.root}/${config.path}`,
|
|
470
|
-
timeoutSeconds: s.entry.timeoutSeconds
|
|
1084
|
+
for (const m of repo.models.filter((m) => m.entry.mode === "gate")) {
|
|
1085
|
+
const result = yield* gate.run(m);
|
|
1086
|
+
if (result._tag === "Failed") problems.push({
|
|
1087
|
+
file: m.config.path,
|
|
1088
|
+
message: result.message
|
|
471
1089
|
});
|
|
472
|
-
|
|
473
|
-
problems.push({
|
|
474
|
-
file: config.path,
|
|
475
|
-
message: `TLC timed out after ${s.entry.timeoutSeconds} s`
|
|
476
|
-
});
|
|
477
|
-
continue;
|
|
478
|
-
}
|
|
479
|
-
const failures = run.messages.filter((m) => m.severity >= 1 && m.severity <= 3);
|
|
480
|
-
const states = run.messages.find((m) => m.code === 2199)?.text.match(/([\d,]+) distinct states/u)?.[1]?.replaceAll(",", "");
|
|
481
|
-
if (failures.length || run.code !== 0 || !run.messages.some((m) => m.code === 2193) || states === void 0 || BigInt(states) === 0n) {
|
|
482
|
-
const meaning = /* @__PURE__ */ new Map([
|
|
483
|
-
[11, "deadlock"],
|
|
484
|
-
[12, "safety violation"],
|
|
485
|
-
[13, "liveness violation"],
|
|
486
|
-
[150, "parse failure"]
|
|
487
|
-
]);
|
|
488
|
-
problems.push({
|
|
489
|
-
file: config.path,
|
|
490
|
-
message: `TLC exited ${run.code}${meaning.has(run.code) ? ` (${meaning.get(run.code)})` : ""}; ${failures.length ? failures.map((m) => `${m.severity === 3 ? "warning" : "error"} ${m.code}: ${m.text}`).join("; ") : "requires 2193 success and nonzero distinct states (2199)"}`
|
|
491
|
-
});
|
|
492
|
-
} else yield* Console.log(`tlc: ${config.path}: ${states} distinct states in ${run.seconds} s`);
|
|
1090
|
+
else yield* Console.log(`tlc: ${m.config.path}: ${result.states} distinct states in ${result.seconds} s`);
|
|
493
1091
|
}
|
|
494
1092
|
return problems;
|
|
495
1093
|
});
|
|
496
1094
|
const checks = {
|
|
497
1095
|
specs,
|
|
1096
|
+
critical,
|
|
498
1097
|
sany,
|
|
499
1098
|
typeok,
|
|
500
1099
|
deadlock,
|
|
1100
|
+
fairness,
|
|
501
1101
|
constants,
|
|
502
1102
|
format,
|
|
503
|
-
tlc
|
|
1103
|
+
tlc,
|
|
1104
|
+
coverage: coverageCheck,
|
|
1105
|
+
witnesses,
|
|
1106
|
+
breaks,
|
|
1107
|
+
traces
|
|
504
1108
|
};
|
|
505
1109
|
// src/formal/specs.ts
|
|
506
1110
|
const RepoPath = Schema.String.check(Schema.isPattern(/^(?!\/)(?!(?:.*\/)?\.\.(?:\/|$))[^\0\\]+$/u));
|
|
507
1111
|
const OperatorName = Schema.String.check(Schema.isPattern(/^[A-Za-z_][A-Za-z0-9_]*$/u));
|
|
508
|
-
const
|
|
1112
|
+
const Common = {
|
|
509
1113
|
synced: Schema.String.check(Schema.isPattern(/^[0-9a-f]{40}$/u)),
|
|
510
|
-
covers: Schema.NonEmptyArray(Schema.NonEmptyString)
|
|
511
|
-
|
|
1114
|
+
covers: Schema.NonEmptyArray(Schema.NonEmptyString)
|
|
1115
|
+
};
|
|
1116
|
+
const Runtime = {
|
|
1117
|
+
...Common,
|
|
512
1118
|
timeoutSeconds: Schema.Int.check(Schema.isBetween({
|
|
513
1119
|
minimum: 1,
|
|
514
1120
|
maximum: 1800
|
|
515
|
-
})).pipe(Schema.withDecodingDefaultKey(Effect.succeed(60)))
|
|
516
|
-
|
|
1121
|
+
})).pipe(Schema.withDecodingDefaultKey(Effect.succeed(60)))
|
|
1122
|
+
};
|
|
1123
|
+
const Proof = {
|
|
1124
|
+
...Runtime,
|
|
1125
|
+
witnesses: Schema.NonEmptyArray(OperatorName).check(Schema.makeFilter((names) => new Set(names).size === names.length || "witnesses must be unique")),
|
|
517
1126
|
breaks: Schema.Array(Schema.Struct({
|
|
518
1127
|
violates: OperatorName,
|
|
519
1128
|
find: Schema.NonEmptyString,
|
|
520
1129
|
replace: Schema.String,
|
|
521
1130
|
file: Schema.optionalKey(RepoPath)
|
|
522
|
-
})).pipe(Schema.withDecodingDefaultKey(Effect.succeed([])))
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
1131
|
+
})).pipe(Schema.withDecodingDefaultKey(Effect.succeed([])))
|
|
1132
|
+
};
|
|
1133
|
+
const Entry = Schema.Union([
|
|
1134
|
+
Schema.Struct({
|
|
1135
|
+
...Proof,
|
|
1136
|
+
mode: Schema.Literal("gate"),
|
|
1137
|
+
traces: Schema.optionalKey(Schema.Struct({
|
|
1138
|
+
seed: Schema.Int.check(Schema.isGreaterThanOrEqualTo(0)),
|
|
1139
|
+
depth: Schema.Int.check(Schema.isBetween({
|
|
1140
|
+
minimum: 1,
|
|
1141
|
+
maximum: 1e4
|
|
1142
|
+
})),
|
|
1143
|
+
count: Schema.Int.check(Schema.isBetween({
|
|
1144
|
+
minimum: 1,
|
|
1145
|
+
maximum: 1e5
|
|
1146
|
+
})),
|
|
1147
|
+
steer: Schema.optionalKey(Schema.Record(OperatorName, Schema.Int.check(Schema.isBetween({
|
|
1148
|
+
minimum: 2,
|
|
1149
|
+
maximum: 1e3
|
|
1150
|
+
})))),
|
|
1151
|
+
conformance: RepoPath
|
|
532
1152
|
}))
|
|
533
|
-
})
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
1153
|
+
}),
|
|
1154
|
+
Schema.Struct({
|
|
1155
|
+
...Proof,
|
|
1156
|
+
mode: Schema.Literal("manual")
|
|
1157
|
+
}),
|
|
1158
|
+
Schema.Struct({
|
|
1159
|
+
...Common,
|
|
1160
|
+
mode: Schema.Literal("library")
|
|
1161
|
+
})
|
|
1162
|
+
]);
|
|
1163
|
+
const SpecsFile = Schema.Struct({
|
|
1164
|
+
critical: Schema.NonEmptyArray(Schema.NonEmptyString),
|
|
1165
|
+
specs: Schema.Record(RepoPath.check(Schema.isPattern(/\.tla$/u)), Entry),
|
|
1166
|
+
kani: Schema.Record(RepoPath.check(Schema.isPattern(/\.rs$/u)), Schema.Struct({
|
|
1167
|
+
...Runtime,
|
|
1168
|
+
mode: Schema.Literals(["gate", "manual"])
|
|
1169
|
+
})).pipe(Schema.withDecodingDefaultKey(Effect.succeed({})))
|
|
1170
|
+
});
|
|
537
1171
|
const load = Effect.fn("formal.load")(function* (root) {
|
|
538
1172
|
const fs = yield* FileSystem;
|
|
539
1173
|
const path = yield* Path;
|
|
@@ -545,7 +1179,11 @@ const load = Effect.fn("formal.load")(function* (root) {
|
|
|
545
1179
|
const [files, code] = yield* Effect.all([git.stdout.pipe(Stream.decodeText, Stream.mkString), git.exitCode], { concurrency: "unbounded" });
|
|
546
1180
|
if (Number(code) !== 0) return yield* new FormalError({ message: "git ls-files failed" });
|
|
547
1181
|
const tracked = yield* Effect.filter(files.split("\0").filter(Boolean), (file) => fs.exists(path.join(root, file)));
|
|
548
|
-
let file = {
|
|
1182
|
+
let file = {
|
|
1183
|
+
critical: [],
|
|
1184
|
+
specs: {},
|
|
1185
|
+
kani: {}
|
|
1186
|
+
};
|
|
549
1187
|
let decodeErrors = [];
|
|
550
1188
|
if (yield* fs.exists(path.join(root, "specs.json"))) {
|
|
551
1189
|
const raw = yield* fs.readFileString(path.join(root, "specs.json"));
|
|
@@ -561,7 +1199,7 @@ const load = Effect.fn("formal.load")(function* (root) {
|
|
|
561
1199
|
const paired = /* @__PURE__ */ new Set();
|
|
562
1200
|
const specs = yield* Effect.forEach(modules, (module) => Effect.gen(function* () {
|
|
563
1201
|
const stem = module.slice(0, -4);
|
|
564
|
-
const matches = configs.filter((c) => c === `${stem}.cfg`
|
|
1202
|
+
const matches = configs.filter((c) => c === `${stem}.cfg`);
|
|
565
1203
|
const parsed = yield* Effect.forEach(matches, (c) => Effect.gen(function* () {
|
|
566
1204
|
paired.add(c);
|
|
567
1205
|
return {
|
|
@@ -571,7 +1209,7 @@ const load = Effect.fn("formal.load")(function* (root) {
|
|
|
571
1209
|
}), { concurrency: 1 });
|
|
572
1210
|
return {
|
|
573
1211
|
path: module,
|
|
574
|
-
entry: file[module],
|
|
1212
|
+
entry: file.specs[module],
|
|
575
1213
|
analysis: yield* tla.analyze(path.join(root, module)),
|
|
576
1214
|
configs: parsed
|
|
577
1215
|
};
|
|
@@ -581,6 +1219,19 @@ const load = Effect.fn("formal.load")(function* (root) {
|
|
|
581
1219
|
tracked,
|
|
582
1220
|
file,
|
|
583
1221
|
specs,
|
|
1222
|
+
models: specs.flatMap((s) => {
|
|
1223
|
+
const c = s.configs[0];
|
|
1224
|
+
return s.analysis._tag === "Parsed" && s.entry && s.entry.mode !== "library" && c && !("_tag" in c.cfg) ? [{
|
|
1225
|
+
root,
|
|
1226
|
+
path: s.path,
|
|
1227
|
+
entry: s.entry,
|
|
1228
|
+
semantics: s.analysis.semantics,
|
|
1229
|
+
config: {
|
|
1230
|
+
path: c.path,
|
|
1231
|
+
cfg: c.cfg
|
|
1232
|
+
}
|
|
1233
|
+
}] : [];
|
|
1234
|
+
}),
|
|
584
1235
|
orphans: configs.filter((c) => !paired.has(c)),
|
|
585
1236
|
decodeErrors
|
|
586
1237
|
};
|
|
@@ -599,7 +1250,7 @@ const formal = Effect.fn("formal")(function* (root) {
|
|
|
599
1250
|
check: name
|
|
600
1251
|
})));
|
|
601
1252
|
}
|
|
602
|
-
findings.sort((a, b) => a.file.localeCompare(b.file) || a.check.
|
|
1253
|
+
findings.sort((a, b) => a.file.localeCompare(b.file) || Object.keys(checks).indexOf(a.check) - Object.keys(checks).indexOf(b.check));
|
|
603
1254
|
if (findings.length) {
|
|
604
1255
|
for (const f of findings) yield* Console.error(`${f.file}: ${f.check}: ${singleLine(f.message)}`);
|
|
605
1256
|
yield* Console.error(`formal: ${findings.length} ${findings.length === 1 ? "finding" : "findings"}`);
|
|
@@ -615,7 +1266,7 @@ if (import.meta.main) {
|
|
|
615
1266
|
const git = yield* ChildProcess.make("git", ["rev-parse", "--show-toplevel"], { stdin: "ignore" });
|
|
616
1267
|
const [root, code] = yield* Effect.all([git.stdout.pipe(Stream.decodeText, Stream.mkString), git.exitCode], { concurrency: "unbounded" });
|
|
617
1268
|
if (Number(code) !== 0) return yield* new FormalError({ message: "run formal inside a git repository" });
|
|
618
|
-
return yield* formal(root.trim()).pipe(Effect.provide(Tla.layer));
|
|
1269
|
+
return yield* formal(root.trim()).pipe(Effect.provide(Gate.layer.pipe(Layer.provideMerge(Tla.layer))));
|
|
619
1270
|
})).pipe(Effect.provide(NodeServices.layer)));
|
|
620
1271
|
if (Exit.isSuccess(exit)) process.exitCode = exit.value;
|
|
621
1272
|
else {
|