@milkenjoyer/toolchain 0.11.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 (38) hide show
  1. package/README.md +98 -14
  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 +131 -3
  9. package/dist/formal.mjs +716 -65
  10. package/dist/guard.mjs +2 -2
  11. package/dist/{lint-JSnPsGO1.mjs → lint-IhmtnBbx.mjs} +3 -3
  12. package/dist/lint.mjs +1 -1
  13. package/dist/plugin.mjs +1 -1
  14. package/dist/proof.mjs +1 -1
  15. package/dist/trace-file-KuPRWgwt.mjs +17 -0
  16. package/dist/verdict.d.mts +1 -1
  17. package/dist/verdict.mjs +1 -1
  18. package/dist/{verification-p2rSx5lc.d.mts → verification-FgQFU1YN.d.mts} +1 -1
  19. package/dist/verification.d.mts +1 -1
  20. package/dist/verification.mjs +1 -1
  21. package/package.json +6 -2
  22. package/src/formal/cfg.ts +58 -1
  23. package/src/formal/checks.ts +39 -52
  24. package/src/formal/coverage.ts +79 -0
  25. package/src/formal/gate.ts +74 -0
  26. package/src/formal/git.ts +38 -0
  27. package/src/formal/proofs.ts +217 -0
  28. package/src/formal/semantics.ts +110 -0
  29. package/src/formal/specs.ts +80 -34
  30. package/src/formal/tla.ts +24 -2
  31. package/src/formal/trace-file.ts +35 -0
  32. package/src/formal/traces.ts +293 -0
  33. package/src/formal/violations.ts +76 -0
  34. package/src/formal.ts +9 -2
  35. package/dist/exceptions-Bq-9MxoR.mjs +0 -3
  36. package/dist/{effect-plugin-kYxB-5LJ.mjs → effect-plugin-BXUx9PPu.mjs} +1 -1
  37. package/dist/{plugin-BoBRGtGf.mjs → plugin-CwxhPtui.mjs} +1 -1
  38. 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";
@@ -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.12.0",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/birbprophet/agent-kit.git",
@@ -88,7 +88,11 @@
88
88
  "./renovate.json": "./renovate.json",
89
89
  "./tsconfig.base.json": "./tsconfig.base.json",
90
90
  "./vscode/settings.json": "./vscode/settings.json",
91
- "./vscode/extensions.json": "./vscode/extensions.json"
91
+ "./vscode/extensions.json": "./vscode/extensions.json",
92
+ "./conformance": {
93
+ "types": "./dist/conformance.d.mts",
94
+ "default": "./dist/conformance.mjs"
95
+ }
92
96
  },
93
97
  "publishConfig": {
94
98
  "access": "public"
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,29 @@
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
 
5
6
  import { integer } from "./cfg.ts";
7
+ import { Gate } from "./gate.ts";
8
+ import {
9
+ critical,
10
+ fairness,
11
+ coverageCheck,
12
+ witnesses,
13
+ breaks,
14
+ } from "./proofs.ts";
6
15
  import type { Repo } from "./specs.ts";
7
16
  import { FormalError, Tla } from "./tla.ts";
17
+ import { traces } from "./traces.ts";
8
18
 
9
19
  type Problem = { readonly file: string; readonly message: string };
10
- type Check = (
20
+ export type Check = (
11
21
  repo: Repo
12
- ) => Effect.Effect<ReadonlyArray<Problem>, FormalError, Tla | FileSystem>;
22
+ ) => Effect.Effect<
23
+ ReadonlyArray<Problem>,
24
+ FormalError,
25
+ Tla | Gate | FileSystem | ChildProcessSpawner.ChildProcessSpawner
26
+ >;
13
27
  export type Finding = Problem & { readonly check: keyof typeof checks };
14
28
 
15
29
  const configs = (repo: Repo) =>
@@ -24,20 +38,25 @@ const specs: Check = (repo) =>
24
38
  ...(!s.entry && repo.decodeErrors.length === 0
25
39
  ? [{ file: s.path, message: "tracked spec has no specs.json entry" }]
26
40
  : []),
27
- ...(s.entry?.mode === "gate" &&
41
+ ...(s.entry &&
42
+ s.entry.mode !== "library" &&
28
43
  !s.configs.some((c) => c.path === s.path.replace(/\.tla$/u, ".cfg"))
29
- ? [{ file: s.path, message: "gate spec has no matching .cfg" }]
44
+ ? [{ file: s.path, message: "runnable spec has no matching .cfg" }]
45
+ : []),
46
+ ...(s.entry?.mode === "library" && s.configs.length
47
+ ? [{ file: s.path, message: "library spec must not have a config" }]
30
48
  : []),
31
49
  ...s.configs.flatMap((c) =>
32
50
  "_tag" in c.cfg ? [{ file: c.path, message: c.cfg.message }] : []
33
51
  ),
34
52
  ]),
35
- ...Object.keys(repo.file)
36
- .filter((f) => f.endsWith(".tla") && !repo.tracked.includes(f))
53
+ ...Object.keys({ ...repo.file.specs, ...repo.file.kani })
54
+ .filter((f) => !repo.tracked.includes(f))
37
55
  .map((file) => ({ file, message: "entry names no tracked file" })),
38
56
  ...repo.orphans.map((file) => ({
39
57
  file,
40
- message: "orphan config has no matching module",
58
+ message:
59
+ "config pairs with no module; give a variant its own module that EXTENDS X",
41
60
  })),
42
61
  ]);
43
62
  const sany: Check = (repo) =>
@@ -140,53 +159,15 @@ const format: Check = (repo) =>
140
159
  );
141
160
  const tlc: Check = (repo) =>
142
161
  Effect.gen(function* () {
143
- const tla = yield* Tla;
162
+ const gate = yield* Gate;
144
163
  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
164
+ for (const m of repo.models.filter((m) => m.entry.mode === "gate")) {
165
+ const result = yield* gate.run(m);
166
+ if (result._tag === "Failed")
167
+ problems.push({ file: m.config.path, message: result.message });
168
+ else
188
169
  yield* Console.log(
189
- `tlc: ${config.path}: ${states} distinct states in ${run.seconds} s`
170
+ `tlc: ${m.config.path}: ${result.states} distinct states in ${result.seconds} s`
190
171
  );
191
172
  }
192
173
  return problems;
@@ -194,10 +175,16 @@ const tlc: Check = (repo) =>
194
175
 
195
176
  export const checks = {
196
177
  specs,
178
+ critical,
197
179
  sany,
198
180
  typeok,
199
181
  deadlock,
182
+ fairness,
200
183
  constants,
201
184
  format,
202
185
  tlc,
186
+ coverage: coverageCheck,
187
+ witnesses,
188
+ breaks,
189
+ traces,
203
190
  } 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
+ };
@@ -0,0 +1,74 @@
1
+ import * as Cache from "effect/Cache";
2
+ import * as Context from "effect/Context";
3
+ import * as Effect from "effect/Effect";
4
+ import * as Layer from "effect/Layer";
5
+
6
+ import { coverage } from "./coverage.ts";
7
+ import type { Coverage } from "./coverage.ts";
8
+ import type { Model } from "./specs.ts";
9
+ import { Tla } from "./tla.ts";
10
+ import type { FormalError } from "./tla.ts";
11
+ type GateResult =
12
+ | {
13
+ readonly _tag: "Passed";
14
+ readonly states: string;
15
+ readonly seconds: number;
16
+ readonly coverage: Coverage;
17
+ }
18
+ | { readonly _tag: "Failed"; readonly message: string };
19
+ const make = Effect.gen(function* () {
20
+ const tla = yield* Tla;
21
+ const run = yield* Cache.make({
22
+ capacity: 10000,
23
+ lookup: Effect.fn("formal.gate")(function* (
24
+ m: Model
25
+ ): Effect.fn.Return<GateResult, FormalError> {
26
+ const r = yield* tla.tlc({
27
+ spec: `${m.root}/${m.path}`,
28
+ config: `${m.root}/${m.config.path}`,
29
+ timeoutSeconds: m.entry.timeoutSeconds,
30
+ mode: { _tag: "Check", coverage: true },
31
+ });
32
+ if (r._tag === "TimedOut")
33
+ return {
34
+ _tag: "Failed",
35
+ message: `TLC timed out after ${m.entry.timeoutSeconds} s`,
36
+ };
37
+ const failures = r.messages.filter(
38
+ (x) => x.severity >= 1 && x.severity <= 3
39
+ );
40
+ const states = r.messages
41
+ .find((x) => x.code === 2199)
42
+ ?.text.match(/([\d,]+) distinct states/u)?.[1]
43
+ ?.replaceAll(",", "");
44
+ if (
45
+ r.code !== 0 ||
46
+ failures.length ||
47
+ !r.messages.some((x) => x.code === 2193) ||
48
+ states === undefined ||
49
+ BigInt(states) === 0n
50
+ ) {
51
+ const meaning = new Map([
52
+ [11, "deadlock"],
53
+ [12, "safety violation"],
54
+ [13, "liveness violation"],
55
+ [150, "parse failure"],
56
+ ]);
57
+ return {
58
+ _tag: "Failed",
59
+ 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)"}`,
60
+ };
61
+ }
62
+ const c = coverage(r.messages);
63
+ return typeof c === "string"
64
+ ? { _tag: "Failed", message: c }
65
+ : { _tag: "Passed", states, seconds: r.seconds, coverage: c };
66
+ }),
67
+ });
68
+ return { run: (model: Model) => Cache.get(run, model) };
69
+ });
70
+ export class Gate extends Context.Service<Gate, Effect.Success<typeof make>>()(
71
+ "formal/Gate"
72
+ ) {
73
+ static readonly layer = Layer.effect(Gate, make);
74
+ }
@@ -0,0 +1,38 @@
1
+ import * as Config from "effect/Config";
2
+ import * as Effect from "effect/Effect";
3
+ import { ChildProcess } from "effect/process";
4
+ import * as Stream from "effect/Stream";
5
+ export const changes = Effect.fn("formal.changes")(function* (root: string) {
6
+ const base = yield* Config.String("BASE").pipe(
7
+ Config.withDefault("origin/main")
8
+ );
9
+ const run = (args: ReadonlyArray<string>) =>
10
+ Effect.scoped(
11
+ Effect.gen(function* () {
12
+ const p = yield* ChildProcess.make("git", args, {
13
+ cwd: root,
14
+ stdin: "ignore",
15
+ });
16
+ const [out, , code] = yield* Effect.all(
17
+ [
18
+ p.stdout.pipe(Stream.decodeText, Stream.mkString),
19
+ p.stderr.pipe(Stream.decodeText, Stream.mkString),
20
+ p.exitCode,
21
+ ],
22
+ { concurrency: "unbounded" }
23
+ );
24
+ return { out, code: Number(code) };
25
+ })
26
+ );
27
+ const merge = yield* run(["merge-base", base, "HEAD"]);
28
+ if (merge.code !== 0) return { _tag: "All" as const, base };
29
+ const sha = merge.out.trim();
30
+ const diff = yield* run(["diff", "--name-only", "-z", sha]);
31
+ return diff.code !== 0
32
+ ? { _tag: "All" as const, base }
33
+ : {
34
+ _tag: "Changed" as const,
35
+ sha,
36
+ files: diff.out.split("\0").filter(Boolean),
37
+ };
38
+ });