@milkenjoyer/toolchain 0.10.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.
- package/README.md +117 -0
- package/dist/conformance.d.mts +18 -0
- package/dist/conformance.mjs +31 -0
- package/dist/effect-plugin.mjs +1 -1
- package/dist/exceptions-Dybl9xpc.mjs +11 -0
- package/dist/exceptions.mjs +1 -1
- package/dist/foldkit-plugin.mjs +1 -1
- package/dist/formal.d.mts +188 -0
- package/dist/formal.mjs +1279 -0
- package/dist/guard.mjs +2 -2
- package/dist/hooks.mjs +2 -2
- package/dist/{lint-JSnPsGO1.mjs → lint-IhmtnBbx.mjs} +3 -3
- package/dist/lint.mjs +1 -1
- package/dist/plugin.mjs +1 -1
- package/dist/proof.mjs +6 -6
- package/dist/trace-file-KuPRWgwt.mjs +17 -0
- package/dist/verdict.d.mts +1 -1
- package/dist/verdict.mjs +2 -2
- package/dist/{verification-MNG9WKEH.d.mts → verification-FgQFU1YN.d.mts} +2 -2
- package/dist/verification.d.mts +1 -1
- package/dist/verification.mjs +1 -1
- package/package.json +9 -2
- package/src/formal/cfg.ts +168 -0
- package/src/formal/checks.ts +190 -0
- package/src/formal/coverage.ts +79 -0
- package/src/formal/gate.ts +74 -0
- package/src/formal/git.ts +38 -0
- package/src/formal/proofs.ts +217 -0
- package/src/formal/semantics.ts +255 -0
- package/src/formal/specs.ts +212 -0
- package/src/formal/tla.ts +306 -0
- package/src/formal/trace-file.ts +35 -0
- package/src/formal/traces.ts +293 -0
- package/src/formal/violations.ts +76 -0
- package/src/formal.ts +82 -0
- package/dist/exceptions-Bq-9MxoR.mjs +0 -3
- package/dist/{effect-plugin-kYxB-5LJ.mjs → effect-plugin-BXUx9PPu.mjs} +1 -1
- package/dist/{plugin-BoBRGtGf.mjs → plugin-CwxhPtui.mjs} +1 -1
- package/dist/{verification-2bhNnJr-.mjs → verification-oAc1Q6I9.mjs} +4 -4
|
@@ -0,0 +1,212 @@
|
|
|
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 Common = {
|
|
22
|
+
synced: Schema.String.check(Schema.isPattern(/^[0-9a-f]{40}$/u)),
|
|
23
|
+
covers: Schema.NonEmptyArray(Schema.NonEmptyString),
|
|
24
|
+
};
|
|
25
|
+
const Runtime = {
|
|
26
|
+
...Common,
|
|
27
|
+
timeoutSeconds: Schema.Int.check(
|
|
28
|
+
Schema.isBetween({ minimum: 1, maximum: 1800 })
|
|
29
|
+
).pipe(Schema.withDecodingDefaultKey(Effect.succeed(60))),
|
|
30
|
+
};
|
|
31
|
+
const Proof = {
|
|
32
|
+
...Runtime,
|
|
33
|
+
witnesses: Schema.NonEmptyArray(OperatorName).check(
|
|
34
|
+
Schema.makeFilter(
|
|
35
|
+
(names) =>
|
|
36
|
+
new Set(names).size === names.length || "witnesses must be unique"
|
|
37
|
+
)
|
|
38
|
+
),
|
|
39
|
+
breaks: Schema.Array(
|
|
40
|
+
Schema.Struct({
|
|
41
|
+
violates: OperatorName,
|
|
42
|
+
find: Schema.NonEmptyString,
|
|
43
|
+
replace: Schema.String,
|
|
44
|
+
file: Schema.optionalKey(RepoPath),
|
|
45
|
+
})
|
|
46
|
+
).pipe(Schema.withDecodingDefaultKey(Effect.succeed([]))),
|
|
47
|
+
};
|
|
48
|
+
const Entry = Schema.Union([
|
|
49
|
+
Schema.Struct({
|
|
50
|
+
...Proof,
|
|
51
|
+
mode: Schema.Literal("gate"),
|
|
52
|
+
traces: Schema.optionalKey(
|
|
53
|
+
Schema.Struct({
|
|
54
|
+
seed: Schema.Int.check(Schema.isGreaterThanOrEqualTo(0)),
|
|
55
|
+
depth: Schema.Int.check(
|
|
56
|
+
Schema.isBetween({ minimum: 1, maximum: 10000 })
|
|
57
|
+
),
|
|
58
|
+
count: Schema.Int.check(
|
|
59
|
+
Schema.isBetween({ minimum: 1, maximum: 100000 })
|
|
60
|
+
),
|
|
61
|
+
steer: Schema.optionalKey(
|
|
62
|
+
Schema.Record(
|
|
63
|
+
OperatorName,
|
|
64
|
+
Schema.Int.check(Schema.isBetween({ minimum: 2, maximum: 1000 }))
|
|
65
|
+
)
|
|
66
|
+
),
|
|
67
|
+
conformance: RepoPath,
|
|
68
|
+
})
|
|
69
|
+
),
|
|
70
|
+
}),
|
|
71
|
+
Schema.Struct({ ...Proof, mode: Schema.Literal("manual") }),
|
|
72
|
+
Schema.Struct({ ...Common, mode: Schema.Literal("library") }),
|
|
73
|
+
]);
|
|
74
|
+
const SpecsFile = Schema.Struct({
|
|
75
|
+
critical: Schema.NonEmptyArray(Schema.NonEmptyString),
|
|
76
|
+
specs: Schema.Record(RepoPath.check(Schema.isPattern(/\.tla$/u)), Entry),
|
|
77
|
+
kani: Schema.Record(
|
|
78
|
+
RepoPath.check(Schema.isPattern(/\.rs$/u)),
|
|
79
|
+
Schema.Struct({ ...Runtime, mode: Schema.Literals(["gate", "manual"]) })
|
|
80
|
+
).pipe(Schema.withDecodingDefaultKey(Effect.succeed({}))),
|
|
81
|
+
});
|
|
82
|
+
type SpecsFile = Omit<typeof SpecsFile.Type, "critical"> & {
|
|
83
|
+
readonly critical: ReadonlyArray<string>;
|
|
84
|
+
};
|
|
85
|
+
export type Analysis =
|
|
86
|
+
| { readonly _tag: "Parsed"; readonly semantics: Semantics }
|
|
87
|
+
| { readonly _tag: "Unparsed"; readonly output: string };
|
|
88
|
+
type ConfigFile = {
|
|
89
|
+
readonly path: string;
|
|
90
|
+
readonly cfg: Cfg | CfgError;
|
|
91
|
+
};
|
|
92
|
+
type Spec = {
|
|
93
|
+
readonly path: string;
|
|
94
|
+
readonly entry: typeof Entry.Type | undefined;
|
|
95
|
+
readonly analysis: Analysis;
|
|
96
|
+
readonly configs: ReadonlyArray<ConfigFile>;
|
|
97
|
+
};
|
|
98
|
+
export type Repo = {
|
|
99
|
+
readonly root: string;
|
|
100
|
+
readonly tracked: ReadonlyArray<string>;
|
|
101
|
+
readonly file: SpecsFile;
|
|
102
|
+
readonly specs: ReadonlyArray<Spec>;
|
|
103
|
+
readonly models: ReadonlyArray<Model>;
|
|
104
|
+
readonly orphans: ReadonlyArray<string>;
|
|
105
|
+
readonly decodeErrors: ReadonlyArray<string>;
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
export const load = Effect.fn("formal.load")(function* (root: string) {
|
|
109
|
+
const fs = yield* FileSystem;
|
|
110
|
+
const path = yield* Path;
|
|
111
|
+
const tla = yield* Tla;
|
|
112
|
+
const git = yield* ChildProcess.make("git", ["ls-files", "-z"], {
|
|
113
|
+
cwd: root,
|
|
114
|
+
stdin: "ignore",
|
|
115
|
+
});
|
|
116
|
+
const [files, code] = yield* Effect.all(
|
|
117
|
+
[git.stdout.pipe(Stream.decodeText, Stream.mkString), git.exitCode],
|
|
118
|
+
{ concurrency: "unbounded" }
|
|
119
|
+
);
|
|
120
|
+
if (Number(code) !== 0)
|
|
121
|
+
return yield* new FormalError({ message: "git ls-files failed" });
|
|
122
|
+
const tracked = yield* Effect.filter(
|
|
123
|
+
files.split("\0").filter(Boolean),
|
|
124
|
+
(file) => fs.exists(path.join(root, file))
|
|
125
|
+
);
|
|
126
|
+
let file: SpecsFile = { critical: [], specs: {}, kani: {} };
|
|
127
|
+
let decodeErrors: ReadonlyArray<string> = [];
|
|
128
|
+
if (yield* fs.exists(path.join(root, "specs.json"))) {
|
|
129
|
+
const raw = yield* fs.readFileString(path.join(root, "specs.json"));
|
|
130
|
+
const decoded = Schema.decodeResult(Schema.fromJsonString(SpecsFile))(raw, {
|
|
131
|
+
errors: "all",
|
|
132
|
+
onExcessProperty: "error",
|
|
133
|
+
});
|
|
134
|
+
if (Result.isSuccess(decoded)) file = decoded.success;
|
|
135
|
+
else
|
|
136
|
+
decodeErrors = SchemaIssue.makeFormatterStandardSchemaV1()(
|
|
137
|
+
decoded.failure.issue
|
|
138
|
+
).issues.map(({ message, path: at = [] }) =>
|
|
139
|
+
at.length === 0
|
|
140
|
+
? message
|
|
141
|
+
: `${at.map((key) => `[${JSON.stringify(typeof key === "object" ? key.key : key)}]`).join("")}: ${message}`
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
const modules = tracked.filter((f) => f.endsWith(".tla"));
|
|
145
|
+
const configs = tracked.filter(
|
|
146
|
+
(f) =>
|
|
147
|
+
f.endsWith(".cfg") &&
|
|
148
|
+
modules.some((m) => path.dirname(m) === path.dirname(f))
|
|
149
|
+
);
|
|
150
|
+
const paired = new Set<string>();
|
|
151
|
+
const specs = yield* Effect.forEach(
|
|
152
|
+
modules,
|
|
153
|
+
(module) =>
|
|
154
|
+
Effect.gen(function* () {
|
|
155
|
+
const stem = module.slice(0, -4);
|
|
156
|
+
const matches = configs.filter((c) => c === `${stem}.cfg`);
|
|
157
|
+
const parsed = yield* Effect.forEach(
|
|
158
|
+
matches,
|
|
159
|
+
(c) =>
|
|
160
|
+
Effect.gen(function* () {
|
|
161
|
+
paired.add(c);
|
|
162
|
+
return {
|
|
163
|
+
path: c,
|
|
164
|
+
cfg: parseCfg(yield* fs.readFileString(path.join(root, c))),
|
|
165
|
+
};
|
|
166
|
+
}),
|
|
167
|
+
{ concurrency: 1 }
|
|
168
|
+
);
|
|
169
|
+
return {
|
|
170
|
+
path: module,
|
|
171
|
+
entry: file.specs[module],
|
|
172
|
+
analysis: yield* tla.analyze(path.join(root, module)),
|
|
173
|
+
configs: parsed,
|
|
174
|
+
};
|
|
175
|
+
}),
|
|
176
|
+
{ concurrency: 4 }
|
|
177
|
+
);
|
|
178
|
+
return {
|
|
179
|
+
root,
|
|
180
|
+
tracked,
|
|
181
|
+
file,
|
|
182
|
+
specs,
|
|
183
|
+
models: specs.flatMap((s) => {
|
|
184
|
+
const c = s.configs[0];
|
|
185
|
+
return s.analysis._tag === "Parsed" &&
|
|
186
|
+
s.entry &&
|
|
187
|
+
s.entry.mode !== "library" &&
|
|
188
|
+
c &&
|
|
189
|
+
!("_tag" in c.cfg)
|
|
190
|
+
? [
|
|
191
|
+
{
|
|
192
|
+
root,
|
|
193
|
+
path: s.path,
|
|
194
|
+
entry: s.entry,
|
|
195
|
+
semantics: s.analysis.semantics,
|
|
196
|
+
config: { path: c.path, cfg: c.cfg },
|
|
197
|
+
},
|
|
198
|
+
]
|
|
199
|
+
: [];
|
|
200
|
+
}),
|
|
201
|
+
orphans: configs.filter((c) => !paired.has(c)),
|
|
202
|
+
decodeErrors,
|
|
203
|
+
} satisfies Repo;
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
export type Model = {
|
|
207
|
+
readonly root: string;
|
|
208
|
+
readonly path: string;
|
|
209
|
+
readonly entry: Exclude<typeof Entry.Type, { readonly mode: "library" }>;
|
|
210
|
+
readonly semantics: Semantics;
|
|
211
|
+
readonly config: { readonly path: string; readonly cfg: Cfg };
|
|
212
|
+
};
|
|
@@ -0,0 +1,306 @@
|
|
|
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
|
+
export 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 mode:
|
|
37
|
+
| { readonly _tag: "Check"; readonly coverage: boolean }
|
|
38
|
+
| {
|
|
39
|
+
readonly _tag: "Simulate";
|
|
40
|
+
readonly num: number;
|
|
41
|
+
readonly depth: number;
|
|
42
|
+
readonly seed: number;
|
|
43
|
+
};
|
|
44
|
+
readonly spec: string;
|
|
45
|
+
readonly config: string;
|
|
46
|
+
readonly timeoutSeconds: number;
|
|
47
|
+
};
|
|
48
|
+
type Formatted =
|
|
49
|
+
| { readonly _tag: "Formatted"; readonly text: string }
|
|
50
|
+
| { readonly _tag: "Failed"; readonly output: string };
|
|
51
|
+
|
|
52
|
+
const pins = [
|
|
53
|
+
{
|
|
54
|
+
file: "tla2tools-1.8.0.jar",
|
|
55
|
+
url: "https://github.com/tlaplus/tlaplus/releases/download/v1.8.0/tla2tools.jar",
|
|
56
|
+
sha256: "e1f11b75c4b5bc4478ba0ac5cb6ae28e40a01b0016bc33e0aa97da22dfdbcd7f",
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
file: "CommunityModules-deps-202611060952.jar",
|
|
60
|
+
url: "https://github.com/tlaplus/CommunityModules/releases/download/202611060952/CommunityModules-deps-202611060952.jar",
|
|
61
|
+
sha256: "a3b27c40f1fb5085f30c6ead37e918457f8cfc62c1e50a1dc668ef592ad2b5fb",
|
|
62
|
+
},
|
|
63
|
+
];
|
|
64
|
+
const error = (cause: unknown) =>
|
|
65
|
+
new FormalError({
|
|
66
|
+
message: cause instanceof Error ? cause.message : String(cause),
|
|
67
|
+
});
|
|
68
|
+
const digest = (bytes: Uint8Array) =>
|
|
69
|
+
createHash("sha256").update(bytes).digest("hex");
|
|
70
|
+
const messages = (output: string): ToolMessage[] =>
|
|
71
|
+
[
|
|
72
|
+
...output.matchAll(
|
|
73
|
+
/@!@!@STARTMSG (\d+):([0-4]) @!@!@([\s\S]*?)@!@!@ENDMSG \1 @!@!@/gu
|
|
74
|
+
),
|
|
75
|
+
].map((match) => ({
|
|
76
|
+
code: Number(match[1]),
|
|
77
|
+
severity: Number(match[2]),
|
|
78
|
+
text: (match[3] ?? "").trim(),
|
|
79
|
+
}));
|
|
80
|
+
const sanyReport = (output: string) =>
|
|
81
|
+
output
|
|
82
|
+
.split("\n")
|
|
83
|
+
.filter(
|
|
84
|
+
(line) =>
|
|
85
|
+
!/^(?:\*{6} SANY2 Version|Parsing file |Semantic processing of module |Linting of module )/u.test(
|
|
86
|
+
line
|
|
87
|
+
)
|
|
88
|
+
)
|
|
89
|
+
.join("\n")
|
|
90
|
+
.trim();
|
|
91
|
+
|
|
92
|
+
const make = Effect.gen(function* () {
|
|
93
|
+
const fs = yield* FileSystem;
|
|
94
|
+
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
|
|
95
|
+
const path = yield* Path;
|
|
96
|
+
const cacheHome = yield* Config.String("XDG_CACHE_HOME").pipe(
|
|
97
|
+
Config.withDefault("")
|
|
98
|
+
);
|
|
99
|
+
const home =
|
|
100
|
+
cacheHome !== ""
|
|
101
|
+
? cacheHome
|
|
102
|
+
: path.join(yield* Config.String("HOME"), ".cache");
|
|
103
|
+
const cache = path.join(home, "tla");
|
|
104
|
+
yield* fs.makeDirectory(cache, { recursive: true });
|
|
105
|
+
const jars: string[] = [];
|
|
106
|
+
for (const pin of pins) {
|
|
107
|
+
const file = path.join(cache, pin.file);
|
|
108
|
+
if (yield* fs.exists(file)) {
|
|
109
|
+
const actual = digest(yield* fs.readFile(file));
|
|
110
|
+
if (actual !== pin.sha256)
|
|
111
|
+
return yield* new FormalError({
|
|
112
|
+
message: `${file} has sha256 ${actual}, expected ${pin.sha256}; delete it to refetch`,
|
|
113
|
+
});
|
|
114
|
+
} else {
|
|
115
|
+
yield* Effect.scoped(
|
|
116
|
+
Effect.gen(function* () {
|
|
117
|
+
const temp = yield* fs.makeTempFileScoped({
|
|
118
|
+
directory: cache,
|
|
119
|
+
prefix: `.${pin.file}.`,
|
|
120
|
+
suffix: ".part",
|
|
121
|
+
});
|
|
122
|
+
const bytes = yield* HttpClient.get(pin.url).pipe(
|
|
123
|
+
Effect.flatMap((response) =>
|
|
124
|
+
response.status === 200
|
|
125
|
+
? response.arrayBuffer.pipe(Effect.mapError(error))
|
|
126
|
+
: Effect.fail(
|
|
127
|
+
new FormalError({
|
|
128
|
+
message: pin.url + ": HTTP " + response.status,
|
|
129
|
+
})
|
|
130
|
+
)
|
|
131
|
+
),
|
|
132
|
+
Effect.map((buffer) => new Uint8Array(buffer)),
|
|
133
|
+
Effect.timeout("120 seconds"),
|
|
134
|
+
Effect.provide(FetchHttpClient.layer)
|
|
135
|
+
);
|
|
136
|
+
yield* fs.writeFile(temp, bytes);
|
|
137
|
+
const actual = digest(bytes);
|
|
138
|
+
if (actual !== pin.sha256)
|
|
139
|
+
return yield* new FormalError({
|
|
140
|
+
message: `${pin.url} has sha256 ${actual}, expected ${pin.sha256}`,
|
|
141
|
+
});
|
|
142
|
+
return yield* fs.rename(temp, file);
|
|
143
|
+
})
|
|
144
|
+
);
|
|
145
|
+
}
|
|
146
|
+
jars.push(file);
|
|
147
|
+
}
|
|
148
|
+
const classpath = jars.join(":");
|
|
149
|
+
const run = Effect.fn("formal.java")(function* (
|
|
150
|
+
args: ReadonlyArray<string>,
|
|
151
|
+
cwd: string,
|
|
152
|
+
seconds = 120
|
|
153
|
+
) {
|
|
154
|
+
return yield* Effect.scoped(
|
|
155
|
+
Effect.gen(function* () {
|
|
156
|
+
const process = yield* ChildProcess.make("java", args, {
|
|
157
|
+
cwd,
|
|
158
|
+
stdin: "ignore",
|
|
159
|
+
killSignal: "SIGKILL",
|
|
160
|
+
});
|
|
161
|
+
return yield* Effect.all(
|
|
162
|
+
[
|
|
163
|
+
process.stdout.pipe(Stream.decodeText, Stream.mkString),
|
|
164
|
+
process.stderr.pipe(Stream.decodeText, Stream.mkString),
|
|
165
|
+
process.exitCode,
|
|
166
|
+
],
|
|
167
|
+
{ concurrency: "unbounded" }
|
|
168
|
+
);
|
|
169
|
+
}).pipe(
|
|
170
|
+
Effect.timeoutOption(`${seconds} seconds`),
|
|
171
|
+
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
|
|
172
|
+
Effect.mapError(error)
|
|
173
|
+
)
|
|
174
|
+
);
|
|
175
|
+
});
|
|
176
|
+
const version = yield* run(["-XshowSettings:properties", "-version"], cache);
|
|
177
|
+
if (
|
|
178
|
+
Option.isNone(version) ||
|
|
179
|
+
Number(version.value[2]) !== 0 ||
|
|
180
|
+
!/^\s*java\.specification\.version = 17\s*$/mu.test(version.value[1])
|
|
181
|
+
)
|
|
182
|
+
return yield* new FormalError({ message: "Java 17 is required on PATH" });
|
|
183
|
+
const invoke = (args: ReadonlyArray<string>, cwd: string, seconds?: number) =>
|
|
184
|
+
run(["-XX:+UseParallelGC", "-cp", classpath, ...args], cwd, seconds);
|
|
185
|
+
return {
|
|
186
|
+
fixCommand: (file: string) =>
|
|
187
|
+
`java -cp ${JSON.stringify(classpath)} formatter.Main ${JSON.stringify(file)} ${JSON.stringify(file)}`,
|
|
188
|
+
analyze: Effect.fn("formal.analyze")(function* (
|
|
189
|
+
file: string
|
|
190
|
+
): Effect.fn.Return<Analysis, FormalError> {
|
|
191
|
+
const sany = yield* invoke(
|
|
192
|
+
[
|
|
193
|
+
"tla2sany.SANY",
|
|
194
|
+
"-error-codes",
|
|
195
|
+
"-messagesAsErrors",
|
|
196
|
+
"4800,4801,4802,4803,4804,4805",
|
|
197
|
+
path.basename(file),
|
|
198
|
+
],
|
|
199
|
+
path.dirname(file)
|
|
200
|
+
);
|
|
201
|
+
if (Option.isNone(sany))
|
|
202
|
+
return { _tag: "Unparsed", output: "SANY timed out" };
|
|
203
|
+
const [stdout, stderr, code] = sany.value;
|
|
204
|
+
if (Number(code) !== 0 || /\*\*\* Warnings:/u.test(stdout + stderr))
|
|
205
|
+
return { _tag: "Unparsed", output: sanyReport(stdout + stderr) };
|
|
206
|
+
const xml = yield* invoke(
|
|
207
|
+
["tla2sany.xml.XMLExporter", "-o", "-t", path.basename(file)],
|
|
208
|
+
path.dirname(file)
|
|
209
|
+
);
|
|
210
|
+
if (Option.isNone(xml))
|
|
211
|
+
return { _tag: "Unparsed", output: "XMLExporter timed out" };
|
|
212
|
+
if (Number(xml.value[2]) !== 0)
|
|
213
|
+
return { _tag: "Unparsed", output: xml.value[0] + xml.value[1] };
|
|
214
|
+
return yield* Effect.try({
|
|
215
|
+
try: () => ({
|
|
216
|
+
_tag: "Parsed" as const,
|
|
217
|
+
semantics: fromXml(xml.value[0]),
|
|
218
|
+
}),
|
|
219
|
+
catch: error,
|
|
220
|
+
});
|
|
221
|
+
}),
|
|
222
|
+
format: Effect.fn("formal.format")(function* (
|
|
223
|
+
file: string
|
|
224
|
+
): Effect.fn.Return<Formatted, FormalError> {
|
|
225
|
+
return yield* Effect.scoped(
|
|
226
|
+
Effect.gen(function* () {
|
|
227
|
+
const temp = yield* fs.makeTempDirectoryScoped({
|
|
228
|
+
prefix: "formal-format-",
|
|
229
|
+
});
|
|
230
|
+
const output = path.join(temp, path.basename(file));
|
|
231
|
+
const result = yield* invoke(
|
|
232
|
+
["formatter.Main", "-v", "ERROR", file, output],
|
|
233
|
+
path.dirname(file)
|
|
234
|
+
);
|
|
235
|
+
if (Option.isNone(result))
|
|
236
|
+
return { _tag: "Failed" as const, output: "formatter timed out" };
|
|
237
|
+
if (Number(result.value[2]) !== 0 || !(yield* fs.exists(output)))
|
|
238
|
+
return {
|
|
239
|
+
_tag: "Failed" as const,
|
|
240
|
+
output: `formatter exited ${result.value[2]}: ${result.value[0]} ${result.value[1]}`,
|
|
241
|
+
};
|
|
242
|
+
return {
|
|
243
|
+
_tag: "Formatted" as const,
|
|
244
|
+
text: yield* fs.readFileString(output),
|
|
245
|
+
};
|
|
246
|
+
})
|
|
247
|
+
).pipe(Effect.mapError(error));
|
|
248
|
+
}),
|
|
249
|
+
tlc: Effect.fn("formal.tlc")(function* (
|
|
250
|
+
input: TlcInput
|
|
251
|
+
): Effect.fn.Return<TlcRun, FormalError> {
|
|
252
|
+
return yield* Effect.scoped(
|
|
253
|
+
Effect.gen(function* () {
|
|
254
|
+
const temp = yield* fs.makeTempDirectoryScoped({
|
|
255
|
+
prefix: "formal-tlc-",
|
|
256
|
+
});
|
|
257
|
+
const started = yield* Clock.currentTimeMillis;
|
|
258
|
+
const result = yield* invoke(
|
|
259
|
+
[
|
|
260
|
+
"tlc2.TLC",
|
|
261
|
+
"-tool",
|
|
262
|
+
"-workers",
|
|
263
|
+
input.mode._tag === "Simulate" ? "1" : "auto",
|
|
264
|
+
...(input.mode._tag === "Simulate"
|
|
265
|
+
? [
|
|
266
|
+
"-simulate",
|
|
267
|
+
`num=${input.mode.num}`,
|
|
268
|
+
"-depth",
|
|
269
|
+
String(input.mode.depth),
|
|
270
|
+
"-seed",
|
|
271
|
+
String(input.mode.seed),
|
|
272
|
+
]
|
|
273
|
+
: [
|
|
274
|
+
"-lncheck",
|
|
275
|
+
"final",
|
|
276
|
+
...(input.mode.coverage ? ["-coverage", "60"] : []),
|
|
277
|
+
]),
|
|
278
|
+
"-metadir",
|
|
279
|
+
temp,
|
|
280
|
+
"-noGenerateSpecTE",
|
|
281
|
+
"-config",
|
|
282
|
+
input.config,
|
|
283
|
+
path.basename(input.spec),
|
|
284
|
+
],
|
|
285
|
+
path.dirname(input.spec),
|
|
286
|
+
input.timeoutSeconds
|
|
287
|
+
);
|
|
288
|
+
const seconds = ((yield* Clock.currentTimeMillis) - started) / 1000;
|
|
289
|
+
if (Option.isNone(result)) return { _tag: "TimedOut" as const };
|
|
290
|
+
return {
|
|
291
|
+
_tag: "Exited" as const,
|
|
292
|
+
seconds,
|
|
293
|
+
code: Number(result.value[2]),
|
|
294
|
+
messages: messages(result.value[0] + result.value[1]),
|
|
295
|
+
};
|
|
296
|
+
})
|
|
297
|
+
).pipe(Effect.mapError(error));
|
|
298
|
+
}),
|
|
299
|
+
};
|
|
300
|
+
}).pipe(Effect.mapError(error));
|
|
301
|
+
|
|
302
|
+
export class Tla extends Context.Service<Tla, Effect.Success<typeof make>>()(
|
|
303
|
+
"formal/Tla"
|
|
304
|
+
) {
|
|
305
|
+
static readonly layer = Layer.effect(Tla, make);
|
|
306
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import * as Schema from "effect/Schema";
|
|
2
|
+
export const FORMAL_TRACES = "FORMAL_TRACES";
|
|
3
|
+
const ObjectValue = Schema.Record(Schema.String, Schema.Json);
|
|
4
|
+
export const Trace = Schema.Struct({
|
|
5
|
+
index: Schema.Int.check(Schema.isGreaterThanOrEqualTo(0)),
|
|
6
|
+
constants: ObjectValue,
|
|
7
|
+
steps: Schema.NonEmptyArray(
|
|
8
|
+
Schema.Struct({
|
|
9
|
+
action: Schema.NonEmptyString,
|
|
10
|
+
args: ObjectValue,
|
|
11
|
+
state: ObjectValue,
|
|
12
|
+
})
|
|
13
|
+
),
|
|
14
|
+
});
|
|
15
|
+
export type Trace = typeof Trace.Type;
|
|
16
|
+
export const receiptPath = (file: string) => `${file}.replayed`;
|
|
17
|
+
export const readTraces = (text: string): ReadonlyArray<Trace> =>
|
|
18
|
+
text
|
|
19
|
+
.trimEnd()
|
|
20
|
+
.split("\n")
|
|
21
|
+
.map((line) =>
|
|
22
|
+
Schema.decodeSync(Schema.fromJsonString(Trace))(line, {
|
|
23
|
+
onExcessProperty: "error",
|
|
24
|
+
})
|
|
25
|
+
);
|
|
26
|
+
export const sorted = (value: Schema.Json): Schema.Json =>
|
|
27
|
+
Array.isArray(value)
|
|
28
|
+
? value.map(sorted)
|
|
29
|
+
: value !== null && typeof value === "object"
|
|
30
|
+
? Object.fromEntries(
|
|
31
|
+
Object.entries(value)
|
|
32
|
+
.toSorted(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))
|
|
33
|
+
.map(([key, v]) => [key, sorted(v)])
|
|
34
|
+
)
|
|
35
|
+
: value;
|