tslaws 0.9.0 → 1.0.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.
Files changed (123) hide show
  1. package/README.md +8 -2
  2. package/bin/tslaws.js +8 -2
  3. package/dist/browser.d.ts +1 -1
  4. package/dist/browser.js +36 -7
  5. package/dist/bundled.js +2 -2
  6. package/dist/bundlerPlugins.d.ts +2 -0
  7. package/dist/bundlerPlugins.js +7 -0
  8. package/dist/certificates.d.ts +139 -0
  9. package/dist/certificates.js +433 -0
  10. package/dist/chc.d.ts +85 -0
  11. package/dist/chc.js +298 -0
  12. package/dist/check.d.ts +13 -1
  13. package/dist/check.js +215 -28
  14. package/dist/cli.d.ts +4 -0
  15. package/dist/cli.js +324 -41
  16. package/dist/compile.d.ts +17 -1
  17. package/dist/compile.js +244 -25
  18. package/dist/coverage.d.ts +55 -3
  19. package/dist/coverage.js +231 -12
  20. package/dist/e2eApi.js +3 -1
  21. package/dist/generators.js +12 -2
  22. package/dist/github.d.ts +49 -5
  23. package/dist/github.js +107 -24
  24. package/dist/githubCli.d.ts +28 -0
  25. package/dist/githubCli.js +1039 -211
  26. package/dist/hints.js +2 -1
  27. package/dist/init.js +42 -0
  28. package/dist/isolate.d.ts +28 -0
  29. package/dist/isolate.js +262 -0
  30. package/dist/isolatedCheck.d.ts +1 -0
  31. package/dist/isolatedCheck.js +16 -0
  32. package/dist/lawHost.d.ts +50 -0
  33. package/dist/lawHost.js +199 -0
  34. package/dist/lawRuntime.d.ts +39 -0
  35. package/dist/lawRuntime.js +242 -0
  36. package/dist/lock.d.ts +49 -9
  37. package/dist/lock.js +132 -21
  38. package/dist/mcp.js +28 -7
  39. package/dist/mutate.d.ts +4 -0
  40. package/dist/mutate.js +7 -1
  41. package/dist/plan.d.ts +1 -0
  42. package/dist/plan.js +4 -0
  43. package/dist/pluginHooks.d.ts +16 -0
  44. package/dist/pluginHooks.js +48 -0
  45. package/dist/pluginHost.d.ts +5 -0
  46. package/dist/pluginHost.js +43 -0
  47. package/dist/pluginWorker.d.ts +1 -0
  48. package/dist/pluginWorker.js +40 -0
  49. package/dist/plural.d.ts +2 -0
  50. package/dist/plural.js +4 -0
  51. package/dist/pool.d.ts +80 -7
  52. package/dist/pool.js +222 -11
  53. package/dist/projects.d.ts +70 -0
  54. package/dist/projects.js +457 -0
  55. package/dist/promote.d.ts +56 -2
  56. package/dist/promote.js +429 -13
  57. package/dist/property.js +13 -2
  58. package/dist/propertyWorker.js +11 -1
  59. package/dist/proposals.d.ts +52 -4
  60. package/dist/proposals.js +484 -21
  61. package/dist/reporters.js +5 -1
  62. package/dist/runtimeEnv.d.ts +32 -0
  63. package/dist/runtimeEnv.js +146 -0
  64. package/dist/sandbox.d.ts +98 -0
  65. package/dist/sandbox.js +673 -0
  66. package/dist/sandboxGuard.d.ts +56 -0
  67. package/dist/sandboxGuard.js +249 -0
  68. package/dist/seal.d.ts +9 -0
  69. package/dist/seal.js +426 -0
  70. package/dist/security/cli.d.ts +6 -0
  71. package/dist/security/cli.js +39 -0
  72. package/dist/security/cwe.d.ts +14 -0
  73. package/dist/security/cwe.js +40 -0
  74. package/dist/security/deps/cache.d.ts +28 -0
  75. package/dist/security/deps/cache.js +107 -0
  76. package/dist/security/deps/config.d.ts +16 -0
  77. package/dist/security/deps/config.js +43 -0
  78. package/dist/security/deps/imports.d.ts +4 -0
  79. package/dist/security/deps/imports.js +33 -0
  80. package/dist/security/deps/lockfiles.d.ts +23 -0
  81. package/dist/security/deps/lockfiles.js +153 -0
  82. package/dist/security/deps/osv.d.ts +66 -0
  83. package/dist/security/deps/osv.js +137 -0
  84. package/dist/security/deps/run.d.ts +54 -0
  85. package/dist/security/deps/run.js +135 -0
  86. package/dist/security/deps/severity.d.ts +26 -0
  87. package/dist/security/deps/severity.js +78 -0
  88. package/dist/security/generators.d.ts +9 -0
  89. package/dist/security/generators.js +36 -0
  90. package/dist/security/index.d.ts +27 -0
  91. package/dist/security/index.js +18 -0
  92. package/dist/security/integers.d.ts +31 -0
  93. package/dist/security/integers.js +105 -0
  94. package/dist/security/law.d.ts +54 -0
  95. package/dist/security/law.js +54 -0
  96. package/dist/security/pathTraversal.d.ts +29 -0
  97. package/dist/security/pathTraversal.js +146 -0
  98. package/dist/security/prototypePollution.d.ts +30 -0
  99. package/dist/security/prototypePollution.js +142 -0
  100. package/dist/security/redirect.d.ts +21 -0
  101. package/dist/security/redirect.js +154 -0
  102. package/dist/security/redos.d.ts +21 -0
  103. package/dist/security/redos.js +93 -0
  104. package/dist/security/validation.d.ts +25 -0
  105. package/dist/security/validation.js +89 -0
  106. package/dist/security/xss.d.ts +21 -0
  107. package/dist/security/xss.js +90 -0
  108. package/dist/settings.d.ts +20 -0
  109. package/dist/settings.js +32 -0
  110. package/dist/smt.d.ts +29 -1
  111. package/dist/smt.js +239 -107
  112. package/dist/smtWorker.d.ts +1 -0
  113. package/dist/smtWorker.js +26 -0
  114. package/dist/solvers.d.ts +9 -0
  115. package/dist/solvers.js +11 -5
  116. package/dist/symbolic.d.ts +54 -1
  117. package/dist/symbolic.js +239 -13
  118. package/dist/tsconfigResolve.d.ts +28 -0
  119. package/dist/tsconfigResolve.js +141 -0
  120. package/dist/types.d.ts +28 -0
  121. package/env.d.ts +17 -0
  122. package/hooks/claude-hook.mjs +2 -0
  123. package/package.json +22 -6
package/dist/pool.js CHANGED
@@ -1,28 +1,200 @@
1
- import { availableParallelism } from 'node:os';
2
- /** How many laws run at once: the machine's cores, up to 8, or TSLAWS_JOBS. */
1
+ import { AsyncLocalStorage } from 'node:async_hooks';
2
+ import { availableParallelism, constants, freemem, setPriority } from 'node:os';
3
+ const gibibyte = 2 ** 30;
4
+ /**
5
+ * How many things run at once. A given number (flag, then `TSLAWS_JOBS`, then `jobs` in tslaws.json) is used as it is.
6
+ * Without one: the cores less one, so the machine stays usable, up to 8, and no more than one slot per GiB of free memory
7
+ * (a Z3 instance holds a large WebAssembly heap).
8
+ */
9
+ export function resolveJobs(input) {
10
+ const given = (value) => value !== undefined && Number.isInteger(value) && value > 0 ? value : undefined;
11
+ const fromEnv = input.env === undefined || input.env.trim() === '' ? undefined : given(Number(input.env));
12
+ const chosen = given(input.flag) ?? fromEnv ?? given(input.config);
13
+ if (chosen !== undefined)
14
+ return chosen;
15
+ const byCores = Math.min(8, Math.max(1, input.cores - 1));
16
+ return Math.max(1, Math.min(byCores, Math.floor(input.freeBytes / gibibyte)));
17
+ }
18
+ let sources = {};
19
+ let cached;
20
+ /**
21
+ * Sets where the number of slots comes from (the command line flag, `jobs` of tslaws.json). An absent `flag` leaves the
22
+ * flag as it was; a `config` key replaces the config source even when it is undefined, so one project's `jobs` never
23
+ * carries over to the next project checked in the same process.
24
+ */
25
+ export function configureJobs(next = {}) {
26
+ sources = {
27
+ flag: next.flag ?? sources.flag,
28
+ config: 'config' in next ? next.config : sources.config,
29
+ };
30
+ cached = undefined;
31
+ }
32
+ /** Forgets every configured source and the number worked out from them; for tests. */
33
+ export function resetJobs() {
34
+ sources = {};
35
+ cached = undefined;
36
+ }
37
+ /** The size of the CPU budget. Worked out once per configuration, so free memory does not move it mid-run. */
3
38
  export function jobs() {
4
- const given = Number(process.env.TSLAWS_JOBS);
5
- return Number.isInteger(given) && given > 0 ? given : Math.min(8, availableParallelism());
39
+ cached ??= resolveJobs({
40
+ ...sources,
41
+ env: process.env.TSLAWS_JOBS,
42
+ cores: availableParallelism(),
43
+ freeBytes: freemem(),
44
+ });
45
+ return cached;
46
+ }
47
+ /** Runs below normal priority, so a solver does not starve the machine's other work. Failures are ignored. */
48
+ export function lowerPriority(pid) {
49
+ if (pid === undefined)
50
+ return;
51
+ try {
52
+ setPriority(pid, constants.priority.PRIORITY_BELOW_NORMAL);
53
+ }
54
+ catch {
55
+ // Not allowed, or the process is already gone: it simply runs at its usual priority.
56
+ }
6
57
  }
58
+ /**
59
+ * Slots of CPU for everything heavy: a law run, a solve, a spawned solver, a property worker task, a sandboxed law
60
+ * process. One budget serves the whole process, so the pools below no longer each take every core.
61
+ *
62
+ * Nesting reuses the slot: work started inside a function that `run` is holding a slot for (a proof under the law that
63
+ * asked for it) takes no second slot, so a waiting law can never be starved by the work it is waiting on, and a budget
64
+ * of 1 cannot deadlock. A race or portfolio asks for one extra slot per additional member with `portfolio`, never
65
+ * waiting for it: with none free the members run one after another on the slot already held.
66
+ */
67
+ export class Budget {
68
+ used = 0;
69
+ waiting = [];
70
+ held = new AsyncLocalStorage();
71
+ limit;
72
+ constructor(limit = jobs) {
73
+ this.limit = limit;
74
+ }
75
+ /** The slots in use now. */
76
+ get inUse() {
77
+ return this.used;
78
+ }
79
+ /** The most slots there may be. */
80
+ get size() {
81
+ return this.limit();
82
+ }
83
+ /** Whether this async context already holds a slot. */
84
+ get holding() {
85
+ return this.held.getStore() === true;
86
+ }
87
+ /** Runs `work` holding one slot, waiting for one when none is free; reuses the slot this context already holds. */
88
+ async run(work) {
89
+ if (this.holding)
90
+ return work();
91
+ await this.take();
92
+ try {
93
+ return await this.held.run(true, work);
94
+ }
95
+ finally {
96
+ this.give();
97
+ }
98
+ }
99
+ /** Takes a slot if one is free right now; the returned function gives it back. */
100
+ tryHold() {
101
+ if (this.used >= this.limit())
102
+ return undefined;
103
+ this.used += 1;
104
+ let given = false;
105
+ return () => {
106
+ if (given)
107
+ return;
108
+ given = true;
109
+ this.give();
110
+ };
111
+ }
112
+ /**
113
+ * Runs the tasks together, each past the first on an extra slot, and the rest one after another on the slot already
114
+ * held. Results come back in the order of the tasks.
115
+ */
116
+ portfolio(tasks) {
117
+ return this.run(async () => {
118
+ const results = Array.from({ length: tasks.length });
119
+ const together = [];
120
+ const inTurn = [];
121
+ let failure;
122
+ tasks.forEach((task, index) => {
123
+ const release = index === 0 ? undefined : this.tryHold();
124
+ if (release === undefined) {
125
+ inTurn.push(index);
126
+ return;
127
+ }
128
+ together.push(this.held
129
+ .run(true, task)
130
+ .then((value) => {
131
+ results[index] = value;
132
+ }, (error) => {
133
+ failure ??= { error };
134
+ })
135
+ .finally(release));
136
+ });
137
+ for (const index of inTurn) {
138
+ try {
139
+ results[index] = await tasks[index]();
140
+ }
141
+ catch (error) {
142
+ failure ??= { error };
143
+ }
144
+ }
145
+ await Promise.all(together);
146
+ if (failure !== undefined)
147
+ throw failure.error;
148
+ return results;
149
+ });
150
+ }
151
+ take() {
152
+ if (this.used < this.limit()) {
153
+ this.used += 1;
154
+ return Promise.resolve();
155
+ }
156
+ return new Promise((resolve) => this.waiting.push(resolve));
157
+ }
158
+ give() {
159
+ // A slot goes straight to the next one waiting, unless the budget has shrunk below what is in use.
160
+ const next = this.used > this.limit() ? undefined : this.waiting.shift();
161
+ if (next === undefined)
162
+ this.used -= 1;
163
+ else
164
+ next();
165
+ }
166
+ }
167
+ /** The one budget of this process. */
168
+ export const budget = new Budget();
7
169
  /**
8
170
  * A pool of workers where each request holds one exclusively, so a timeout can terminate its worker without
9
- * disturbing a neighbour. Workers start on demand and are reused.
171
+ * disturbing a neighbour. Workers start on demand and are reused. `size` is read each time, so it may follow the budget.
172
+ * With `idleMs`, a worker that sits unused that long is terminated.
10
173
  */
11
174
  export class WorkerPool {
12
175
  idle = [];
176
+ timers = new Map();
13
177
  waiting = [];
14
178
  count = 0;
15
179
  start;
16
180
  size;
17
- constructor(start, size = jobs()) {
181
+ idleMs;
182
+ constructor(start, size = jobs, idleMs) {
18
183
  this.start = start;
19
- this.size = size;
184
+ this.size = typeof size === 'number' ? () => size : size;
185
+ this.idleMs = idleMs;
186
+ }
187
+ /** The workers this pool has started and not yet ended, busy or idle. */
188
+ get live() {
189
+ return this.count;
20
190
  }
21
191
  acquire() {
22
192
  const free = this.idle.pop();
23
- if (free !== undefined)
193
+ if (free !== undefined) {
194
+ this.untimed(free);
24
195
  return Promise.resolve(free);
25
- if (this.count < this.size) {
196
+ }
197
+ if (this.count < this.size()) {
26
198
  this.count += 1;
27
199
  return Promise.resolve(this.start());
28
200
  }
@@ -30,8 +202,9 @@ export class WorkerPool {
30
202
  }
31
203
  /** Starts up to `n` workers now, so they finish loading while the caller does other work. */
32
204
  warm(n) {
33
- while (this.count < Math.min(n, this.size)) {
205
+ while (this.count < Math.min(n, this.size())) {
34
206
  this.count += 1;
207
+ // Not timed: it waits for the first law, which may be a compile away.
35
208
  this.idle.push(this.start());
36
209
  }
37
210
  }
@@ -40,7 +213,22 @@ export class WorkerPool {
40
213
  if (next !== undefined)
41
214
  next(worker);
42
215
  else
43
- this.idle.push(worker);
216
+ this.park(worker);
217
+ }
218
+ /** Terminates one idle worker, to make room elsewhere; false when none is idle. */
219
+ evictOne() {
220
+ const worker = this.idle.shift();
221
+ if (worker === undefined)
222
+ return false;
223
+ this.untimed(worker);
224
+ void worker.terminate();
225
+ this.count -= 1;
226
+ return true;
227
+ }
228
+ /** Terminates the idle workers, for a project that is done (a scratch copy that is about to be deleted). */
229
+ close() {
230
+ while (this.evictOne())
231
+ ;
44
232
  }
45
233
  /** Terminates a worker that timed out or crashed; a waiting request gets a fresh one. */
46
234
  discard(worker) {
@@ -51,4 +239,27 @@ export class WorkerPool {
51
239
  else
52
240
  next(this.start());
53
241
  }
242
+ park(worker) {
243
+ this.idle.push(worker);
244
+ if (this.idleMs === undefined)
245
+ return;
246
+ const timer = setTimeout(() => {
247
+ const at = this.idle.indexOf(worker);
248
+ if (at < 0)
249
+ return;
250
+ this.idle.splice(at, 1);
251
+ this.timers.delete(worker);
252
+ void worker.terminate();
253
+ this.count -= 1;
254
+ }, this.idleMs);
255
+ timer.unref();
256
+ this.timers.set(worker, timer);
257
+ }
258
+ untimed(worker) {
259
+ const timer = this.timers.get(worker);
260
+ if (timer === undefined)
261
+ return;
262
+ clearTimeout(timer);
263
+ this.timers.delete(worker);
264
+ }
54
265
  }
@@ -0,0 +1,70 @@
1
+ export interface Project {
2
+ /** Absolute directory of the project. */
3
+ dir: string;
4
+ /** Directory relative to the repository root, with `/` separators; '' for the root itself. */
5
+ path: string;
6
+ /** Its tslaws.json says `"standalone": true`: root runs leave it alone unless it is named with `--project`. */
7
+ standalone: boolean;
8
+ }
9
+ export interface DiscoverOptions {
10
+ /** Keep projects whose `path` matches one of these globs (a leading `!` excludes). */
11
+ filter?: string[];
12
+ /** Keep only projects whose `path` is exactly one of these. A standalone project is kept only when named here. */
13
+ only?: string[];
14
+ /** Keep standalone projects even when they are not named. For code that must know every project. */
15
+ standalone?: boolean;
16
+ }
17
+ /** The `workspaces` globs of a package.json text (an array, or `{ packages: [...] }`); [] when absent or unparsable. */
18
+ export declare function workspacesIn(text: string): string[];
19
+ /**
20
+ * The `packages:` list of pnpm-workspace.yaml. Only the plain shapes pnpm documents are read: a block list of
21
+ * (optionally quoted) globs, or a one-line `[a, b]`. That keeps this free of a YAML dependency.
22
+ */
23
+ export declare function pnpmPackages(text: string): string[];
24
+ /**
25
+ * Every project under `root`, sorted by path. The root itself is one only when it has a tslaws.json and either that
26
+ * file has no `projects` key (a plain single project that happens to hold others) or the root carries laws of its
27
+ * own (see `rootHasLaws`); a root whose tslaws.json only lists projects is just the list.
28
+ */
29
+ export declare function discoverProjects(root: string, opts?: DiscoverOptions): Project[];
30
+ /**
31
+ * Whether `cwd` is the root of a monorepo run: it has no tslaws.json but discovery finds projects below it, or its
32
+ * tslaws.json has a `projects` list.
33
+ */
34
+ export declare function isMonorepoRoot(cwd: string): boolean;
35
+ /** The files that decide which directories are projects, as text (undefined when the file is absent). */
36
+ export interface DiscoveryInputs {
37
+ tslaws?: string;
38
+ packageJson?: string;
39
+ pnpmWorkspace?: string;
40
+ }
41
+ /**
42
+ * What `discoverProjects` would find, from a list of repository paths and the three discovery inputs instead of a
43
+ * directory: for reading a commit through the API. `files` are paths relative to the root. Sorted; '' is the root.
44
+ */
45
+ export declare function discoverFromFiles(files: readonly string[], inputs: DiscoveryInputs): string[];
46
+ /** A canonical form of what the inputs say about discovery: equal signatures, same discovery. */
47
+ export declare function discoverySignature(inputs: DiscoveryInputs): string;
48
+ /** What changed against a base ref. */
49
+ export interface ChangeSet {
50
+ /** The merge-base of the ref and HEAD that the working tree was compared with. */
51
+ base: string;
52
+ /** Changed files relative to the directory asked about, with `/` separators; deleted files are included. */
53
+ files: string[];
54
+ }
55
+ /**
56
+ * What changed since `git merge-base <ref> HEAD`: committed, staged, unstaged and untracked files under `cwd` (ignored
57
+ * files do not count, nor do `.tslaws` and `node_modules`).
58
+ */
59
+ export declare function changedSince(cwd: string, ref: string): ChangeSet;
60
+ export interface Affected {
61
+ project: Project;
62
+ /** Why it runs, in a few words. */
63
+ reason: string;
64
+ }
65
+ /**
66
+ * The projects of `root` that changed files (paths relative to `root`) can affect: the project that holds each file (the
67
+ * nearest enclosing one) and every project that depends on one, however indirectly. A change to a root config file or
68
+ * to a file a tslaws.json extends selects every project.
69
+ */
70
+ export declare function affectedProjects(root: string, files: string[]): Affected[];