tslaws 1.1.0 → 1.3.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/dist/badge.d.ts CHANGED
@@ -1,18 +1,34 @@
1
+ import type { Certificate } from './certificates.ts';
1
2
  import type { Lock } from './lock.ts';
2
- import type { LawResult } from './types.ts';
3
+ import type { LawResult, LawStatus } from './types.ts';
3
4
  /** The shields.io endpoint schema: https://shields.io/badges/endpoint-badge */
4
5
  export interface Endpoint {
5
6
  schemaVersion: 1;
6
7
  label: string;
7
8
  message: string;
8
- color: 'brightgreen' | 'yellow';
9
+ color: 'brightgreen' | 'yellow' | 'red';
9
10
  }
10
- /** What a badge counts of one law. */
11
+ /** What a check concluded of a law. `tampered`, `uncovered` and `error` are failures of the check and count as `failed`. */
12
+ export type Outcome = 'proved' | 'bounded' | 'tested' | 'open' | 'failed';
13
+ /**
14
+ * Where a law stands with a certificate (a proof that a separate checker accepted).
15
+ * - `certified`: a checker accepted a proof of it.
16
+ * - `not-certifiable`: the proof format cannot check it. The law uses floating point or strings, which Alethe cannot
17
+ * express, or it was only tested or checked within a bound, so there is no proof to certify.
18
+ * - `not-certified`: a proof could be certified, but none was (certificates were off, or a tool was missing).
19
+ * - `rejected`: a certificate was tried and did not pass (cvc5 gave no proof, Carcara did not accept it, a timeout).
20
+ */
21
+ export type Certification = 'certified' | 'not-certifiable' | 'not-certified' | 'rejected';
22
+ /** What a badge or the certificates page reports of one law. */
11
23
  export interface Standing {
12
- /** Z3 proved it (`proved`; a law that is only `bounded` or `tested` is not counted). */
13
- proved: boolean;
14
- /** A checker accepted a proof of it. */
15
- certified: boolean;
24
+ outcome: Outcome;
25
+ certification: Certification;
26
+ /** For `not-certifiable`, `not-certified` and `rejected`: why, in the words of tslaws or of the tool. */
27
+ reason?: string;
28
+ /** For a tested or bounded law: how many random inputs ran. */
29
+ runs?: number;
30
+ /** For a tested, bounded or open law: what the prover said. */
31
+ note?: string;
16
32
  }
17
33
  export interface Badges {
18
34
  laws: Endpoint;
@@ -23,13 +39,43 @@ export declare const badgeFiles: {
23
39
  readonly laws: "badge-laws.json";
24
40
  readonly certificates: "badge-certificates.json";
25
41
  };
26
- /** Green when every law is proved (or certified), yellow otherwise, and for a project with no laws. */
42
+ /** What a check shows of a law: its status, the certificate of its proof if there is one, and whether the lock vouches for it. */
43
+ export interface LawFacts {
44
+ status: LawStatus;
45
+ certificate?: Pick<Certificate, 'status' | 'reason'>;
46
+ /** The lock records a certificate that is still for the law. */
47
+ certified?: boolean;
48
+ runs?: number;
49
+ message?: string;
50
+ }
51
+ /** The standing of a law. Only a `proved` law is certifiable: a tested, bounded or open one has no proof to check. */
52
+ export declare function standingOf(law: LawFacts): Standing;
53
+ /**
54
+ * The two badges as a breakdown, never "N of M".
55
+ *
56
+ * Laws, such as `7 proved · 1 tested`: red when a law failed, yellow when one is open or there are no laws, else green.
57
+ *
58
+ * Certificates, such as `7 certified · 1 not certifiable`: red when a law failed. Yellow when a law that could be
59
+ * certified has no certificate, or its certificate was rejected. Green otherwise: a law that cannot be certified does not
60
+ * count against the project.
61
+ */
27
62
  export declare function badgesOf(laws: readonly Standing[]): Badges;
28
63
  /**
29
64
  * The standing of each law of a check. A law is certified when this run checked a proof of it, or when the lock records
30
65
  * a certificate that is still for the law (`tslaws accept` verified it with Carcara before writing it).
31
66
  */
32
67
  export declare function standingsOf(results: readonly LawResult[], lock?: Lock): Standing[];
68
+ /** A reason a law cannot be certified, in plain words. Other reasons (a tool's own message) are shown as they are. */
69
+ export declare function plainReason(reason: string): string;
70
+ /** The status of a law in a table, in words that need no knowledge of solvers. */
71
+ export declare function statusWords(standing: Standing): string;
72
+ /** The section that opens a certificates page. */
73
+ export declare const whatIsACertificate: readonly string[];
74
+ /** The summary of a certificates page: the laws grouped by what they can and cannot do. Deterministic for a given input. */
75
+ export declare function certificateSummary(laws: readonly {
76
+ name: string;
77
+ standing: Standing;
78
+ }[]): string[];
33
79
  /** The text of the two files, one JSON object each. */
34
80
  export declare function badgeTexts(badges: Badges): Record<string, string>;
35
81
  /** Writes the two files into `dir` (made if missing) and returns their paths. */
@@ -57,8 +103,9 @@ export declare function badgeMarkdown(options: MarkdownOptions): string;
57
103
  /** `dir` as the repository root sees it (`/` separators), or as given when there is no repository. */
58
104
  export declare function repoRelative(cwd: string, dir: string, top: string | undefined): string;
59
105
  /**
60
- * The certificates page of `tslaws badge`: every law with its status and, for a certified one, its proof files. They are
61
- * copied from `.tslaws/certificates` into `certificates/` beside the page, so the page links files the repository holds.
62
- * Writes nothing and returns false when no law is certified.
106
+ * The certificates page of `tslaws badge`: what a certificate is, the laws grouped by what they can and cannot do, and
107
+ * every law with its status and, for a certified one, its proof files. They are copied from `.tslaws/certificates` into
108
+ * `certificates/` beside the page, so the page links files the repository holds. Writes nothing and returns false when no
109
+ * law is certified.
63
110
  */
64
111
  export declare function writeCertificatesPage(cwd: string, dir: string, results: readonly LawResult[], lock?: Lock): boolean;
package/dist/badge.js CHANGED
@@ -1,27 +1,86 @@
1
- // `tslaws badge`: the two shields.io endpoint badges of a project, "laws: N proved" and "certificates: N verified", as
2
- // JSON files to commit, and the markdown that shows them. An endpoint badge reads its JSON from a URL, so a file in the
3
- // repository served by raw.githubusercontent.com is enough: no GitHub Pages. See docs/certificates.md, "Badges".
1
+ // `tslaws badge`: the two shields.io endpoint badges of a project, "laws: 7 proved · 1 tested" and "certificates: 7
2
+ // certified · 1 not certifiable", as JSON files to commit, and the markdown that shows them. An endpoint badge reads its
3
+ // JSON from a URL, so a file in the repository served by raw.githubusercontent.com is enough: no GitHub Pages. See
4
+ // docs/certificates.md, "Badges".
4
5
  import { spawnSync } from 'node:child_process';
5
6
  import { copyFileSync, existsSync, mkdirSync, writeFileSync } from 'node:fs';
6
7
  import { join, relative, resolve } from 'node:path';
7
- import { certificateName, certificatesDir } from "./certificates.js";
8
+ import { certificateName, certificatesDir, noCertificate } from "./certificates.js";
8
9
  import { isCertified, lawKey } from "./lock.js";
9
10
  /** The files `tslaws badge` writes, and the repository's own gallery keeps beside its certificates. */
10
11
  export const badgeFiles = { laws: 'badge-laws.json', certificates: 'badge-certificates.json' };
11
- function endpoint(label, count, total, verb) {
12
- const all = total > 0 && count === total;
13
- return {
14
- schemaVersion: 1,
15
- label,
16
- message: all ? `${count} ${verb}` : `${count} of ${total} ${verb}`,
17
- color: all ? 'brightgreen' : 'yellow',
12
+ /** The reasons `uncertifiable` (certificates.ts) gives for floating point and strings both end "which Alethe proofs do not cover". */
13
+ const outsideAlethe = /Alethe proofs do not cover/;
14
+ function outcomeOf(status) {
15
+ return status === 'proved' || status === 'bounded' || status === 'tested' || status === 'open' ? status : 'failed';
16
+ }
17
+ /** The standing of a law. Only a `proved` law is certifiable: a tested, bounded or open one has no proof to check. */
18
+ export function standingOf(law) {
19
+ const outcome = outcomeOf(law.status);
20
+ const detail = {
21
+ ...(law.runs === undefined ? {} : { runs: law.runs }),
22
+ ...(law.message === undefined ? {} : { note: law.message }),
18
23
  };
24
+ const { certificate } = law;
25
+ if (law.certified === true || certificate?.status === 'certified')
26
+ return { outcome, certification: 'certified', ...detail };
27
+ if (outcome !== 'proved')
28
+ return { outcome, certification: 'not-certifiable', ...detail };
29
+ const reason = certificate?.reason === undefined ? {} : { reason: certificate.reason };
30
+ if (certificate?.status === 'uncertified' &&
31
+ certificate.reason !== undefined &&
32
+ outsideAlethe.test(certificate.reason))
33
+ return { outcome, certification: 'not-certifiable', ...reason, ...detail };
34
+ if (certificate?.status === 'uncertified' && certificate.reason !== noCertificate.reason)
35
+ return { outcome, certification: 'rejected', ...reason, ...detail };
36
+ return { outcome, certification: 'not-certified', ...reason, ...detail };
37
+ }
38
+ /** `3 proved · 1 tested`: the non-zero counts in order, or `no laws`. */
39
+ function breakdown(counts) {
40
+ const parts = counts.filter(([count]) => count > 0).map(([count, word]) => `${count} ${word}`);
41
+ return parts.length === 0 ? 'no laws' : parts.join(' · ');
19
42
  }
20
- /** Green when every law is proved (or certified), yellow otherwise, and for a project with no laws. */
43
+ /**
44
+ * The two badges as a breakdown, never "N of M".
45
+ *
46
+ * Laws, such as `7 proved · 1 tested`: red when a law failed, yellow when one is open or there are no laws, else green.
47
+ *
48
+ * Certificates, such as `7 certified · 1 not certifiable`: red when a law failed. Yellow when a law that could be
49
+ * certified has no certificate, or its certificate was rejected. Green otherwise: a law that cannot be certified does not
50
+ * count against the project.
51
+ */
21
52
  export function badgesOf(laws) {
53
+ const outcomes = (outcome) => laws.filter((law) => law.outcome === outcome).length;
54
+ const certifications = (wanted) => laws.filter((law) => law.outcome !== 'failed' && law.certification === wanted).length;
55
+ const failed = outcomes('failed');
56
+ const open = outcomes('open');
57
+ const missing = certifications('not-certified');
58
+ const rejected = certifications('rejected');
22
59
  return {
23
- laws: endpoint('laws', laws.filter((law) => law.proved).length, laws.length, 'proved'),
24
- certificates: endpoint('certificates', laws.filter((law) => law.certified).length, laws.length, 'verified'),
60
+ laws: {
61
+ schemaVersion: 1,
62
+ label: 'laws',
63
+ message: breakdown([
64
+ [outcomes('proved'), 'proved'],
65
+ [outcomes('bounded'), 'bounded'],
66
+ [outcomes('tested'), 'tested'],
67
+ [open, 'open'],
68
+ [failed, 'failed'],
69
+ ]),
70
+ color: failed > 0 ? 'red' : open > 0 || laws.length === 0 ? 'yellow' : 'brightgreen',
71
+ },
72
+ certificates: {
73
+ schemaVersion: 1,
74
+ label: 'certificates',
75
+ message: breakdown([
76
+ [certifications('certified'), 'certified'],
77
+ [certifications('not-certifiable'), 'not certifiable'],
78
+ [missing, 'not certified'],
79
+ [rejected, 'certificate failed'],
80
+ [failed, 'failed'],
81
+ ]),
82
+ color: failed > 0 ? 'red' : missing + rejected > 0 || laws.length === 0 ? 'yellow' : 'brightgreen',
83
+ },
25
84
  };
26
85
  }
27
86
  /**
@@ -29,11 +88,114 @@ export function badgesOf(laws) {
29
88
  * a certificate that is still for the law (`tslaws accept` verified it with Carcara before writing it).
30
89
  */
31
90
  export function standingsOf(results, lock) {
32
- return results.map((result) => ({
33
- proved: result.status === 'proved',
34
- certified: result.certificate?.status === 'certified' || isCertified(lock, lawKey(result.file, result.name)),
91
+ return results.map((result) => standingOf({
92
+ status: result.status,
93
+ certificate: result.certificate,
94
+ certified: isCertified(lock, lawKey(result.file, result.name)),
95
+ runs: result.runs,
96
+ message: result.message,
35
97
  }));
36
98
  }
99
+ const firstLine = (text) => text.split('\n')[0].trim();
100
+ /** A reason a law cannot be certified, in plain words. Other reasons (a tool's own message) are shown as they are. */
101
+ export function plainReason(reason) {
102
+ const needsNumbers = 'the proof checker only handles whole numbers and true/false logic today.';
103
+ if (/floating-point/.test(reason))
104
+ return `uses decimal (floating-point) numbers; ${needsNumbers} Use \`int\` or \`bigint\` to get a certificate.`;
105
+ if (/strings/.test(reason))
106
+ return `uses text (strings); ${needsNumbers}`;
107
+ return reason;
108
+ }
109
+ /** The status of a law in a table, in words that need no knowledge of solvers. */
110
+ export function statusWords(standing) {
111
+ const base = {
112
+ proved: 'Proved',
113
+ bounded: 'Proved within a bound',
114
+ tested: 'Tested, not proved',
115
+ open: 'Not resolved',
116
+ failed: 'Failed',
117
+ }[standing.outcome];
118
+ if (standing.certification === 'certified')
119
+ return `${base}, independently checked`;
120
+ if (standing.outcome !== 'proved')
121
+ return base;
122
+ return {
123
+ 'not-certifiable': `${base}, can't be certified`,
124
+ 'not-certified': `${base}, not yet checked`,
125
+ rejected: `${base}, the check failed`,
126
+ certified: base,
127
+ }[standing.certification];
128
+ }
129
+ /** The section that opens a certificates page. */
130
+ export const whatIsACertificate = [
131
+ '## What is a certificate?',
132
+ '',
133
+ "tslaws proves a law with a solver. A certificate is the solver's proof written to a file, and a separate, independent checker ([Carcara](https://github.com/ufmg-smite/carcara)) re-checks every step of it. You don't have to trust tslaws or the solver: anyone can re-run the check. A law can only get a certificate if the proof format can express it. Today that means whole numbers and true/false logic, so a law about decimal numbers is proved but has no certificate.",
134
+ '',
135
+ ];
136
+ const groups = [
137
+ {
138
+ title: 'Proved and independently checked',
139
+ intro: 'A solver proved each of these laws, and Carcara re-checked every step of its proof.',
140
+ always: true,
141
+ has: (s) => s.certification === 'certified',
142
+ line: (name) => `- **${name}**`,
143
+ },
144
+ {
145
+ title: "Proved, but the proof can't be checked independently yet",
146
+ intro: 'A solver proved each of these laws, but no certificate that the checker accepted is on record. Run `tslaws check --certify` with cvc5 and Carcara installed to make them.',
147
+ always: true,
148
+ has: (s) => s.outcome === 'proved' && (s.certification === 'not-certified' || s.certification === 'rejected'),
149
+ line: (name, s) => `- **${name}**${s.reason === undefined ? '' : `: ${s.certification === 'rejected' ? `the check did not pass (${s.reason})` : s.reason}`}`,
150
+ },
151
+ {
152
+ title: 'Tested, not proved',
153
+ intro: 'These laws were run on random inputs. Passing is evidence, not a proof that they hold for every input, so there is nothing to certify.',
154
+ always: true,
155
+ has: (s) => s.outcome === 'tested' && s.certification !== 'certified',
156
+ line: (name, s) => `- **${name}**: ${s.runs === undefined ? 'run on random inputs' : `run on ${s.runs} random inputs`} without finding a counterexample${s.note === undefined ? '' : `. The prover said: ${firstLine(s.note)}`}`,
157
+ },
158
+ {
159
+ title: "Can't be certified, and why",
160
+ intro: 'These laws are proved, but the proof format cannot express them, so the checker has nothing to check.',
161
+ always: true,
162
+ has: (s) => s.outcome === 'proved' && s.certification === 'not-certifiable',
163
+ line: (name, s) => `- **${name}**: ${plainReason(s.reason ?? 'the proof format cannot express it')}`,
164
+ },
165
+ {
166
+ title: 'Proved only within a bound',
167
+ intro: 'These laws hold for every input up to a bound (array length, loop passes, a range of numbers), not for all inputs.',
168
+ always: false,
169
+ has: (s) => s.outcome === 'bounded' && s.certification !== 'certified',
170
+ line: (name, s) => `- **${name}**${s.note === undefined ? '' : `: ${firstLine(s.note)}`}`,
171
+ },
172
+ {
173
+ title: 'Not resolved',
174
+ intro: 'The prover could not settle these laws and they are marked `proved`, so the check fails until they are fixed.',
175
+ always: false,
176
+ has: (s) => s.outcome === 'open',
177
+ line: (name, s) => `- **${name}**${s.note === undefined ? '' : `: ${firstLine(s.note)}`}`,
178
+ },
179
+ {
180
+ title: 'Failed',
181
+ intro: 'These laws failed their check.',
182
+ always: false,
183
+ has: (s) => s.outcome === 'failed',
184
+ line: (name) => `- **${name}**`,
185
+ },
186
+ ];
187
+ /** The summary of a certificates page: the laws grouped by what they can and cannot do. Deterministic for a given input. */
188
+ export function certificateSummary(laws) {
189
+ const out = ['## Summary', ''];
190
+ for (const group of groups) {
191
+ const members = laws.filter((law) => group.has(law.standing));
192
+ if (members.length === 0 && !group.always)
193
+ continue;
194
+ out.push(`### ${group.title} (${members.length})`, '', group.intro, '');
195
+ out.push(...(members.length === 0 ? ['None.'] : members.map((law) => group.line(law.name, law.standing))), '');
196
+ }
197
+ return out;
198
+ }
37
199
  /** The text of the two files, one JSON object each. */
38
200
  export function badgeTexts(badges) {
39
201
  return {
@@ -85,17 +247,19 @@ export function repoRelative(cwd, dir, top) {
85
247
  const proofExtensions = ['smt2', 'alethe', 'horn', 'inv'];
86
248
  const cell = (text) => text.replaceAll('|', '\\|').replaceAll('\n', ' ');
87
249
  /**
88
- * The certificates page of `tslaws badge`: every law with its status and, for a certified one, its proof files. They are
89
- * copied from `.tslaws/certificates` into `certificates/` beside the page, so the page links files the repository holds.
90
- * Writes nothing and returns false when no law is certified.
250
+ * The certificates page of `tslaws badge`: what a certificate is, the laws grouped by what they can and cannot do, and
251
+ * every law with its status and, for a certified one, its proof files. They are copied from `.tslaws/certificates` into
252
+ * `certificates/` beside the page, so the page links files the repository holds. Writes nothing and returns false when no
253
+ * law is certified.
91
254
  */
92
255
  export function writeCertificatesPage(cwd, dir, results, lock) {
93
256
  const standings = standingsOf(results, lock);
94
- if (!standings.some((standing) => standing.certified))
257
+ if (!standings.some((standing) => standing.certification === 'certified'))
95
258
  return false;
96
259
  const saved = certificatesDir(cwd);
97
260
  const rows = results.map((result, index) => {
98
- const certified = standings[index].certified;
261
+ const standing = standings[index];
262
+ const certified = standing.certification === 'certified';
99
263
  const name = certificateName(lawKey(result.file, result.name));
100
264
  const present = proofExtensions.filter((ext) => existsSync(join(saved, `${name}.${ext}`)));
101
265
  if (certified && present.length > 0)
@@ -103,17 +267,20 @@ export function writeCertificatesPage(cwd, dir, results, lock) {
103
267
  for (const ext of certified ? present : [])
104
268
  copyFileSync(join(saved, `${name}.${ext}`), join(dir, 'certificates', `${name}.${ext}`));
105
269
  const files = certified && present.length > 0 ? present.map((ext) => `[${ext}](certificates/${name}.${ext})`) : [];
106
- return `| ${cell(result.name)} | ${cell(`${result.file}:${result.line}`)} | ${result.status} | ${certified ? 'certified' : 'not certified'} | ${files.length === 0 ? '-' : files.join(', ')} |`;
270
+ return `| ${cell(result.name)} | ${cell(`${result.file}:${result.line}`)} | ${statusWords(standing)} | ${files.length === 0 ? '-' : files.join(', ')} |`;
107
271
  });
108
- const count = standings.filter((standing) => standing.certified).length;
109
272
  mkdirSync(dir, { recursive: true });
110
273
  writeFileSync(join(dir, 'README.md'), [
111
274
  '# Certificates',
112
275
  '',
113
- `${count} of ${results.length} laws are certified. For a certified law, cvc5 wrote an [Alethe](https://verit.loria.fr/documentation/alethe-spec.pdf) proof of its SMT-LIB problem and [Carcara](https://github.com/ufmg-smite/carcara) accepted it. A law the lock records a certificate for counts as certified, and its proof files are listed when this run saved them. Generated by \`tslaws badge\`; do not edit it by hand.`,
276
+ ...whatIsACertificate,
277
+ ...certificateSummary(results.map((result, index) => ({ name: result.name, standing: standings[index] }))),
278
+ '## Technical details',
279
+ '',
280
+ 'For a certified law, cvc5 wrote an [Alethe](https://verit.loria.fr/documentation/alethe-spec.pdf) proof of its SMT-LIB problem and Carcara accepted it. A law the lock records a certificate for counts as certified, and its proof files are listed when this run saved them. Generated by `tslaws badge`; do not edit it by hand.',
114
281
  '',
115
- '| Law | Source | Status | Certificate | Proof files |',
116
- '| --- | --- | --- | --- | --- |',
282
+ '| Law | Source | Status | Proof files |',
283
+ '| --- | --- | --- | --- |',
117
284
  ...rows,
118
285
  '',
119
286
  ].join('\n'));
package/dist/browser.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import type { Browser } from 'playwright-core';
2
2
  import type * as fc from 'fast-check';
3
+ import type { Plan } from './plan.ts';
3
4
  import { type PropertyOutcome } from './property.ts';
4
5
  import type { CheckOptions } from './types.ts';
5
6
  export type BrowserEngine = 'chromium' | 'firefox' | 'webkit';
@@ -10,7 +11,7 @@ export declare function resolveEngine(requested?: string, env?: NodeJS.ProcessEn
10
11
  * TSLAWS_BROWSER or Playwright's own download. Throws, naming the install command, when there is none.
11
12
  */
12
13
  export declare function engineExecutable(engine: BrowserEngine, env?: NodeJS.ProcessEnv): string;
13
- export declare function launch(engine: BrowserEngine): Promise<Browser>;
14
+ export declare function launch(engine: BrowserEngine, headless?: boolean): Promise<Browser>;
14
15
  export declare function closeBrowser(): Promise<void>;
15
16
  export interface BrowserLaw {
16
17
  file: string;
@@ -19,6 +20,8 @@ export interface BrowserLaw {
19
20
  symbol?: string;
20
21
  jsFile: string;
21
22
  generators: fc.Arbitrary<unknown>[];
23
+ /** Serialized generator plans for zero-IPC in-browser fast-check execution. */
24
+ plans?: Plan[];
22
25
  }
23
- /** Property-tests one law in a page; `'timeout'` when it does not finish within `options.propertyTimeoutMs`. */
26
+ /** Property-tests one browser law on a real HTTP origin with configurable `page` (default) or `iframe` isolation. */
24
27
  export declare function runBrowserLaw(law: BrowserLaw, options: CheckOptions): Promise<PropertyOutcome | 'timeout'>;