@milkenjoyer/toolchain 0.11.0 → 0.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. package/README.md +190 -17
  2. package/dist/arithmetic.d.mts +45 -0
  3. package/dist/arithmetic.mjs +25 -0
  4. package/dist/conformance.d.mts +18 -0
  5. package/dist/conformance.mjs +31 -0
  6. package/dist/effect-plugin.mjs +1 -1
  7. package/dist/exceptions-Dybl9xpc.mjs +11 -0
  8. package/dist/exceptions.mjs +1 -1
  9. package/dist/foldkit-plugin.mjs +1 -1
  10. package/dist/formal.d.mts +131 -3
  11. package/dist/formal.mjs +1006 -146
  12. package/dist/guard.mjs +2 -2
  13. package/dist/knip.mjs +2 -1
  14. package/dist/{lint-JSnPsGO1.mjs → lint-IhmtnBbx.mjs} +3 -3
  15. package/dist/lint.mjs +1 -1
  16. package/dist/plugin.mjs +1 -1
  17. package/dist/proof.mjs +1 -1
  18. package/dist/trace-file-KuPRWgwt.mjs +17 -0
  19. package/dist/verdict.d.mts +1 -1
  20. package/dist/verdict.mjs +1 -1
  21. package/dist/{verification-p2rSx5lc.d.mts → verification-FgQFU1YN.d.mts} +1 -1
  22. package/dist/verification.d.mts +1 -1
  23. package/dist/verification.mjs +1 -1
  24. package/package.json +12 -3
  25. package/src/formal/arithmetic.ts +190 -0
  26. package/src/formal/cfg.ts +58 -1
  27. package/src/formal/checks.ts +57 -52
  28. package/src/formal/coverage.ts +79 -0
  29. package/src/formal/gate.ts +74 -0
  30. package/src/formal/git.ts +38 -0
  31. package/src/formal/proof-run.ts +77 -0
  32. package/src/formal/proofs.ts +226 -0
  33. package/src/formal/prover.ts +291 -0
  34. package/src/formal/semantics.ts +110 -0
  35. package/src/formal/specs.ts +103 -31
  36. package/src/formal/tla.ts +60 -19
  37. package/src/formal/trace-file.ts +35 -0
  38. package/src/formal/traces.ts +293 -0
  39. package/src/formal/violations.ts +76 -0
  40. package/src/formal/z3.ts +321 -0
  41. package/src/formal.ts +10 -3
  42. package/dist/exceptions-Bq-9MxoR.mjs +0 -3
  43. package/dist/{effect-plugin-kYxB-5LJ.mjs → effect-plugin-BXUx9PPu.mjs} +1 -1
  44. package/dist/{plugin-BoBRGtGf.mjs → plugin-CwxhPtui.mjs} +1 -1
  45. package/dist/{verification-6s2chr4X.mjs → verification-oAc1Q6I9.mjs} +1 -1
@@ -1,4 +1,23 @@
1
+ type Operator = {
2
+ readonly name: string;
3
+ readonly level: number;
4
+ readonly params: ReadonlyArray<{
5
+ readonly name: string;
6
+ readonly arity: number;
7
+ }>;
8
+ readonly module: string;
9
+ };
1
10
  export type Semantics = {
11
+ readonly operators: ReadonlyMap<string, Operator>;
12
+ readonly fairness: ReadonlyArray<{
13
+ readonly kind: string;
14
+ readonly line: string;
15
+ readonly valid: boolean;
16
+ }>;
17
+ readonly specifications: ReadonlyMap<
18
+ string,
19
+ { readonly init: string; readonly next: string }
20
+ >;
2
21
  readonly variables: ReadonlyArray<string>;
3
22
  readonly typeOK: ReadonlySet<string> | undefined;
4
23
  };
@@ -138,7 +157,98 @@ export const fromXml = (xml: string): Semantics => {
138
157
  (node) => node !== undefined && value(node, "uniquename") === "TypeOK"
139
158
  );
140
159
  const body = typeOK && child(typeOK, "body")?.children[0];
160
+ const definitions = [...entries.values()].filter(
161
+ (n): n is Node =>
162
+ n?.tag === "UserDefinedOpKind" &&
163
+ closure.has(value(child(n, "location") ?? n, "filename"))
164
+ );
165
+ const operators = new Map<string, Operator>();
166
+ const fairness: { kind: string; line: string; valid: boolean }[] = [];
167
+ const specifications = new Map<string, { init: string; next: string }>();
168
+ const opName = (n: Node) => {
169
+ const t = target(n);
170
+ return t ? value(entries.get(t.uid) ?? n, "uniquename") : "";
171
+ };
172
+ for (const n of definitions) {
173
+ const name = value(n, "uniquename");
174
+ const params = (child(n, "params")?.children ?? [])
175
+ .map(
176
+ (ref) =>
177
+ entries.get(value(child(ref, "FormalParamNodeRef") ?? ref, "UID")) ??
178
+ ref
179
+ )
180
+ .map((p) => ({
181
+ name: value(p, "uniquename"),
182
+ arity: Number(value(p, "arity")),
183
+ }));
184
+ operators.set(name, {
185
+ name,
186
+ level: Number(value(n, "level")),
187
+ params,
188
+ module: value(child(n, "location") ?? n, "filename"),
189
+ });
190
+ if (
191
+ value(child(n, "location") ?? n, "filename") ===
192
+ value(modules, "RootModule")
193
+ )
194
+ for (const a of descendants(child(n, "body") ?? n)) {
195
+ const kind = opName(a);
196
+ if (!["$WF", "$SF"].includes(kind)) continue;
197
+ const sub = operands(a)[0];
198
+ fairness.push({
199
+ kind: `${kind.slice(1)}_${sub && opName(sub) === "$Tuple" ? `<< ${operands(sub).map(opName).join(", ")} >>` : sub ? opName(sub) : "?"}`,
200
+ line: value(child(child(a, "location") ?? a, "line") ?? a, "begin"),
201
+ valid:
202
+ sub !== undefined &&
203
+ opName(sub) === "vars" &&
204
+ operands(sub).length === 0,
205
+ });
206
+ }
207
+ const flatten = (a: Node, seen = new Set<string>()): Node[] => {
208
+ const t = target(a);
209
+ if (["$ConjList", "\\land"].includes(opName(a)))
210
+ return operands(a).flatMap((x) => flatten(x, new Set(seen)));
211
+ if (
212
+ t &&
213
+ t.ref.tag === "UserDefinedOpKindRef" &&
214
+ Number(value(a, "level")) === 3 &&
215
+ operands(a).length === 0 &&
216
+ !seen.has(t.uid)
217
+ ) {
218
+ const b = child(entries.get(t.uid) ?? a, "body")?.children[0];
219
+ if (b) {
220
+ seen.add(t.uid);
221
+ return flatten(b, seen);
222
+ }
223
+ }
224
+ return [a];
225
+ };
226
+ const body = child(n, "body")?.children[0];
227
+ if (!body) continue;
228
+ const conjuncts = flatten(body);
229
+ const state = conjuncts.filter((a) => Number(value(a, "level")) === 1);
230
+ const boxes = conjuncts.filter((a) => opName(a) === "[]");
231
+ const box = boxes[0] && operands(boxes[0])[0];
232
+ const next =
233
+ box && opName(box) === "$SquareAct" ? operands(box)[0] : undefined;
234
+ if (
235
+ state.length === 1 &&
236
+ boxes.length === 1 &&
237
+ next &&
238
+ operands(next).length === 0
239
+ ) {
240
+ const init =
241
+ state[0] && operands(state[0]).length === 0
242
+ ? opName(state[0])
243
+ : undefined;
244
+ if (init !== undefined && init !== "")
245
+ specifications.set(name, { init, next: opName(next) });
246
+ }
247
+ }
141
248
  return {
249
+ operators,
250
+ fairness,
251
+ specifications,
142
252
  variables: [...new Set(variables.values())],
143
253
  typeOK: typeOK && new Set(body ? typed(body, new Set()) : []),
144
254
  };
@@ -18,15 +18,23 @@ const RepoPath = Schema.String.check(
18
18
  const OperatorName = Schema.String.check(
19
19
  Schema.isPattern(/^[A-Za-z_][A-Za-z0-9_]*$/u)
20
20
  );
21
- const Entry = Schema.Struct({
21
+ const Common = {
22
22
  synced: Schema.String.check(Schema.isPattern(/^[0-9a-f]{40}$/u)),
23
23
  covers: Schema.NonEmptyArray(Schema.NonEmptyString),
24
- mode: Schema.Literals(["gate", "manual"]),
24
+ };
25
+ const Runtime = {
26
+ ...Common,
25
27
  timeoutSeconds: Schema.Int.check(
26
28
  Schema.isBetween({ minimum: 1, maximum: 1800 })
27
29
  ).pipe(Schema.withDecodingDefaultKey(Effect.succeed(60))),
28
- witnesses: Schema.Array(OperatorName).pipe(
29
- Schema.withDecodingDefaultKey(Effect.succeed([]))
30
+ };
31
+ const Proof = {
32
+ ...Runtime,
33
+ witnesses: Schema.NonEmptyArray(OperatorName).check(
34
+ Schema.makeFilter(
35
+ (names) =>
36
+ new Set(names).size === names.length || "witnesses must be unique"
37
+ )
30
38
  ),
31
39
  breaks: Schema.Array(
32
40
  Schema.Struct({
@@ -36,28 +44,70 @@ const Entry = Schema.Struct({
36
44
  file: Schema.optionalKey(RepoPath),
37
45
  })
38
46
  ).pipe(Schema.withDecodingDefaultKey(Effect.succeed([]))),
39
- traces: Schema.optionalKey(
47
+ };
48
+ const Entry = Schema.Union([
49
+ Schema.Struct({
50
+ ...Proof,
51
+ mode: Schema.Literal("gate"),
52
+ traces: Schema.optionalKey(
53
+ Schema.Struct({
54
+ seed: Schema.Int.check(Schema.isGreaterThanOrEqualTo(0)),
55
+ depth: Schema.Int.check(
56
+ Schema.isBetween({ minimum: 1, maximum: 10000 })
57
+ ),
58
+ count: Schema.Int.check(
59
+ Schema.isBetween({ minimum: 1, maximum: 100000 })
60
+ ),
61
+ steer: Schema.optionalKey(
62
+ Schema.Record(
63
+ OperatorName,
64
+ Schema.Int.check(Schema.isBetween({ minimum: 2, maximum: 1000 }))
65
+ )
66
+ ),
67
+ conformance: RepoPath,
68
+ })
69
+ ),
70
+ }),
71
+ Schema.Struct({ ...Proof, mode: Schema.Literal("manual") }),
72
+ Schema.Struct({ ...Common, mode: Schema.Literal("library") }),
73
+ ]);
74
+ const ArithmeticCommon = {
75
+ covers: Common.covers,
76
+ timeoutSeconds: Runtime.timeoutSeconds,
77
+ breaks: Schema.NonEmptyArray(
40
78
  Schema.Struct({
41
- seed: Schema.Int.check(Schema.isGreaterThanOrEqualTo(0)),
42
- depth: Schema.Int.check(Schema.isBetween({ minimum: 1, maximum: 10000 })),
43
- count: Schema.Int.check(
44
- Schema.isBetween({ minimum: 1, maximum: 100000 })
45
- ),
79
+ file: RepoPath,
80
+ find: Schema.NonEmptyString,
81
+ replace: Schema.String,
46
82
  })
47
83
  ),
48
- conformance: Schema.optionalKey(RepoPath),
49
- }).check(
50
- Schema.makeFilter(
51
- (entry) =>
52
- (entry.traces === undefined) === (entry.conformance === undefined) ||
53
- "traces and conformance go together"
54
- )
55
- );
56
- const SpecsFile = Schema.Record(
57
- RepoPath.check(Schema.isPattern(/\.(?:tla|rs)$/u)),
58
- Entry
59
- );
60
- type SpecsFile = typeof SpecsFile.Type;
84
+ };
85
+ const ArithmeticProof = Schema.Union([
86
+ Schema.Struct({ ...ArithmeticCommon, kind: Schema.Literal("every") }),
87
+ Schema.Struct({
88
+ ...ArithmeticCommon,
89
+ kind: Schema.Literal("z3"),
90
+ samples: Schema.Int.check(
91
+ Schema.isBetween({ minimum: 10000, maximum: 1000000 })
92
+ ).pipe(Schema.withDecodingDefaultKey(Effect.succeed(10000))),
93
+ }),
94
+ ]);
95
+ const SpecsFile = Schema.Struct({
96
+ critical: Schema.NonEmptyArray(Schema.NonEmptyString),
97
+ arithmetic: Schema.optionalKey(Schema.Array(Schema.NonEmptyString)),
98
+ proofs: Schema.Record(
99
+ RepoPath.check(Schema.isPattern(/\.proof\.ts$/u)),
100
+ ArithmeticProof
101
+ ).pipe(Schema.withDecodingDefaultKey(Effect.succeed({}))),
102
+ specs: Schema.Record(RepoPath.check(Schema.isPattern(/\.tla$/u)), Entry),
103
+ kani: Schema.Record(
104
+ RepoPath.check(Schema.isPattern(/\.rs$/u)),
105
+ Schema.Struct({ ...Runtime, mode: Schema.Literals(["gate", "manual"]) })
106
+ ).pipe(Schema.withDecodingDefaultKey(Effect.succeed({}))),
107
+ });
108
+ type SpecsFile = Omit<typeof SpecsFile.Type, "critical"> & {
109
+ readonly critical: ReadonlyArray<string>;
110
+ };
61
111
  export type Analysis =
62
112
  | { readonly _tag: "Parsed"; readonly semantics: Semantics }
63
113
  | { readonly _tag: "Unparsed"; readonly output: string };
@@ -76,6 +126,7 @@ export type Repo = {
76
126
  readonly tracked: ReadonlyArray<string>;
77
127
  readonly file: SpecsFile;
78
128
  readonly specs: ReadonlyArray<Spec>;
129
+ readonly models: ReadonlyArray<Model>;
79
130
  readonly orphans: ReadonlyArray<string>;
80
131
  readonly decodeErrors: ReadonlyArray<string>;
81
132
  };
@@ -98,7 +149,7 @@ export const load = Effect.fn("formal.load")(function* (root: string) {
98
149
  files.split("\0").filter(Boolean),
99
150
  (file) => fs.exists(path.join(root, file))
100
151
  );
101
- let file: SpecsFile = {};
152
+ let file: SpecsFile = { critical: [], specs: {}, kani: {}, proofs: {} };
102
153
  let decodeErrors: ReadonlyArray<string> = [];
103
154
  if (yield* fs.exists(path.join(root, "specs.json"))) {
104
155
  const raw = yield* fs.readFileString(path.join(root, "specs.json"));
@@ -128,12 +179,7 @@ export const load = Effect.fn("formal.load")(function* (root: string) {
128
179
  (module) =>
129
180
  Effect.gen(function* () {
130
181
  const stem = module.slice(0, -4);
131
- const matches = configs.filter(
132
- (c) =>
133
- c === `${stem}.cfg` ||
134
- (c.startsWith(`${stem}.`) &&
135
- path.dirname(c) === path.dirname(module))
136
- );
182
+ const matches = configs.filter((c) => c === `${stem}.cfg`);
137
183
  const parsed = yield* Effect.forEach(
138
184
  matches,
139
185
  (c) =>
@@ -148,7 +194,7 @@ export const load = Effect.fn("formal.load")(function* (root: string) {
148
194
  );
149
195
  return {
150
196
  path: module,
151
- entry: file[module],
197
+ entry: file.specs[module],
152
198
  analysis: yield* tla.analyze(path.join(root, module)),
153
199
  configs: parsed,
154
200
  };
@@ -160,7 +206,33 @@ export const load = Effect.fn("formal.load")(function* (root: string) {
160
206
  tracked,
161
207
  file,
162
208
  specs,
209
+ models: specs.flatMap((s) => {
210
+ const c = s.configs[0];
211
+ return s.analysis._tag === "Parsed" &&
212
+ s.entry &&
213
+ s.entry.mode !== "library" &&
214
+ c &&
215
+ !("_tag" in c.cfg)
216
+ ? [
217
+ {
218
+ root,
219
+ path: s.path,
220
+ entry: s.entry,
221
+ semantics: s.analysis.semantics,
222
+ config: { path: c.path, cfg: c.cfg },
223
+ },
224
+ ]
225
+ : [];
226
+ }),
163
227
  orphans: configs.filter((c) => !paired.has(c)),
164
228
  decodeErrors,
165
229
  } satisfies Repo;
166
230
  });
231
+
232
+ export type Model = {
233
+ readonly root: string;
234
+ readonly path: string;
235
+ readonly entry: Exclude<typeof Entry.Type, { readonly mode: "library" }>;
236
+ readonly semantics: Semantics;
237
+ readonly config: { readonly path: string; readonly cfg: Cfg };
238
+ };
package/src/formal/tla.ts CHANGED
@@ -11,6 +11,7 @@ import * as Layer from "effect/Layer";
11
11
  import * as Option from "effect/Option";
12
12
  import { Path } from "effect/Path";
13
13
  import { ChildProcess, ChildProcessSpawner } from "effect/process";
14
+ import * as Result from "effect/Result";
14
15
  import * as Stream from "effect/Stream";
15
16
 
16
17
  import { fromXml } from "./semantics.ts";
@@ -19,7 +20,7 @@ import type { Analysis } from "./specs.ts";
19
20
  export class FormalError extends Data.TaggedError("FormalError")<{
20
21
  readonly message: string;
21
22
  }> {}
22
- type ToolMessage = {
23
+ export type ToolMessage = {
23
24
  readonly code: number;
24
25
  readonly severity: number;
25
26
  readonly text: string;
@@ -33,6 +34,14 @@ type TlcRun =
33
34
  }
34
35
  | { readonly _tag: "TimedOut" };
35
36
  type TlcInput = {
37
+ readonly mode:
38
+ | { readonly _tag: "Check"; readonly coverage: boolean }
39
+ | {
40
+ readonly _tag: "Simulate";
41
+ readonly num: number;
42
+ readonly depth: number;
43
+ readonly seed: number;
44
+ };
36
45
  readonly spec: string;
37
46
  readonly config: string;
38
47
  readonly timeoutSeconds: number;
@@ -44,12 +53,18 @@ type Formatted =
44
53
  const pins = [
45
54
  {
46
55
  file: "tla2tools-1.8.0.jar",
47
- url: "https://github.com/tlaplus/tlaplus/releases/download/v1.8.0/tla2tools.jar",
56
+ urls: [
57
+ "https://github.com/tlaplus/tlaplus/releases/download/v1.8.0/tla2tools.jar",
58
+ "https://api.github.com/repos/tlaplus/tlaplus/releases/assets/718777374",
59
+ ],
48
60
  sha256: "e1f11b75c4b5bc4478ba0ac5cb6ae28e40a01b0016bc33e0aa97da22dfdbcd7f",
49
61
  },
50
62
  {
51
63
  file: "CommunityModules-deps-202611060952.jar",
52
- url: "https://github.com/tlaplus/CommunityModules/releases/download/202611060952/CommunityModules-deps-202611060952.jar",
64
+ urls: [
65
+ "https://github.com/tlaplus/CommunityModules/releases/download/202611060952/CommunityModules-deps-202611060952.jar",
66
+ "https://api.github.com/repos/tlaplus/CommunityModules/releases/assets/716678698",
67
+ ],
53
68
  sha256: "a3b27c40f1fb5085f30c6ead37e918457f8cfc62c1e50a1dc668ef592ad2b5fb",
54
69
  },
55
70
  ];
@@ -111,25 +126,37 @@ const make = Effect.gen(function* () {
111
126
  prefix: `.${pin.file}.`,
112
127
  suffix: ".part",
113
128
  });
114
- const bytes = yield* HttpClient.get(pin.url).pipe(
115
- Effect.flatMap((response) =>
116
- response.status === 200
117
- ? response.arrayBuffer.pipe(Effect.mapError(error))
118
- : Effect.fail(
119
- new FormalError({
120
- message: pin.url + ": HTTP " + response.status,
121
- })
122
- )
123
- ),
124
- Effect.map((buffer) => new Uint8Array(buffer)),
125
- Effect.timeout("120 seconds"),
126
- Effect.provide(FetchHttpClient.layer)
127
- );
129
+ const { url, bytes } = yield* Effect.gen(function* () {
130
+ const failures: string[] = [];
131
+ for (const url of pin.urls) {
132
+ const result = yield* HttpClient.get(url, {
133
+ headers: { Accept: "application/octet-stream" },
134
+ }).pipe(
135
+ Effect.flatMap((response) =>
136
+ response.status === 200
137
+ ? response.arrayBuffer.pipe(Effect.mapError(error))
138
+ : Effect.fail(
139
+ new FormalError({
140
+ message: `HTTP ${response.status}`,
141
+ })
142
+ )
143
+ ),
144
+ Effect.map((buffer) => new Uint8Array(buffer)),
145
+ Effect.timeout("120 seconds"),
146
+ Effect.provide(FetchHttpClient.layer),
147
+ Effect.result
148
+ );
149
+ if (Result.isSuccess(result))
150
+ return { url, bytes: result.success };
151
+ failures.push(`${url}: ${error(result.failure).message}`);
152
+ }
153
+ return yield* new FormalError({ message: failures.join("; ") });
154
+ });
128
155
  yield* fs.writeFile(temp, bytes);
129
156
  const actual = digest(bytes);
130
157
  if (actual !== pin.sha256)
131
158
  return yield* new FormalError({
132
- message: `${pin.url} has sha256 ${actual}, expected ${pin.sha256}`,
159
+ message: `${url} has sha256 ${actual}, expected ${pin.sha256}`,
133
160
  });
134
161
  return yield* fs.rename(temp, file);
135
162
  })
@@ -252,7 +279,21 @@ const make = Effect.gen(function* () {
252
279
  "tlc2.TLC",
253
280
  "-tool",
254
281
  "-workers",
255
- "auto",
282
+ input.mode._tag === "Simulate" ? "1" : "auto",
283
+ ...(input.mode._tag === "Simulate"
284
+ ? [
285
+ "-simulate",
286
+ `num=${input.mode.num}`,
287
+ "-depth",
288
+ String(input.mode.depth),
289
+ "-seed",
290
+ String(input.mode.seed),
291
+ ]
292
+ : [
293
+ "-lncheck",
294
+ "final",
295
+ ...(input.mode.coverage ? ["-coverage", "60"] : []),
296
+ ]),
256
297
  "-metadir",
257
298
  temp,
258
299
  "-noGenerateSpecTE",
@@ -0,0 +1,35 @@
1
+ import * as Schema from "effect/Schema";
2
+ export const FORMAL_TRACES = "FORMAL_TRACES";
3
+ const ObjectValue = Schema.Record(Schema.String, Schema.Json);
4
+ export const Trace = Schema.Struct({
5
+ index: Schema.Int.check(Schema.isGreaterThanOrEqualTo(0)),
6
+ constants: ObjectValue,
7
+ steps: Schema.NonEmptyArray(
8
+ Schema.Struct({
9
+ action: Schema.NonEmptyString,
10
+ args: ObjectValue,
11
+ state: ObjectValue,
12
+ })
13
+ ),
14
+ });
15
+ export type Trace = typeof Trace.Type;
16
+ export const receiptPath = (file: string) => `${file}.replayed`;
17
+ export const readTraces = (text: string): ReadonlyArray<Trace> =>
18
+ text
19
+ .trimEnd()
20
+ .split("\n")
21
+ .map((line) =>
22
+ Schema.decodeSync(Schema.fromJsonString(Trace))(line, {
23
+ onExcessProperty: "error",
24
+ })
25
+ );
26
+ export const sorted = (value: Schema.Json): Schema.Json =>
27
+ Array.isArray(value)
28
+ ? value.map(sorted)
29
+ : value !== null && typeof value === "object"
30
+ ? Object.fromEntries(
31
+ Object.entries(value)
32
+ .toSorted(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))
33
+ .map(([key, v]) => [key, sorted(v)])
34
+ )
35
+ : value;