@milkenjoyer/toolchain 0.12.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 +95 -6
- package/dist/arithmetic.d.mts +45 -0
- package/dist/arithmetic.mjs +25 -0
- package/dist/formal.mjs +393 -184
- package/dist/knip.mjs +2 -1
- package/package.json +7 -2
- package/src/formal/arithmetic.ts +190 -0
- package/src/formal/checks.ts +19 -1
- package/src/formal/proof-run.ts +77 -0
- package/src/formal/proofs.ts +18 -9
- package/src/formal/prover.ts +291 -0
- package/src/formal/specs.ts +27 -1
- package/src/formal/tla.ts +36 -17
- package/src/formal/z3.ts +321 -0
- package/src/formal.ts +1 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@milkenjoyer/toolchain",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.13.0",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "git+https://github.com/birbprophet/agent-kit.git",
|
|
@@ -28,6 +28,10 @@
|
|
|
28
28
|
],
|
|
29
29
|
"type": "module",
|
|
30
30
|
"exports": {
|
|
31
|
+
"./arithmetic": {
|
|
32
|
+
"types": "./dist/arithmetic.d.mts",
|
|
33
|
+
"default": "./dist/arithmetic.mjs"
|
|
34
|
+
},
|
|
31
35
|
".": {
|
|
32
36
|
"types": "./dist/index.d.mts",
|
|
33
37
|
"default": "./dist/index.mjs"
|
|
@@ -108,7 +112,8 @@
|
|
|
108
112
|
"effect": "4.1.2",
|
|
109
113
|
"effect-oxlint": "0.4.0",
|
|
110
114
|
"semver": "7.8.6",
|
|
111
|
-
"ultracite": "8.1.1"
|
|
115
|
+
"ultracite": "8.1.1",
|
|
116
|
+
"z3-solver": "5.3.3"
|
|
112
117
|
},
|
|
113
118
|
"devDependencies": {
|
|
114
119
|
"@birbprophet/test-kit": "0.0.0",
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
import { join } from "node:path";
|
|
2
|
+
import { fileURLToPath } from "node:url";
|
|
3
|
+
|
|
4
|
+
import * as Console from "effect/Console";
|
|
5
|
+
import * as Effect from "effect/Effect";
|
|
6
|
+
import { FileSystem } from "effect/FileSystem";
|
|
7
|
+
import * as Option from "effect/Option";
|
|
8
|
+
import { ChildProcess } from "effect/process";
|
|
9
|
+
import * as Result from "effect/Result";
|
|
10
|
+
import * as Schema from "effect/Schema";
|
|
11
|
+
import * as Stream from "effect/Stream";
|
|
12
|
+
|
|
13
|
+
import type { Check } from "./checks.ts";
|
|
14
|
+
import { describe, Result as ProofResult } from "./proof-run.ts";
|
|
15
|
+
import { Request } from "./proof-run.ts";
|
|
16
|
+
import { FormalError } from "./tla.ts";
|
|
17
|
+
|
|
18
|
+
const matches = (glob: string, file: string) => new Bun.Glob(glob).match(file);
|
|
19
|
+
const moduleFile = /\.(?:[cm]?[jt]s|[jt]sx)$/u;
|
|
20
|
+
export const arithmetic: Check = (repo) =>
|
|
21
|
+
Effect.gen(function* () {
|
|
22
|
+
if (repo.file.arithmetic === undefined) {
|
|
23
|
+
yield* Console.log(
|
|
24
|
+
"arithmetic: specs.json lists no arithmetic globs; rule 16 is off"
|
|
25
|
+
);
|
|
26
|
+
return [];
|
|
27
|
+
}
|
|
28
|
+
return repo.file.arithmetic.flatMap((glob) => {
|
|
29
|
+
const files = repo.tracked.filter((f) => matches(glob, f));
|
|
30
|
+
return files.length === 0
|
|
31
|
+
? [
|
|
32
|
+
{
|
|
33
|
+
file: "specs.json",
|
|
34
|
+
message: `arithmetic glob ${glob} matches no tracked file`,
|
|
35
|
+
},
|
|
36
|
+
]
|
|
37
|
+
: files
|
|
38
|
+
.filter(
|
|
39
|
+
(f) =>
|
|
40
|
+
!Object.values({ ...repo.file.proofs, ...repo.file.kani }).some(
|
|
41
|
+
(e) => e.covers.some((g) => matches(g, f))
|
|
42
|
+
)
|
|
43
|
+
)
|
|
44
|
+
.map((file) => ({
|
|
45
|
+
file,
|
|
46
|
+
message: `matches arithmetic glob ${glob} but no proof covers it`,
|
|
47
|
+
}));
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
const run = Effect.fn("formal.proof.run")(function* (request: Request) {
|
|
51
|
+
const fs = yield* FileSystem;
|
|
52
|
+
const child = yield* Effect.scoped(
|
|
53
|
+
Effect.gen(function* () {
|
|
54
|
+
const p = yield* ChildProcess.make(
|
|
55
|
+
"bun",
|
|
56
|
+
[fileURLToPath(new URL("./prover.ts", import.meta.url))],
|
|
57
|
+
{
|
|
58
|
+
cwd: request.root,
|
|
59
|
+
env: {
|
|
60
|
+
FORMAL_PROOF: yield* Schema.encodeEffect(
|
|
61
|
+
Schema.fromJsonString(Request)
|
|
62
|
+
)(request),
|
|
63
|
+
},
|
|
64
|
+
extendEnv: true,
|
|
65
|
+
stdin: "ignore",
|
|
66
|
+
killSignal: "SIGKILL",
|
|
67
|
+
}
|
|
68
|
+
);
|
|
69
|
+
return yield* Effect.all(
|
|
70
|
+
[
|
|
71
|
+
p.stdout.pipe(Stream.decodeText, Stream.mkString),
|
|
72
|
+
p.stderr.pipe(Stream.decodeText, Stream.mkString),
|
|
73
|
+
p.exitCode,
|
|
74
|
+
],
|
|
75
|
+
{ concurrency: "unbounded" }
|
|
76
|
+
);
|
|
77
|
+
}).pipe(Effect.timeoutOption(`${request.timeoutSeconds + 30} seconds`))
|
|
78
|
+
);
|
|
79
|
+
if (Option.isNone(child))
|
|
80
|
+
return `timed out after ${request.timeoutSeconds + 30} s`;
|
|
81
|
+
if (!(yield* fs.exists(request.result)))
|
|
82
|
+
return `the proof exited ${child.value[2]} without a result: ${child.value[1].trim().split("\n").at(-1) ?? ""}`;
|
|
83
|
+
const result = Schema.decodeResult(Schema.fromJsonString(ProofResult))(
|
|
84
|
+
yield* fs.readFileString(request.result),
|
|
85
|
+
{ onExcessProperty: "error" }
|
|
86
|
+
);
|
|
87
|
+
return Result.isSuccess(result)
|
|
88
|
+
? result.success
|
|
89
|
+
: "the proof wrote a malformed result";
|
|
90
|
+
});
|
|
91
|
+
export const proofs: Check = (repo) =>
|
|
92
|
+
Effect.scoped(
|
|
93
|
+
Effect.gen(function* () {
|
|
94
|
+
const fs = yield* FileSystem;
|
|
95
|
+
const dir = yield* fs.makeTempDirectoryScoped({
|
|
96
|
+
prefix: "formal-proofs-",
|
|
97
|
+
});
|
|
98
|
+
const problems: { file: string; message: string }[] = [];
|
|
99
|
+
let index = 0;
|
|
100
|
+
for (const [file, entry] of Object.entries(repo.file.proofs).toSorted(
|
|
101
|
+
([a], [b]) => a.localeCompare(b)
|
|
102
|
+
)) {
|
|
103
|
+
if (!repo.tracked.includes(file)) continue;
|
|
104
|
+
const add = (message: string) => problems.push({ file, message });
|
|
105
|
+
const covered = repo.tracked.filter((f) =>
|
|
106
|
+
entry.covers.some((g) => matches(g, f))
|
|
107
|
+
);
|
|
108
|
+
const before = problems.length;
|
|
109
|
+
for (const [i, edit] of entry.breaks.entries()) {
|
|
110
|
+
if (!covered.includes(edit.file)) {
|
|
111
|
+
add(`break ${i + 1}: ${edit.file} is not in this proof's covers`);
|
|
112
|
+
continue;
|
|
113
|
+
}
|
|
114
|
+
const count =
|
|
115
|
+
(yield* fs.readFileString(join(repo.root, edit.file))).split(
|
|
116
|
+
edit.find
|
|
117
|
+
).length - 1;
|
|
118
|
+
if (count !== 1)
|
|
119
|
+
add(`break ${i + 1} find matches ${count} times in ${edit.file}`);
|
|
120
|
+
}
|
|
121
|
+
if (problems.length !== before) continue;
|
|
122
|
+
const covers = yield* Effect.forEach(
|
|
123
|
+
covered,
|
|
124
|
+
(f) => fs.realPath(join(repo.root, f)),
|
|
125
|
+
{ concurrency: 1 }
|
|
126
|
+
);
|
|
127
|
+
const request = {
|
|
128
|
+
root: repo.root,
|
|
129
|
+
proof: join(repo.root, file),
|
|
130
|
+
kind: entry.kind,
|
|
131
|
+
covers,
|
|
132
|
+
samples: entry.kind === "z3" ? entry.samples : 0,
|
|
133
|
+
timeoutSeconds: entry.timeoutSeconds,
|
|
134
|
+
result: join(dir, `${index++}.json`),
|
|
135
|
+
} satisfies Request;
|
|
136
|
+
const baseline = yield* run(request);
|
|
137
|
+
if (typeof baseline === "string") {
|
|
138
|
+
add(baseline);
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
if (baseline.outcome._tag !== "Proved") {
|
|
142
|
+
add(describe(baseline.outcome));
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
covered.forEach((f, i) => {
|
|
146
|
+
if (moduleFile.test(f) && !baseline.loaded.includes(covers[i] ?? ""))
|
|
147
|
+
add(
|
|
148
|
+
`covers ${f} but never loads it; a proof covers only code it runs`
|
|
149
|
+
);
|
|
150
|
+
});
|
|
151
|
+
if (problems.length !== before) continue;
|
|
152
|
+
yield* Console.log(
|
|
153
|
+
`proofs: ${file}: ${baseline.outcome.detail} in ${baseline.outcome.seconds.toFixed(2)} s`
|
|
154
|
+
);
|
|
155
|
+
for (const [i, edit] of entry.breaks.entries()) {
|
|
156
|
+
const absolute = yield* fs.realPath(join(repo.root, edit.file));
|
|
157
|
+
const broken = yield* run({
|
|
158
|
+
...request,
|
|
159
|
+
result: join(dir, `${index++}.json`),
|
|
160
|
+
edit: { ...edit, file: absolute },
|
|
161
|
+
});
|
|
162
|
+
if (typeof broken === "string")
|
|
163
|
+
add(
|
|
164
|
+
`break ${i + 1} (${edit.file}): ${broken}; a break counts only as a counterexample, a mismatch or a throw`
|
|
165
|
+
);
|
|
166
|
+
else if (!broken.loaded.includes(absolute))
|
|
167
|
+
add(`break ${i + 1}: the proof never loads ${edit.file}`);
|
|
168
|
+
else if (broken.outcome._tag === "Proved")
|
|
169
|
+
add(
|
|
170
|
+
`break ${i + 1} (${edit.file}): the proof still holds after the edit`
|
|
171
|
+
);
|
|
172
|
+
else if (
|
|
173
|
+
["Counterexample", "Disagrees", "Throws"].includes(
|
|
174
|
+
broken.outcome._tag
|
|
175
|
+
)
|
|
176
|
+
)
|
|
177
|
+
yield* Console.log(
|
|
178
|
+
`proofs: ${file}: break ${i + 1} ${broken.outcome._tag}`
|
|
179
|
+
);
|
|
180
|
+
else
|
|
181
|
+
add(
|
|
182
|
+
`break ${i + 1} (${edit.file}): ${describe(broken.outcome)}; a break counts only as a counterexample, a mismatch or a throw`
|
|
183
|
+
);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
return problems;
|
|
187
|
+
})
|
|
188
|
+
).pipe(
|
|
189
|
+
Effect.mapError((cause) => new FormalError({ message: String(cause) }))
|
|
190
|
+
);
|
package/src/formal/checks.ts
CHANGED
|
@@ -3,6 +3,7 @@ import * as Effect from "effect/Effect";
|
|
|
3
3
|
import { FileSystem } from "effect/FileSystem";
|
|
4
4
|
import { ChildProcessSpawner } from "effect/process";
|
|
5
5
|
|
|
6
|
+
import { arithmetic, proofs } from "./arithmetic.ts";
|
|
6
7
|
import { integer } from "./cfg.ts";
|
|
7
8
|
import { Gate } from "./gate.ts";
|
|
8
9
|
import {
|
|
@@ -50,7 +51,22 @@ const specs: Check = (repo) =>
|
|
|
50
51
|
"_tag" in c.cfg ? [{ file: c.path, message: c.cfg.message }] : []
|
|
51
52
|
),
|
|
52
53
|
]),
|
|
53
|
-
...
|
|
54
|
+
...repo.tracked
|
|
55
|
+
.filter(
|
|
56
|
+
(f) =>
|
|
57
|
+
f.endsWith(".proof.ts") &&
|
|
58
|
+
!repo.file.proofs[f] &&
|
|
59
|
+
repo.decodeErrors.length === 0
|
|
60
|
+
)
|
|
61
|
+
.map((file) => ({
|
|
62
|
+
file,
|
|
63
|
+
message: "tracked proof has no specs.json entry",
|
|
64
|
+
})),
|
|
65
|
+
...Object.keys({
|
|
66
|
+
...repo.file.specs,
|
|
67
|
+
...repo.file.kani,
|
|
68
|
+
...repo.file.proofs,
|
|
69
|
+
})
|
|
54
70
|
.filter((f) => !repo.tracked.includes(f))
|
|
55
71
|
.map((file) => ({ file, message: "entry names no tracked file" })),
|
|
56
72
|
...repo.orphans.map((file) => ({
|
|
@@ -176,6 +192,7 @@ const tlc: Check = (repo) =>
|
|
|
176
192
|
export const checks = {
|
|
177
193
|
specs,
|
|
178
194
|
critical,
|
|
195
|
+
arithmetic,
|
|
179
196
|
sany,
|
|
180
197
|
typeok,
|
|
181
198
|
deadlock,
|
|
@@ -187,4 +204,5 @@ export const checks = {
|
|
|
187
204
|
witnesses,
|
|
188
205
|
breaks,
|
|
189
206
|
traces,
|
|
207
|
+
proofs,
|
|
190
208
|
} satisfies Record<string, Check>;
|
|
@@ -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
|
+
};
|
package/src/formal/proofs.ts
CHANGED
|
@@ -22,19 +22,28 @@ export const critical: Check = (repo) =>
|
|
|
22
22
|
},
|
|
23
23
|
]
|
|
24
24
|
),
|
|
25
|
-
...Object.entries({
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
)
|
|
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
|
+
)
|
|
32
35
|
),
|
|
33
36
|
...repo.tracked.flatMap((file) => {
|
|
34
37
|
const glob = repo.file.critical.find((g) => matches(g, file));
|
|
35
38
|
return glob !== undefined &&
|
|
36
|
-
!Object.values({
|
|
37
|
-
|
|
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))
|
|
38
47
|
)
|
|
39
48
|
? [
|
|
40
49
|
{
|
|
@@ -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)));
|