@milkenjoyer/toolchain 0.12.0 → 0.14.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 +115 -6
- package/dist/arithmetic.d.mts +45 -0
- package/dist/arithmetic.mjs +25 -0
- package/dist/formal.mjs +369 -273
- package/dist/guard.d.mts +2 -2
- package/dist/guard.mjs +1 -46
- package/dist/knip.mjs +2 -1
- package/dist/manifest-xQwgnYI1.mjs +53 -0
- package/dist/specs-file-NsQF4V7X.mjs +135 -0
- package/dist/standard.d.mts +2 -0
- package/dist/standard.mjs +398 -0
- package/package.json +10 -2
- package/src/formal/arithmetic.ts +190 -0
- package/src/formal/checks.ts +19 -1
- package/src/formal/critical.ts +48 -0
- package/src/formal/proof-run.ts +77 -0
- package/src/formal/proofs.ts +2 -36
- package/src/formal/prover.ts +291 -0
- package/src/formal/specs-file.ts +119 -0
- package/src/formal/specs.ts +3 -87
- package/src/formal/tla.ts +36 -17
- package/src/formal/z3.ts +321 -0
- package/src/formal.ts +1 -1
- package/src/standard/agents.ts +44 -0
- package/src/standard/checks.ts +19 -0
- package/src/standard/config.ts +110 -0
- package/src/standard/formal.ts +93 -0
- package/src/standard/manifest.ts +102 -0
- package/src/standard/plugin-skills.ts +88 -0
- package/src/standard/repo.ts +69 -0
- package/src/standard/toolchain.ts +90 -0
- package/src/standard.ts +71 -0
package/src/formal/z3.ts
ADDED
|
@@ -0,0 +1,321 @@
|
|
|
1
|
+
import { relative } from "node:path";
|
|
2
|
+
|
|
3
|
+
import * as Schema from "effect/Schema";
|
|
4
|
+
import { init, killThreads } from "z3-solver";
|
|
5
|
+
import type { Arith, BitVec, Bool, Context, Expr, Solver } from "z3-solver";
|
|
6
|
+
|
|
7
|
+
import { invalid, message } from "./proof-run.ts";
|
|
8
|
+
import type { Domain, Outcome, Request } from "./proof-run.ts";
|
|
9
|
+
|
|
10
|
+
type Term = Arith | BitVec | Bool;
|
|
11
|
+
type Body = {
|
|
12
|
+
readonly assume?: (...args: unknown[]) => unknown;
|
|
13
|
+
readonly model: (...args: unknown[]) => unknown;
|
|
14
|
+
readonly code: (...args: unknown[]) => unknown;
|
|
15
|
+
readonly holds: (...args: unknown[]) => unknown;
|
|
16
|
+
};
|
|
17
|
+
type Tuple = Record<string, number>;
|
|
18
|
+
const random = (path: string) => {
|
|
19
|
+
let seed = 0xcbf29ce484222325n;
|
|
20
|
+
const mask = (1n << 64n) - 1n;
|
|
21
|
+
for (const byte of new TextEncoder().encode(path))
|
|
22
|
+
seed = ((seed ^ BigInt(byte)) * 0x100000001b3n) & mask;
|
|
23
|
+
return (min: number, max: number) => {
|
|
24
|
+
const span = BigInt(max) - BigInt(min) + 1n;
|
|
25
|
+
const limit = (1n << 64n) - ((1n << 64n) % span);
|
|
26
|
+
for (;;) {
|
|
27
|
+
seed = (seed + 0x9e3779b97f4a7c15n) & mask;
|
|
28
|
+
let z = seed;
|
|
29
|
+
z = ((z ^ (z >> 30n)) * 0xbf58476d1ce4e5b9n) & mask;
|
|
30
|
+
z = ((z ^ (z >> 27n)) * 0x94d049bb133111ebn) & mask;
|
|
31
|
+
z ^= z >> 31n;
|
|
32
|
+
if (z < limit) return Number(BigInt(min) + (z % span));
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
};
|
|
36
|
+
const corners = (domains: ReadonlyArray<Domain>) => {
|
|
37
|
+
let tuples: Tuple[] = [{}];
|
|
38
|
+
for (const d of domains) {
|
|
39
|
+
const values = [...new Set([d.min, d.min + 1, d.max - 1, d.max])].filter(
|
|
40
|
+
(n) => n >= d.min && n <= d.max
|
|
41
|
+
);
|
|
42
|
+
tuples = tuples
|
|
43
|
+
.flatMap((at) => values.map((n) => ({ ...at, [d.name]: n })))
|
|
44
|
+
.slice(0, 1000);
|
|
45
|
+
}
|
|
46
|
+
return tuples;
|
|
47
|
+
};
|
|
48
|
+
const bits = async (
|
|
49
|
+
Z3: Context,
|
|
50
|
+
claims: ReadonlyArray<Bool>
|
|
51
|
+
): Promise<string | undefined> => {
|
|
52
|
+
if (claims.length === 0) return "";
|
|
53
|
+
const parts = claims.map((c) =>
|
|
54
|
+
Z3.If(c, Z3.BitVec.val(1, 1), Z3.BitVec.val(0, 1))
|
|
55
|
+
);
|
|
56
|
+
const first = parts[0];
|
|
57
|
+
if (!first) return "";
|
|
58
|
+
let layer: BitVec[] = parts;
|
|
59
|
+
while (layer.length > 1000) {
|
|
60
|
+
const next: BitVec[] = [];
|
|
61
|
+
for (let i = 0; i < layer.length; i += 1000) {
|
|
62
|
+
const group = layer.slice(i, i + 1000);
|
|
63
|
+
const head = group[0];
|
|
64
|
+
if (head) next.push(group.length === 1 ? head : Z3.Concat(...group));
|
|
65
|
+
}
|
|
66
|
+
layer = next;
|
|
67
|
+
}
|
|
68
|
+
const term = layer.length === 1 ? (layer[0] ?? first) : Z3.Concat(...layer);
|
|
69
|
+
const result = await Z3.simplify(term);
|
|
70
|
+
return Z3.isBitVecVal(result)
|
|
71
|
+
? result.value().toString(2).padStart(parts.length, "0")
|
|
72
|
+
: undefined;
|
|
73
|
+
};
|
|
74
|
+
export const prove = async (
|
|
75
|
+
request: Request,
|
|
76
|
+
body: Body,
|
|
77
|
+
domains: ReadonlyArray<Domain>,
|
|
78
|
+
started: number
|
|
79
|
+
): Promise<Outcome> => {
|
|
80
|
+
const api = await init();
|
|
81
|
+
const makeContext = api.Context;
|
|
82
|
+
const em: unknown = api.em;
|
|
83
|
+
try {
|
|
84
|
+
const Z3 = makeContext("main");
|
|
85
|
+
const symbols: Record<string, Arith | BitVec> = {};
|
|
86
|
+
const domain: Bool[] = [];
|
|
87
|
+
for (const d of domains) {
|
|
88
|
+
if (d.kind === "uint") symbols[d.name] = Z3.BitVec.const(d.name, d.bits);
|
|
89
|
+
else {
|
|
90
|
+
const term = Z3.Int.const(d.name);
|
|
91
|
+
symbols[d.name] = term;
|
|
92
|
+
domain.push(term.ge(d.min), term.le(d.max));
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
const isTerm = (value: unknown): value is Term =>
|
|
96
|
+
Z3.isArith(value) || Z3.isBitVec(value) || Z3.isBool(value);
|
|
97
|
+
const output = body.model(symbols, Z3);
|
|
98
|
+
const claim = body.holds(symbols, output, Z3);
|
|
99
|
+
const assumption = body.assume
|
|
100
|
+
? body.assume(symbols, Z3)
|
|
101
|
+
: Z3.Bool.val(true);
|
|
102
|
+
if (!Z3.isBool(claim) || !Z3.isBool(assumption))
|
|
103
|
+
return invalid("holds and assume must return Bool terms");
|
|
104
|
+
const scalar = Z3.isExpr(output);
|
|
105
|
+
const record = scalar
|
|
106
|
+
? { value: output }
|
|
107
|
+
: Schema.decodeUnknownSync(Schema.Record(Schema.String, Schema.Unknown))(
|
|
108
|
+
output
|
|
109
|
+
);
|
|
110
|
+
const sorts = new Set<string>();
|
|
111
|
+
const seen = new Set<number>();
|
|
112
|
+
const walk = (expr: Expr): void => {
|
|
113
|
+
if (seen.has(expr.id())) return;
|
|
114
|
+
seen.add(expr.id());
|
|
115
|
+
sorts.add(String(expr.sort.name()));
|
|
116
|
+
if (Z3.isApp(expr) || Z3.isQuantifier(expr))
|
|
117
|
+
for (const child of expr.children()) walk(child);
|
|
118
|
+
};
|
|
119
|
+
for (const root of [assumption, claim, ...Object.values(record)])
|
|
120
|
+
if (Z3.isExpr(root)) walk(root);
|
|
121
|
+
const main = new Z3.Solver();
|
|
122
|
+
main.add(...domain, assumption, Z3.Not(claim));
|
|
123
|
+
if (["Real", "FloatingPoint", "RoundingMode"].some((s) => sorts.has(s)))
|
|
124
|
+
return invalid(
|
|
125
|
+
"the claim uses Real or Float arithmetic; money is whole numbers, so state it over Int or BitVec"
|
|
126
|
+
);
|
|
127
|
+
const terms: [string, Term][] = [];
|
|
128
|
+
for (const [key, term] of Object.entries(record)) {
|
|
129
|
+
if (!isTerm(term))
|
|
130
|
+
return invalid(
|
|
131
|
+
"model must return an Int, BitVec, Bool or a record of those terms"
|
|
132
|
+
);
|
|
133
|
+
terms.push([key, term]);
|
|
134
|
+
}
|
|
135
|
+
if (terms.length === 0)
|
|
136
|
+
return invalid("model must return at least one term");
|
|
137
|
+
const substitutions = (at: Tuple): [Expr, Expr][] =>
|
|
138
|
+
domains.map((d) => {
|
|
139
|
+
const symbol = symbols[d.name];
|
|
140
|
+
if (!symbol) throw new Error(`missing symbol ${d.name}`);
|
|
141
|
+
return [
|
|
142
|
+
symbol,
|
|
143
|
+
d.kind === "uint"
|
|
144
|
+
? Z3.BitVec.val(at[d.name] ?? 0, d.bits)
|
|
145
|
+
: Z3.Int.val(at[d.name] ?? 0),
|
|
146
|
+
];
|
|
147
|
+
});
|
|
148
|
+
const valueOf = async (at: Tuple) => {
|
|
149
|
+
const values: Record<string, string | number | boolean> = {};
|
|
150
|
+
for (const [key, term] of terms) {
|
|
151
|
+
const value = await Z3.simplify(
|
|
152
|
+
Z3.substitute(term, ...substitutions(at))
|
|
153
|
+
);
|
|
154
|
+
values[key] =
|
|
155
|
+
Z3.isIntVal(value) || Z3.isBitVecVal(value)
|
|
156
|
+
? Number(value.value())
|
|
157
|
+
: Z3.isTrue(value)
|
|
158
|
+
? true
|
|
159
|
+
: Z3.isFalse(value)
|
|
160
|
+
? false
|
|
161
|
+
: value.sexpr();
|
|
162
|
+
}
|
|
163
|
+
return scalar ? values.value : values;
|
|
164
|
+
};
|
|
165
|
+
const draw = random(relative(request.root, request.proof));
|
|
166
|
+
const corner = corners(domains);
|
|
167
|
+
const accepted: Tuple[] = [];
|
|
168
|
+
let drawn = 0;
|
|
169
|
+
while (accepted.length < request.samples && drawn < 100 * request.samples) {
|
|
170
|
+
const batch: Tuple[] = [];
|
|
171
|
+
const count = Math.min(
|
|
172
|
+
request.samples - accepted.length,
|
|
173
|
+
100 * request.samples - drawn
|
|
174
|
+
);
|
|
175
|
+
for (let i = 0; i < count; i++) {
|
|
176
|
+
batch.push(
|
|
177
|
+
corner[drawn] ??
|
|
178
|
+
Object.fromEntries(domains.map((d) => [d.name, draw(d.min, d.max)]))
|
|
179
|
+
);
|
|
180
|
+
drawn++;
|
|
181
|
+
}
|
|
182
|
+
const selected = body.assume
|
|
183
|
+
? await bits(
|
|
184
|
+
Z3,
|
|
185
|
+
batch.map((at) => {
|
|
186
|
+
const term = Z3.substitute(assumption, ...substitutions(at));
|
|
187
|
+
if (!Z3.isBool(term))
|
|
188
|
+
throw new Error("assume did not produce a Bool");
|
|
189
|
+
return term;
|
|
190
|
+
})
|
|
191
|
+
)
|
|
192
|
+
: "1".repeat(batch.length);
|
|
193
|
+
if (selected === undefined)
|
|
194
|
+
return invalid("assume has no value on the sampled inputs");
|
|
195
|
+
batch.forEach((at, i) => {
|
|
196
|
+
if (selected[i] === "1") accepted.push(at);
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
if (accepted.length < request.samples)
|
|
200
|
+
return invalid(
|
|
201
|
+
`assume accepted ${accepted.length} of ${drawn} drawn inputs; need ${request.samples}`
|
|
202
|
+
);
|
|
203
|
+
const equalities: Bool[] = [];
|
|
204
|
+
const codes: unknown[] = [];
|
|
205
|
+
for (const at of accepted) {
|
|
206
|
+
let code: unknown;
|
|
207
|
+
try {
|
|
208
|
+
code = body.code(at);
|
|
209
|
+
} catch (error) {
|
|
210
|
+
return { _tag: "Throws", at, message: message(error) };
|
|
211
|
+
}
|
|
212
|
+
codes.push(code);
|
|
213
|
+
const fields = Schema.decodeUnknownOption(
|
|
214
|
+
Schema.Record(Schema.String, Schema.Unknown)
|
|
215
|
+
)(code);
|
|
216
|
+
const values = scalar
|
|
217
|
+
? { value: code }
|
|
218
|
+
: fields._tag === "Some"
|
|
219
|
+
? fields.value
|
|
220
|
+
: undefined;
|
|
221
|
+
const eq: Bool[] = [];
|
|
222
|
+
if (values === undefined || Object.keys(values).length !== terms.length)
|
|
223
|
+
return { _tag: "Disagrees", at, code, model: await valueOf(at) };
|
|
224
|
+
for (const [key, term] of terms) {
|
|
225
|
+
const value = values[key];
|
|
226
|
+
const substituted = Z3.substitute(term, ...substitutions(at));
|
|
227
|
+
if (Z3.isBool(substituted) && typeof value === "boolean")
|
|
228
|
+
eq.push(substituted.eq(value));
|
|
229
|
+
else if (
|
|
230
|
+
(Z3.isArith(substituted) || Z3.isBitVec(substituted)) &&
|
|
231
|
+
typeof value === "number" &&
|
|
232
|
+
Number.isSafeInteger(value) &&
|
|
233
|
+
(!Z3.isBitVec(substituted) ||
|
|
234
|
+
(value >= 0 && BigInt(value) < 1n << BigInt(substituted.size())))
|
|
235
|
+
)
|
|
236
|
+
eq.push(
|
|
237
|
+
substituted.eq(
|
|
238
|
+
Z3.isBitVec(substituted)
|
|
239
|
+
? Z3.BitVec.val(value, substituted.size())
|
|
240
|
+
: Z3.Int.val(value)
|
|
241
|
+
)
|
|
242
|
+
);
|
|
243
|
+
else return { _tag: "Disagrees", at, code, model: await valueOf(at) };
|
|
244
|
+
}
|
|
245
|
+
equalities.push(Z3.And(...eq));
|
|
246
|
+
}
|
|
247
|
+
const same = await bits(Z3, equalities);
|
|
248
|
+
if (same === undefined) {
|
|
249
|
+
for (const [i, eq] of equalities.entries()) {
|
|
250
|
+
const value = await Z3.simplify(eq);
|
|
251
|
+
if (!Z3.isTrue(value) && !Z3.isFalse(value))
|
|
252
|
+
return invalid(
|
|
253
|
+
`the model has no value at ${JSON.stringify(accepted[i])}`
|
|
254
|
+
);
|
|
255
|
+
}
|
|
256
|
+
return invalid("the model has no value on the sampled inputs");
|
|
257
|
+
}
|
|
258
|
+
const mismatch = same.indexOf("0");
|
|
259
|
+
const at = accepted[mismatch];
|
|
260
|
+
if (at)
|
|
261
|
+
return {
|
|
262
|
+
_tag: "Disagrees",
|
|
263
|
+
at,
|
|
264
|
+
code: codes[mismatch],
|
|
265
|
+
model: await valueOf(at),
|
|
266
|
+
};
|
|
267
|
+
const fresh: Record<string, Term> = {};
|
|
268
|
+
for (const [key, term] of terms)
|
|
269
|
+
fresh[key] = Z3.isBool(term)
|
|
270
|
+
? Z3.Bool.fresh()
|
|
271
|
+
: Z3.isBitVec(term)
|
|
272
|
+
? Z3.FreshConst(term.sort)
|
|
273
|
+
: Z3.Int.fresh();
|
|
274
|
+
const freshClaim = body.holds(symbols, scalar ? fresh.value : fresh, Z3);
|
|
275
|
+
if (!Z3.isBool(freshClaim)) return invalid("holds must return a Bool term");
|
|
276
|
+
const vacuity = new Z3.Solver();
|
|
277
|
+
vacuity.add(...domain, assumption, Z3.Not(freshClaim));
|
|
278
|
+
const check = async (solver: Solver) => {
|
|
279
|
+
const left =
|
|
280
|
+
request.timeoutSeconds * 1000 - (performance.now() - started);
|
|
281
|
+
if (left <= 0) return "unknown";
|
|
282
|
+
solver.set("timeout", Math.max(1, Math.floor(left)));
|
|
283
|
+
return await solver.check();
|
|
284
|
+
};
|
|
285
|
+
const unknown = (solver: Solver): Outcome => ({
|
|
286
|
+
_tag: "Unknown",
|
|
287
|
+
reason:
|
|
288
|
+
performance.now() - started >= request.timeoutSeconds * 1000
|
|
289
|
+
? "timeout"
|
|
290
|
+
: solver.reasonUnknown(),
|
|
291
|
+
seconds: (performance.now() - started) / 1000,
|
|
292
|
+
});
|
|
293
|
+
const meaningful = await check(vacuity);
|
|
294
|
+
if (meaningful === "unsat")
|
|
295
|
+
return invalid(
|
|
296
|
+
"holds is true for any output, so it says nothing about the code"
|
|
297
|
+
);
|
|
298
|
+
if (meaningful === "unknown") return unknown(vacuity);
|
|
299
|
+
const result = await check(main);
|
|
300
|
+
if (result === "unknown") return unknown(main);
|
|
301
|
+
if (result === "sat") {
|
|
302
|
+
const at: Tuple = {};
|
|
303
|
+
const model = main.model();
|
|
304
|
+
for (const [name, term] of Object.entries(symbols)) {
|
|
305
|
+
const value = model.eval(term, true);
|
|
306
|
+
if (Z3.isIntVal(value) || Z3.isBitVecVal(value))
|
|
307
|
+
at[name] = Number(value.value());
|
|
308
|
+
}
|
|
309
|
+
return { _tag: "Counterexample", at };
|
|
310
|
+
}
|
|
311
|
+
return {
|
|
312
|
+
_tag: "Proved",
|
|
313
|
+
detail: `z3 unsat, code agrees on ${accepted.length} samples`,
|
|
314
|
+
seconds: (performance.now() - started) / 1000,
|
|
315
|
+
};
|
|
316
|
+
} catch (error) {
|
|
317
|
+
return invalid(message(error));
|
|
318
|
+
} finally {
|
|
319
|
+
await killThreads(em);
|
|
320
|
+
}
|
|
321
|
+
};
|
package/src/formal.ts
CHANGED
|
@@ -38,7 +38,7 @@ export const formal = Effect.fn("formal")(function* (root: string) {
|
|
|
38
38
|
);
|
|
39
39
|
} else
|
|
40
40
|
yield* Console.log(
|
|
41
|
-
`formal: ${repo.specs.length} specs, ${repo.specs.reduce((n, s) => n + s.configs.length, 0)} configs, no findings`
|
|
41
|
+
`formal: ${repo.specs.length} specs, ${repo.specs.reduce((n, s) => n + s.configs.length, 0)} configs, ${Object.keys(repo.file.proofs).length} proofs, no findings`
|
|
42
42
|
);
|
|
43
43
|
return findings.length ? 1 : 0;
|
|
44
44
|
});
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import * as Effect from "effect/Effect";
|
|
2
|
+
|
|
3
|
+
import { pluginSkills } from "./plugin-skills.ts";
|
|
4
|
+
import { read, type Rule } from "./repo.ts";
|
|
5
|
+
|
|
6
|
+
export const taskAgents: Rule = (repo) =>
|
|
7
|
+
Effect.gen(function* () {
|
|
8
|
+
const input = yield* read(repo, "AGENTS.md", (text) => text);
|
|
9
|
+
if (!input.ok) return input.findings;
|
|
10
|
+
const lines = input.value.split("\n");
|
|
11
|
+
const start = lines.findIndex((line) => /^## Task agents\s*$/u.test(line));
|
|
12
|
+
const section: string[] = [];
|
|
13
|
+
if (start !== -1)
|
|
14
|
+
for (const line of lines.slice(start + 1)) {
|
|
15
|
+
if (/^#{1,6}\s/u.test(line)) break;
|
|
16
|
+
if (line.trim()) section.push(line.trim());
|
|
17
|
+
}
|
|
18
|
+
return section.length === 1 &&
|
|
19
|
+
section[0]?.includes("`agent-kit:task-agents`") === true
|
|
20
|
+
? []
|
|
21
|
+
: [
|
|
22
|
+
{
|
|
23
|
+
file: "AGENTS.md",
|
|
24
|
+
message:
|
|
25
|
+
"add a ## Task agents section containing only one line pointing to `agent-kit:task-agents`",
|
|
26
|
+
},
|
|
27
|
+
];
|
|
28
|
+
});
|
|
29
|
+
export const skillNames: Rule = (repo) =>
|
|
30
|
+
Effect.succeed(
|
|
31
|
+
repo.tracked.flatMap((file) => {
|
|
32
|
+
const name =
|
|
33
|
+
/^(?:\.agents|\.claude|\.codex)\/skills\/([^/]+)\/SKILL\.md$/u.exec(
|
|
34
|
+
file
|
|
35
|
+
)?.[1];
|
|
36
|
+
return name === undefined
|
|
37
|
+
? []
|
|
38
|
+
: Object.entries(pluginSkills).flatMap(([plugin, names]) =>
|
|
39
|
+
names.includes(name)
|
|
40
|
+
? [{ file, message: `shares its name with ${plugin}:${name}` }]
|
|
41
|
+
: []
|
|
42
|
+
);
|
|
43
|
+
})
|
|
44
|
+
);
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { skillNames, taskAgents } from "./agents.ts";
|
|
2
|
+
import { lintFmt, tsconfig } from "./config.ts";
|
|
3
|
+
import { readyFormal, specs } from "./formal.ts";
|
|
4
|
+
import type { Rule } from "./repo.ts";
|
|
5
|
+
import { exact, lintPackage, nodeVersion, pin, renovate } from "./toolchain.ts";
|
|
6
|
+
|
|
7
|
+
export const rules = {
|
|
8
|
+
"toolchain-pin": pin,
|
|
9
|
+
"lint-package": lintPackage,
|
|
10
|
+
"lint-fmt": lintFmt,
|
|
11
|
+
"tsconfig-base": tsconfig,
|
|
12
|
+
"node-version": nodeVersion,
|
|
13
|
+
"renovate-preset": renovate,
|
|
14
|
+
"exact-versions": exact,
|
|
15
|
+
specs,
|
|
16
|
+
"ready-formal": readyFormal,
|
|
17
|
+
"task-agents": taskAgents,
|
|
18
|
+
"skill-names": skillNames,
|
|
19
|
+
} satisfies Record<string, Rule>;
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import * as Effect from "effect/Effect";
|
|
2
|
+
import { Path } from "effect/Path";
|
|
3
|
+
import * as Schema from "effect/Schema";
|
|
4
|
+
|
|
5
|
+
import { read, type Finding, type Rule } from "./repo.ts";
|
|
6
|
+
|
|
7
|
+
export const lintFmt: Rule = (repo) =>
|
|
8
|
+
Effect.gen(function* () {
|
|
9
|
+
const path = yield* Path;
|
|
10
|
+
const roots = repo.tracked.filter((file) =>
|
|
11
|
+
/^vite\.config\.(?:ts|mts|js|mjs)$/u.test(file)
|
|
12
|
+
);
|
|
13
|
+
if (!roots.length)
|
|
14
|
+
return [
|
|
15
|
+
{
|
|
16
|
+
file: "vite.config.ts",
|
|
17
|
+
message: "track a root vite.config.ts, .mts, .js or .mjs",
|
|
18
|
+
},
|
|
19
|
+
];
|
|
20
|
+
const pending = [...roots];
|
|
21
|
+
const visited = new Set<string>();
|
|
22
|
+
const imports = new Set<string>();
|
|
23
|
+
const findings: Finding[] = [];
|
|
24
|
+
const transpiler = new Bun.Transpiler({ loader: "ts" });
|
|
25
|
+
for (const file of pending) {
|
|
26
|
+
if (visited.has(file)) continue;
|
|
27
|
+
visited.add(file);
|
|
28
|
+
const input = yield* read(repo, file, (text) =>
|
|
29
|
+
transpiler.scanImports(text)
|
|
30
|
+
);
|
|
31
|
+
if (!input.ok) {
|
|
32
|
+
findings.push(...input.findings);
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
for (const item of input.value) {
|
|
36
|
+
imports.add(item.path);
|
|
37
|
+
if (!item.path.startsWith(".")) continue;
|
|
38
|
+
const stem = path.normalize(path.join(path.dirname(file), item.path));
|
|
39
|
+
const candidates = [
|
|
40
|
+
stem,
|
|
41
|
+
stem.replace(/\.(m?)js$/u, ".$1ts"),
|
|
42
|
+
...[".ts", ".mts", ".js", ".mjs"].map((ext) => stem + ext),
|
|
43
|
+
...["ts", "mts", "js", "mjs"].map((ext) => `${stem}/index.${ext}`),
|
|
44
|
+
];
|
|
45
|
+
const target = candidates.find((candidate) =>
|
|
46
|
+
repo.tracked.includes(candidate)
|
|
47
|
+
);
|
|
48
|
+
if (target !== undefined) pending.push(target);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
for (const area of ["lint", "fmt"]) {
|
|
52
|
+
const name = `@milkenjoyer/toolchain/${area}`;
|
|
53
|
+
if (!imports.has(name))
|
|
54
|
+
findings.push({
|
|
55
|
+
file: roots[0] ?? "vite.config.ts",
|
|
56
|
+
message: `import ${name} from the root Vite config or its relative imports`,
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
return findings;
|
|
60
|
+
});
|
|
61
|
+
const Config = Schema.Struct({
|
|
62
|
+
extends: Schema.optionalKey(
|
|
63
|
+
Schema.Union([Schema.String, Schema.Array(Schema.String)])
|
|
64
|
+
),
|
|
65
|
+
});
|
|
66
|
+
export const tsconfig: Rule = (repo) =>
|
|
67
|
+
Effect.gen(function* () {
|
|
68
|
+
const path = yield* Path;
|
|
69
|
+
const pending = ["tsconfig.json"];
|
|
70
|
+
const visited = new Set<string>();
|
|
71
|
+
const findings: Finding[] = [];
|
|
72
|
+
let reached = false;
|
|
73
|
+
for (const file of pending) {
|
|
74
|
+
if (visited.has(file)) continue;
|
|
75
|
+
visited.add(file);
|
|
76
|
+
const input = yield* read(repo, file, (text) =>
|
|
77
|
+
Schema.decodeUnknownSync(Config)(Bun.JSONC.parse(text))
|
|
78
|
+
);
|
|
79
|
+
if (!input.ok) {
|
|
80
|
+
findings.push(...input.findings);
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
const parents =
|
|
84
|
+
typeof input.value.extends === "string"
|
|
85
|
+
? [input.value.extends]
|
|
86
|
+
: (input.value.extends ?? []);
|
|
87
|
+
for (const parent of parents) {
|
|
88
|
+
if (parent === "@milkenjoyer/toolchain/tsconfig.base.json")
|
|
89
|
+
reached = true;
|
|
90
|
+
if (!parent.startsWith(".")) continue;
|
|
91
|
+
const stem = path.normalize(path.join(path.dirname(file), parent));
|
|
92
|
+
const target = [stem, `${stem}.json`].find((candidate) =>
|
|
93
|
+
repo.tracked.includes(candidate)
|
|
94
|
+
);
|
|
95
|
+
if (target !== undefined) pending.push(target);
|
|
96
|
+
else
|
|
97
|
+
findings.push({
|
|
98
|
+
file: stem,
|
|
99
|
+
message: "track the relative tsconfig extends file",
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
if (!reached && !findings.length)
|
|
104
|
+
findings.push({
|
|
105
|
+
file: "tsconfig.json",
|
|
106
|
+
message:
|
|
107
|
+
"extend @milkenjoyer/toolchain/tsconfig.base.json through the extends chain",
|
|
108
|
+
});
|
|
109
|
+
return findings;
|
|
110
|
+
});
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import * as Effect from "effect/Effect";
|
|
2
|
+
import * as Schema from "effect/Schema";
|
|
3
|
+
|
|
4
|
+
import { criticalFindings } from "../formal/critical.ts";
|
|
5
|
+
import { decode } from "../formal/specs-file.ts";
|
|
6
|
+
import { read, type Rule } from "./repo.ts";
|
|
7
|
+
|
|
8
|
+
export const specs: Rule = (repo) =>
|
|
9
|
+
Effect.gen(function* () {
|
|
10
|
+
if (!repo.tracked.includes("specs.json"))
|
|
11
|
+
return [
|
|
12
|
+
{
|
|
13
|
+
file: "specs.json",
|
|
14
|
+
message: "track specs.json with at least one spec entry",
|
|
15
|
+
},
|
|
16
|
+
];
|
|
17
|
+
const input = yield* read(repo, "specs.json", decode);
|
|
18
|
+
if (!input.ok) return input.findings;
|
|
19
|
+
const { file, decodeErrors } = input.value;
|
|
20
|
+
if (decodeErrors.length)
|
|
21
|
+
return decodeErrors.map((message) => ({ file: "specs.json", message }));
|
|
22
|
+
return [
|
|
23
|
+
...(Object.keys(file.specs).length
|
|
24
|
+
? []
|
|
25
|
+
: [{ file: "specs.json", message: "add at least one spec entry" }]),
|
|
26
|
+
...criticalFindings({ tracked: repo.tracked, file }),
|
|
27
|
+
];
|
|
28
|
+
});
|
|
29
|
+
const Gates = {
|
|
30
|
+
if: Schema.optionalKey(Schema.Unknown),
|
|
31
|
+
"continue-on-error": Schema.optionalKey(Schema.Unknown),
|
|
32
|
+
};
|
|
33
|
+
const Workflow = Schema.Struct({
|
|
34
|
+
jobs: Schema.Record(
|
|
35
|
+
Schema.String,
|
|
36
|
+
Schema.Struct({
|
|
37
|
+
...Gates,
|
|
38
|
+
steps: Schema.optionalKey(
|
|
39
|
+
Schema.Array(
|
|
40
|
+
Schema.Struct({ ...Gates, run: Schema.optionalKey(Schema.String) })
|
|
41
|
+
)
|
|
42
|
+
),
|
|
43
|
+
})
|
|
44
|
+
),
|
|
45
|
+
});
|
|
46
|
+
const FALSE = /^\s*(?:\$\{\{\s*false\s*\}\}|false)\s*$/u;
|
|
47
|
+
const enforced = (gates: {
|
|
48
|
+
readonly if?: unknown;
|
|
49
|
+
readonly "continue-on-error"?: unknown;
|
|
50
|
+
}) =>
|
|
51
|
+
gates.if !== false &&
|
|
52
|
+
!(typeof gates.if === "string" && FALSE.test(gates.if)) &&
|
|
53
|
+
(gates["continue-on-error"] === undefined ||
|
|
54
|
+
gates["continue-on-error"] === false ||
|
|
55
|
+
(typeof gates["continue-on-error"] === "string" &&
|
|
56
|
+
FALSE.test(gates["continue-on-error"])));
|
|
57
|
+
export const readyFormal: Rule = (repo) =>
|
|
58
|
+
Effect.gen(function* () {
|
|
59
|
+
const file = [
|
|
60
|
+
".github/workflows/ready.yml",
|
|
61
|
+
".github/workflows/ready.yaml",
|
|
62
|
+
].find((name) => repo.tracked.includes(name));
|
|
63
|
+
if (file === undefined)
|
|
64
|
+
return [
|
|
65
|
+
{
|
|
66
|
+
file: ".github/workflows/ready.yml",
|
|
67
|
+
message: "track a ready workflow running vp exec formal",
|
|
68
|
+
},
|
|
69
|
+
];
|
|
70
|
+
const input = yield* read(repo, file, (text) =>
|
|
71
|
+
Schema.decodeUnknownSync(Workflow)(Bun.YAML.parse(text))
|
|
72
|
+
);
|
|
73
|
+
if (!input.ok) return input.findings;
|
|
74
|
+
return Object.values(input.value.jobs).some(
|
|
75
|
+
(job) =>
|
|
76
|
+
enforced(job) &&
|
|
77
|
+
(job.steps ?? []).some(
|
|
78
|
+
(step) =>
|
|
79
|
+
enforced(step) &&
|
|
80
|
+
(step.run ?? "")
|
|
81
|
+
.split("\n")
|
|
82
|
+
.some((line) => line.trim() === "vp exec formal")
|
|
83
|
+
)
|
|
84
|
+
)
|
|
85
|
+
? []
|
|
86
|
+
: [
|
|
87
|
+
{
|
|
88
|
+
file,
|
|
89
|
+
message:
|
|
90
|
+
"add a step with a vp exec formal line that no if: false or continue-on-error skips or ignores",
|
|
91
|
+
},
|
|
92
|
+
];
|
|
93
|
+
});
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import * as Arr from "effect/Array";
|
|
2
|
+
import * as Effect from "effect/Effect";
|
|
3
|
+
import { dual } from "effect/Function";
|
|
4
|
+
import * as R from "effect/Record";
|
|
5
|
+
import * as Result from "effect/Result";
|
|
6
|
+
import * as Schema from "effect/Schema";
|
|
7
|
+
|
|
8
|
+
const Versions = Schema.Record(Schema.String, Schema.String).pipe(
|
|
9
|
+
Schema.withDecodingDefaultKey(Effect.succeed({}))
|
|
10
|
+
);
|
|
11
|
+
|
|
12
|
+
const Catalogs = Schema.Record(
|
|
13
|
+
Schema.String,
|
|
14
|
+
Schema.Record(Schema.String, Schema.String)
|
|
15
|
+
).pipe(Schema.withDecodingDefaultKey(Effect.succeed({})));
|
|
16
|
+
|
|
17
|
+
const WorkspaceList = Schema.Array(Schema.String);
|
|
18
|
+
|
|
19
|
+
const WorkspaceCatalogs = Schema.Struct({
|
|
20
|
+
catalog: Versions,
|
|
21
|
+
catalogs: Catalogs,
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
const Manifest = Schema.fromJsonString(
|
|
25
|
+
Schema.Struct({
|
|
26
|
+
dependencies: Versions,
|
|
27
|
+
devDependencies: Versions,
|
|
28
|
+
peerDependencies: Versions,
|
|
29
|
+
optionalDependencies: Versions,
|
|
30
|
+
overrides: Versions,
|
|
31
|
+
resolutions: Versions,
|
|
32
|
+
catalog: Versions,
|
|
33
|
+
catalogs: Catalogs,
|
|
34
|
+
workspaces: Schema.optionalKey(
|
|
35
|
+
Schema.Union([WorkspaceList, WorkspaceCatalogs])
|
|
36
|
+
),
|
|
37
|
+
})
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
export type Ranges = Readonly<Record<string, string>>;
|
|
41
|
+
|
|
42
|
+
const named = (
|
|
43
|
+
prefix: string,
|
|
44
|
+
catalogs: Readonly<Record<string, Ranges>>
|
|
45
|
+
): ReadonlyArray<readonly [string, Ranges]> =>
|
|
46
|
+
Arr.map(R.toEntries(catalogs), ([name, ranges]) => [
|
|
47
|
+
`${prefix}.${name}`,
|
|
48
|
+
ranges,
|
|
49
|
+
]);
|
|
50
|
+
|
|
51
|
+
export const manifestRanges = (
|
|
52
|
+
text: string
|
|
53
|
+
): ReadonlyArray<readonly [string, Ranges]> => {
|
|
54
|
+
const decoded = Schema.decodeResult(Manifest)(text);
|
|
55
|
+
|
|
56
|
+
if (Result.isFailure(decoded)) {
|
|
57
|
+
throw decoded.failure;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const manifest = decoded.success;
|
|
61
|
+
|
|
62
|
+
const workspaces = Schema.is(WorkspaceCatalogs)(manifest.workspaces)
|
|
63
|
+
? manifest.workspaces
|
|
64
|
+
: { catalog: {}, catalogs: {} };
|
|
65
|
+
|
|
66
|
+
return [
|
|
67
|
+
["dependencies", manifest.dependencies],
|
|
68
|
+
["devDependencies", manifest.devDependencies],
|
|
69
|
+
["peerDependencies", manifest.peerDependencies],
|
|
70
|
+
["optionalDependencies", manifest.optionalDependencies],
|
|
71
|
+
["overrides", manifest.overrides],
|
|
72
|
+
["resolutions", manifest.resolutions],
|
|
73
|
+
["catalog", manifest.catalog],
|
|
74
|
+
["workspaces.catalog", workspaces.catalog],
|
|
75
|
+
...named("catalogs", manifest.catalogs),
|
|
76
|
+
...named("workspaces.catalogs", workspaces.catalogs),
|
|
77
|
+
];
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
const SEMVER = String.raw`\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?(?:\+[0-9A-Za-z.-]+)?`;
|
|
81
|
+
|
|
82
|
+
export const PLAIN = new RegExp(`^${SEMVER}$`, "u");
|
|
83
|
+
|
|
84
|
+
const EXACT = new RegExp(
|
|
85
|
+
String.raw`^(?:npm:(?:@[\w.-]+\/)?[\w.-]+@)?${SEMVER}$`,
|
|
86
|
+
"u"
|
|
87
|
+
);
|
|
88
|
+
|
|
89
|
+
const REFERENCE = /^(?:catalog:[\w.-]*|workspace:\*)$/u;
|
|
90
|
+
|
|
91
|
+
export const exactFindings: {
|
|
92
|
+
(text: string): (file: string) => ReadonlyArray<string>;
|
|
93
|
+
(file: string, text: string): ReadonlyArray<string>;
|
|
94
|
+
} = dual(2, (file: string, text: string): ReadonlyArray<string> =>
|
|
95
|
+
Arr.flatMap(manifestRanges(text), ([field, ranges]) =>
|
|
96
|
+
Arr.flatMap(R.toEntries(ranges), ([name, version]) =>
|
|
97
|
+
EXACT.test(version) || REFERENCE.test(version)
|
|
98
|
+
? []
|
|
99
|
+
: [`${file} ${field} ${name}: ${version} is not an exact version`]
|
|
100
|
+
)
|
|
101
|
+
)
|
|
102
|
+
);
|