@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
package/dist/guard.mjs CHANGED
@@ -1,7 +1,7 @@
1
1
  import { d as otherTags, l as latestOf, u as onLatestLine } from "./compatible-D8n_sQHa.mjs";
2
2
  import { i as enforcedTsgoRules } from "./effect-tsgo-D0QfeKGD.mjs";
3
- import { t as exceptions } from "./exceptions-Bq-9MxoR.mjs";
4
- import { a as toolchainScopes, n as effectRules, r as foldkitPreset, t as birbprophetRules } from "./lint-JSnPsGO1.mjs";
3
+ import { t as exceptions } from "./exceptions-Dybl9xpc.mjs";
4
+ import { a as toolchainScopes, n as effectRules, r as foldkitPreset, t as birbprophetRules } from "./lint-IhmtnBbx.mjs";
5
5
  import { c as lead, f as versionOf } from "./versions-C7LKZGr3.mjs";
6
6
  import * as Arr from "effect/Array";
7
7
  import { dual } from "effect/Function";
package/dist/knip.mjs CHANGED
@@ -2,7 +2,8 @@
2
2
  const baseEntry = [
3
3
  "*.run.ts",
4
4
  "scripts/*.ts",
5
- "tests/fixtures/**/*.ts"
5
+ "tests/fixtures/**/*.ts",
6
+ "**/*.proof.ts"
6
7
  ];
7
8
  const toolchainPlugins = ["@effect/language-service", "@foldkit/oxlint-plugin"];
8
9
  const knip = ({ entry = [], ignoreDependencies = [], workspaces = {} } = {}) => ({
@@ -1,7 +1,7 @@
1
- import { t as plugin } from "./effect-plugin-kYxB-5LJ.mjs";
1
+ import { t as plugin } from "./effect-plugin-BXUx9PPu.mjs";
2
2
  import { n as effectTsgoPlugin, r as effectTsgoRules } from "./effect-tsgo-D0QfeKGD.mjs";
3
- import { t as exceptions } from "./exceptions-Bq-9MxoR.mjs";
4
- import { t as plugin$1 } from "./plugin-BoBRGtGf.mjs";
3
+ import { t as exceptions } from "./exceptions-Dybl9xpc.mjs";
4
+ import { t as plugin$1 } from "./plugin-CwxhPtui.mjs";
5
5
  import { t as staticRules } from "./rules-vHmNoAU0.mjs";
6
6
  import * as Arr from "effect/Array";
7
7
  import * as Option from "effect/Option";
package/dist/lint.mjs CHANGED
@@ -1 +1 @@
1
- export { t as birbprophetRules, n as effectRules, r as foldkitPreset, i as lint, a as toolchainScopes } from "./lint-JSnPsGO1.mjs";
1
+ export { t as birbprophetRules, n as effectRules, r as foldkitPreset, i as lint, a as toolchainScopes } from "./lint-IhmtnBbx.mjs";
package/dist/plugin.mjs CHANGED
@@ -1 +1 @@
1
- export { t as default } from "./plugin-BoBRGtGf.mjs";
1
+ export { t as default } from "./plugin-CwxhPtui.mjs";
package/dist/proof.mjs CHANGED
@@ -1,6 +1,7 @@
1
1
  #!/usr/bin/env bun
2
2
  import * as Data from "effect/Data";
3
3
  import * as Schema from "effect/Schema";
4
+ import * as Config from "effect/Config";
4
5
  import * as Effect from "effect/Effect";
5
6
  import { NodeServices } from "@effect/platform-node";
6
7
  import * as Cause from "effect/Cause";
@@ -8,7 +9,6 @@ import * as Exit from "effect/Exit";
8
9
  import { ChildProcess } from "effect/process";
9
10
  import * as Stream from "effect/Stream";
10
11
  import { FileSystem } from "effect/FileSystem";
11
- import * as Config from "effect/Config";
12
12
  import { Path } from "effect/Path";
13
13
  import { Octokit } from "@octokit/rest";
14
14
  import { Base64 } from "effect/encoding";
@@ -0,0 +1,17 @@
1
+ import * as Schema from "effect/Schema";
2
+ // src/formal/trace-file.ts
3
+ const FORMAL_TRACES = "FORMAL_TRACES";
4
+ const ObjectValue = Schema.Record(Schema.String, Schema.Json);
5
+ const Trace = Schema.Struct({
6
+ index: Schema.Int.check(Schema.isGreaterThanOrEqualTo(0)),
7
+ constants: ObjectValue,
8
+ steps: Schema.NonEmptyArray(Schema.Struct({
9
+ action: Schema.NonEmptyString,
10
+ args: ObjectValue,
11
+ state: ObjectValue
12
+ }))
13
+ });
14
+ const receiptPath = (file) => `${file}.replayed`;
15
+ const readTraces = (text) => text.trimEnd().split("\n").map((line) => Schema.decodeSync(Schema.fromJsonString(Trace))(line, { onExcessProperty: "error" }));
16
+ const sorted = (value) => Array.isArray(value) ? value.map(sorted) : value !== null && typeof value === "object" ? Object.fromEntries(Object.entries(value).toSorted(([a], [b]) => a < b ? -1 : a > b ? 1 : 0).map(([key, v]) => [key, sorted(v)])) : value;
17
+ export { sorted as i, readTraces as n, receiptPath as r, FORMAL_TRACES as t };
@@ -1,3 +1,3 @@
1
- import { i as VerificationError } from "./verification-p2rSx5lc.mjs";
1
+ import { i as VerificationError } from "./verification-FgQFU1YN.mjs";
2
2
  import * as Effect from "effect/Effect";
3
3
  export declare const checkVerdict: Effect.Effect<number, import("effect/Config").ConfigError | import("effect/PlatformError").PlatformError | import("effect/Schema").SchemaError | VerificationError, import("effect/FileSystem").FileSystem>;
package/dist/verdict.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env bun
2
- import { a as readPullRequest, i as github, n as autoPass } from "./verification-6s2chr4X.mjs";
2
+ import { a as readPullRequest, i as github, n as autoPass } from "./verification-oAc1Q6I9.mjs";
3
3
  import * as Effect from "effect/Effect";
4
4
  import { NodeServices } from "@effect/platform-node";
5
5
  import * as Cause from "effect/Cause";
@@ -1,8 +1,8 @@
1
1
  import * as Schema from "effect/Schema";
2
+ import * as Config from "effect/Config";
2
3
  import * as Effect from "effect/Effect";
3
4
  import * as Cause from "effect/Cause";
4
5
  import { FileSystem } from "effect/FileSystem";
5
- import * as Config from "effect/Config";
6
6
  import { Octokit } from "@octokit/rest";
7
7
  type PullRequest = {
8
8
  readonly author: string;
@@ -1,2 +1,2 @@
1
- import { a as autoPass, c as readPullRequest, i as VerificationError, l as verdict, n as PullRequestContext, o as checkVerification, r as Verdict, s as github, t as PullRequest } from "./verification-p2rSx5lc.mjs";
1
+ import { a as autoPass, c as readPullRequest, i as VerificationError, l as verdict, n as PullRequestContext, o as checkVerification, r as Verdict, s as github, t as PullRequest } from "./verification-FgQFU1YN.mjs";
2
2
  export { PullRequest, PullRequestContext, Verdict, VerificationError, autoPass, checkVerification, github, readPullRequest, verdict };
@@ -1,2 +1,2 @@
1
1
  #!/usr/bin/env bun
2
- export { t as VerificationError, n as autoPass, r as checkVerification, i as github, a as readPullRequest, o as verdict } from "./verification-6s2chr4X.mjs";
2
+ export { t as VerificationError, n as autoPass, r as checkVerification, i as github, a as readPullRequest, o as verdict } from "./verification-oAc1Q6I9.mjs";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@milkenjoyer/toolchain",
3
- "version": "0.11.0",
3
+ "version": "0.13.0",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/birbprophet/agent-kit.git",
@@ -28,6 +28,10 @@
28
28
  ],
29
29
  "type": "module",
30
30
  "exports": {
31
+ "./arithmetic": {
32
+ "types": "./dist/arithmetic.d.mts",
33
+ "default": "./dist/arithmetic.mjs"
34
+ },
31
35
  ".": {
32
36
  "types": "./dist/index.d.mts",
33
37
  "default": "./dist/index.mjs"
@@ -88,7 +92,11 @@
88
92
  "./renovate.json": "./renovate.json",
89
93
  "./tsconfig.base.json": "./tsconfig.base.json",
90
94
  "./vscode/settings.json": "./vscode/settings.json",
91
- "./vscode/extensions.json": "./vscode/extensions.json"
95
+ "./vscode/extensions.json": "./vscode/extensions.json",
96
+ "./conformance": {
97
+ "types": "./dist/conformance.d.mts",
98
+ "default": "./dist/conformance.mjs"
99
+ }
92
100
  },
93
101
  "publishConfig": {
94
102
  "access": "public"
@@ -104,7 +112,8 @@
104
112
  "effect": "4.1.2",
105
113
  "effect-oxlint": "0.4.0",
106
114
  "semver": "7.8.6",
107
- "ultracite": "8.1.1"
115
+ "ultracite": "8.1.1",
116
+ "z3-solver": "5.3.3"
108
117
  },
109
118
  "devDependencies": {
110
119
  "@birbprophet/test-kit": "0.0.0",
@@ -0,0 +1,190 @@
1
+ import { join } from "node:path";
2
+ import { fileURLToPath } from "node:url";
3
+
4
+ import * as Console from "effect/Console";
5
+ import * as Effect from "effect/Effect";
6
+ import { FileSystem } from "effect/FileSystem";
7
+ import * as Option from "effect/Option";
8
+ import { ChildProcess } from "effect/process";
9
+ import * as Result from "effect/Result";
10
+ import * as Schema from "effect/Schema";
11
+ import * as Stream from "effect/Stream";
12
+
13
+ import type { Check } from "./checks.ts";
14
+ import { describe, Result as ProofResult } from "./proof-run.ts";
15
+ import { Request } from "./proof-run.ts";
16
+ import { FormalError } from "./tla.ts";
17
+
18
+ const matches = (glob: string, file: string) => new Bun.Glob(glob).match(file);
19
+ const moduleFile = /\.(?:[cm]?[jt]s|[jt]sx)$/u;
20
+ export const arithmetic: Check = (repo) =>
21
+ Effect.gen(function* () {
22
+ if (repo.file.arithmetic === undefined) {
23
+ yield* Console.log(
24
+ "arithmetic: specs.json lists no arithmetic globs; rule 16 is off"
25
+ );
26
+ return [];
27
+ }
28
+ return repo.file.arithmetic.flatMap((glob) => {
29
+ const files = repo.tracked.filter((f) => matches(glob, f));
30
+ return files.length === 0
31
+ ? [
32
+ {
33
+ file: "specs.json",
34
+ message: `arithmetic glob ${glob} matches no tracked file`,
35
+ },
36
+ ]
37
+ : files
38
+ .filter(
39
+ (f) =>
40
+ !Object.values({ ...repo.file.proofs, ...repo.file.kani }).some(
41
+ (e) => e.covers.some((g) => matches(g, f))
42
+ )
43
+ )
44
+ .map((file) => ({
45
+ file,
46
+ message: `matches arithmetic glob ${glob} but no proof covers it`,
47
+ }));
48
+ });
49
+ });
50
+ const run = Effect.fn("formal.proof.run")(function* (request: Request) {
51
+ const fs = yield* FileSystem;
52
+ const child = yield* Effect.scoped(
53
+ Effect.gen(function* () {
54
+ const p = yield* ChildProcess.make(
55
+ "bun",
56
+ [fileURLToPath(new URL("./prover.ts", import.meta.url))],
57
+ {
58
+ cwd: request.root,
59
+ env: {
60
+ FORMAL_PROOF: yield* Schema.encodeEffect(
61
+ Schema.fromJsonString(Request)
62
+ )(request),
63
+ },
64
+ extendEnv: true,
65
+ stdin: "ignore",
66
+ killSignal: "SIGKILL",
67
+ }
68
+ );
69
+ return yield* Effect.all(
70
+ [
71
+ p.stdout.pipe(Stream.decodeText, Stream.mkString),
72
+ p.stderr.pipe(Stream.decodeText, Stream.mkString),
73
+ p.exitCode,
74
+ ],
75
+ { concurrency: "unbounded" }
76
+ );
77
+ }).pipe(Effect.timeoutOption(`${request.timeoutSeconds + 30} seconds`))
78
+ );
79
+ if (Option.isNone(child))
80
+ return `timed out after ${request.timeoutSeconds + 30} s`;
81
+ if (!(yield* fs.exists(request.result)))
82
+ return `the proof exited ${child.value[2]} without a result: ${child.value[1].trim().split("\n").at(-1) ?? ""}`;
83
+ const result = Schema.decodeResult(Schema.fromJsonString(ProofResult))(
84
+ yield* fs.readFileString(request.result),
85
+ { onExcessProperty: "error" }
86
+ );
87
+ return Result.isSuccess(result)
88
+ ? result.success
89
+ : "the proof wrote a malformed result";
90
+ });
91
+ export const proofs: Check = (repo) =>
92
+ Effect.scoped(
93
+ Effect.gen(function* () {
94
+ const fs = yield* FileSystem;
95
+ const dir = yield* fs.makeTempDirectoryScoped({
96
+ prefix: "formal-proofs-",
97
+ });
98
+ const problems: { file: string; message: string }[] = [];
99
+ let index = 0;
100
+ for (const [file, entry] of Object.entries(repo.file.proofs).toSorted(
101
+ ([a], [b]) => a.localeCompare(b)
102
+ )) {
103
+ if (!repo.tracked.includes(file)) continue;
104
+ const add = (message: string) => problems.push({ file, message });
105
+ const covered = repo.tracked.filter((f) =>
106
+ entry.covers.some((g) => matches(g, f))
107
+ );
108
+ const before = problems.length;
109
+ for (const [i, edit] of entry.breaks.entries()) {
110
+ if (!covered.includes(edit.file)) {
111
+ add(`break ${i + 1}: ${edit.file} is not in this proof's covers`);
112
+ continue;
113
+ }
114
+ const count =
115
+ (yield* fs.readFileString(join(repo.root, edit.file))).split(
116
+ edit.find
117
+ ).length - 1;
118
+ if (count !== 1)
119
+ add(`break ${i + 1} find matches ${count} times in ${edit.file}`);
120
+ }
121
+ if (problems.length !== before) continue;
122
+ const covers = yield* Effect.forEach(
123
+ covered,
124
+ (f) => fs.realPath(join(repo.root, f)),
125
+ { concurrency: 1 }
126
+ );
127
+ const request = {
128
+ root: repo.root,
129
+ proof: join(repo.root, file),
130
+ kind: entry.kind,
131
+ covers,
132
+ samples: entry.kind === "z3" ? entry.samples : 0,
133
+ timeoutSeconds: entry.timeoutSeconds,
134
+ result: join(dir, `${index++}.json`),
135
+ } satisfies Request;
136
+ const baseline = yield* run(request);
137
+ if (typeof baseline === "string") {
138
+ add(baseline);
139
+ continue;
140
+ }
141
+ if (baseline.outcome._tag !== "Proved") {
142
+ add(describe(baseline.outcome));
143
+ continue;
144
+ }
145
+ covered.forEach((f, i) => {
146
+ if (moduleFile.test(f) && !baseline.loaded.includes(covers[i] ?? ""))
147
+ add(
148
+ `covers ${f} but never loads it; a proof covers only code it runs`
149
+ );
150
+ });
151
+ if (problems.length !== before) continue;
152
+ yield* Console.log(
153
+ `proofs: ${file}: ${baseline.outcome.detail} in ${baseline.outcome.seconds.toFixed(2)} s`
154
+ );
155
+ for (const [i, edit] of entry.breaks.entries()) {
156
+ const absolute = yield* fs.realPath(join(repo.root, edit.file));
157
+ const broken = yield* run({
158
+ ...request,
159
+ result: join(dir, `${index++}.json`),
160
+ edit: { ...edit, file: absolute },
161
+ });
162
+ if (typeof broken === "string")
163
+ add(
164
+ `break ${i + 1} (${edit.file}): ${broken}; a break counts only as a counterexample, a mismatch or a throw`
165
+ );
166
+ else if (!broken.loaded.includes(absolute))
167
+ add(`break ${i + 1}: the proof never loads ${edit.file}`);
168
+ else if (broken.outcome._tag === "Proved")
169
+ add(
170
+ `break ${i + 1} (${edit.file}): the proof still holds after the edit`
171
+ );
172
+ else if (
173
+ ["Counterexample", "Disagrees", "Throws"].includes(
174
+ broken.outcome._tag
175
+ )
176
+ )
177
+ yield* Console.log(
178
+ `proofs: ${file}: break ${i + 1} ${broken.outcome._tag}`
179
+ );
180
+ else
181
+ add(
182
+ `break ${i + 1} (${edit.file}): ${describe(broken.outcome)}; a break counts only as a counterexample, a mismatch or a throw`
183
+ );
184
+ }
185
+ }
186
+ return problems;
187
+ })
188
+ ).pipe(
189
+ Effect.mapError((cause) => new FormalError({ message: String(cause) }))
190
+ );
package/src/formal/cfg.ts CHANGED
@@ -7,6 +7,11 @@ export type CfgConstant =
7
7
  };
8
8
  export type Cfg = {
9
9
  readonly constants: ReadonlyArray<CfgConstant>;
10
+ readonly sections: ReadonlyArray<{
11
+ readonly keyword: string;
12
+ readonly text: string;
13
+ }>;
14
+ readonly properties: ReadonlyArray<string>;
10
15
  readonly invariants: ReadonlyArray<string>;
11
16
  readonly checkDeadlock: ReadonlyArray<boolean>;
12
17
  };
@@ -37,6 +42,8 @@ export const parseCfg = (source: string): Cfg | CfgError => {
37
42
  const tokens =
38
43
  text.match(/"(?:\\.|[^"\\])*"|<-|[A-Za-z_][A-Za-z0-9_!]*|-?\d+|[^\s]/gu) ??
39
44
  [];
45
+ const sections: { keyword: string; text: string }[] = [];
46
+ const properties: string[] = [];
40
47
  const constants: CfgConstant[] = [];
41
48
  const invariants: string[] = [];
42
49
  const checkDeadlock: boolean[] = [];
@@ -49,6 +56,7 @@ export const parseCfg = (source: string): Cfg | CfgError => {
49
56
  const operands: string[] = [];
50
57
  while (i < tokens.length && !keywords.has(tokens[i] ?? ""))
51
58
  operands.push(tokens[i++] ?? "");
59
+ sections.push({ keyword, text: operands.join(" ") });
52
60
  if (operands.length === 0) return fail(`missing operand for ${keyword}`);
53
61
  if (keyword === "CONSTANT" || keyword === "CONSTANTS") {
54
62
  let j = 0;
@@ -100,12 +108,61 @@ export const parseCfg = (source: string): Cfg | CfgError => {
100
108
  } else {
101
109
  if (!operands.every((token) => /^[A-Za-z_][A-Za-z0-9_!]*$/u.test(token)))
102
110
  return fail(`invalid operands for ${keyword}`);
111
+ if (keyword.startsWith("PROPERT")) properties.push(...operands);
103
112
  if (keyword.startsWith("INVARIANT")) invariants.push(...operands);
104
113
  }
105
114
  }
106
- return { constants, invariants, checkDeadlock };
115
+ return { sections, constants, invariants, properties, checkDeadlock };
107
116
  };
108
117
  export const integer = (constant: CfgConstant): bigint | undefined =>
109
118
  constant._tag === "Value" && /^-?\d+$/u.test(constant.value)
110
119
  ? BigInt(constant.value)
111
120
  : undefined;
121
+
122
+ export type Plan = {
123
+ readonly target?: { readonly name: string; readonly level: number };
124
+ readonly simulation?: {
125
+ readonly init: string;
126
+ readonly next: string;
127
+ readonly overrides: ReadonlyArray<string>;
128
+ };
129
+ };
130
+ export const derive = (cfg: Cfg, plan: Plan): string => {
131
+ const removed = new Set([
132
+ "INVARIANT",
133
+ "INVARIANTS",
134
+ "PROPERTY",
135
+ "PROPERTIES",
136
+ ]);
137
+ if (plan.simulation)
138
+ for (const k of [
139
+ "INIT",
140
+ "NEXT",
141
+ "SPECIFICATION",
142
+ "SYMMETRY",
143
+ "VIEW",
144
+ "ALIAS",
145
+ "POSTCONDITION",
146
+ "CHECK_DEADLOCK",
147
+ ])
148
+ removed.add(k);
149
+ const lines = cfg.sections
150
+ .filter((s) => !removed.has(s.keyword))
151
+ .map((s) => s.keyword + " " + s.text);
152
+ if (plan.target)
153
+ lines.push(
154
+ (plan.target.level < 2 ? "INVARIANT " : "PROPERTY ") + plan.target.name
155
+ );
156
+ if (plan.simulation) {
157
+ const s = plan.simulation;
158
+ lines.push(
159
+ "CONSTANTS",
160
+ ...s.overrides,
161
+ "INIT " + s.init,
162
+ "NEXT " + s.next,
163
+ "INVARIANT Formal_Export",
164
+ "CHECK_DEADLOCK FALSE"
165
+ );
166
+ }
167
+ return lines.join("\n") + "\n";
168
+ };
@@ -1,15 +1,30 @@
1
1
  import * as Console from "effect/Console";
2
2
  import * as Effect from "effect/Effect";
3
3
  import { FileSystem } from "effect/FileSystem";
4
+ import { ChildProcessSpawner } from "effect/process";
4
5
 
6
+ import { arithmetic, proofs } from "./arithmetic.ts";
5
7
  import { integer } from "./cfg.ts";
8
+ import { Gate } from "./gate.ts";
9
+ import {
10
+ critical,
11
+ fairness,
12
+ coverageCheck,
13
+ witnesses,
14
+ breaks,
15
+ } from "./proofs.ts";
6
16
  import type { Repo } from "./specs.ts";
7
17
  import { FormalError, Tla } from "./tla.ts";
18
+ import { traces } from "./traces.ts";
8
19
 
9
20
  type Problem = { readonly file: string; readonly message: string };
10
- type Check = (
21
+ export type Check = (
11
22
  repo: Repo
12
- ) => Effect.Effect<ReadonlyArray<Problem>, FormalError, Tla | FileSystem>;
23
+ ) => Effect.Effect<
24
+ ReadonlyArray<Problem>,
25
+ FormalError,
26
+ Tla | Gate | FileSystem | ChildProcessSpawner.ChildProcessSpawner
27
+ >;
13
28
  export type Finding = Problem & { readonly check: keyof typeof checks };
14
29
 
15
30
  const configs = (repo: Repo) =>
@@ -24,20 +39,40 @@ const specs: Check = (repo) =>
24
39
  ...(!s.entry && repo.decodeErrors.length === 0
25
40
  ? [{ file: s.path, message: "tracked spec has no specs.json entry" }]
26
41
  : []),
27
- ...(s.entry?.mode === "gate" &&
42
+ ...(s.entry &&
43
+ s.entry.mode !== "library" &&
28
44
  !s.configs.some((c) => c.path === s.path.replace(/\.tla$/u, ".cfg"))
29
- ? [{ file: s.path, message: "gate spec has no matching .cfg" }]
45
+ ? [{ file: s.path, message: "runnable spec has no matching .cfg" }]
46
+ : []),
47
+ ...(s.entry?.mode === "library" && s.configs.length
48
+ ? [{ file: s.path, message: "library spec must not have a config" }]
30
49
  : []),
31
50
  ...s.configs.flatMap((c) =>
32
51
  "_tag" in c.cfg ? [{ file: c.path, message: c.cfg.message }] : []
33
52
  ),
34
53
  ]),
35
- ...Object.keys(repo.file)
36
- .filter((f) => f.endsWith(".tla") && !repo.tracked.includes(f))
54
+ ...repo.tracked
55
+ .filter(
56
+ (f) =>
57
+ f.endsWith(".proof.ts") &&
58
+ !repo.file.proofs[f] &&
59
+ repo.decodeErrors.length === 0
60
+ )
61
+ .map((file) => ({
62
+ file,
63
+ message: "tracked proof has no specs.json entry",
64
+ })),
65
+ ...Object.keys({
66
+ ...repo.file.specs,
67
+ ...repo.file.kani,
68
+ ...repo.file.proofs,
69
+ })
70
+ .filter((f) => !repo.tracked.includes(f))
37
71
  .map((file) => ({ file, message: "entry names no tracked file" })),
38
72
  ...repo.orphans.map((file) => ({
39
73
  file,
40
- message: "orphan config has no matching module",
74
+ message:
75
+ "config pairs with no module; give a variant its own module that EXTENDS X",
41
76
  })),
42
77
  ]);
43
78
  const sany: Check = (repo) =>
@@ -140,53 +175,15 @@ const format: Check = (repo) =>
140
175
  );
141
176
  const tlc: Check = (repo) =>
142
177
  Effect.gen(function* () {
143
- const tla = yield* Tla;
178
+ const gate = yield* Gate;
144
179
  const problems: Problem[] = [];
145
- for (const s of repo.specs) {
146
- if (s.analysis._tag !== "Parsed" || s.entry?.mode !== "gate") continue;
147
- const config = s.configs.find(
148
- (c) => c.path === s.path.replace(/\.tla$/u, ".cfg")
149
- );
150
- if (!config || "_tag" in config.cfg) continue;
151
- const run = yield* tla.tlc({
152
- spec: `${repo.root}/${s.path}`,
153
- config: `${repo.root}/${config.path}`,
154
- timeoutSeconds: s.entry.timeoutSeconds,
155
- });
156
- if (run._tag === "TimedOut") {
157
- problems.push({
158
- file: config.path,
159
- message: `TLC timed out after ${s.entry.timeoutSeconds} s`,
160
- });
161
- continue;
162
- }
163
- const failures = run.messages.filter(
164
- (m) => m.severity >= 1 && m.severity <= 3
165
- );
166
- const states = run.messages
167
- .find((m) => m.code === 2199)
168
- ?.text.match(/([\d,]+) distinct states/u)?.[1]
169
- ?.replaceAll(",", "");
170
- if (
171
- failures.length ||
172
- run.code !== 0 ||
173
- !run.messages.some((m) => m.code === 2193) ||
174
- states === undefined ||
175
- BigInt(states) === 0n
176
- ) {
177
- const meaning = new Map([
178
- [11, "deadlock"],
179
- [12, "safety violation"],
180
- [13, "liveness violation"],
181
- [150, "parse failure"],
182
- ]);
183
- problems.push({
184
- file: config.path,
185
- message: `TLC exited ${run.code}${meaning.has(run.code) ? ` (${meaning.get(run.code)})` : ""}; ${failures.length ? failures.map((m) => `${m.severity === 3 ? "warning" : "error"} ${m.code}: ${m.text}`).join("; ") : "requires 2193 success and nonzero distinct states (2199)"}`,
186
- });
187
- } else
180
+ for (const m of repo.models.filter((m) => m.entry.mode === "gate")) {
181
+ const result = yield* gate.run(m);
182
+ if (result._tag === "Failed")
183
+ problems.push({ file: m.config.path, message: result.message });
184
+ else
188
185
  yield* Console.log(
189
- `tlc: ${config.path}: ${states} distinct states in ${run.seconds} s`
186
+ `tlc: ${m.config.path}: ${result.states} distinct states in ${result.seconds} s`
190
187
  );
191
188
  }
192
189
  return problems;
@@ -194,10 +191,18 @@ const tlc: Check = (repo) =>
194
191
 
195
192
  export const checks = {
196
193
  specs,
194
+ critical,
195
+ arithmetic,
197
196
  sany,
198
197
  typeok,
199
198
  deadlock,
199
+ fairness,
200
200
  constants,
201
201
  format,
202
202
  tlc,
203
+ coverage: coverageCheck,
204
+ witnesses,
205
+ breaks,
206
+ traces,
207
+ proofs,
203
208
  } satisfies Record<string, Check>;
@@ -0,0 +1,79 @@
1
+ import type { ToolMessage } from "./tla.ts";
2
+
3
+ type Span = {
4
+ readonly module: string;
5
+ readonly line: number;
6
+ readonly col: number;
7
+ readonly endLine: number;
8
+ readonly endCol: number;
9
+ };
10
+ export type Coverage = {
11
+ readonly actions: ReadonlyArray<{
12
+ readonly name: string;
13
+ readonly span: Span;
14
+ readonly generated: bigint;
15
+ }>;
16
+ readonly zeros: ReadonlyArray<{
17
+ readonly scope: string;
18
+ readonly span: Span;
19
+ }>;
20
+ };
21
+ const span = (text: string): Span | undefined => {
22
+ const m =
23
+ /line (\d+), col (\d+) to line (\d+), col (\d+) of module (\w+)/u.exec(
24
+ text
25
+ );
26
+ return m
27
+ ? {
28
+ line: Number(m[1]),
29
+ col: Number(m[2]),
30
+ endLine: Number(m[3]),
31
+ endCol: Number(m[4]),
32
+ module: m[5] ?? "",
33
+ }
34
+ : undefined;
35
+ };
36
+ export const coverage = (
37
+ messages: ReadonlyArray<ToolMessage>
38
+ ): Coverage | string => {
39
+ const start = messages.findLastIndex((m) => m.code === 2201);
40
+ const end = messages.findIndex(
41
+ (m, i) => i > start && (m.code === 2202 || m.code === 2777)
42
+ );
43
+ if (start < 0 || end < 0) return "missing final coverage report";
44
+ const actions: { name: string; span: Span; generated: bigint }[] = [];
45
+ const zeros: { scope: string; span: Span }[] = [];
46
+ let scope: { name: string; enabled: boolean } | undefined;
47
+ let zeroDepth: number | undefined;
48
+ for (const m of messages.slice(start + 1, end)) {
49
+ if (m.code === 2779) continue;
50
+ if (m.code === 2772 || m.code === 2773) {
51
+ const loc = span(m.text);
52
+ const match = /^<(\w+) .*: ([\d,]+):([\d,]+)$/u.exec(m.text);
53
+ if (!loc || !match) return `unparseable coverage ${m.code}: ${m.text}`;
54
+ const name = match[1] ?? "";
55
+ const generated = BigInt((match[3] ?? "0").replaceAll(",", ""));
56
+ scope = { name, enabled: generated > 0n };
57
+ zeroDepth = undefined;
58
+ if (m.code === 2772) actions.push({ name, span: loc, generated });
59
+ } else if (m.code === 2221 || m.code === 2775) {
60
+ const loc = span(m.text);
61
+ const count = /: ([\d,]+)(?::[\d,]+)?$/u.exec(m.text);
62
+ if (!loc || !count) return `unparseable coverage ${m.code}: ${m.text}`;
63
+ const depth = m.text.indexOf("line");
64
+ if (zeroDepth !== undefined && depth <= zeroDepth) zeroDepth = undefined;
65
+ if (
66
+ scope?.enabled === true &&
67
+ BigInt((count[1] ?? "0").replaceAll(",", "")) === 0n &&
68
+ zeroDepth === undefined
69
+ ) {
70
+ zeros.push({ scope: scope.name, span: loc });
71
+ zeroDepth = depth;
72
+ }
73
+ } else if (m.code === 2774 || m.code === 2778) {
74
+ scope = undefined;
75
+ zeroDepth = undefined;
76
+ } else return `unparseable coverage ${m.code}: ${m.text}`;
77
+ }
78
+ return { actions, zeros };
79
+ };