@milkenjoyer/toolchain 0.10.0 → 0.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. package/README.md +117 -0
  2. package/dist/conformance.d.mts +18 -0
  3. package/dist/conformance.mjs +31 -0
  4. package/dist/effect-plugin.mjs +1 -1
  5. package/dist/exceptions-Dybl9xpc.mjs +11 -0
  6. package/dist/exceptions.mjs +1 -1
  7. package/dist/foldkit-plugin.mjs +1 -1
  8. package/dist/formal.d.mts +188 -0
  9. package/dist/formal.mjs +1279 -0
  10. package/dist/guard.mjs +2 -2
  11. package/dist/hooks.mjs +2 -2
  12. package/dist/{lint-JSnPsGO1.mjs → lint-IhmtnBbx.mjs} +3 -3
  13. package/dist/lint.mjs +1 -1
  14. package/dist/plugin.mjs +1 -1
  15. package/dist/proof.mjs +6 -6
  16. package/dist/trace-file-KuPRWgwt.mjs +17 -0
  17. package/dist/verdict.d.mts +1 -1
  18. package/dist/verdict.mjs +2 -2
  19. package/dist/{verification-MNG9WKEH.d.mts → verification-FgQFU1YN.d.mts} +2 -2
  20. package/dist/verification.d.mts +1 -1
  21. package/dist/verification.mjs +1 -1
  22. package/package.json +9 -2
  23. package/src/formal/cfg.ts +168 -0
  24. package/src/formal/checks.ts +190 -0
  25. package/src/formal/coverage.ts +79 -0
  26. package/src/formal/gate.ts +74 -0
  27. package/src/formal/git.ts +38 -0
  28. package/src/formal/proofs.ts +217 -0
  29. package/src/formal/semantics.ts +255 -0
  30. package/src/formal/specs.ts +212 -0
  31. package/src/formal/tla.ts +306 -0
  32. package/src/formal/trace-file.ts +35 -0
  33. package/src/formal/traces.ts +293 -0
  34. package/src/formal/violations.ts +76 -0
  35. package/src/formal.ts +82 -0
  36. package/dist/exceptions-Bq-9MxoR.mjs +0 -3
  37. package/dist/{effect-plugin-kYxB-5LJ.mjs → effect-plugin-BXUx9PPu.mjs} +1 -1
  38. package/dist/{plugin-BoBRGtGf.mjs → plugin-CwxhPtui.mjs} +1 -1
  39. package/dist/{verification-2bhNnJr-.mjs → verification-oAc1Q6I9.mjs} +4 -4
@@ -0,0 +1,1279 @@
1
+ #!/usr/bin/env bun
2
+ import { i as sorted, r as receiptPath, t as FORMAL_TRACES } from "./trace-file-KuPRWgwt.mjs";
3
+ import * as Data from "effect/Data";
4
+ import * as Option from "effect/Option";
5
+ import * as Schema from "effect/Schema";
6
+ import * as Config from "effect/Config";
7
+ import * as Effect from "effect/Effect";
8
+ import * as Result from "effect/Result";
9
+ import { basename, dirname, join, relative } from "node:path";
10
+ import { NodeServices } from "@effect/platform-node";
11
+ import * as Cause from "effect/Cause";
12
+ import * as Console from "effect/Console";
13
+ import * as Exit from "effect/Exit";
14
+ import * as Layer from "effect/Layer";
15
+ import { ChildProcess, ChildProcessSpawner } from "effect/process";
16
+ import * as Stream from "effect/Stream";
17
+ import { FileSystem } from "effect/FileSystem";
18
+ import * as Cache from "effect/Cache";
19
+ import * as Context from "effect/Context";
20
+ import { createHash } from "node:crypto";
21
+ import * as Clock from "effect/Clock";
22
+ import { FetchHttpClient, HttpClient } from "effect/http";
23
+ import { Path } from "effect/Path";
24
+ import * as SchemaIssue from "effect/SchemaIssue";
25
+ // src/formal/cfg.ts
26
+ const keywords = /* @__PURE__ */ new Set([
27
+ "CONSTANT",
28
+ "CONSTANTS",
29
+ "INIT",
30
+ "NEXT",
31
+ "SPECIFICATION",
32
+ "INVARIANT",
33
+ "INVARIANTS",
34
+ "PROPERTY",
35
+ "PROPERTIES",
36
+ "CONSTRAINT",
37
+ "CONSTRAINTS",
38
+ "ACTION_CONSTRAINT",
39
+ "ACTION_CONSTRAINTS",
40
+ "SYMMETRY",
41
+ "VIEW",
42
+ "ALIAS",
43
+ "POSTCONDITION",
44
+ "CHECK_DEADLOCK"
45
+ ]);
46
+ const parseCfg = (source) => {
47
+ const tokens = source.replaceAll(/\(\*[\s\S]*?\*\)|\\\*[^\n]*/gu, " ").match(/"(?:\\.|[^"\\])*"|<-|[A-Za-z_][A-Za-z0-9_!]*|-?\d+|[^\s]/gu) ?? [];
48
+ const sections = [];
49
+ const properties = [];
50
+ const constants = [];
51
+ const invariants = [];
52
+ const checkDeadlock = [];
53
+ const fail = (message) => ({
54
+ _tag: "CfgError",
55
+ message
56
+ });
57
+ let i = 0;
58
+ while (i < tokens.length) {
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}`;
193
+ }
194
+ return {
195
+ actions,
196
+ zeros
197
+ };
198
+ };
199
+ // src/formal/semantics.ts
200
+ const child = (node, tag) => node.children.find((n) => n.tag === tag);
201
+ const value = (node, tag) => child(node, tag)?.text.trim() ?? "";
202
+ const descendants = (node) => [node, ...node.children.flatMap(descendants)];
203
+ const fromXml = (xml) => {
204
+ const root = {
205
+ tag: "document",
206
+ text: "",
207
+ children: []
208
+ };
209
+ const stack = [root];
210
+ for (const match of xml.matchAll(/<!\[CDATA\[[\s\S]*?\]\]>|<\?[^>]*\?>|<([^>]+)>|([^<]+)/gu)) {
211
+ const token = match[0];
212
+ const parent = stack.at(-1);
213
+ if (parent === void 0) throw new Error("invalid SANY XML stack");
214
+ if (token.startsWith("<!") || token.startsWith("<?")) continue;
215
+ if (token.startsWith("</")) {
216
+ if (stack.pop()?.tag !== token.slice(2, -1)) throw new Error("invalid SANY XML closing tag");
217
+ } else if (token.startsWith("<")) {
218
+ const node = {
219
+ tag: token.slice(1, token.endsWith("/>") ? -2 : -1),
220
+ text: "",
221
+ children: []
222
+ };
223
+ parent.children.push(node);
224
+ if (!token.endsWith("/>")) stack.push(node);
225
+ } else parent.text += token.replaceAll("&lt;", "<").replaceAll("&gt;", ">").replaceAll("&amp;", "&");
226
+ }
227
+ const modules = child(root, "modules");
228
+ const context = modules && child(modules, "context");
229
+ if (stack.length !== 1 || !modules || !context) throw new Error("missing SANY XML context");
230
+ const entries = new Map(context.children.map((entry) => [value(entry, "UID"), entry.children.find((n) => n.tag !== "UID")]));
231
+ const module = [...entries.values()].find((n) => n?.tag === "ModuleNode" && value(n, "uniquename") === value(modules, "RootModule"));
232
+ if (!module) throw new Error("missing root module in SANY XML");
233
+ const closure = /* @__PURE__ */ new Set();
234
+ const visitModule = (node) => {
235
+ if (closure.has(value(node, "uniquename"))) return;
236
+ closure.add(value(node, "uniquename"));
237
+ for (const ref of descendants(child(node, "extends") ?? {
238
+ tag: "",
239
+ text: "",
240
+ children: []
241
+ })) {
242
+ if (ref.tag !== "uniquename") continue;
243
+ const next = [...entries.values()].find((entry) => entry?.tag === "ModuleNode" && value(entry, "uniquename") === ref.text.trim());
244
+ if (!next) throw new Error("unresolved SANY EXTENDS module");
245
+ visitModule(next);
246
+ }
247
+ };
248
+ visitModule(module);
249
+ const variables = new Map([...entries].flatMap(([uid, node]) => node?.tag === "OpDeclNode" && value(node, "kind") === "3" && closure.has(value(child(node, "location") ?? node, "filename")) ? [[uid, value(node, "uniquename")]] : []));
250
+ const target = (appl) => {
251
+ const ref = child(appl, "operator")?.children[0];
252
+ return ref && {
253
+ ref,
254
+ uid: value(ref, "UID")
255
+ };
256
+ };
257
+ const operands = (appl) => child(appl, "operands")?.children ?? [];
258
+ const reaches = (body, seen = /* @__PURE__ */ new Set()) => {
259
+ const reached = /* @__PURE__ */ new Set();
260
+ for (const n of descendants(body)) {
261
+ if (!n.tag.endsWith("Ref")) continue;
262
+ const uid = value(n, "UID");
263
+ const variable = variables.get(uid);
264
+ if (variable !== void 0) reached.add(variable);
265
+ if (seen.has(uid)) continue;
266
+ seen.add(uid);
267
+ const next = child(entries.get(uid) ?? n, "body");
268
+ if (next) for (const v of reaches(next, seen)) reached.add(v);
269
+ }
270
+ return reached;
271
+ };
272
+ const typed = (appl, seen) => {
273
+ if (appl.tag === "LetInNode") {
274
+ const body = child(appl, "body")?.children[0];
275
+ return body ? typed(body, seen) : [];
276
+ }
277
+ const op = target(appl);
278
+ if (appl.tag !== "OpApplNode" || op === void 0) return [];
279
+ const args = operands(appl);
280
+ if (op.ref.tag === "UserDefinedOpKindRef" && args.length === 0) {
281
+ if (seen.has(op.uid)) return [];
282
+ seen.add(op.uid);
283
+ const body = child(entries.get(op.uid) ?? appl, "body");
284
+ return body?.children[0] ? typed(body.children[0], seen) : [];
285
+ }
286
+ if (op.ref.tag !== "BuiltInKindRef") return [];
287
+ const name = value(entries.get(op.uid) ?? appl, "uniquename");
288
+ if (name === "$ConjList" || name === "\\land") return args.flatMap((arg) => typed(arg, seen));
289
+ const [left, set] = args;
290
+ const variable = left && target(left)?.ref.tag === "OpDeclNodeRef" ? variables.get(target(left)?.uid ?? "") : void 0;
291
+ return (name === "\\in" || name === "\\subseteq") && args.length === 2 && variable !== void 0 && set !== void 0 && !reaches(set).has(variable) ? [variable] : [];
292
+ };
293
+ const typeOK = module.children.filter((n) => n.tag === "UserDefinedOpKindRef").map((ref) => entries.get(value(ref, "UID"))).find((node) => node !== void 0 && value(node, "uniquename") === "TypeOK");
294
+ const body = typeOK && child(typeOK, "body")?.children[0];
295
+ const definitions = [...entries.values()].filter((n) => n?.tag === "UserDefinedOpKind" && closure.has(value(child(n, "location") ?? n, "filename")));
296
+ const operators = /* @__PURE__ */ new Map();
297
+ const fairness = [];
298
+ const specifications = /* @__PURE__ */ new Map();
299
+ const opName = (n) => {
300
+ const t = target(n);
301
+ return t ? value(entries.get(t.uid) ?? n, "uniquename") : "";
302
+ };
303
+ for (const n of definitions) {
304
+ const name = value(n, "uniquename");
305
+ const params = (child(n, "params")?.children ?? []).map((ref) => entries.get(value(child(ref, "FormalParamNodeRef") ?? ref, "UID")) ?? ref).map((p) => ({
306
+ name: value(p, "uniquename"),
307
+ arity: Number(value(p, "arity"))
308
+ }));
309
+ operators.set(name, {
310
+ name,
311
+ level: Number(value(n, "level")),
312
+ params,
313
+ module: value(child(n, "location") ?? n, "filename")
314
+ });
315
+ if (value(child(n, "location") ?? n, "filename") === value(modules, "RootModule")) for (const a of descendants(child(n, "body") ?? n)) {
316
+ const kind = opName(a);
317
+ if (!["$WF", "$SF"].includes(kind)) continue;
318
+ const sub = operands(a)[0];
319
+ fairness.push({
320
+ kind: `${kind.slice(1)}_${sub && opName(sub) === "$Tuple" ? `<< ${operands(sub).map(opName).join(", ")} >>` : sub ? opName(sub) : "?"}`,
321
+ line: value(child(child(a, "location") ?? a, "line") ?? a, "begin"),
322
+ valid: sub !== void 0 && opName(sub) === "vars" && operands(sub).length === 0
323
+ });
324
+ }
325
+ const flatten = (a, seen = /* @__PURE__ */ new Set()) => {
326
+ const t = target(a);
327
+ if (["$ConjList", "\\land"].includes(opName(a))) return operands(a).flatMap((x) => flatten(x, new Set(seen)));
328
+ if (t && t.ref.tag === "UserDefinedOpKindRef" && Number(value(a, "level")) === 3 && operands(a).length === 0 && !seen.has(t.uid)) {
329
+ const b = child(entries.get(t.uid) ?? a, "body")?.children[0];
330
+ if (b) {
331
+ seen.add(t.uid);
332
+ return flatten(b, seen);
333
+ }
334
+ }
335
+ return [a];
336
+ };
337
+ const body = child(n, "body")?.children[0];
338
+ if (!body) continue;
339
+ const conjuncts = flatten(body);
340
+ const state = conjuncts.filter((a) => Number(value(a, "level")) === 1);
341
+ const boxes = conjuncts.filter((a) => opName(a) === "[]");
342
+ const box = boxes[0] && operands(boxes[0])[0];
343
+ const next = box && opName(box) === "$SquareAct" ? operands(box)[0] : void 0;
344
+ if (state.length === 1 && boxes.length === 1 && next && operands(next).length === 0) {
345
+ const init = state[0] && operands(state[0]).length === 0 ? opName(state[0]) : void 0;
346
+ if (init !== void 0 && init !== "") specifications.set(name, {
347
+ init,
348
+ next: opName(next)
349
+ });
350
+ }
351
+ }
352
+ return {
353
+ operators,
354
+ fairness,
355
+ specifications,
356
+ variables: [...new Set(variables.values())],
357
+ typeOK: typeOK && new Set(body ? typed(body, /* @__PURE__ */ new Set()) : [])
358
+ };
359
+ };
360
+ // src/formal/tla.ts
361
+ var FormalError = class extends Data.TaggedError("FormalError") {};
362
+ const pins = [{
363
+ file: "tla2tools-1.8.0.jar",
364
+ url: "https://github.com/tlaplus/tlaplus/releases/download/v1.8.0/tla2tools.jar",
365
+ sha256: "e1f11b75c4b5bc4478ba0ac5cb6ae28e40a01b0016bc33e0aa97da22dfdbcd7f"
366
+ }, {
367
+ file: "CommunityModules-deps-202611060952.jar",
368
+ url: "https://github.com/tlaplus/CommunityModules/releases/download/202611060952/CommunityModules-deps-202611060952.jar",
369
+ sha256: "a3b27c40f1fb5085f30c6ead37e918457f8cfc62c1e50a1dc668ef592ad2b5fb"
370
+ }];
371
+ const error = (cause) => new FormalError({ message: cause instanceof Error ? cause.message : String(cause) });
372
+ const digest = (bytes) => createHash("sha256").update(bytes).digest("hex");
373
+ const messages = (output) => [...output.matchAll(/@!@!@STARTMSG (\d+):([0-4]) @!@!@([\s\S]*?)@!@!@ENDMSG \1 @!@!@/gu)].map((match) => ({
374
+ code: Number(match[1]),
375
+ severity: Number(match[2]),
376
+ text: (match[3] ?? "").trim()
377
+ }));
378
+ const sanyReport = (output) => output.split("\n").filter((line) => !/^(?:\*{6} SANY2 Version|Parsing file |Semantic processing of module |Linting of module )/u.test(line)).join("\n").trim();
379
+ const make$1 = Effect.gen(function* () {
380
+ const fs = yield* FileSystem;
381
+ const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
382
+ const path = yield* Path;
383
+ const cacheHome = yield* Config.String("XDG_CACHE_HOME").pipe(Config.withDefault(""));
384
+ const home = cacheHome !== "" ? cacheHome : path.join(yield* Config.String("HOME"), ".cache");
385
+ const cache = path.join(home, "tla");
386
+ yield* fs.makeDirectory(cache, { recursive: true });
387
+ const jars = [];
388
+ for (const pin of pins) {
389
+ const file = path.join(cache, pin.file);
390
+ if (yield* fs.exists(file)) {
391
+ const actual = digest(yield* fs.readFile(file));
392
+ if (actual !== pin.sha256) return yield* new FormalError({ message: `${file} has sha256 ${actual}, expected ${pin.sha256}; delete it to refetch` });
393
+ } else yield* Effect.scoped(Effect.gen(function* () {
394
+ const temp = yield* fs.makeTempFileScoped({
395
+ directory: cache,
396
+ prefix: `.${pin.file}.`,
397
+ suffix: ".part"
398
+ });
399
+ const bytes = yield* HttpClient.get(pin.url).pipe(Effect.flatMap((response) => response.status === 200 ? response.arrayBuffer.pipe(Effect.mapError(error)) : Effect.fail(new FormalError({ message: pin.url + ": HTTP " + response.status }))), Effect.map((buffer) => new Uint8Array(buffer)), Effect.timeout("120 seconds"), Effect.provide(FetchHttpClient.layer));
400
+ yield* fs.writeFile(temp, bytes);
401
+ const actual = digest(bytes);
402
+ if (actual !== pin.sha256) return yield* new FormalError({ message: `${pin.url} has sha256 ${actual}, expected ${pin.sha256}` });
403
+ return yield* fs.rename(temp, file);
404
+ }));
405
+ jars.push(file);
406
+ }
407
+ const classpath = jars.join(":");
408
+ const run = Effect.fn("formal.java")(function* (args, cwd, seconds = 120) {
409
+ return yield* Effect.scoped(Effect.gen(function* () {
410
+ const process = yield* ChildProcess.make("java", args, {
411
+ cwd,
412
+ stdin: "ignore",
413
+ killSignal: "SIGKILL"
414
+ });
415
+ return yield* Effect.all([
416
+ process.stdout.pipe(Stream.decodeText, Stream.mkString),
417
+ process.stderr.pipe(Stream.decodeText, Stream.mkString),
418
+ process.exitCode
419
+ ], { concurrency: "unbounded" });
420
+ }).pipe(Effect.timeoutOption(`${seconds} seconds`), Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner), Effect.mapError(error)));
421
+ });
422
+ const version = yield* run(["-XshowSettings:properties", "-version"], cache);
423
+ if (Option.isNone(version) || Number(version.value[2]) !== 0 || !/^\s*java\.specification\.version = 17\s*$/mu.test(version.value[1])) return yield* new FormalError({ message: "Java 17 is required on PATH" });
424
+ const invoke = (args, cwd, seconds) => run([
425
+ "-XX:+UseParallelGC",
426
+ "-cp",
427
+ classpath,
428
+ ...args
429
+ ], cwd, seconds);
430
+ return {
431
+ fixCommand: (file) => `java -cp ${JSON.stringify(classpath)} formatter.Main ${JSON.stringify(file)} ${JSON.stringify(file)}`,
432
+ analyze: Effect.fn("formal.analyze")(function* (file) {
433
+ const sany = yield* invoke([
434
+ "tla2sany.SANY",
435
+ "-error-codes",
436
+ "-messagesAsErrors",
437
+ "4800,4801,4802,4803,4804,4805",
438
+ path.basename(file)
439
+ ], path.dirname(file));
440
+ if (Option.isNone(sany)) return {
441
+ _tag: "Unparsed",
442
+ output: "SANY timed out"
443
+ };
444
+ const [stdout, stderr, code] = sany.value;
445
+ if (Number(code) !== 0 || /\*\*\* Warnings:/u.test(stdout + stderr)) return {
446
+ _tag: "Unparsed",
447
+ output: sanyReport(stdout + stderr)
448
+ };
449
+ const xml = yield* invoke([
450
+ "tla2sany.xml.XMLExporter",
451
+ "-o",
452
+ "-t",
453
+ path.basename(file)
454
+ ], path.dirname(file));
455
+ if (Option.isNone(xml)) return {
456
+ _tag: "Unparsed",
457
+ output: "XMLExporter timed out"
458
+ };
459
+ if (Number(xml.value[2]) !== 0) return {
460
+ _tag: "Unparsed",
461
+ output: xml.value[0] + xml.value[1]
462
+ };
463
+ return yield* Effect.try({
464
+ try: () => ({
465
+ _tag: "Parsed",
466
+ semantics: fromXml(xml.value[0])
467
+ }),
468
+ catch: error
469
+ });
470
+ }),
471
+ format: Effect.fn("formal.format")(function* (file) {
472
+ return yield* Effect.scoped(Effect.gen(function* () {
473
+ const temp = yield* fs.makeTempDirectoryScoped({ prefix: "formal-format-" });
474
+ const output = path.join(temp, path.basename(file));
475
+ const result = yield* invoke([
476
+ "formatter.Main",
477
+ "-v",
478
+ "ERROR",
479
+ file,
480
+ output
481
+ ], path.dirname(file));
482
+ if (Option.isNone(result)) return {
483
+ _tag: "Failed",
484
+ output: "formatter timed out"
485
+ };
486
+ if (Number(result.value[2]) !== 0 || !(yield* fs.exists(output))) return {
487
+ _tag: "Failed",
488
+ output: `formatter exited ${result.value[2]}: ${result.value[0]} ${result.value[1]}`
489
+ };
490
+ return {
491
+ _tag: "Formatted",
492
+ text: yield* fs.readFileString(output)
493
+ };
494
+ })).pipe(Effect.mapError(error));
495
+ }),
496
+ tlc: Effect.fn("formal.tlc")(function* (input) {
497
+ return yield* Effect.scoped(Effect.gen(function* () {
498
+ const temp = yield* fs.makeTempDirectoryScoped({ prefix: "formal-tlc-" });
499
+ const started = yield* Clock.currentTimeMillis;
500
+ const result = yield* invoke([
501
+ "tlc2.TLC",
502
+ "-tool",
503
+ "-workers",
504
+ input.mode._tag === "Simulate" ? "1" : "auto",
505
+ ...input.mode._tag === "Simulate" ? [
506
+ "-simulate",
507
+ `num=${input.mode.num}`,
508
+ "-depth",
509
+ String(input.mode.depth),
510
+ "-seed",
511
+ String(input.mode.seed)
512
+ ] : [
513
+ "-lncheck",
514
+ "final",
515
+ ...input.mode.coverage ? ["-coverage", "60"] : []
516
+ ],
517
+ "-metadir",
518
+ temp,
519
+ "-noGenerateSpecTE",
520
+ "-config",
521
+ input.config,
522
+ path.basename(input.spec)
523
+ ], path.dirname(input.spec), input.timeoutSeconds);
524
+ const seconds = ((yield* Clock.currentTimeMillis) - started) / 1e3;
525
+ if (Option.isNone(result)) return { _tag: "TimedOut" };
526
+ return {
527
+ _tag: "Exited",
528
+ seconds,
529
+ code: Number(result.value[2]),
530
+ messages: messages(result.value[0] + result.value[1])
531
+ };
532
+ })).pipe(Effect.mapError(error));
533
+ })
534
+ };
535
+ }).pipe(Effect.mapError(error));
536
+ var Tla = class Tla extends Context.Service()("formal/Tla") {
537
+ static layer = Layer.effect(Tla, make$1);
538
+ };
539
+ // src/formal/gate.ts
540
+ const make = Effect.gen(function* () {
541
+ const tla = yield* Tla;
542
+ const run = yield* Cache.make({
543
+ capacity: 1e4,
544
+ lookup: Effect.fn("formal.gate")(function* (m) {
545
+ const r = yield* tla.tlc({
546
+ spec: `${m.root}/${m.path}`,
547
+ config: `${m.root}/${m.config.path}`,
548
+ timeoutSeconds: m.entry.timeoutSeconds,
549
+ mode: {
550
+ _tag: "Check",
551
+ coverage: true
552
+ }
553
+ });
554
+ if (r._tag === "TimedOut") return {
555
+ _tag: "Failed",
556
+ message: `TLC timed out after ${m.entry.timeoutSeconds} s`
557
+ };
558
+ const failures = r.messages.filter((x) => x.severity >= 1 && x.severity <= 3);
559
+ const states = r.messages.find((x) => x.code === 2199)?.text.match(/([\d,]+) distinct states/u)?.[1]?.replaceAll(",", "");
560
+ if (r.code !== 0 || failures.length || !r.messages.some((x) => x.code === 2193) || states === void 0 || BigInt(states) === 0n) {
561
+ const meaning = /* @__PURE__ */ new Map([
562
+ [11, "deadlock"],
563
+ [12, "safety violation"],
564
+ [13, "liveness violation"],
565
+ [150, "parse failure"]
566
+ ]);
567
+ return {
568
+ _tag: "Failed",
569
+ message: `TLC exited ${r.code}${meaning.has(r.code) ? ` (${meaning.get(r.code)})` : ""}; ${failures.length ? failures.map((x) => `${x.severity === 3 ? "warning" : "error"} ${x.code}: ${x.text}`).join("; ") : "requires 2193 success and nonzero distinct states (2199)"}`
570
+ };
571
+ }
572
+ const c = coverage(r.messages);
573
+ return typeof c === "string" ? {
574
+ _tag: "Failed",
575
+ message: c
576
+ } : {
577
+ _tag: "Passed",
578
+ states,
579
+ seconds: r.seconds,
580
+ coverage: c
581
+ };
582
+ })
583
+ });
584
+ return { run: (model) => Cache.get(run, model) };
585
+ });
586
+ var Gate = class Gate extends Context.Service()("formal/Gate") {
587
+ static layer = Layer.effect(Gate, make);
588
+ };
589
+ // src/formal/git.ts
590
+ const changes = Effect.fn("formal.changes")(function* (root) {
591
+ const base = yield* Config.String("BASE").pipe(Config.withDefault("origin/main"));
592
+ const run = (args) => Effect.scoped(Effect.gen(function* () {
593
+ const p = yield* ChildProcess.make("git", args, {
594
+ cwd: root,
595
+ stdin: "ignore"
596
+ });
597
+ const [out, , code] = yield* Effect.all([
598
+ p.stdout.pipe(Stream.decodeText, Stream.mkString),
599
+ p.stderr.pipe(Stream.decodeText, Stream.mkString),
600
+ p.exitCode
601
+ ], { concurrency: "unbounded" });
602
+ return {
603
+ out,
604
+ code: Number(code)
605
+ };
606
+ }));
607
+ const merge = yield* run([
608
+ "merge-base",
609
+ base,
610
+ "HEAD"
611
+ ]);
612
+ if (merge.code !== 0) return {
613
+ _tag: "All",
614
+ base
615
+ };
616
+ const sha = merge.out.trim();
617
+ const diff = yield* run([
618
+ "diff",
619
+ "--name-only",
620
+ "-z",
621
+ sha
622
+ ]);
623
+ return diff.code !== 0 ? {
624
+ _tag: "All",
625
+ base
626
+ } : {
627
+ _tag: "Changed",
628
+ sha,
629
+ files: diff.out.split("\0").filter(Boolean)
630
+ };
631
+ });
632
+ // src/formal/violations.ts
633
+ const sandbox = Effect.fn("formal.sandbox")(function* (repo, model) {
634
+ const fs = yield* FileSystem;
635
+ const dir = yield* fs.makeTempDirectoryScoped({ prefix: "formal-model-" });
636
+ for (const file of repo.tracked.filter((f) => dirname(f) === dirname(model.path) && f.endsWith(".tla"))) yield* fs.copyFile(`${repo.root}/${file}`, `${dir}/${basename(file)}`);
637
+ return dir;
638
+ });
639
+ const held = Symbol("held");
640
+ const violate = Effect.fn("formal.violate")(function* (repo, m, target, edit) {
641
+ return yield* Effect.scoped(Effect.gen(function* () {
642
+ const fs = yield* FileSystem;
643
+ const tla = yield* Tla;
644
+ const dir = yield* sandbox(repo, m);
645
+ let cfg = m.config.cfg;
646
+ if (edit) {
647
+ const file = edit.file ?? m.path;
648
+ const changed = (yield* fs.readFileString(`${repo.root}/${file}`)).replace(edit.find, edit.replace);
649
+ if (file === m.config.path) {
650
+ const parsed = parseCfg(changed);
651
+ if ("_tag" in parsed) return parsed.message;
652
+ cfg = parsed;
653
+ } else yield* fs.writeFileString(`${dir}/${basename(file)}`, changed);
654
+ }
655
+ const level = m.semantics.operators.get(target)?.level ?? (cfg.properties.includes(target) ? 3 : 1);
656
+ const config = `${dir}/Check.cfg`;
657
+ yield* fs.writeFileString(config, derive(cfg, { target: {
658
+ name: target,
659
+ level
660
+ } }));
661
+ const run = yield* tla.tlc({
662
+ spec: `${dir}/${basename(m.path)}`,
663
+ config,
664
+ timeoutSeconds: m.entry.timeoutSeconds,
665
+ mode: {
666
+ _tag: "Check",
667
+ coverage: false
668
+ }
669
+ });
670
+ if (run._tag === "TimedOut") return `TLC timed out after ${m.entry.timeoutSeconds} s`;
671
+ if (run.code === 0) return held;
672
+ const violated = [
673
+ 2107,
674
+ 2108,
675
+ 2110,
676
+ 2112,
677
+ 2116
678
+ ];
679
+ if ([12, 13].includes(run.code) && run.messages.some((x) => violated.includes(x.code)) && !run.messages.some((x) => x.severity >= 1 && x.severity <= 3 && ![
680
+ ...violated,
681
+ 2121,
682
+ 2264
683
+ ].includes(x.code))) return void 0;
684
+ return `TLC exited ${run.code}; ${run.messages.find((x) => x.severity >= 1 && x.severity <= 3)?.text ?? "expected a violation of " + target}`;
685
+ }));
686
+ });
687
+ // src/formal/proofs.ts
688
+ const matches = (glob, file) => new Bun.Glob(glob).match(file);
689
+ const critical = (repo) => Effect.succeed([
690
+ ...repo.file.critical.flatMap((glob) => repo.tracked.some((f) => matches(glob, f)) ? [] : [{
691
+ file: "specs.json",
692
+ message: `critical glob ${glob} matches no tracked file`
693
+ }]),
694
+ ...Object.entries({
695
+ ...repo.file.specs,
696
+ ...repo.file.kani
697
+ }).flatMap(([file, e]) => e.covers.flatMap((glob) => repo.tracked.some((f) => matches(glob, f)) ? [] : [{
698
+ file,
699
+ message: `covers glob ${glob} matches no tracked file`
700
+ }])),
701
+ ...repo.tracked.flatMap((file) => {
702
+ const glob = repo.file.critical.find((g) => matches(g, file));
703
+ return glob !== void 0 && !Object.values({
704
+ ...repo.file.specs,
705
+ ...repo.file.kani
706
+ }).some((e) => e.mode !== "library" && e.covers.some((g) => matches(g, file))) ? [{
707
+ file,
708
+ message: `matches critical glob ${glob} but no spec covers it`
709
+ }] : [];
710
+ })
711
+ ]);
712
+ const fairness = (repo) => Effect.succeed(repo.specs.flatMap((s) => s.analysis._tag === "Parsed" ? s.analysis.semantics.fairness.filter((f) => !f.valid).map((f) => ({
713
+ file: s.path,
714
+ message: `${f.kind} at line ${f.line}; fairness must be WF_vars or SF_vars`
715
+ })) : []));
716
+ const coverageCheck = (repo) => Effect.gen(function* () {
717
+ const gate = yield* Gate;
718
+ const fs = yield* FileSystem;
719
+ const problems = [];
720
+ for (const m of repo.models.filter((m) => m.entry.mode === "gate")) {
721
+ const result = yield* gate.run(m);
722
+ if (result._tag === "Failed") continue;
723
+ const start = problems.length;
724
+ for (const a of result.coverage.actions.filter((a) => a.generated === 0n)) problems.push({
725
+ file: m.path,
726
+ message: `action ${a.name} (line ${a.span.line}) is never enabled`
727
+ });
728
+ for (const z of result.coverage.zeros) {
729
+ const candidate = join(dirname(m.path), `${z.span.module}.tla`);
730
+ const file = repo.tracked.includes(candidate) ? candidate : m.path;
731
+ const source = (yield* fs.readFileString(`${repo.root}/${file}`)).split("\n").slice(z.span.line - 1, z.span.endLine).join("\n").slice(z.span.col - 1, z.span.endLine === z.span.line ? z.span.endCol : void 0).slice(0, 60);
732
+ problems.push({
733
+ file,
734
+ message: `line ${z.span.line}, col ${z.span.col} to line ${z.span.endLine}, col ${z.span.endCol} never runs in ${z.scope}: ${source}`
735
+ });
736
+ }
737
+ if (problems.length === start) yield* Console.log(`coverage: ${m.path}: no dead logic`);
738
+ }
739
+ return problems;
740
+ }).pipe(Effect.mapError((e) => new FormalError({ message: String(e) })));
741
+ const witnesses = (repo) => Effect.gen(function* () {
742
+ const gate = yield* Gate;
743
+ const problems = [];
744
+ for (const m of repo.models) for (const name of m.entry.witnesses) {
745
+ const op = m.semantics.operators.get(name);
746
+ const message = !op ? `${name} is not defined` : op.level === 2 ? `${name} is an action; check [][${name}]_vars instead` : op.params.length ? `${name} must have zero arguments` : [...m.config.cfg.invariants, ...m.config.cfg.properties].includes(name) ? `${name} is already checked by ${m.config.path}` : void 0;
747
+ if (message !== void 0) {
748
+ problems.push({
749
+ file: m.path,
750
+ message
751
+ });
752
+ continue;
753
+ }
754
+ if (m.entry.mode !== "gate" || (yield* gate.run(m))._tag === "Failed") continue;
755
+ const result = yield* violate(repo, m, name);
756
+ if (result !== void 0) problems.push({
757
+ file: m.path,
758
+ message: result === held ? `${name} holds; a witness must be violated` : result
759
+ });
760
+ else yield* Console.log(`witnesses: ${m.path}: ${name} violated`);
761
+ }
762
+ return problems;
763
+ }).pipe(Effect.mapError((e) => new FormalError({ message: String(e) })));
764
+ const breaks = (repo) => Effect.gen(function* () {
765
+ const gate = yield* Gate;
766
+ const fs = yield* FileSystem;
767
+ const changed = yield* changes(repo.root);
768
+ const problems = [];
769
+ if (changed._tag === "All") yield* Console.log(`breaks: BASE ${changed.base} does not resolve; running every break`);
770
+ for (const m of repo.models) {
771
+ const targets = [...m.config.cfg.invariants, ...m.config.cfg.properties];
772
+ for (const target of targets) if (!m.entry.breaks.some((b) => b.violates === target)) problems.push({
773
+ file: m.config.path,
774
+ message: `${target} has no break`
775
+ });
776
+ const dirty = changed._tag === "All" || changed.files.some((f) => f === "specs.json" || dirname(f) === dirname(m.path) && /\.(?:tla|cfg)$/u.test(f) || m.entry.breaks.some((b) => b.file === f));
777
+ if (!dirty && m.entry.mode === "gate") yield* Console.log(`breaks: ${m.path}: unchanged since ${changed.sha.slice(0, 7)}, not run`);
778
+ for (const [index, b] of m.entry.breaks.entries()) {
779
+ const label = `break ${index + 1}`;
780
+ const file = b.file ?? m.path;
781
+ if (!targets.includes(b.violates)) {
782
+ problems.push({
783
+ file: m.path,
784
+ message: `${label}: ${b.violates} is not checked by ${m.config.path}`
785
+ });
786
+ continue;
787
+ }
788
+ if (!repo.tracked.includes(file) || !(file === m.config.path || dirname(file) === dirname(m.path) && file.endsWith(".tla"))) {
789
+ problems.push({
790
+ file: m.path,
791
+ message: `${label}: ${file} must be a tracked module in the spec directory or its config`
792
+ });
793
+ continue;
794
+ }
795
+ const count = (yield* fs.readFileString(`${repo.root}/${file}`)).split(b.find).length - 1;
796
+ if (count !== 1) {
797
+ problems.push({
798
+ file: m.path,
799
+ message: `${label} find matches ${count} times in ${file}`
800
+ });
801
+ continue;
802
+ }
803
+ if (!dirty || m.entry.mode !== "gate" || (yield* gate.run(m))._tag === "Failed") continue;
804
+ const result = yield* violate(repo, m, b.violates, b);
805
+ if (result !== void 0) problems.push({
806
+ file: m.path,
807
+ message: `${label} (${b.violates}): ${result === held ? `${b.violates} still holds after the edit` : result}`
808
+ });
809
+ else yield* Console.log(`breaks: ${m.path}: ${label} violated ${b.violates}`);
810
+ }
811
+ }
812
+ return problems;
813
+ }).pipe(Effect.mapError((e) => new FormalError({ message: String(e) })));
814
+ // src/formal/traces.ts
815
+ const quote = (s) => JSON.stringify(s);
816
+ const RawState = Schema.Record(Schema.String, Schema.Json);
817
+ const Raw = Schema.Array(RawState);
818
+ const decodeAction = Schema.decodeUnknownSync(Schema.Struct({
819
+ name: Schema.String,
820
+ context: Schema.optionalKey(Schema.NullOr(Schema.Record(Schema.String, Schema.Json))),
821
+ location: Schema.optionalKey(Schema.Json)
822
+ }));
823
+ const normalize = (text, plan) => {
824
+ const lines = text === "" ? [] : (text.endsWith("\n") ? text.slice(0, -1) : text).split("\n");
825
+ if (lines.length !== plan.count) throw new Error(`only ${lines.length} of ${plan.count} behaviors reached depth ${plan.depth}`);
826
+ return lines.map((line, index) => {
827
+ const states = Schema.decodeSync(Schema.fromJsonString(Raw))(line);
828
+ if (states.length !== plan.depth) throw new Error(`trace ${index} has ${states.length} states; expected ${plan.depth}`);
829
+ const steps = states.map((s, i) => {
830
+ if (s.formal_tick !== i) throw new Error(`trace ${index} state ${i} has wrong formal_tick`);
831
+ const action = decodeAction(s._action);
832
+ const name = action.name.replace(/^Formal_/u, "");
833
+ if (action.name !== `Formal_${name}` || (i === 0 ? name !== plan.init : !plan.actions.has(name))) throw new Error(`trace ${index} state ${i} has unknown action ${action.name}`);
834
+ const args = action.context ?? {};
835
+ if (Object.keys(args).length !== (i === 0 ? 0 : plan.actions.get(name))) throw new Error(`trace ${index} state ${i} has wrong action arity`);
836
+ const keys = Object.keys(s).toSorted();
837
+ const expected = [
838
+ ...plan.variables,
839
+ "_action",
840
+ "formal_tick"
841
+ ].toSorted();
842
+ if (JSON.stringify(keys) !== JSON.stringify(expected)) throw new Error(`trace ${index} state ${i} has wrong variables`);
843
+ return {
844
+ action: name,
845
+ args,
846
+ state: Object.fromEntries(Object.entries(s).filter(([k]) => k !== "_action" && k !== "formal_tick"))
847
+ };
848
+ });
849
+ return JSON.stringify(sorted({
850
+ index,
851
+ constants: plan.constants,
852
+ steps
853
+ }));
854
+ }).join("\n") + "\n";
855
+ };
856
+ const traces = (repo) => Effect.gen(function* () {
857
+ const fs = yield* FileSystem;
858
+ const tla = yield* Tla;
859
+ const gate = yield* Gate;
860
+ const problems = [];
861
+ for (const m of repo.models) {
862
+ if (m.entry.mode !== "gate" || !m.entry.traces) continue;
863
+ const t = m.entry.traces;
864
+ const result = yield* gate.run(m);
865
+ if (result._tag === "Failed") continue;
866
+ const finding = yield* Effect.scoped(Effect.gen(function* () {
867
+ const section = (k) => m.config.cfg.sections.find((s) => s.keyword === k)?.text;
868
+ const spec = section("SPECIFICATION");
869
+ const pair = spec !== void 0 ? m.semantics.specifications.get(spec) : {
870
+ init: section("INIT"),
871
+ next: section("NEXT")
872
+ };
873
+ if (pair?.init === void 0 || pair.next === void 0) return "cannot derive zero-argument INIT/NEXT from config";
874
+ const { init, next } = pair;
875
+ const names = [...new Set(result.coverage.actions.map((a) => a.name))];
876
+ const actions = /* @__PURE__ */ new Map();
877
+ for (const name of names) {
878
+ const op = m.semantics.operators.get(name);
879
+ if (name === next || !op || op.params.some((p) => p.arity !== 0)) return `cannot wrap action ${name}: unnamed, INSTANCE'd, or higher-order action`;
880
+ actions.set(name, op.params.length);
881
+ }
882
+ for (const name of Object.keys(t.steer ?? {})) if (!actions.has(name)) return `steer key ${name} is not an action`;
883
+ if ([...m.semantics.operators.keys(), ...m.semantics.variables].some((n) => n === "FormalTraces" || n === "formal_tick" || n.startsWith("Formal_"))) return "generated trace name collides with spec";
884
+ const dir = yield* sandbox(repo, m);
885
+ const raw = `${dir}/raw.ndjson`;
886
+ const module = basename(m.path, ".tla");
887
+ const slash = "\\";
888
+ const defs = names.map((name) => {
889
+ const params = m.semantics.operators.get(name)?.params.map((p) => p.name) ?? [];
890
+ const args = params.length ? `(${params.join(", ")})` : "";
891
+ const keep = t.steer?.[name];
892
+ return `Formal_${name}${args} == Formal_M!${name}${args} /${slash} formal_tick' = formal_tick + 1${keep === void 0 ? "" : ` /${slash} Formal_TLC!RandomElement(1 .. ${keep}) = 1`}`;
893
+ });
894
+ const wrapper = [
895
+ "---- MODULE FormalTraces ----",
896
+ `EXTENDS ${module}`,
897
+ "VARIABLE formal_tick",
898
+ `Formal_M == INSTANCE ${module}`,
899
+ "Formal_TLC == INSTANCE TLC",
900
+ "Formal_Ext == INSTANCE TLCExt",
901
+ "Formal_IO == INSTANCE IOUtils",
902
+ `Formal_${init} == Formal_M!${init} /${slash} formal_tick = 0`,
903
+ ...defs,
904
+ "Formal_Count == Formal_TLC!TLCGet(\"stats\").traces",
905
+ `Formal_Fresh == 1 ${slash}notin DOMAIN Formal_TLC!TLCGet("all") ${slash}/ Formal_TLC!TLCGet(1) # Formal_Count`,
906
+ `Formal_Export == (Formal_TLC!TLCGet("level") = ${t.depth} /${slash} Formal_Fresh) =>`,
907
+ ` /${slash} Formal_TLC!TLCSet(1, Formal_Count)`,
908
+ ` /${slash} Formal_IO!Serialize(<<Formal_Ext!Trace>>, ${quote(raw)}, [format |-> "NDJSON", charset |-> "UTF-8", openOptions |-> <<"WRITE", "CREATE", "APPEND">>])`,
909
+ "====",
910
+ ""
911
+ ].join("\n");
912
+ yield* fs.writeFileString(`${dir}/FormalTraces.tla`, wrapper);
913
+ const cfg = derive(m.config.cfg, { simulation: {
914
+ init,
915
+ next,
916
+ overrides: [`${init} <- Formal_${init}`, ...names.map((n) => `${n} <- Formal_${n}`)]
917
+ } });
918
+ yield* fs.writeFileString(`${dir}/FormalTraces.cfg`, cfg);
919
+ const run = yield* tla.tlc({
920
+ spec: `${dir}/FormalTraces.tla`,
921
+ config: `${dir}/FormalTraces.cfg`,
922
+ timeoutSeconds: m.entry.timeoutSeconds,
923
+ mode: {
924
+ _tag: "Simulate",
925
+ num: t.count,
926
+ depth: t.depth,
927
+ seed: t.seed
928
+ }
929
+ });
930
+ if (run._tag === "TimedOut") return `simulation timed out after ${m.entry.timeoutSeconds} s`;
931
+ if (run.code !== 0 || run.messages.some((x) => x.severity >= 1 && x.severity <= 3)) return `simulation exited ${run.code}: ${run.messages.filter((x) => x.severity >= 1 && x.severity <= 3).map((x) => x.text).join("; ")}`;
932
+ const text = (yield* fs.exists(raw)) ? yield* fs.readFileString(raw) : "";
933
+ const normalized = yield* Effect.try({
934
+ try: () => normalize(text, {
935
+ count: t.count,
936
+ depth: t.depth,
937
+ init,
938
+ actions,
939
+ variables: m.semantics.variables,
940
+ constants: Object.fromEntries(m.config.cfg.constants.flatMap((c) => c._tag === "Value" ? [[c.name, /^-?\d+$/u.test(c.value) ? Number(c.value) : c.value]] : []))
941
+ }),
942
+ catch: (e) => new FormalError({ message: e instanceof Error ? e.message : String(e) })
943
+ });
944
+ const file = `${dir}/traces.ndjson`;
945
+ yield* fs.writeFileString(file, normalized);
946
+ let owner = dirname(t.conformance);
947
+ while (owner !== "." && !repo.tracked.includes(`${owner}/package.json`)) owner = dirname(owner);
948
+ const started = yield* Clock.currentTimeMillis;
949
+ const conformance = yield* Effect.scoped(Effect.gen(function* () {
950
+ const p = yield* ChildProcess.make("vp", [
951
+ "test",
952
+ "run",
953
+ relative(owner, t.conformance)
954
+ ], {
955
+ cwd: `${repo.root}/${owner}`,
956
+ env: { [FORMAL_TRACES]: file },
957
+ extendEnv: true,
958
+ stdin: "ignore",
959
+ killSignal: "SIGKILL"
960
+ });
961
+ return yield* Effect.all([
962
+ p.stdout.pipe(Stream.decodeText, Stream.mkString),
963
+ p.stderr.pipe(Stream.decodeText, Stream.mkString),
964
+ p.exitCode
965
+ ], { concurrency: "unbounded" });
966
+ }).pipe(Effect.timeoutOption(`${m.entry.timeoutSeconds} seconds`)));
967
+ if (Option.isNone(conformance)) return `conformance test ${t.conformance} timed out`;
968
+ if (Number(conformance.value[2]) !== 0) {
969
+ yield* Console.log((conformance.value[0] + conformance.value[1]).slice(-4e3));
970
+ return `conformance test ${t.conformance} failed (vp test exited ${conformance.value[2]})`;
971
+ }
972
+ const receipt = receiptPath(file);
973
+ const replayed = (yield* fs.exists(receipt)) ? (yield* fs.readFileString(receipt)).trim() : "0";
974
+ if (replayed !== String(t.count)) return `conformance test replayed ${replayed} of ${t.count} traces; register them with replayTraces`;
975
+ yield* Console.log(`traces: ${m.path}: ${t.count} traces of ${t.depth} states (sha256 ${createHash("sha256").update(normalized).digest("hex")}), conformance passed in ${((yield* Clock.currentTimeMillis) - started) / 1e3} s`);
976
+ })).pipe(Effect.catch((e) => Effect.succeed(e.message)));
977
+ if (finding !== void 0) problems.push({
978
+ file: m.path,
979
+ message: finding
980
+ });
981
+ }
982
+ return problems;
983
+ });
984
+ // src/formal/checks.ts
985
+ const configs = (repo) => repo.specs.filter((s) => s.analysis._tag === "Parsed").flatMap((s) => s.configs).flatMap((c) => "_tag" in c.cfg ? [] : [{
986
+ path: c.path,
987
+ cfg: c.cfg
988
+ }]);
989
+ const specs = (repo) => Effect.succeed([
990
+ ...repo.decodeErrors.map((message) => ({
991
+ file: "specs.json",
992
+ message
993
+ })),
994
+ ...repo.specs.flatMap((s) => [
995
+ ...!s.entry && repo.decodeErrors.length === 0 ? [{
996
+ file: s.path,
997
+ message: "tracked spec has no specs.json entry"
998
+ }] : [],
999
+ ...s.entry && s.entry.mode !== "library" && !s.configs.some((c) => c.path === s.path.replace(/\.tla$/u, ".cfg")) ? [{
1000
+ file: s.path,
1001
+ message: "runnable spec has no matching .cfg"
1002
+ }] : [],
1003
+ ...s.entry?.mode === "library" && s.configs.length ? [{
1004
+ file: s.path,
1005
+ message: "library spec must not have a config"
1006
+ }] : [],
1007
+ ...s.configs.flatMap((c) => "_tag" in c.cfg ? [{
1008
+ file: c.path,
1009
+ message: c.cfg.message
1010
+ }] : [])
1011
+ ]),
1012
+ ...Object.keys({
1013
+ ...repo.file.specs,
1014
+ ...repo.file.kani
1015
+ }).filter((f) => !repo.tracked.includes(f)).map((file) => ({
1016
+ file,
1017
+ message: "entry names no tracked file"
1018
+ })),
1019
+ ...repo.orphans.map((file) => ({
1020
+ file,
1021
+ message: "config pairs with no module; give a variant its own module that EXTENDS X"
1022
+ }))
1023
+ ]);
1024
+ const sany = (repo) => Effect.succeed(repo.specs.flatMap((s) => s.analysis._tag === "Unparsed" ? [{
1025
+ file: s.path,
1026
+ message: s.analysis.output
1027
+ }] : []));
1028
+ const typeok = (repo) => Effect.succeed([...configs(repo).filter((c) => !c.cfg.invariants.includes("TypeOK")).map((c) => ({
1029
+ file: c.path,
1030
+ message: "does not check INVARIANT TypeOK"
1031
+ })), ...repo.specs.flatMap((s) => {
1032
+ if (s.analysis._tag !== "Parsed" || s.configs.length === 0) return [];
1033
+ const typed = s.analysis.semantics.typeOK;
1034
+ if (!typed) return s.configs.some((c) => !("_tag" in c.cfg) && c.cfg.invariants.includes("TypeOK")) ? [{
1035
+ file: s.path,
1036
+ message: "TypeOK is not defined"
1037
+ }] : [];
1038
+ const missing = s.analysis.semantics.variables.filter((v) => !typed.has(v));
1039
+ return missing.length ? [{
1040
+ file: s.path,
1041
+ message: `TypeOK does not constrain ${missing.join(", ")}; give each a conjunct such as ${missing[0]} \\in S`
1042
+ }] : [];
1043
+ })]);
1044
+ const deadlock = (repo) => Effect.succeed(configs(repo).filter((c) => c.cfg.checkDeadlock.includes(false)).map((c) => ({
1045
+ file: c.path,
1046
+ message: "CHECK_DEADLOCK FALSE; give the bounded spec an explicit finished step"
1047
+ })));
1048
+ const constants = (repo) => Effect.succeed(configs(repo).flatMap((c) => {
1049
+ const groups = /* @__PURE__ */ new Map();
1050
+ for (const constant of c.cfg.constants) {
1051
+ const n = integer(constant);
1052
+ if (n === void 0) continue;
1053
+ groups.set(n, [...groups.get(n) ?? [], constant.name]);
1054
+ }
1055
+ return [...groups].filter(([, names]) => names.length > 1).map(([n, names]) => ({
1056
+ file: c.path,
1057
+ message: `${names.length === 2 ? names.join(" and ") : `${names.slice(0, -1).join(", ")} and ${names.at(-1)}`} ${names.length === 2 ? "both" : "all"} equal ${n}`
1058
+ }));
1059
+ }));
1060
+ const format = (repo) => Effect.gen(function* () {
1061
+ const tla = yield* Tla;
1062
+ const fs = yield* FileSystem;
1063
+ return (yield* Effect.forEach(repo.specs.filter((s) => s.analysis._tag === "Parsed"), (s) => Effect.gen(function* () {
1064
+ const result = yield* tla.format(`${repo.root}/${s.path}`);
1065
+ if (result._tag === "Failed") return [{
1066
+ file: s.path,
1067
+ message: result.output
1068
+ }];
1069
+ const text = yield* fs.readFileString(`${repo.root}/${s.path}`);
1070
+ if (text === result.text) return [];
1071
+ const lines = text.split("\n");
1072
+ const expected = result.text.split("\n");
1073
+ let line = 0;
1074
+ while (line < Math.max(lines.length, expected.length) && lines[line] === expected[line]) line++;
1075
+ return [{
1076
+ file: s.path,
1077
+ message: `differs from formatter.Main at line ${line + 1}; run ${tla.fixCommand(s.path)}`
1078
+ }];
1079
+ }), { concurrency: 4 })).flat();
1080
+ }).pipe(Effect.mapError((cause) => new FormalError({ message: String(cause) })));
1081
+ const tlc = (repo) => Effect.gen(function* () {
1082
+ const gate = yield* Gate;
1083
+ const problems = [];
1084
+ for (const m of repo.models.filter((m) => m.entry.mode === "gate")) {
1085
+ const result = yield* gate.run(m);
1086
+ if (result._tag === "Failed") problems.push({
1087
+ file: m.config.path,
1088
+ message: result.message
1089
+ });
1090
+ else yield* Console.log(`tlc: ${m.config.path}: ${result.states} distinct states in ${result.seconds} s`);
1091
+ }
1092
+ return problems;
1093
+ });
1094
+ const checks = {
1095
+ specs,
1096
+ critical,
1097
+ sany,
1098
+ typeok,
1099
+ deadlock,
1100
+ fairness,
1101
+ constants,
1102
+ format,
1103
+ tlc,
1104
+ coverage: coverageCheck,
1105
+ witnesses,
1106
+ breaks,
1107
+ traces
1108
+ };
1109
+ // 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
+ const load = Effect.fn("formal.load")(function* (root) {
1172
+ const fs = yield* FileSystem;
1173
+ const path = yield* Path;
1174
+ const tla = yield* Tla;
1175
+ const git = yield* ChildProcess.make("git", ["ls-files", "-z"], {
1176
+ cwd: root,
1177
+ stdin: "ignore"
1178
+ });
1179
+ const [files, code] = yield* Effect.all([git.stdout.pipe(Stream.decodeText, Stream.mkString), git.exitCode], { concurrency: "unbounded" });
1180
+ if (Number(code) !== 0) return yield* new FormalError({ message: "git ls-files failed" });
1181
+ const tracked = yield* Effect.filter(files.split("\0").filter(Boolean), (file) => fs.exists(path.join(root, file)));
1182
+ let file = {
1183
+ critical: [],
1184
+ specs: {},
1185
+ kani: {}
1186
+ };
1187
+ let decodeErrors = [];
1188
+ if (yield* fs.exists(path.join(root, "specs.json"))) {
1189
+ const raw = yield* fs.readFileString(path.join(root, "specs.json"));
1190
+ const decoded = Schema.decodeResult(Schema.fromJsonString(SpecsFile))(raw, {
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}`);
1196
+ }
1197
+ const modules = tracked.filter((f) => f.endsWith(".tla"));
1198
+ const configs = tracked.filter((f) => f.endsWith(".cfg") && modules.some((m) => path.dirname(m) === path.dirname(f)));
1199
+ const paired = /* @__PURE__ */ new Set();
1200
+ const specs = yield* Effect.forEach(modules, (module) => Effect.gen(function* () {
1201
+ const stem = module.slice(0, -4);
1202
+ const matches = configs.filter((c) => c === `${stem}.cfg`);
1203
+ const parsed = yield* Effect.forEach(matches, (c) => Effect.gen(function* () {
1204
+ paired.add(c);
1205
+ return {
1206
+ path: c,
1207
+ cfg: parseCfg(yield* fs.readFileString(path.join(root, c)))
1208
+ };
1209
+ }), { concurrency: 1 });
1210
+ return {
1211
+ path: module,
1212
+ entry: file.specs[module],
1213
+ analysis: yield* tla.analyze(path.join(root, module)),
1214
+ configs: parsed
1215
+ };
1216
+ }), { concurrency: 4 });
1217
+ return {
1218
+ root,
1219
+ tracked,
1220
+ file,
1221
+ specs,
1222
+ models: specs.flatMap((s) => {
1223
+ const c = s.configs[0];
1224
+ return s.analysis._tag === "Parsed" && s.entry && s.entry.mode !== "library" && c && !("_tag" in c.cfg) ? [{
1225
+ root,
1226
+ path: s.path,
1227
+ entry: s.entry,
1228
+ semantics: s.analysis.semantics,
1229
+ config: {
1230
+ path: c.path,
1231
+ cfg: c.cfg
1232
+ }
1233
+ }] : [];
1234
+ }),
1235
+ orphans: configs.filter((c) => !paired.has(c)),
1236
+ decodeErrors
1237
+ };
1238
+ });
1239
+ // src/formal.ts
1240
+ const singleLine = (message) => message.replaceAll(/\s+/gu, " ").trim();
1241
+ const checkName = (name) => Object.hasOwn(checks, name);
1242
+ const formal = Effect.fn("formal")(function* (root) {
1243
+ const repo = yield* load(root);
1244
+ const findings = [];
1245
+ for (const name of Object.keys(checks)) {
1246
+ if (!checkName(name)) continue;
1247
+ const problems = yield* checks[name](repo);
1248
+ findings.push(...problems.map((problem) => ({
1249
+ ...problem,
1250
+ check: name
1251
+ })));
1252
+ }
1253
+ findings.sort((a, b) => a.file.localeCompare(b.file) || Object.keys(checks).indexOf(a.check) - Object.keys(checks).indexOf(b.check));
1254
+ if (findings.length) {
1255
+ for (const f of findings) yield* Console.error(`${f.file}: ${f.check}: ${singleLine(f.message)}`);
1256
+ 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`);
1258
+ return findings.length ? 1 : 0;
1259
+ });
1260
+ if (import.meta.main) {
1261
+ if (process.argv.length > 2) {
1262
+ process.stderr.write("usage: formal\n");
1263
+ process.exitCode = 2;
1264
+ } else {
1265
+ const exit = await Effect.runPromiseExit(Effect.scoped(Effect.gen(function* () {
1266
+ const git = yield* ChildProcess.make("git", ["rev-parse", "--show-toplevel"], { stdin: "ignore" });
1267
+ const [root, code] = yield* Effect.all([git.stdout.pipe(Stream.decodeText, Stream.mkString), git.exitCode], { concurrency: "unbounded" });
1268
+ if (Number(code) !== 0) return yield* new FormalError({ message: "run formal inside a git repository" });
1269
+ return yield* formal(root.trim()).pipe(Effect.provide(Gate.layer.pipe(Layer.provideMerge(Tla.layer))));
1270
+ })).pipe(Effect.provide(NodeServices.layer)));
1271
+ if (Exit.isSuccess(exit)) process.exitCode = exit.value;
1272
+ else {
1273
+ const error = Cause.squash(exit.cause);
1274
+ process.stderr.write(`formal: ${singleLine(error instanceof Error ? error.message : String(error))}\n`);
1275
+ process.exitCode = 1;
1276
+ }
1277
+ }
1278
+ }
1279
+ export { formal };