@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
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
|
|
3
|
+
import * as Clock from "effect/Clock";
|
|
4
|
+
import * as Config from "effect/Config";
|
|
5
|
+
import * as Context from "effect/Context";
|
|
6
|
+
import * as Data from "effect/Data";
|
|
7
|
+
import * as Effect from "effect/Effect";
|
|
8
|
+
import { FileSystem } from "effect/FileSystem";
|
|
9
|
+
import { FetchHttpClient, HttpClient } from "effect/http";
|
|
10
|
+
import * as Layer from "effect/Layer";
|
|
11
|
+
import * as Option from "effect/Option";
|
|
12
|
+
import { Path } from "effect/Path";
|
|
13
|
+
import { ChildProcess, ChildProcessSpawner } from "effect/process";
|
|
14
|
+
import * as Stream from "effect/Stream";
|
|
15
|
+
|
|
16
|
+
import { fromXml } from "./semantics.ts";
|
|
17
|
+
import type { Analysis } from "./specs.ts";
|
|
18
|
+
|
|
19
|
+
export class FormalError extends Data.TaggedError("FormalError")<{
|
|
20
|
+
readonly message: string;
|
|
21
|
+
}> {}
|
|
22
|
+
type ToolMessage = {
|
|
23
|
+
readonly code: number;
|
|
24
|
+
readonly severity: number;
|
|
25
|
+
readonly text: string;
|
|
26
|
+
};
|
|
27
|
+
type TlcRun =
|
|
28
|
+
| {
|
|
29
|
+
readonly _tag: "Exited";
|
|
30
|
+
readonly code: number;
|
|
31
|
+
readonly messages: ReadonlyArray<ToolMessage>;
|
|
32
|
+
readonly seconds: number;
|
|
33
|
+
}
|
|
34
|
+
| { readonly _tag: "TimedOut" };
|
|
35
|
+
type TlcInput = {
|
|
36
|
+
readonly spec: string;
|
|
37
|
+
readonly config: string;
|
|
38
|
+
readonly timeoutSeconds: number;
|
|
39
|
+
};
|
|
40
|
+
type Formatted =
|
|
41
|
+
| { readonly _tag: "Formatted"; readonly text: string }
|
|
42
|
+
| { readonly _tag: "Failed"; readonly output: string };
|
|
43
|
+
|
|
44
|
+
const pins = [
|
|
45
|
+
{
|
|
46
|
+
file: "tla2tools-1.8.0.jar",
|
|
47
|
+
url: "https://github.com/tlaplus/tlaplus/releases/download/v1.8.0/tla2tools.jar",
|
|
48
|
+
sha256: "e1f11b75c4b5bc4478ba0ac5cb6ae28e40a01b0016bc33e0aa97da22dfdbcd7f",
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
file: "CommunityModules-deps-202611060952.jar",
|
|
52
|
+
url: "https://github.com/tlaplus/CommunityModules/releases/download/202611060952/CommunityModules-deps-202611060952.jar",
|
|
53
|
+
sha256: "a3b27c40f1fb5085f30c6ead37e918457f8cfc62c1e50a1dc668ef592ad2b5fb",
|
|
54
|
+
},
|
|
55
|
+
];
|
|
56
|
+
const error = (cause: unknown) =>
|
|
57
|
+
new FormalError({
|
|
58
|
+
message: cause instanceof Error ? cause.message : String(cause),
|
|
59
|
+
});
|
|
60
|
+
const digest = (bytes: Uint8Array) =>
|
|
61
|
+
createHash("sha256").update(bytes).digest("hex");
|
|
62
|
+
const messages = (output: string): ToolMessage[] =>
|
|
63
|
+
[
|
|
64
|
+
...output.matchAll(
|
|
65
|
+
/@!@!@STARTMSG (\d+):([0-4]) @!@!@([\s\S]*?)@!@!@ENDMSG \1 @!@!@/gu
|
|
66
|
+
),
|
|
67
|
+
].map((match) => ({
|
|
68
|
+
code: Number(match[1]),
|
|
69
|
+
severity: Number(match[2]),
|
|
70
|
+
text: (match[3] ?? "").trim(),
|
|
71
|
+
}));
|
|
72
|
+
const sanyReport = (output: string) =>
|
|
73
|
+
output
|
|
74
|
+
.split("\n")
|
|
75
|
+
.filter(
|
|
76
|
+
(line) =>
|
|
77
|
+
!/^(?:\*{6} SANY2 Version|Parsing file |Semantic processing of module |Linting of module )/u.test(
|
|
78
|
+
line
|
|
79
|
+
)
|
|
80
|
+
)
|
|
81
|
+
.join("\n")
|
|
82
|
+
.trim();
|
|
83
|
+
|
|
84
|
+
const make = Effect.gen(function* () {
|
|
85
|
+
const fs = yield* FileSystem;
|
|
86
|
+
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
|
|
87
|
+
const path = yield* Path;
|
|
88
|
+
const cacheHome = yield* Config.String("XDG_CACHE_HOME").pipe(
|
|
89
|
+
Config.withDefault("")
|
|
90
|
+
);
|
|
91
|
+
const home =
|
|
92
|
+
cacheHome !== ""
|
|
93
|
+
? cacheHome
|
|
94
|
+
: path.join(yield* Config.String("HOME"), ".cache");
|
|
95
|
+
const cache = path.join(home, "tla");
|
|
96
|
+
yield* fs.makeDirectory(cache, { recursive: true });
|
|
97
|
+
const jars: string[] = [];
|
|
98
|
+
for (const pin of pins) {
|
|
99
|
+
const file = path.join(cache, pin.file);
|
|
100
|
+
if (yield* fs.exists(file)) {
|
|
101
|
+
const actual = digest(yield* fs.readFile(file));
|
|
102
|
+
if (actual !== pin.sha256)
|
|
103
|
+
return yield* new FormalError({
|
|
104
|
+
message: `${file} has sha256 ${actual}, expected ${pin.sha256}; delete it to refetch`,
|
|
105
|
+
});
|
|
106
|
+
} else {
|
|
107
|
+
yield* Effect.scoped(
|
|
108
|
+
Effect.gen(function* () {
|
|
109
|
+
const temp = yield* fs.makeTempFileScoped({
|
|
110
|
+
directory: cache,
|
|
111
|
+
prefix: `.${pin.file}.`,
|
|
112
|
+
suffix: ".part",
|
|
113
|
+
});
|
|
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
|
+
);
|
|
128
|
+
yield* fs.writeFile(temp, bytes);
|
|
129
|
+
const actual = digest(bytes);
|
|
130
|
+
if (actual !== pin.sha256)
|
|
131
|
+
return yield* new FormalError({
|
|
132
|
+
message: `${pin.url} has sha256 ${actual}, expected ${pin.sha256}`,
|
|
133
|
+
});
|
|
134
|
+
return yield* fs.rename(temp, file);
|
|
135
|
+
})
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
jars.push(file);
|
|
139
|
+
}
|
|
140
|
+
const classpath = jars.join(":");
|
|
141
|
+
const run = Effect.fn("formal.java")(function* (
|
|
142
|
+
args: ReadonlyArray<string>,
|
|
143
|
+
cwd: string,
|
|
144
|
+
seconds = 120
|
|
145
|
+
) {
|
|
146
|
+
return yield* Effect.scoped(
|
|
147
|
+
Effect.gen(function* () {
|
|
148
|
+
const process = yield* ChildProcess.make("java", args, {
|
|
149
|
+
cwd,
|
|
150
|
+
stdin: "ignore",
|
|
151
|
+
killSignal: "SIGKILL",
|
|
152
|
+
});
|
|
153
|
+
return yield* Effect.all(
|
|
154
|
+
[
|
|
155
|
+
process.stdout.pipe(Stream.decodeText, Stream.mkString),
|
|
156
|
+
process.stderr.pipe(Stream.decodeText, Stream.mkString),
|
|
157
|
+
process.exitCode,
|
|
158
|
+
],
|
|
159
|
+
{ concurrency: "unbounded" }
|
|
160
|
+
);
|
|
161
|
+
}).pipe(
|
|
162
|
+
Effect.timeoutOption(`${seconds} seconds`),
|
|
163
|
+
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
|
|
164
|
+
Effect.mapError(error)
|
|
165
|
+
)
|
|
166
|
+
);
|
|
167
|
+
});
|
|
168
|
+
const version = yield* run(["-XshowSettings:properties", "-version"], cache);
|
|
169
|
+
if (
|
|
170
|
+
Option.isNone(version) ||
|
|
171
|
+
Number(version.value[2]) !== 0 ||
|
|
172
|
+
!/^\s*java\.specification\.version = 17\s*$/mu.test(version.value[1])
|
|
173
|
+
)
|
|
174
|
+
return yield* new FormalError({ message: "Java 17 is required on PATH" });
|
|
175
|
+
const invoke = (args: ReadonlyArray<string>, cwd: string, seconds?: number) =>
|
|
176
|
+
run(["-XX:+UseParallelGC", "-cp", classpath, ...args], cwd, seconds);
|
|
177
|
+
return {
|
|
178
|
+
fixCommand: (file: string) =>
|
|
179
|
+
`java -cp ${JSON.stringify(classpath)} formatter.Main ${JSON.stringify(file)} ${JSON.stringify(file)}`,
|
|
180
|
+
analyze: Effect.fn("formal.analyze")(function* (
|
|
181
|
+
file: string
|
|
182
|
+
): Effect.fn.Return<Analysis, FormalError> {
|
|
183
|
+
const sany = yield* invoke(
|
|
184
|
+
[
|
|
185
|
+
"tla2sany.SANY",
|
|
186
|
+
"-error-codes",
|
|
187
|
+
"-messagesAsErrors",
|
|
188
|
+
"4800,4801,4802,4803,4804,4805",
|
|
189
|
+
path.basename(file),
|
|
190
|
+
],
|
|
191
|
+
path.dirname(file)
|
|
192
|
+
);
|
|
193
|
+
if (Option.isNone(sany))
|
|
194
|
+
return { _tag: "Unparsed", output: "SANY timed out" };
|
|
195
|
+
const [stdout, stderr, code] = sany.value;
|
|
196
|
+
if (Number(code) !== 0 || /\*\*\* Warnings:/u.test(stdout + stderr))
|
|
197
|
+
return { _tag: "Unparsed", output: sanyReport(stdout + stderr) };
|
|
198
|
+
const xml = yield* invoke(
|
|
199
|
+
["tla2sany.xml.XMLExporter", "-o", "-t", path.basename(file)],
|
|
200
|
+
path.dirname(file)
|
|
201
|
+
);
|
|
202
|
+
if (Option.isNone(xml))
|
|
203
|
+
return { _tag: "Unparsed", output: "XMLExporter timed out" };
|
|
204
|
+
if (Number(xml.value[2]) !== 0)
|
|
205
|
+
return { _tag: "Unparsed", output: xml.value[0] + xml.value[1] };
|
|
206
|
+
return yield* Effect.try({
|
|
207
|
+
try: () => ({
|
|
208
|
+
_tag: "Parsed" as const,
|
|
209
|
+
semantics: fromXml(xml.value[0]),
|
|
210
|
+
}),
|
|
211
|
+
catch: error,
|
|
212
|
+
});
|
|
213
|
+
}),
|
|
214
|
+
format: Effect.fn("formal.format")(function* (
|
|
215
|
+
file: string
|
|
216
|
+
): Effect.fn.Return<Formatted, FormalError> {
|
|
217
|
+
return yield* Effect.scoped(
|
|
218
|
+
Effect.gen(function* () {
|
|
219
|
+
const temp = yield* fs.makeTempDirectoryScoped({
|
|
220
|
+
prefix: "formal-format-",
|
|
221
|
+
});
|
|
222
|
+
const output = path.join(temp, path.basename(file));
|
|
223
|
+
const result = yield* invoke(
|
|
224
|
+
["formatter.Main", "-v", "ERROR", file, output],
|
|
225
|
+
path.dirname(file)
|
|
226
|
+
);
|
|
227
|
+
if (Option.isNone(result))
|
|
228
|
+
return { _tag: "Failed" as const, output: "formatter timed out" };
|
|
229
|
+
if (Number(result.value[2]) !== 0 || !(yield* fs.exists(output)))
|
|
230
|
+
return {
|
|
231
|
+
_tag: "Failed" as const,
|
|
232
|
+
output: `formatter exited ${result.value[2]}: ${result.value[0]} ${result.value[1]}`,
|
|
233
|
+
};
|
|
234
|
+
return {
|
|
235
|
+
_tag: "Formatted" as const,
|
|
236
|
+
text: yield* fs.readFileString(output),
|
|
237
|
+
};
|
|
238
|
+
})
|
|
239
|
+
).pipe(Effect.mapError(error));
|
|
240
|
+
}),
|
|
241
|
+
tlc: Effect.fn("formal.tlc")(function* (
|
|
242
|
+
input: TlcInput
|
|
243
|
+
): Effect.fn.Return<TlcRun, FormalError> {
|
|
244
|
+
return yield* Effect.scoped(
|
|
245
|
+
Effect.gen(function* () {
|
|
246
|
+
const temp = yield* fs.makeTempDirectoryScoped({
|
|
247
|
+
prefix: "formal-tlc-",
|
|
248
|
+
});
|
|
249
|
+
const started = yield* Clock.currentTimeMillis;
|
|
250
|
+
const result = yield* invoke(
|
|
251
|
+
[
|
|
252
|
+
"tlc2.TLC",
|
|
253
|
+
"-tool",
|
|
254
|
+
"-workers",
|
|
255
|
+
"auto",
|
|
256
|
+
"-metadir",
|
|
257
|
+
temp,
|
|
258
|
+
"-noGenerateSpecTE",
|
|
259
|
+
"-config",
|
|
260
|
+
input.config,
|
|
261
|
+
path.basename(input.spec),
|
|
262
|
+
],
|
|
263
|
+
path.dirname(input.spec),
|
|
264
|
+
input.timeoutSeconds
|
|
265
|
+
);
|
|
266
|
+
const seconds = ((yield* Clock.currentTimeMillis) - started) / 1000;
|
|
267
|
+
if (Option.isNone(result)) return { _tag: "TimedOut" as const };
|
|
268
|
+
return {
|
|
269
|
+
_tag: "Exited" as const,
|
|
270
|
+
seconds,
|
|
271
|
+
code: Number(result.value[2]),
|
|
272
|
+
messages: messages(result.value[0] + result.value[1]),
|
|
273
|
+
};
|
|
274
|
+
})
|
|
275
|
+
).pipe(Effect.mapError(error));
|
|
276
|
+
}),
|
|
277
|
+
};
|
|
278
|
+
}).pipe(Effect.mapError(error));
|
|
279
|
+
|
|
280
|
+
export class Tla extends Context.Service<Tla, Effect.Success<typeof make>>()(
|
|
281
|
+
"formal/Tla"
|
|
282
|
+
) {
|
|
283
|
+
static readonly layer = Layer.effect(Tla, make);
|
|
284
|
+
}
|
package/src/formal.ts
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
#!/usr/bin/env bun
|
|
2
|
+
import { NodeServices } from "@effect/platform-node";
|
|
3
|
+
import * as Cause from "effect/Cause";
|
|
4
|
+
import * as Console from "effect/Console";
|
|
5
|
+
import * as Effect from "effect/Effect";
|
|
6
|
+
import * as Exit from "effect/Exit";
|
|
7
|
+
import { ChildProcess } from "effect/process";
|
|
8
|
+
import * as Stream from "effect/Stream";
|
|
9
|
+
|
|
10
|
+
import { checks, type Finding } from "./formal/checks.ts";
|
|
11
|
+
import { load } from "./formal/specs.ts";
|
|
12
|
+
import { FormalError, Tla } from "./formal/tla.ts";
|
|
13
|
+
|
|
14
|
+
const singleLine = (message: string) => message.replaceAll(/\s+/gu, " ").trim();
|
|
15
|
+
const checkName = (name: string): name is keyof typeof checks =>
|
|
16
|
+
Object.hasOwn(checks, name);
|
|
17
|
+
export const formal = Effect.fn("formal")(function* (root: string) {
|
|
18
|
+
const repo = yield* load(root);
|
|
19
|
+
const findings: Finding[] = [];
|
|
20
|
+
for (const name of Object.keys(checks)) {
|
|
21
|
+
if (!checkName(name)) continue;
|
|
22
|
+
const problems = yield* checks[name](repo);
|
|
23
|
+
findings.push(...problems.map((problem) => ({ ...problem, check: name })));
|
|
24
|
+
}
|
|
25
|
+
findings.sort(
|
|
26
|
+
(a, b) => a.file.localeCompare(b.file) || a.check.localeCompare(b.check)
|
|
27
|
+
);
|
|
28
|
+
if (findings.length) {
|
|
29
|
+
for (const f of findings)
|
|
30
|
+
yield* Console.error(`${f.file}: ${f.check}: ${singleLine(f.message)}`);
|
|
31
|
+
yield* Console.error(
|
|
32
|
+
`formal: ${findings.length} ${findings.length === 1 ? "finding" : "findings"}`
|
|
33
|
+
);
|
|
34
|
+
} else
|
|
35
|
+
yield* Console.log(
|
|
36
|
+
`formal: ${repo.specs.length} specs, ${repo.specs.reduce((n, s) => n + s.configs.length, 0)} configs, no findings`
|
|
37
|
+
);
|
|
38
|
+
return findings.length ? 1 : 0;
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
if (import.meta.main) {
|
|
42
|
+
if (process.argv.length > 2) {
|
|
43
|
+
process.stderr.write("usage: formal\n");
|
|
44
|
+
process.exitCode = 2;
|
|
45
|
+
} else {
|
|
46
|
+
const exit = await Effect.runPromiseExit(
|
|
47
|
+
Effect.scoped(
|
|
48
|
+
Effect.gen(function* () {
|
|
49
|
+
const git = yield* ChildProcess.make(
|
|
50
|
+
"git",
|
|
51
|
+
["rev-parse", "--show-toplevel"],
|
|
52
|
+
{ stdin: "ignore" }
|
|
53
|
+
);
|
|
54
|
+
const [root, code] = yield* Effect.all(
|
|
55
|
+
[git.stdout.pipe(Stream.decodeText, Stream.mkString), git.exitCode],
|
|
56
|
+
{ concurrency: "unbounded" }
|
|
57
|
+
);
|
|
58
|
+
if (Number(code) !== 0)
|
|
59
|
+
return yield* new FormalError({
|
|
60
|
+
message: "run formal inside a git repository",
|
|
61
|
+
});
|
|
62
|
+
return yield* formal(root.trim()).pipe(Effect.provide(Tla.layer));
|
|
63
|
+
})
|
|
64
|
+
).pipe(Effect.provide(NodeServices.layer))
|
|
65
|
+
);
|
|
66
|
+
if (Exit.isSuccess(exit)) process.exitCode = exit.value;
|
|
67
|
+
else {
|
|
68
|
+
const error = Cause.squash(exit.cause);
|
|
69
|
+
process.stderr.write(
|
|
70
|
+
`formal: ${singleLine(error instanceof Error ? error.message : String(error))}\n`
|
|
71
|
+
);
|
|
72
|
+
process.exitCode = 1;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import * as Data from "effect/Data";
|
|
2
2
|
import * as Schema from "effect/Schema";
|
|
3
3
|
import * as Effect from "effect/Effect";
|
|
4
|
-
import { FileSystem } from "effect/FileSystem";
|
|
5
4
|
import { NodeServices } from "@effect/platform-node";
|
|
6
|
-
import { Octokit } from "@octokit/rest";
|
|
7
5
|
import * as Cause from "effect/Cause";
|
|
8
|
-
import * as
|
|
6
|
+
import * as Console from "effect/Console";
|
|
9
7
|
import * as Exit from "effect/Exit";
|
|
8
|
+
import { FileSystem } from "effect/FileSystem";
|
|
9
|
+
import * as Config from "effect/Config";
|
|
10
|
+
import { Octokit } from "@octokit/rest";
|
|
10
11
|
import * as Redacted from "effect/Redacted";
|
|
11
|
-
import * as Console from "effect/Console";
|
|
12
12
|
// src/verification.ts
|
|
13
13
|
const exemptions = [{
|
|
14
14
|
reason: "Renovate PR",
|