tslaws 0.7.0 → 0.9.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 +8 -1
- package/dist/check.js +87 -17
- package/dist/cli.js +43 -3
- package/dist/coverage.d.ts +8 -0
- package/dist/coverage.js +18 -1
- package/dist/github.d.ts +123 -0
- package/dist/github.js +276 -0
- package/dist/githubCli.d.ts +53 -0
- package/dist/githubCli.js +535 -0
- package/dist/lock.d.ts +23 -0
- package/dist/lock.js +84 -0
- package/dist/propertyRun.d.ts +11 -0
- package/dist/propertyRun.js +23 -0
- package/dist/reporters.d.ts +3 -0
- package/dist/reporters.js +17 -0
- package/dist/smt.d.ts +16 -0
- package/dist/smt.js +43 -1
- package/dist/solvers.d.ts +31 -0
- package/dist/solvers.js +121 -0
- package/dist/symbolic.js +2 -2
- package/dist/types.d.ts +17 -0
- package/package.json +7 -7
package/README.md
CHANGED
|
@@ -35,6 +35,7 @@ ordinary identifier.
|
|
|
35
35
|
```
|
|
36
36
|
$ tslaws check
|
|
37
37
|
PROVED clampWithinBounds (src/math.laws.ts:5) [smt]
|
|
38
|
+
assuming: number parameters exclude NaN and ±Infinity
|
|
38
39
|
BOUNDED sortOrders (src/math.laws.ts:11) [smt]
|
|
39
40
|
holds within bounds: int values stay within ±2^26; arrays hold at most 4 elements; loops run at most 4 times
|
|
40
41
|
FAILED sortKeepsLength (src/math.laws.ts:17) [property]
|
|
@@ -67,6 +68,12 @@ Two things, both required for exit status 0:
|
|
|
67
68
|
| `tampered` | A law changed, was added or removed since `tsc-laws.lock` was accepted, or a coverage exemption is not in it. Fails the check. |
|
|
68
69
|
| `error` | The law cannot run: not exported, a parameter has no type, no generator for a type, load failure. |
|
|
69
70
|
|
|
71
|
+
A `proved` or `bounded` result also lists the domain assumptions its proof rests on, for the parameter types of that law
|
|
72
|
+
only: `number` parameters exclude NaN and ±Infinity, `int` parameters stay within ±(2^53 − 1), and `string` parameters use
|
|
73
|
+
only characters up to U+FFFF with a length cap. They show as `assuming: …` in text output, as `assumptions` in JSON and
|
|
74
|
+
ndjson, and in the step-summary Detail cell and the `::notice` of a `bounded` result. A law whose parameters need none
|
|
75
|
+
(`boolean`, `bigint`) prints as before.
|
|
76
|
+
|
|
70
77
|
`tslaws check` exits 0 only when every law is `proved`, `bounded` or `tested`, nothing is `uncovered`, the files compile, and
|
|
71
78
|
the lock matches.
|
|
72
79
|
|
|
@@ -160,7 +167,7 @@ See [docs/laws.md](../../docs/laws.md#the-dashboard).
|
|
|
160
167
|
`tslaws mutate [files...] [--max N] [--seed N] [--json]` mutation-tests the laws: it mutates the covered code, re-runs the laws against each mutant, and lists the survivors no law caught. It handles `.tsx`/`.jsx` and projects with a lock, and takes the run flags of `check`. See [docs/laws.md](../../docs/laws.md#mutation-testing).
|
|
161
168
|
|
|
162
169
|
- **Other source formats:** `tslaws.json` `bundlerPlugins` lists esbuild plugin modules (paths relative to the file) that teach the browser bundler formats like `.tsrx`. See [docs/laws.md](../../docs/laws.md#bundler-plugins).
|
|
163
|
-
- **Settings:** `tslaws.json` `runs`, `bound`, `proofTimeoutMs` and `propertyTimeoutMs` set the run defaults; a command line flag beats them. See [docs/laws.md](../../docs/laws.md#settings).
|
|
170
|
+
- **Settings:** `tslaws.json` `runs`, `bound`, `proofTimeoutMs` and `propertyTimeoutMs` set the run defaults; a command line flag beats them. `"solvers": ["z3", "cvc5"]` makes cvc5 confirm every Z3 proof (set `TSLAWS_CVC5` to its path, or put it on the PATH). See [docs/laws.md](../../docs/laws.md#settings).
|
|
164
171
|
|
|
165
172
|
## Agents cannot move the goalposts
|
|
166
173
|
|
package/dist/check.js
CHANGED
|
@@ -7,6 +7,7 @@ import { analyzeCoverage, coverFiles, readConfig } from "./coverage.js";
|
|
|
7
7
|
import { derivedStore } from "./derived.js";
|
|
8
8
|
import { jobs } from "./pool.js";
|
|
9
9
|
import { compareLock, lawKey, readLock } from "./lock.js";
|
|
10
|
+
import { crossCheck, disagreement } from "./propertyRun.js";
|
|
10
11
|
import { proveLaw } from "./smt.js";
|
|
11
12
|
import { decodeExample, loadExamples, regressionMessage, saveExample } from "./regressions.js";
|
|
12
13
|
import { closeBrowser, runBrowserLaw } from "./browser.js";
|
|
@@ -63,7 +64,12 @@ export async function check(options, enforceLock) {
|
|
|
63
64
|
const blocked = new Set();
|
|
64
65
|
const notices = [];
|
|
65
66
|
// The level of a laws file comes from its name (the default) or its imports (`levels: "imports"`); looked at once per file.
|
|
66
|
-
const
|
|
67
|
+
const config = readConfig(options.cwd);
|
|
68
|
+
const levelsMode = config.levels ?? 'names';
|
|
69
|
+
// Who records a law in the lock: a person locally, or the tslaws workflow on merge when "github" is configured.
|
|
70
|
+
const acceptor = readConfig(options.cwd).github !== undefined
|
|
71
|
+
? 'it is accepted by the tslaws workflow when the pull request merges'
|
|
72
|
+
: 'a person must run `tslaws accept`';
|
|
67
73
|
const verdicts = new Map();
|
|
68
74
|
const verdictOf = (file) => {
|
|
69
75
|
let verdict = verdicts.get(file);
|
|
@@ -85,17 +91,41 @@ export async function check(options, enforceLock) {
|
|
|
85
91
|
return [];
|
|
86
92
|
}
|
|
87
93
|
};
|
|
94
|
+
// Laws the lock does not record as they are now: they still run, and their result is reported as what the law would
|
|
95
|
+
// be once accepted, under a `tampered` status that keeps the check failing until the lock records them.
|
|
96
|
+
const pending = new Map();
|
|
88
97
|
const add = (law, status, method, extra = {}) => {
|
|
89
98
|
const exercises = status === 'tampered' ? [] : (extra.exercises ?? exercisesFor(law));
|
|
90
|
-
const
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
+
const proposed = status === 'tampered' || status === 'uncovered' ? undefined : pending.get(lawKey(law.file, law.name));
|
|
100
|
+
const result = proposed === undefined
|
|
101
|
+
? {
|
|
102
|
+
file: law.file,
|
|
103
|
+
name: law.name,
|
|
104
|
+
line: law.line,
|
|
105
|
+
status,
|
|
106
|
+
method,
|
|
107
|
+
...extra,
|
|
108
|
+
...(exercises.length > 0 ? { exercises } : {}),
|
|
109
|
+
}
|
|
110
|
+
: {
|
|
111
|
+
file: law.file,
|
|
112
|
+
name: law.name,
|
|
113
|
+
line: law.line,
|
|
114
|
+
status: 'tampered',
|
|
115
|
+
method: 'lock',
|
|
116
|
+
message: proposed.message,
|
|
117
|
+
...(extra.seed === undefined ? {} : { seed: extra.seed }),
|
|
118
|
+
...(extra.runs === undefined ? {} : { runs: extra.runs }),
|
|
119
|
+
...(extra.screenshot === undefined ? {} : { screenshot: extra.screenshot }),
|
|
120
|
+
proposal: {
|
|
121
|
+
reason: proposed.reason,
|
|
122
|
+
status: status,
|
|
123
|
+
method,
|
|
124
|
+
...(extra.counterexample === undefined ? {} : { counterexample: extra.counterexample }),
|
|
125
|
+
...(extra.message === undefined ? {} : { message: extra.message }),
|
|
126
|
+
},
|
|
127
|
+
...(exercises.length > 0 ? { exercises } : {}),
|
|
128
|
+
};
|
|
99
129
|
results.push(result);
|
|
100
130
|
blocked.add(lawKey(law.file, law.name));
|
|
101
131
|
options.onEvent?.({ type: 'result', result });
|
|
@@ -125,14 +155,16 @@ export async function check(options, enforceLock) {
|
|
|
125
155
|
status: 'tampered',
|
|
126
156
|
method: 'lock',
|
|
127
157
|
message: 'law was removed since the lock was accepted',
|
|
158
|
+
proposal: { reason: 'removed', status: 'proved', method: 'lock', message: 'removing a law runs nothing' },
|
|
128
159
|
});
|
|
129
160
|
}
|
|
130
161
|
else {
|
|
131
|
-
|
|
162
|
+
pending.set(violation.key, {
|
|
163
|
+
reason: violation.reason,
|
|
132
164
|
message: violation.reason === 'added'
|
|
133
|
-
?
|
|
165
|
+
? `law is not in the lock; ${acceptor}`
|
|
134
166
|
: violation.reason === 'level'
|
|
135
|
-
?
|
|
167
|
+
? `the level this laws file runs at (node, browser or e2e) changed since the lock was accepted; ${acceptor}`
|
|
136
168
|
: 'law changed since the lock was accepted',
|
|
137
169
|
});
|
|
138
170
|
}
|
|
@@ -292,9 +324,39 @@ export async function check(options, enforceLock) {
|
|
|
292
324
|
bound: options.bound,
|
|
293
325
|
timeoutMs: options.proofTimeoutMs,
|
|
294
326
|
holds,
|
|
327
|
+
solvers: config.solvers,
|
|
295
328
|
});
|
|
296
|
-
|
|
297
|
-
|
|
329
|
+
const assumed = (list) => (list.length > 0 ? { assumptions: list } : {});
|
|
330
|
+
if (proof.kind === 'proved') {
|
|
331
|
+
// The property run above used generators inside the proved domain; a short one is topped up before the proof stands.
|
|
332
|
+
const passed = details;
|
|
333
|
+
const rerun = (runs) => runLawGuarded(built.outDir, law, { ...lawOptions, runs, seed: passed.seed }, types.plans);
|
|
334
|
+
let disagrees;
|
|
335
|
+
try {
|
|
336
|
+
disagrees = await crossCheck(details, types.plans.length, rerun);
|
|
337
|
+
}
|
|
338
|
+
catch (error) {
|
|
339
|
+
add(law, 'error', 'smt', { message: error.message });
|
|
340
|
+
return;
|
|
341
|
+
}
|
|
342
|
+
if (disagrees === 'timeout')
|
|
343
|
+
add(law, 'error', 'smt', {
|
|
344
|
+
message: `proved, but the cross-check run in JavaScript ran longer than ${limitMs / 1000}s`,
|
|
345
|
+
});
|
|
346
|
+
else if (disagrees !== undefined) {
|
|
347
|
+
if (!options.noRegressions && disagrees.encoded !== undefined)
|
|
348
|
+
saveExample(options.cwd, law.file, law.name, decodeExample(disagrees.encoded));
|
|
349
|
+
add(law, 'failed', 'smt', {
|
|
350
|
+
exercises: markFailing(exercisesFor(law), disagrees.stack, built.outDir),
|
|
351
|
+
runs: disagrees.numRuns,
|
|
352
|
+
seed: disagrees.seed,
|
|
353
|
+
counterexample: disagrees.counterexample,
|
|
354
|
+
message: disagreement(disagrees),
|
|
355
|
+
});
|
|
356
|
+
}
|
|
357
|
+
else
|
|
358
|
+
add(law, 'proved', 'smt', { runs: details.numRuns, seed: details.seed, ...assumed(proof.assumptions) });
|
|
359
|
+
}
|
|
298
360
|
else if (proof.kind === 'bounded') {
|
|
299
361
|
// `law proved` promises every input; a bounded proof is the most Z3 could show.
|
|
300
362
|
const message = `holds within bounds: ${proof.limits.join('; ')}`;
|
|
@@ -305,8 +367,15 @@ export async function check(options, enforceLock) {
|
|
|
305
367
|
message: `marked proved but only ${message}`,
|
|
306
368
|
});
|
|
307
369
|
else
|
|
308
|
-
add(law, 'bounded', 'smt', {
|
|
370
|
+
add(law, 'bounded', 'smt', {
|
|
371
|
+
runs: details.numRuns,
|
|
372
|
+
seed: details.seed,
|
|
373
|
+
message,
|
|
374
|
+
...assumed(proof.assumptions),
|
|
375
|
+
});
|
|
309
376
|
}
|
|
377
|
+
else if (proof.kind === 'error')
|
|
378
|
+
add(law, 'error', 'smt', { message: proof.message });
|
|
310
379
|
else if (proof.kind === 'refuted')
|
|
311
380
|
add(law, 'failed', 'smt', { counterexample: proof.model, message: 'refuted by the prover' });
|
|
312
381
|
else if (law.proved)
|
|
@@ -369,7 +438,8 @@ export async function check(options, enforceLock) {
|
|
|
369
438
|
line: 0,
|
|
370
439
|
status: 'tampered',
|
|
371
440
|
method: 'lock',
|
|
372
|
-
message:
|
|
441
|
+
message: `exempted from coverage but not in the lock; ${acceptor}`,
|
|
442
|
+
proposal: { reason: 'exempt', status: 'proved', method: 'lock', message: 'an exemption runs nothing' },
|
|
373
443
|
});
|
|
374
444
|
}
|
|
375
445
|
}
|
package/dist/cli.js
CHANGED
|
@@ -14,7 +14,7 @@ import { serveMcp } from "./mcp.js";
|
|
|
14
14
|
import { promoteFiles, promoteProposal } from "./promote.js";
|
|
15
15
|
import { addProposal, ProposalError } from "./proposals.js";
|
|
16
16
|
import { clearCorpus, listCorpus } from "./regressions.js";
|
|
17
|
-
import { reporterNames, reportAs } from "./reporters.js";
|
|
17
|
+
import { proposalText, reporterNames, reportAs } from "./reporters.js";
|
|
18
18
|
import { readConfig } from "./coverage.js";
|
|
19
19
|
import { migrateCommand, migrateUsage } from "./migrate/index.js";
|
|
20
20
|
import { mutate, renderMutation } from "./mutate.js";
|
|
@@ -35,11 +35,12 @@ tslaws delegate status list delegations and when they end
|
|
|
35
35
|
tslaws propose <file.ts> [--from PATH] write a proposal into .tslaws/proposals/ from PATH or stdin (outside the tree, git-ignored; never touches the lock or any other file)
|
|
36
36
|
tslaws promote <proposal> [--to PATH] move a proposal from .tslaws/proposals/ into the tree at a path you confirm (people only: needs a terminal; run accept afterwards). In-tree *.proposed.laws.ts files (the usual proposals) promote with --all or by name
|
|
37
37
|
tslaws list [--json] the laws files and proposals of the project: the level each runs at (from its name, or its imports with levels: imports) and whether it is accepted, unaccepted or proposed (reads files and the lock; runs nothing)
|
|
38
|
+
tslaws github report|command|merge|verify accept laws on GitHub: flag and run each proposal on a pull request, record /tslaws accept comments, and write tsc-laws.lock on merge (CI only; see "github" in tslaws.json)
|
|
38
39
|
tslaws mcp [--compiler PATH] serve check, check_proposed, explain and propose as Model Context Protocol tools on stdio (cannot accept, promote or touch the lock)
|
|
39
40
|
${migrateUsage}
|
|
40
41
|
|
|
41
42
|
Without files, every *.laws.ts under the current directory (and every file the "laws" globs of tslaws.json pick that declares a law) is checked; with --proposed, the in-tree *.proposed.laws.ts files are (and any proposals in .tslaws/proposals/), with no lock and no coverage gate.
|
|
42
|
-
A laws file
|
|
43
|
+
A laws file's level comes from its name by default: *.browser.laws.ts is a browser law, *.e2e.laws.ts an end-to-end law, anything else runs in Node. With "levels": "imports" in tslaws.json it comes from the import graph instead (tslaws/browser, tslaws/adapters/* or tslaws/e2e, directly or through the file's own modules), and the names are hints.
|
|
43
44
|
Exit status is 0 only when every law is proved, bounded or tested and nothing is open, failed, uncovered or tampered with.
|
|
44
45
|
--json machine-readable report; each result carries a hint with the next action (same as --reporter json)
|
|
45
46
|
--reporter text (default), json, junit (JUnit XML), github (workflow annotations; appends a table to $GITHUB_STEP_SUMMARY) or ndjson (one JSON event per line as the check proceeds, then the report; for editors)
|
|
@@ -72,6 +73,12 @@ function parse(args) {
|
|
|
72
73
|
'to',
|
|
73
74
|
'from',
|
|
74
75
|
'out',
|
|
76
|
+
'pr',
|
|
77
|
+
'config-from',
|
|
78
|
+
'head-sha',
|
|
79
|
+
'rerun-workflow',
|
|
80
|
+
'protect',
|
|
81
|
+
'project-path',
|
|
75
82
|
]);
|
|
76
83
|
for (let i = 0; i < args.length; i += 1) {
|
|
77
84
|
const arg = args[i];
|
|
@@ -94,7 +101,8 @@ function parse(args) {
|
|
|
94
101
|
arg === 'propose' ||
|
|
95
102
|
arg === 'list' ||
|
|
96
103
|
arg === 'migrate' ||
|
|
97
|
-
arg === 'delegate'
|
|
104
|
+
arg === 'delegate' ||
|
|
105
|
+
arg === 'github'))
|
|
98
106
|
command = arg;
|
|
99
107
|
else
|
|
100
108
|
files.push(arg);
|
|
@@ -107,6 +115,11 @@ function describe(result) {
|
|
|
107
115
|
const lines = [head];
|
|
108
116
|
if (result.message !== undefined)
|
|
109
117
|
lines.push(` ${result.message}`);
|
|
118
|
+
if (result.assumptions !== undefined && result.assumptions.length > 0)
|
|
119
|
+
lines.push(` assuming: ${result.assumptions.join('; ')}`);
|
|
120
|
+
const proposed = proposalText(result);
|
|
121
|
+
if (proposed !== undefined)
|
|
122
|
+
lines.push(` ${proposed}`);
|
|
110
123
|
if (result.counterexample !== undefined)
|
|
111
124
|
lines.push(` counterexample: ${result.counterexample}`);
|
|
112
125
|
if (result.status === 'failed' && result.seed !== undefined)
|
|
@@ -119,6 +132,7 @@ function describe(result) {
|
|
|
119
132
|
function summarise(result) {
|
|
120
133
|
const detail = [
|
|
121
134
|
result.message,
|
|
135
|
+
proposalText(result),
|
|
122
136
|
result.counterexample !== undefined ? `counterexample: ${result.counterexample}` : undefined,
|
|
123
137
|
]
|
|
124
138
|
.filter((d) => d !== undefined)
|
|
@@ -270,6 +284,10 @@ export async function acceptCommand(cwd, flags, files, ctx) {
|
|
|
270
284
|
err('no laws files found (looking for *.laws.ts)\n');
|
|
271
285
|
return 1;
|
|
272
286
|
}
|
|
287
|
+
if (readConfig(cwd).github !== undefined) {
|
|
288
|
+
err('this project accepts laws on GitHub: tsc-laws.lock is written only by the tslaws workflow when a pull request merges (see "github" in tslaws.json); propose the laws in a pull request instead\n');
|
|
289
|
+
return 1;
|
|
290
|
+
}
|
|
273
291
|
const gate = acceptGate({ isTTY: ctx.isTTY, env: ctx.env, root: cwd, now: ctx.now, home: ctx.home });
|
|
274
292
|
if (!gate.ok) {
|
|
275
293
|
err(`${gate.message}\n`);
|
|
@@ -459,6 +477,28 @@ export async function main(args) {
|
|
|
459
477
|
process.stdout.write(`${init({ cwd, force: flags.has('force') }).join('\n')}\n\n${nextStep}\n`);
|
|
460
478
|
return 0;
|
|
461
479
|
}
|
|
480
|
+
if (command === 'github') {
|
|
481
|
+
const { githubCommand, projectPathOf } = await import("./githubCli.js");
|
|
482
|
+
// The token stays with tslaws: laws run in worker threads and the compiler in a child process, and both get a copy of
|
|
483
|
+
// process.env, so a law under test could otherwise read the token and act as the bot.
|
|
484
|
+
const env = { ...process.env };
|
|
485
|
+
delete process.env.GITHUB_TOKEN;
|
|
486
|
+
delete process.env.GH_TOKEN;
|
|
487
|
+
// With --config-from, the project is a pull request's files, read as data: never run a compiler from among them.
|
|
488
|
+
const trusted = flags.get('config-from');
|
|
489
|
+
const chosen = options(cwd, flags, discover(cwd, false));
|
|
490
|
+
if (typeof trusted === 'string' && chosen.compiler === checkoutCompiler(cwd))
|
|
491
|
+
chosen.compiler = checkoutCompiler(resolve(cwd, trusted)) ?? 'tsc-laws';
|
|
492
|
+
return githubCommand(files[0], {
|
|
493
|
+
cwd,
|
|
494
|
+
projectPath: projectPathOf(cwd, flags.get('project-path')),
|
|
495
|
+
options: chosen,
|
|
496
|
+
env,
|
|
497
|
+
flags,
|
|
498
|
+
out: (text) => void process.stdout.write(text),
|
|
499
|
+
err: (text) => void process.stderr.write(text),
|
|
500
|
+
});
|
|
501
|
+
}
|
|
462
502
|
if (command === 'accept')
|
|
463
503
|
return acceptCommand(cwd, flags, files, { isTTY: process.stdin.isTTY === true, env: process.env });
|
|
464
504
|
if (command === 'delegate')
|
package/dist/coverage.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import ts from 'typescript';
|
|
2
2
|
import { type E2eConfig } from './e2e.ts';
|
|
3
|
+
import { type SolverName } from './solvers.ts';
|
|
3
4
|
export interface CoverageConfig {
|
|
4
5
|
cover?: string[];
|
|
5
6
|
/** Compiler options the runner passes to tsc; see `compilerFlags` in compile.ts for the supported keys. */
|
|
@@ -16,6 +17,11 @@ export interface CoverageConfig {
|
|
|
16
17
|
* (`*.browser.laws.ts`, `*.e2e.laws.ts`). `imports`: the file's import graph; names are still recognised as hints.
|
|
17
18
|
*/
|
|
18
19
|
levels?: 'names' | 'imports';
|
|
20
|
+
/**
|
|
21
|
+
* The SMT solvers that must agree before a law is `proved`: `["z3"]` (the default) or `["z3", "cvc5"]`, where cvc5
|
|
22
|
+
* must also answer `unsat` or the law is `bounded`. Z3 is required. See solvers.ts.
|
|
23
|
+
*/
|
|
24
|
+
solvers?: SolverName[];
|
|
19
25
|
/** esbuild plugin modules (paths relative to tslaws.json) that teach the browser bundler new source formats. */
|
|
20
26
|
bundlerPlugins?: string[];
|
|
21
27
|
/** Run settings; a command line flag beats them and they beat the built-in default. */
|
|
@@ -23,6 +29,8 @@ export interface CoverageConfig {
|
|
|
23
29
|
propertyTimeoutMs?: number;
|
|
24
30
|
proofTimeoutMs?: number;
|
|
25
31
|
bound?: number;
|
|
32
|
+
/** Accepting laws on GitHub: see github.ts. When present, `tslaws accept` refuses to run outside that workflow. */
|
|
33
|
+
github?: unknown;
|
|
26
34
|
}
|
|
27
35
|
export interface Uncovered {
|
|
28
36
|
file: string;
|
package/dist/coverage.js
CHANGED
|
@@ -4,14 +4,26 @@
|
|
|
4
4
|
// Covered modules are the `cover` globs of tslaws.json, or, without that file, every module the laws import
|
|
5
5
|
// (directly or through other project modules). A function carrying `@tslaws-exempt` in its doc comment is exempt,
|
|
6
6
|
// but only once the exemption is in tsc-laws.lock, so an agent cannot exempt its own code.
|
|
7
|
+
import { parseGithubConfig } from "./github.js";
|
|
7
8
|
import { existsSync, readdirSync, readFileSync, statSync } from 'node:fs';
|
|
8
9
|
import { dirname, join, relative, resolve } from 'node:path';
|
|
9
10
|
import ts from 'typescript';
|
|
10
11
|
import { parseE2eConfig } from "./e2e.js";
|
|
11
12
|
import { globMatcher } from "./globs.js";
|
|
12
13
|
import { customLawFile } from "./lawFiles.js";
|
|
14
|
+
import { solversProblem } from "./solvers.js";
|
|
13
15
|
const settingKeys = ['runs', 'propertyTimeoutMs', 'proofTimeoutMs', 'bound'];
|
|
14
|
-
const configKeys = [
|
|
16
|
+
const configKeys = [
|
|
17
|
+
'cover',
|
|
18
|
+
'laws',
|
|
19
|
+
'levels',
|
|
20
|
+
'solvers',
|
|
21
|
+
'compilerOptions',
|
|
22
|
+
'e2e',
|
|
23
|
+
'bundlerPlugins',
|
|
24
|
+
'github',
|
|
25
|
+
...settingKeys,
|
|
26
|
+
];
|
|
15
27
|
/** Validates the top level of tslaws.json: known keys only, and the run settings are integers of at least 1. */
|
|
16
28
|
export function parseConfig(raw) {
|
|
17
29
|
const fail = (message) => {
|
|
@@ -36,11 +48,16 @@ export function parseConfig(raw) {
|
|
|
36
48
|
const levels = object.levels;
|
|
37
49
|
if (levels !== undefined && levels !== 'names' && levels !== 'imports')
|
|
38
50
|
fail(`"levels" must be "names" or "imports", got ${JSON.stringify(levels)}`);
|
|
51
|
+
const problem = object.solvers === undefined ? undefined : solversProblem(object.solvers);
|
|
52
|
+
if (problem !== undefined)
|
|
53
|
+
fail(problem);
|
|
39
54
|
const laws = object.laws;
|
|
40
55
|
if (laws !== undefined &&
|
|
41
56
|
(!Array.isArray(laws) ||
|
|
42
57
|
laws.some((entry) => typeof entry !== 'string' || entry.replace(/^!/, '') === '' || entry.startsWith('/'))))
|
|
43
58
|
fail(`"laws" must be an array of project-relative globs, got ${JSON.stringify(laws)}`);
|
|
59
|
+
if (object.github !== undefined)
|
|
60
|
+
parseGithubConfig(object.github);
|
|
44
61
|
return object;
|
|
45
62
|
}
|
|
46
63
|
export function readConfig(cwd) {
|
package/dist/github.d.ts
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import type { CheckReport } from './check.ts';
|
|
2
|
+
import type { LockChange } from './lock.ts';
|
|
3
|
+
import type { ProposalOutcome } from './types.ts';
|
|
4
|
+
export interface GithubConfig {
|
|
5
|
+
/** When proposals become accepted laws: on merge (default), or on merge once someone with authority said accept. */
|
|
6
|
+
accept: 'merge' | 'comment';
|
|
7
|
+
/** The branch whose merges write the lock. Default `master`. */
|
|
8
|
+
branch: string;
|
|
9
|
+
/**
|
|
10
|
+
* The login that alone writes tsc-laws.lock and the approval records. Default `github-actions[bot]`, allowed only
|
|
11
|
+
* with `accept: "merge"`: every workflow's token acts as that account, including jobs that run a pull request's
|
|
12
|
+
* code, so it could forge approval records, and only a ruleset on the branch keeps it from writing the lock. A
|
|
13
|
+
* dedicated GitHub App (`<app-slug>[bot]`) is required for `accept: "comment"` and is the stronger choice for both.
|
|
14
|
+
*/
|
|
15
|
+
bot: string;
|
|
16
|
+
/**
|
|
17
|
+
* Who may accept in comment mode: `author` (the pull request's author, when a person), `reviewers` (anyone asked to
|
|
18
|
+
* review or who has reviewed), and/or a minimum repository permission (`write`, `maintain`, `admin`). Every login also
|
|
19
|
+
* needs write access to the repository. Default `["author", "reviewers"]`.
|
|
20
|
+
*/
|
|
21
|
+
approvers: ('author' | 'reviewers' | 'write' | 'maintain' | 'admin')[];
|
|
22
|
+
/** Comment mode: approve the pull request once every proposal is accepted. Default false. */
|
|
23
|
+
approvePullRequest: boolean;
|
|
24
|
+
/** A proposal that fails (or is open) is never accepted, whatever was said. Default true. */
|
|
25
|
+
requirePassing: boolean;
|
|
26
|
+
}
|
|
27
|
+
export declare const githubDefaults: GithubConfig;
|
|
28
|
+
/** Whether the bot is an account any workflow can act as, rather than a dedicated GitHub App. */
|
|
29
|
+
export declare function isSharedBot(config: GithubConfig): boolean;
|
|
30
|
+
/** Validates the `github` section of tslaws.json and fills in the defaults. */
|
|
31
|
+
export declare function parseGithubConfig(raw: unknown): GithubConfig;
|
|
32
|
+
export interface Proposal {
|
|
33
|
+
key: string;
|
|
34
|
+
file: string;
|
|
35
|
+
name: string;
|
|
36
|
+
line: number;
|
|
37
|
+
kind: LockChange['kind'];
|
|
38
|
+
reason: LockChange['reason'];
|
|
39
|
+
fingerprint: string;
|
|
40
|
+
/** What the law did when it ran; undefined when the check did not report it (it did not run). */
|
|
41
|
+
outcome?: ProposalOutcome;
|
|
42
|
+
passes: boolean;
|
|
43
|
+
}
|
|
44
|
+
/** Joins the lock changes with what the check reported for each. */
|
|
45
|
+
export declare function proposalsOf(changes: LockChange[], report: CheckReport | undefined): Proposal[];
|
|
46
|
+
export type Command = {
|
|
47
|
+
verb: 'accept' | 'reject';
|
|
48
|
+
all: boolean;
|
|
49
|
+
names: string[];
|
|
50
|
+
};
|
|
51
|
+
/**
|
|
52
|
+
* The `/tslaws accept ...` and `/tslaws reject ...` lines of a comment. Names are law names or `file::name` keys,
|
|
53
|
+
* separated by spaces or commas; a name with spaces is written in backticks or quotes.
|
|
54
|
+
*/
|
|
55
|
+
export declare function parseCommands(body: string): Command[];
|
|
56
|
+
/** The proposals a command names: by `file::name` key, or by law name when that name is unambiguous. */
|
|
57
|
+
export declare function resolveNames(proposals: Proposal[], command: Command): {
|
|
58
|
+
matched: Proposal[];
|
|
59
|
+
unknown: string[];
|
|
60
|
+
ambiguous: string[];
|
|
61
|
+
};
|
|
62
|
+
export interface ApprovalRecord {
|
|
63
|
+
verb: 'accept' | 'reject';
|
|
64
|
+
by: string;
|
|
65
|
+
/** The head commit the person saw. */
|
|
66
|
+
sha: string;
|
|
67
|
+
/** key and fingerprint of each proposal the record covers. */
|
|
68
|
+
items: {
|
|
69
|
+
key: string;
|
|
70
|
+
fingerprint: string;
|
|
71
|
+
}[];
|
|
72
|
+
}
|
|
73
|
+
/** The first line of a record comment. A record counts only in a bot comment made of this line and markers alone. */
|
|
74
|
+
export declare const recordHeader = "<!-- tslaws-records -->";
|
|
75
|
+
export declare function approvalMarker(record: ApprovalRecord): string;
|
|
76
|
+
/** The body of a record comment: the header line, then one marker per line. Nothing from the pull request appears raw. */
|
|
77
|
+
export declare function recordComment(records: ApprovalRecord[]): string;
|
|
78
|
+
/** Text from a pull request (law names, file paths, unknown words), made inert in a markdown comment. */
|
|
79
|
+
export declare function inert(text: string): string;
|
|
80
|
+
export interface IssueComment {
|
|
81
|
+
id: number;
|
|
82
|
+
body: string;
|
|
83
|
+
user: {
|
|
84
|
+
login: string;
|
|
85
|
+
type?: string;
|
|
86
|
+
};
|
|
87
|
+
created_at: string;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* The approval records in record comments the bot wrote, oldest first. A comment counts only if it is the header line
|
|
91
|
+
* followed by markers and nothing else. Records in anyone else's comment, or in a bot comment that also holds other
|
|
92
|
+
* text (such as text a pull request put there), are ignored.
|
|
93
|
+
*/
|
|
94
|
+
export declare function readApprovals(comments: IssueComment[], bot: string): ApprovalRecord[];
|
|
95
|
+
/**
|
|
96
|
+
* The fingerprints accepted by the records, latest word wins: a later reject withdraws an earlier accept of the same
|
|
97
|
+
* fingerprint. A fingerprint names one version of a law, so an edit after the accept leaves the new version unaccepted.
|
|
98
|
+
*/
|
|
99
|
+
export declare function acceptedFingerprints(records: ApprovalRecord[]): Map<string, string>;
|
|
100
|
+
export interface Decision {
|
|
101
|
+
/** Fingerprints to write to the lock. */
|
|
102
|
+
accept: Set<string>;
|
|
103
|
+
/** Proposals left as they are, with the reason. */
|
|
104
|
+
held: {
|
|
105
|
+
proposal: Proposal;
|
|
106
|
+
why: string;
|
|
107
|
+
}[];
|
|
108
|
+
}
|
|
109
|
+
/** Which proposals the merge job accepts. */
|
|
110
|
+
export declare function decide(proposals: Proposal[], config: GithubConfig, approvals: Map<string, string>): Decision;
|
|
111
|
+
/**
|
|
112
|
+
* The pull request's verdict: it fails when a law that is already accepted fails, when a proposal fails (a failing
|
|
113
|
+
* proposal is rejected outright), or in comment mode while a proposal waits for an accept.
|
|
114
|
+
*/
|
|
115
|
+
export declare function pullRequestVerdict(report: CheckReport, proposals: Proposal[], config: GithubConfig, approvals: Map<string, string>): {
|
|
116
|
+
ok: boolean;
|
|
117
|
+
problems: string[];
|
|
118
|
+
};
|
|
119
|
+
export declare const stickyMarker = "<!-- tslaws-proposals -->";
|
|
120
|
+
/** The sticky comment's marker for a project: the root's is unchanged; another project's names its path. */
|
|
121
|
+
export declare function stickyMarkerFor(project?: string): string;
|
|
122
|
+
/** The sticky pull request comment: every proposal, what it did, and whether it will be accepted on merge. */
|
|
123
|
+
export declare function renderSticky(proposals: Proposal[], config: GithubConfig, approvals: Map<string, string>, link: (p: Proposal) => string, project?: string): string;
|