@sublang/spex 6.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.
Files changed (108) hide show
  1. package/README.md +419 -12
  2. package/dist/cli.js +12 -1
  3. package/dist/commands/add.d.ts +1 -1
  4. package/dist/commands/add.js +2 -2
  5. package/dist/commands/environment.d.ts +3 -3
  6. package/dist/commands/environment.js +5 -7
  7. package/dist/commands/export.js +2 -2
  8. package/dist/commands/home.d.ts +2 -2
  9. package/dist/commands/home.js +3 -3
  10. package/dist/commands/list.js +1 -1
  11. package/dist/commands/login.js +12 -7
  12. package/dist/commands/logout.js +2 -3
  13. package/dist/commands/publish.js +1 -1
  14. package/dist/commands/remove.js +1 -1
  15. package/dist/commands/run.d.ts +1 -1
  16. package/dist/commands/run.js +16 -0
  17. package/dist/commands/settle.d.ts +1 -1
  18. package/dist/commands/settle.js +1 -1
  19. package/dist/guards/approval.d.ts +56 -0
  20. package/dist/guards/approval.js +158 -0
  21. package/dist/guards/checks.d.ts +207 -0
  22. package/dist/guards/checks.js +681 -0
  23. package/dist/guards/commands.d.ts +15 -0
  24. package/dist/guards/commands.js +313 -0
  25. package/dist/guards/copy.d.ts +67 -0
  26. package/dist/guards/copy.js +306 -0
  27. package/dist/guards/declaration.d.ts +45 -0
  28. package/dist/guards/declaration.js +224 -0
  29. package/dist/guards/files.d.ts +137 -0
  30. package/dist/guards/files.js +544 -0
  31. package/dist/guards/harness.d.ts +58 -0
  32. package/dist/guards/harness.js +420 -0
  33. package/dist/guards/items.d.ts +61 -0
  34. package/dist/guards/items.js +161 -0
  35. package/dist/guards/patch.d.ts +55 -0
  36. package/dist/guards/patch.js +258 -0
  37. package/dist/guards/processes.d.ts +49 -0
  38. package/dist/guards/processes.js +233 -0
  39. package/dist/guards/proofs.d.ts +88 -0
  40. package/dist/guards/proofs.js +503 -0
  41. package/dist/guards/records.d.ts +176 -0
  42. package/dist/guards/records.js +513 -0
  43. package/dist/guards/report.d.ts +26 -0
  44. package/dist/guards/report.js +492 -0
  45. package/dist/guards/runner.d.ts +42 -0
  46. package/dist/guards/runner.js +167 -0
  47. package/dist/guards/tla.d.ts +51 -0
  48. package/dist/guards/tla.js +352 -0
  49. package/dist/guards/tlapm.d.ts +81 -0
  50. package/dist/guards/tlapm.js +172 -0
  51. package/dist/guards/tlc.d.ts +191 -0
  52. package/dist/guards/tlc.js +457 -0
  53. package/dist/guards/translation.d.ts +66 -0
  54. package/dist/guards/translation.js +202 -0
  55. package/dist/guards/types.d.ts +185 -0
  56. package/dist/guards/types.js +437 -0
  57. package/dist/guards/verify.d.ts +290 -0
  58. package/dist/guards/verify.js +1191 -0
  59. package/dist/guards/xml.d.ts +18 -0
  60. package/dist/guards/xml.js +105 -0
  61. package/dist/lib/environment/archive.d.ts +2 -2
  62. package/dist/lib/environment/archive.js +1 -1
  63. package/dist/lib/environment/builtins.d.ts +10 -12
  64. package/dist/lib/environment/builtins.js +17 -19
  65. package/dist/lib/environment/exports.d.ts +14 -13
  66. package/dist/lib/environment/exports.js +31 -16
  67. package/dist/lib/environment/format.d.ts +2 -2
  68. package/dist/lib/environment/format.js +3 -3
  69. package/dist/lib/environment/git-source.js +6 -3
  70. package/dist/lib/environment/install.d.ts +14 -16
  71. package/dist/lib/environment/install.js +66 -61
  72. package/dist/lib/environment/lock.d.ts +4 -3
  73. package/dist/lib/environment/lock.js +6 -5
  74. package/dist/lib/environment/marker.js +1 -2
  75. package/dist/lib/environment/own-paths.d.ts +7 -10
  76. package/dist/lib/environment/own-paths.js +10 -13
  77. package/dist/lib/environment/placed-record.d.ts +3 -2
  78. package/dist/lib/environment/placed-record.js +5 -4
  79. package/dist/lib/environment/registry.d.ts +1 -1
  80. package/dist/lib/environment/registry.js +1 -1
  81. package/dist/lib/environment/requests.d.ts +2 -2
  82. package/dist/lib/environment/requests.js +2 -2
  83. package/dist/lib/environment/resolve.d.ts +4 -4
  84. package/dist/lib/environment/resolve.js +3 -4
  85. package/dist/lib/environment/store.d.ts +34 -8
  86. package/dist/lib/environment/store.js +138 -43
  87. package/dist/lib/files.d.ts +72 -12
  88. package/dist/lib/files.js +200 -65
  89. package/dist/lib/git-host.d.ts +52 -258
  90. package/dist/lib/git-host.js +52 -265
  91. package/dist/lib/home-file.d.ts +13 -10
  92. package/dist/lib/home-file.js +28 -18
  93. package/dist/lib/instance.d.ts +1 -1
  94. package/dist/lib/instance.js +1 -1
  95. package/dist/lint.d.ts +22 -0
  96. package/dist/lint.js +36 -0
  97. package/dist/testing/standin-registry.js +1 -1
  98. package/package.json +7 -3
  99. package/scaffold/.file-history.json +14 -7
  100. package/scaffold/i18n/zh/source-pins.json +41 -0
  101. package/scaffold/i18n/zh/specs/decisions/000-spec-structure-format.md +0 -1
  102. package/scaffold/i18n/zh/specs/intents/000-spdx-headers.md +0 -1
  103. package/scaffold/i18n/zh/specs/map.md +0 -1
  104. package/scaffold/i18n/zh/specs/meta.md +18 -35
  105. package/scaffold/i18n/zh/specs/packages/git.md +0 -1
  106. package/scaffold/i18n/zh/specs/packages/licensing.md +0 -1
  107. package/scaffold/specs/meta.md +18 -3
  108. package/scripts/test-exit-guard.mjs +28 -0
@@ -0,0 +1,15 @@
1
+ /**
2
+ * `spex verify [--keep <folder>] [<guard>…]` (guards-17). Returns the
3
+ * exit status: 0
4
+ * when every guard it checked passes or there is none, else 1. An
5
+ * interrupt stops the copy being checked, removes every copy and
6
+ * exits 130 at once; a quit exits 131, a request to end 143 and a
7
+ * hangup 129 the same way (guards-27).
8
+ */
9
+ export declare function verify(words: string[]): Promise<number>;
10
+ /**
11
+ * `spex approve <guard>` (guards-16). Returns the exit status: 0 with
12
+ * the record written, else 1 with nothing written. It runs no check
13
+ * and asks no question.
14
+ */
15
+ export declare function approve(args: string[]): Promise<number>;
@@ -0,0 +1,313 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ // SPDX-FileCopyrightText: 2026 SubLang International <https://sublang.ai>
3
+ /*
4
+ * Guards package (guards-16, guards-17, guards-27, guards-41),
5
+ * DR-252: the two commands. `spex verify [--keep <folder>] [<guard>…]`
6
+ * checks every guard, or the ones named, and exits 0 only when each
7
+ * passes. `spex approve <guard>` prints what a person is approving
8
+ * and writes the approval record. Both keep their own exits, as
9
+ * `spex lint` does.
10
+ */
11
+ import { lstatSync, readlinkSync, realpathSync, statSync } from "node:fs";
12
+ import * as nodeModule from "node:module";
13
+ import { tmpdir } from "node:os";
14
+ import { dirname, join, resolve } from "node:path";
15
+ import { resolveBase } from "../resolve-base.js";
16
+ import { compareApproval, readApproval, writeApproval } from "./approval.js";
17
+ import { keptBySpex } from "./checks.js";
18
+ import { inside, removeCopies } from "./copy.js";
19
+ import { listGuards, loadGuard } from "./files.js";
20
+ import { readItems } from "./items.js";
21
+ import { stopProcesses } from "./processes.js";
22
+ import { refuse, refuseUse, sayApproved, sayCount, sayGuard, sayHeading, sayNoGuards } from "./report.js";
23
+ import { findTools } from "./tlc.js";
24
+ import { formFaults } from "./translation.js";
25
+ import { GuardError } from "./types.js";
26
+ import { UNEXPECTED, checkGuard, passes } from "./verify.js";
27
+ /** The first option among a command's words, if any. */
28
+ function optionIn(args) {
29
+ return args.find((arg) => arg.startsWith("-") && arg !== "-");
30
+ }
31
+ function causeOf(error) {
32
+ return error instanceof Error ? error.message : String(error);
33
+ }
34
+ /** Whether nothing stands at `path`. A path below a file has nothing
35
+ * there either: one system says so and another calls it no folder,
36
+ * and both are taken alike, so that a run refuses the same on each. */
37
+ function absent(path) {
38
+ try {
39
+ return lstatSync(path, { throwIfNoEntry: false }) === undefined;
40
+ }
41
+ catch (error) {
42
+ if (error?.code === "ENOTDIR")
43
+ return true;
44
+ throw error;
45
+ }
46
+ }
47
+ /** A path with every link in the part of it that exists followed, a
48
+ * link that leads to a place that is not there yet too: where it
49
+ * would be written is where it leads. */
50
+ function realPath(path, hops = 0) {
51
+ let known = resolve(path);
52
+ const rest = [];
53
+ while (absent(known) && dirname(known) !== known) {
54
+ rest.unshift(known.slice(dirname(known).length).replace(/^[\\/]/, ""));
55
+ known = dirname(known);
56
+ }
57
+ try {
58
+ return join(realpathSync.native(known), ...rest);
59
+ }
60
+ catch (error) {
61
+ // No more links than a system follows before it calls them a loop.
62
+ if (hops >= 40 || absent(known) || !lstatSync(known).isSymbolicLink())
63
+ throw error;
64
+ return join(realPath(resolve(dirname(known), readlinkSync(known)), hops + 1), ...rest);
65
+ }
66
+ }
67
+ /**
68
+ * Why the OS temp folder cannot hold a run's copies and generated
69
+ * files, or null: it lies in the project, where a run writes nothing
70
+ * and where a copied folder could hold the copy being made of it
71
+ * (guards-28).
72
+ */
73
+ function tempFault(root) {
74
+ let temp;
75
+ try {
76
+ temp = realpathSync.native(tmpdir());
77
+ if (!inside(realpathSync.native(root), temp))
78
+ return null;
79
+ }
80
+ catch {
81
+ // A temp folder that is not there fails by itself where it is used.
82
+ return null;
83
+ }
84
+ return `the OS temp folder ${temp} lies in the project \u2014 point TMPDIR, or TEMP and TMP on Windows, at a folder outside it`;
85
+ }
86
+ /**
87
+ * Why `--keep <folder>` cannot be used, or null: the folder, links
88
+ * followed, lies in the project. What a run generates is kept outside
89
+ * it alone, ignored by Git or not, so that no guard, of this run or of
90
+ * a later one, meets it among the files it copies or approves
91
+ * (guards-28).
92
+ */
93
+ function keepFault(root, keep) {
94
+ if (inside(realpathSync.native(root), keep))
95
+ return "the folder lies in the project \u2014 name a folder outside it";
96
+ // Nothing is kept in a file: the folder, or the part of its path
97
+ // that is there, is a folder.
98
+ let standing = keep;
99
+ while (absent(standing) && dirname(standing) !== standing)
100
+ standing = dirname(standing);
101
+ return statSync(standing).isDirectory() ? null : `${standing} is no folder`;
102
+ }
103
+ /**
104
+ * `spex verify [--keep <folder>] [<guard>…]` (guards-17). Returns the
105
+ * exit status: 0
106
+ * when every guard it checked passes or there is none, else 1. An
107
+ * interrupt stops the copy being checked, removes every copy and
108
+ * exits 130 at once; a quit exits 131, a request to end 143 and a
109
+ * hangup 129 the same way (guards-27).
110
+ */
111
+ export async function verify(words) {
112
+ const args = [];
113
+ let kept;
114
+ for (let at = 0; at < words.length; at += 1) {
115
+ const word = words[at];
116
+ if (word === "--keep" || word.startsWith("--keep=")) {
117
+ const value = word === "--keep" ? words[++at] : word.slice("--keep=".length);
118
+ if (value === undefined || value === "") {
119
+ refuseUse("verify", "--keep needs a folder");
120
+ return 1;
121
+ }
122
+ kept = value;
123
+ }
124
+ else if (word.startsWith("-") && word !== "-") {
125
+ refuseUse("verify", `unknown option ${word}`);
126
+ return 1;
127
+ }
128
+ else
129
+ args.push(word);
130
+ }
131
+ // The process of a copy tells of the modules it loads through
132
+ // module.registerHooks, which Node has from 22.15.
133
+ if (typeof nodeModule.registerHooks !== "function") {
134
+ refuse("verify", `needs Node 22.15 or newer to see what a copy's process loads; this is Node ${process.versions.node}`);
135
+ return 1;
136
+ }
137
+ let root;
138
+ let known;
139
+ try {
140
+ root = resolveBase(undefined);
141
+ known = listGuards(root);
142
+ }
143
+ catch (error) {
144
+ refuse("verify", causeOf(error));
145
+ return 1;
146
+ }
147
+ const unknown = args.find((name) => !known.includes(name));
148
+ if (unknown !== undefined) {
149
+ refuse("verify", `${unknown} is no guard under guards/`);
150
+ return 1;
151
+ }
152
+ const names = args.length > 0 ? args : known;
153
+ let keep = null;
154
+ if (kept !== undefined) {
155
+ let fault;
156
+ try {
157
+ keep = realPath(kept);
158
+ fault = keepFault(root, keep);
159
+ }
160
+ catch (error) {
161
+ refuse("verify", `--keep ${kept}: ${causeOf(error)}`);
162
+ return 1;
163
+ }
164
+ if (fault !== null) {
165
+ refuse("verify", `--keep ${kept}: ${fault}`);
166
+ return 1;
167
+ }
168
+ // What a run writes is `<folder>/<guard>`: a guard named as a
169
+ // folder that holds the project would be given the project itself.
170
+ // Links are followed, and the name is as the file system keeps it:
171
+ // one that ignores case holds the project under another spelling.
172
+ const base = realpathSync.native(root);
173
+ let holds;
174
+ let taken;
175
+ try {
176
+ holds = names.find((name) => inside(realPath(join(keep, name)), base));
177
+ taken = names.find((name) => !absent(join(keep, name)) && !keptBySpex(join(keep, name)));
178
+ }
179
+ catch (error) {
180
+ // The folder lies where this user may not look.
181
+ refuse("verify", `--keep ${kept}: ${causeOf(error)}`);
182
+ return 1;
183
+ }
184
+ if (holds !== undefined) {
185
+ refuse("verify", `--keep ${kept}: ${join(resolve(kept), holds)} holds the project \u2014 name another folder`);
186
+ return 1;
187
+ }
188
+ if (taken !== undefined) {
189
+ refuse("verify", `--keep ${kept}: ${join(resolve(kept), taken)} was not kept by spex verify \u2014 remove it, or name another folder`);
190
+ return 1;
191
+ }
192
+ }
193
+ if (names.length === 0) {
194
+ sayNoGuards();
195
+ return 0;
196
+ }
197
+ const temp = tempFault(root);
198
+ if (temp !== null) {
199
+ refuse("verify", temp);
200
+ return 1;
201
+ }
202
+ // A signal stops the copy's processes with all they started, removes
203
+ // every copy and exits at once with 128 plus the signal's number.
204
+ // Each process a run starts leads a session of its own, so no key
205
+ // typed at the terminal reaches it: SIGQUIT is handled as well.
206
+ const signals = { SIGINT: 130, SIGQUIT: 131, SIGTERM: 143, SIGHUP: 129 };
207
+ const stops = Object.entries(signals).map(([signal, status]) => {
208
+ const stop = () => {
209
+ stopProcesses();
210
+ removeCopies();
211
+ process.exit(status);
212
+ };
213
+ process.on(signal, stop);
214
+ return () => void process.off(signal, stop);
215
+ });
216
+ try {
217
+ // The tree's items are read once, and only when a guard is loaded.
218
+ let items;
219
+ const index = () => (items ??= readItems(root));
220
+ // Java and the jar are found once, and only when a guard needs TLC.
221
+ let found;
222
+ const tools = () => (found ??= findTools());
223
+ sayHeading();
224
+ let passing = 0;
225
+ for (const name of names) {
226
+ // One guard's check is no other's: an error Spex did not expect
227
+ // while it checked this one leaves this one not checked, and the
228
+ // guards after it are checked all the same (guards-17).
229
+ let report;
230
+ try {
231
+ report = await checkGuard(root, name, { index, tools, keep });
232
+ }
233
+ catch (error) {
234
+ report = { name, cannot: [{ cause: `${UNEXPECTED}${causeOf(error)}`, note: null, steps: [] }] };
235
+ }
236
+ if (passes(report))
237
+ passing += 1;
238
+ sayGuard(report);
239
+ }
240
+ sayCount(passing, names.length);
241
+ return passing === names.length ? 0 : 1;
242
+ }
243
+ catch (error) {
244
+ // Nothing about a guard: the run itself could not go on.
245
+ refuse("verify", causeOf(error));
246
+ return 1;
247
+ }
248
+ finally {
249
+ for (const off of stops)
250
+ off();
251
+ removeCopies();
252
+ }
253
+ }
254
+ /**
255
+ * `spex approve <guard>` (guards-16). Returns the exit status: 0 with
256
+ * the record written, else 1 with nothing written. It runs no check
257
+ * and asks no question.
258
+ */
259
+ export async function approve(args) {
260
+ const option = optionIn(args);
261
+ if (option !== undefined) {
262
+ refuseUse("approve", `unknown option ${option}`);
263
+ return 1;
264
+ }
265
+ if (args.length === 0) {
266
+ refuseUse("approve", "missing <guard>");
267
+ return 1;
268
+ }
269
+ if (args.length > 1) {
270
+ refuseUse("approve", `unexpected argument ${args[1]}`);
271
+ return 1;
272
+ }
273
+ const name = args[0];
274
+ try {
275
+ const root = resolveBase(undefined);
276
+ if (!listGuards(root).includes(name)) {
277
+ refuse("approve", `${name} is no guard under guards/`);
278
+ return 1;
279
+ }
280
+ let guard;
281
+ try {
282
+ guard = loadGuard(root, name);
283
+ }
284
+ catch (error) {
285
+ refuse("approve", `${name} cannot be checked — ${causeOf(error)}`);
286
+ return 1;
287
+ }
288
+ // A translation's form is checked by its text; TLC is not run.
289
+ const faults = guard.translations.flatMap((translation) => formFaults(translation).map((fault) => `${translation.file}${fault.line === null ? "" : `:${fault.line}:${fault.col}`}: ${fault.message}`));
290
+ if (faults.length > 0) {
291
+ refuse("approve", `${name} cannot be checked — ${faults.join("; ")}`);
292
+ return 1;
293
+ }
294
+ let since;
295
+ try {
296
+ const record = readApproval(root, name);
297
+ since = record === null ? null : compareApproval(guard.inputs, record);
298
+ }
299
+ catch (error) {
300
+ if (!(error instanceof GuardError))
301
+ throw error;
302
+ since = error.message;
303
+ }
304
+ const record = writeApproval(root, name, guard.inputs);
305
+ sayApproved(guard, since, record.path, record.written);
306
+ return 0;
307
+ }
308
+ catch (error) {
309
+ refuse("approve", causeOf(error));
310
+ return 1;
311
+ }
312
+ }
313
+ //# sourceMappingURL=commands.js.map
@@ -0,0 +1,67 @@
1
+ /** One copy and the folders made beside it. */
2
+ export type Copy = {
3
+ /** The fresh folder that holds everything below. */
4
+ home: string;
5
+ /** The copied files: the working folder of every check. */
6
+ folder: string;
7
+ /** An empty folder given to the copy's processes as their temp
8
+ * folder, so what they leave there goes with the copy. */
9
+ scratch: string;
10
+ /** An empty folder beside the copy, outside it, that SPEX_RECORDS
11
+ * names to the project's tests: a probe writes its lines there. */
12
+ records: string;
13
+ /** One SHA-256 over the sorted relative paths and the bytes of
14
+ * everything copied, with whether its owner may execute each file,
15
+ * before any patch. */
16
+ digest: string;
17
+ /** One SHA-256 over that digest and the mode of every file and
18
+ * folder copied: what a kept fault's copy is held to, so that a
19
+ * mode changed since the copy of the code was made is a change, as
20
+ * changed bytes are. It is told nowhere: all of a mode but the
21
+ * owner's right to execute differs from one checkout to the next. */
22
+ state: string;
23
+ /** Every file copied, by its path from the root, in order. */
24
+ files: readonly string[];
25
+ /** Where a link led and each link met on the way to a copied entry,
26
+ * by its path from the root, in order: these decide what was copied
27
+ * and may be no part of the declared paths, so Git is asked about
28
+ * them too (guards-26). */
29
+ linked: readonly string[];
30
+ };
31
+ /** Whether an entry of this name is left out of every copy. A guard's
32
+ * inputs leave out the same entries of a binding's folder, so that a
33
+ * copy holds exactly the files the inputs name (guards-12, guards-14). */
34
+ export declare function leftOut(name: string): boolean;
35
+ /** Whether the real path `real` is `base` or under it. Both are given
36
+ * as `realpathSync.native` gives them, which on Windows is the long
37
+ * name on either side. */
38
+ export declare function inside(base: string, real: string): boolean;
39
+ /** Remove every copy still standing. Safe to call at any time. */
40
+ export declare function removeCopies(): void;
41
+ /** Remove one copy. */
42
+ export declare function removeCopy(copy: Copy): void;
43
+ /**
44
+ * A fresh folder under the OS temp folder for what a run writes
45
+ * beside its copies: the TLA+ files it generates and what TLC leaves.
46
+ * It is removed with the copies, however the process exits.
47
+ */
48
+ export declare function makeHome(): string;
49
+ /** Remove a folder `makeHome` made. */
50
+ export declare function removeFolder(home: string): void;
51
+ /**
52
+ * `text` with every path into a copy told relative to the copy's
53
+ * folder, as a path or as a `file:` URL: `src/lock.mjs` for a copied
54
+ * file, `.` for the folder itself and `../tmp/…` for what lies in the
55
+ * copy's temp folder. `home` is the copy's fresh folder, whose name is
56
+ * new in every run, so a cause told this way is the same in every run
57
+ * (guards-36).
58
+ */
59
+ export declare function relativeToCopy(text: string, home: string): string;
60
+ /**
61
+ * Make a copy of `paths` (files and folders relative to `root`) in a
62
+ * fresh folder under the OS temp folder (guards-12, steps 1 to 3).
63
+ * Every folder of the copy whose project counterpart holds a
64
+ * `node_modules` gets a link to it, as does the root and each folder
65
+ * above a copied path. An error leaves no folder behind.
66
+ */
67
+ export declare function makeCopy(root: string, paths: readonly string[]): Copy;