@smoothbricks/nx-plugin 0.5.3 → 0.5.5

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 (29) hide show
  1. package/README.md +52 -12
  2. package/dist/executors/bounded-exec/cpu-tokens.d.ts +13 -0
  3. package/dist/executors/bounded-exec/cpu-tokens.d.ts.map +1 -1
  4. package/dist/executors/bounded-exec/cpu-tokens.js +9 -9
  5. package/dist/executors/bounded-exec/executor.d.ts.map +1 -1
  6. package/dist/executors/bounded-exec/executor.js +20 -6
  7. package/dist/executors/bounded-exec/progress.d.ts +8 -3
  8. package/dist/executors/bounded-exec/progress.d.ts.map +1 -1
  9. package/dist/executors/bounded-exec/progress.js +100 -10
  10. package/dist/executors/bounded-exec/verdict.d.ts +5 -4
  11. package/dist/executors/bounded-exec/verdict.d.ts.map +1 -1
  12. package/dist/executors/bounded-exec/verdict.js +1 -0
  13. package/dist/runtime-input-policy.d.ts +60 -0
  14. package/dist/runtime-input-policy.d.ts.map +1 -0
  15. package/dist/runtime-input-policy.js +379 -0
  16. package/dist/testing.js +1 -1
  17. package/managed/raw/patches/nx@23.2.1.patch +26 -0
  18. package/package.json +7 -2
  19. package/src/executors/bounded-exec/cpu-tokens.ts +15 -9
  20. package/src/executors/bounded-exec/executor.test.ts +26 -23
  21. package/src/executors/bounded-exec/executor.ts +19 -4
  22. package/src/executors/bounded-exec/go-progress-host.test.ts +171 -0
  23. package/src/executors/bounded-exec/progress.test.ts +73 -0
  24. package/src/executors/bounded-exec/progress.ts +95 -12
  25. package/src/executors/bounded-exec/verdict.ts +5 -4
  26. package/src/nx-daemon-start-never.test.ts +30 -1
  27. package/src/runtime-input-policy.ts +395 -0
  28. package/src/runtime-input.test.ts +160 -3
  29. package/src/testing.ts +1 -1
@@ -0,0 +1,379 @@
1
+ import { accessSync, constants, readdirSync, readFileSync, realpathSync, statSync } from 'node:fs';
2
+ import { delimiter, isAbsolute, join, relative, resolve, sep } from 'node:path';
3
+ import { RUNTIME_INPUT_SCRIPT } from './runtime-input.js';
4
+ /**
5
+ * What a runtime input may run, and how its failures are read back after Nx refuses it.
6
+ *
7
+ * A runtime input runs wherever Nx hashes: a developer's Mac, a NixOS CI runner, a cowshed sandbox. Only the programs a
8
+ * workspace declares are the same in all of them: devenv's Nix profile and the checkout's own `node_modules`. A
9
+ * program the host's operating system ships is not. macOS ships `shasum` in /usr/bin; it is a Perl script, not
10
+ * coreutils, and no devenv profile provides it (devenv's carries coreutils, `sha256sum` included). An input that named
11
+ * it hashed on every Mac and exited 127 on the NixOS runner, which Nx reported only as
12
+ * `invalid utf-8 sequence of 1 bytes from index 0` (runtime-input.sh says why).
13
+ */
14
+ /** Words a POSIX `sh` runs itself, without a PATH lookup. */
15
+ const SHELL_BUILTINS = new Set([
16
+ '.',
17
+ ':',
18
+ '[',
19
+ 'alias',
20
+ 'break',
21
+ 'cd',
22
+ 'continue',
23
+ 'echo',
24
+ 'eval',
25
+ 'exit',
26
+ 'export',
27
+ 'false',
28
+ 'getopts',
29
+ 'hash',
30
+ 'local',
31
+ 'printf',
32
+ 'pwd',
33
+ 'read',
34
+ 'readonly',
35
+ 'return',
36
+ 'set',
37
+ 'shift',
38
+ 'test',
39
+ 'times',
40
+ 'trap',
41
+ 'true',
42
+ 'type',
43
+ 'ulimit',
44
+ 'umask',
45
+ 'unalias',
46
+ 'unset',
47
+ 'wait',
48
+ ]);
49
+ /** Reserved words that may stand before a command's program. */
50
+ const SHELL_KEYWORDS = new Set([
51
+ '!',
52
+ '{',
53
+ '}',
54
+ 'if',
55
+ 'then',
56
+ 'else',
57
+ 'elif',
58
+ 'fi',
59
+ 'do',
60
+ 'done',
61
+ 'while',
62
+ 'until',
63
+ ]);
64
+ /** Reserved words whose clause names no program (`for name in words`, `case word in`). */
65
+ const CLAUSE_KEYWORDS = new Set(['for', 'case', 'esac', 'in']);
66
+ /** Shells whose `-c` operand is a script of further commands. */
67
+ const SHELLS = new Set(['sh', 'bash']);
68
+ const ASSIGNMENT = /^[A-Za-z_][A-Za-z0-9_]*=/;
69
+ /** The index of the character that closes the quote opened before `from`, or the end of `text` when none does. */
70
+ function closingQuote(text, from, quote) {
71
+ for (let at = from; at < text.length; at += 1) {
72
+ if (text[at] === '\\' && quote !== "'")
73
+ at += 1;
74
+ else if (text[at] === quote)
75
+ return at;
76
+ }
77
+ return text.length;
78
+ }
79
+ /** The index of the `close` that balances the `open` before `from`, skipping quoted text, or the end of `text`. */
80
+ function closingBracket(text, from, open, close) {
81
+ let depth = 1;
82
+ for (let at = from; at < text.length; at += 1) {
83
+ const character = text[at];
84
+ if (character === '\\')
85
+ at += 1;
86
+ else if (character === "'" || character === '"' || character === '`')
87
+ at = closingQuote(text, at + 1, character);
88
+ else if (character === open)
89
+ depth += 1;
90
+ else if (character === close) {
91
+ depth -= 1;
92
+ if (depth === 0)
93
+ return at;
94
+ }
95
+ }
96
+ return text.length;
97
+ }
98
+ /**
99
+ * The shell tokens of `script`. A command substitution is part of its word, and the programs it runs are appended to
100
+ * `nested`; quoting is removed from the words, which are only inspected, never run.
101
+ */
102
+ function shellTokens(script, nested) {
103
+ const tokens = [];
104
+ let word;
105
+ const flush = () => {
106
+ if (word !== undefined)
107
+ tokens.push({ kind: 'word', text: word });
108
+ word = undefined;
109
+ };
110
+ const append = (text) => {
111
+ word = (word ?? '') + text;
112
+ };
113
+ /** Consumes the expansion starting with `$` or a backquote at `at`; returns the index after it. */
114
+ const expansion = (at) => {
115
+ if (script[at] === '`') {
116
+ const end = closingQuote(script, at + 1, '`');
117
+ nested.push(...commandPrograms(script.slice(at + 1, end)));
118
+ append('`…`');
119
+ return end + 1;
120
+ }
121
+ if (script.startsWith('$((', at)) {
122
+ const end = closingBracket(script, at + 2, '(', ')');
123
+ append(script.slice(at, end + 1));
124
+ return end + 1;
125
+ }
126
+ if (script.startsWith('$(', at)) {
127
+ const end = closingBracket(script, at + 2, '(', ')');
128
+ nested.push(...commandPrograms(script.slice(at + 2, end)));
129
+ append('$(…)');
130
+ return end + 1;
131
+ }
132
+ if (script.startsWith('${', at)) {
133
+ const end = closingBracket(script, at + 2, '{', '}');
134
+ append(script.slice(at, end + 1));
135
+ return end + 1;
136
+ }
137
+ append('$');
138
+ return at + 1;
139
+ };
140
+ let at = 0;
141
+ while (at < script.length) {
142
+ const character = script[at];
143
+ if (character === "'") {
144
+ const end = closingQuote(script, at + 1, "'");
145
+ append(script.slice(at + 1, end));
146
+ at = end + 1;
147
+ }
148
+ else if (character === '"') {
149
+ append('');
150
+ at += 1;
151
+ while (at < script.length && script[at] !== '"') {
152
+ if (script[at] === '\\' && at + 1 < script.length) {
153
+ append('$`"\\\n'.includes(script[at + 1]) ? script[at + 1] : script.slice(at, at + 2));
154
+ at += 2;
155
+ }
156
+ else if (script[at] === '$' || script[at] === '`') {
157
+ at = expansion(at);
158
+ }
159
+ else {
160
+ append(script[at]);
161
+ at += 1;
162
+ }
163
+ }
164
+ at += 1;
165
+ }
166
+ else if (character === '\\') {
167
+ append(script.slice(at + 1, at + 2));
168
+ at += 2;
169
+ }
170
+ else if (character === '$' || character === '`') {
171
+ at = expansion(at);
172
+ }
173
+ else if (character === '#' && word === undefined) {
174
+ at = script.indexOf('\n', at);
175
+ if (at < 0)
176
+ at = script.length;
177
+ }
178
+ else if (character === ' ' || character === '\t') {
179
+ flush();
180
+ at += 1;
181
+ }
182
+ else if (';&|()\n'.includes(character)) {
183
+ flush();
184
+ tokens.push({ kind: 'separator' });
185
+ at += 1;
186
+ }
187
+ else if (character === '<' || character === '>') {
188
+ // A file-descriptor number written against the operator (`2>&1`) is part of it, not a word.
189
+ if (word !== undefined && /^\d+$/.test(word))
190
+ word = undefined;
191
+ flush();
192
+ at += 1;
193
+ while (at < script.length && '<>&|'.includes(script[at]))
194
+ at += 1;
195
+ tokens.push({ kind: 'redirect' });
196
+ }
197
+ else {
198
+ append(character);
199
+ at += 1;
200
+ }
201
+ }
202
+ flush();
203
+ return tokens;
204
+ }
205
+ /** The programs one simple command runs, given its words in order. */
206
+ function simpleCommandPrograms(words) {
207
+ let at = 0;
208
+ while (at < words.length && (SHELL_KEYWORDS.has(words[at]) || ASSIGNMENT.test(words[at])))
209
+ at += 1;
210
+ const program = words[at];
211
+ if (program === undefined || CLAUSE_KEYWORDS.has(program))
212
+ return [];
213
+ const operands = words.slice(at + 1);
214
+ if (program === 'exec')
215
+ return simpleCommandPrograms(operands);
216
+ // `command -v name` looks a name up; `command name` runs it.
217
+ if (program === 'command')
218
+ return operands[0]?.startsWith('-') ? [] : simpleCommandPrograms(operands);
219
+ if (program === 'env') {
220
+ let operand = 0;
221
+ while (operand < operands.length && operands[operand].startsWith('-') && operands[operand] !== '--') {
222
+ operand += operands[operand] === '-u' || operands[operand] === '-C' ? 2 : 1;
223
+ }
224
+ if (operands[operand] === '--')
225
+ operand += 1;
226
+ return [program, ...simpleCommandPrograms(operands.slice(operand))];
227
+ }
228
+ if (SHELLS.has(program)) {
229
+ // The wrapper runs its operands as one command.
230
+ if (operands[0] === RUNTIME_INPUT_SCRIPT)
231
+ return [program, ...simpleCommandPrograms(operands.slice(1))];
232
+ const options = operands.findIndex((operand) => !operand.startsWith('-'));
233
+ const flags = operands.slice(0, options < 0 ? operands.length : options);
234
+ const script = options < 0 ? undefined : operands[options];
235
+ if (script !== undefined && flags.some((flag) => flag !== '--' && !flag.startsWith('--') && flag.includes('c'))) {
236
+ return [program, ...commandPrograms(script)];
237
+ }
238
+ }
239
+ return [program];
240
+ }
241
+ /**
242
+ * The programs `script` runs by name or path: the program of each simple command, through `env`, `exec`,
243
+ * `command`, `sh -c` and command substitutions, and the command a `runtime-input.sh` input runs. Shell builtins are
244
+ * not programs. A script file a command runs (`sh tooling/x.sh`) is its own source's concern, not read here.
245
+ */
246
+ export function commandPrograms(script) {
247
+ const programs = [];
248
+ const nested = [];
249
+ let words = [];
250
+ let redirectTarget = false;
251
+ const end = () => {
252
+ programs.push(...simpleCommandPrograms(words));
253
+ words = [];
254
+ };
255
+ for (const token of shellTokens(script, nested)) {
256
+ if (token.kind === 'separator')
257
+ end();
258
+ else if (token.kind === 'redirect')
259
+ redirectTarget = true;
260
+ else if (redirectTarget)
261
+ redirectTarget = false;
262
+ else
263
+ words.push(token.text);
264
+ }
265
+ end();
266
+ return [...programs, ...nested].filter((program) => !SHELL_BUILTINS.has(program));
267
+ }
268
+ function within(directory, file) {
269
+ const path = relative(directory, file);
270
+ return path !== '' && !path.startsWith('..') && !isAbsolute(path);
271
+ }
272
+ function isExecutableFile(file) {
273
+ try {
274
+ accessSync(file, constants.X_OK);
275
+ return statSync(file).isFile();
276
+ }
277
+ catch (error) {
278
+ if (error instanceof Error && 'code' in error && ['ENOENT', 'ENOTDIR', 'EACCES'].includes(String(error.code))) {
279
+ return false;
280
+ }
281
+ throw error;
282
+ }
283
+ }
284
+ function onPath(program, path) {
285
+ for (const directory of path.split(delimiter)) {
286
+ // An empty entry means the working directory; Nx runs inputs from the root, which a name never resolves into.
287
+ if (directory === '')
288
+ continue;
289
+ const candidate = join(directory, program);
290
+ if (isExecutableFile(candidate))
291
+ return candidate;
292
+ }
293
+ return undefined;
294
+ }
295
+ /**
296
+ * The only host paths every platform guarantees: NixOS keeps exactly these two outside the store, for `#!` lines and
297
+ * for Nx, which runs every runtime input as `sh -c`.
298
+ */
299
+ const GUARANTEED_HOST_PROGRAMS = new Set(['/bin/sh', '/usr/bin/env']);
300
+ /**
301
+ * The build output of a package in this checkout that `found`, a link in the checkout's `node_modules/.bin`, names;
302
+ * undefined for any other file, an installed package's bin included.
303
+ */
304
+ function workspaceBuildOutput(found, root) {
305
+ if (!within(join(root, 'node_modules', '.bin'), found))
306
+ return undefined;
307
+ const physicalRoot = realpathSync(root);
308
+ const target = realpathSync(found);
309
+ return within(physicalRoot, target) && !relative(physicalRoot, target).split(sep).includes('node_modules')
310
+ ? target
311
+ : undefined;
312
+ }
313
+ /**
314
+ * Every program `command` runs that the workspace does not declare: one PATH finds outside the Nix store, the
315
+ * checkout, the home and the guaranteed host paths, one nothing on PATH provides, or one only a workspace package's
316
+ * build output provides. A relative path names a file in the checkout.
317
+ */
318
+ export function hostPrograms(command, search) {
319
+ const declared = (file) => file.startsWith('/nix/') ||
320
+ GUARANTEED_HOST_PROGRAMS.has(file) ||
321
+ within(search.root, file) ||
322
+ within(search.home, file);
323
+ return [...new Set(commandPrograms(command))].flatMap((program) => {
324
+ if (program.includes('/')) {
325
+ const found = resolve(search.root, program);
326
+ return declared(found) ? [] : [{ program, refusal: 'host', found }];
327
+ }
328
+ const found = onPath(program, search.path);
329
+ if (found === undefined)
330
+ return [{ program, refusal: 'absent' }];
331
+ if (!declared(found))
332
+ return [{ program, refusal: 'host', found }];
333
+ const target = workspaceBuildOutput(found, search.root);
334
+ return target === undefined ? [] : [{ program, refusal: 'workspace-bin', found, target }];
335
+ });
336
+ }
337
+ /** Why `host` is refused, naming where PATH found it and what to run instead. */
338
+ export function hostProgramReason(host) {
339
+ switch (host.refusal) {
340
+ case 'host':
341
+ return `PATH finds ${host.program} at ${host.found}, a host program no devenv profile declares`;
342
+ case 'absent':
343
+ return `nothing on PATH provides ${host.program}`;
344
+ case 'workspace-bin':
345
+ return (`PATH finds ${host.program} at ${host.found}, a link to this checkout's build output ${host.target}, ` +
346
+ 'which an install that runs before the build does not create; run that file with node or bun instead');
347
+ }
348
+ }
349
+ /** The directory under the workspace root where runtime-input.sh retains each failed input. */
350
+ export const RUNTIME_INPUT_FAILURES = '.runtime-input-failures';
351
+ function absentAsUndefined(read) {
352
+ try {
353
+ return read();
354
+ }
355
+ catch (error) {
356
+ if (error instanceof Error && 'code' in error && error.code === 'ENOENT')
357
+ return undefined;
358
+ throw error;
359
+ }
360
+ }
361
+ /**
362
+ * The report of every failure runtime-input.sh retained under `root` at or after `sinceMs`, oldest first, each headed
363
+ * by its path. Nx 23.2.1 names a failed runtime input only as `invalid utf-8 sequence of 1 bytes from index N` and
364
+ * drops the input's stderr, so a headless run prints these after Nx fails: they name the command, its exit status and
365
+ * its stderr. A record without a report (its evidence could not be written) is named as such.
366
+ */
367
+ export function runtimeInputFailureReports(root, sinceMs) {
368
+ const directory = join(root, RUNTIME_INPUT_FAILURES);
369
+ return (absentAsUndefined(() => readdirSync(directory)) ?? [])
370
+ .map((name) => join(directory, name))
371
+ .map((record) => ({ record, modifiedMs: statSync(record).mtimeMs }))
372
+ .filter(({ modifiedMs }) => modifiedMs >= sinceMs)
373
+ .sort((left, right) => left.modifiedMs - right.modifiedMs)
374
+ .map(({ record }) => {
375
+ const report = join(record, 'report');
376
+ const text = absentAsUndefined(() => readFileSync(report, 'utf8'));
377
+ return text === undefined ? `${record}: no report; its evidence is incomplete\n` : `${report}:\n${text}`;
378
+ });
379
+ }
package/dist/testing.js CHANGED
@@ -145,7 +145,7 @@ export async function pidsWorkingIn(root) {
145
145
  for (const pid of (await readdir('/proc')).filter((name) => /^\d+$/.test(name))) {
146
146
  const cwd = await readlink(`/proc/${pid}/cwd`).catch((error) => {
147
147
  // Another user's process, or one that exited since the listing.
148
- if (errorCode(error) === 'EACCES' || errorCode(error) === 'ENOENT') {
148
+ if (errorCode(error) === 'EACCES' || errorCode(error) === 'ENOENT' || errorCode(error) === 'ESRCH') {
149
149
  return null;
150
150
  }
151
151
  throw error;
@@ -67,6 +67,32 @@ index 5a59f762f2002f61f84d0966c8fb48d48b3bdce2..f190d2b93cc7c1d0f59dcb1e76ef7d3e
67
67
  function handleNoWorkspace(globalNxVersion) {
68
68
  output_1.output.log({
69
69
  title: `The current directory isn't part of an Nx workspace.`,
70
+ diff --git a/dist/src/command-line/daemon/daemon.js b/dist/src/command-line/daemon/daemon.js
71
+ index 237830c6863aa8c5bdab8c4794c6fc9f1e4a48b5..857a97118d646f25f61696b2db672de56f558327 100644
72
+ --- a/dist/src/command-line/daemon/daemon.js
73
+ +++ b/dist/src/command-line/daemon/daemon.js
74
+ @@ -7,6 +7,21 @@ const handle_import_1 = require("../../utils/handle-import");
75
+ const output_1 = require("../../utils/output");
76
+ async function daemonHandler(args) {
77
+ const { daemonClient } = await (0, handle_import_1.handleImport)('../../daemon/client/client.js', __dirname);
78
+ + if (args.start && process.env.NX_DAEMON_START === 'never') {
79
+ + // An explicit start is a start: under the rule every cowshed job runs
80
+ + // with, it is refused as policy, never quietly honoured, or one command
81
+ + // would recreate the daemon that outlives its job.
82
+ + output_1.output.error({
83
+ + title: 'Nx daemon not started: NX_DAEMON_START=never forbids this client starting a daemon',
84
+ + bodyLines: [
85
+ + 'Every cowshed job runs under it, so no daemon outlives the job that started it.',
86
+ + "The checkout's daemon is its keeper's to start. A test that must start its own daemon runs that one nx",
87
+ + 'without NX_DAEMON_START, through its named fixture escape (FIXTURE_STARTS_ITS_OWN_DAEMON).',
88
+ + ],
89
+ + });
90
+ + // The handler's number is the command's exit code (handleErrors).
91
+ + return 1;
92
+ + }
93
+ if (args.start) {
94
+ const pid = await daemonClient.startInBackground();
95
+ output_1.output.log({
70
96
  diff --git a/dist/src/command-line/release/version/version-actions.js b/dist/src/command-line/release/version/version-actions.js
71
97
  index 006c077033a6e92a8bdcc6c732110736fb465b5a..2ac4a0252c813cec5492b7ac83e2d5160b97bc32 100644
72
98
  --- a/dist/src/command-line/release/version/version-actions.js
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@smoothbricks/nx-plugin",
3
- "version": "0.5.3",
3
+ "version": "0.5.5",
4
4
  "type": "module",
5
5
  "sideEffects": false,
6
6
  "main": "./loader.js",
@@ -103,6 +103,11 @@
103
103
  "import": "./dist/cargo-toolchain-policy.js",
104
104
  "default": "./dist/cargo-toolchain-policy.js"
105
105
  },
106
+ "./runtime-input-policy": {
107
+ "types": "./dist/runtime-input-policy.d.ts",
108
+ "import": "./dist/runtime-input-policy.js",
109
+ "default": "./dist/runtime-input-policy.js"
110
+ },
106
111
  "./cargo-dev-profile": {
107
112
  "types": "./dist/cargo-dev-profile.d.ts",
108
113
  "import": "./dist/cargo-dev-profile.js",
@@ -263,7 +268,7 @@
263
268
  }
264
269
  ],
265
270
  "options": {
266
- "command": "bun test --timeout=0 ./src/executors/bounded-exec/ram-temp-host.test.ts ./src/executors/bounded-exec/executor-host.test.ts",
271
+ "command": "bun test --timeout=0 ./src/executors/bounded-exec/ram-temp-host.test.ts ./src/executors/bounded-exec/executor-host.test.ts ./src/executors/bounded-exec/go-progress-host.test.ts",
267
272
  "cwd": "{projectRoot}",
268
273
  "env": {
269
274
  "COWSHED_EXECUTABLE": "{workspaceRoot}/packages/cowshed/bin/cowshed"
@@ -1,5 +1,5 @@
1
1
  import { GATEWAY_SOCKET, holdLease, type UnleasedCause } from './gateway-lease.js';
2
- import { BUN_TEST, NEXTEST_RUN } from './verdict.js';
2
+ import { BUN_TEST, GO_TEST, NEXTEST_RUN } from './verdict.js';
3
3
 
4
4
  /**
5
5
  * The client half of cowshed's host CPU budget (specs/cowshed/05_gateway.md, "Host CPU budget").
@@ -105,7 +105,7 @@ const WORKER_FLAGS: Readonly<Record<WorkerFlagKind, WorkerFlag>> = {
105
105
  },
106
106
  /** `go test -p`: packages built and tested at once, `GOMAXPROCS` by default, normally one per core. */
107
107
  'go-test': {
108
- invocation: /(?<=^|[\s;&|()])go\s+test(?=\s|$)/,
108
+ invocation: GO_TEST,
109
109
  flag: new RegExp(String.raw`(?<![\w-])-p(?:=|\s+)${WORD}`, 'g'),
110
110
  sized: (count) => `-p=${count}`,
111
111
  ownDefault: (cores, env) => parseInheritedTokens(env.GOMAXPROCS) ?? cores,
@@ -236,7 +236,7 @@ function namedCounts(text: string, flag: RegExp): number[] {
236
236
  * Where `invocation` ends in `command`, and where its own arguments end: the next `;`, `&`, `|`, `(` or `)` outside
237
237
  * quotes and `$(…)`.
238
238
  */
239
- function invocationSpan(command: string, invocation: RegExp): { end: number; argsEnd: number } {
239
+ export function invocationSpan(command: string, invocation: RegExp): { end: number; argsEnd: number } {
240
240
  const found = invocation.exec(command);
241
241
  const end = found === null ? command.length : found.index + found[0].length;
242
242
  let quote: string | null = null;
@@ -265,13 +265,19 @@ function invocationArgs(command: string, invocation: RegExp): string {
265
265
  }
266
266
 
267
267
  /**
268
- * `command` with every `flag` in `invocation`'s own arguments replaced by `sized`; when it names none, `sized` is
268
+ * `command` with every `flag` in `invocation`'s own arguments replaced by `value`; when it names none, `value` is
269
269
  * added right after the invocation if `add` says so.
270
270
  */
271
- function withSizedFlag(command: string, invocation: RegExp, flag: RegExp, sized: string, add: boolean): string {
271
+ export function withInvocationFlag(
272
+ command: string,
273
+ invocation: RegExp,
274
+ flag: RegExp,
275
+ value: string,
276
+ add: boolean,
277
+ ): string {
272
278
  const { end, argsEnd } = invocationSpan(command, invocation);
273
279
  const args = command.slice(end, argsEnd);
274
- const sizedArgs = args.search(flag) === -1 && add ? ` ${sized}${args}` : args.replace(flag, sized);
280
+ const sizedArgs = args.search(flag) === -1 && add ? ` ${value}${args}` : args.replace(flag, value);
275
281
  return `${command.slice(0, end)}${sizedArgs}${command.slice(argsEnd)}`;
276
282
  }
277
283
 
@@ -317,7 +323,7 @@ export function sizeRunner(
317
323
  case 'go-test': {
318
324
  const runner = WORKER_FLAGS[demand.kind];
319
325
  return {
320
- command: withSizedFlag(command, runner.invocation, runner.flag, runner.sized(runnerCount), true),
326
+ command: withInvocationFlag(command, runner.invocation, runner.flag, runner.sized(runnerCount), true),
321
327
  env: { [CPU_TOKENS_ENV]: held },
322
328
  };
323
329
  }
@@ -326,9 +332,9 @@ export function sizeRunner(
326
332
  // `--jobs` is lowered only where it was set: cargo-mutants refuses it beside `--in-place`.
327
333
  const jobs = Math.min(demand.jobs, runnerCount);
328
334
  const share = String(Math.max(1, Math.floor(runnerCount / jobs)));
329
- const tasks = withSizedFlag(command, CARGO_MUTANTS, JOBSERVER_TASKS, `--jobserver-tasks=${count}`, true);
335
+ const tasks = withInvocationFlag(command, CARGO_MUTANTS, JOBSERVER_TASKS, `--jobserver-tasks=${count}`, true);
330
336
  return {
331
- command: withSizedFlag(tasks, CARGO_MUTANTS, MUTANTS_JOBS, `--jobs=${jobs}`, false),
337
+ command: withInvocationFlag(tasks, CARGO_MUTANTS, MUTANTS_JOBS, `--jobs=${jobs}`, false),
332
338
  env: {
333
339
  [CPU_TOKENS_ENV]: held,
334
340
  ...(demand.jobs > 1 ? { CARGO_MUTANTS_JOBS: String(jobs) } : {}),
@@ -81,9 +81,7 @@ describe('@smoothbricks/nx-plugin:bounded-exec', () => {
81
81
  process.kill(-pid, 'SIGKILL');
82
82
  } catch (cause) {
83
83
  const code = cause instanceof Error && 'code' in cause ? cause.code : undefined;
84
- // Darwin answers EPERM, not ESRCH, for a group whose only members are unreaped zombies (a killed
85
- // descendant waiting for launchd to reap it): such a group has nothing left to kill.
86
- if (code !== 'ESRCH' && !(code === 'EPERM' && onlyZombiesIn(pid))) {
84
+ if (code !== 'ESRCH') {
87
85
  throw new Error(`cleanup owned readiness process group pgid=${pid} failed`, { cause });
88
86
  }
89
87
  } finally {
@@ -574,6 +572,8 @@ describe('@smoothbricks/nx-plugin:bounded-exec', () => {
574
572
  );
575
573
 
576
574
  await termination.armed();
575
+ // This fixture has no descendants; runBoundedExec has observed its leader's exit.
576
+ termination.release();
577
577
  expect(result.success).toBe(false);
578
578
  expect(await readFile(handled, 'utf8'), `cooperative handler pid=${handlerPid} must handle SIGTERM`).toBe(
579
579
  `handled pid=${handlerPid}\n`,
@@ -736,7 +736,9 @@ describe('@smoothbricks/nx-plugin:bounded-exec', () => {
736
736
  );
737
737
 
738
738
  await termination.armed();
739
- expect(await closed, `ready descendant pid=${descendantPid} must close its lifetime FIFO on exit`).toBe('');
739
+ const descendantOutput = await closed;
740
+ termination.release();
741
+ expect(descendantOutput, `ready descendant pid=${descendantPid} must close its lifetime FIFO on exit`).toBe('');
740
742
  expect(result.success).toBe(false);
741
743
  });
742
744
 
@@ -750,19 +752,20 @@ describe('@smoothbricks/nx-plugin:bounded-exec', () => {
750
752
  const ready = join(workspace.root, 'straggler-ready.fifo');
751
753
  expect(spawnSync('mkfifo', [fifo, ready]).status).toBe(0);
752
754
  // The leader exits on SIGTERM. The subshell beside it ignores SIGTERM, a disposition its
753
- // `tail` inherits, and announces itself only once it holds the FIFO: from then on the
754
- // graceful signal cannot end it. A process forked while that signal goes out is the same
755
- // case, without the trap.
755
+ // `tail` inherits, and announces itself only once that disposition is installed. Every
756
+ // script descendant inherits the lifetime FIFO, so EOF proves all of them ended, not
757
+ // just the ignoring child. A fork during signal delivery inherits that witness too.
756
758
  await workspace.write(
757
759
  'straggle.sh',
758
760
  [
759
- '( trap "" TERM; exec 3>"$1"; echo ready; echo armed >"$2"; exec tail -f /dev/null ) | {',
761
+ 'exec 3>"$1"',
762
+ '( trap "" TERM; echo ready; echo armed >"$2"; exec tail -f /dev/null ) | {',
760
763
  ' read -r line; echo "$line"; exec tail -f /dev/null',
761
764
  '}',
762
765
  '',
763
766
  ].join('\n'),
764
767
  );
765
- // The straggler is the FIFO's only writer and never ends on its own, so the read ends only when it is killed.
768
+ // All script descendants own a writer; the read ends only when the last one exits.
766
769
  const straggler = readToEnd(fifo);
767
770
  const termination = armedKiller('TERM-ignoring FIFO straggler', async (pid) => {
768
771
  expect(await readToEnd(ready), `straggler readiness pgid=${pid} must follow trap and FIFO open`).toBe('armed\n');
@@ -777,10 +780,16 @@ describe('@smoothbricks/nx-plugin:bounded-exec', () => {
777
780
  );
778
781
 
779
782
  await termination.armed();
783
+ const stragglerOutput = await straggler;
784
+ termination.release();
780
785
  expect(result.terminalOutput).toContain('ready');
781
- expect(await straggler).toBe('');
786
+ expect(stragglerOutput).toBe('');
782
787
  expect(result.success).toBe(false);
783
788
  expect(result.terminalOutput).toContain('Force-killing what the timed-out command left in its process group');
789
+ expect(
790
+ [...ownedProcessGroups],
791
+ 'leader exit and straggler FIFO EOF retire cleanup authority; afterEach must not signal their stale PGID',
792
+ ).toEqual([]);
784
793
  });
785
794
 
786
795
  it('runs the command with its RAM lease as TMPDIR, names held dead leases, and ends the lease after it exits', async () => {
@@ -1573,10 +1582,12 @@ function armedKiller(subject: string, ready: (pid: number) => Promise<void>) {
1573
1582
  const real = createProcessTreeKiller();
1574
1583
  const calls: NodeJS.Signals[] = [];
1575
1584
  let armed: Promise<void> | undefined;
1585
+ let group: number | undefined;
1576
1586
  return {
1577
1587
  calls,
1578
1588
  killer: {
1579
1589
  async kill(pid: number, signal: NodeJS.Signals) {
1590
+ group = pid;
1580
1591
  ownedProcessGroups.add(pid);
1581
1592
  armed ??= ready(pid);
1582
1593
  await armed;
@@ -1589,6 +1600,11 @@ function armedKiller(subject: string, ready: (pid: number) => Promise<void>) {
1589
1600
  // The executor tolerates killer errors; re-await here so a failed readiness assertion cannot be swallowed.
1590
1601
  await armed;
1591
1602
  },
1603
+ /** Called only after leader exit and EOF from every descendant's inherited lifetime writer. */
1604
+ release() {
1605
+ if (group === undefined) throw new Error(`${subject}: no process group was armed`);
1606
+ ownedProcessGroups.delete(group);
1607
+ },
1592
1608
  };
1593
1609
  }
1594
1610
 
@@ -1754,16 +1770,3 @@ function inheritedFrom(pool: CpuPool): InheritedCpuPool {
1754
1770
  function lentCount(want: number, tokens: number): number {
1755
1771
  return Math.min(want, tokens);
1756
1772
  }
1757
-
1758
- /** Every process left in group `pgid` is a zombie: `ps` lists no member in any other state. */
1759
- function onlyZombiesIn(pgid: number): boolean {
1760
- const listed = spawnSync('ps', ['-A', '-o', 'pgid=,stat='], { encoding: 'utf8' });
1761
- if (listed.status !== 0) {
1762
- return false;
1763
- }
1764
- return listed.stdout
1765
- .split('\n')
1766
- .map((line) => line.trim().split(/\s+/))
1767
- .filter(([group]) => Number(group) === pgid)
1768
- .every(([, state]) => state?.startsWith('Z') === true);
1769
- }