@milkenjoyer/toolchain 0.12.0 → 0.14.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 +115 -6
- package/dist/arithmetic.d.mts +45 -0
- package/dist/arithmetic.mjs +25 -0
- package/dist/formal.mjs +369 -273
- package/dist/guard.d.mts +2 -2
- package/dist/guard.mjs +1 -46
- package/dist/knip.mjs +2 -1
- package/dist/manifest-xQwgnYI1.mjs +53 -0
- package/dist/specs-file-NsQF4V7X.mjs +135 -0
- package/dist/standard.d.mts +2 -0
- package/dist/standard.mjs +398 -0
- package/package.json +10 -2
- package/src/formal/arithmetic.ts +190 -0
- package/src/formal/checks.ts +19 -1
- package/src/formal/critical.ts +48 -0
- package/src/formal/proof-run.ts +77 -0
- package/src/formal/proofs.ts +2 -36
- package/src/formal/prover.ts +291 -0
- package/src/formal/specs-file.ts +119 -0
- package/src/formal/specs.ts +3 -87
- package/src/formal/tla.ts +36 -17
- package/src/formal/z3.ts +321 -0
- package/src/formal.ts +1 -1
- package/src/standard/agents.ts +44 -0
- package/src/standard/checks.ts +19 -0
- package/src/standard/config.ts +110 -0
- package/src/standard/formal.ts +93 -0
- package/src/standard/manifest.ts +102 -0
- package/src/standard/plugin-skills.ts +88 -0
- package/src/standard/repo.ts +69 -0
- package/src/standard/toolchain.ts +90 -0
- package/src/standard.ts +71 -0
package/dist/formal.mjs
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
#!/usr/bin/env bun
|
|
2
|
+
import { n as criticalFindings, t as decode } from "./specs-file-NsQF4V7X.mjs";
|
|
2
3
|
import { i as sorted, r as receiptPath, t as FORMAL_TRACES } from "./trace-file-KuPRWgwt.mjs";
|
|
3
4
|
import * as Data from "effect/Data";
|
|
4
5
|
import * as Option from "effect/Option";
|
|
@@ -15,186 +16,65 @@ import * as Layer from "effect/Layer";
|
|
|
15
16
|
import { ChildProcess, ChildProcessSpawner } from "effect/process";
|
|
16
17
|
import * as Stream from "effect/Stream";
|
|
17
18
|
import { FileSystem } from "effect/FileSystem";
|
|
18
|
-
import
|
|
19
|
-
import * as Context from "effect/Context";
|
|
19
|
+
import { fileURLToPath } from "node:url";
|
|
20
20
|
import { createHash } from "node:crypto";
|
|
21
21
|
import * as Clock from "effect/Clock";
|
|
22
|
+
import * as Context from "effect/Context";
|
|
22
23
|
import { FetchHttpClient, HttpClient } from "effect/http";
|
|
23
24
|
import { Path } from "effect/Path";
|
|
24
|
-
import * as
|
|
25
|
-
// src/formal/
|
|
26
|
-
const
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
"
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
"
|
|
44
|
-
|
|
25
|
+
import * as Cache from "effect/Cache";
|
|
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 = (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(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(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(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;
|
|
@@ -685,30 +862,7 @@ const violate = Effect.fn("formal.violate")(function* (repo, m, target, edit) {
|
|
|
685
862
|
}));
|
|
686
863
|
});
|
|
687
864
|
// src/formal/proofs.ts
|
|
688
|
-
const
|
|
689
|
-
const critical = (repo) => Effect.succeed([
|
|
690
|
-
...repo.file.critical.flatMap((glob) => repo.tracked.some((f) => matches(glob, f)) ? [] : [{
|
|
691
|
-
file: "specs.json",
|
|
692
|
-
message: `critical glob ${glob} matches no tracked file`
|
|
693
|
-
}]),
|
|
694
|
-
...Object.entries({
|
|
695
|
-
...repo.file.specs,
|
|
696
|
-
...repo.file.kani
|
|
697
|
-
}).flatMap(([file, e]) => e.covers.flatMap((glob) => repo.tracked.some((f) => matches(glob, f)) ? [] : [{
|
|
698
|
-
file,
|
|
699
|
-
message: `covers glob ${glob} matches no tracked file`
|
|
700
|
-
}])),
|
|
701
|
-
...repo.tracked.flatMap((file) => {
|
|
702
|
-
const glob = repo.file.critical.find((g) => matches(g, file));
|
|
703
|
-
return glob !== void 0 && !Object.values({
|
|
704
|
-
...repo.file.specs,
|
|
705
|
-
...repo.file.kani
|
|
706
|
-
}).some((e) => e.mode !== "library" && e.covers.some((g) => matches(g, file))) ? [{
|
|
707
|
-
file,
|
|
708
|
-
message: `matches critical glob ${glob} but no spec covers it`
|
|
709
|
-
}] : [];
|
|
710
|
-
})
|
|
711
|
-
]);
|
|
865
|
+
const critical = (repo) => Effect.succeed(criticalFindings(repo));
|
|
712
866
|
const fairness = (repo) => Effect.succeed(repo.specs.flatMap((s) => s.analysis._tag === "Parsed" ? s.analysis.semantics.fairness.filter((f) => !f.valid).map((f) => ({
|
|
713
867
|
file: s.path,
|
|
714
868
|
message: `${f.kind} at line ${f.line}; fairness must be WF_vars or SF_vars`
|
|
@@ -1009,9 +1163,14 @@ const specs = (repo) => Effect.succeed([
|
|
|
1009
1163
|
message: c.cfg.message
|
|
1010
1164
|
}] : [])
|
|
1011
1165
|
]),
|
|
1166
|
+
...repo.tracked.filter((f) => f.endsWith(".proof.ts") && !repo.file.proofs[f] && repo.decodeErrors.length === 0).map((file) => ({
|
|
1167
|
+
file,
|
|
1168
|
+
message: "tracked proof has no specs.json entry"
|
|
1169
|
+
})),
|
|
1012
1170
|
...Object.keys({
|
|
1013
1171
|
...repo.file.specs,
|
|
1014
|
-
...repo.file.kani
|
|
1172
|
+
...repo.file.kani,
|
|
1173
|
+
...repo.file.proofs
|
|
1015
1174
|
}).filter((f) => !repo.tracked.includes(f)).map((file) => ({
|
|
1016
1175
|
file,
|
|
1017
1176
|
message: "entry names no tracked file"
|
|
@@ -1094,6 +1253,7 @@ const tlc = (repo) => Effect.gen(function* () {
|
|
|
1094
1253
|
const checks = {
|
|
1095
1254
|
specs,
|
|
1096
1255
|
critical,
|
|
1256
|
+
arithmetic,
|
|
1097
1257
|
sany,
|
|
1098
1258
|
typeok,
|
|
1099
1259
|
deadlock,
|
|
@@ -1104,70 +1264,10 @@ const checks = {
|
|
|
1104
1264
|
coverage: coverageCheck,
|
|
1105
1265
|
witnesses,
|
|
1106
1266
|
breaks,
|
|
1107
|
-
traces
|
|
1267
|
+
traces,
|
|
1268
|
+
proofs
|
|
1108
1269
|
};
|
|
1109
1270
|
// src/formal/specs.ts
|
|
1110
|
-
const RepoPath = Schema.String.check(Schema.isPattern(/^(?!\/)(?!(?:.*\/)?\.\.(?:\/|$))[^\0\\]+$/u));
|
|
1111
|
-
const OperatorName = Schema.String.check(Schema.isPattern(/^[A-Za-z_][A-Za-z0-9_]*$/u));
|
|
1112
|
-
const Common = {
|
|
1113
|
-
synced: Schema.String.check(Schema.isPattern(/^[0-9a-f]{40}$/u)),
|
|
1114
|
-
covers: Schema.NonEmptyArray(Schema.NonEmptyString)
|
|
1115
|
-
};
|
|
1116
|
-
const Runtime = {
|
|
1117
|
-
...Common,
|
|
1118
|
-
timeoutSeconds: Schema.Int.check(Schema.isBetween({
|
|
1119
|
-
minimum: 1,
|
|
1120
|
-
maximum: 1800
|
|
1121
|
-
})).pipe(Schema.withDecodingDefaultKey(Effect.succeed(60)))
|
|
1122
|
-
};
|
|
1123
|
-
const Proof = {
|
|
1124
|
-
...Runtime,
|
|
1125
|
-
witnesses: Schema.NonEmptyArray(OperatorName).check(Schema.makeFilter((names) => new Set(names).size === names.length || "witnesses must be unique")),
|
|
1126
|
-
breaks: Schema.Array(Schema.Struct({
|
|
1127
|
-
violates: OperatorName,
|
|
1128
|
-
find: Schema.NonEmptyString,
|
|
1129
|
-
replace: Schema.String,
|
|
1130
|
-
file: Schema.optionalKey(RepoPath)
|
|
1131
|
-
})).pipe(Schema.withDecodingDefaultKey(Effect.succeed([])))
|
|
1132
|
-
};
|
|
1133
|
-
const Entry = Schema.Union([
|
|
1134
|
-
Schema.Struct({
|
|
1135
|
-
...Proof,
|
|
1136
|
-
mode: Schema.Literal("gate"),
|
|
1137
|
-
traces: Schema.optionalKey(Schema.Struct({
|
|
1138
|
-
seed: Schema.Int.check(Schema.isGreaterThanOrEqualTo(0)),
|
|
1139
|
-
depth: Schema.Int.check(Schema.isBetween({
|
|
1140
|
-
minimum: 1,
|
|
1141
|
-
maximum: 1e4
|
|
1142
|
-
})),
|
|
1143
|
-
count: Schema.Int.check(Schema.isBetween({
|
|
1144
|
-
minimum: 1,
|
|
1145
|
-
maximum: 1e5
|
|
1146
|
-
})),
|
|
1147
|
-
steer: Schema.optionalKey(Schema.Record(OperatorName, Schema.Int.check(Schema.isBetween({
|
|
1148
|
-
minimum: 2,
|
|
1149
|
-
maximum: 1e3
|
|
1150
|
-
})))),
|
|
1151
|
-
conformance: RepoPath
|
|
1152
|
-
}))
|
|
1153
|
-
}),
|
|
1154
|
-
Schema.Struct({
|
|
1155
|
-
...Proof,
|
|
1156
|
-
mode: Schema.Literal("manual")
|
|
1157
|
-
}),
|
|
1158
|
-
Schema.Struct({
|
|
1159
|
-
...Common,
|
|
1160
|
-
mode: Schema.Literal("library")
|
|
1161
|
-
})
|
|
1162
|
-
]);
|
|
1163
|
-
const SpecsFile = Schema.Struct({
|
|
1164
|
-
critical: Schema.NonEmptyArray(Schema.NonEmptyString),
|
|
1165
|
-
specs: Schema.Record(RepoPath.check(Schema.isPattern(/\.tla$/u)), Entry),
|
|
1166
|
-
kani: Schema.Record(RepoPath.check(Schema.isPattern(/\.rs$/u)), Schema.Struct({
|
|
1167
|
-
...Runtime,
|
|
1168
|
-
mode: Schema.Literals(["gate", "manual"])
|
|
1169
|
-
})).pipe(Schema.withDecodingDefaultKey(Effect.succeed({})))
|
|
1170
|
-
});
|
|
1171
1271
|
const load = Effect.fn("formal.load")(function* (root) {
|
|
1172
1272
|
const fs = yield* FileSystem;
|
|
1173
1273
|
const path = yield* Path;
|
|
@@ -1182,17 +1282,13 @@ const load = Effect.fn("formal.load")(function* (root) {
|
|
|
1182
1282
|
let file = {
|
|
1183
1283
|
critical: [],
|
|
1184
1284
|
specs: {},
|
|
1185
|
-
kani: {}
|
|
1285
|
+
kani: {},
|
|
1286
|
+
proofs: {}
|
|
1186
1287
|
};
|
|
1187
1288
|
let decodeErrors = [];
|
|
1188
1289
|
if (yield* fs.exists(path.join(root, "specs.json"))) {
|
|
1189
1290
|
const raw = yield* fs.readFileString(path.join(root, "specs.json"));
|
|
1190
|
-
|
|
1191
|
-
errors: "all",
|
|
1192
|
-
onExcessProperty: "error"
|
|
1193
|
-
});
|
|
1194
|
-
if (Result.isSuccess(decoded)) file = decoded.success;
|
|
1195
|
-
else decodeErrors = SchemaIssue.makeFormatterStandardSchemaV1()(decoded.failure.issue).issues.map(({ message, path: at = [] }) => at.length === 0 ? message : `${at.map((key) => `[${JSON.stringify(typeof key === "object" ? key.key : key)}]`).join("")}: ${message}`);
|
|
1291
|
+
({file, decodeErrors} = decode(raw));
|
|
1196
1292
|
}
|
|
1197
1293
|
const modules = tracked.filter((f) => f.endsWith(".tla"));
|
|
1198
1294
|
const configs = tracked.filter((f) => f.endsWith(".cfg") && modules.some((m) => path.dirname(m) === path.dirname(f)));
|
|
@@ -1254,7 +1350,7 @@ const formal = Effect.fn("formal")(function* (root) {
|
|
|
1254
1350
|
if (findings.length) {
|
|
1255
1351
|
for (const f of findings) yield* Console.error(`${f.file}: ${f.check}: ${singleLine(f.message)}`);
|
|
1256
1352
|
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`);
|
|
1353
|
+
} 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
1354
|
return findings.length ? 1 : 0;
|
|
1259
1355
|
});
|
|
1260
1356
|
if (import.meta.main) {
|