@milkenjoyer/toolchain 0.9.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 +38 -1
- 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 +3 -0
- package/dist/verdict.mjs +39 -0
- package/dist/verification-6s2chr4X.mjs +111 -0
- package/dist/verification-p2rSx5lc.d.mts +40 -0
- package/dist/verification.d.mts +2 -28
- package/dist/verification.mjs +1 -97
- package/package.json +6 -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/src/verdict.ts +50 -0
- package/src/verification.ts +50 -22
|
@@ -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
|
+
});
|
|
@@ -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
|
+
}
|