@milkenjoyer/toolchain 0.11.0 → 0.13.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 +190 -17
- package/dist/arithmetic.d.mts +45 -0
- package/dist/arithmetic.mjs +25 -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 +131 -3
- package/dist/formal.mjs +1006 -146
- package/dist/guard.mjs +2 -2
- package/dist/knip.mjs +2 -1
- 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 +1 -1
- package/dist/trace-file-KuPRWgwt.mjs +17 -0
- package/dist/verdict.d.mts +1 -1
- package/dist/verdict.mjs +1 -1
- package/dist/{verification-p2rSx5lc.d.mts → verification-FgQFU1YN.d.mts} +1 -1
- package/dist/verification.d.mts +1 -1
- package/dist/verification.mjs +1 -1
- package/package.json +12 -3
- package/src/formal/arithmetic.ts +190 -0
- package/src/formal/cfg.ts +58 -1
- package/src/formal/checks.ts +57 -52
- package/src/formal/coverage.ts +79 -0
- package/src/formal/gate.ts +74 -0
- package/src/formal/git.ts +38 -0
- package/src/formal/proof-run.ts +77 -0
- package/src/formal/proofs.ts +226 -0
- package/src/formal/prover.ts +291 -0
- package/src/formal/semantics.ts +110 -0
- package/src/formal/specs.ts +103 -31
- package/src/formal/tla.ts +60 -19
- 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/z3.ts +321 -0
- package/src/formal.ts +10 -3
- 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-6s2chr4X.mjs → verification-oAc1Q6I9.mjs} +1 -1
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import * as Cache from "effect/Cache";
|
|
2
|
+
import * as Context from "effect/Context";
|
|
3
|
+
import * as Effect from "effect/Effect";
|
|
4
|
+
import * as Layer from "effect/Layer";
|
|
5
|
+
|
|
6
|
+
import { coverage } from "./coverage.ts";
|
|
7
|
+
import type { Coverage } from "./coverage.ts";
|
|
8
|
+
import type { Model } from "./specs.ts";
|
|
9
|
+
import { Tla } from "./tla.ts";
|
|
10
|
+
import type { FormalError } from "./tla.ts";
|
|
11
|
+
type GateResult =
|
|
12
|
+
| {
|
|
13
|
+
readonly _tag: "Passed";
|
|
14
|
+
readonly states: string;
|
|
15
|
+
readonly seconds: number;
|
|
16
|
+
readonly coverage: Coverage;
|
|
17
|
+
}
|
|
18
|
+
| { readonly _tag: "Failed"; readonly message: string };
|
|
19
|
+
const make = Effect.gen(function* () {
|
|
20
|
+
const tla = yield* Tla;
|
|
21
|
+
const run = yield* Cache.make({
|
|
22
|
+
capacity: 10000,
|
|
23
|
+
lookup: Effect.fn("formal.gate")(function* (
|
|
24
|
+
m: Model
|
|
25
|
+
): Effect.fn.Return<GateResult, FormalError> {
|
|
26
|
+
const r = yield* tla.tlc({
|
|
27
|
+
spec: `${m.root}/${m.path}`,
|
|
28
|
+
config: `${m.root}/${m.config.path}`,
|
|
29
|
+
timeoutSeconds: m.entry.timeoutSeconds,
|
|
30
|
+
mode: { _tag: "Check", coverage: true },
|
|
31
|
+
});
|
|
32
|
+
if (r._tag === "TimedOut")
|
|
33
|
+
return {
|
|
34
|
+
_tag: "Failed",
|
|
35
|
+
message: `TLC timed out after ${m.entry.timeoutSeconds} s`,
|
|
36
|
+
};
|
|
37
|
+
const failures = r.messages.filter(
|
|
38
|
+
(x) => x.severity >= 1 && x.severity <= 3
|
|
39
|
+
);
|
|
40
|
+
const states = r.messages
|
|
41
|
+
.find((x) => x.code === 2199)
|
|
42
|
+
?.text.match(/([\d,]+) distinct states/u)?.[1]
|
|
43
|
+
?.replaceAll(",", "");
|
|
44
|
+
if (
|
|
45
|
+
r.code !== 0 ||
|
|
46
|
+
failures.length ||
|
|
47
|
+
!r.messages.some((x) => x.code === 2193) ||
|
|
48
|
+
states === undefined ||
|
|
49
|
+
BigInt(states) === 0n
|
|
50
|
+
) {
|
|
51
|
+
const meaning = new Map([
|
|
52
|
+
[11, "deadlock"],
|
|
53
|
+
[12, "safety violation"],
|
|
54
|
+
[13, "liveness violation"],
|
|
55
|
+
[150, "parse failure"],
|
|
56
|
+
]);
|
|
57
|
+
return {
|
|
58
|
+
_tag: "Failed",
|
|
59
|
+
message: `TLC exited ${r.code}${meaning.has(r.code) ? ` (${meaning.get(r.code)})` : ""}; ${failures.length ? failures.map((x) => `${x.severity === 3 ? "warning" : "error"} ${x.code}: ${x.text}`).join("; ") : "requires 2193 success and nonzero distinct states (2199)"}`,
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
const c = coverage(r.messages);
|
|
63
|
+
return typeof c === "string"
|
|
64
|
+
? { _tag: "Failed", message: c }
|
|
65
|
+
: { _tag: "Passed", states, seconds: r.seconds, coverage: c };
|
|
66
|
+
}),
|
|
67
|
+
});
|
|
68
|
+
return { run: (model: Model) => Cache.get(run, model) };
|
|
69
|
+
});
|
|
70
|
+
export class Gate extends Context.Service<Gate, Effect.Success<typeof make>>()(
|
|
71
|
+
"formal/Gate"
|
|
72
|
+
) {
|
|
73
|
+
static readonly layer = Layer.effect(Gate, make);
|
|
74
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import * as Config from "effect/Config";
|
|
2
|
+
import * as Effect from "effect/Effect";
|
|
3
|
+
import { ChildProcess } from "effect/process";
|
|
4
|
+
import * as Stream from "effect/Stream";
|
|
5
|
+
export const changes = Effect.fn("formal.changes")(function* (root: string) {
|
|
6
|
+
const base = yield* Config.String("BASE").pipe(
|
|
7
|
+
Config.withDefault("origin/main")
|
|
8
|
+
);
|
|
9
|
+
const run = (args: ReadonlyArray<string>) =>
|
|
10
|
+
Effect.scoped(
|
|
11
|
+
Effect.gen(function* () {
|
|
12
|
+
const p = yield* ChildProcess.make("git", args, {
|
|
13
|
+
cwd: root,
|
|
14
|
+
stdin: "ignore",
|
|
15
|
+
});
|
|
16
|
+
const [out, , code] = yield* Effect.all(
|
|
17
|
+
[
|
|
18
|
+
p.stdout.pipe(Stream.decodeText, Stream.mkString),
|
|
19
|
+
p.stderr.pipe(Stream.decodeText, Stream.mkString),
|
|
20
|
+
p.exitCode,
|
|
21
|
+
],
|
|
22
|
+
{ concurrency: "unbounded" }
|
|
23
|
+
);
|
|
24
|
+
return { out, code: Number(code) };
|
|
25
|
+
})
|
|
26
|
+
);
|
|
27
|
+
const merge = yield* run(["merge-base", base, "HEAD"]);
|
|
28
|
+
if (merge.code !== 0) return { _tag: "All" as const, base };
|
|
29
|
+
const sha = merge.out.trim();
|
|
30
|
+
const diff = yield* run(["diff", "--name-only", "-z", sha]);
|
|
31
|
+
return diff.code !== 0
|
|
32
|
+
? { _tag: "All" as const, base }
|
|
33
|
+
: {
|
|
34
|
+
_tag: "Changed" as const,
|
|
35
|
+
sha,
|
|
36
|
+
files: diff.out.split("\0").filter(Boolean),
|
|
37
|
+
};
|
|
38
|
+
});
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import * as Schema from "effect/Schema";
|
|
2
|
+
|
|
3
|
+
const Edit = Schema.Struct({
|
|
4
|
+
file: Schema.String,
|
|
5
|
+
find: Schema.NonEmptyString,
|
|
6
|
+
replace: Schema.String,
|
|
7
|
+
});
|
|
8
|
+
export const Request = Schema.Struct({
|
|
9
|
+
root: Schema.String,
|
|
10
|
+
proof: Schema.String,
|
|
11
|
+
kind: Schema.Literals(["every", "z3"]),
|
|
12
|
+
covers: Schema.Array(Schema.String),
|
|
13
|
+
samples: Schema.Int,
|
|
14
|
+
timeoutSeconds: Schema.Int,
|
|
15
|
+
edit: Schema.optionalKey(Edit),
|
|
16
|
+
result: Schema.String,
|
|
17
|
+
});
|
|
18
|
+
export type Request = typeof Request.Type;
|
|
19
|
+
const At = Schema.Record(Schema.String, Schema.Number);
|
|
20
|
+
export const Outcome = Schema.Union([
|
|
21
|
+
Schema.TaggedStruct("Proved", {
|
|
22
|
+
detail: Schema.String,
|
|
23
|
+
seconds: Schema.Number,
|
|
24
|
+
}),
|
|
25
|
+
Schema.TaggedStruct("Counterexample", { at: At }),
|
|
26
|
+
Schema.TaggedStruct("Disagrees", {
|
|
27
|
+
at: At,
|
|
28
|
+
code: Schema.Unknown,
|
|
29
|
+
model: Schema.Unknown,
|
|
30
|
+
}),
|
|
31
|
+
Schema.TaggedStruct("Throws", { at: At, message: Schema.String }),
|
|
32
|
+
Schema.TaggedStruct("Unknown", {
|
|
33
|
+
reason: Schema.String,
|
|
34
|
+
seconds: Schema.Number,
|
|
35
|
+
}),
|
|
36
|
+
Schema.TaggedStruct("Invalid", { message: Schema.String }),
|
|
37
|
+
]);
|
|
38
|
+
export type Outcome = typeof Outcome.Type;
|
|
39
|
+
export const Result = Schema.Struct({
|
|
40
|
+
loaded: Schema.Array(Schema.String),
|
|
41
|
+
outcome: Outcome,
|
|
42
|
+
});
|
|
43
|
+
export type Result = typeof Result.Type;
|
|
44
|
+
export type Domain = {
|
|
45
|
+
readonly name: string;
|
|
46
|
+
readonly min: number;
|
|
47
|
+
readonly max: number;
|
|
48
|
+
} & (
|
|
49
|
+
| { readonly kind: "int" }
|
|
50
|
+
| { readonly kind: "uint"; readonly bits: number }
|
|
51
|
+
);
|
|
52
|
+
export const invalid = (message: string): Outcome => ({
|
|
53
|
+
_tag: "Invalid",
|
|
54
|
+
message,
|
|
55
|
+
});
|
|
56
|
+
export const message = (error: unknown) =>
|
|
57
|
+
error instanceof Error ? error.message : String(error);
|
|
58
|
+
export const describe = (outcome: Outcome): string => {
|
|
59
|
+
switch (outcome._tag) {
|
|
60
|
+
case "Proved":
|
|
61
|
+
return outcome.detail;
|
|
62
|
+
case "Counterexample":
|
|
63
|
+
return `counterexample at ${JSON.stringify(outcome.at)}`;
|
|
64
|
+
case "Disagrees":
|
|
65
|
+
return `code and model disagree at ${JSON.stringify(outcome.at)}: code ${JSON.stringify(outcome.code)}, model ${JSON.stringify(outcome.model)}`;
|
|
66
|
+
case "Throws":
|
|
67
|
+
return `throws at ${JSON.stringify(outcome.at)}: ${outcome.message}`;
|
|
68
|
+
case "Unknown":
|
|
69
|
+
return `z3 returned unknown: ${outcome.reason}`;
|
|
70
|
+
case "Invalid":
|
|
71
|
+
return outcome.message;
|
|
72
|
+
default: {
|
|
73
|
+
const exhaustive: never = outcome;
|
|
74
|
+
return exhaustive;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
};
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
import { dirname, join } from "node:path";
|
|
2
|
+
|
|
3
|
+
import * as Console from "effect/Console";
|
|
4
|
+
import * as Effect from "effect/Effect";
|
|
5
|
+
import { FileSystem } from "effect/FileSystem";
|
|
6
|
+
|
|
7
|
+
import type { Check } from "./checks.ts";
|
|
8
|
+
import { Gate } from "./gate.ts";
|
|
9
|
+
import { changes } from "./git.ts";
|
|
10
|
+
import { FormalError } from "./tla.ts";
|
|
11
|
+
import { held, violate } from "./violations.ts";
|
|
12
|
+
const matches = (glob: string, file: string) => new Bun.Glob(glob).match(file);
|
|
13
|
+
export const critical: Check = (repo) =>
|
|
14
|
+
Effect.succeed([
|
|
15
|
+
...repo.file.critical.flatMap((glob) =>
|
|
16
|
+
repo.tracked.some((f) => matches(glob, f))
|
|
17
|
+
? []
|
|
18
|
+
: [
|
|
19
|
+
{
|
|
20
|
+
file: "specs.json",
|
|
21
|
+
message: `critical glob ${glob} matches no tracked file`,
|
|
22
|
+
},
|
|
23
|
+
]
|
|
24
|
+
),
|
|
25
|
+
...Object.entries({
|
|
26
|
+
...repo.file.specs,
|
|
27
|
+
...repo.file.kani,
|
|
28
|
+
...repo.file.proofs,
|
|
29
|
+
}).flatMap(([file, e]) =>
|
|
30
|
+
e.covers.flatMap((glob) =>
|
|
31
|
+
repo.tracked.some((f) => matches(glob, f))
|
|
32
|
+
? []
|
|
33
|
+
: [{ file, message: `covers glob ${glob} matches no tracked file` }]
|
|
34
|
+
)
|
|
35
|
+
),
|
|
36
|
+
...repo.tracked.flatMap((file) => {
|
|
37
|
+
const glob = repo.file.critical.find((g) => matches(g, file));
|
|
38
|
+
return glob !== undefined &&
|
|
39
|
+
!Object.values({
|
|
40
|
+
...repo.file.specs,
|
|
41
|
+
...repo.file.kani,
|
|
42
|
+
...repo.file.proofs,
|
|
43
|
+
}).some(
|
|
44
|
+
(e) =>
|
|
45
|
+
(!("mode" in e) || e.mode !== "library") &&
|
|
46
|
+
e.covers.some((g) => matches(g, file))
|
|
47
|
+
)
|
|
48
|
+
? [
|
|
49
|
+
{
|
|
50
|
+
file,
|
|
51
|
+
message: `matches critical glob ${glob} but no spec covers it`,
|
|
52
|
+
},
|
|
53
|
+
]
|
|
54
|
+
: [];
|
|
55
|
+
}),
|
|
56
|
+
]);
|
|
57
|
+
export const fairness: Check = (repo) =>
|
|
58
|
+
Effect.succeed(
|
|
59
|
+
repo.specs.flatMap((s) =>
|
|
60
|
+
s.analysis._tag === "Parsed"
|
|
61
|
+
? s.analysis.semantics.fairness
|
|
62
|
+
.filter((f) => !f.valid)
|
|
63
|
+
.map((f) => ({
|
|
64
|
+
file: s.path,
|
|
65
|
+
message: `${f.kind} at line ${f.line}; fairness must be WF_vars or SF_vars`,
|
|
66
|
+
}))
|
|
67
|
+
: []
|
|
68
|
+
)
|
|
69
|
+
);
|
|
70
|
+
export const coverageCheck: Check = (repo) =>
|
|
71
|
+
Effect.gen(function* () {
|
|
72
|
+
const gate = yield* Gate;
|
|
73
|
+
const fs = yield* FileSystem;
|
|
74
|
+
const problems: { file: string; message: string }[] = [];
|
|
75
|
+
for (const m of repo.models.filter((m) => m.entry.mode === "gate")) {
|
|
76
|
+
const result = yield* gate.run(m);
|
|
77
|
+
if (result._tag === "Failed") continue;
|
|
78
|
+
const start = problems.length;
|
|
79
|
+
for (const a of result.coverage.actions.filter((a) => a.generated === 0n))
|
|
80
|
+
problems.push({
|
|
81
|
+
file: m.path,
|
|
82
|
+
message: `action ${a.name} (line ${a.span.line}) is never enabled`,
|
|
83
|
+
});
|
|
84
|
+
for (const z of result.coverage.zeros) {
|
|
85
|
+
const candidate = join(dirname(m.path), `${z.span.module}.tla`);
|
|
86
|
+
const file = repo.tracked.includes(candidate) ? candidate : m.path;
|
|
87
|
+
const source = (yield* fs.readFileString(`${repo.root}/${file}`))
|
|
88
|
+
.split("\n")
|
|
89
|
+
.slice(z.span.line - 1, z.span.endLine)
|
|
90
|
+
.join("\n")
|
|
91
|
+
.slice(
|
|
92
|
+
z.span.col - 1,
|
|
93
|
+
z.span.endLine === z.span.line ? z.span.endCol : undefined
|
|
94
|
+
)
|
|
95
|
+
.slice(0, 60);
|
|
96
|
+
problems.push({
|
|
97
|
+
file,
|
|
98
|
+
message: `line ${z.span.line}, col ${z.span.col} to line ${z.span.endLine}, col ${z.span.endCol} never runs in ${z.scope}: ${source}`,
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
if (problems.length === start)
|
|
102
|
+
yield* Console.log(`coverage: ${m.path}: no dead logic`);
|
|
103
|
+
}
|
|
104
|
+
return problems;
|
|
105
|
+
}).pipe(Effect.mapError((e) => new FormalError({ message: String(e) })));
|
|
106
|
+
export const witnesses: Check = (repo) =>
|
|
107
|
+
Effect.gen(function* () {
|
|
108
|
+
const gate = yield* Gate;
|
|
109
|
+
const problems: { file: string; message: string }[] = [];
|
|
110
|
+
for (const m of repo.models) {
|
|
111
|
+
for (const name of m.entry.witnesses) {
|
|
112
|
+
const op = m.semantics.operators.get(name);
|
|
113
|
+
const message = !op
|
|
114
|
+
? `${name} is not defined`
|
|
115
|
+
: op.level === 2
|
|
116
|
+
? `${name} is an action; check [][${name}]_vars instead`
|
|
117
|
+
: op.params.length
|
|
118
|
+
? `${name} must have zero arguments`
|
|
119
|
+
: [
|
|
120
|
+
...m.config.cfg.invariants,
|
|
121
|
+
...m.config.cfg.properties,
|
|
122
|
+
].includes(name)
|
|
123
|
+
? `${name} is already checked by ${m.config.path}`
|
|
124
|
+
: undefined;
|
|
125
|
+
if (message !== undefined) {
|
|
126
|
+
problems.push({ file: m.path, message });
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
if (m.entry.mode !== "gate" || (yield* gate.run(m))._tag === "Failed")
|
|
130
|
+
continue;
|
|
131
|
+
const result = yield* violate(repo, m, name);
|
|
132
|
+
if (result !== undefined)
|
|
133
|
+
problems.push({
|
|
134
|
+
file: m.path,
|
|
135
|
+
message:
|
|
136
|
+
result === held
|
|
137
|
+
? `${name} holds; a witness must be violated`
|
|
138
|
+
: result,
|
|
139
|
+
});
|
|
140
|
+
else yield* Console.log(`witnesses: ${m.path}: ${name} violated`);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
return problems;
|
|
144
|
+
}).pipe(Effect.mapError((e) => new FormalError({ message: String(e) })));
|
|
145
|
+
export const breaks: Check = (repo) =>
|
|
146
|
+
Effect.gen(function* () {
|
|
147
|
+
const gate = yield* Gate;
|
|
148
|
+
const fs = yield* FileSystem;
|
|
149
|
+
const changed = yield* changes(repo.root);
|
|
150
|
+
const problems: { file: string; message: string }[] = [];
|
|
151
|
+
if (changed._tag === "All")
|
|
152
|
+
yield* Console.log(
|
|
153
|
+
`breaks: BASE ${changed.base} does not resolve; running every break`
|
|
154
|
+
);
|
|
155
|
+
for (const m of repo.models) {
|
|
156
|
+
const targets = [...m.config.cfg.invariants, ...m.config.cfg.properties];
|
|
157
|
+
for (const target of targets)
|
|
158
|
+
if (!m.entry.breaks.some((b) => b.violates === target))
|
|
159
|
+
problems.push({
|
|
160
|
+
file: m.config.path,
|
|
161
|
+
message: `${target} has no break`,
|
|
162
|
+
});
|
|
163
|
+
const dirty =
|
|
164
|
+
changed._tag === "All" ||
|
|
165
|
+
changed.files.some(
|
|
166
|
+
(f) =>
|
|
167
|
+
f === "specs.json" ||
|
|
168
|
+
(dirname(f) === dirname(m.path) && /\.(?:tla|cfg)$/u.test(f)) ||
|
|
169
|
+
m.entry.breaks.some((b) => b.file === f)
|
|
170
|
+
);
|
|
171
|
+
if (!dirty && m.entry.mode === "gate")
|
|
172
|
+
yield* Console.log(
|
|
173
|
+
`breaks: ${m.path}: unchanged since ${changed.sha.slice(0, 7)}, not run`
|
|
174
|
+
);
|
|
175
|
+
for (const [index, b] of m.entry.breaks.entries()) {
|
|
176
|
+
const label = `break ${index + 1}`;
|
|
177
|
+
const file = b.file ?? m.path;
|
|
178
|
+
if (!targets.includes(b.violates)) {
|
|
179
|
+
problems.push({
|
|
180
|
+
file: m.path,
|
|
181
|
+
message: `${label}: ${b.violates} is not checked by ${m.config.path}`,
|
|
182
|
+
});
|
|
183
|
+
continue;
|
|
184
|
+
}
|
|
185
|
+
if (
|
|
186
|
+
!repo.tracked.includes(file) ||
|
|
187
|
+
!(
|
|
188
|
+
file === m.config.path ||
|
|
189
|
+
(dirname(file) === dirname(m.path) && file.endsWith(".tla"))
|
|
190
|
+
)
|
|
191
|
+
) {
|
|
192
|
+
problems.push({
|
|
193
|
+
file: m.path,
|
|
194
|
+
message: `${label}: ${file} must be a tracked module in the spec directory or its config`,
|
|
195
|
+
});
|
|
196
|
+
continue;
|
|
197
|
+
}
|
|
198
|
+
const text = yield* fs.readFileString(`${repo.root}/${file}`);
|
|
199
|
+
const count = text.split(b.find).length - 1;
|
|
200
|
+
if (count !== 1) {
|
|
201
|
+
problems.push({
|
|
202
|
+
file: m.path,
|
|
203
|
+
message: `${label} find matches ${count} times in ${file}`,
|
|
204
|
+
});
|
|
205
|
+
continue;
|
|
206
|
+
}
|
|
207
|
+
if (
|
|
208
|
+
!dirty ||
|
|
209
|
+
m.entry.mode !== "gate" ||
|
|
210
|
+
(yield* gate.run(m))._tag === "Failed"
|
|
211
|
+
)
|
|
212
|
+
continue;
|
|
213
|
+
const result = yield* violate(repo, m, b.violates, b);
|
|
214
|
+
if (result !== undefined)
|
|
215
|
+
problems.push({
|
|
216
|
+
file: m.path,
|
|
217
|
+
message: `${label} (${b.violates}): ${result === held ? `${b.violates} still holds after the edit` : result}`,
|
|
218
|
+
});
|
|
219
|
+
else
|
|
220
|
+
yield* Console.log(
|
|
221
|
+
`breaks: ${m.path}: ${label} violated ${b.violates}`
|
|
222
|
+
);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
return problems;
|
|
226
|
+
}).pipe(Effect.mapError((e) => new FormalError({ message: String(e) })));
|
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
import { realpathSync } from "node:fs";
|
|
2
|
+
import { extname } from "node:path";
|
|
3
|
+
|
|
4
|
+
import { NodeServices } from "@effect/platform-node";
|
|
5
|
+
import * as Config from "effect/Config";
|
|
6
|
+
import * as Effect from "effect/Effect";
|
|
7
|
+
import { FileSystem } from "effect/FileSystem";
|
|
8
|
+
import * as Schema from "effect/Schema";
|
|
9
|
+
|
|
10
|
+
import { invalid, message, Request, Result } from "./proof-run.ts";
|
|
11
|
+
import type { Domain, Outcome } from "./proof-run.ts";
|
|
12
|
+
import { prove } from "./z3.ts";
|
|
13
|
+
|
|
14
|
+
const Callable = Schema.declare(
|
|
15
|
+
(value: unknown): value is (...args: unknown[]) => unknown =>
|
|
16
|
+
typeof value === "function"
|
|
17
|
+
);
|
|
18
|
+
const Body = Schema.Union([
|
|
19
|
+
Schema.Struct({
|
|
20
|
+
kind: Schema.Literal("every"),
|
|
21
|
+
inputs: Schema.Record(Schema.String, Schema.Unknown),
|
|
22
|
+
holds: Callable,
|
|
23
|
+
}),
|
|
24
|
+
Schema.Struct({
|
|
25
|
+
kind: Schema.Literal("z3"),
|
|
26
|
+
inputs: Schema.Record(Schema.String, Schema.Unknown),
|
|
27
|
+
model: Callable,
|
|
28
|
+
code: Callable,
|
|
29
|
+
holds: Callable,
|
|
30
|
+
assume: Schema.optionalKey(Callable),
|
|
31
|
+
}),
|
|
32
|
+
]);
|
|
33
|
+
const Literal = Schema.Union([
|
|
34
|
+
Schema.Struct({
|
|
35
|
+
kind: Schema.Literal("int"),
|
|
36
|
+
min: Schema.Number,
|
|
37
|
+
max: Schema.Number,
|
|
38
|
+
}),
|
|
39
|
+
Schema.Struct({ kind: Schema.Literal("uint"), bits: Schema.Number }),
|
|
40
|
+
]);
|
|
41
|
+
const JsonBounds = Schema.Struct({
|
|
42
|
+
type: Schema.Literal("integer"),
|
|
43
|
+
minimum: Schema.optionalKey(Schema.Number),
|
|
44
|
+
maximum: Schema.optionalKey(Schema.Number),
|
|
45
|
+
exclusiveMinimum: Schema.optionalKey(Schema.Number),
|
|
46
|
+
exclusiveMaximum: Schema.optionalKey(Schema.Number),
|
|
47
|
+
});
|
|
48
|
+
const enumerate = (
|
|
49
|
+
domains: ReadonlyArray<Domain>,
|
|
50
|
+
holds: (at: Record<string, number>) => unknown,
|
|
51
|
+
deadline: number,
|
|
52
|
+
seconds: number
|
|
53
|
+
): Outcome => {
|
|
54
|
+
const late = (checked: number) =>
|
|
55
|
+
invalid(
|
|
56
|
+
`every checked ${checked} of ${count} inputs before its ${seconds} s budget ran out`
|
|
57
|
+
);
|
|
58
|
+
let checked = 0;
|
|
59
|
+
const count = domains.reduce(
|
|
60
|
+
(n, d) => n * (BigInt(d.max) - BigInt(d.min) + 1n),
|
|
61
|
+
1n
|
|
62
|
+
);
|
|
63
|
+
if (count > 4294967296n)
|
|
64
|
+
return invalid(
|
|
65
|
+
`inputs span ${count} values; every checks at most 4294967296, so prove it with z3`
|
|
66
|
+
);
|
|
67
|
+
const at: Record<string, number> = Object.fromEntries(
|
|
68
|
+
domains.map((d) => [d.name, d.min])
|
|
69
|
+
);
|
|
70
|
+
for (;;) {
|
|
71
|
+
try {
|
|
72
|
+
if (holds(at) !== true) return { _tag: "Counterexample", at };
|
|
73
|
+
} catch (error) {
|
|
74
|
+
return { _tag: "Throws", at, message: message(error) };
|
|
75
|
+
}
|
|
76
|
+
checked++;
|
|
77
|
+
if ((checked & 0xfff) === 0 && performance.now() > deadline)
|
|
78
|
+
return late(checked);
|
|
79
|
+
let i = domains.length - 1;
|
|
80
|
+
while (i >= 0) {
|
|
81
|
+
const d = domains[i];
|
|
82
|
+
if (d && (at[d.name] ?? d.max) < d.max) {
|
|
83
|
+
at[d.name] = (at[d.name] ?? d.min) + 1;
|
|
84
|
+
break;
|
|
85
|
+
}
|
|
86
|
+
if (d) at[d.name] = d.min;
|
|
87
|
+
i--;
|
|
88
|
+
}
|
|
89
|
+
if (i < 0) break;
|
|
90
|
+
}
|
|
91
|
+
if (performance.now() > deadline) return late(checked);
|
|
92
|
+
return {
|
|
93
|
+
_tag: "Proved",
|
|
94
|
+
detail: `checked every one of ${count} inputs`,
|
|
95
|
+
seconds: 0,
|
|
96
|
+
};
|
|
97
|
+
};
|
|
98
|
+
const evaluate = async (
|
|
99
|
+
request: Request,
|
|
100
|
+
paths: ReadonlyArray<string>,
|
|
101
|
+
loaded: Set<string>,
|
|
102
|
+
snapshot: (paths: string[]) => void,
|
|
103
|
+
started: number
|
|
104
|
+
): Promise<Outcome> => {
|
|
105
|
+
let imported: unknown;
|
|
106
|
+
try {
|
|
107
|
+
imported = await import(request.proof);
|
|
108
|
+
} finally {
|
|
109
|
+
snapshot([...loaded]);
|
|
110
|
+
}
|
|
111
|
+
let body: typeof Body.Type;
|
|
112
|
+
try {
|
|
113
|
+
const module = Schema.decodeUnknownSync(
|
|
114
|
+
Schema.Struct({ default: Schema.Unknown })
|
|
115
|
+
)(imported);
|
|
116
|
+
body = Schema.decodeUnknownSync(Body)(module.default);
|
|
117
|
+
} catch {
|
|
118
|
+
return invalid("the default export is not every(...) or z3(...)");
|
|
119
|
+
}
|
|
120
|
+
if (body.kind !== request.kind)
|
|
121
|
+
return invalid(
|
|
122
|
+
`specs.json says ${request.kind} but the file exports ${body.kind}(...)`
|
|
123
|
+
);
|
|
124
|
+
const exports: unknown[] = [];
|
|
125
|
+
for (const file of paths) {
|
|
126
|
+
const covered: unknown = await import(file);
|
|
127
|
+
exports.push(
|
|
128
|
+
...Object.values(
|
|
129
|
+
Schema.decodeUnknownSync(Schema.Record(Schema.String, Schema.Unknown))(
|
|
130
|
+
covered
|
|
131
|
+
)
|
|
132
|
+
)
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
const domains: Domain[] = [];
|
|
136
|
+
for (const [name, input] of Object.entries(body.inputs)) {
|
|
137
|
+
if (body.kind === "z3" && !Schema.isSchema(input)) {
|
|
138
|
+
try {
|
|
139
|
+
const literal = Schema.decodeUnknownSync(Literal)(input);
|
|
140
|
+
if (literal.kind === "uint") {
|
|
141
|
+
if (
|
|
142
|
+
!Number.isInteger(literal.bits) ||
|
|
143
|
+
literal.bits < 1 ||
|
|
144
|
+
literal.bits > 53
|
|
145
|
+
)
|
|
146
|
+
return invalid(
|
|
147
|
+
`input ${name}: uint needs a bit width from 1 to 53`
|
|
148
|
+
);
|
|
149
|
+
domains.push({
|
|
150
|
+
name,
|
|
151
|
+
...literal,
|
|
152
|
+
min: 0,
|
|
153
|
+
max: 2 ** literal.bits - 1,
|
|
154
|
+
});
|
|
155
|
+
} else {
|
|
156
|
+
if (
|
|
157
|
+
!Number.isSafeInteger(literal.min) ||
|
|
158
|
+
!Number.isSafeInteger(literal.max) ||
|
|
159
|
+
literal.min > literal.max
|
|
160
|
+
)
|
|
161
|
+
return invalid(
|
|
162
|
+
`input ${name}: int needs safe integer bounds with min <= max`
|
|
163
|
+
);
|
|
164
|
+
domains.push({ name, ...literal });
|
|
165
|
+
}
|
|
166
|
+
continue;
|
|
167
|
+
} catch {
|
|
168
|
+
return invalid(
|
|
169
|
+
`input ${name}: expected an integer schema, int(...) or uint(...)`
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
if (!exports.includes(input))
|
|
174
|
+
return invalid(
|
|
175
|
+
`input ${name}: its schema is not exported by a file this proof covers, so the range may be narrower than the code enforces`
|
|
176
|
+
);
|
|
177
|
+
let json: unknown = input;
|
|
178
|
+
try {
|
|
179
|
+
if (!Schema.isSchema(input)) throw new Error("not a schema");
|
|
180
|
+
const doc = Schema.toJsonSchemaDocument(Schema.toType(input));
|
|
181
|
+
json = doc.schema;
|
|
182
|
+
const ref = Schema.decodeUnknownOption(
|
|
183
|
+
Schema.Struct({ $ref: Schema.String })
|
|
184
|
+
)(json);
|
|
185
|
+
if (ref._tag === "Some") {
|
|
186
|
+
const key = ref.value.$ref.split("/").at(-1) ?? "";
|
|
187
|
+
json = doc.definitions[key.replaceAll("~1", "/").replaceAll("~0", "~")];
|
|
188
|
+
}
|
|
189
|
+
const bounds = Schema.decodeUnknownSync(JsonBounds)(json);
|
|
190
|
+
const min = Math.max(
|
|
191
|
+
bounds.minimum ?? -Infinity,
|
|
192
|
+
bounds.exclusiveMinimum === undefined
|
|
193
|
+
? -Infinity
|
|
194
|
+
: Math.floor(bounds.exclusiveMinimum) + 1
|
|
195
|
+
);
|
|
196
|
+
const max = Math.min(
|
|
197
|
+
bounds.maximum ?? Infinity,
|
|
198
|
+
bounds.exclusiveMaximum === undefined
|
|
199
|
+
? Infinity
|
|
200
|
+
: Math.ceil(bounds.exclusiveMaximum) - 1
|
|
201
|
+
);
|
|
202
|
+
if (!Number.isSafeInteger(min) || !Number.isSafeInteger(max) || min > max)
|
|
203
|
+
throw new Error("unbounded");
|
|
204
|
+
domains.push({ name, kind: "int", min, max });
|
|
205
|
+
} catch {
|
|
206
|
+
return invalid(
|
|
207
|
+
`input ${name}: every needs an integer schema with a minimum and a maximum, found ${JSON.stringify(json)}`
|
|
208
|
+
);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
return body.kind === "every"
|
|
212
|
+
? enumerate(
|
|
213
|
+
domains,
|
|
214
|
+
body.holds,
|
|
215
|
+
started + request.timeoutSeconds * 1000,
|
|
216
|
+
request.timeoutSeconds
|
|
217
|
+
)
|
|
218
|
+
: await prove(request, body, domains, started);
|
|
219
|
+
};
|
|
220
|
+
const run = Effect.gen(function* () {
|
|
221
|
+
const fs = yield* FileSystem;
|
|
222
|
+
const request = yield* Schema.decodeEffect(Schema.fromJsonString(Request))(
|
|
223
|
+
yield* Config.String("FORMAL_PROOF")
|
|
224
|
+
);
|
|
225
|
+
const started = performance.now();
|
|
226
|
+
const loaded = new Set<string>();
|
|
227
|
+
const paths = request.covers.filter((f) =>
|
|
228
|
+
/\.(?:[cm]?[jt]s|[jt]sx)$/u.test(f)
|
|
229
|
+
);
|
|
230
|
+
Bun.plugin({
|
|
231
|
+
name: "formal-proof",
|
|
232
|
+
setup(build) {
|
|
233
|
+
if (paths.length === 0) return;
|
|
234
|
+
const filter = new RegExp(
|
|
235
|
+
`^(?:${paths.map((p) => p.replace(/[.*+?^${}()|[\]\\]/gu, "\\$&")).join("|")})$`,
|
|
236
|
+
"u"
|
|
237
|
+
);
|
|
238
|
+
build.onLoad({ filter }, async ({ path }) => {
|
|
239
|
+
const file = realpathSync(path);
|
|
240
|
+
if (!request.covers.includes(file)) return undefined;
|
|
241
|
+
loaded.add(file);
|
|
242
|
+
const text = await Bun.file(file).text();
|
|
243
|
+
const extension = extname(file);
|
|
244
|
+
const loader =
|
|
245
|
+
extension === ".tsx"
|
|
246
|
+
? "tsx"
|
|
247
|
+
: extension === ".jsx"
|
|
248
|
+
? "jsx"
|
|
249
|
+
: /\.[cm]?js$/u.test(extension)
|
|
250
|
+
? "js"
|
|
251
|
+
: "ts";
|
|
252
|
+
return {
|
|
253
|
+
contents:
|
|
254
|
+
request.edit?.file === file
|
|
255
|
+
? text.replace(
|
|
256
|
+
request.edit.find,
|
|
257
|
+
() => request.edit?.replace ?? ""
|
|
258
|
+
)
|
|
259
|
+
: text,
|
|
260
|
+
loader,
|
|
261
|
+
};
|
|
262
|
+
});
|
|
263
|
+
},
|
|
264
|
+
});
|
|
265
|
+
let snapshot: string[] = [];
|
|
266
|
+
const outcome = yield* Effect.tryPromise({
|
|
267
|
+
try: () =>
|
|
268
|
+
evaluate(
|
|
269
|
+
request,
|
|
270
|
+
paths,
|
|
271
|
+
loaded,
|
|
272
|
+
(paths) => {
|
|
273
|
+
snapshot = paths;
|
|
274
|
+
},
|
|
275
|
+
started
|
|
276
|
+
),
|
|
277
|
+
catch: (error) => invalid(message(error)),
|
|
278
|
+
}).pipe(Effect.catch((error) => Effect.succeed(error)));
|
|
279
|
+
const timed =
|
|
280
|
+
outcome._tag === "Proved"
|
|
281
|
+
? { ...outcome, seconds: (performance.now() - started) / 1000 }
|
|
282
|
+
: outcome;
|
|
283
|
+
yield* fs.writeFileString(
|
|
284
|
+
request.result,
|
|
285
|
+
yield* Schema.encodeEffect(Schema.fromJsonString(Result))({
|
|
286
|
+
loaded: snapshot,
|
|
287
|
+
outcome: timed,
|
|
288
|
+
})
|
|
289
|
+
);
|
|
290
|
+
});
|
|
291
|
+
await Effect.runPromise(run.pipe(Effect.provide(NodeServices.layer)));
|