@milkenjoyer/toolchain 0.11.0 → 0.13.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 +190 -17
- package/dist/arithmetic.d.mts +45 -0
- package/dist/arithmetic.mjs +25 -0
- 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 +1006 -146
- package/dist/guard.mjs +2 -2
- package/dist/knip.mjs +2 -1
- 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 +12 -3
- package/src/formal/arithmetic.ts +190 -0
- package/src/formal/cfg.ts +58 -1
- package/src/formal/checks.ts +57 -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/proof-run.ts +77 -0
- package/src/formal/proofs.ts +226 -0
- package/src/formal/prover.ts +291 -0
- package/src/formal/semantics.ts +110 -0
- package/src/formal/specs.ts +103 -31
- package/src/formal/tla.ts +60 -19
- 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/z3.ts +321 -0
- package/src/formal.ts +10 -3
- 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,104 +1,81 @@
|
|
|
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 { fileURLToPath } from "node:url";
|
|
14
19
|
import { createHash } from "node:crypto";
|
|
15
20
|
import * as Clock from "effect/Clock";
|
|
16
|
-
import * as Config from "effect/Config";
|
|
17
21
|
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";
|
|
24
|
+
import * as Cache from "effect/Cache";
|
|
21
25
|
import * as SchemaIssue from "effect/SchemaIssue";
|
|
22
|
-
// src/formal/
|
|
23
|
-
const
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
"
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
"
|
|
41
|
-
|
|
26
|
+
// src/formal/proof-run.ts
|
|
27
|
+
const Edit = Schema.Struct({
|
|
28
|
+
file: Schema.String,
|
|
29
|
+
find: Schema.NonEmptyString,
|
|
30
|
+
replace: Schema.String
|
|
31
|
+
});
|
|
32
|
+
const Request = Schema.Struct({
|
|
33
|
+
root: Schema.String,
|
|
34
|
+
proof: Schema.String,
|
|
35
|
+
kind: Schema.Literals(["every", "z3"]),
|
|
36
|
+
covers: Schema.Array(Schema.String),
|
|
37
|
+
samples: Schema.Int,
|
|
38
|
+
timeoutSeconds: Schema.Int,
|
|
39
|
+
edit: Schema.optionalKey(Edit),
|
|
40
|
+
result: Schema.String
|
|
41
|
+
});
|
|
42
|
+
const At = Schema.Record(Schema.String, Schema.Number);
|
|
43
|
+
const Outcome = Schema.Union([
|
|
44
|
+
Schema.TaggedStruct("Proved", {
|
|
45
|
+
detail: Schema.String,
|
|
46
|
+
seconds: Schema.Number
|
|
47
|
+
}),
|
|
48
|
+
Schema.TaggedStruct("Counterexample", { at: At }),
|
|
49
|
+
Schema.TaggedStruct("Disagrees", {
|
|
50
|
+
at: At,
|
|
51
|
+
code: Schema.Unknown,
|
|
52
|
+
model: Schema.Unknown
|
|
53
|
+
}),
|
|
54
|
+
Schema.TaggedStruct("Throws", {
|
|
55
|
+
at: At,
|
|
56
|
+
message: Schema.String
|
|
57
|
+
}),
|
|
58
|
+
Schema.TaggedStruct("Unknown", {
|
|
59
|
+
reason: Schema.String,
|
|
60
|
+
seconds: Schema.Number
|
|
61
|
+
}),
|
|
62
|
+
Schema.TaggedStruct("Invalid", { message: Schema.String })
|
|
42
63
|
]);
|
|
43
|
-
const
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
const operands = [];
|
|
57
|
-
while (i < tokens.length && !keywords.has(tokens[i] ?? "")) operands.push(tokens[i++] ?? "");
|
|
58
|
-
if (operands.length === 0) return fail(`missing operand for ${keyword}`);
|
|
59
|
-
if (keyword === "CONSTANT" || keyword === "CONSTANTS") {
|
|
60
|
-
let j = 0;
|
|
61
|
-
while (j < operands.length) {
|
|
62
|
-
const name = operands[j++];
|
|
63
|
-
const assignment = operands[j++];
|
|
64
|
-
if (name === void 0 || !/^[A-Za-z_][A-Za-z0-9_!]*$/u.test(name) || assignment !== "=" && assignment !== "<-") return fail(`unparseable constant ${name}`);
|
|
65
|
-
const value = [];
|
|
66
|
-
let depth = 0;
|
|
67
|
-
while (j < operands.length) {
|
|
68
|
-
const token = operands[j] ?? "";
|
|
69
|
-
if (depth === 0 && value.length > 0 && ["=", "<-"].includes(operands[j + 1] ?? "")) break;
|
|
70
|
-
if (token === "{") depth++;
|
|
71
|
-
if (token === "}") depth--;
|
|
72
|
-
value.push(token);
|
|
73
|
-
j++;
|
|
74
|
-
}
|
|
75
|
-
const raw = value.join(" ");
|
|
76
|
-
if (depth !== 0 || !/^(?:-?\d+|[A-Za-z_][A-Za-z0-9_!]*|"(?:\\.|[^"\\])*"|\{[\s\S]*\})$/u.test(raw)) return fail(`unparseable constant ${name} ${assignment} ${raw}`);
|
|
77
|
-
constants.push(assignment === "=" ? {
|
|
78
|
-
_tag: "Value",
|
|
79
|
-
name,
|
|
80
|
-
value: raw
|
|
81
|
-
} : {
|
|
82
|
-
_tag: "Substitution",
|
|
83
|
-
name,
|
|
84
|
-
by: raw
|
|
85
|
-
});
|
|
86
|
-
}
|
|
87
|
-
} else if (keyword === "CHECK_DEADLOCK") {
|
|
88
|
-
if (operands.length !== 1 || !["TRUE", "FALSE"].includes(operands[0] ?? "")) return fail("CHECK_DEADLOCK needs TRUE or FALSE");
|
|
89
|
-
checkDeadlock.push(operands[0] === "TRUE");
|
|
90
|
-
} else {
|
|
91
|
-
if (!operands.every((token) => /^[A-Za-z_][A-Za-z0-9_!]*$/u.test(token))) return fail(`invalid operands for ${keyword}`);
|
|
92
|
-
if (keyword.startsWith("INVARIANT")) invariants.push(...operands);
|
|
93
|
-
}
|
|
64
|
+
const Result$1 = Schema.Struct({
|
|
65
|
+
loaded: Schema.Array(Schema.String),
|
|
66
|
+
outcome: Outcome
|
|
67
|
+
});
|
|
68
|
+
const describe = (outcome) => {
|
|
69
|
+
switch (outcome._tag) {
|
|
70
|
+
case "Proved": return outcome.detail;
|
|
71
|
+
case "Counterexample": return `counterexample at ${JSON.stringify(outcome.at)}`;
|
|
72
|
+
case "Disagrees": return `code and model disagree at ${JSON.stringify(outcome.at)}: code ${JSON.stringify(outcome.code)}, model ${JSON.stringify(outcome.model)}`;
|
|
73
|
+
case "Throws": return `throws at ${JSON.stringify(outcome.at)}: ${outcome.message}`;
|
|
74
|
+
case "Unknown": return `z3 returned unknown: ${outcome.reason}`;
|
|
75
|
+
case "Invalid": return outcome.message;
|
|
76
|
+
default: return outcome;
|
|
94
77
|
}
|
|
95
|
-
return {
|
|
96
|
-
constants,
|
|
97
|
-
invariants,
|
|
98
|
-
checkDeadlock
|
|
99
|
-
};
|
|
100
78
|
};
|
|
101
|
-
const integer = (constant) => constant._tag === "Value" && /^-?\d+$/u.test(constant.value) ? BigInt(constant.value) : void 0;
|
|
102
79
|
// src/formal/semantics.ts
|
|
103
80
|
const child = (node, tag) => node.children.find((n) => n.tag === tag);
|
|
104
81
|
const value = (node, tag) => child(node, tag)?.text.trim() ?? "";
|
|
@@ -195,7 +172,67 @@ const fromXml = (xml) => {
|
|
|
195
172
|
};
|
|
196
173
|
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
174
|
const body = typeOK && child(typeOK, "body")?.children[0];
|
|
175
|
+
const definitions = [...entries.values()].filter((n) => n?.tag === "UserDefinedOpKind" && closure.has(value(child(n, "location") ?? n, "filename")));
|
|
176
|
+
const operators = /* @__PURE__ */ new Map();
|
|
177
|
+
const fairness = [];
|
|
178
|
+
const specifications = /* @__PURE__ */ new Map();
|
|
179
|
+
const opName = (n) => {
|
|
180
|
+
const t = target(n);
|
|
181
|
+
return t ? value(entries.get(t.uid) ?? n, "uniquename") : "";
|
|
182
|
+
};
|
|
183
|
+
for (const n of definitions) {
|
|
184
|
+
const name = value(n, "uniquename");
|
|
185
|
+
const params = (child(n, "params")?.children ?? []).map((ref) => entries.get(value(child(ref, "FormalParamNodeRef") ?? ref, "UID")) ?? ref).map((p) => ({
|
|
186
|
+
name: value(p, "uniquename"),
|
|
187
|
+
arity: Number(value(p, "arity"))
|
|
188
|
+
}));
|
|
189
|
+
operators.set(name, {
|
|
190
|
+
name,
|
|
191
|
+
level: Number(value(n, "level")),
|
|
192
|
+
params,
|
|
193
|
+
module: value(child(n, "location") ?? n, "filename")
|
|
194
|
+
});
|
|
195
|
+
if (value(child(n, "location") ?? n, "filename") === value(modules, "RootModule")) for (const a of descendants(child(n, "body") ?? n)) {
|
|
196
|
+
const kind = opName(a);
|
|
197
|
+
if (!["$WF", "$SF"].includes(kind)) continue;
|
|
198
|
+
const sub = operands(a)[0];
|
|
199
|
+
fairness.push({
|
|
200
|
+
kind: `${kind.slice(1)}_${sub && opName(sub) === "$Tuple" ? `<< ${operands(sub).map(opName).join(", ")} >>` : sub ? opName(sub) : "?"}`,
|
|
201
|
+
line: value(child(child(a, "location") ?? a, "line") ?? a, "begin"),
|
|
202
|
+
valid: sub !== void 0 && opName(sub) === "vars" && operands(sub).length === 0
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
const flatten = (a, seen = /* @__PURE__ */ new Set()) => {
|
|
206
|
+
const t = target(a);
|
|
207
|
+
if (["$ConjList", "\\land"].includes(opName(a))) return operands(a).flatMap((x) => flatten(x, new Set(seen)));
|
|
208
|
+
if (t && t.ref.tag === "UserDefinedOpKindRef" && Number(value(a, "level")) === 3 && operands(a).length === 0 && !seen.has(t.uid)) {
|
|
209
|
+
const b = child(entries.get(t.uid) ?? a, "body")?.children[0];
|
|
210
|
+
if (b) {
|
|
211
|
+
seen.add(t.uid);
|
|
212
|
+
return flatten(b, seen);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
return [a];
|
|
216
|
+
};
|
|
217
|
+
const body = child(n, "body")?.children[0];
|
|
218
|
+
if (!body) continue;
|
|
219
|
+
const conjuncts = flatten(body);
|
|
220
|
+
const state = conjuncts.filter((a) => Number(value(a, "level")) === 1);
|
|
221
|
+
const boxes = conjuncts.filter((a) => opName(a) === "[]");
|
|
222
|
+
const box = boxes[0] && operands(boxes[0])[0];
|
|
223
|
+
const next = box && opName(box) === "$SquareAct" ? operands(box)[0] : void 0;
|
|
224
|
+
if (state.length === 1 && boxes.length === 1 && next && operands(next).length === 0) {
|
|
225
|
+
const init = state[0] && operands(state[0]).length === 0 ? opName(state[0]) : void 0;
|
|
226
|
+
if (init !== void 0 && init !== "") specifications.set(name, {
|
|
227
|
+
init,
|
|
228
|
+
next: opName(next)
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
}
|
|
198
232
|
return {
|
|
233
|
+
operators,
|
|
234
|
+
fairness,
|
|
235
|
+
specifications,
|
|
199
236
|
variables: [...new Set(variables.values())],
|
|
200
237
|
typeOK: typeOK && new Set(body ? typed(body, /* @__PURE__ */ new Set()) : [])
|
|
201
238
|
};
|
|
@@ -204,11 +241,11 @@ const fromXml = (xml) => {
|
|
|
204
241
|
var FormalError = class extends Data.TaggedError("FormalError") {};
|
|
205
242
|
const pins = [{
|
|
206
243
|
file: "tla2tools-1.8.0.jar",
|
|
207
|
-
|
|
244
|
+
urls: ["https://github.com/tlaplus/tlaplus/releases/download/v1.8.0/tla2tools.jar", "https://api.github.com/repos/tlaplus/tlaplus/releases/assets/718777374"],
|
|
208
245
|
sha256: "e1f11b75c4b5bc4478ba0ac5cb6ae28e40a01b0016bc33e0aa97da22dfdbcd7f"
|
|
209
246
|
}, {
|
|
210
247
|
file: "CommunityModules-deps-202611060952.jar",
|
|
211
|
-
|
|
248
|
+
urls: ["https://github.com/tlaplus/CommunityModules/releases/download/202611060952/CommunityModules-deps-202611060952.jar", "https://api.github.com/repos/tlaplus/CommunityModules/releases/assets/716678698"],
|
|
212
249
|
sha256: "a3b27c40f1fb5085f30c6ead37e918457f8cfc62c1e50a1dc668ef592ad2b5fb"
|
|
213
250
|
}];
|
|
214
251
|
const error = (cause) => new FormalError({ message: cause instanceof Error ? cause.message : String(cause) });
|
|
@@ -219,7 +256,7 @@ const messages = (output) => [...output.matchAll(/@!@!@STARTMSG (\d+):([0-4]) @!
|
|
|
219
256
|
text: (match[3] ?? "").trim()
|
|
220
257
|
}));
|
|
221
258
|
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* () {
|
|
259
|
+
const make$1 = Effect.gen(function* () {
|
|
223
260
|
const fs = yield* FileSystem;
|
|
224
261
|
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
|
|
225
262
|
const path = yield* Path;
|
|
@@ -239,10 +276,21 @@ const make = Effect.gen(function* () {
|
|
|
239
276
|
prefix: `.${pin.file}.`,
|
|
240
277
|
suffix: ".part"
|
|
241
278
|
});
|
|
242
|
-
const bytes = yield*
|
|
279
|
+
const { url, bytes } = yield* Effect.gen(function* () {
|
|
280
|
+
const failures = [];
|
|
281
|
+
for (const url of pin.urls) {
|
|
282
|
+
const result = yield* HttpClient.get(url, { headers: { Accept: "application/octet-stream" } }).pipe(Effect.flatMap((response) => response.status === 200 ? response.arrayBuffer.pipe(Effect.mapError(error)) : Effect.fail(new FormalError({ message: `HTTP ${response.status}` }))), Effect.map((buffer) => new Uint8Array(buffer)), Effect.timeout("120 seconds"), Effect.provide(FetchHttpClient.layer), Effect.result);
|
|
283
|
+
if (Result.isSuccess(result)) return {
|
|
284
|
+
url,
|
|
285
|
+
bytes: result.success
|
|
286
|
+
};
|
|
287
|
+
failures.push(`${url}: ${error(result.failure).message}`);
|
|
288
|
+
}
|
|
289
|
+
return yield* new FormalError({ message: failures.join("; ") });
|
|
290
|
+
});
|
|
243
291
|
yield* fs.writeFile(temp, bytes);
|
|
244
292
|
const actual = digest(bytes);
|
|
245
|
-
if (actual !== pin.sha256) return yield* new FormalError({ message: `${
|
|
293
|
+
if (actual !== pin.sha256) return yield* new FormalError({ message: `${url} has sha256 ${actual}, expected ${pin.sha256}` });
|
|
246
294
|
return yield* fs.rename(temp, file);
|
|
247
295
|
}));
|
|
248
296
|
jars.push(file);
|
|
@@ -344,7 +392,19 @@ const make = Effect.gen(function* () {
|
|
|
344
392
|
"tlc2.TLC",
|
|
345
393
|
"-tool",
|
|
346
394
|
"-workers",
|
|
347
|
-
"auto",
|
|
395
|
+
input.mode._tag === "Simulate" ? "1" : "auto",
|
|
396
|
+
...input.mode._tag === "Simulate" ? [
|
|
397
|
+
"-simulate",
|
|
398
|
+
`num=${input.mode.num}`,
|
|
399
|
+
"-depth",
|
|
400
|
+
String(input.mode.depth),
|
|
401
|
+
"-seed",
|
|
402
|
+
String(input.mode.seed)
|
|
403
|
+
] : [
|
|
404
|
+
"-lncheck",
|
|
405
|
+
"final",
|
|
406
|
+
...input.mode.coverage ? ["-coverage", "60"] : []
|
|
407
|
+
],
|
|
348
408
|
"-metadir",
|
|
349
409
|
temp,
|
|
350
410
|
"-noGenerateSpecTE",
|
|
@@ -365,8 +425,741 @@ const make = Effect.gen(function* () {
|
|
|
365
425
|
};
|
|
366
426
|
}).pipe(Effect.mapError(error));
|
|
367
427
|
var Tla = class Tla extends Context.Service()("formal/Tla") {
|
|
368
|
-
static layer = Layer.effect(Tla, make);
|
|
428
|
+
static layer = Layer.effect(Tla, make$1);
|
|
429
|
+
};
|
|
430
|
+
// src/formal/arithmetic.ts
|
|
431
|
+
const matches$1 = (glob, file) => new Bun.Glob(glob).match(file);
|
|
432
|
+
const moduleFile = /\.(?:[cm]?[jt]s|[jt]sx)$/u;
|
|
433
|
+
const arithmetic = (repo) => Effect.gen(function* () {
|
|
434
|
+
if (repo.file.arithmetic === void 0) {
|
|
435
|
+
yield* Console.log("arithmetic: specs.json lists no arithmetic globs; rule 16 is off");
|
|
436
|
+
return [];
|
|
437
|
+
}
|
|
438
|
+
return repo.file.arithmetic.flatMap((glob) => {
|
|
439
|
+
const files = repo.tracked.filter((f) => matches$1(glob, f));
|
|
440
|
+
return files.length === 0 ? [{
|
|
441
|
+
file: "specs.json",
|
|
442
|
+
message: `arithmetic glob ${glob} matches no tracked file`
|
|
443
|
+
}] : files.filter((f) => !Object.values({
|
|
444
|
+
...repo.file.proofs,
|
|
445
|
+
...repo.file.kani
|
|
446
|
+
}).some((e) => e.covers.some((g) => matches$1(g, f)))).map((file) => ({
|
|
447
|
+
file,
|
|
448
|
+
message: `matches arithmetic glob ${glob} but no proof covers it`
|
|
449
|
+
}));
|
|
450
|
+
});
|
|
451
|
+
});
|
|
452
|
+
const run = Effect.fn("formal.proof.run")(function* (request) {
|
|
453
|
+
const fs = yield* FileSystem;
|
|
454
|
+
const child = yield* Effect.scoped(Effect.gen(function* () {
|
|
455
|
+
const p = yield* ChildProcess.make("bun", [fileURLToPath(new URL("./prover.ts", import.meta.url))], {
|
|
456
|
+
cwd: request.root,
|
|
457
|
+
env: { FORMAL_PROOF: yield* Schema.encodeEffect(Schema.fromJsonString(Request))(request) },
|
|
458
|
+
extendEnv: true,
|
|
459
|
+
stdin: "ignore",
|
|
460
|
+
killSignal: "SIGKILL"
|
|
461
|
+
});
|
|
462
|
+
return yield* Effect.all([
|
|
463
|
+
p.stdout.pipe(Stream.decodeText, Stream.mkString),
|
|
464
|
+
p.stderr.pipe(Stream.decodeText, Stream.mkString),
|
|
465
|
+
p.exitCode
|
|
466
|
+
], { concurrency: "unbounded" });
|
|
467
|
+
}).pipe(Effect.timeoutOption(`${request.timeoutSeconds + 30} seconds`)));
|
|
468
|
+
if (Option.isNone(child)) return `timed out after ${request.timeoutSeconds + 30} s`;
|
|
469
|
+
if (!(yield* fs.exists(request.result))) return `the proof exited ${child.value[2]} without a result: ${child.value[1].trim().split("\n").at(-1) ?? ""}`;
|
|
470
|
+
const result = Schema.decodeResult(Schema.fromJsonString(Result$1))(yield* fs.readFileString(request.result), { onExcessProperty: "error" });
|
|
471
|
+
return Result.isSuccess(result) ? result.success : "the proof wrote a malformed result";
|
|
472
|
+
});
|
|
473
|
+
const proofs = (repo) => Effect.scoped(Effect.gen(function* () {
|
|
474
|
+
const fs = yield* FileSystem;
|
|
475
|
+
const dir = yield* fs.makeTempDirectoryScoped({ prefix: "formal-proofs-" });
|
|
476
|
+
const problems = [];
|
|
477
|
+
let index = 0;
|
|
478
|
+
for (const [file, entry] of Object.entries(repo.file.proofs).toSorted(([a], [b]) => a.localeCompare(b))) {
|
|
479
|
+
if (!repo.tracked.includes(file)) continue;
|
|
480
|
+
const add = (message) => problems.push({
|
|
481
|
+
file,
|
|
482
|
+
message
|
|
483
|
+
});
|
|
484
|
+
const covered = repo.tracked.filter((f) => entry.covers.some((g) => matches$1(g, f)));
|
|
485
|
+
const before = problems.length;
|
|
486
|
+
for (const [i, edit] of entry.breaks.entries()) {
|
|
487
|
+
if (!covered.includes(edit.file)) {
|
|
488
|
+
add(`break ${i + 1}: ${edit.file} is not in this proof's covers`);
|
|
489
|
+
continue;
|
|
490
|
+
}
|
|
491
|
+
const count = (yield* fs.readFileString(join(repo.root, edit.file))).split(edit.find).length - 1;
|
|
492
|
+
if (count !== 1) add(`break ${i + 1} find matches ${count} times in ${edit.file}`);
|
|
493
|
+
}
|
|
494
|
+
if (problems.length !== before) continue;
|
|
495
|
+
const covers = yield* Effect.forEach(covered, (f) => fs.realPath(join(repo.root, f)), { concurrency: 1 });
|
|
496
|
+
const request = {
|
|
497
|
+
root: repo.root,
|
|
498
|
+
proof: join(repo.root, file),
|
|
499
|
+
kind: entry.kind,
|
|
500
|
+
covers,
|
|
501
|
+
samples: entry.kind === "z3" ? entry.samples : 0,
|
|
502
|
+
timeoutSeconds: entry.timeoutSeconds,
|
|
503
|
+
result: join(dir, `${index++}.json`)
|
|
504
|
+
};
|
|
505
|
+
const baseline = yield* run(request);
|
|
506
|
+
if (typeof baseline === "string") {
|
|
507
|
+
add(baseline);
|
|
508
|
+
continue;
|
|
509
|
+
}
|
|
510
|
+
if (baseline.outcome._tag !== "Proved") {
|
|
511
|
+
add(describe(baseline.outcome));
|
|
512
|
+
continue;
|
|
513
|
+
}
|
|
514
|
+
covered.forEach((f, i) => {
|
|
515
|
+
if (moduleFile.test(f) && !baseline.loaded.includes(covers[i] ?? "")) add(`covers ${f} but never loads it; a proof covers only code it runs`);
|
|
516
|
+
});
|
|
517
|
+
if (problems.length !== before) continue;
|
|
518
|
+
yield* Console.log(`proofs: ${file}: ${baseline.outcome.detail} in ${baseline.outcome.seconds.toFixed(2)} s`);
|
|
519
|
+
for (const [i, edit] of entry.breaks.entries()) {
|
|
520
|
+
const absolute = yield* fs.realPath(join(repo.root, edit.file));
|
|
521
|
+
const broken = yield* run({
|
|
522
|
+
...request,
|
|
523
|
+
result: join(dir, `${index++}.json`),
|
|
524
|
+
edit: {
|
|
525
|
+
...edit,
|
|
526
|
+
file: absolute
|
|
527
|
+
}
|
|
528
|
+
});
|
|
529
|
+
if (typeof broken === "string") add(`break ${i + 1} (${edit.file}): ${broken}; a break counts only as a counterexample, a mismatch or a throw`);
|
|
530
|
+
else if (!broken.loaded.includes(absolute)) add(`break ${i + 1}: the proof never loads ${edit.file}`);
|
|
531
|
+
else if (broken.outcome._tag === "Proved") add(`break ${i + 1} (${edit.file}): the proof still holds after the edit`);
|
|
532
|
+
else if ([
|
|
533
|
+
"Counterexample",
|
|
534
|
+
"Disagrees",
|
|
535
|
+
"Throws"
|
|
536
|
+
].includes(broken.outcome._tag)) yield* Console.log(`proofs: ${file}: break ${i + 1} ${broken.outcome._tag}`);
|
|
537
|
+
else add(`break ${i + 1} (${edit.file}): ${describe(broken.outcome)}; a break counts only as a counterexample, a mismatch or a throw`);
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
return problems;
|
|
541
|
+
})).pipe(Effect.mapError((cause) => new FormalError({ message: String(cause) })));
|
|
542
|
+
// src/formal/cfg.ts
|
|
543
|
+
const keywords = /* @__PURE__ */ new Set([
|
|
544
|
+
"CONSTANT",
|
|
545
|
+
"CONSTANTS",
|
|
546
|
+
"INIT",
|
|
547
|
+
"NEXT",
|
|
548
|
+
"SPECIFICATION",
|
|
549
|
+
"INVARIANT",
|
|
550
|
+
"INVARIANTS",
|
|
551
|
+
"PROPERTY",
|
|
552
|
+
"PROPERTIES",
|
|
553
|
+
"CONSTRAINT",
|
|
554
|
+
"CONSTRAINTS",
|
|
555
|
+
"ACTION_CONSTRAINT",
|
|
556
|
+
"ACTION_CONSTRAINTS",
|
|
557
|
+
"SYMMETRY",
|
|
558
|
+
"VIEW",
|
|
559
|
+
"ALIAS",
|
|
560
|
+
"POSTCONDITION",
|
|
561
|
+
"CHECK_DEADLOCK"
|
|
562
|
+
]);
|
|
563
|
+
const parseCfg = (source) => {
|
|
564
|
+
const tokens = source.replaceAll(/\(\*[\s\S]*?\*\)|\\\*[^\n]*/gu, " ").match(/"(?:\\.|[^"\\])*"|<-|[A-Za-z_][A-Za-z0-9_!]*|-?\d+|[^\s]/gu) ?? [];
|
|
565
|
+
const sections = [];
|
|
566
|
+
const properties = [];
|
|
567
|
+
const constants = [];
|
|
568
|
+
const invariants = [];
|
|
569
|
+
const checkDeadlock = [];
|
|
570
|
+
const fail = (message) => ({
|
|
571
|
+
_tag: "CfgError",
|
|
572
|
+
message
|
|
573
|
+
});
|
|
574
|
+
let i = 0;
|
|
575
|
+
while (i < tokens.length) {
|
|
576
|
+
const keyword = tokens[i++];
|
|
577
|
+
if (keyword === void 0 || !keywords.has(keyword)) return fail(`unknown config keyword ${keyword}`);
|
|
578
|
+
const operands = [];
|
|
579
|
+
while (i < tokens.length && !keywords.has(tokens[i] ?? "")) operands.push(tokens[i++] ?? "");
|
|
580
|
+
sections.push({
|
|
581
|
+
keyword,
|
|
582
|
+
text: operands.join(" ")
|
|
583
|
+
});
|
|
584
|
+
if (operands.length === 0) return fail(`missing operand for ${keyword}`);
|
|
585
|
+
if (keyword === "CONSTANT" || keyword === "CONSTANTS") {
|
|
586
|
+
let j = 0;
|
|
587
|
+
while (j < operands.length) {
|
|
588
|
+
const name = operands[j++];
|
|
589
|
+
const assignment = operands[j++];
|
|
590
|
+
if (name === void 0 || !/^[A-Za-z_][A-Za-z0-9_!]*$/u.test(name) || assignment !== "=" && assignment !== "<-") return fail(`unparseable constant ${name}`);
|
|
591
|
+
const value = [];
|
|
592
|
+
let depth = 0;
|
|
593
|
+
while (j < operands.length) {
|
|
594
|
+
const token = operands[j] ?? "";
|
|
595
|
+
if (depth === 0 && value.length > 0 && ["=", "<-"].includes(operands[j + 1] ?? "")) break;
|
|
596
|
+
if (token === "{") depth++;
|
|
597
|
+
if (token === "}") depth--;
|
|
598
|
+
value.push(token);
|
|
599
|
+
j++;
|
|
600
|
+
}
|
|
601
|
+
const raw = value.join(" ");
|
|
602
|
+
if (depth !== 0 || !/^(?:-?\d+|[A-Za-z_][A-Za-z0-9_!]*|"(?:\\.|[^"\\])*"|\{[\s\S]*\})$/u.test(raw)) return fail(`unparseable constant ${name} ${assignment} ${raw}`);
|
|
603
|
+
constants.push(assignment === "=" ? {
|
|
604
|
+
_tag: "Value",
|
|
605
|
+
name,
|
|
606
|
+
value: raw
|
|
607
|
+
} : {
|
|
608
|
+
_tag: "Substitution",
|
|
609
|
+
name,
|
|
610
|
+
by: raw
|
|
611
|
+
});
|
|
612
|
+
}
|
|
613
|
+
} else if (keyword === "CHECK_DEADLOCK") {
|
|
614
|
+
if (operands.length !== 1 || !["TRUE", "FALSE"].includes(operands[0] ?? "")) return fail("CHECK_DEADLOCK needs TRUE or FALSE");
|
|
615
|
+
checkDeadlock.push(operands[0] === "TRUE");
|
|
616
|
+
} else {
|
|
617
|
+
if (!operands.every((token) => /^[A-Za-z_][A-Za-z0-9_!]*$/u.test(token))) return fail(`invalid operands for ${keyword}`);
|
|
618
|
+
if (keyword.startsWith("PROPERT")) properties.push(...operands);
|
|
619
|
+
if (keyword.startsWith("INVARIANT")) invariants.push(...operands);
|
|
620
|
+
}
|
|
621
|
+
}
|
|
622
|
+
return {
|
|
623
|
+
sections,
|
|
624
|
+
constants,
|
|
625
|
+
invariants,
|
|
626
|
+
properties,
|
|
627
|
+
checkDeadlock
|
|
628
|
+
};
|
|
369
629
|
};
|
|
630
|
+
const integer = (constant) => constant._tag === "Value" && /^-?\d+$/u.test(constant.value) ? BigInt(constant.value) : void 0;
|
|
631
|
+
const derive = (cfg, plan) => {
|
|
632
|
+
const removed = /* @__PURE__ */ new Set([
|
|
633
|
+
"INVARIANT",
|
|
634
|
+
"INVARIANTS",
|
|
635
|
+
"PROPERTY",
|
|
636
|
+
"PROPERTIES"
|
|
637
|
+
]);
|
|
638
|
+
if (plan.simulation) for (const k of [
|
|
639
|
+
"INIT",
|
|
640
|
+
"NEXT",
|
|
641
|
+
"SPECIFICATION",
|
|
642
|
+
"SYMMETRY",
|
|
643
|
+
"VIEW",
|
|
644
|
+
"ALIAS",
|
|
645
|
+
"POSTCONDITION",
|
|
646
|
+
"CHECK_DEADLOCK"
|
|
647
|
+
]) removed.add(k);
|
|
648
|
+
const lines = cfg.sections.filter((s) => !removed.has(s.keyword)).map((s) => s.keyword + " " + s.text);
|
|
649
|
+
if (plan.target) lines.push((plan.target.level < 2 ? "INVARIANT " : "PROPERTY ") + plan.target.name);
|
|
650
|
+
if (plan.simulation) {
|
|
651
|
+
const s = plan.simulation;
|
|
652
|
+
lines.push("CONSTANTS", ...s.overrides, "INIT " + s.init, "NEXT " + s.next, "INVARIANT Formal_Export", "CHECK_DEADLOCK FALSE");
|
|
653
|
+
}
|
|
654
|
+
return lines.join("\n") + "\n";
|
|
655
|
+
};
|
|
656
|
+
// src/formal/coverage.ts
|
|
657
|
+
const span = (text) => {
|
|
658
|
+
const m = /line (\d+), col (\d+) to line (\d+), col (\d+) of module (\w+)/u.exec(text);
|
|
659
|
+
return m ? {
|
|
660
|
+
line: Number(m[1]),
|
|
661
|
+
col: Number(m[2]),
|
|
662
|
+
endLine: Number(m[3]),
|
|
663
|
+
endCol: Number(m[4]),
|
|
664
|
+
module: m[5] ?? ""
|
|
665
|
+
} : void 0;
|
|
666
|
+
};
|
|
667
|
+
const coverage = (messages) => {
|
|
668
|
+
const start = messages.findLastIndex((m) => m.code === 2201);
|
|
669
|
+
const end = messages.findIndex((m, i) => i > start && (m.code === 2202 || m.code === 2777));
|
|
670
|
+
if (start < 0 || end < 0) return "missing final coverage report";
|
|
671
|
+
const actions = [];
|
|
672
|
+
const zeros = [];
|
|
673
|
+
let scope;
|
|
674
|
+
let zeroDepth;
|
|
675
|
+
for (const m of messages.slice(start + 1, end)) {
|
|
676
|
+
if (m.code === 2779) continue;
|
|
677
|
+
if (m.code === 2772 || m.code === 2773) {
|
|
678
|
+
const loc = span(m.text);
|
|
679
|
+
const match = /^<(\w+) .*: ([\d,]+):([\d,]+)$/u.exec(m.text);
|
|
680
|
+
if (!loc || !match) return `unparseable coverage ${m.code}: ${m.text}`;
|
|
681
|
+
const name = match[1] ?? "";
|
|
682
|
+
const generated = BigInt((match[3] ?? "0").replaceAll(",", ""));
|
|
683
|
+
scope = {
|
|
684
|
+
name,
|
|
685
|
+
enabled: generated > 0n
|
|
686
|
+
};
|
|
687
|
+
zeroDepth = void 0;
|
|
688
|
+
if (m.code === 2772) actions.push({
|
|
689
|
+
name,
|
|
690
|
+
span: loc,
|
|
691
|
+
generated
|
|
692
|
+
});
|
|
693
|
+
} else if (m.code === 2221 || m.code === 2775) {
|
|
694
|
+
const loc = span(m.text);
|
|
695
|
+
const count = /: ([\d,]+)(?::[\d,]+)?$/u.exec(m.text);
|
|
696
|
+
if (!loc || !count) return `unparseable coverage ${m.code}: ${m.text}`;
|
|
697
|
+
const depth = m.text.indexOf("line");
|
|
698
|
+
if (zeroDepth !== void 0 && depth <= zeroDepth) zeroDepth = void 0;
|
|
699
|
+
if (scope?.enabled === true && BigInt((count[1] ?? "0").replaceAll(",", "")) === 0n && zeroDepth === void 0) {
|
|
700
|
+
zeros.push({
|
|
701
|
+
scope: scope.name,
|
|
702
|
+
span: loc
|
|
703
|
+
});
|
|
704
|
+
zeroDepth = depth;
|
|
705
|
+
}
|
|
706
|
+
} else if (m.code === 2774 || m.code === 2778) {
|
|
707
|
+
scope = void 0;
|
|
708
|
+
zeroDepth = void 0;
|
|
709
|
+
} else return `unparseable coverage ${m.code}: ${m.text}`;
|
|
710
|
+
}
|
|
711
|
+
return {
|
|
712
|
+
actions,
|
|
713
|
+
zeros
|
|
714
|
+
};
|
|
715
|
+
};
|
|
716
|
+
// src/formal/gate.ts
|
|
717
|
+
const make = Effect.gen(function* () {
|
|
718
|
+
const tla = yield* Tla;
|
|
719
|
+
const run = yield* Cache.make({
|
|
720
|
+
capacity: 1e4,
|
|
721
|
+
lookup: Effect.fn("formal.gate")(function* (m) {
|
|
722
|
+
const r = yield* tla.tlc({
|
|
723
|
+
spec: `${m.root}/${m.path}`,
|
|
724
|
+
config: `${m.root}/${m.config.path}`,
|
|
725
|
+
timeoutSeconds: m.entry.timeoutSeconds,
|
|
726
|
+
mode: {
|
|
727
|
+
_tag: "Check",
|
|
728
|
+
coverage: true
|
|
729
|
+
}
|
|
730
|
+
});
|
|
731
|
+
if (r._tag === "TimedOut") return {
|
|
732
|
+
_tag: "Failed",
|
|
733
|
+
message: `TLC timed out after ${m.entry.timeoutSeconds} s`
|
|
734
|
+
};
|
|
735
|
+
const failures = r.messages.filter((x) => x.severity >= 1 && x.severity <= 3);
|
|
736
|
+
const states = r.messages.find((x) => x.code === 2199)?.text.match(/([\d,]+) distinct states/u)?.[1]?.replaceAll(",", "");
|
|
737
|
+
if (r.code !== 0 || failures.length || !r.messages.some((x) => x.code === 2193) || states === void 0 || BigInt(states) === 0n) {
|
|
738
|
+
const meaning = /* @__PURE__ */ new Map([
|
|
739
|
+
[11, "deadlock"],
|
|
740
|
+
[12, "safety violation"],
|
|
741
|
+
[13, "liveness violation"],
|
|
742
|
+
[150, "parse failure"]
|
|
743
|
+
]);
|
|
744
|
+
return {
|
|
745
|
+
_tag: "Failed",
|
|
746
|
+
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)"}`
|
|
747
|
+
};
|
|
748
|
+
}
|
|
749
|
+
const c = coverage(r.messages);
|
|
750
|
+
return typeof c === "string" ? {
|
|
751
|
+
_tag: "Failed",
|
|
752
|
+
message: c
|
|
753
|
+
} : {
|
|
754
|
+
_tag: "Passed",
|
|
755
|
+
states,
|
|
756
|
+
seconds: r.seconds,
|
|
757
|
+
coverage: c
|
|
758
|
+
};
|
|
759
|
+
})
|
|
760
|
+
});
|
|
761
|
+
return { run: (model) => Cache.get(run, model) };
|
|
762
|
+
});
|
|
763
|
+
var Gate = class Gate extends Context.Service()("formal/Gate") {
|
|
764
|
+
static layer = Layer.effect(Gate, make);
|
|
765
|
+
};
|
|
766
|
+
// src/formal/git.ts
|
|
767
|
+
const changes = Effect.fn("formal.changes")(function* (root) {
|
|
768
|
+
const base = yield* Config.String("BASE").pipe(Config.withDefault("origin/main"));
|
|
769
|
+
const run = (args) => Effect.scoped(Effect.gen(function* () {
|
|
770
|
+
const p = yield* ChildProcess.make("git", args, {
|
|
771
|
+
cwd: root,
|
|
772
|
+
stdin: "ignore"
|
|
773
|
+
});
|
|
774
|
+
const [out, , code] = yield* Effect.all([
|
|
775
|
+
p.stdout.pipe(Stream.decodeText, Stream.mkString),
|
|
776
|
+
p.stderr.pipe(Stream.decodeText, Stream.mkString),
|
|
777
|
+
p.exitCode
|
|
778
|
+
], { concurrency: "unbounded" });
|
|
779
|
+
return {
|
|
780
|
+
out,
|
|
781
|
+
code: Number(code)
|
|
782
|
+
};
|
|
783
|
+
}));
|
|
784
|
+
const merge = yield* run([
|
|
785
|
+
"merge-base",
|
|
786
|
+
base,
|
|
787
|
+
"HEAD"
|
|
788
|
+
]);
|
|
789
|
+
if (merge.code !== 0) return {
|
|
790
|
+
_tag: "All",
|
|
791
|
+
base
|
|
792
|
+
};
|
|
793
|
+
const sha = merge.out.trim();
|
|
794
|
+
const diff = yield* run([
|
|
795
|
+
"diff",
|
|
796
|
+
"--name-only",
|
|
797
|
+
"-z",
|
|
798
|
+
sha
|
|
799
|
+
]);
|
|
800
|
+
return diff.code !== 0 ? {
|
|
801
|
+
_tag: "All",
|
|
802
|
+
base
|
|
803
|
+
} : {
|
|
804
|
+
_tag: "Changed",
|
|
805
|
+
sha,
|
|
806
|
+
files: diff.out.split("\0").filter(Boolean)
|
|
807
|
+
};
|
|
808
|
+
});
|
|
809
|
+
// src/formal/violations.ts
|
|
810
|
+
const sandbox = Effect.fn("formal.sandbox")(function* (repo, model) {
|
|
811
|
+
const fs = yield* FileSystem;
|
|
812
|
+
const dir = yield* fs.makeTempDirectoryScoped({ prefix: "formal-model-" });
|
|
813
|
+
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)}`);
|
|
814
|
+
return dir;
|
|
815
|
+
});
|
|
816
|
+
const held = Symbol("held");
|
|
817
|
+
const violate = Effect.fn("formal.violate")(function* (repo, m, target, edit) {
|
|
818
|
+
return yield* Effect.scoped(Effect.gen(function* () {
|
|
819
|
+
const fs = yield* FileSystem;
|
|
820
|
+
const tla = yield* Tla;
|
|
821
|
+
const dir = yield* sandbox(repo, m);
|
|
822
|
+
let cfg = m.config.cfg;
|
|
823
|
+
if (edit) {
|
|
824
|
+
const file = edit.file ?? m.path;
|
|
825
|
+
const changed = (yield* fs.readFileString(`${repo.root}/${file}`)).replace(edit.find, edit.replace);
|
|
826
|
+
if (file === m.config.path) {
|
|
827
|
+
const parsed = parseCfg(changed);
|
|
828
|
+
if ("_tag" in parsed) return parsed.message;
|
|
829
|
+
cfg = parsed;
|
|
830
|
+
} else yield* fs.writeFileString(`${dir}/${basename(file)}`, changed);
|
|
831
|
+
}
|
|
832
|
+
const level = m.semantics.operators.get(target)?.level ?? (cfg.properties.includes(target) ? 3 : 1);
|
|
833
|
+
const config = `${dir}/Check.cfg`;
|
|
834
|
+
yield* fs.writeFileString(config, derive(cfg, { target: {
|
|
835
|
+
name: target,
|
|
836
|
+
level
|
|
837
|
+
} }));
|
|
838
|
+
const run = yield* tla.tlc({
|
|
839
|
+
spec: `${dir}/${basename(m.path)}`,
|
|
840
|
+
config,
|
|
841
|
+
timeoutSeconds: m.entry.timeoutSeconds,
|
|
842
|
+
mode: {
|
|
843
|
+
_tag: "Check",
|
|
844
|
+
coverage: false
|
|
845
|
+
}
|
|
846
|
+
});
|
|
847
|
+
if (run._tag === "TimedOut") return `TLC timed out after ${m.entry.timeoutSeconds} s`;
|
|
848
|
+
if (run.code === 0) return held;
|
|
849
|
+
const violated = [
|
|
850
|
+
2107,
|
|
851
|
+
2108,
|
|
852
|
+
2110,
|
|
853
|
+
2112,
|
|
854
|
+
2116
|
|
855
|
+
];
|
|
856
|
+
if ([12, 13].includes(run.code) && run.messages.some((x) => violated.includes(x.code)) && !run.messages.some((x) => x.severity >= 1 && x.severity <= 3 && ![
|
|
857
|
+
...violated,
|
|
858
|
+
2121,
|
|
859
|
+
2264
|
|
860
|
+
].includes(x.code))) return void 0;
|
|
861
|
+
return `TLC exited ${run.code}; ${run.messages.find((x) => x.severity >= 1 && x.severity <= 3)?.text ?? "expected a violation of " + target}`;
|
|
862
|
+
}));
|
|
863
|
+
});
|
|
864
|
+
// src/formal/proofs.ts
|
|
865
|
+
const matches = (glob, file) => new Bun.Glob(glob).match(file);
|
|
866
|
+
const critical = (repo) => Effect.succeed([
|
|
867
|
+
...repo.file.critical.flatMap((glob) => repo.tracked.some((f) => matches(glob, f)) ? [] : [{
|
|
868
|
+
file: "specs.json",
|
|
869
|
+
message: `critical glob ${glob} matches no tracked file`
|
|
870
|
+
}]),
|
|
871
|
+
...Object.entries({
|
|
872
|
+
...repo.file.specs,
|
|
873
|
+
...repo.file.kani,
|
|
874
|
+
...repo.file.proofs
|
|
875
|
+
}).flatMap(([file, e]) => e.covers.flatMap((glob) => repo.tracked.some((f) => matches(glob, f)) ? [] : [{
|
|
876
|
+
file,
|
|
877
|
+
message: `covers glob ${glob} matches no tracked file`
|
|
878
|
+
}])),
|
|
879
|
+
...repo.tracked.flatMap((file) => {
|
|
880
|
+
const glob = repo.file.critical.find((g) => matches(g, file));
|
|
881
|
+
return glob !== void 0 && !Object.values({
|
|
882
|
+
...repo.file.specs,
|
|
883
|
+
...repo.file.kani,
|
|
884
|
+
...repo.file.proofs
|
|
885
|
+
}).some((e) => (!("mode" in e) || e.mode !== "library") && e.covers.some((g) => matches(g, file))) ? [{
|
|
886
|
+
file,
|
|
887
|
+
message: `matches critical glob ${glob} but no spec covers it`
|
|
888
|
+
}] : [];
|
|
889
|
+
})
|
|
890
|
+
]);
|
|
891
|
+
const fairness = (repo) => Effect.succeed(repo.specs.flatMap((s) => s.analysis._tag === "Parsed" ? s.analysis.semantics.fairness.filter((f) => !f.valid).map((f) => ({
|
|
892
|
+
file: s.path,
|
|
893
|
+
message: `${f.kind} at line ${f.line}; fairness must be WF_vars or SF_vars`
|
|
894
|
+
})) : []));
|
|
895
|
+
const coverageCheck = (repo) => Effect.gen(function* () {
|
|
896
|
+
const gate = yield* Gate;
|
|
897
|
+
const fs = yield* FileSystem;
|
|
898
|
+
const problems = [];
|
|
899
|
+
for (const m of repo.models.filter((m) => m.entry.mode === "gate")) {
|
|
900
|
+
const result = yield* gate.run(m);
|
|
901
|
+
if (result._tag === "Failed") continue;
|
|
902
|
+
const start = problems.length;
|
|
903
|
+
for (const a of result.coverage.actions.filter((a) => a.generated === 0n)) problems.push({
|
|
904
|
+
file: m.path,
|
|
905
|
+
message: `action ${a.name} (line ${a.span.line}) is never enabled`
|
|
906
|
+
});
|
|
907
|
+
for (const z of result.coverage.zeros) {
|
|
908
|
+
const candidate = join(dirname(m.path), `${z.span.module}.tla`);
|
|
909
|
+
const file = repo.tracked.includes(candidate) ? candidate : m.path;
|
|
910
|
+
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);
|
|
911
|
+
problems.push({
|
|
912
|
+
file,
|
|
913
|
+
message: `line ${z.span.line}, col ${z.span.col} to line ${z.span.endLine}, col ${z.span.endCol} never runs in ${z.scope}: ${source}`
|
|
914
|
+
});
|
|
915
|
+
}
|
|
916
|
+
if (problems.length === start) yield* Console.log(`coverage: ${m.path}: no dead logic`);
|
|
917
|
+
}
|
|
918
|
+
return problems;
|
|
919
|
+
}).pipe(Effect.mapError((e) => new FormalError({ message: String(e) })));
|
|
920
|
+
const witnesses = (repo) => Effect.gen(function* () {
|
|
921
|
+
const gate = yield* Gate;
|
|
922
|
+
const problems = [];
|
|
923
|
+
for (const m of repo.models) for (const name of m.entry.witnesses) {
|
|
924
|
+
const op = m.semantics.operators.get(name);
|
|
925
|
+
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;
|
|
926
|
+
if (message !== void 0) {
|
|
927
|
+
problems.push({
|
|
928
|
+
file: m.path,
|
|
929
|
+
message
|
|
930
|
+
});
|
|
931
|
+
continue;
|
|
932
|
+
}
|
|
933
|
+
if (m.entry.mode !== "gate" || (yield* gate.run(m))._tag === "Failed") continue;
|
|
934
|
+
const result = yield* violate(repo, m, name);
|
|
935
|
+
if (result !== void 0) problems.push({
|
|
936
|
+
file: m.path,
|
|
937
|
+
message: result === held ? `${name} holds; a witness must be violated` : result
|
|
938
|
+
});
|
|
939
|
+
else yield* Console.log(`witnesses: ${m.path}: ${name} violated`);
|
|
940
|
+
}
|
|
941
|
+
return problems;
|
|
942
|
+
}).pipe(Effect.mapError((e) => new FormalError({ message: String(e) })));
|
|
943
|
+
const breaks = (repo) => Effect.gen(function* () {
|
|
944
|
+
const gate = yield* Gate;
|
|
945
|
+
const fs = yield* FileSystem;
|
|
946
|
+
const changed = yield* changes(repo.root);
|
|
947
|
+
const problems = [];
|
|
948
|
+
if (changed._tag === "All") yield* Console.log(`breaks: BASE ${changed.base} does not resolve; running every break`);
|
|
949
|
+
for (const m of repo.models) {
|
|
950
|
+
const targets = [...m.config.cfg.invariants, ...m.config.cfg.properties];
|
|
951
|
+
for (const target of targets) if (!m.entry.breaks.some((b) => b.violates === target)) problems.push({
|
|
952
|
+
file: m.config.path,
|
|
953
|
+
message: `${target} has no break`
|
|
954
|
+
});
|
|
955
|
+
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));
|
|
956
|
+
if (!dirty && m.entry.mode === "gate") yield* Console.log(`breaks: ${m.path}: unchanged since ${changed.sha.slice(0, 7)}, not run`);
|
|
957
|
+
for (const [index, b] of m.entry.breaks.entries()) {
|
|
958
|
+
const label = `break ${index + 1}`;
|
|
959
|
+
const file = b.file ?? m.path;
|
|
960
|
+
if (!targets.includes(b.violates)) {
|
|
961
|
+
problems.push({
|
|
962
|
+
file: m.path,
|
|
963
|
+
message: `${label}: ${b.violates} is not checked by ${m.config.path}`
|
|
964
|
+
});
|
|
965
|
+
continue;
|
|
966
|
+
}
|
|
967
|
+
if (!repo.tracked.includes(file) || !(file === m.config.path || dirname(file) === dirname(m.path) && file.endsWith(".tla"))) {
|
|
968
|
+
problems.push({
|
|
969
|
+
file: m.path,
|
|
970
|
+
message: `${label}: ${file} must be a tracked module in the spec directory or its config`
|
|
971
|
+
});
|
|
972
|
+
continue;
|
|
973
|
+
}
|
|
974
|
+
const count = (yield* fs.readFileString(`${repo.root}/${file}`)).split(b.find).length - 1;
|
|
975
|
+
if (count !== 1) {
|
|
976
|
+
problems.push({
|
|
977
|
+
file: m.path,
|
|
978
|
+
message: `${label} find matches ${count} times in ${file}`
|
|
979
|
+
});
|
|
980
|
+
continue;
|
|
981
|
+
}
|
|
982
|
+
if (!dirty || m.entry.mode !== "gate" || (yield* gate.run(m))._tag === "Failed") continue;
|
|
983
|
+
const result = yield* violate(repo, m, b.violates, b);
|
|
984
|
+
if (result !== void 0) problems.push({
|
|
985
|
+
file: m.path,
|
|
986
|
+
message: `${label} (${b.violates}): ${result === held ? `${b.violates} still holds after the edit` : result}`
|
|
987
|
+
});
|
|
988
|
+
else yield* Console.log(`breaks: ${m.path}: ${label} violated ${b.violates}`);
|
|
989
|
+
}
|
|
990
|
+
}
|
|
991
|
+
return problems;
|
|
992
|
+
}).pipe(Effect.mapError((e) => new FormalError({ message: String(e) })));
|
|
993
|
+
// src/formal/traces.ts
|
|
994
|
+
const quote = (s) => JSON.stringify(s);
|
|
995
|
+
const RawState = Schema.Record(Schema.String, Schema.Json);
|
|
996
|
+
const Raw = Schema.Array(RawState);
|
|
997
|
+
const decodeAction = Schema.decodeUnknownSync(Schema.Struct({
|
|
998
|
+
name: Schema.String,
|
|
999
|
+
context: Schema.optionalKey(Schema.NullOr(Schema.Record(Schema.String, Schema.Json))),
|
|
1000
|
+
location: Schema.optionalKey(Schema.Json)
|
|
1001
|
+
}));
|
|
1002
|
+
const normalize = (text, plan) => {
|
|
1003
|
+
const lines = text === "" ? [] : (text.endsWith("\n") ? text.slice(0, -1) : text).split("\n");
|
|
1004
|
+
if (lines.length !== plan.count) throw new Error(`only ${lines.length} of ${plan.count} behaviors reached depth ${plan.depth}`);
|
|
1005
|
+
return lines.map((line, index) => {
|
|
1006
|
+
const states = Schema.decodeSync(Schema.fromJsonString(Raw))(line);
|
|
1007
|
+
if (states.length !== plan.depth) throw new Error(`trace ${index} has ${states.length} states; expected ${plan.depth}`);
|
|
1008
|
+
const steps = states.map((s, i) => {
|
|
1009
|
+
if (s.formal_tick !== i) throw new Error(`trace ${index} state ${i} has wrong formal_tick`);
|
|
1010
|
+
const action = decodeAction(s._action);
|
|
1011
|
+
const name = action.name.replace(/^Formal_/u, "");
|
|
1012
|
+
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}`);
|
|
1013
|
+
const args = action.context ?? {};
|
|
1014
|
+
if (Object.keys(args).length !== (i === 0 ? 0 : plan.actions.get(name))) throw new Error(`trace ${index} state ${i} has wrong action arity`);
|
|
1015
|
+
const keys = Object.keys(s).toSorted();
|
|
1016
|
+
const expected = [
|
|
1017
|
+
...plan.variables,
|
|
1018
|
+
"_action",
|
|
1019
|
+
"formal_tick"
|
|
1020
|
+
].toSorted();
|
|
1021
|
+
if (JSON.stringify(keys) !== JSON.stringify(expected)) throw new Error(`trace ${index} state ${i} has wrong variables`);
|
|
1022
|
+
return {
|
|
1023
|
+
action: name,
|
|
1024
|
+
args,
|
|
1025
|
+
state: Object.fromEntries(Object.entries(s).filter(([k]) => k !== "_action" && k !== "formal_tick"))
|
|
1026
|
+
};
|
|
1027
|
+
});
|
|
1028
|
+
return JSON.stringify(sorted({
|
|
1029
|
+
index,
|
|
1030
|
+
constants: plan.constants,
|
|
1031
|
+
steps
|
|
1032
|
+
}));
|
|
1033
|
+
}).join("\n") + "\n";
|
|
1034
|
+
};
|
|
1035
|
+
const traces = (repo) => Effect.gen(function* () {
|
|
1036
|
+
const fs = yield* FileSystem;
|
|
1037
|
+
const tla = yield* Tla;
|
|
1038
|
+
const gate = yield* Gate;
|
|
1039
|
+
const problems = [];
|
|
1040
|
+
for (const m of repo.models) {
|
|
1041
|
+
if (m.entry.mode !== "gate" || !m.entry.traces) continue;
|
|
1042
|
+
const t = m.entry.traces;
|
|
1043
|
+
const result = yield* gate.run(m);
|
|
1044
|
+
if (result._tag === "Failed") continue;
|
|
1045
|
+
const finding = yield* Effect.scoped(Effect.gen(function* () {
|
|
1046
|
+
const section = (k) => m.config.cfg.sections.find((s) => s.keyword === k)?.text;
|
|
1047
|
+
const spec = section("SPECIFICATION");
|
|
1048
|
+
const pair = spec !== void 0 ? m.semantics.specifications.get(spec) : {
|
|
1049
|
+
init: section("INIT"),
|
|
1050
|
+
next: section("NEXT")
|
|
1051
|
+
};
|
|
1052
|
+
if (pair?.init === void 0 || pair.next === void 0) return "cannot derive zero-argument INIT/NEXT from config";
|
|
1053
|
+
const { init, next } = pair;
|
|
1054
|
+
const names = [...new Set(result.coverage.actions.map((a) => a.name))];
|
|
1055
|
+
const actions = /* @__PURE__ */ new Map();
|
|
1056
|
+
for (const name of names) {
|
|
1057
|
+
const op = m.semantics.operators.get(name);
|
|
1058
|
+
if (name === next || !op || op.params.some((p) => p.arity !== 0)) return `cannot wrap action ${name}: unnamed, INSTANCE'd, or higher-order action`;
|
|
1059
|
+
actions.set(name, op.params.length);
|
|
1060
|
+
}
|
|
1061
|
+
for (const name of Object.keys(t.steer ?? {})) if (!actions.has(name)) return `steer key ${name} is not an action`;
|
|
1062
|
+
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";
|
|
1063
|
+
const dir = yield* sandbox(repo, m);
|
|
1064
|
+
const raw = `${dir}/raw.ndjson`;
|
|
1065
|
+
const module = basename(m.path, ".tla");
|
|
1066
|
+
const slash = "\\";
|
|
1067
|
+
const defs = names.map((name) => {
|
|
1068
|
+
const params = m.semantics.operators.get(name)?.params.map((p) => p.name) ?? [];
|
|
1069
|
+
const args = params.length ? `(${params.join(", ")})` : "";
|
|
1070
|
+
const keep = t.steer?.[name];
|
|
1071
|
+
return `Formal_${name}${args} == Formal_M!${name}${args} /${slash} formal_tick' = formal_tick + 1${keep === void 0 ? "" : ` /${slash} Formal_TLC!RandomElement(1 .. ${keep}) = 1`}`;
|
|
1072
|
+
});
|
|
1073
|
+
const wrapper = [
|
|
1074
|
+
"---- MODULE FormalTraces ----",
|
|
1075
|
+
`EXTENDS ${module}`,
|
|
1076
|
+
"VARIABLE formal_tick",
|
|
1077
|
+
`Formal_M == INSTANCE ${module}`,
|
|
1078
|
+
"Formal_TLC == INSTANCE TLC",
|
|
1079
|
+
"Formal_Ext == INSTANCE TLCExt",
|
|
1080
|
+
"Formal_IO == INSTANCE IOUtils",
|
|
1081
|
+
`Formal_${init} == Formal_M!${init} /${slash} formal_tick = 0`,
|
|
1082
|
+
...defs,
|
|
1083
|
+
"Formal_Count == Formal_TLC!TLCGet(\"stats\").traces",
|
|
1084
|
+
`Formal_Fresh == 1 ${slash}notin DOMAIN Formal_TLC!TLCGet("all") ${slash}/ Formal_TLC!TLCGet(1) # Formal_Count`,
|
|
1085
|
+
`Formal_Export == (Formal_TLC!TLCGet("level") = ${t.depth} /${slash} Formal_Fresh) =>`,
|
|
1086
|
+
` /${slash} Formal_TLC!TLCSet(1, Formal_Count)`,
|
|
1087
|
+
` /${slash} Formal_IO!Serialize(<<Formal_Ext!Trace>>, ${quote(raw)}, [format |-> "NDJSON", charset |-> "UTF-8", openOptions |-> <<"WRITE", "CREATE", "APPEND">>])`,
|
|
1088
|
+
"====",
|
|
1089
|
+
""
|
|
1090
|
+
].join("\n");
|
|
1091
|
+
yield* fs.writeFileString(`${dir}/FormalTraces.tla`, wrapper);
|
|
1092
|
+
const cfg = derive(m.config.cfg, { simulation: {
|
|
1093
|
+
init,
|
|
1094
|
+
next,
|
|
1095
|
+
overrides: [`${init} <- Formal_${init}`, ...names.map((n) => `${n} <- Formal_${n}`)]
|
|
1096
|
+
} });
|
|
1097
|
+
yield* fs.writeFileString(`${dir}/FormalTraces.cfg`, cfg);
|
|
1098
|
+
const run = yield* tla.tlc({
|
|
1099
|
+
spec: `${dir}/FormalTraces.tla`,
|
|
1100
|
+
config: `${dir}/FormalTraces.cfg`,
|
|
1101
|
+
timeoutSeconds: m.entry.timeoutSeconds,
|
|
1102
|
+
mode: {
|
|
1103
|
+
_tag: "Simulate",
|
|
1104
|
+
num: t.count,
|
|
1105
|
+
depth: t.depth,
|
|
1106
|
+
seed: t.seed
|
|
1107
|
+
}
|
|
1108
|
+
});
|
|
1109
|
+
if (run._tag === "TimedOut") return `simulation timed out after ${m.entry.timeoutSeconds} s`;
|
|
1110
|
+
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("; ")}`;
|
|
1111
|
+
const text = (yield* fs.exists(raw)) ? yield* fs.readFileString(raw) : "";
|
|
1112
|
+
const normalized = yield* Effect.try({
|
|
1113
|
+
try: () => normalize(text, {
|
|
1114
|
+
count: t.count,
|
|
1115
|
+
depth: t.depth,
|
|
1116
|
+
init,
|
|
1117
|
+
actions,
|
|
1118
|
+
variables: m.semantics.variables,
|
|
1119
|
+
constants: Object.fromEntries(m.config.cfg.constants.flatMap((c) => c._tag === "Value" ? [[c.name, /^-?\d+$/u.test(c.value) ? Number(c.value) : c.value]] : []))
|
|
1120
|
+
}),
|
|
1121
|
+
catch: (e) => new FormalError({ message: e instanceof Error ? e.message : String(e) })
|
|
1122
|
+
});
|
|
1123
|
+
const file = `${dir}/traces.ndjson`;
|
|
1124
|
+
yield* fs.writeFileString(file, normalized);
|
|
1125
|
+
let owner = dirname(t.conformance);
|
|
1126
|
+
while (owner !== "." && !repo.tracked.includes(`${owner}/package.json`)) owner = dirname(owner);
|
|
1127
|
+
const started = yield* Clock.currentTimeMillis;
|
|
1128
|
+
const conformance = yield* Effect.scoped(Effect.gen(function* () {
|
|
1129
|
+
const p = yield* ChildProcess.make("vp", [
|
|
1130
|
+
"test",
|
|
1131
|
+
"run",
|
|
1132
|
+
relative(owner, t.conformance)
|
|
1133
|
+
], {
|
|
1134
|
+
cwd: `${repo.root}/${owner}`,
|
|
1135
|
+
env: { [FORMAL_TRACES]: file },
|
|
1136
|
+
extendEnv: true,
|
|
1137
|
+
stdin: "ignore",
|
|
1138
|
+
killSignal: "SIGKILL"
|
|
1139
|
+
});
|
|
1140
|
+
return yield* Effect.all([
|
|
1141
|
+
p.stdout.pipe(Stream.decodeText, Stream.mkString),
|
|
1142
|
+
p.stderr.pipe(Stream.decodeText, Stream.mkString),
|
|
1143
|
+
p.exitCode
|
|
1144
|
+
], { concurrency: "unbounded" });
|
|
1145
|
+
}).pipe(Effect.timeoutOption(`${m.entry.timeoutSeconds} seconds`)));
|
|
1146
|
+
if (Option.isNone(conformance)) return `conformance test ${t.conformance} timed out`;
|
|
1147
|
+
if (Number(conformance.value[2]) !== 0) {
|
|
1148
|
+
yield* Console.log((conformance.value[0] + conformance.value[1]).slice(-4e3));
|
|
1149
|
+
return `conformance test ${t.conformance} failed (vp test exited ${conformance.value[2]})`;
|
|
1150
|
+
}
|
|
1151
|
+
const receipt = receiptPath(file);
|
|
1152
|
+
const replayed = (yield* fs.exists(receipt)) ? (yield* fs.readFileString(receipt)).trim() : "0";
|
|
1153
|
+
if (replayed !== String(t.count)) return `conformance test replayed ${replayed} of ${t.count} traces; register them with replayTraces`;
|
|
1154
|
+
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`);
|
|
1155
|
+
})).pipe(Effect.catch((e) => Effect.succeed(e.message)));
|
|
1156
|
+
if (finding !== void 0) problems.push({
|
|
1157
|
+
file: m.path,
|
|
1158
|
+
message: finding
|
|
1159
|
+
});
|
|
1160
|
+
}
|
|
1161
|
+
return problems;
|
|
1162
|
+
});
|
|
370
1163
|
// src/formal/checks.ts
|
|
371
1164
|
const configs = (repo) => repo.specs.filter((s) => s.analysis._tag === "Parsed").flatMap((s) => s.configs).flatMap((c) => "_tag" in c.cfg ? [] : [{
|
|
372
1165
|
path: c.path,
|
|
@@ -382,22 +1175,34 @@ const specs = (repo) => Effect.succeed([
|
|
|
382
1175
|
file: s.path,
|
|
383
1176
|
message: "tracked spec has no specs.json entry"
|
|
384
1177
|
}] : [],
|
|
385
|
-
...s.entry
|
|
1178
|
+
...s.entry && s.entry.mode !== "library" && !s.configs.some((c) => c.path === s.path.replace(/\.tla$/u, ".cfg")) ? [{
|
|
386
1179
|
file: s.path,
|
|
387
|
-
message: "
|
|
1180
|
+
message: "runnable spec has no matching .cfg"
|
|
1181
|
+
}] : [],
|
|
1182
|
+
...s.entry?.mode === "library" && s.configs.length ? [{
|
|
1183
|
+
file: s.path,
|
|
1184
|
+
message: "library spec must not have a config"
|
|
388
1185
|
}] : [],
|
|
389
1186
|
...s.configs.flatMap((c) => "_tag" in c.cfg ? [{
|
|
390
1187
|
file: c.path,
|
|
391
1188
|
message: c.cfg.message
|
|
392
1189
|
}] : [])
|
|
393
1190
|
]),
|
|
394
|
-
...
|
|
1191
|
+
...repo.tracked.filter((f) => f.endsWith(".proof.ts") && !repo.file.proofs[f] && repo.decodeErrors.length === 0).map((file) => ({
|
|
1192
|
+
file,
|
|
1193
|
+
message: "tracked proof has no specs.json entry"
|
|
1194
|
+
})),
|
|
1195
|
+
...Object.keys({
|
|
1196
|
+
...repo.file.specs,
|
|
1197
|
+
...repo.file.kani,
|
|
1198
|
+
...repo.file.proofs
|
|
1199
|
+
}).filter((f) => !repo.tracked.includes(f)).map((file) => ({
|
|
395
1200
|
file,
|
|
396
1201
|
message: "entry names no tracked file"
|
|
397
1202
|
})),
|
|
398
1203
|
...repo.orphans.map((file) => ({
|
|
399
1204
|
file,
|
|
400
|
-
message: "
|
|
1205
|
+
message: "config pairs with no module; give a variant its own module that EXTENDS X"
|
|
401
1206
|
}))
|
|
402
1207
|
]);
|
|
403
1208
|
const sany = (repo) => Effect.succeed(repo.specs.flatMap((s) => s.analysis._tag === "Unparsed" ? [{
|
|
@@ -458,82 +1263,119 @@ const format = (repo) => Effect.gen(function* () {
|
|
|
458
1263
|
}), { concurrency: 4 })).flat();
|
|
459
1264
|
}).pipe(Effect.mapError((cause) => new FormalError({ message: String(cause) })));
|
|
460
1265
|
const tlc = (repo) => Effect.gen(function* () {
|
|
461
|
-
const
|
|
1266
|
+
const gate = yield* Gate;
|
|
462
1267
|
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
|
|
1268
|
+
for (const m of repo.models.filter((m) => m.entry.mode === "gate")) {
|
|
1269
|
+
const result = yield* gate.run(m);
|
|
1270
|
+
if (result._tag === "Failed") problems.push({
|
|
1271
|
+
file: m.config.path,
|
|
1272
|
+
message: result.message
|
|
471
1273
|
});
|
|
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`);
|
|
1274
|
+
else yield* Console.log(`tlc: ${m.config.path}: ${result.states} distinct states in ${result.seconds} s`);
|
|
493
1275
|
}
|
|
494
1276
|
return problems;
|
|
495
1277
|
});
|
|
496
1278
|
const checks = {
|
|
497
1279
|
specs,
|
|
1280
|
+
critical,
|
|
1281
|
+
arithmetic,
|
|
498
1282
|
sany,
|
|
499
1283
|
typeok,
|
|
500
1284
|
deadlock,
|
|
1285
|
+
fairness,
|
|
501
1286
|
constants,
|
|
502
1287
|
format,
|
|
503
|
-
tlc
|
|
1288
|
+
tlc,
|
|
1289
|
+
coverage: coverageCheck,
|
|
1290
|
+
witnesses,
|
|
1291
|
+
breaks,
|
|
1292
|
+
traces,
|
|
1293
|
+
proofs
|
|
504
1294
|
};
|
|
505
1295
|
// src/formal/specs.ts
|
|
506
1296
|
const RepoPath = Schema.String.check(Schema.isPattern(/^(?!\/)(?!(?:.*\/)?\.\.(?:\/|$))[^\0\\]+$/u));
|
|
507
1297
|
const OperatorName = Schema.String.check(Schema.isPattern(/^[A-Za-z_][A-Za-z0-9_]*$/u));
|
|
508
|
-
const
|
|
1298
|
+
const Common = {
|
|
509
1299
|
synced: Schema.String.check(Schema.isPattern(/^[0-9a-f]{40}$/u)),
|
|
510
|
-
covers: Schema.NonEmptyArray(Schema.NonEmptyString)
|
|
511
|
-
|
|
1300
|
+
covers: Schema.NonEmptyArray(Schema.NonEmptyString)
|
|
1301
|
+
};
|
|
1302
|
+
const Runtime = {
|
|
1303
|
+
...Common,
|
|
512
1304
|
timeoutSeconds: Schema.Int.check(Schema.isBetween({
|
|
513
1305
|
minimum: 1,
|
|
514
1306
|
maximum: 1800
|
|
515
|
-
})).pipe(Schema.withDecodingDefaultKey(Effect.succeed(60)))
|
|
516
|
-
|
|
1307
|
+
})).pipe(Schema.withDecodingDefaultKey(Effect.succeed(60)))
|
|
1308
|
+
};
|
|
1309
|
+
const Proof = {
|
|
1310
|
+
...Runtime,
|
|
1311
|
+
witnesses: Schema.NonEmptyArray(OperatorName).check(Schema.makeFilter((names) => new Set(names).size === names.length || "witnesses must be unique")),
|
|
517
1312
|
breaks: Schema.Array(Schema.Struct({
|
|
518
1313
|
violates: OperatorName,
|
|
519
1314
|
find: Schema.NonEmptyString,
|
|
520
1315
|
replace: Schema.String,
|
|
521
1316
|
file: Schema.optionalKey(RepoPath)
|
|
522
|
-
})).pipe(Schema.withDecodingDefaultKey(Effect.succeed([])))
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
1317
|
+
})).pipe(Schema.withDecodingDefaultKey(Effect.succeed([])))
|
|
1318
|
+
};
|
|
1319
|
+
const Entry = Schema.Union([
|
|
1320
|
+
Schema.Struct({
|
|
1321
|
+
...Proof,
|
|
1322
|
+
mode: Schema.Literal("gate"),
|
|
1323
|
+
traces: Schema.optionalKey(Schema.Struct({
|
|
1324
|
+
seed: Schema.Int.check(Schema.isGreaterThanOrEqualTo(0)),
|
|
1325
|
+
depth: Schema.Int.check(Schema.isBetween({
|
|
1326
|
+
minimum: 1,
|
|
1327
|
+
maximum: 1e4
|
|
1328
|
+
})),
|
|
1329
|
+
count: Schema.Int.check(Schema.isBetween({
|
|
1330
|
+
minimum: 1,
|
|
1331
|
+
maximum: 1e5
|
|
1332
|
+
})),
|
|
1333
|
+
steer: Schema.optionalKey(Schema.Record(OperatorName, Schema.Int.check(Schema.isBetween({
|
|
1334
|
+
minimum: 2,
|
|
1335
|
+
maximum: 1e3
|
|
1336
|
+
})))),
|
|
1337
|
+
conformance: RepoPath
|
|
532
1338
|
}))
|
|
533
|
-
})
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
1339
|
+
}),
|
|
1340
|
+
Schema.Struct({
|
|
1341
|
+
...Proof,
|
|
1342
|
+
mode: Schema.Literal("manual")
|
|
1343
|
+
}),
|
|
1344
|
+
Schema.Struct({
|
|
1345
|
+
...Common,
|
|
1346
|
+
mode: Schema.Literal("library")
|
|
1347
|
+
})
|
|
1348
|
+
]);
|
|
1349
|
+
const ArithmeticCommon = {
|
|
1350
|
+
covers: Common.covers,
|
|
1351
|
+
timeoutSeconds: Runtime.timeoutSeconds,
|
|
1352
|
+
breaks: Schema.NonEmptyArray(Schema.Struct({
|
|
1353
|
+
file: RepoPath,
|
|
1354
|
+
find: Schema.NonEmptyString,
|
|
1355
|
+
replace: Schema.String
|
|
1356
|
+
}))
|
|
1357
|
+
};
|
|
1358
|
+
const ArithmeticProof = Schema.Union([Schema.Struct({
|
|
1359
|
+
...ArithmeticCommon,
|
|
1360
|
+
kind: Schema.Literal("every")
|
|
1361
|
+
}), Schema.Struct({
|
|
1362
|
+
...ArithmeticCommon,
|
|
1363
|
+
kind: Schema.Literal("z3"),
|
|
1364
|
+
samples: Schema.Int.check(Schema.isBetween({
|
|
1365
|
+
minimum: 1e4,
|
|
1366
|
+
maximum: 1e6
|
|
1367
|
+
})).pipe(Schema.withDecodingDefaultKey(Effect.succeed(1e4)))
|
|
1368
|
+
})]);
|
|
1369
|
+
const SpecsFile = Schema.Struct({
|
|
1370
|
+
critical: Schema.NonEmptyArray(Schema.NonEmptyString),
|
|
1371
|
+
arithmetic: Schema.optionalKey(Schema.Array(Schema.NonEmptyString)),
|
|
1372
|
+
proofs: Schema.Record(RepoPath.check(Schema.isPattern(/\.proof\.ts$/u)), ArithmeticProof).pipe(Schema.withDecodingDefaultKey(Effect.succeed({}))),
|
|
1373
|
+
specs: Schema.Record(RepoPath.check(Schema.isPattern(/\.tla$/u)), Entry),
|
|
1374
|
+
kani: Schema.Record(RepoPath.check(Schema.isPattern(/\.rs$/u)), Schema.Struct({
|
|
1375
|
+
...Runtime,
|
|
1376
|
+
mode: Schema.Literals(["gate", "manual"])
|
|
1377
|
+
})).pipe(Schema.withDecodingDefaultKey(Effect.succeed({})))
|
|
1378
|
+
});
|
|
537
1379
|
const load = Effect.fn("formal.load")(function* (root) {
|
|
538
1380
|
const fs = yield* FileSystem;
|
|
539
1381
|
const path = yield* Path;
|
|
@@ -545,7 +1387,12 @@ const load = Effect.fn("formal.load")(function* (root) {
|
|
|
545
1387
|
const [files, code] = yield* Effect.all([git.stdout.pipe(Stream.decodeText, Stream.mkString), git.exitCode], { concurrency: "unbounded" });
|
|
546
1388
|
if (Number(code) !== 0) return yield* new FormalError({ message: "git ls-files failed" });
|
|
547
1389
|
const tracked = yield* Effect.filter(files.split("\0").filter(Boolean), (file) => fs.exists(path.join(root, file)));
|
|
548
|
-
let file = {
|
|
1390
|
+
let file = {
|
|
1391
|
+
critical: [],
|
|
1392
|
+
specs: {},
|
|
1393
|
+
kani: {},
|
|
1394
|
+
proofs: {}
|
|
1395
|
+
};
|
|
549
1396
|
let decodeErrors = [];
|
|
550
1397
|
if (yield* fs.exists(path.join(root, "specs.json"))) {
|
|
551
1398
|
const raw = yield* fs.readFileString(path.join(root, "specs.json"));
|
|
@@ -561,7 +1408,7 @@ const load = Effect.fn("formal.load")(function* (root) {
|
|
|
561
1408
|
const paired = /* @__PURE__ */ new Set();
|
|
562
1409
|
const specs = yield* Effect.forEach(modules, (module) => Effect.gen(function* () {
|
|
563
1410
|
const stem = module.slice(0, -4);
|
|
564
|
-
const matches = configs.filter((c) => c === `${stem}.cfg`
|
|
1411
|
+
const matches = configs.filter((c) => c === `${stem}.cfg`);
|
|
565
1412
|
const parsed = yield* Effect.forEach(matches, (c) => Effect.gen(function* () {
|
|
566
1413
|
paired.add(c);
|
|
567
1414
|
return {
|
|
@@ -571,7 +1418,7 @@ const load = Effect.fn("formal.load")(function* (root) {
|
|
|
571
1418
|
}), { concurrency: 1 });
|
|
572
1419
|
return {
|
|
573
1420
|
path: module,
|
|
574
|
-
entry: file[module],
|
|
1421
|
+
entry: file.specs[module],
|
|
575
1422
|
analysis: yield* tla.analyze(path.join(root, module)),
|
|
576
1423
|
configs: parsed
|
|
577
1424
|
};
|
|
@@ -581,6 +1428,19 @@ const load = Effect.fn("formal.load")(function* (root) {
|
|
|
581
1428
|
tracked,
|
|
582
1429
|
file,
|
|
583
1430
|
specs,
|
|
1431
|
+
models: specs.flatMap((s) => {
|
|
1432
|
+
const c = s.configs[0];
|
|
1433
|
+
return s.analysis._tag === "Parsed" && s.entry && s.entry.mode !== "library" && c && !("_tag" in c.cfg) ? [{
|
|
1434
|
+
root,
|
|
1435
|
+
path: s.path,
|
|
1436
|
+
entry: s.entry,
|
|
1437
|
+
semantics: s.analysis.semantics,
|
|
1438
|
+
config: {
|
|
1439
|
+
path: c.path,
|
|
1440
|
+
cfg: c.cfg
|
|
1441
|
+
}
|
|
1442
|
+
}] : [];
|
|
1443
|
+
}),
|
|
584
1444
|
orphans: configs.filter((c) => !paired.has(c)),
|
|
585
1445
|
decodeErrors
|
|
586
1446
|
};
|
|
@@ -599,11 +1459,11 @@ const formal = Effect.fn("formal")(function* (root) {
|
|
|
599
1459
|
check: name
|
|
600
1460
|
})));
|
|
601
1461
|
}
|
|
602
|
-
findings.sort((a, b) => a.file.localeCompare(b.file) || a.check.
|
|
1462
|
+
findings.sort((a, b) => a.file.localeCompare(b.file) || Object.keys(checks).indexOf(a.check) - Object.keys(checks).indexOf(b.check));
|
|
603
1463
|
if (findings.length) {
|
|
604
1464
|
for (const f of findings) yield* Console.error(`${f.file}: ${f.check}: ${singleLine(f.message)}`);
|
|
605
1465
|
yield* Console.error(`formal: ${findings.length} ${findings.length === 1 ? "finding" : "findings"}`);
|
|
606
|
-
} else yield* Console.log(`formal: ${repo.specs.length} specs, ${repo.specs.reduce((n, s) => n + s.configs.length, 0)} configs, no findings`);
|
|
1466
|
+
} else yield* Console.log(`formal: ${repo.specs.length} specs, ${repo.specs.reduce((n, s) => n + s.configs.length, 0)} configs, ${Object.keys(repo.file.proofs).length} proofs, no findings`);
|
|
607
1467
|
return findings.length ? 1 : 0;
|
|
608
1468
|
});
|
|
609
1469
|
if (import.meta.main) {
|
|
@@ -615,7 +1475,7 @@ if (import.meta.main) {
|
|
|
615
1475
|
const git = yield* ChildProcess.make("git", ["rev-parse", "--show-toplevel"], { stdin: "ignore" });
|
|
616
1476
|
const [root, code] = yield* Effect.all([git.stdout.pipe(Stream.decodeText, Stream.mkString), git.exitCode], { concurrency: "unbounded" });
|
|
617
1477
|
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));
|
|
1478
|
+
return yield* formal(root.trim()).pipe(Effect.provide(Gate.layer.pipe(Layer.provideMerge(Tla.layer))));
|
|
619
1479
|
})).pipe(Effect.provide(NodeServices.layer)));
|
|
620
1480
|
if (Exit.isSuccess(exit)) process.exitCode = exit.value;
|
|
621
1481
|
else {
|