@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.
Files changed (114) hide show
  1. package/README.md +453 -41
  2. package/dist/cli.js +12 -1
  3. package/dist/commands/add.d.ts +1 -1
  4. package/dist/commands/add.js +3 -3
  5. package/dist/commands/auth-target.d.ts +1 -0
  6. package/dist/commands/auth-target.js +4 -2
  7. package/dist/commands/environment.d.ts +15 -21
  8. package/dist/commands/environment.js +43 -57
  9. package/dist/commands/export.js +2 -2
  10. package/dist/commands/home.d.ts +2 -2
  11. package/dist/commands/home.js +3 -3
  12. package/dist/commands/list.js +1 -1
  13. package/dist/commands/login.js +12 -7
  14. package/dist/commands/logout.js +2 -3
  15. package/dist/commands/publish.js +16 -16
  16. package/dist/commands/remove.js +1 -1
  17. package/dist/commands/run.d.ts +1 -1
  18. package/dist/commands/run.js +22 -3
  19. package/dist/commands/search.js +1 -1
  20. package/dist/commands/settle.d.ts +1 -1
  21. package/dist/commands/settle.js +3 -3
  22. package/dist/commands/whoami.js +2 -6
  23. package/dist/guards/approval.d.ts +56 -0
  24. package/dist/guards/approval.js +158 -0
  25. package/dist/guards/checks.d.ts +207 -0
  26. package/dist/guards/checks.js +681 -0
  27. package/dist/guards/commands.d.ts +15 -0
  28. package/dist/guards/commands.js +313 -0
  29. package/dist/guards/copy.d.ts +67 -0
  30. package/dist/guards/copy.js +306 -0
  31. package/dist/guards/declaration.d.ts +45 -0
  32. package/dist/guards/declaration.js +224 -0
  33. package/dist/guards/files.d.ts +137 -0
  34. package/dist/guards/files.js +544 -0
  35. package/dist/guards/harness.d.ts +58 -0
  36. package/dist/guards/harness.js +420 -0
  37. package/dist/guards/items.d.ts +61 -0
  38. package/dist/guards/items.js +161 -0
  39. package/dist/guards/patch.d.ts +55 -0
  40. package/dist/guards/patch.js +258 -0
  41. package/dist/guards/processes.d.ts +49 -0
  42. package/dist/guards/processes.js +233 -0
  43. package/dist/guards/proofs.d.ts +88 -0
  44. package/dist/guards/proofs.js +503 -0
  45. package/dist/guards/records.d.ts +176 -0
  46. package/dist/guards/records.js +513 -0
  47. package/dist/guards/report.d.ts +26 -0
  48. package/dist/guards/report.js +492 -0
  49. package/dist/guards/runner.d.ts +42 -0
  50. package/dist/guards/runner.js +167 -0
  51. package/dist/guards/tla.d.ts +51 -0
  52. package/dist/guards/tla.js +352 -0
  53. package/dist/guards/tlapm.d.ts +81 -0
  54. package/dist/guards/tlapm.js +172 -0
  55. package/dist/guards/tlc.d.ts +191 -0
  56. package/dist/guards/tlc.js +457 -0
  57. package/dist/guards/translation.d.ts +66 -0
  58. package/dist/guards/translation.js +202 -0
  59. package/dist/guards/types.d.ts +185 -0
  60. package/dist/guards/types.js +437 -0
  61. package/dist/guards/verify.d.ts +290 -0
  62. package/dist/guards/verify.js +1191 -0
  63. package/dist/guards/xml.d.ts +18 -0
  64. package/dist/guards/xml.js +105 -0
  65. package/dist/lib/environment/archive.d.ts +16 -3
  66. package/dist/lib/environment/archive.js +39 -8
  67. package/dist/lib/environment/builtins.d.ts +10 -12
  68. package/dist/lib/environment/builtins.js +18 -20
  69. package/dist/lib/environment/exports.d.ts +14 -13
  70. package/dist/lib/environment/exports.js +31 -16
  71. package/dist/lib/environment/format.d.ts +2 -2
  72. package/dist/lib/environment/format.js +3 -3
  73. package/dist/lib/environment/git-source.js +6 -3
  74. package/dist/lib/environment/install.d.ts +14 -16
  75. package/dist/lib/environment/install.js +66 -61
  76. package/dist/lib/environment/lock.d.ts +4 -3
  77. package/dist/lib/environment/lock.js +6 -5
  78. package/dist/lib/environment/marker.js +1 -2
  79. package/dist/lib/environment/own-paths.d.ts +7 -10
  80. package/dist/lib/environment/own-paths.js +10 -13
  81. package/dist/lib/environment/placed-record.d.ts +3 -2
  82. package/dist/lib/environment/placed-record.js +5 -4
  83. package/dist/lib/environment/registries.d.ts +7 -9
  84. package/dist/lib/environment/registries.js +9 -36
  85. package/dist/lib/environment/registry.d.ts +6 -7
  86. package/dist/lib/environment/registry.js +12 -12
  87. package/dist/lib/environment/requests.d.ts +6 -5
  88. package/dist/lib/environment/requests.js +6 -8
  89. package/dist/lib/environment/resolve.d.ts +4 -4
  90. package/dist/lib/environment/resolve.js +3 -4
  91. package/dist/lib/environment/store.d.ts +34 -8
  92. package/dist/lib/environment/store.js +138 -43
  93. package/dist/lib/files.d.ts +72 -12
  94. package/dist/lib/files.js +200 -65
  95. package/dist/lib/git-host.d.ts +52 -258
  96. package/dist/lib/git-host.js +52 -265
  97. package/dist/lib/home-file.d.ts +13 -10
  98. package/dist/lib/home-file.js +28 -18
  99. package/dist/lib/instance.d.ts +1 -1
  100. package/dist/lib/instance.js +1 -1
  101. package/dist/lint.d.ts +22 -0
  102. package/dist/lint.js +36 -0
  103. package/dist/testing/standin-registry.js +1 -1
  104. package/package.json +7 -3
  105. package/scaffold/.file-history.json +14 -7
  106. package/scaffold/i18n/zh/source-pins.json +41 -0
  107. package/scaffold/i18n/zh/specs/decisions/000-spec-structure-format.md +0 -1
  108. package/scaffold/i18n/zh/specs/intents/000-spdx-headers.md +0 -1
  109. package/scaffold/i18n/zh/specs/map.md +0 -1
  110. package/scaffold/i18n/zh/specs/meta.md +18 -35
  111. package/scaffold/i18n/zh/specs/packages/git.md +0 -1
  112. package/scaffold/i18n/zh/specs/packages/licensing.md +0 -1
  113. package/scaffold/specs/meta.md +18 -3
  114. package/scripts/test-exit-guard.mjs +28 -0
@@ -0,0 +1,233 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ // SPDX-FileCopyrightText: 2026 SubLang International <https://sublang.ai>
3
+ /*
4
+ * Guards package (guards-12), DR-252: the processes a run starts,
5
+ * each of a binding's commands and the copy's own process alike. A
6
+ * process is stopped at the time limit, and whatever it started is
7
+ * stopped with it: at the limit, when it ends, and when the run is
8
+ * interrupted. The run never waits for what such a process prints:
9
+ * standard error goes to a file, read once the process has ended, and
10
+ * standard output is dropped or goes to a file the caller names.
11
+ *
12
+ * When a process ends by itself, what it left running is noted
13
+ * before it is stopped, so that the caller can say so.
14
+ *
15
+ * On POSIX a process leads a group of its own and the whole group is
16
+ * killed. A process that has left that group, by starting a session
17
+ * of its own, is found only on Linux, and only where an entry of its
18
+ * environment holds the run's mark: `/proc` shows every process's
19
+ * group and environment. On macOS it is neither found nor stopped.
20
+ * On Windows `taskkill /T /F` stops a process with all it started; it
21
+ * finds them only while the process itself still runs, so what a
22
+ * process left behind when it ended by itself is neither found nor
23
+ * stopped.
24
+ */
25
+ import { spawn, spawnSync } from "node:child_process";
26
+ import { closeSync, fstatSync, openSync, readFileSync, readSync, readdirSync } from "node:fs";
27
+ /** The most of a process's standard error that is read back. */
28
+ const MOST_ERRORS = 8000;
29
+ /** Each process running, with its mark. */
30
+ const running = new Map();
31
+ let hooked = false;
32
+ /**
33
+ * On Linux, the processes other than this one that are not ended
34
+ * and are of the group `group` or hold `mark` in an entry of their
35
+ * environment; null where `/proc` cannot be read. A process that has
36
+ * ended and is not yet collected is not counted.
37
+ */
38
+ function linuxMembers(group, mark) {
39
+ if (process.platform !== "linux")
40
+ return null;
41
+ let names;
42
+ try {
43
+ names = readdirSync("/proc");
44
+ }
45
+ catch {
46
+ return null;
47
+ }
48
+ const found = [];
49
+ for (const name of names) {
50
+ if (!/^\d+$/.test(name))
51
+ continue;
52
+ const pid = Number(name);
53
+ if (pid === process.pid)
54
+ continue;
55
+ let fields;
56
+ try {
57
+ // After the name, which is in brackets: the state, the parent,
58
+ // the group.
59
+ const stat = readFileSync(`/proc/${name}/stat`, "utf8");
60
+ fields = stat.slice(stat.lastIndexOf(")") + 2).split(" ");
61
+ }
62
+ catch {
63
+ continue;
64
+ }
65
+ if (fields[0] === "Z" || fields[0] === "X" || fields[0] === "x")
66
+ continue;
67
+ if (Number(fields[2]) === group) {
68
+ found.push(pid);
69
+ continue;
70
+ }
71
+ if (mark === undefined)
72
+ continue;
73
+ try {
74
+ if (readFileSync(`/proc/${name}/environ`, "latin1").split("\0").some((entry) => entry.includes(mark))) {
75
+ found.push(pid);
76
+ }
77
+ }
78
+ catch {
79
+ // Another user's process, or one that has just ended.
80
+ }
81
+ }
82
+ return found;
83
+ }
84
+ /** Whether a process of the group `group`, or on Linux one that holds
85
+ * `mark`, still runs. Never on Windows, which cannot see one. */
86
+ function anyLeft(group, mark) {
87
+ if (process.platform === "win32")
88
+ return false;
89
+ const members = linuxMembers(group, mark);
90
+ if (members !== null)
91
+ return members.length > 0;
92
+ try {
93
+ process.kill(-group, 0);
94
+ return true;
95
+ }
96
+ catch (error) {
97
+ // EPERM: a process of the group runs as another user.
98
+ return error.code === "EPERM";
99
+ }
100
+ }
101
+ /** Stop one process and every process it started. `ended` says the
102
+ * process itself is gone already. */
103
+ function stop(child, ended, mark) {
104
+ if (child.pid === undefined)
105
+ return;
106
+ if (process.platform === "win32") {
107
+ if (!ended) {
108
+ spawnSync("taskkill", ["/pid", String(child.pid), "/T", "/F"], { stdio: "ignore", windowsHide: true });
109
+ }
110
+ return;
111
+ }
112
+ const kill = (target) => {
113
+ try {
114
+ process.kill(target, "SIGKILL");
115
+ }
116
+ catch {
117
+ // Nothing of it is left.
118
+ }
119
+ };
120
+ kill(-child.pid);
121
+ if (mark === undefined)
122
+ return;
123
+ // A process found by its mark may start another as it is stopped:
124
+ // look again until none is found, a few times at most.
125
+ for (let round = 0; round < 5; round += 1) {
126
+ const members = linuxMembers(child.pid, mark);
127
+ if (members === null || members.length === 0)
128
+ return;
129
+ for (const pid of members)
130
+ kill(pid);
131
+ }
132
+ }
133
+ /** Stop every process still running, with all they started. Safe to
134
+ * call at any time; it waits for nothing. */
135
+ export function stopProcesses() {
136
+ for (const [child, mark] of running)
137
+ stop(child, false, mark);
138
+ }
139
+ function endOf(file) {
140
+ try {
141
+ const fd = openSync(file, "r");
142
+ try {
143
+ const size = fstatSync(fd).size;
144
+ const length = Math.min(size, MOST_ERRORS);
145
+ const bytes = Buffer.alloc(length);
146
+ readSync(fd, bytes, 0, length, size - length);
147
+ return bytes.toString("utf8");
148
+ }
149
+ finally {
150
+ closeSync(fd);
151
+ }
152
+ }
153
+ catch {
154
+ return "";
155
+ }
156
+ }
157
+ /**
158
+ * Run one program to its end or to the time limit, whichever comes
159
+ * first. What it prints on standard output is dropped, or kept in the
160
+ * file `output` names. When it ends, however it ends, every process
161
+ * it started that can be found is stopped too; where it ended by
162
+ * itself, `left` says whether one was still running.
163
+ */
164
+ export function runProcess(program, args, options) {
165
+ if (!hooked) {
166
+ hooked = true;
167
+ process.on("exit", stopProcesses);
168
+ }
169
+ return new Promise((resolve) => {
170
+ const errors = openSync(options.errors, "w");
171
+ const output = options.output === undefined ? null : options.output === options.errors ? errors : openSync(options.output, "w");
172
+ let child;
173
+ try {
174
+ child = spawn(program, [...args], {
175
+ cwd: options.cwd,
176
+ env: options.env,
177
+ // No pipe: a process left behind cannot hold one open.
178
+ stdio: ["ignore", output ?? "ignore", errors],
179
+ // A group of its own on POSIX, so the whole group can be stopped.
180
+ detached: process.platform !== "win32",
181
+ windowsHide: true,
182
+ });
183
+ }
184
+ catch (error) {
185
+ resolve({ code: null, timedOut: false, errors: "", failed: error instanceof Error ? error.message : String(error), left: false });
186
+ return;
187
+ }
188
+ finally {
189
+ closeSync(errors);
190
+ if (output !== null && output !== errors)
191
+ closeSync(output);
192
+ }
193
+ const { mark } = options;
194
+ running.set(child, mark);
195
+ let timedOut = false;
196
+ let settled = false;
197
+ const timer = setTimeout(() => {
198
+ timedOut = true;
199
+ stop(child, false, mark);
200
+ child.kill("SIGKILL");
201
+ }, options.seconds * 1000);
202
+ const settle = (code, failed) => {
203
+ if (settled)
204
+ return;
205
+ settled = true;
206
+ clearTimeout(timer);
207
+ running.delete(child);
208
+ // What it left running, seen before it is stopped.
209
+ const left = !timedOut && failed === null && child.pid !== undefined && anyLeft(child.pid, mark);
210
+ stop(child, true, mark);
211
+ resolve({ code, timedOut, errors: endOf(options.errors), failed, left });
212
+ };
213
+ child.on("error", (error) => {
214
+ // Only a process that never started ends here.
215
+ if (child.pid === undefined)
216
+ settle(null, error.message);
217
+ });
218
+ child.on("exit", (code) => settle(code, null));
219
+ });
220
+ }
221
+ /**
222
+ * The cause a process gave for ending early: the last line of its
223
+ * standard error that is neither Node's `Node.js v…` line nor a line
224
+ * of a stack, starting `at `. Empty where it wrote none.
225
+ */
226
+ export function lastWords(errors) {
227
+ const lines = errors
228
+ .split("\n")
229
+ .map((line) => line.trim())
230
+ .filter((line) => line !== "" && !/^Node\.js v/.test(line) && !line.startsWith("at "));
231
+ return lines.length === 0 ? "" : lines[lines.length - 1];
232
+ }
233
+ //# sourceMappingURL=processes.js.map
@@ -0,0 +1,88 @@
1
+ import { type Shop } from "./checks.js";
2
+ /** The module of a translation's statements, which its proof file
3
+ * extends. */
4
+ export declare function statementsName(translation: string): string;
5
+ /** The module that proves the statements from a proof file. */
6
+ export declare function provedName(translation: string): string;
7
+ /** The module of a translation's proof file, which is its file's name
8
+ * too. */
9
+ export declare function proofsName(translation: string): string;
10
+ /**
11
+ * What the proofs of `translation` must prove, for any values: that
12
+ * its model keeps the rule and, unless it is `first`, that it agrees
13
+ * with `first`. The same names always give the same bytes.
14
+ */
15
+ export declare function statementsModule(translation: string, first: string): string;
16
+ /** The module that proves `translation`'s statements from its proof
17
+ * file's theorems, so that a proof file proves them and no other
18
+ * statement of its own. */
19
+ export declare function provedModule(translation: string, first: string): string;
20
+ /** Where a proof file's text holds `(*{`, which the proof system reads
21
+ * as an instruction to a prover and TLC's parser as a comment, or null. */
22
+ export declare function instructionAt(text: string): {
23
+ line: number;
24
+ col: number;
25
+ } | null;
26
+ /**
27
+ * Why a proof file's text is refused before any tool reads it, with
28
+ * its place where it has one, or null: a character TLC's parser and
29
+ * the proof system place differently, an instruction to a prover in a
30
+ * comment, or a module beside the one the file is named after, which
31
+ * could load or define what the checks of the file's own units do not
32
+ * see.
33
+ */
34
+ export declare function proofTextFault(text: string, module: string): {
35
+ line: number | null;
36
+ col: number | null;
37
+ why: string;
38
+ } | null;
39
+ /** What the XML of a proof file says it holds: its own units, in the
40
+ * order of their place, the modules it loads, and the names of its
41
+ * theorems. */
42
+ export type ProofHolds = {
43
+ /** Its first unit that is not one a proof file holds, with why. */
44
+ refused: {
45
+ line: number | null;
46
+ col: number | null;
47
+ why: string;
48
+ } | null;
49
+ /** Every operator defined at the module's own level, its own and
50
+ * those it extends, with the module whose file defines it. */
51
+ defined: Record<string, string>;
52
+ /** The names of its own named theorems and lemmas. */
53
+ theorems: string[];
54
+ };
55
+ /**
56
+ * Read what the XML TLC's parser wrote of the module `module` holds.
57
+ * Its own units are those whose place names the module itself; the
58
+ * units it extends are listed under it too. Throws a GuardError where
59
+ * the XML is not of the shape TLC's parser writes.
60
+ */
61
+ export declare function proofHolds(xml: string, module: string): ProofHolds;
62
+ /** How the proofs of a guard stand. */
63
+ export type ProofCheck =
64
+ /** Accepted, for every translation in name order. */
65
+ {
66
+ kind: "accepted";
67
+ translations: string[];
68
+ }
69
+ /** Not accepted, with the first reason found: by translation in
70
+ * name order, and for each, the first condition that does not hold. */
71
+ | {
72
+ kind: "not accepted";
73
+ reason: string;
74
+ };
75
+ /** The words of the command line after `java` that run TLC's parser
76
+ * alone on `<module>.tla`, with the proof system's standard modules
77
+ * on its library path. */
78
+ export declare function sanyArgs(jar: string, stdlib: string, module: string): string[];
79
+ /** The words of the command line after `java` that write the XML of
80
+ * `<module>.tla`, written to standard output. */
81
+ export declare function xmlArgs(jar: string, stdlib: string, module: string): string[];
82
+ /**
83
+ * Check a guard's proofs, translation by translation in name order,
84
+ * each by the five conditions in order, and stop at the first that
85
+ * does not hold. Throws a GuardError only where Java could not run
86
+ * TLC's parser: no proof is at fault then.
87
+ */
88
+ export declare function checkProofs(shop: Shop): Promise<ProofCheck>;