@sublang/spex 5.0.0 → 7.0.0-beta.2
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 +453 -41
- package/dist/cli.js +12 -1
- package/dist/commands/add.d.ts +1 -1
- package/dist/commands/add.js +3 -3
- package/dist/commands/auth-target.d.ts +1 -0
- package/dist/commands/auth-target.js +4 -2
- package/dist/commands/environment.d.ts +15 -21
- package/dist/commands/environment.js +43 -57
- package/dist/commands/export.js +2 -2
- package/dist/commands/home.d.ts +2 -2
- package/dist/commands/home.js +3 -3
- package/dist/commands/list.js +1 -1
- package/dist/commands/login.js +12 -7
- package/dist/commands/logout.js +2 -3
- package/dist/commands/publish.js +16 -16
- package/dist/commands/remove.js +1 -1
- package/dist/commands/run.d.ts +1 -1
- package/dist/commands/run.js +22 -3
- package/dist/commands/search.js +1 -1
- package/dist/commands/settle.d.ts +1 -1
- package/dist/commands/settle.js +3 -3
- package/dist/commands/whoami.js +2 -6
- package/dist/guards/approval.d.ts +56 -0
- package/dist/guards/approval.js +158 -0
- package/dist/guards/checks.d.ts +207 -0
- package/dist/guards/checks.js +681 -0
- package/dist/guards/commands.d.ts +15 -0
- package/dist/guards/commands.js +313 -0
- package/dist/guards/copy.d.ts +67 -0
- package/dist/guards/copy.js +306 -0
- package/dist/guards/declaration.d.ts +45 -0
- package/dist/guards/declaration.js +224 -0
- package/dist/guards/files.d.ts +137 -0
- package/dist/guards/files.js +544 -0
- package/dist/guards/harness.d.ts +58 -0
- package/dist/guards/harness.js +420 -0
- package/dist/guards/items.d.ts +61 -0
- package/dist/guards/items.js +161 -0
- package/dist/guards/patch.d.ts +55 -0
- package/dist/guards/patch.js +258 -0
- package/dist/guards/processes.d.ts +49 -0
- package/dist/guards/processes.js +233 -0
- package/dist/guards/proofs.d.ts +88 -0
- package/dist/guards/proofs.js +503 -0
- package/dist/guards/records.d.ts +176 -0
- package/dist/guards/records.js +513 -0
- package/dist/guards/report.d.ts +26 -0
- package/dist/guards/report.js +492 -0
- package/dist/guards/runner.d.ts +42 -0
- package/dist/guards/runner.js +167 -0
- package/dist/guards/tla.d.ts +51 -0
- package/dist/guards/tla.js +352 -0
- package/dist/guards/tlapm.d.ts +81 -0
- package/dist/guards/tlapm.js +172 -0
- package/dist/guards/tlc.d.ts +191 -0
- package/dist/guards/tlc.js +457 -0
- package/dist/guards/translation.d.ts +66 -0
- package/dist/guards/translation.js +202 -0
- package/dist/guards/types.d.ts +185 -0
- package/dist/guards/types.js +437 -0
- package/dist/guards/verify.d.ts +290 -0
- package/dist/guards/verify.js +1191 -0
- package/dist/guards/xml.d.ts +18 -0
- package/dist/guards/xml.js +105 -0
- package/dist/lib/environment/archive.d.ts +16 -3
- package/dist/lib/environment/archive.js +39 -8
- package/dist/lib/environment/builtins.d.ts +10 -12
- package/dist/lib/environment/builtins.js +18 -20
- package/dist/lib/environment/exports.d.ts +14 -13
- package/dist/lib/environment/exports.js +31 -16
- package/dist/lib/environment/format.d.ts +2 -2
- package/dist/lib/environment/format.js +3 -3
- package/dist/lib/environment/git-source.js +6 -3
- package/dist/lib/environment/install.d.ts +14 -16
- package/dist/lib/environment/install.js +66 -61
- package/dist/lib/environment/lock.d.ts +4 -3
- package/dist/lib/environment/lock.js +6 -5
- package/dist/lib/environment/marker.js +1 -2
- package/dist/lib/environment/own-paths.d.ts +7 -10
- package/dist/lib/environment/own-paths.js +10 -13
- package/dist/lib/environment/placed-record.d.ts +3 -2
- package/dist/lib/environment/placed-record.js +5 -4
- package/dist/lib/environment/registries.d.ts +7 -9
- package/dist/lib/environment/registries.js +9 -36
- package/dist/lib/environment/registry.d.ts +6 -7
- package/dist/lib/environment/registry.js +12 -12
- package/dist/lib/environment/requests.d.ts +6 -5
- package/dist/lib/environment/requests.js +6 -8
- package/dist/lib/environment/resolve.d.ts +4 -4
- package/dist/lib/environment/resolve.js +3 -4
- package/dist/lib/environment/store.d.ts +34 -8
- package/dist/lib/environment/store.js +138 -43
- package/dist/lib/files.d.ts +72 -12
- package/dist/lib/files.js +200 -65
- package/dist/lib/git-host.d.ts +52 -258
- package/dist/lib/git-host.js +52 -265
- package/dist/lib/home-file.d.ts +13 -10
- package/dist/lib/home-file.js +28 -18
- package/dist/lib/instance.d.ts +1 -1
- package/dist/lib/instance.js +1 -1
- package/dist/lint.d.ts +22 -0
- package/dist/lint.js +36 -0
- package/dist/testing/standin-registry.js +1 -1
- package/package.json +7 -3
- package/scaffold/.file-history.json +14 -7
- package/scaffold/i18n/zh/source-pins.json +41 -0
- package/scaffold/i18n/zh/specs/decisions/000-spec-structure-format.md +0 -1
- package/scaffold/i18n/zh/specs/intents/000-spdx-headers.md +0 -1
- package/scaffold/i18n/zh/specs/map.md +0 -1
- package/scaffold/i18n/zh/specs/meta.md +18 -35
- package/scaffold/i18n/zh/specs/packages/git.md +0 -1
- package/scaffold/i18n/zh/specs/packages/licensing.md +0 -1
- package/scaffold/specs/meta.md +18 -3
- package/scripts/test-exit-guard.mjs +28 -0
|
@@ -0,0 +1,306 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// SPDX-FileCopyrightText: 2026 SubLang International <https://sublang.ai>
|
|
3
|
+
/*
|
|
4
|
+
* Guards package (guards-12, guards-28), DR-252: the copy a run
|
|
5
|
+
* checks. It is made in a fresh folder under the OS temp folder: the
|
|
6
|
+
* binding's `files` and the binding's own folder at the paths they
|
|
7
|
+
* have in the project, links followed, every `node_modules` and
|
|
8
|
+
* `.git` folder left out, and the project's `node_modules` folders
|
|
9
|
+
* linked in so the copy uses the installed dependencies. Beside the
|
|
10
|
+
* copy, outside it, stand its temp folder and the empty folder of
|
|
11
|
+
* records a probe writes into.
|
|
12
|
+
*
|
|
13
|
+
* Nothing is written in the project. Every folder made here is
|
|
14
|
+
* removed when its checks end, and whatever is left is removed when
|
|
15
|
+
* the process exits, however it exits.
|
|
16
|
+
*/
|
|
17
|
+
import { createHash } from "node:crypto";
|
|
18
|
+
import { chmodSync, existsSync, lstatSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, readlinkSync, realpathSync, rmSync, statSync, symlinkSync, writeFileSync, } from "node:fs";
|
|
19
|
+
import { tmpdir } from "node:os";
|
|
20
|
+
import { dirname, join, parse, relative, sep } from "node:path";
|
|
21
|
+
import { pathToFileURL } from "node:url";
|
|
22
|
+
import { byText } from "./types.js";
|
|
23
|
+
/** Entries that never go into a copy, whatever they are: the
|
|
24
|
+
* project's installed dependencies and its Git folder, which a copy
|
|
25
|
+
* links or leaves, and the file a Mac leaves in a folder it showed. */
|
|
26
|
+
const LEFT_OUT = new Set(["node_modules", ".git", ".DS_Store"]);
|
|
27
|
+
/** Whether an entry of this name is left out of every copy. A guard's
|
|
28
|
+
* inputs leave out the same entries of a binding's folder, so that a
|
|
29
|
+
* copy holds exactly the files the inputs name (guards-12, guards-14). */
|
|
30
|
+
export function leftOut(name) {
|
|
31
|
+
return LEFT_OUT.has(name);
|
|
32
|
+
}
|
|
33
|
+
/** Whether the real path `real` is `base` or under it. Both are given
|
|
34
|
+
* as `realpathSync.native` gives them, which on Windows is the long
|
|
35
|
+
* name on either side. */
|
|
36
|
+
export function inside(base, real) {
|
|
37
|
+
return real === base || real.startsWith(base.endsWith(sep) ? base : base + sep);
|
|
38
|
+
}
|
|
39
|
+
const live = new Set();
|
|
40
|
+
let hooked = false;
|
|
41
|
+
/** Remove every copy still standing. Safe to call at any time. */
|
|
42
|
+
export function removeCopies() {
|
|
43
|
+
for (const home of [...live])
|
|
44
|
+
removeHome(home);
|
|
45
|
+
}
|
|
46
|
+
function removeHome(home) {
|
|
47
|
+
live.delete(home);
|
|
48
|
+
// A linked node_modules is unlinked, never followed.
|
|
49
|
+
const remove = () => rmSync(home, { recursive: true, force: true, maxRetries: 3, retryDelay: 100 });
|
|
50
|
+
try {
|
|
51
|
+
remove();
|
|
52
|
+
}
|
|
53
|
+
catch {
|
|
54
|
+
try {
|
|
55
|
+
// A copied folder keeps the project's mode, and one its owner
|
|
56
|
+
// may not write cannot be emptied until he may.
|
|
57
|
+
unlock(home);
|
|
58
|
+
remove();
|
|
59
|
+
}
|
|
60
|
+
catch {
|
|
61
|
+
// Nothing more can be done for a temp folder that will not go.
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
/** Give the owner every right in `dir` and in each folder under it,
|
|
66
|
+
* links not followed. */
|
|
67
|
+
function unlock(dir) {
|
|
68
|
+
chmodSync(dir, 0o700);
|
|
69
|
+
for (const entry of readdirSync(dir, { withFileTypes: true })) {
|
|
70
|
+
if (entry.isDirectory())
|
|
71
|
+
unlock(join(dir, entry.name));
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
/** Remove one copy. */
|
|
75
|
+
export function removeCopy(copy) {
|
|
76
|
+
removeHome(copy.home);
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* A fresh folder under the OS temp folder for what a run writes
|
|
80
|
+
* beside its copies: the TLA+ files it generates and what TLC leaves.
|
|
81
|
+
* It is removed with the copies, however the process exits.
|
|
82
|
+
*/
|
|
83
|
+
export function makeHome() {
|
|
84
|
+
const home = realpathSync.native(mkdtempSync(join(tmpdir(), "spex-verify-")));
|
|
85
|
+
live.add(home);
|
|
86
|
+
if (!hooked) {
|
|
87
|
+
hooked = true;
|
|
88
|
+
process.on("exit", removeCopies);
|
|
89
|
+
}
|
|
90
|
+
return home;
|
|
91
|
+
}
|
|
92
|
+
/** Remove a folder `makeHome` made. */
|
|
93
|
+
export function removeFolder(home) {
|
|
94
|
+
removeHome(home);
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* `text` with every path into a copy told relative to the copy's
|
|
98
|
+
* folder, as a path or as a `file:` URL: `src/lock.mjs` for a copied
|
|
99
|
+
* file, `.` for the folder itself and `../tmp/…` for what lies in the
|
|
100
|
+
* copy's temp folder. `home` is the copy's fresh folder, whose name is
|
|
101
|
+
* new in every run, so a cause told this way is the same in every run
|
|
102
|
+
* (guards-36).
|
|
103
|
+
*/
|
|
104
|
+
export function relativeToCopy(text, home) {
|
|
105
|
+
const folder = join(home, "copy");
|
|
106
|
+
const swap = (told, prefix, slash, inside, alone) => told.split(prefix + slash).join(inside).split(prefix).join(alone);
|
|
107
|
+
let told = text;
|
|
108
|
+
told = swap(told, pathToFileURL(folder).href, "/", "", ".");
|
|
109
|
+
told = swap(told, folder, sep, "", ".");
|
|
110
|
+
told = swap(told, pathToFileURL(home).href, "/", "../", "..");
|
|
111
|
+
told = swap(told, home, sep, `..${sep}`, "..");
|
|
112
|
+
return told;
|
|
113
|
+
}
|
|
114
|
+
function abs(base, path) {
|
|
115
|
+
return path === "" ? base : join(base, ...path.split("/"));
|
|
116
|
+
}
|
|
117
|
+
function isDir(path) {
|
|
118
|
+
return existsSync(path) && statSync(path).isDirectory();
|
|
119
|
+
}
|
|
120
|
+
/** The folders above `path`, the root (`""`) first. */
|
|
121
|
+
function above(path) {
|
|
122
|
+
const parts = path.split("/");
|
|
123
|
+
return parts.map((_, index) => parts.slice(0, index).join("/"));
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Add to `linked` every link met on the way from the project's real
|
|
127
|
+
* path `base` to `path`, one above it and one that a link's own text
|
|
128
|
+
* passes through among them, each by its real place in the project:
|
|
129
|
+
* each decides what is copied, and may be no part of a declared path.
|
|
130
|
+
*/
|
|
131
|
+
function linksOn(base, path, linked) {
|
|
132
|
+
// `at` holds no link: each one met is replaced by where it leads.
|
|
133
|
+
let at = base;
|
|
134
|
+
let rest = path.split("/");
|
|
135
|
+
// No more links than a system follows before it calls them a loop.
|
|
136
|
+
for (let hops = 0; rest.length > 0 && hops <= 40;) {
|
|
137
|
+
const part = rest.shift();
|
|
138
|
+
if (part === "" || part === ".")
|
|
139
|
+
continue;
|
|
140
|
+
if (part === "..") {
|
|
141
|
+
at = dirname(at);
|
|
142
|
+
continue;
|
|
143
|
+
}
|
|
144
|
+
const next = join(at, part);
|
|
145
|
+
let target;
|
|
146
|
+
try {
|
|
147
|
+
if (!lstatSync(next).isSymbolicLink()) {
|
|
148
|
+
at = next;
|
|
149
|
+
continue;
|
|
150
|
+
}
|
|
151
|
+
target = readlinkSync(next);
|
|
152
|
+
}
|
|
153
|
+
catch {
|
|
154
|
+
// Not there, or not to be read: the copy itself says so.
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
hops += 1;
|
|
158
|
+
if (inside(base, next))
|
|
159
|
+
linked.add(relative(base, next).split(sep).join("/"));
|
|
160
|
+
const start = parse(target).root;
|
|
161
|
+
rest = [...target.slice(start.length).split(sep === "/" ? "/" : /[\\/]/), ...rest];
|
|
162
|
+
if (start !== "")
|
|
163
|
+
at = start;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Copy `path` (a file or a folder, relative to the root) from the
|
|
168
|
+
* project into the copy. `files` gathers every file copied and
|
|
169
|
+
* `folders` every folder walked; `held` is the chain of real folders
|
|
170
|
+
* being walked, so a link back into it is refused and not followed
|
|
171
|
+
* for ever. `base` is the project's real path: a link is followed
|
|
172
|
+
* only where it leads, so an entry that leads out of the project is
|
|
173
|
+
* refused, and a copy holds nothing a fresh checkout would lack.
|
|
174
|
+
* `expected` is the real path the entry has where no link is met on
|
|
175
|
+
* the way to it; where it has another, a link led there, and `linked`
|
|
176
|
+
* gathers the place and every link met on the way. `modes` gathers
|
|
177
|
+
* the mode of each folder copied, given to it once the copy is whole.
|
|
178
|
+
*/
|
|
179
|
+
function copyEntry(root, base, folder, path, expected, files, folders, linked, held, modes) {
|
|
180
|
+
const from = abs(root, path);
|
|
181
|
+
const to = abs(folder, path);
|
|
182
|
+
const stat = statSync(from);
|
|
183
|
+
const real = realpathSync.native(from);
|
|
184
|
+
if (!inside(base, real))
|
|
185
|
+
throw new Error(`${path} leads out of the project`);
|
|
186
|
+
if (real !== expected) {
|
|
187
|
+
linksOn(base, path, linked);
|
|
188
|
+
linked.add(relative(base, real).split(sep).join("/"));
|
|
189
|
+
}
|
|
190
|
+
if (stat.isDirectory()) {
|
|
191
|
+
if (held.includes(real))
|
|
192
|
+
throw new Error(`${path} links back to a folder that holds it`);
|
|
193
|
+
folders.add(path);
|
|
194
|
+
modes.set(path, stat.mode & 0o777);
|
|
195
|
+
mkdirSync(to, { recursive: true });
|
|
196
|
+
for (const name of readdirSync(from).sort(byText)) {
|
|
197
|
+
if (leftOut(name))
|
|
198
|
+
continue;
|
|
199
|
+
copyEntry(root, base, folder, `${path}/${name}`, join(real, name), files, folders, linked, [...held, real], modes);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
else if (stat.isFile()) {
|
|
203
|
+
// A file an earlier declared path already brought is copied once.
|
|
204
|
+
if (files.has(path))
|
|
205
|
+
return;
|
|
206
|
+
mkdirSync(dirname(to), { recursive: true });
|
|
207
|
+
// The file's own mode, whatever the mask of this process: the code
|
|
208
|
+
// in a copy meets its files as the project keeps them, a read-only
|
|
209
|
+
// one read-only.
|
|
210
|
+
writeFileSync(to, readFileSync(from), { mode: stat.mode & 0o777 });
|
|
211
|
+
chmodSync(to, stat.mode & 0o777);
|
|
212
|
+
files.add(path);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
/** One SHA-256 over each path and its bytes, paths in order, with a
|
|
216
|
+
* mark for a file its owner may execute: the one thing of a file's
|
|
217
|
+
* mode that changes what running the code does and that Git keeps. */
|
|
218
|
+
function digestOf(folder, files) {
|
|
219
|
+
const hash = createHash("sha256");
|
|
220
|
+
for (const path of files) {
|
|
221
|
+
const file = abs(folder, path);
|
|
222
|
+
const bytes = readFileSync(file);
|
|
223
|
+
hash.update(`${path}\0${bytes.length}\0`);
|
|
224
|
+
if ((statSync(file).mode & 0o100) !== 0)
|
|
225
|
+
hash.update("x\0");
|
|
226
|
+
hash.update(bytes);
|
|
227
|
+
}
|
|
228
|
+
return hash.digest("hex");
|
|
229
|
+
}
|
|
230
|
+
/** One SHA-256 over the digest of the copied files and the mode of
|
|
231
|
+
* each file and each folder copied. */
|
|
232
|
+
function stateOf(folder, digest, files, folders) {
|
|
233
|
+
const hash = createHash("sha256");
|
|
234
|
+
hash.update(`${digest}\0`);
|
|
235
|
+
for (const path of files)
|
|
236
|
+
hash.update(`${path}\0${statSync(abs(folder, path)).mode & 0o777}\0`);
|
|
237
|
+
for (const [path, mode] of folders)
|
|
238
|
+
hash.update(`${path}/\0${mode}\0`);
|
|
239
|
+
return hash.digest("hex");
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* Make a copy of `paths` (files and folders relative to `root`) in a
|
|
243
|
+
* fresh folder under the OS temp folder (guards-12, steps 1 to 3).
|
|
244
|
+
* Every folder of the copy whose project counterpart holds a
|
|
245
|
+
* `node_modules` gets a link to it, as does the root and each folder
|
|
246
|
+
* above a copied path. An error leaves no folder behind.
|
|
247
|
+
*/
|
|
248
|
+
export function makeCopy(root, paths) {
|
|
249
|
+
const home = realpathSync.native(mkdtempSync(join(tmpdir(), "spex-guard-")));
|
|
250
|
+
live.add(home);
|
|
251
|
+
if (!hooked) {
|
|
252
|
+
hooked = true;
|
|
253
|
+
process.on("exit", removeCopies);
|
|
254
|
+
}
|
|
255
|
+
try {
|
|
256
|
+
const folder = join(home, "copy");
|
|
257
|
+
const scratch = join(home, "tmp");
|
|
258
|
+
const records = join(home, "records");
|
|
259
|
+
mkdirSync(folder);
|
|
260
|
+
mkdirSync(scratch);
|
|
261
|
+
mkdirSync(records);
|
|
262
|
+
const files = new Set();
|
|
263
|
+
const folders = new Set();
|
|
264
|
+
const linked = new Set();
|
|
265
|
+
const modes = new Map();
|
|
266
|
+
const base = realpathSync.native(root);
|
|
267
|
+
for (const path of paths) {
|
|
268
|
+
for (const parent of above(path)) {
|
|
269
|
+
folders.add(parent);
|
|
270
|
+
// The root and each folder above a declared path stand for a
|
|
271
|
+
// folder of the project too, and take its mode as a copied
|
|
272
|
+
// folder does: code may write beside a declared file only
|
|
273
|
+
// where the project lets it.
|
|
274
|
+
if (!modes.has(parent))
|
|
275
|
+
modes.set(parent, statSync(abs(root, parent)).mode & 0o777);
|
|
276
|
+
// It stands in the copy whatever the declared path is: one
|
|
277
|
+
// that is neither a file nor a folder is left out below it.
|
|
278
|
+
mkdirSync(abs(folder, parent), { recursive: true });
|
|
279
|
+
}
|
|
280
|
+
copyEntry(root, base, folder, path, join(base, ...path.split("/")), files, folders, linked, [], modes);
|
|
281
|
+
}
|
|
282
|
+
for (const path of folders) {
|
|
283
|
+
const modules = join(abs(root, path), "node_modules");
|
|
284
|
+
if (!isDir(modules))
|
|
285
|
+
continue;
|
|
286
|
+
mkdirSync(abs(folder, path), { recursive: true });
|
|
287
|
+
// A junction on Windows, which needs no special right.
|
|
288
|
+
symlinkSync(modules, join(abs(folder, path), "node_modules"), "junction");
|
|
289
|
+
}
|
|
290
|
+
const copied = [...files].sort(byText);
|
|
291
|
+
const digest = digestOf(folder, copied);
|
|
292
|
+
// The mode of each folder of the copy that stands for one of the
|
|
293
|
+
// project, a folder after those it holds and the root last:
|
|
294
|
+
// nothing more is written into one its owner may not write.
|
|
295
|
+
const depth = (path) => (path === "" ? 0 : path.split("/").length);
|
|
296
|
+
const within = [...modes].sort(([a], [b]) => depth(b) - depth(a) || byText(a, b));
|
|
297
|
+
for (const [path, mode] of within)
|
|
298
|
+
chmodSync(abs(folder, path), mode);
|
|
299
|
+
return { home, folder, scratch, records, digest, state: stateOf(folder, digest, copied, within), files: copied, linked: [...linked].sort(byText) };
|
|
300
|
+
}
|
|
301
|
+
catch (error) {
|
|
302
|
+
removeHome(home);
|
|
303
|
+
throw new Error(relativeToCopy(error instanceof Error ? error.message : String(error), home));
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
//# sourceMappingURL=copy.js.map
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { type Declaration, type Type } from "./types.js";
|
|
2
|
+
/** The TLA+ module every translation extends. */
|
|
3
|
+
export declare const DECLARATION_MODULE = "Declaration";
|
|
4
|
+
/** The seven operators a translation defines, with how many
|
|
5
|
+
* parameters each takes. */
|
|
6
|
+
export declare const OPERATORS: readonly (readonly [name: string, params: readonly string[]])[];
|
|
7
|
+
/** The theorems a proof file holds: the one that proves the model
|
|
8
|
+
* keeps the rule, and, for every translation but the first, the one
|
|
9
|
+
* that proves it agrees with the first. */
|
|
10
|
+
export declare const RULE_PROVED = "RuleProved";
|
|
11
|
+
export declare const AGREEMENT_PROVED = "AgreementProved";
|
|
12
|
+
/** Every name the statements a proof file proves, and the module that
|
|
13
|
+
* proves them from it, define (`proofs.ts`). */
|
|
14
|
+
export declare const STATEMENT_NAMES: readonly string[];
|
|
15
|
+
/** Names the declaration in TLA+ may not define for a values set,
|
|
16
|
+
* because TLA+, a module a translation may load, a translation or one
|
|
17
|
+
* of Spex's own TLA+ files defines them. */
|
|
18
|
+
export declare const RESERVED: ReadonlySet<string>;
|
|
19
|
+
/** A values set's name in TLA+: its first letter in upper case. */
|
|
20
|
+
export declare function setName(set: string): string;
|
|
21
|
+
/** The names Spex's files for TLC define for a set: its declared
|
|
22
|
+
* values, where the configuration file cannot write them, and its
|
|
23
|
+
* declared values in order with the order they give. */
|
|
24
|
+
export declare function spexNames(set: string): [values: string, order: string, before: string];
|
|
25
|
+
/** The names the file that judges the records (`SpexRecords`) defines
|
|
26
|
+
* for a set beside those of `spexNames`: the place of each value in
|
|
27
|
+
* its order, and the two assumptions TLC checks of the values. */
|
|
28
|
+
export declare function recordsNames(set: string): [place: string, once: string, notMissing: string];
|
|
29
|
+
/**
|
|
30
|
+
* Refuse a declaration whose generated names would clash: a values
|
|
31
|
+
* set, a param or an effect field TLA+ could not hold under its name.
|
|
32
|
+
* Throws a GuardError; the caller adds the file.
|
|
33
|
+
*/
|
|
34
|
+
export declare function checkNames(declaration: Declaration): void;
|
|
35
|
+
/** A type as the TLA+ set of its values. */
|
|
36
|
+
export declare function typeSet(type: Type): string;
|
|
37
|
+
/** The text of `Declaration.tla` for the binding `binding`. */
|
|
38
|
+
export declare function declarationModule(declaration: Declaration, binding: string): string;
|
|
39
|
+
/**
|
|
40
|
+
* The name of the assumption of the declaration in TLA+ `text` that
|
|
41
|
+
* spans line `line` (from 1), or null. TLC names a false assumption
|
|
42
|
+
* only by the place of its body, which may be the line after
|
|
43
|
+
* `ASSUME <name> ==`.
|
|
44
|
+
*/
|
|
45
|
+
export declare function assumptionAt(text: string, line: number): string | null;
|
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// SPDX-FileCopyrightText: 2026 SubLang International <https://sublang.ai>
|
|
3
|
+
/*
|
|
4
|
+
* Guards package (guards-8), DR-244 and DR-252: the declaration in
|
|
5
|
+
* TLA+. It is the file `Declaration.tla` that Spex generates from a
|
|
6
|
+
* binding's declaration, and the vocabulary every translation is
|
|
7
|
+
* written in. The same declaration always gives the same bytes. Text
|
|
8
|
+
* is written from data only: nothing here evaluates TLA+.
|
|
9
|
+
*/
|
|
10
|
+
import { tlaString } from "./tla.js";
|
|
11
|
+
import { GuardError } from "./types.js";
|
|
12
|
+
/** The TLA+ module every translation extends. */
|
|
13
|
+
export const DECLARATION_MODULE = "Declaration";
|
|
14
|
+
/** The seven operators a translation defines, with how many
|
|
15
|
+
* parameters each takes. */
|
|
16
|
+
export const OPERATORS = [
|
|
17
|
+
["Start", []],
|
|
18
|
+
["Legal", ["s", "e"]],
|
|
19
|
+
["Effect", ["s", "e"]],
|
|
20
|
+
["After", ["s", "e"]],
|
|
21
|
+
["When", ["e"]],
|
|
22
|
+
["While", ["s", "e"]],
|
|
23
|
+
["Shall", ["s", "e", "f"]],
|
|
24
|
+
];
|
|
25
|
+
/** The theorems a proof file holds: the one that proves the model
|
|
26
|
+
* keeps the rule, and, for every translation but the first, the one
|
|
27
|
+
* that proves it agrees with the first. */
|
|
28
|
+
export const RULE_PROVED = "RuleProved";
|
|
29
|
+
export const AGREEMENT_PROVED = "AgreementProved";
|
|
30
|
+
/** Every name the statements a proof file proves, and the module that
|
|
31
|
+
* proves them from it, define (`proofs.ts`). */
|
|
32
|
+
export const STATEMENT_NAMES = [
|
|
33
|
+
"T", "F", "state", "first", "other",
|
|
34
|
+
"RuleInit", "RuleNext", "RuleSpec", "RuleAt", "KeepsRule",
|
|
35
|
+
"AgreeInit", "AgreeNext", "AgreeSpec", "AgreeAt", "Agrees",
|
|
36
|
+
RULE_PROVED, AGREEMENT_PROVED, "SpexKeepsRule", "SpexAgrees",
|
|
37
|
+
];
|
|
38
|
+
/** What the proof system's module TLAPS defines, which every proof
|
|
39
|
+
* file extends. */
|
|
40
|
+
const TLAPS_NAMES = [
|
|
41
|
+
"AllIsa", "AllIsaT", "AllProvers", "AllProversT", "AllSMT", "AllSMTT", "Auto", "AutoBlast", "AutoUSE", "Blast",
|
|
42
|
+
"CdotWrapper", "ENABLEDaxioms", "ENABLEDrewrites", "ENABLEDrules", "EnabledWrapper", "ExpandCdot", "ExpandENABLED",
|
|
43
|
+
"Force", "Isa", "IsaM", "IsaMT", "IsaT", "IsaWithSetExtensionality", "Lambdify", "LevelComparison", "PTL",
|
|
44
|
+
"PropositionalTemporalLogic", "SMT", "SMTT", "SimpleArithmetic", "Simplification", "SimplifyAndSolve", "SlowAuto",
|
|
45
|
+
"SlowBlast", "SlowForce", "SlowSimplification", "SlowSimplifyAndSolve", "SlowZenon", "SlowerAuto", "SlowerBlast",
|
|
46
|
+
"SlowerForce", "SlowerSimplification", "SlowerSimplifyAndSolve", "SlowerZenon", "SlowestAuto", "SlowestBlast",
|
|
47
|
+
"SlowestForce", "SlowestSimplification", "SlowestSimplifyAndSolve", "SlowestZenon", "Spass", "SpassT", "Trivial",
|
|
48
|
+
"VerySlowZenon", "Yices", "YicesT", "Zenon", "ZenonT", "Zipper", "ZipperT", "veriT", "veriTT",
|
|
49
|
+
"SetExtensionality", "NoSetContainsEverything", "RuleTLA1", "RuleTLA2", "RuleINV1", "RuleINV2", "RuleWF1", "RuleSF1",
|
|
50
|
+
"RuleWF2", "RuleSF2", "RuleInvImplication", "RuleStepSimulation",
|
|
51
|
+
];
|
|
52
|
+
/** Names the declaration in TLA+ may not define for a values set,
|
|
53
|
+
* because TLA+, a module a translation may load, a translation or one
|
|
54
|
+
* of Spex's own TLA+ files defines them. */
|
|
55
|
+
export const RESERVED = new Set([
|
|
56
|
+
// The declaration's own fixed names.
|
|
57
|
+
"Missing", "Events", "Effects",
|
|
58
|
+
// A translation's seven operators.
|
|
59
|
+
...OPERATORS.map(([name]) => name),
|
|
60
|
+
// Naturals, Integers, FiniteSets and Sequences.
|
|
61
|
+
"Nat", "Int", "Seq", "Len", "Head", "Tail", "Append", "SubSeq", "SelectSeq", "IsFiniteSet", "Cardinality",
|
|
62
|
+
// The module TLC, which two of Spex's own files extend.
|
|
63
|
+
"Print", "PrintT", "Assert", "JavaTime", "TLCGet", "TLCSet", "Permutations", "SortSeq", "RandomElement", "Any",
|
|
64
|
+
"ToString", "TLCEval",
|
|
65
|
+
// Spex's own files.
|
|
66
|
+
"T", "M", "Form", "Agrees", "Applies", "Forbidden", "Same", "Difference", "Row", "Init", "Take", "Land", "Next",
|
|
67
|
+
"Agree", "Declared", "Kept", "Reached", "Judge",
|
|
68
|
+
// The name the declaration in TLA+ gives a set it speaks of.
|
|
69
|
+
"S",
|
|
70
|
+
// The words of the file that gives TLC a model's values, where a
|
|
71
|
+
// set is named.
|
|
72
|
+
"INIT", "NEXT", "SPECIFICATION", "INVARIANT", "INVARIANTS", "PROPERTY", "PROPERTIES", "CONSTRAINT", "CONSTRAINTS",
|
|
73
|
+
"ACTION_CONSTRAINT", "ACTION_CONSTRAINTS", "SYMMETRY", "VIEW", "ALIAS", "POSTCONDITION", "CHECK_DEADLOCK",
|
|
74
|
+
// The statements a proof file proves, and TLAPS, which it extends.
|
|
75
|
+
...STATEMENT_NAMES,
|
|
76
|
+
...TLAPS_NAMES,
|
|
77
|
+
// The words of TLA+.
|
|
78
|
+
"ACTION", "ASSUME", "ASSUMPTION", "AXIOM", "BOOLEAN", "BY", "CASE", "CHOOSE", "CONSTANT", "CONSTANTS",
|
|
79
|
+
"COROLLARY", "DEF", "DEFINE", "DEFS", "DOMAIN", "ELSE", "ENABLED", "EXCEPT", "EXTENDS", "FALSE", "HAVE", "HIDE",
|
|
80
|
+
"IF", "IN", "INSTANCE", "LAMBDA", "LEMMA", "LET", "LOCAL", "MODULE", "NEW", "OBVIOUS", "OMITTED", "ONLY", "OTHER",
|
|
81
|
+
"PICK", "PROOF", "PROPOSITION", "PROVE", "QED", "RECURSIVE", "STATE", "STRING", "SUBSET", "SUFFICES", "TAKE",
|
|
82
|
+
"TEMPORAL", "THEN", "THEOREM", "TRUE", "UNCHANGED", "UNION", "USE", "VARIABLE", "VARIABLES", "WITH", "WITNESS",
|
|
83
|
+
]);
|
|
84
|
+
/** A values set's name in TLA+: its first letter in upper case. */
|
|
85
|
+
export function setName(set) {
|
|
86
|
+
return set[0].toUpperCase() + set.slice(1);
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* The names generated for one values set, whether or not it has an
|
|
90
|
+
* order, so that giving a set an order never makes its name taken:
|
|
91
|
+
* in the declaration in TLA+ the set, its order, its first member,
|
|
92
|
+
* its finite subsets and the four assumptions on them; in Spex's own
|
|
93
|
+
* files for TLC the values, the order between them, the place of each
|
|
94
|
+
* value in it and the assumptions on them.
|
|
95
|
+
*/
|
|
96
|
+
function namesOf(set) {
|
|
97
|
+
const name = setName(set);
|
|
98
|
+
return [
|
|
99
|
+
name,
|
|
100
|
+
`${name}Before`,
|
|
101
|
+
`${name}First`,
|
|
102
|
+
`${name}Sets`,
|
|
103
|
+
...assumptionNames(set),
|
|
104
|
+
...spexNames(set),
|
|
105
|
+
...recordsNames(set),
|
|
106
|
+
];
|
|
107
|
+
}
|
|
108
|
+
/** The four assumptions the declaration in TLA+ states of a set:
|
|
109
|
+
* Missing is not in it, and, where it is ordered, its order is a
|
|
110
|
+
* strict total order. */
|
|
111
|
+
function assumptionNames(set) {
|
|
112
|
+
const name = setName(set);
|
|
113
|
+
return [`${name}NotMissing`, `${name}BeforeIrreflexive`, `${name}BeforeTransitive`, `${name}BeforeTotal`];
|
|
114
|
+
}
|
|
115
|
+
/** The names Spex's files for TLC define for a set: its declared
|
|
116
|
+
* values, where the configuration file cannot write them, and its
|
|
117
|
+
* declared values in order with the order they give. */
|
|
118
|
+
export function spexNames(set) {
|
|
119
|
+
const name = setName(set);
|
|
120
|
+
return [`Spex${name}Values`, `Spex${name}Order`, `Spex${name}Before`];
|
|
121
|
+
}
|
|
122
|
+
/** The names the file that judges the records (`SpexRecords`) defines
|
|
123
|
+
* for a set beside those of `spexNames`: the place of each value in
|
|
124
|
+
* its order, and the two assumptions TLC checks of the values. */
|
|
125
|
+
export function recordsNames(set) {
|
|
126
|
+
const name = setName(set);
|
|
127
|
+
return [`Spex${name}Place`, `Spex${name}Once`, `Spex${name}NotMissing`];
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Refuse a declaration whose generated names would clash: a values
|
|
131
|
+
* set, a param or an effect field TLA+ could not hold under its name.
|
|
132
|
+
* Throws a GuardError; the caller adds the file.
|
|
133
|
+
*/
|
|
134
|
+
export function checkNames(declaration) {
|
|
135
|
+
const taken = new Map();
|
|
136
|
+
for (const set of Object.keys(declaration.vocabulary.sets)) {
|
|
137
|
+
for (const name of namesOf(set)) {
|
|
138
|
+
if (RESERVED.has(name)) {
|
|
139
|
+
throw new GuardError(`values: '${set}' cannot name a values set: the declaration in TLA+ would define ${name}, which is taken`);
|
|
140
|
+
}
|
|
141
|
+
const other = taken.get(name);
|
|
142
|
+
if (other !== undefined) {
|
|
143
|
+
throw new GuardError(`values: '${set}' cannot name a values set: the declaration in TLA+ would define ${name}, as values ${other} does`);
|
|
144
|
+
}
|
|
145
|
+
taken.set(name, set);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
for (const [event, declared] of Object.entries(declaration.events)) {
|
|
149
|
+
if (Object.hasOwn(declared.params, "event")) {
|
|
150
|
+
throw new GuardError(`event ${event}: params: 'event' names the event itself; pick another name`);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
/** A type as the TLA+ set of its values. */
|
|
155
|
+
export function typeSet(type) {
|
|
156
|
+
switch (type.kind) {
|
|
157
|
+
case "bool":
|
|
158
|
+
return "BOOLEAN";
|
|
159
|
+
case "int": {
|
|
160
|
+
const bound = (n) => (n < 0 ? `(${n})` : String(n));
|
|
161
|
+
return `(${bound(type.min)}..${bound(type.max)})`;
|
|
162
|
+
}
|
|
163
|
+
case "value":
|
|
164
|
+
return setName(type.set);
|
|
165
|
+
case "set":
|
|
166
|
+
// A values set is any set, so its subsets are only its finite
|
|
167
|
+
// ones; a range of whole numbers is finite already.
|
|
168
|
+
return type.of.kind === "value" ? `${setName(type.of.set)}Sets` : `(SUBSET ${typeSet(type.of)})`;
|
|
169
|
+
case "optional":
|
|
170
|
+
return `(${typeSet(type.of)} \\cup {Missing})`;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
/** The set of the records with the given fields; `{<<>>}` for none. */
|
|
174
|
+
function recordSet(fields, first = []) {
|
|
175
|
+
const parts = [...first, ...Object.entries(fields).map(([name, type]) => `${name} : ${typeSet(type)}`)];
|
|
176
|
+
return parts.length === 0 ? "{<<>>}" : `[${parts.join(", ")}]`;
|
|
177
|
+
}
|
|
178
|
+
/** The text of `Declaration.tla` for the binding `binding`. */
|
|
179
|
+
export function declarationModule(declaration, binding) {
|
|
180
|
+
const lines = [
|
|
181
|
+
`---- MODULE ${DECLARATION_MODULE} ----`,
|
|
182
|
+
`\\* Generated by Spex from guards/bindings/${binding}/binding.yaml. Do not edit.`,
|
|
183
|
+
"EXTENDS Integers, FiniteSets, Sequences",
|
|
184
|
+
"",
|
|
185
|
+
"\\* The value of a field of a type `T?` that holds nothing.",
|
|
186
|
+
"CONSTANT Missing",
|
|
187
|
+
];
|
|
188
|
+
// Every set is a parameter, so what a proof proves holds for any
|
|
189
|
+
// values; only these assumptions are made of them. TLC is given
|
|
190
|
+
// the declared values by its configuration files.
|
|
191
|
+
const { vocabulary } = declaration;
|
|
192
|
+
for (const set of Object.keys(vocabulary.sets)) {
|
|
193
|
+
const name = setName(set);
|
|
194
|
+
const [notMissing, irreflexive, transitive, total] = assumptionNames(set);
|
|
195
|
+
const before = `${name}Before`;
|
|
196
|
+
if (!Object.hasOwn(vocabulary.orders, set)) {
|
|
197
|
+
lines.push("", `\\* values.${set}: any set of values, in no order the binding declares.`, `CONSTANT ${name}`, `ASSUME ${notMissing} == Missing \\notin ${name}`, `${name}Sets == {S \\in SUBSET ${name} : IsFiniteSet(S)}`);
|
|
198
|
+
continue;
|
|
199
|
+
}
|
|
200
|
+
lines.push("", `\\* values.${set}: any set of values, with the order the binding declares,`, "\\* and the member of a nonempty finite set of them that comes first in it.", `CONSTANT ${name}, ${before}(_, _)`, `ASSUME ${notMissing} == Missing \\notin ${name}`, `ASSUME ${irreflexive} == \\A x \\in ${name} : ~${before}(x, x)`, `ASSUME ${transitive} ==`, ` \\A x, y, z \\in ${name} : ${before}(x, y) /\\ ${before}(y, z) => ${before}(x, z)`, `ASSUME ${total} ==`, ` \\A x, y \\in ${name} : x # y => ${before}(x, y) \\/ ${before}(y, x)`, `${name}Sets == {S \\in SUBSET ${name} : IsFiniteSet(S)}`, `${name}First(S) == CHOOSE x \\in S : \\A y \\in S : x # y => ${before}(x, y)`);
|
|
201
|
+
}
|
|
202
|
+
const events = Object.entries(declaration.events);
|
|
203
|
+
lines.push("", "\\* Every event with its params: a record whose field `event` names it.", "Events ==", events.map(([event, { params }]) => ` ${recordSet(params, [`event : {${tlaString(event)}}`])}`).join("\n \\cup\n"), "", "\\* Every effect the event `e` is declared to give.", "Effects(e) ==", ` CASE ${events.map(([event, { effect }]) => `e.event = ${tlaString(event)} -> ${recordSet(effect)}`).join("\n [] ")}`, "====", "");
|
|
204
|
+
return lines.join("\n");
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* The name of the assumption of the declaration in TLA+ `text` that
|
|
208
|
+
* spans line `line` (from 1), or null. TLC names a false assumption
|
|
209
|
+
* only by the place of its body, which may be the line after
|
|
210
|
+
* `ASSUME <name> ==`.
|
|
211
|
+
*/
|
|
212
|
+
export function assumptionAt(text, line) {
|
|
213
|
+
const lines = text.split("\n");
|
|
214
|
+
for (let at = line - 1; at >= 0 && at < lines.length; at -= 1) {
|
|
215
|
+
const named = /^ASSUME (\w+) ==/.exec(lines[at]);
|
|
216
|
+
if (named !== null)
|
|
217
|
+
return named[1];
|
|
218
|
+
// A body goes on only on lines that start with two spaces.
|
|
219
|
+
if (!lines[at].startsWith(" "))
|
|
220
|
+
return null;
|
|
221
|
+
}
|
|
222
|
+
return null;
|
|
223
|
+
}
|
|
224
|
+
//# sourceMappingURL=declaration.js.map
|