@milkenjoyer/toolchain 0.10.0 → 0.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +33 -0
- package/dist/formal.d.mts +60 -0
- package/dist/formal.mjs +628 -0
- package/dist/hooks.mjs +2 -2
- package/dist/proof.mjs +6 -6
- package/dist/verdict.d.mts +1 -1
- package/dist/verdict.mjs +2 -2
- package/dist/{verification-MNG9WKEH.d.mts → verification-p2rSx5lc.d.mts} +2 -2
- package/dist/verification.d.mts +1 -1
- package/dist/verification.mjs +1 -1
- package/package.json +4 -1
- package/src/formal/cfg.ts +111 -0
- package/src/formal/checks.ts +203 -0
- package/src/formal/semantics.ts +145 -0
- package/src/formal/specs.ts +166 -0
- package/src/formal/tla.ts +284 -0
- package/src/formal.ts +75 -0
- package/dist/{verification-2bhNnJr-.mjs → verification-6s2chr4X.mjs} +4 -4
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import * as Schema from "effect/Schema";
|
|
2
2
|
import * as Effect from "effect/Effect";
|
|
3
|
-
import { FileSystem } from "effect/FileSystem";
|
|
4
|
-
import { Octokit } from "@octokit/rest";
|
|
5
3
|
import * as Cause from "effect/Cause";
|
|
4
|
+
import { FileSystem } from "effect/FileSystem";
|
|
6
5
|
import * as Config from "effect/Config";
|
|
6
|
+
import { Octokit } from "@octokit/rest";
|
|
7
7
|
type PullRequest = {
|
|
8
8
|
readonly author: string;
|
|
9
9
|
readonly body: string | null;
|
package/dist/verification.d.mts
CHANGED
|
@@ -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-
|
|
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";
|
|
2
2
|
export { PullRequest, PullRequestContext, Verdict, VerificationError, autoPass, checkVerification, github, readPullRequest, verdict };
|
package/dist/verification.mjs
CHANGED
|
@@ -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-
|
|
2
|
+
export { t as VerificationError, n as autoPass, r as checkVerification, i as github, a as readPullRequest, o as verdict } from "./verification-6s2chr4X.mjs";
|
package/package.json
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@milkenjoyer/toolchain",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.11.0",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "git+https://github.com/birbprophet/agent-kit.git",
|
|
7
7
|
"directory": "packages/toolchain"
|
|
8
8
|
},
|
|
9
9
|
"bin": {
|
|
10
|
+
"formal": "./src/formal.ts",
|
|
10
11
|
"proof": "./src/proof.ts",
|
|
11
12
|
"verdict": "./src/verdict.ts",
|
|
12
13
|
"verification": "./src/verification.ts"
|
|
@@ -16,6 +17,8 @@
|
|
|
16
17
|
"licenses",
|
|
17
18
|
"node-version",
|
|
18
19
|
"renovate.json",
|
|
20
|
+
"src/formal.ts",
|
|
21
|
+
"src/formal/",
|
|
19
22
|
"src/proof.ts",
|
|
20
23
|
"src/verdict.ts",
|
|
21
24
|
"src/verification.ts",
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
export type CfgConstant =
|
|
2
|
+
| { readonly _tag: "Value"; readonly name: string; readonly value: string }
|
|
3
|
+
| {
|
|
4
|
+
readonly _tag: "Substitution";
|
|
5
|
+
readonly name: string;
|
|
6
|
+
readonly by: string;
|
|
7
|
+
};
|
|
8
|
+
export type Cfg = {
|
|
9
|
+
readonly constants: ReadonlyArray<CfgConstant>;
|
|
10
|
+
readonly invariants: ReadonlyArray<string>;
|
|
11
|
+
readonly checkDeadlock: ReadonlyArray<boolean>;
|
|
12
|
+
};
|
|
13
|
+
export type CfgError = { readonly _tag: "CfgError"; readonly message: string };
|
|
14
|
+
|
|
15
|
+
const keywords = new Set([
|
|
16
|
+
"CONSTANT",
|
|
17
|
+
"CONSTANTS",
|
|
18
|
+
"INIT",
|
|
19
|
+
"NEXT",
|
|
20
|
+
"SPECIFICATION",
|
|
21
|
+
"INVARIANT",
|
|
22
|
+
"INVARIANTS",
|
|
23
|
+
"PROPERTY",
|
|
24
|
+
"PROPERTIES",
|
|
25
|
+
"CONSTRAINT",
|
|
26
|
+
"CONSTRAINTS",
|
|
27
|
+
"ACTION_CONSTRAINT",
|
|
28
|
+
"ACTION_CONSTRAINTS",
|
|
29
|
+
"SYMMETRY",
|
|
30
|
+
"VIEW",
|
|
31
|
+
"ALIAS",
|
|
32
|
+
"POSTCONDITION",
|
|
33
|
+
"CHECK_DEADLOCK",
|
|
34
|
+
]);
|
|
35
|
+
export const parseCfg = (source: string): Cfg | CfgError => {
|
|
36
|
+
const text = source.replaceAll(/\(\*[\s\S]*?\*\)|\\\*[^\n]*/gu, " ");
|
|
37
|
+
const tokens =
|
|
38
|
+
text.match(/"(?:\\.|[^"\\])*"|<-|[A-Za-z_][A-Za-z0-9_!]*|-?\d+|[^\s]/gu) ??
|
|
39
|
+
[];
|
|
40
|
+
const constants: CfgConstant[] = [];
|
|
41
|
+
const invariants: string[] = [];
|
|
42
|
+
const checkDeadlock: boolean[] = [];
|
|
43
|
+
const fail = (message: string): CfgError => ({ _tag: "CfgError", message });
|
|
44
|
+
let i = 0;
|
|
45
|
+
while (i < tokens.length) {
|
|
46
|
+
const keyword = tokens[i++];
|
|
47
|
+
if (keyword === undefined || !keywords.has(keyword))
|
|
48
|
+
return fail(`unknown config keyword ${keyword}`);
|
|
49
|
+
const operands: string[] = [];
|
|
50
|
+
while (i < tokens.length && !keywords.has(tokens[i] ?? ""))
|
|
51
|
+
operands.push(tokens[i++] ?? "");
|
|
52
|
+
if (operands.length === 0) return fail(`missing operand for ${keyword}`);
|
|
53
|
+
if (keyword === "CONSTANT" || keyword === "CONSTANTS") {
|
|
54
|
+
let j = 0;
|
|
55
|
+
while (j < operands.length) {
|
|
56
|
+
const name = operands[j++];
|
|
57
|
+
const assignment = operands[j++];
|
|
58
|
+
if (
|
|
59
|
+
name === undefined ||
|
|
60
|
+
!/^[A-Za-z_][A-Za-z0-9_!]*$/u.test(name) ||
|
|
61
|
+
(assignment !== "=" && assignment !== "<-")
|
|
62
|
+
)
|
|
63
|
+
return fail(`unparseable constant ${name}`);
|
|
64
|
+
const value: string[] = [];
|
|
65
|
+
let depth = 0;
|
|
66
|
+
while (j < operands.length) {
|
|
67
|
+
const token = operands[j] ?? "";
|
|
68
|
+
if (
|
|
69
|
+
depth === 0 &&
|
|
70
|
+
value.length > 0 &&
|
|
71
|
+
["=", "<-"].includes(operands[j + 1] ?? "")
|
|
72
|
+
)
|
|
73
|
+
break;
|
|
74
|
+
if (token === "{") depth++;
|
|
75
|
+
if (token === "}") depth--;
|
|
76
|
+
value.push(token);
|
|
77
|
+
j++;
|
|
78
|
+
}
|
|
79
|
+
const raw = value.join(" ");
|
|
80
|
+
if (
|
|
81
|
+
depth !== 0 ||
|
|
82
|
+
!/^(?:-?\d+|[A-Za-z_][A-Za-z0-9_!]*|"(?:\\.|[^"\\])*"|\{[\s\S]*\})$/u.test(
|
|
83
|
+
raw
|
|
84
|
+
)
|
|
85
|
+
)
|
|
86
|
+
return fail(`unparseable constant ${name} ${assignment} ${raw}`);
|
|
87
|
+
constants.push(
|
|
88
|
+
assignment === "="
|
|
89
|
+
? { _tag: "Value", name, value: raw }
|
|
90
|
+
: { _tag: "Substitution", name, by: raw }
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
} else if (keyword === "CHECK_DEADLOCK") {
|
|
94
|
+
if (
|
|
95
|
+
operands.length !== 1 ||
|
|
96
|
+
!["TRUE", "FALSE"].includes(operands[0] ?? "")
|
|
97
|
+
)
|
|
98
|
+
return fail("CHECK_DEADLOCK needs TRUE or FALSE");
|
|
99
|
+
checkDeadlock.push(operands[0] === "TRUE");
|
|
100
|
+
} else {
|
|
101
|
+
if (!operands.every((token) => /^[A-Za-z_][A-Za-z0-9_!]*$/u.test(token)))
|
|
102
|
+
return fail(`invalid operands for ${keyword}`);
|
|
103
|
+
if (keyword.startsWith("INVARIANT")) invariants.push(...operands);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return { constants, invariants, checkDeadlock };
|
|
107
|
+
};
|
|
108
|
+
export const integer = (constant: CfgConstant): bigint | undefined =>
|
|
109
|
+
constant._tag === "Value" && /^-?\d+$/u.test(constant.value)
|
|
110
|
+
? BigInt(constant.value)
|
|
111
|
+
: undefined;
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
import * as Console from "effect/Console";
|
|
2
|
+
import * as Effect from "effect/Effect";
|
|
3
|
+
import { FileSystem } from "effect/FileSystem";
|
|
4
|
+
|
|
5
|
+
import { integer } from "./cfg.ts";
|
|
6
|
+
import type { Repo } from "./specs.ts";
|
|
7
|
+
import { FormalError, Tla } from "./tla.ts";
|
|
8
|
+
|
|
9
|
+
type Problem = { readonly file: string; readonly message: string };
|
|
10
|
+
type Check = (
|
|
11
|
+
repo: Repo
|
|
12
|
+
) => Effect.Effect<ReadonlyArray<Problem>, FormalError, Tla | FileSystem>;
|
|
13
|
+
export type Finding = Problem & { readonly check: keyof typeof checks };
|
|
14
|
+
|
|
15
|
+
const configs = (repo: Repo) =>
|
|
16
|
+
repo.specs
|
|
17
|
+
.filter((s) => s.analysis._tag === "Parsed")
|
|
18
|
+
.flatMap((s) => s.configs)
|
|
19
|
+
.flatMap((c) => ("_tag" in c.cfg ? [] : [{ path: c.path, cfg: c.cfg }]));
|
|
20
|
+
const specs: Check = (repo) =>
|
|
21
|
+
Effect.succeed([
|
|
22
|
+
...repo.decodeErrors.map((message) => ({ file: "specs.json", message })),
|
|
23
|
+
...repo.specs.flatMap((s) => [
|
|
24
|
+
...(!s.entry && repo.decodeErrors.length === 0
|
|
25
|
+
? [{ file: s.path, message: "tracked spec has no specs.json entry" }]
|
|
26
|
+
: []),
|
|
27
|
+
...(s.entry?.mode === "gate" &&
|
|
28
|
+
!s.configs.some((c) => c.path === s.path.replace(/\.tla$/u, ".cfg"))
|
|
29
|
+
? [{ file: s.path, message: "gate spec has no matching .cfg" }]
|
|
30
|
+
: []),
|
|
31
|
+
...s.configs.flatMap((c) =>
|
|
32
|
+
"_tag" in c.cfg ? [{ file: c.path, message: c.cfg.message }] : []
|
|
33
|
+
),
|
|
34
|
+
]),
|
|
35
|
+
...Object.keys(repo.file)
|
|
36
|
+
.filter((f) => f.endsWith(".tla") && !repo.tracked.includes(f))
|
|
37
|
+
.map((file) => ({ file, message: "entry names no tracked file" })),
|
|
38
|
+
...repo.orphans.map((file) => ({
|
|
39
|
+
file,
|
|
40
|
+
message: "orphan config has no matching module",
|
|
41
|
+
})),
|
|
42
|
+
]);
|
|
43
|
+
const sany: Check = (repo) =>
|
|
44
|
+
Effect.succeed(
|
|
45
|
+
repo.specs.flatMap((s) =>
|
|
46
|
+
s.analysis._tag === "Unparsed"
|
|
47
|
+
? [{ file: s.path, message: s.analysis.output }]
|
|
48
|
+
: []
|
|
49
|
+
)
|
|
50
|
+
);
|
|
51
|
+
const typeok: Check = (repo) =>
|
|
52
|
+
Effect.succeed([
|
|
53
|
+
...configs(repo)
|
|
54
|
+
.filter((c) => !c.cfg.invariants.includes("TypeOK"))
|
|
55
|
+
.map((c) => ({
|
|
56
|
+
file: c.path,
|
|
57
|
+
message: "does not check INVARIANT TypeOK",
|
|
58
|
+
})),
|
|
59
|
+
...repo.specs.flatMap((s) => {
|
|
60
|
+
if (s.analysis._tag !== "Parsed" || s.configs.length === 0) return [];
|
|
61
|
+
const typed = s.analysis.semantics.typeOK;
|
|
62
|
+
if (!typed)
|
|
63
|
+
return s.configs.some(
|
|
64
|
+
(c) => !("_tag" in c.cfg) && c.cfg.invariants.includes("TypeOK")
|
|
65
|
+
)
|
|
66
|
+
? [{ file: s.path, message: "TypeOK is not defined" }]
|
|
67
|
+
: [];
|
|
68
|
+
const missing = s.analysis.semantics.variables.filter(
|
|
69
|
+
(v) => !typed.has(v)
|
|
70
|
+
);
|
|
71
|
+
return missing.length
|
|
72
|
+
? [
|
|
73
|
+
{
|
|
74
|
+
file: s.path,
|
|
75
|
+
message: `TypeOK does not constrain ${missing.join(", ")}; give each a conjunct such as ${missing[0]} \\in S`,
|
|
76
|
+
},
|
|
77
|
+
]
|
|
78
|
+
: [];
|
|
79
|
+
}),
|
|
80
|
+
]);
|
|
81
|
+
const deadlock: Check = (repo) =>
|
|
82
|
+
Effect.succeed(
|
|
83
|
+
configs(repo)
|
|
84
|
+
.filter((c) => c.cfg.checkDeadlock.includes(false))
|
|
85
|
+
.map((c) => ({
|
|
86
|
+
file: c.path,
|
|
87
|
+
message:
|
|
88
|
+
"CHECK_DEADLOCK FALSE; give the bounded spec an explicit finished step",
|
|
89
|
+
}))
|
|
90
|
+
);
|
|
91
|
+
const constants: Check = (repo) =>
|
|
92
|
+
Effect.succeed(
|
|
93
|
+
configs(repo).flatMap((c) => {
|
|
94
|
+
const groups = new Map<bigint, string[]>();
|
|
95
|
+
for (const constant of c.cfg.constants) {
|
|
96
|
+
const n = integer(constant);
|
|
97
|
+
if (n === undefined) continue;
|
|
98
|
+
groups.set(n, [...(groups.get(n) ?? []), constant.name]);
|
|
99
|
+
}
|
|
100
|
+
return [...groups]
|
|
101
|
+
.filter(([, names]) => names.length > 1)
|
|
102
|
+
.map(([n, names]) => ({
|
|
103
|
+
file: c.path,
|
|
104
|
+
message: `${names.length === 2 ? names.join(" and ") : `${names.slice(0, -1).join(", ")} and ${names.at(-1)}`} ${names.length === 2 ? "both" : "all"} equal ${n}`,
|
|
105
|
+
}));
|
|
106
|
+
})
|
|
107
|
+
);
|
|
108
|
+
const format: Check = (repo) =>
|
|
109
|
+
Effect.gen(function* () {
|
|
110
|
+
const tla = yield* Tla;
|
|
111
|
+
const fs = yield* FileSystem;
|
|
112
|
+
return (yield* Effect.forEach(
|
|
113
|
+
repo.specs.filter((s) => s.analysis._tag === "Parsed"),
|
|
114
|
+
(s) =>
|
|
115
|
+
Effect.gen(function* () {
|
|
116
|
+
const result = yield* tla.format(`${repo.root}/${s.path}`);
|
|
117
|
+
if (result._tag === "Failed")
|
|
118
|
+
return [{ file: s.path, message: result.output }];
|
|
119
|
+
const text = yield* fs.readFileString(`${repo.root}/${s.path}`);
|
|
120
|
+
if (text === result.text) return [];
|
|
121
|
+
const lines = text.split("\n");
|
|
122
|
+
const expected = result.text.split("\n");
|
|
123
|
+
let line = 0;
|
|
124
|
+
while (
|
|
125
|
+
line < Math.max(lines.length, expected.length) &&
|
|
126
|
+
lines[line] === expected[line]
|
|
127
|
+
)
|
|
128
|
+
line++;
|
|
129
|
+
return [
|
|
130
|
+
{
|
|
131
|
+
file: s.path,
|
|
132
|
+
message: `differs from formatter.Main at line ${line + 1}; run ${tla.fixCommand(s.path)}`,
|
|
133
|
+
},
|
|
134
|
+
];
|
|
135
|
+
}),
|
|
136
|
+
{ concurrency: 4 }
|
|
137
|
+
)).flat();
|
|
138
|
+
}).pipe(
|
|
139
|
+
Effect.mapError((cause) => new FormalError({ message: String(cause) }))
|
|
140
|
+
);
|
|
141
|
+
const tlc: Check = (repo) =>
|
|
142
|
+
Effect.gen(function* () {
|
|
143
|
+
const tla = yield* Tla;
|
|
144
|
+
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
|
|
188
|
+
yield* Console.log(
|
|
189
|
+
`tlc: ${config.path}: ${states} distinct states in ${run.seconds} s`
|
|
190
|
+
);
|
|
191
|
+
}
|
|
192
|
+
return problems;
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
export const checks = {
|
|
196
|
+
specs,
|
|
197
|
+
sany,
|
|
198
|
+
typeok,
|
|
199
|
+
deadlock,
|
|
200
|
+
constants,
|
|
201
|
+
format,
|
|
202
|
+
tlc,
|
|
203
|
+
} satisfies Record<string, Check>;
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
export type Semantics = {
|
|
2
|
+
readonly variables: ReadonlyArray<string>;
|
|
3
|
+
readonly typeOK: ReadonlySet<string> | undefined;
|
|
4
|
+
};
|
|
5
|
+
|
|
6
|
+
type Node = { readonly tag: string; text: string; readonly children: Node[] };
|
|
7
|
+
const child = (node: Node, tag: string) =>
|
|
8
|
+
node.children.find((n) => n.tag === tag);
|
|
9
|
+
const value = (node: Node, tag: string) => child(node, tag)?.text.trim() ?? "";
|
|
10
|
+
const descendants = (node: Node): Node[] => [
|
|
11
|
+
node,
|
|
12
|
+
...node.children.flatMap(descendants),
|
|
13
|
+
];
|
|
14
|
+
export const fromXml = (xml: string): Semantics => {
|
|
15
|
+
const root: Node = { tag: "document", text: "", children: [] };
|
|
16
|
+
const stack = [root];
|
|
17
|
+
for (const match of xml.matchAll(
|
|
18
|
+
/<!\[CDATA\[[\s\S]*?\]\]>|<\?[^>]*\?>|<([^>]+)>|([^<]+)/gu
|
|
19
|
+
)) {
|
|
20
|
+
const token = match[0];
|
|
21
|
+
const parent = stack.at(-1);
|
|
22
|
+
if (parent === undefined) throw new Error("invalid SANY XML stack");
|
|
23
|
+
if (token.startsWith("<!") || token.startsWith("<?")) continue;
|
|
24
|
+
if (token.startsWith("</")) {
|
|
25
|
+
if (stack.pop()?.tag !== token.slice(2, -1))
|
|
26
|
+
throw new Error("invalid SANY XML closing tag");
|
|
27
|
+
} else if (token.startsWith("<")) {
|
|
28
|
+
const node: Node = {
|
|
29
|
+
tag: token.slice(1, token.endsWith("/>") ? -2 : -1),
|
|
30
|
+
text: "",
|
|
31
|
+
children: [],
|
|
32
|
+
};
|
|
33
|
+
parent.children.push(node);
|
|
34
|
+
if (!token.endsWith("/>")) stack.push(node);
|
|
35
|
+
} else
|
|
36
|
+
parent.text += token
|
|
37
|
+
.replaceAll("<", "<")
|
|
38
|
+
.replaceAll(">", ">")
|
|
39
|
+
.replaceAll("&", "&");
|
|
40
|
+
}
|
|
41
|
+
const modules = child(root, "modules");
|
|
42
|
+
const context = modules && child(modules, "context");
|
|
43
|
+
if (stack.length !== 1 || !modules || !context)
|
|
44
|
+
throw new Error("missing SANY XML context");
|
|
45
|
+
const entries = new Map(
|
|
46
|
+
context.children.map((entry) => [
|
|
47
|
+
value(entry, "UID"),
|
|
48
|
+
entry.children.find((n) => n.tag !== "UID"),
|
|
49
|
+
])
|
|
50
|
+
);
|
|
51
|
+
const module = [...entries.values()].find(
|
|
52
|
+
(n) =>
|
|
53
|
+
n?.tag === "ModuleNode" &&
|
|
54
|
+
value(n, "uniquename") === value(modules, "RootModule")
|
|
55
|
+
);
|
|
56
|
+
if (!module) throw new Error("missing root module in SANY XML");
|
|
57
|
+
const closure = new Set<string>();
|
|
58
|
+
const visitModule = (node: Node) => {
|
|
59
|
+
if (closure.has(value(node, "uniquename"))) return;
|
|
60
|
+
closure.add(value(node, "uniquename"));
|
|
61
|
+
for (const ref of descendants(
|
|
62
|
+
child(node, "extends") ?? { tag: "", text: "", children: [] }
|
|
63
|
+
)) {
|
|
64
|
+
if (ref.tag !== "uniquename") continue;
|
|
65
|
+
const next = [...entries.values()].find(
|
|
66
|
+
(entry) =>
|
|
67
|
+
entry?.tag === "ModuleNode" &&
|
|
68
|
+
value(entry, "uniquename") === ref.text.trim()
|
|
69
|
+
);
|
|
70
|
+
if (!next) throw new Error("unresolved SANY EXTENDS module");
|
|
71
|
+
visitModule(next);
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
visitModule(module);
|
|
75
|
+
const variables = new Map(
|
|
76
|
+
[...entries].flatMap(([uid, node]) =>
|
|
77
|
+
node?.tag === "OpDeclNode" &&
|
|
78
|
+
value(node, "kind") === "3" &&
|
|
79
|
+
closure.has(value(child(node, "location") ?? node, "filename"))
|
|
80
|
+
? [[uid, value(node, "uniquename")] as const]
|
|
81
|
+
: []
|
|
82
|
+
)
|
|
83
|
+
);
|
|
84
|
+
const target = (appl: Node) => {
|
|
85
|
+
const ref = child(appl, "operator")?.children[0];
|
|
86
|
+
return ref && { ref, uid: value(ref, "UID") };
|
|
87
|
+
};
|
|
88
|
+
const operands = (appl: Node) => child(appl, "operands")?.children ?? [];
|
|
89
|
+
const reaches = (body: Node, seen = new Set<string>()): Set<string> => {
|
|
90
|
+
const reached = new Set<string>();
|
|
91
|
+
for (const n of descendants(body)) {
|
|
92
|
+
if (!n.tag.endsWith("Ref")) continue;
|
|
93
|
+
const uid = value(n, "UID");
|
|
94
|
+
const variable = variables.get(uid);
|
|
95
|
+
if (variable !== undefined) reached.add(variable);
|
|
96
|
+
if (seen.has(uid)) continue;
|
|
97
|
+
seen.add(uid);
|
|
98
|
+
const next = child(entries.get(uid) ?? n, "body");
|
|
99
|
+
if (next) for (const v of reaches(next, seen)) reached.add(v);
|
|
100
|
+
}
|
|
101
|
+
return reached;
|
|
102
|
+
};
|
|
103
|
+
const typed = (appl: Node, seen: Set<string>): ReadonlyArray<string> => {
|
|
104
|
+
if (appl.tag === "LetInNode") {
|
|
105
|
+
const body = child(appl, "body")?.children[0];
|
|
106
|
+
return body ? typed(body, seen) : [];
|
|
107
|
+
}
|
|
108
|
+
const op = target(appl);
|
|
109
|
+
if (appl.tag !== "OpApplNode" || op === undefined) return [];
|
|
110
|
+
const args = operands(appl);
|
|
111
|
+
if (op.ref.tag === "UserDefinedOpKindRef" && args.length === 0) {
|
|
112
|
+
if (seen.has(op.uid)) return [];
|
|
113
|
+
seen.add(op.uid);
|
|
114
|
+
const body = child(entries.get(op.uid) ?? appl, "body");
|
|
115
|
+
return body?.children[0] ? typed(body.children[0], seen) : [];
|
|
116
|
+
}
|
|
117
|
+
if (op.ref.tag !== "BuiltInKindRef") return [];
|
|
118
|
+
const name = value(entries.get(op.uid) ?? appl, "uniquename");
|
|
119
|
+
if (name === "$ConjList" || name === "\\land")
|
|
120
|
+
return args.flatMap((arg) => typed(arg, seen));
|
|
121
|
+
const [left, set] = args;
|
|
122
|
+
const variable =
|
|
123
|
+
left && target(left)?.ref.tag === "OpDeclNodeRef"
|
|
124
|
+
? variables.get(target(left)?.uid ?? "")
|
|
125
|
+
: undefined;
|
|
126
|
+
return (name === "\\in" || name === "\\subseteq") &&
|
|
127
|
+
args.length === 2 &&
|
|
128
|
+
variable !== undefined &&
|
|
129
|
+
set !== undefined &&
|
|
130
|
+
!reaches(set).has(variable)
|
|
131
|
+
? [variable]
|
|
132
|
+
: [];
|
|
133
|
+
};
|
|
134
|
+
const typeOK = module.children
|
|
135
|
+
.filter((n) => n.tag === "UserDefinedOpKindRef")
|
|
136
|
+
.map((ref) => entries.get(value(ref, "UID")))
|
|
137
|
+
.find(
|
|
138
|
+
(node) => node !== undefined && value(node, "uniquename") === "TypeOK"
|
|
139
|
+
);
|
|
140
|
+
const body = typeOK && child(typeOK, "body")?.children[0];
|
|
141
|
+
return {
|
|
142
|
+
variables: [...new Set(variables.values())],
|
|
143
|
+
typeOK: typeOK && new Set(body ? typed(body, new Set()) : []),
|
|
144
|
+
};
|
|
145
|
+
};
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import * as Effect from "effect/Effect";
|
|
2
|
+
import { FileSystem } from "effect/FileSystem";
|
|
3
|
+
import { Path } from "effect/Path";
|
|
4
|
+
import { ChildProcess } from "effect/process";
|
|
5
|
+
import * as Result from "effect/Result";
|
|
6
|
+
import * as Schema from "effect/Schema";
|
|
7
|
+
import * as SchemaIssue from "effect/SchemaIssue";
|
|
8
|
+
import * as Stream from "effect/Stream";
|
|
9
|
+
|
|
10
|
+
import { parseCfg } from "./cfg.ts";
|
|
11
|
+
import type { Cfg, CfgError } from "./cfg.ts";
|
|
12
|
+
import type { Semantics } from "./semantics.ts";
|
|
13
|
+
import { FormalError, Tla } from "./tla.ts";
|
|
14
|
+
|
|
15
|
+
const RepoPath = Schema.String.check(
|
|
16
|
+
Schema.isPattern(/^(?!\/)(?!(?:.*\/)?\.\.(?:\/|$))[^\0\\]+$/u)
|
|
17
|
+
);
|
|
18
|
+
const OperatorName = Schema.String.check(
|
|
19
|
+
Schema.isPattern(/^[A-Za-z_][A-Za-z0-9_]*$/u)
|
|
20
|
+
);
|
|
21
|
+
const Entry = Schema.Struct({
|
|
22
|
+
synced: Schema.String.check(Schema.isPattern(/^[0-9a-f]{40}$/u)),
|
|
23
|
+
covers: Schema.NonEmptyArray(Schema.NonEmptyString),
|
|
24
|
+
mode: Schema.Literals(["gate", "manual"]),
|
|
25
|
+
timeoutSeconds: Schema.Int.check(
|
|
26
|
+
Schema.isBetween({ minimum: 1, maximum: 1800 })
|
|
27
|
+
).pipe(Schema.withDecodingDefaultKey(Effect.succeed(60))),
|
|
28
|
+
witnesses: Schema.Array(OperatorName).pipe(
|
|
29
|
+
Schema.withDecodingDefaultKey(Effect.succeed([]))
|
|
30
|
+
),
|
|
31
|
+
breaks: Schema.Array(
|
|
32
|
+
Schema.Struct({
|
|
33
|
+
violates: OperatorName,
|
|
34
|
+
find: Schema.NonEmptyString,
|
|
35
|
+
replace: Schema.String,
|
|
36
|
+
file: Schema.optionalKey(RepoPath),
|
|
37
|
+
})
|
|
38
|
+
).pipe(Schema.withDecodingDefaultKey(Effect.succeed([]))),
|
|
39
|
+
traces: Schema.optionalKey(
|
|
40
|
+
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
|
+
),
|
|
46
|
+
})
|
|
47
|
+
),
|
|
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;
|
|
61
|
+
export type Analysis =
|
|
62
|
+
| { readonly _tag: "Parsed"; readonly semantics: Semantics }
|
|
63
|
+
| { readonly _tag: "Unparsed"; readonly output: string };
|
|
64
|
+
type ConfigFile = {
|
|
65
|
+
readonly path: string;
|
|
66
|
+
readonly cfg: Cfg | CfgError;
|
|
67
|
+
};
|
|
68
|
+
type Spec = {
|
|
69
|
+
readonly path: string;
|
|
70
|
+
readonly entry: typeof Entry.Type | undefined;
|
|
71
|
+
readonly analysis: Analysis;
|
|
72
|
+
readonly configs: ReadonlyArray<ConfigFile>;
|
|
73
|
+
};
|
|
74
|
+
export type Repo = {
|
|
75
|
+
readonly root: string;
|
|
76
|
+
readonly tracked: ReadonlyArray<string>;
|
|
77
|
+
readonly file: SpecsFile;
|
|
78
|
+
readonly specs: ReadonlyArray<Spec>;
|
|
79
|
+
readonly orphans: ReadonlyArray<string>;
|
|
80
|
+
readonly decodeErrors: ReadonlyArray<string>;
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
export const load = Effect.fn("formal.load")(function* (root: string) {
|
|
84
|
+
const fs = yield* FileSystem;
|
|
85
|
+
const path = yield* Path;
|
|
86
|
+
const tla = yield* Tla;
|
|
87
|
+
const git = yield* ChildProcess.make("git", ["ls-files", "-z"], {
|
|
88
|
+
cwd: root,
|
|
89
|
+
stdin: "ignore",
|
|
90
|
+
});
|
|
91
|
+
const [files, code] = yield* Effect.all(
|
|
92
|
+
[git.stdout.pipe(Stream.decodeText, Stream.mkString), git.exitCode],
|
|
93
|
+
{ concurrency: "unbounded" }
|
|
94
|
+
);
|
|
95
|
+
if (Number(code) !== 0)
|
|
96
|
+
return yield* new FormalError({ message: "git ls-files failed" });
|
|
97
|
+
const tracked = yield* Effect.filter(
|
|
98
|
+
files.split("\0").filter(Boolean),
|
|
99
|
+
(file) => fs.exists(path.join(root, file))
|
|
100
|
+
);
|
|
101
|
+
let file: SpecsFile = {};
|
|
102
|
+
let decodeErrors: ReadonlyArray<string> = [];
|
|
103
|
+
if (yield* fs.exists(path.join(root, "specs.json"))) {
|
|
104
|
+
const raw = yield* fs.readFileString(path.join(root, "specs.json"));
|
|
105
|
+
const decoded = Schema.decodeResult(Schema.fromJsonString(SpecsFile))(raw, {
|
|
106
|
+
errors: "all",
|
|
107
|
+
onExcessProperty: "error",
|
|
108
|
+
});
|
|
109
|
+
if (Result.isSuccess(decoded)) file = decoded.success;
|
|
110
|
+
else
|
|
111
|
+
decodeErrors = SchemaIssue.makeFormatterStandardSchemaV1()(
|
|
112
|
+
decoded.failure.issue
|
|
113
|
+
).issues.map(({ message, path: at = [] }) =>
|
|
114
|
+
at.length === 0
|
|
115
|
+
? message
|
|
116
|
+
: `${at.map((key) => `[${JSON.stringify(typeof key === "object" ? key.key : key)}]`).join("")}: ${message}`
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
const modules = tracked.filter((f) => f.endsWith(".tla"));
|
|
120
|
+
const configs = tracked.filter(
|
|
121
|
+
(f) =>
|
|
122
|
+
f.endsWith(".cfg") &&
|
|
123
|
+
modules.some((m) => path.dirname(m) === path.dirname(f))
|
|
124
|
+
);
|
|
125
|
+
const paired = new Set<string>();
|
|
126
|
+
const specs = yield* Effect.forEach(
|
|
127
|
+
modules,
|
|
128
|
+
(module) =>
|
|
129
|
+
Effect.gen(function* () {
|
|
130
|
+
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
|
+
);
|
|
137
|
+
const parsed = yield* Effect.forEach(
|
|
138
|
+
matches,
|
|
139
|
+
(c) =>
|
|
140
|
+
Effect.gen(function* () {
|
|
141
|
+
paired.add(c);
|
|
142
|
+
return {
|
|
143
|
+
path: c,
|
|
144
|
+
cfg: parseCfg(yield* fs.readFileString(path.join(root, c))),
|
|
145
|
+
};
|
|
146
|
+
}),
|
|
147
|
+
{ concurrency: 1 }
|
|
148
|
+
);
|
|
149
|
+
return {
|
|
150
|
+
path: module,
|
|
151
|
+
entry: file[module],
|
|
152
|
+
analysis: yield* tla.analyze(path.join(root, module)),
|
|
153
|
+
configs: parsed,
|
|
154
|
+
};
|
|
155
|
+
}),
|
|
156
|
+
{ concurrency: 4 }
|
|
157
|
+
);
|
|
158
|
+
return {
|
|
159
|
+
root,
|
|
160
|
+
tracked,
|
|
161
|
+
file,
|
|
162
|
+
specs,
|
|
163
|
+
orphans: configs.filter((c) => !paired.has(c)),
|
|
164
|
+
decodeErrors,
|
|
165
|
+
} satisfies Repo;
|
|
166
|
+
});
|