@milkenjoyer/toolchain 0.12.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 +95 -6
- package/dist/arithmetic.d.mts +45 -0
- package/dist/arithmetic.mjs +25 -0
- package/dist/formal.mjs +393 -184
- package/dist/knip.mjs +2 -1
- package/package.json +7 -2
- package/src/formal/arithmetic.ts +190 -0
- package/src/formal/checks.ts +19 -1
- package/src/formal/proof-run.ts +77 -0
- package/src/formal/proofs.ts +18 -9
- package/src/formal/prover.ts +291 -0
- package/src/formal/specs.ts +27 -1
- package/src/formal/tla.ts +36 -17
- package/src/formal/z3.ts +321 -0
- package/src/formal.ts +1 -1
package/dist/formal.mjs
CHANGED
|
@@ -15,186 +15,66 @@ import * as Layer from "effect/Layer";
|
|
|
15
15
|
import { ChildProcess, ChildProcessSpawner } from "effect/process";
|
|
16
16
|
import * as Stream from "effect/Stream";
|
|
17
17
|
import { FileSystem } from "effect/FileSystem";
|
|
18
|
-
import
|
|
19
|
-
import * as Context from "effect/Context";
|
|
18
|
+
import { fileURLToPath } from "node:url";
|
|
20
19
|
import { createHash } from "node:crypto";
|
|
21
20
|
import * as Clock from "effect/Clock";
|
|
21
|
+
import * as Context from "effect/Context";
|
|
22
22
|
import { FetchHttpClient, HttpClient } from "effect/http";
|
|
23
23
|
import { Path } from "effect/Path";
|
|
24
|
+
import * as Cache from "effect/Cache";
|
|
24
25
|
import * as SchemaIssue from "effect/SchemaIssue";
|
|
25
|
-
// src/formal/
|
|
26
|
-
const
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
"
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
"
|
|
44
|
-
|
|
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 })
|
|
45
63
|
]);
|
|
46
|
-
const
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
message
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
const keyword = tokens[i++];
|
|
60
|
-
if (keyword === void 0 || !keywords.has(keyword)) return fail(`unknown config keyword ${keyword}`);
|
|
61
|
-
const operands = [];
|
|
62
|
-
while (i < tokens.length && !keywords.has(tokens[i] ?? "")) operands.push(tokens[i++] ?? "");
|
|
63
|
-
sections.push({
|
|
64
|
-
keyword,
|
|
65
|
-
text: operands.join(" ")
|
|
66
|
-
});
|
|
67
|
-
if (operands.length === 0) return fail(`missing operand for ${keyword}`);
|
|
68
|
-
if (keyword === "CONSTANT" || keyword === "CONSTANTS") {
|
|
69
|
-
let j = 0;
|
|
70
|
-
while (j < operands.length) {
|
|
71
|
-
const name = operands[j++];
|
|
72
|
-
const assignment = operands[j++];
|
|
73
|
-
if (name === void 0 || !/^[A-Za-z_][A-Za-z0-9_!]*$/u.test(name) || assignment !== "=" && assignment !== "<-") return fail(`unparseable constant ${name}`);
|
|
74
|
-
const value = [];
|
|
75
|
-
let depth = 0;
|
|
76
|
-
while (j < operands.length) {
|
|
77
|
-
const token = operands[j] ?? "";
|
|
78
|
-
if (depth === 0 && value.length > 0 && ["=", "<-"].includes(operands[j + 1] ?? "")) break;
|
|
79
|
-
if (token === "{") depth++;
|
|
80
|
-
if (token === "}") depth--;
|
|
81
|
-
value.push(token);
|
|
82
|
-
j++;
|
|
83
|
-
}
|
|
84
|
-
const raw = value.join(" ");
|
|
85
|
-
if (depth !== 0 || !/^(?:-?\d+|[A-Za-z_][A-Za-z0-9_!]*|"(?:\\.|[^"\\])*"|\{[\s\S]*\})$/u.test(raw)) return fail(`unparseable constant ${name} ${assignment} ${raw}`);
|
|
86
|
-
constants.push(assignment === "=" ? {
|
|
87
|
-
_tag: "Value",
|
|
88
|
-
name,
|
|
89
|
-
value: raw
|
|
90
|
-
} : {
|
|
91
|
-
_tag: "Substitution",
|
|
92
|
-
name,
|
|
93
|
-
by: raw
|
|
94
|
-
});
|
|
95
|
-
}
|
|
96
|
-
} else if (keyword === "CHECK_DEADLOCK") {
|
|
97
|
-
if (operands.length !== 1 || !["TRUE", "FALSE"].includes(operands[0] ?? "")) return fail("CHECK_DEADLOCK needs TRUE or FALSE");
|
|
98
|
-
checkDeadlock.push(operands[0] === "TRUE");
|
|
99
|
-
} else {
|
|
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);
|
|
102
|
-
if (keyword.startsWith("INVARIANT")) invariants.push(...operands);
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
return {
|
|
106
|
-
sections,
|
|
107
|
-
constants,
|
|
108
|
-
invariants,
|
|
109
|
-
properties,
|
|
110
|
-
checkDeadlock
|
|
111
|
-
};
|
|
112
|
-
};
|
|
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}`;
|
|
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;
|
|
193
77
|
}
|
|
194
|
-
return {
|
|
195
|
-
actions,
|
|
196
|
-
zeros
|
|
197
|
-
};
|
|
198
78
|
};
|
|
199
79
|
// src/formal/semantics.ts
|
|
200
80
|
const child = (node, tag) => node.children.find((n) => n.tag === tag);
|
|
@@ -361,11 +241,11 @@ const fromXml = (xml) => {
|
|
|
361
241
|
var FormalError = class extends Data.TaggedError("FormalError") {};
|
|
362
242
|
const pins = [{
|
|
363
243
|
file: "tla2tools-1.8.0.jar",
|
|
364
|
-
|
|
244
|
+
urls: ["https://github.com/tlaplus/tlaplus/releases/download/v1.8.0/tla2tools.jar", "https://api.github.com/repos/tlaplus/tlaplus/releases/assets/718777374"],
|
|
365
245
|
sha256: "e1f11b75c4b5bc4478ba0ac5cb6ae28e40a01b0016bc33e0aa97da22dfdbcd7f"
|
|
366
246
|
}, {
|
|
367
247
|
file: "CommunityModules-deps-202611060952.jar",
|
|
368
|
-
|
|
248
|
+
urls: ["https://github.com/tlaplus/CommunityModules/releases/download/202611060952/CommunityModules-deps-202611060952.jar", "https://api.github.com/repos/tlaplus/CommunityModules/releases/assets/716678698"],
|
|
369
249
|
sha256: "a3b27c40f1fb5085f30c6ead37e918457f8cfc62c1e50a1dc668ef592ad2b5fb"
|
|
370
250
|
}];
|
|
371
251
|
const error = (cause) => new FormalError({ message: cause instanceof Error ? cause.message : String(cause) });
|
|
@@ -396,10 +276,21 @@ const make$1 = Effect.gen(function* () {
|
|
|
396
276
|
prefix: `.${pin.file}.`,
|
|
397
277
|
suffix: ".part"
|
|
398
278
|
});
|
|
399
|
-
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
|
+
});
|
|
400
291
|
yield* fs.writeFile(temp, bytes);
|
|
401
292
|
const actual = digest(bytes);
|
|
402
|
-
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}` });
|
|
403
294
|
return yield* fs.rename(temp, file);
|
|
404
295
|
}));
|
|
405
296
|
jars.push(file);
|
|
@@ -536,6 +427,292 @@ const make$1 = Effect.gen(function* () {
|
|
|
536
427
|
var Tla = class Tla extends Context.Service()("formal/Tla") {
|
|
537
428
|
static layer = Layer.effect(Tla, make$1);
|
|
538
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
|
+
};
|
|
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
|
+
};
|
|
539
716
|
// src/formal/gate.ts
|
|
540
717
|
const make = Effect.gen(function* () {
|
|
541
718
|
const tla = yield* Tla;
|
|
@@ -693,7 +870,8 @@ const critical = (repo) => Effect.succeed([
|
|
|
693
870
|
}]),
|
|
694
871
|
...Object.entries({
|
|
695
872
|
...repo.file.specs,
|
|
696
|
-
...repo.file.kani
|
|
873
|
+
...repo.file.kani,
|
|
874
|
+
...repo.file.proofs
|
|
697
875
|
}).flatMap(([file, e]) => e.covers.flatMap((glob) => repo.tracked.some((f) => matches(glob, f)) ? [] : [{
|
|
698
876
|
file,
|
|
699
877
|
message: `covers glob ${glob} matches no tracked file`
|
|
@@ -702,8 +880,9 @@ const critical = (repo) => Effect.succeed([
|
|
|
702
880
|
const glob = repo.file.critical.find((g) => matches(g, file));
|
|
703
881
|
return glob !== void 0 && !Object.values({
|
|
704
882
|
...repo.file.specs,
|
|
705
|
-
...repo.file.kani
|
|
706
|
-
|
|
883
|
+
...repo.file.kani,
|
|
884
|
+
...repo.file.proofs
|
|
885
|
+
}).some((e) => (!("mode" in e) || e.mode !== "library") && e.covers.some((g) => matches(g, file))) ? [{
|
|
707
886
|
file,
|
|
708
887
|
message: `matches critical glob ${glob} but no spec covers it`
|
|
709
888
|
}] : [];
|
|
@@ -1009,9 +1188,14 @@ const specs = (repo) => Effect.succeed([
|
|
|
1009
1188
|
message: c.cfg.message
|
|
1010
1189
|
}] : [])
|
|
1011
1190
|
]),
|
|
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
|
+
})),
|
|
1012
1195
|
...Object.keys({
|
|
1013
1196
|
...repo.file.specs,
|
|
1014
|
-
...repo.file.kani
|
|
1197
|
+
...repo.file.kani,
|
|
1198
|
+
...repo.file.proofs
|
|
1015
1199
|
}).filter((f) => !repo.tracked.includes(f)).map((file) => ({
|
|
1016
1200
|
file,
|
|
1017
1201
|
message: "entry names no tracked file"
|
|
@@ -1094,6 +1278,7 @@ const tlc = (repo) => Effect.gen(function* () {
|
|
|
1094
1278
|
const checks = {
|
|
1095
1279
|
specs,
|
|
1096
1280
|
critical,
|
|
1281
|
+
arithmetic,
|
|
1097
1282
|
sany,
|
|
1098
1283
|
typeok,
|
|
1099
1284
|
deadlock,
|
|
@@ -1104,7 +1289,8 @@ const checks = {
|
|
|
1104
1289
|
coverage: coverageCheck,
|
|
1105
1290
|
witnesses,
|
|
1106
1291
|
breaks,
|
|
1107
|
-
traces
|
|
1292
|
+
traces,
|
|
1293
|
+
proofs
|
|
1108
1294
|
};
|
|
1109
1295
|
// src/formal/specs.ts
|
|
1110
1296
|
const RepoPath = Schema.String.check(Schema.isPattern(/^(?!\/)(?!(?:.*\/)?\.\.(?:\/|$))[^\0\\]+$/u));
|
|
@@ -1160,8 +1346,30 @@ const Entry = Schema.Union([
|
|
|
1160
1346
|
mode: Schema.Literal("library")
|
|
1161
1347
|
})
|
|
1162
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
|
+
})]);
|
|
1163
1369
|
const SpecsFile = Schema.Struct({
|
|
1164
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({}))),
|
|
1165
1373
|
specs: Schema.Record(RepoPath.check(Schema.isPattern(/\.tla$/u)), Entry),
|
|
1166
1374
|
kani: Schema.Record(RepoPath.check(Schema.isPattern(/\.rs$/u)), Schema.Struct({
|
|
1167
1375
|
...Runtime,
|
|
@@ -1182,7 +1390,8 @@ const load = Effect.fn("formal.load")(function* (root) {
|
|
|
1182
1390
|
let file = {
|
|
1183
1391
|
critical: [],
|
|
1184
1392
|
specs: {},
|
|
1185
|
-
kani: {}
|
|
1393
|
+
kani: {},
|
|
1394
|
+
proofs: {}
|
|
1186
1395
|
};
|
|
1187
1396
|
let decodeErrors = [];
|
|
1188
1397
|
if (yield* fs.exists(path.join(root, "specs.json"))) {
|
|
@@ -1254,7 +1463,7 @@ const formal = Effect.fn("formal")(function* (root) {
|
|
|
1254
1463
|
if (findings.length) {
|
|
1255
1464
|
for (const f of findings) yield* Console.error(`${f.file}: ${f.check}: ${singleLine(f.message)}`);
|
|
1256
1465
|
yield* Console.error(`formal: ${findings.length} ${findings.length === 1 ? "finding" : "findings"}`);
|
|
1257
|
-
} 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`);
|
|
1258
1467
|
return findings.length ? 1 : 0;
|
|
1259
1468
|
});
|
|
1260
1469
|
if (import.meta.main) {
|
package/dist/knip.mjs
CHANGED
|
@@ -2,7 +2,8 @@
|
|
|
2
2
|
const baseEntry = [
|
|
3
3
|
"*.run.ts",
|
|
4
4
|
"scripts/*.ts",
|
|
5
|
-
"tests/fixtures/**/*.ts"
|
|
5
|
+
"tests/fixtures/**/*.ts",
|
|
6
|
+
"**/*.proof.ts"
|
|
6
7
|
];
|
|
7
8
|
const toolchainPlugins = ["@effect/language-service", "@foldkit/oxlint-plugin"];
|
|
8
9
|
const knip = ({ entry = [], ignoreDependencies = [], workspaces = {} } = {}) => ({
|