@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
package/dist/verdict.mjs
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
#!/usr/bin/env bun
|
|
2
|
+
import { a as readPullRequest, i as github, n as autoPass } from "./verification-6s2chr4X.mjs";
|
|
3
|
+
import * as Effect from "effect/Effect";
|
|
4
|
+
import { NodeServices } from "@effect/platform-node";
|
|
5
|
+
import * as Cause from "effect/Cause";
|
|
6
|
+
import * as Console from "effect/Console";
|
|
7
|
+
import * as Exit from "effect/Exit";
|
|
8
|
+
// src/verdict.ts
|
|
9
|
+
const checkVerdict = Effect.gen(function* () {
|
|
10
|
+
const request = yield* readPullRequest;
|
|
11
|
+
if (request === void 0) return 0;
|
|
12
|
+
const { octokit, owner, repo, number, sha, pr } = request;
|
|
13
|
+
const reason = autoPass(pr);
|
|
14
|
+
if (reason === void 0) {
|
|
15
|
+
yield* Console.log(`PR #${number} at ${sha} waits for an independent live verdict.`);
|
|
16
|
+
return 0;
|
|
17
|
+
}
|
|
18
|
+
const description = `PASS: auto, ${reason}`.slice(0, 139);
|
|
19
|
+
yield* github(() => octokit.rest.repos.createCommitStatus({
|
|
20
|
+
owner,
|
|
21
|
+
repo,
|
|
22
|
+
sha,
|
|
23
|
+
context: "verdict",
|
|
24
|
+
state: "success",
|
|
25
|
+
description
|
|
26
|
+
}));
|
|
27
|
+
yield* Console.log(`Posted verdict: success on PR #${number} at ${sha}: ${description}`);
|
|
28
|
+
return 0;
|
|
29
|
+
});
|
|
30
|
+
if (import.meta.main) {
|
|
31
|
+
const exit = await Effect.runPromiseExit(checkVerdict.pipe(Effect.provide(NodeServices.layer)));
|
|
32
|
+
if (Exit.isSuccess(exit)) process.exitCode = exit.value;
|
|
33
|
+
else {
|
|
34
|
+
const error = Cause.squash(exit.cause);
|
|
35
|
+
process.stderr.write(`${error instanceof Error ? error.message : String(error)}\n`);
|
|
36
|
+
process.exitCode = 1;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
export { checkVerdict };
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import * as Data from "effect/Data";
|
|
2
|
+
import * as Schema from "effect/Schema";
|
|
3
|
+
import * as Effect from "effect/Effect";
|
|
4
|
+
import { NodeServices } from "@effect/platform-node";
|
|
5
|
+
import * as Cause from "effect/Cause";
|
|
6
|
+
import * as Console from "effect/Console";
|
|
7
|
+
import * as Exit from "effect/Exit";
|
|
8
|
+
import { FileSystem } from "effect/FileSystem";
|
|
9
|
+
import * as Config from "effect/Config";
|
|
10
|
+
import { Octokit } from "@octokit/rest";
|
|
11
|
+
import * as Redacted from "effect/Redacted";
|
|
12
|
+
// src/verification.ts
|
|
13
|
+
const exemptions = [{
|
|
14
|
+
reason: "Renovate PR",
|
|
15
|
+
applies: (pr) => pr.author === "renovate[bot]"
|
|
16
|
+
}, {
|
|
17
|
+
reason: "changes only docs, content or art",
|
|
18
|
+
applies: (pr) => pr.files !== void 0 && pr.files.length > 0 && pr.files.every((file) => !file.split("/").includes("skills") && /^(?:LICENSE.*|.*\.(?:md|mdx|png|jpg|jpeg|gif|webp|avif|svg|ico|mp4|webm|mov|mp3|wav|ogg|woff|woff2|ttf|otf))$/iu.test(file.split("/").at(-1) ?? ""))
|
|
19
|
+
}];
|
|
20
|
+
const autoPass = (pr) => exemptions.find((entry) => entry.applies(pr))?.reason;
|
|
21
|
+
const verdict = (pr) => {
|
|
22
|
+
const exemption = autoPass(pr);
|
|
23
|
+
if (exemption !== void 0) return {
|
|
24
|
+
_tag: "Exempt",
|
|
25
|
+
reason: exemption
|
|
26
|
+
};
|
|
27
|
+
const lines = (pr.body ?? "").replaceAll(/<!--[\s\S]*?-->/gu, "").split(/\r?\n/u);
|
|
28
|
+
const start = lines.findIndex((line) => /^##\s+Verification\s*$/iu.test(line));
|
|
29
|
+
if (start === -1) return {
|
|
30
|
+
_tag: "Unproven",
|
|
31
|
+
reason: "has no ## Verification section"
|
|
32
|
+
};
|
|
33
|
+
const fencedRun = "\0";
|
|
34
|
+
const [section = ""] = lines.slice(start + 1).join("\n").replaceAll(/^ {0,3}(`{3,}|~{3,})[^\n]*\n([\s\S]*?)^ {0,3}\1[ \t]*$/gmu, (_fence, _marker, content) => content.trim() === "" ? "" : fencedRun).split(/^(?:# |## )/mu);
|
|
35
|
+
return section.includes(fencedRun) || /`[^`\s][^`]*`/u.test(section) ? { _tag: "Proven" } : {
|
|
36
|
+
_tag: "Unproven",
|
|
37
|
+
reason: "names no run"
|
|
38
|
+
};
|
|
39
|
+
};
|
|
40
|
+
var VerificationError = class extends Data.TaggedError("VerificationError") {};
|
|
41
|
+
const github = (request) => Effect.tryPromise({
|
|
42
|
+
try: request,
|
|
43
|
+
catch: (cause) => new VerificationError({ message: cause instanceof Error ? cause.message : String(cause) })
|
|
44
|
+
});
|
|
45
|
+
const PullRequestEvent = Schema.fromJsonString(Schema.Struct({
|
|
46
|
+
pull_request: Schema.Struct({
|
|
47
|
+
number: Schema.Finite,
|
|
48
|
+
body: Schema.NullOr(Schema.String),
|
|
49
|
+
user: Schema.Struct({ login: Schema.String }),
|
|
50
|
+
base: Schema.Struct({ sha: Schema.String }),
|
|
51
|
+
head: Schema.Struct({ sha: Schema.String })
|
|
52
|
+
}),
|
|
53
|
+
repository: Schema.Struct({ full_name: Schema.String })
|
|
54
|
+
}));
|
|
55
|
+
const readPullRequest = Effect.gen(function* () {
|
|
56
|
+
if ((yield* Config.String("GITHUB_EVENT_NAME").pipe(Config.withDefault(""))) !== "pull_request") {
|
|
57
|
+
yield* Console.log("Not a pull_request event, so there is no PR to check.");
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
const fs = yield* FileSystem;
|
|
61
|
+
const eventPath = yield* Config.String("GITHUB_EVENT_PATH");
|
|
62
|
+
const payload = yield* fs.readFileString(eventPath);
|
|
63
|
+
const { pull_request: pr, repository } = yield* Schema.decodeEffect(PullRequestEvent)(payload);
|
|
64
|
+
const [, owner, repo] = /^([^/\s]+)\/([^/\s]+)$/u.exec(repository.full_name) ?? [];
|
|
65
|
+
if (owner === void 0 || repo === void 0) return yield* new VerificationError({ message: "Invalid repository.full_name" });
|
|
66
|
+
const token = yield* Config.schema(Schema.Redacted(Schema.NonEmptyString), "GH_TOKEN").pipe(Config.orElse(() => Config.schema(Schema.Redacted(Schema.NonEmptyString), "GITHUB_TOKEN")), Effect.mapError(() => new VerificationError({ message: "Missing GH_TOKEN or GITHUB_TOKEN" })));
|
|
67
|
+
const octokit = new Octokit({ auth: Redacted.value(token) });
|
|
68
|
+
const { data } = yield* github(() => octokit.rest.repos.compareCommitsWithBasehead({
|
|
69
|
+
owner,
|
|
70
|
+
repo,
|
|
71
|
+
basehead: `${pr.base.sha}...${pr.head.sha}`
|
|
72
|
+
}));
|
|
73
|
+
return {
|
|
74
|
+
octokit,
|
|
75
|
+
owner,
|
|
76
|
+
repo,
|
|
77
|
+
number: pr.number,
|
|
78
|
+
sha: pr.head.sha,
|
|
79
|
+
pr: {
|
|
80
|
+
author: pr.user.login,
|
|
81
|
+
body: pr.body,
|
|
82
|
+
files: data.files === void 0 || data.files.length >= 300 ? void 0 : data.files.map((file) => file.filename)
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
});
|
|
86
|
+
const checkVerification = Effect.gen(function* () {
|
|
87
|
+
const request = yield* readPullRequest;
|
|
88
|
+
if (request === void 0) return 0;
|
|
89
|
+
const { number } = request;
|
|
90
|
+
const result = verdict(request.pr);
|
|
91
|
+
if (result._tag === "Exempt") {
|
|
92
|
+
yield* Console.log(`PR #${number} is exempt: ${result.reason}.`);
|
|
93
|
+
return 0;
|
|
94
|
+
}
|
|
95
|
+
if (result._tag === "Proven") {
|
|
96
|
+
yield* Console.log(`PR #${number}'s ## Verification names a run.`);
|
|
97
|
+
return 0;
|
|
98
|
+
}
|
|
99
|
+
yield* Console.error(`PR #${number} ${result.reason}. Add a ## Verification section naming each real run (the command or verifier recipe in backticks) and its outcome, then push again after editing the body: a rerun reads the body from the original event.`);
|
|
100
|
+
return 1;
|
|
101
|
+
});
|
|
102
|
+
if (import.meta.main) {
|
|
103
|
+
const exit = await Effect.runPromiseExit(checkVerification.pipe(Effect.provide(NodeServices.layer)));
|
|
104
|
+
if (Exit.isSuccess(exit)) process.exitCode = exit.value;
|
|
105
|
+
else {
|
|
106
|
+
const error = Cause.squash(exit.cause);
|
|
107
|
+
process.stderr.write(`${error instanceof Error ? error.message : String(error)}\n`);
|
|
108
|
+
process.exitCode = 1;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
export { readPullRequest as a, github as i, autoPass as n, verdict as o, checkVerification as r, VerificationError as t };
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import * as Schema from "effect/Schema";
|
|
2
|
+
import * as Effect from "effect/Effect";
|
|
3
|
+
import * as Cause from "effect/Cause";
|
|
4
|
+
import { FileSystem } from "effect/FileSystem";
|
|
5
|
+
import * as Config from "effect/Config";
|
|
6
|
+
import { Octokit } from "@octokit/rest";
|
|
7
|
+
type PullRequest = {
|
|
8
|
+
readonly author: string;
|
|
9
|
+
readonly body: string | null;
|
|
10
|
+
readonly files: ReadonlyArray<string> | undefined;
|
|
11
|
+
};
|
|
12
|
+
type Verdict = {
|
|
13
|
+
readonly _tag: "Exempt";
|
|
14
|
+
readonly reason: string;
|
|
15
|
+
} | {
|
|
16
|
+
readonly _tag: "Proven";
|
|
17
|
+
} | {
|
|
18
|
+
readonly _tag: "Unproven";
|
|
19
|
+
readonly reason: string;
|
|
20
|
+
};
|
|
21
|
+
declare const autoPass: (pr: PullRequest) => string | undefined;
|
|
22
|
+
declare const verdict: (pr: PullRequest) => Verdict;
|
|
23
|
+
declare const VerificationError_base: new <A extends Record<string, any> = {}>(args: import("effect/Types").VoidIfEmpty<{ readonly [P in keyof A as P extends "_tag" ? never : P]: A[P]; }>) => Cause.YieldableError & {
|
|
24
|
+
readonly _tag: "VerificationError";
|
|
25
|
+
} & Readonly<A>;
|
|
26
|
+
declare class VerificationError extends VerificationError_base<{
|
|
27
|
+
readonly message: string;
|
|
28
|
+
}> {}
|
|
29
|
+
declare const github: <A>(request: () => Promise<A>) => Effect.Effect<A, VerificationError, never>;
|
|
30
|
+
type PullRequestContext = {
|
|
31
|
+
readonly octokit: Octokit;
|
|
32
|
+
readonly owner: string;
|
|
33
|
+
readonly repo: string;
|
|
34
|
+
readonly number: number;
|
|
35
|
+
readonly sha: string;
|
|
36
|
+
readonly pr: PullRequest;
|
|
37
|
+
};
|
|
38
|
+
declare const readPullRequest: Effect.Effect<PullRequestContext | undefined, Config.ConfigError | import("effect/PlatformError").PlatformError | Schema.SchemaError | VerificationError, FileSystem>;
|
|
39
|
+
declare const checkVerification: Effect.Effect<0 | 1, Config.ConfigError | import("effect/PlatformError").PlatformError | Schema.SchemaError | VerificationError, FileSystem>;
|
|
40
|
+
export { autoPass as a, readPullRequest as c, VerificationError as i, verdict as l, PullRequestContext as n, checkVerification as o, Verdict as r, github as s, PullRequest as t };
|
package/dist/verification.d.mts
CHANGED
|
@@ -1,28 +1,2 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
import { FileSystem } from "effect/FileSystem";
|
|
4
|
-
import * as Cause from "effect/Cause";
|
|
5
|
-
import * as Config from "effect/Config";
|
|
6
|
-
export type PullRequest = {
|
|
7
|
-
readonly author: string;
|
|
8
|
-
readonly body: string | null;
|
|
9
|
-
readonly files: ReadonlyArray<string> | undefined;
|
|
10
|
-
};
|
|
11
|
-
export type Verdict = {
|
|
12
|
-
readonly _tag: "Exempt";
|
|
13
|
-
readonly reason: string;
|
|
14
|
-
} | {
|
|
15
|
-
readonly _tag: "Proven";
|
|
16
|
-
} | {
|
|
17
|
-
readonly _tag: "Unproven";
|
|
18
|
-
readonly reason: string;
|
|
19
|
-
};
|
|
20
|
-
export declare const verdict: (pr: PullRequest) => Verdict;
|
|
21
|
-
declare const VerificationError_base: new <A extends Record<string, any> = {}>(args: import("effect/Types").VoidIfEmpty<{ readonly [P in keyof A as P extends "_tag" ? never : P]: A[P]; }>) => Cause.YieldableError & {
|
|
22
|
-
readonly _tag: "VerificationError";
|
|
23
|
-
} & Readonly<A>;
|
|
24
|
-
declare class VerificationError extends VerificationError_base<{
|
|
25
|
-
readonly message: string;
|
|
26
|
-
}> {}
|
|
27
|
-
export declare const checkVerification: Effect.Effect<0 | 1, Config.ConfigError | import("effect/PlatformError").PlatformError | Schema.SchemaError | VerificationError, FileSystem>;
|
|
28
|
-
export {};
|
|
1
|
+
import { a as autoPass, c as readPullRequest, i as VerificationError, l as verdict, n as PullRequestContext, o as checkVerification, r as Verdict, s as github, t as PullRequest } from "./verification-p2rSx5lc.mjs";
|
|
2
|
+
export { PullRequest, PullRequestContext, Verdict, VerificationError, autoPass, checkVerification, github, readPullRequest, verdict };
|
package/dist/verification.mjs
CHANGED
|
@@ -1,98 +1,2 @@
|
|
|
1
1
|
#!/usr/bin/env bun
|
|
2
|
-
|
|
3
|
-
import * as Schema from "effect/Schema";
|
|
4
|
-
import * as Effect from "effect/Effect";
|
|
5
|
-
import { FileSystem } from "effect/FileSystem";
|
|
6
|
-
import { NodeServices } from "@effect/platform-node";
|
|
7
|
-
import { Octokit } from "@octokit/rest";
|
|
8
|
-
import * as Cause from "effect/Cause";
|
|
9
|
-
import * as Config from "effect/Config";
|
|
10
|
-
import * as Exit from "effect/Exit";
|
|
11
|
-
import * as Redacted from "effect/Redacted";
|
|
12
|
-
import * as Console from "effect/Console";
|
|
13
|
-
// src/verification.ts
|
|
14
|
-
const exemptions = [{
|
|
15
|
-
reason: "Renovate PR",
|
|
16
|
-
applies: (pr) => pr.author === "renovate[bot]"
|
|
17
|
-
}, {
|
|
18
|
-
reason: "changes only docs, content or art",
|
|
19
|
-
applies: (pr) => pr.files !== void 0 && pr.files.length > 0 && pr.files.every((file) => /^(?:LICENSE.*|.*\.(?:md|mdx|png|jpg|jpeg|gif|webp|avif|svg|ico|mp4|webm|mov|mp3|wav|ogg|woff|woff2|ttf|otf))$/iu.test(file.split("/").at(-1) ?? ""))
|
|
20
|
-
}];
|
|
21
|
-
const verdict = (pr) => {
|
|
22
|
-
const exemption = exemptions.find((entry) => entry.applies(pr));
|
|
23
|
-
if (exemption !== void 0) return {
|
|
24
|
-
_tag: "Exempt",
|
|
25
|
-
reason: exemption.reason
|
|
26
|
-
};
|
|
27
|
-
const lines = (pr.body ?? "").replaceAll(/<!--[\s\S]*?-->/gu, "").split(/\r?\n/u);
|
|
28
|
-
const start = lines.findIndex((line) => /^##\s+Verification\s*$/iu.test(line));
|
|
29
|
-
if (start === -1) return {
|
|
30
|
-
_tag: "Unproven",
|
|
31
|
-
reason: "has no ## Verification section"
|
|
32
|
-
};
|
|
33
|
-
const fencedRun = "\0";
|
|
34
|
-
const [section = ""] = lines.slice(start + 1).join("\n").replaceAll(/^ {0,3}(`{3,}|~{3,})[^\n]*\n([\s\S]*?)^ {0,3}\1[ \t]*$/gmu, (_fence, _marker, content) => content.trim() === "" ? "" : fencedRun).split(/^(?:# |## )/mu);
|
|
35
|
-
return section.includes(fencedRun) || /`[^`\s][^`]*`/u.test(section) ? { _tag: "Proven" } : {
|
|
36
|
-
_tag: "Unproven",
|
|
37
|
-
reason: "names no run"
|
|
38
|
-
};
|
|
39
|
-
};
|
|
40
|
-
var VerificationError = class extends Data.TaggedError("VerificationError") {};
|
|
41
|
-
const github = (request) => Effect.tryPromise({
|
|
42
|
-
try: request,
|
|
43
|
-
catch: (cause) => new VerificationError({ message: cause instanceof Error ? cause.message : String(cause) })
|
|
44
|
-
});
|
|
45
|
-
const PullRequestEvent = Schema.fromJsonString(Schema.Struct({
|
|
46
|
-
pull_request: Schema.Struct({
|
|
47
|
-
number: Schema.Finite,
|
|
48
|
-
body: Schema.NullOr(Schema.String),
|
|
49
|
-
user: Schema.Struct({ login: Schema.String }),
|
|
50
|
-
base: Schema.Struct({ sha: Schema.String }),
|
|
51
|
-
head: Schema.Struct({ sha: Schema.String })
|
|
52
|
-
}),
|
|
53
|
-
repository: Schema.Struct({ full_name: Schema.String })
|
|
54
|
-
}));
|
|
55
|
-
const checkVerification = Effect.gen(function* () {
|
|
56
|
-
if ((yield* Config.String("GITHUB_EVENT_NAME").pipe(Config.withDefault(""))) !== "pull_request") {
|
|
57
|
-
yield* Console.log("Not a pull_request event, so there is no PR to check.");
|
|
58
|
-
return 0;
|
|
59
|
-
}
|
|
60
|
-
const fs = yield* FileSystem;
|
|
61
|
-
const eventPath = yield* Config.String("GITHUB_EVENT_PATH");
|
|
62
|
-
const payload = yield* fs.readFileString(eventPath);
|
|
63
|
-
const { pull_request: pr, repository } = yield* Schema.decodeEffect(PullRequestEvent)(payload);
|
|
64
|
-
const [, owner, repo] = /^([^/\s]+)\/([^/\s]+)$/u.exec(repository.full_name) ?? [];
|
|
65
|
-
if (owner === void 0 || repo === void 0) return yield* new VerificationError({ message: "Invalid repository.full_name" });
|
|
66
|
-
const token = yield* Config.schema(Schema.Redacted(Schema.NonEmptyString), "GH_TOKEN").pipe(Config.orElse(() => Config.schema(Schema.Redacted(Schema.NonEmptyString), "GITHUB_TOKEN")), Effect.mapError(() => new VerificationError({ message: "Missing GH_TOKEN or GITHUB_TOKEN" })));
|
|
67
|
-
const octokit = new Octokit({ auth: Redacted.value(token) });
|
|
68
|
-
const { data } = yield* github(() => octokit.rest.repos.compareCommitsWithBasehead({
|
|
69
|
-
owner,
|
|
70
|
-
repo,
|
|
71
|
-
basehead: `${pr.base.sha}...${pr.head.sha}`
|
|
72
|
-
}));
|
|
73
|
-
const result = verdict({
|
|
74
|
-
author: pr.user.login,
|
|
75
|
-
body: pr.body,
|
|
76
|
-
files: data.files === void 0 || data.files.length >= 300 ? void 0 : data.files.map((file) => file.filename)
|
|
77
|
-
});
|
|
78
|
-
if (result._tag === "Exempt") {
|
|
79
|
-
yield* Console.log(`PR #${pr.number} is exempt: ${result.reason}.`);
|
|
80
|
-
return 0;
|
|
81
|
-
}
|
|
82
|
-
if (result._tag === "Proven") {
|
|
83
|
-
yield* Console.log(`PR #${pr.number}'s ## Verification names a run.`);
|
|
84
|
-
return 0;
|
|
85
|
-
}
|
|
86
|
-
yield* Console.error(`PR #${pr.number} ${result.reason}. Add a ## Verification section naming each real run (the command or verifier recipe in backticks) and its outcome, then push again after editing the body: a rerun reads the body from the original event.`);
|
|
87
|
-
return 1;
|
|
88
|
-
});
|
|
89
|
-
if (import.meta.main) {
|
|
90
|
-
const exit = await Effect.runPromiseExit(checkVerification.pipe(Effect.provide(NodeServices.layer)));
|
|
91
|
-
if (Exit.isSuccess(exit)) process.exitCode = exit.value;
|
|
92
|
-
else {
|
|
93
|
-
const error = Cause.squash(exit.cause);
|
|
94
|
-
process.stderr.write(`${error instanceof Error ? error.message : String(error)}\n`);
|
|
95
|
-
process.exitCode = 1;
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
export { checkVerification, verdict };
|
|
2
|
+
export { t as VerificationError, n as autoPass, r as checkVerification, i as github, a as readPullRequest, o as verdict } from "./verification-6s2chr4X.mjs";
|
package/package.json
CHANGED
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@milkenjoyer/toolchain",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.11.0",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "git+https://github.com/birbprophet/agent-kit.git",
|
|
7
7
|
"directory": "packages/toolchain"
|
|
8
8
|
},
|
|
9
9
|
"bin": {
|
|
10
|
+
"formal": "./src/formal.ts",
|
|
10
11
|
"proof": "./src/proof.ts",
|
|
12
|
+
"verdict": "./src/verdict.ts",
|
|
11
13
|
"verification": "./src/verification.ts"
|
|
12
14
|
},
|
|
13
15
|
"files": [
|
|
@@ -15,7 +17,10 @@
|
|
|
15
17
|
"licenses",
|
|
16
18
|
"node-version",
|
|
17
19
|
"renovate.json",
|
|
20
|
+
"src/formal.ts",
|
|
21
|
+
"src/formal/",
|
|
18
22
|
"src/proof.ts",
|
|
23
|
+
"src/verdict.ts",
|
|
19
24
|
"src/verification.ts",
|
|
20
25
|
"tsconfig.base.json",
|
|
21
26
|
"vscode/extensions.json",
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
export type CfgConstant =
|
|
2
|
+
| { readonly _tag: "Value"; readonly name: string; readonly value: string }
|
|
3
|
+
| {
|
|
4
|
+
readonly _tag: "Substitution";
|
|
5
|
+
readonly name: string;
|
|
6
|
+
readonly by: string;
|
|
7
|
+
};
|
|
8
|
+
export type Cfg = {
|
|
9
|
+
readonly constants: ReadonlyArray<CfgConstant>;
|
|
10
|
+
readonly invariants: ReadonlyArray<string>;
|
|
11
|
+
readonly checkDeadlock: ReadonlyArray<boolean>;
|
|
12
|
+
};
|
|
13
|
+
export type CfgError = { readonly _tag: "CfgError"; readonly message: string };
|
|
14
|
+
|
|
15
|
+
const keywords = new Set([
|
|
16
|
+
"CONSTANT",
|
|
17
|
+
"CONSTANTS",
|
|
18
|
+
"INIT",
|
|
19
|
+
"NEXT",
|
|
20
|
+
"SPECIFICATION",
|
|
21
|
+
"INVARIANT",
|
|
22
|
+
"INVARIANTS",
|
|
23
|
+
"PROPERTY",
|
|
24
|
+
"PROPERTIES",
|
|
25
|
+
"CONSTRAINT",
|
|
26
|
+
"CONSTRAINTS",
|
|
27
|
+
"ACTION_CONSTRAINT",
|
|
28
|
+
"ACTION_CONSTRAINTS",
|
|
29
|
+
"SYMMETRY",
|
|
30
|
+
"VIEW",
|
|
31
|
+
"ALIAS",
|
|
32
|
+
"POSTCONDITION",
|
|
33
|
+
"CHECK_DEADLOCK",
|
|
34
|
+
]);
|
|
35
|
+
export const parseCfg = (source: string): Cfg | CfgError => {
|
|
36
|
+
const text = source.replaceAll(/\(\*[\s\S]*?\*\)|\\\*[^\n]*/gu, " ");
|
|
37
|
+
const tokens =
|
|
38
|
+
text.match(/"(?:\\.|[^"\\])*"|<-|[A-Za-z_][A-Za-z0-9_!]*|-?\d+|[^\s]/gu) ??
|
|
39
|
+
[];
|
|
40
|
+
const constants: CfgConstant[] = [];
|
|
41
|
+
const invariants: string[] = [];
|
|
42
|
+
const checkDeadlock: boolean[] = [];
|
|
43
|
+
const fail = (message: string): CfgError => ({ _tag: "CfgError", message });
|
|
44
|
+
let i = 0;
|
|
45
|
+
while (i < tokens.length) {
|
|
46
|
+
const keyword = tokens[i++];
|
|
47
|
+
if (keyword === undefined || !keywords.has(keyword))
|
|
48
|
+
return fail(`unknown config keyword ${keyword}`);
|
|
49
|
+
const operands: string[] = [];
|
|
50
|
+
while (i < tokens.length && !keywords.has(tokens[i] ?? ""))
|
|
51
|
+
operands.push(tokens[i++] ?? "");
|
|
52
|
+
if (operands.length === 0) return fail(`missing operand for ${keyword}`);
|
|
53
|
+
if (keyword === "CONSTANT" || keyword === "CONSTANTS") {
|
|
54
|
+
let j = 0;
|
|
55
|
+
while (j < operands.length) {
|
|
56
|
+
const name = operands[j++];
|
|
57
|
+
const assignment = operands[j++];
|
|
58
|
+
if (
|
|
59
|
+
name === undefined ||
|
|
60
|
+
!/^[A-Za-z_][A-Za-z0-9_!]*$/u.test(name) ||
|
|
61
|
+
(assignment !== "=" && assignment !== "<-")
|
|
62
|
+
)
|
|
63
|
+
return fail(`unparseable constant ${name}`);
|
|
64
|
+
const value: string[] = [];
|
|
65
|
+
let depth = 0;
|
|
66
|
+
while (j < operands.length) {
|
|
67
|
+
const token = operands[j] ?? "";
|
|
68
|
+
if (
|
|
69
|
+
depth === 0 &&
|
|
70
|
+
value.length > 0 &&
|
|
71
|
+
["=", "<-"].includes(operands[j + 1] ?? "")
|
|
72
|
+
)
|
|
73
|
+
break;
|
|
74
|
+
if (token === "{") depth++;
|
|
75
|
+
if (token === "}") depth--;
|
|
76
|
+
value.push(token);
|
|
77
|
+
j++;
|
|
78
|
+
}
|
|
79
|
+
const raw = value.join(" ");
|
|
80
|
+
if (
|
|
81
|
+
depth !== 0 ||
|
|
82
|
+
!/^(?:-?\d+|[A-Za-z_][A-Za-z0-9_!]*|"(?:\\.|[^"\\])*"|\{[\s\S]*\})$/u.test(
|
|
83
|
+
raw
|
|
84
|
+
)
|
|
85
|
+
)
|
|
86
|
+
return fail(`unparseable constant ${name} ${assignment} ${raw}`);
|
|
87
|
+
constants.push(
|
|
88
|
+
assignment === "="
|
|
89
|
+
? { _tag: "Value", name, value: raw }
|
|
90
|
+
: { _tag: "Substitution", name, by: raw }
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
} else if (keyword === "CHECK_DEADLOCK") {
|
|
94
|
+
if (
|
|
95
|
+
operands.length !== 1 ||
|
|
96
|
+
!["TRUE", "FALSE"].includes(operands[0] ?? "")
|
|
97
|
+
)
|
|
98
|
+
return fail("CHECK_DEADLOCK needs TRUE or FALSE");
|
|
99
|
+
checkDeadlock.push(operands[0] === "TRUE");
|
|
100
|
+
} else {
|
|
101
|
+
if (!operands.every((token) => /^[A-Za-z_][A-Za-z0-9_!]*$/u.test(token)))
|
|
102
|
+
return fail(`invalid operands for ${keyword}`);
|
|
103
|
+
if (keyword.startsWith("INVARIANT")) invariants.push(...operands);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return { constants, invariants, checkDeadlock };
|
|
107
|
+
};
|
|
108
|
+
export const integer = (constant: CfgConstant): bigint | undefined =>
|
|
109
|
+
constant._tag === "Value" && /^-?\d+$/u.test(constant.value)
|
|
110
|
+
? BigInt(constant.value)
|
|
111
|
+
: undefined;
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
import * as Console from "effect/Console";
|
|
2
|
+
import * as Effect from "effect/Effect";
|
|
3
|
+
import { FileSystem } from "effect/FileSystem";
|
|
4
|
+
|
|
5
|
+
import { integer } from "./cfg.ts";
|
|
6
|
+
import type { Repo } from "./specs.ts";
|
|
7
|
+
import { FormalError, Tla } from "./tla.ts";
|
|
8
|
+
|
|
9
|
+
type Problem = { readonly file: string; readonly message: string };
|
|
10
|
+
type Check = (
|
|
11
|
+
repo: Repo
|
|
12
|
+
) => Effect.Effect<ReadonlyArray<Problem>, FormalError, Tla | FileSystem>;
|
|
13
|
+
export type Finding = Problem & { readonly check: keyof typeof checks };
|
|
14
|
+
|
|
15
|
+
const configs = (repo: Repo) =>
|
|
16
|
+
repo.specs
|
|
17
|
+
.filter((s) => s.analysis._tag === "Parsed")
|
|
18
|
+
.flatMap((s) => s.configs)
|
|
19
|
+
.flatMap((c) => ("_tag" in c.cfg ? [] : [{ path: c.path, cfg: c.cfg }]));
|
|
20
|
+
const specs: Check = (repo) =>
|
|
21
|
+
Effect.succeed([
|
|
22
|
+
...repo.decodeErrors.map((message) => ({ file: "specs.json", message })),
|
|
23
|
+
...repo.specs.flatMap((s) => [
|
|
24
|
+
...(!s.entry && repo.decodeErrors.length === 0
|
|
25
|
+
? [{ file: s.path, message: "tracked spec has no specs.json entry" }]
|
|
26
|
+
: []),
|
|
27
|
+
...(s.entry?.mode === "gate" &&
|
|
28
|
+
!s.configs.some((c) => c.path === s.path.replace(/\.tla$/u, ".cfg"))
|
|
29
|
+
? [{ file: s.path, message: "gate spec has no matching .cfg" }]
|
|
30
|
+
: []),
|
|
31
|
+
...s.configs.flatMap((c) =>
|
|
32
|
+
"_tag" in c.cfg ? [{ file: c.path, message: c.cfg.message }] : []
|
|
33
|
+
),
|
|
34
|
+
]),
|
|
35
|
+
...Object.keys(repo.file)
|
|
36
|
+
.filter((f) => f.endsWith(".tla") && !repo.tracked.includes(f))
|
|
37
|
+
.map((file) => ({ file, message: "entry names no tracked file" })),
|
|
38
|
+
...repo.orphans.map((file) => ({
|
|
39
|
+
file,
|
|
40
|
+
message: "orphan config has no matching module",
|
|
41
|
+
})),
|
|
42
|
+
]);
|
|
43
|
+
const sany: Check = (repo) =>
|
|
44
|
+
Effect.succeed(
|
|
45
|
+
repo.specs.flatMap((s) =>
|
|
46
|
+
s.analysis._tag === "Unparsed"
|
|
47
|
+
? [{ file: s.path, message: s.analysis.output }]
|
|
48
|
+
: []
|
|
49
|
+
)
|
|
50
|
+
);
|
|
51
|
+
const typeok: Check = (repo) =>
|
|
52
|
+
Effect.succeed([
|
|
53
|
+
...configs(repo)
|
|
54
|
+
.filter((c) => !c.cfg.invariants.includes("TypeOK"))
|
|
55
|
+
.map((c) => ({
|
|
56
|
+
file: c.path,
|
|
57
|
+
message: "does not check INVARIANT TypeOK",
|
|
58
|
+
})),
|
|
59
|
+
...repo.specs.flatMap((s) => {
|
|
60
|
+
if (s.analysis._tag !== "Parsed" || s.configs.length === 0) return [];
|
|
61
|
+
const typed = s.analysis.semantics.typeOK;
|
|
62
|
+
if (!typed)
|
|
63
|
+
return s.configs.some(
|
|
64
|
+
(c) => !("_tag" in c.cfg) && c.cfg.invariants.includes("TypeOK")
|
|
65
|
+
)
|
|
66
|
+
? [{ file: s.path, message: "TypeOK is not defined" }]
|
|
67
|
+
: [];
|
|
68
|
+
const missing = s.analysis.semantics.variables.filter(
|
|
69
|
+
(v) => !typed.has(v)
|
|
70
|
+
);
|
|
71
|
+
return missing.length
|
|
72
|
+
? [
|
|
73
|
+
{
|
|
74
|
+
file: s.path,
|
|
75
|
+
message: `TypeOK does not constrain ${missing.join(", ")}; give each a conjunct such as ${missing[0]} \\in S`,
|
|
76
|
+
},
|
|
77
|
+
]
|
|
78
|
+
: [];
|
|
79
|
+
}),
|
|
80
|
+
]);
|
|
81
|
+
const deadlock: Check = (repo) =>
|
|
82
|
+
Effect.succeed(
|
|
83
|
+
configs(repo)
|
|
84
|
+
.filter((c) => c.cfg.checkDeadlock.includes(false))
|
|
85
|
+
.map((c) => ({
|
|
86
|
+
file: c.path,
|
|
87
|
+
message:
|
|
88
|
+
"CHECK_DEADLOCK FALSE; give the bounded spec an explicit finished step",
|
|
89
|
+
}))
|
|
90
|
+
);
|
|
91
|
+
const constants: Check = (repo) =>
|
|
92
|
+
Effect.succeed(
|
|
93
|
+
configs(repo).flatMap((c) => {
|
|
94
|
+
const groups = new Map<bigint, string[]>();
|
|
95
|
+
for (const constant of c.cfg.constants) {
|
|
96
|
+
const n = integer(constant);
|
|
97
|
+
if (n === undefined) continue;
|
|
98
|
+
groups.set(n, [...(groups.get(n) ?? []), constant.name]);
|
|
99
|
+
}
|
|
100
|
+
return [...groups]
|
|
101
|
+
.filter(([, names]) => names.length > 1)
|
|
102
|
+
.map(([n, names]) => ({
|
|
103
|
+
file: c.path,
|
|
104
|
+
message: `${names.length === 2 ? names.join(" and ") : `${names.slice(0, -1).join(", ")} and ${names.at(-1)}`} ${names.length === 2 ? "both" : "all"} equal ${n}`,
|
|
105
|
+
}));
|
|
106
|
+
})
|
|
107
|
+
);
|
|
108
|
+
const format: Check = (repo) =>
|
|
109
|
+
Effect.gen(function* () {
|
|
110
|
+
const tla = yield* Tla;
|
|
111
|
+
const fs = yield* FileSystem;
|
|
112
|
+
return (yield* Effect.forEach(
|
|
113
|
+
repo.specs.filter((s) => s.analysis._tag === "Parsed"),
|
|
114
|
+
(s) =>
|
|
115
|
+
Effect.gen(function* () {
|
|
116
|
+
const result = yield* tla.format(`${repo.root}/${s.path}`);
|
|
117
|
+
if (result._tag === "Failed")
|
|
118
|
+
return [{ file: s.path, message: result.output }];
|
|
119
|
+
const text = yield* fs.readFileString(`${repo.root}/${s.path}`);
|
|
120
|
+
if (text === result.text) return [];
|
|
121
|
+
const lines = text.split("\n");
|
|
122
|
+
const expected = result.text.split("\n");
|
|
123
|
+
let line = 0;
|
|
124
|
+
while (
|
|
125
|
+
line < Math.max(lines.length, expected.length) &&
|
|
126
|
+
lines[line] === expected[line]
|
|
127
|
+
)
|
|
128
|
+
line++;
|
|
129
|
+
return [
|
|
130
|
+
{
|
|
131
|
+
file: s.path,
|
|
132
|
+
message: `differs from formatter.Main at line ${line + 1}; run ${tla.fixCommand(s.path)}`,
|
|
133
|
+
},
|
|
134
|
+
];
|
|
135
|
+
}),
|
|
136
|
+
{ concurrency: 4 }
|
|
137
|
+
)).flat();
|
|
138
|
+
}).pipe(
|
|
139
|
+
Effect.mapError((cause) => new FormalError({ message: String(cause) }))
|
|
140
|
+
);
|
|
141
|
+
const tlc: Check = (repo) =>
|
|
142
|
+
Effect.gen(function* () {
|
|
143
|
+
const tla = yield* Tla;
|
|
144
|
+
const problems: Problem[] = [];
|
|
145
|
+
for (const s of repo.specs) {
|
|
146
|
+
if (s.analysis._tag !== "Parsed" || s.entry?.mode !== "gate") continue;
|
|
147
|
+
const config = s.configs.find(
|
|
148
|
+
(c) => c.path === s.path.replace(/\.tla$/u, ".cfg")
|
|
149
|
+
);
|
|
150
|
+
if (!config || "_tag" in config.cfg) continue;
|
|
151
|
+
const run = yield* tla.tlc({
|
|
152
|
+
spec: `${repo.root}/${s.path}`,
|
|
153
|
+
config: `${repo.root}/${config.path}`,
|
|
154
|
+
timeoutSeconds: s.entry.timeoutSeconds,
|
|
155
|
+
});
|
|
156
|
+
if (run._tag === "TimedOut") {
|
|
157
|
+
problems.push({
|
|
158
|
+
file: config.path,
|
|
159
|
+
message: `TLC timed out after ${s.entry.timeoutSeconds} s`,
|
|
160
|
+
});
|
|
161
|
+
continue;
|
|
162
|
+
}
|
|
163
|
+
const failures = run.messages.filter(
|
|
164
|
+
(m) => m.severity >= 1 && m.severity <= 3
|
|
165
|
+
);
|
|
166
|
+
const states = run.messages
|
|
167
|
+
.find((m) => m.code === 2199)
|
|
168
|
+
?.text.match(/([\d,]+) distinct states/u)?.[1]
|
|
169
|
+
?.replaceAll(",", "");
|
|
170
|
+
if (
|
|
171
|
+
failures.length ||
|
|
172
|
+
run.code !== 0 ||
|
|
173
|
+
!run.messages.some((m) => m.code === 2193) ||
|
|
174
|
+
states === undefined ||
|
|
175
|
+
BigInt(states) === 0n
|
|
176
|
+
) {
|
|
177
|
+
const meaning = new Map([
|
|
178
|
+
[11, "deadlock"],
|
|
179
|
+
[12, "safety violation"],
|
|
180
|
+
[13, "liveness violation"],
|
|
181
|
+
[150, "parse failure"],
|
|
182
|
+
]);
|
|
183
|
+
problems.push({
|
|
184
|
+
file: config.path,
|
|
185
|
+
message: `TLC exited ${run.code}${meaning.has(run.code) ? ` (${meaning.get(run.code)})` : ""}; ${failures.length ? failures.map((m) => `${m.severity === 3 ? "warning" : "error"} ${m.code}: ${m.text}`).join("; ") : "requires 2193 success and nonzero distinct states (2199)"}`,
|
|
186
|
+
});
|
|
187
|
+
} else
|
|
188
|
+
yield* Console.log(
|
|
189
|
+
`tlc: ${config.path}: ${states} distinct states in ${run.seconds} s`
|
|
190
|
+
);
|
|
191
|
+
}
|
|
192
|
+
return problems;
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
export const checks = {
|
|
196
|
+
specs,
|
|
197
|
+
sany,
|
|
198
|
+
typeok,
|
|
199
|
+
deadlock,
|
|
200
|
+
constants,
|
|
201
|
+
format,
|
|
202
|
+
tlc,
|
|
203
|
+
} satisfies Record<string, Check>;
|