@smoothbricks/nx-plugin 0.5.5 → 0.5.7

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 (58) hide show
  1. package/README.md +59 -11
  2. package/dist/ensure-built-receipt.d.ts +2 -2
  3. package/dist/ensure-built-receipt.d.ts.map +1 -1
  4. package/dist/ensure-built-receipt.js +7 -2
  5. package/dist/ensure-built-witness.d.ts +12 -1
  6. package/dist/ensure-built-witness.d.ts.map +1 -1
  7. package/dist/ensure-built-witness.js +32 -0
  8. package/dist/ensure-built.d.ts +2 -2
  9. package/dist/ensure-built.d.ts.map +1 -1
  10. package/dist/ensure-built.js +27 -5
  11. package/dist/executors/all-verdicts/executor.d.ts +35 -0
  12. package/dist/executors/all-verdicts/executor.d.ts.map +1 -0
  13. package/dist/executors/all-verdicts/executor.js +133 -0
  14. package/dist/executors/all-verdicts/schema.d.ts +10 -0
  15. package/dist/executors/all-verdicts/schema.d.ts.map +1 -0
  16. package/dist/executors/all-verdicts/schema.js +2 -0
  17. package/dist/executors/bounded-exec/executor.d.ts.map +1 -1
  18. package/dist/executors/bounded-exec/executor.js +48 -11
  19. package/dist/executors/bounded-exec/progress.d.ts +1 -3
  20. package/dist/executors/bounded-exec/progress.d.ts.map +1 -1
  21. package/dist/executors/bounded-exec/progress.js +1 -2
  22. package/dist/executors/bounded-exec/runner-hook.d.ts +2 -0
  23. package/dist/executors/bounded-exec/runner-hook.d.ts.map +1 -0
  24. package/dist/executors/bounded-exec/runner-hook.js +301 -0
  25. package/dist/executors/bounded-exec/runner-lifecycle.d.ts +18 -0
  26. package/dist/executors/bounded-exec/runner-lifecycle.d.ts.map +1 -0
  27. package/dist/executors/bounded-exec/runner-lifecycle.js +92 -0
  28. package/dist/index.d.ts.map +1 -1
  29. package/dist/index.js +4 -1
  30. package/dist/producer-receipts.d.ts +24 -1
  31. package/dist/producer-receipts.d.ts.map +1 -1
  32. package/dist/producer-receipts.js +91 -60
  33. package/executors.json +5 -0
  34. package/managed/raw/git-format-staged.yml +16 -3
  35. package/managed/raw/patches/nx@23.2.1.patch +397 -33
  36. package/package.json +2 -2
  37. package/src/ensure-built-receipt.ts +8 -2
  38. package/src/ensure-built-witness.ts +29 -1
  39. package/src/ensure-built.ts +39 -7
  40. package/src/executors/all-verdicts/executor.test.ts +135 -0
  41. package/src/executors/all-verdicts/executor.ts +164 -0
  42. package/src/executors/all-verdicts/schema.json +22 -0
  43. package/src/executors/all-verdicts/schema.ts +10 -0
  44. package/src/executors/bounded-exec/cargo-progress-host.test.ts +332 -0
  45. package/src/executors/bounded-exec/executor.test.ts +89 -42
  46. package/src/executors/bounded-exec/executor.ts +45 -11
  47. package/src/executors/bounded-exec/progress.test.ts +1 -14
  48. package/src/executors/bounded-exec/progress.ts +2 -5
  49. package/src/executors/bounded-exec/runner-hook.test.ts +193 -0
  50. package/src/executors/bounded-exec/runner-hook.ts +292 -0
  51. package/src/executors/bounded-exec/runner-lifecycle.test.ts +82 -0
  52. package/src/executors/bounded-exec/runner-lifecycle.ts +102 -0
  53. package/src/index.test.ts +7 -5
  54. package/src/index.ts +4 -1
  55. package/src/nx-daemon-start-never.test.ts +137 -0
  56. package/src/producer-receipts.test.ts +99 -0
  57. package/src/producer-receipts.ts +111 -58
  58. package/test-timings.json +101 -96
@@ -0,0 +1,332 @@
1
+ import { describe, expect, it, jest } from 'bun:test';
2
+ import { mkdir, readFile, rm, writeFile } from 'node:fs/promises';
3
+ import { createServer, type Server, type Socket } from 'node:net';
4
+ import { join } from 'node:path';
5
+ import { BOUNDED_TEST_IDLE_TIMEOUT_MS } from '../../bounded-test-policy.js';
6
+ import { ownedSocketDirectory } from '../../testing.js';
7
+ import { createProcessTreeKiller, runBoundedExec } from './executor.js';
8
+ import { VERDICT_FILE } from './verdict.js';
9
+
10
+ const BUILD_BARRIER = String.raw`use std::env;
11
+ use std::io::{Read, Write};
12
+ use std::os::unix::net::UnixStream;
13
+
14
+ fn main() {
15
+ let mut socket = UnixStream::connect(env::var("CARGO_PHASE_SOCKET").expect("build barrier path"))
16
+ .expect("connect build barrier");
17
+ writeln!(socket, "{}", env::var("CARGO_PKG_NAME").expect("package name"))
18
+ .expect("announce held build");
19
+ let mut release = [0; 3];
20
+ socket.read_exact(&mut release).expect("read build release");
21
+ assert_eq!(&release, b"go\n");
22
+ println!("cargo:rerun-if-changed=build.rs");
23
+ }
24
+ `;
25
+
26
+ /** Real Cargo/rustc/test processes; IPC, not compiler speed, determines every clock step. */
27
+ describe('Cargo test progress host proofs', () => {
28
+ it('excludes both held cold builds in a direct cargo test chain from test idle time', async () => {
29
+ const root = await ownedSocketDirectory('cargo-progress-', 'runner.sock');
30
+ const sockets = new Set<Socket>();
31
+ const exchanges: Promise<void>[] = [];
32
+ const exchangeErrors: unknown[] = [];
33
+ const firstFinished = Promise.withResolvers<boolean>();
34
+ const builds: string[] = [];
35
+ const observed: string[] = [];
36
+ const stdout = process.stdout.write;
37
+ const stderr = process.stderr.write;
38
+ let output = '';
39
+ let firstObserved = false;
40
+ const server = createServer((socket) => {
41
+ sockets.add(socket);
42
+ socket.once('close', () => sockets.delete(socket));
43
+ const { promise, resolve, reject } = Promise.withResolvers<void>();
44
+ socket.once('error', reject);
45
+ socket.once('close', () => reject(new Error('Cargo closed its held build before release')));
46
+ let request = '';
47
+ const received = (data: Buffer) => {
48
+ request += data.toString();
49
+ if (!request.includes('\n')) return;
50
+ socket.off('data', received);
51
+ void (async () => {
52
+ expect(request).toBe(builds.length === 0 ? 'first\n' : 'second\n');
53
+ const name = request.trimEnd();
54
+ builds.push(name);
55
+ if (name === 'second') {
56
+ // && requires the first Cargo/hook to exit before this build can connect. Also
57
+ // observe its libtest terminal here before advancing the clock for the next build.
58
+ expect(await firstFinished.promise).toBe(true);
59
+ }
60
+ observed.push(`${name} build`);
61
+ // build.rs cannot finish until this exchange releases it, so neither cold build
62
+ // can accidentally finish before the unchanged production idle bound is crossed.
63
+ jest.advanceTimersByTime(2 * BOUNDED_TEST_IDLE_TIMEOUT_MS);
64
+ socket.end('go\n');
65
+ resolve();
66
+ })().catch(reject);
67
+ };
68
+ socket.on('data', received);
69
+ exchanges.push(
70
+ promise.catch((error: unknown) => {
71
+ exchangeErrors.push(error);
72
+ socket.destroy();
73
+ }),
74
+ );
75
+ });
76
+ try {
77
+ for (const name of ['first', 'second']) {
78
+ await writeCargoPackage(root, name, `#[test]\nfn ${name}_terminal() { assert_eq!(2 + 2, 4); }\n`);
79
+ await writeFile(join(root, name, 'build.rs'), BUILD_BARRIER);
80
+ }
81
+ await listen(server, join(root, 'runner.sock'));
82
+ process.stdout.write = (chunk) => {
83
+ output += chunk.toString();
84
+ if (
85
+ !firstObserved &&
86
+ output.includes('test first_terminal ... ok\n') &&
87
+ output.includes('test result: ok. 1 passed;')
88
+ ) {
89
+ firstObserved = true;
90
+ observed.push('first terminal');
91
+ firstFinished.resolve(true);
92
+ }
93
+ return true;
94
+ };
95
+ process.stderr.write = () => true;
96
+ jest.useFakeTimers();
97
+ const result = await runBoundedExec(
98
+ {
99
+ command:
100
+ 'cargo test --manifest-path first/Cargo.toml --lib && cargo test --manifest-path second/Cargo.toml --lib',
101
+ env: cargoEnvironment(root),
102
+ idleTimeoutMs: BOUNDED_TEST_IDLE_TIMEOUT_MS,
103
+ killAfterMs: 0,
104
+ },
105
+ { root },
106
+ createProcessTreeKiller(),
107
+ null,
108
+ null,
109
+ );
110
+ // The unfixed executor dies at the first gate: no terminal or second gate will arrive.
111
+ // Resolve absent-event waits before joining the exchanges instead of hanging the baseline.
112
+ firstFinished.resolve(false);
113
+ await Promise.all(exchanges);
114
+ expect(result.success, result.terminalOutput).toBe(true);
115
+ expect(exchangeErrors).toEqual([]);
116
+ expect(builds).toEqual(['first', 'second']);
117
+ expect(observed).toEqual(['first build', 'first terminal', 'second build']);
118
+ expect(output).toContain('test second_terminal ... ok\n');
119
+ expect(output.match(/test result: ok\. 1 passed;/g)).toHaveLength(2);
120
+ expect(result.terminalOutput).not.toContain('Command made no progress');
121
+ } finally {
122
+ firstFinished.resolve(false);
123
+ jest.useRealTimers();
124
+ process.stdout.write = stdout;
125
+ process.stderr.write = stderr;
126
+ try {
127
+ await closeFixture(server, sockets, root);
128
+ } finally {
129
+ await Promise.all(exchanges);
130
+ }
131
+ }
132
+ });
133
+
134
+ it('expires real custom-harness teardown despite a passing summary and nested Cargo JSON', async () => {
135
+ expect(BOUNDED_TEST_IDLE_TIMEOUT_MS).toBe(180_000);
136
+ const root = await ownedSocketDirectory('cargo-progress-', 'runner.sock');
137
+ const sockets = new Set<Socket>();
138
+ const socketErrors: unknown[] = [];
139
+ const stdout = process.stdout.write;
140
+ const stderr = process.stderr.write;
141
+ const treeKiller = createProcessTreeKiller();
142
+ const kills: NodeJS.Signals[] = [];
143
+ let output = '';
144
+ let diagnostics = '';
145
+ let advanced = false;
146
+ let released = false;
147
+ const releaseTeardown = () => {
148
+ if (released) return;
149
+ released = true;
150
+ for (const socket of sockets) socket.end('cleanup\n');
151
+ };
152
+ const server = createServer((socket) => {
153
+ sockets.add(socket);
154
+ socket.once('close', () => sockets.delete(socket));
155
+ socket.once('error', (error) => socketErrors.push(error));
156
+ if (released) socket.end('cleanup\n');
157
+ });
158
+ try {
159
+ await writeCargoPackage(
160
+ root,
161
+ 'active',
162
+ 'pub fn completed_body() { assert_eq!(2 + 2, 4); }\n',
163
+ '\n[[test]]\nname = "teardown"\npath = "tests/teardown.rs"\nharness = false\n',
164
+ );
165
+ await mkdir(join(root, 'active', 'tests'));
166
+ await writeFile(
167
+ join(root, 'active', 'tests', 'teardown.rs'),
168
+ String.raw`use std::env;
169
+ use std::io::{Read, Write};
170
+ use std::os::unix::net::UnixStream;
171
+
172
+ struct Teardown {
173
+ gate: UnixStream,
174
+ }
175
+
176
+ impl Drop for Teardown {
177
+ fn drop(&mut self) {
178
+ println!("test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out;");
179
+ println!("{}", r#"{"reason":"build-finished","success":true}"#);
180
+ println!("cargo-teardown-ready");
181
+ std::io::stdout().flush().expect("flush teardown readiness");
182
+ // The body passed, but the real harness process is still alive in teardown. Only the
183
+ // executor's process-group kill should finish an accepted run of this proof.
184
+ let mut release = [0; 8];
185
+ self.gate.read_exact(&mut release).expect("read failed-proof cleanup");
186
+ assert_eq!(&release, b"cleanup\n");
187
+ }
188
+ }
189
+
190
+ fn main() {
191
+ let _teardown = Teardown {
192
+ gate: UnixStream::connect(env::var("CARGO_PHASE_SOCKET").expect("teardown barrier path"))
193
+ .expect("connect teardown barrier"),
194
+ };
195
+ active::completed_body();
196
+ println!("test completed_body ... ok");
197
+ }
198
+ `,
199
+ );
200
+ await listen(server, join(root, 'runner.sock'));
201
+ process.stdout.write = (chunk) => {
202
+ output += chunk.toString();
203
+ if (!advanced && output.includes('cargo-teardown-ready\n')) {
204
+ advanced = true;
205
+ queueMicrotask(() => {
206
+ jest.advanceTimersByTime(BOUNDED_TEST_IDLE_TIMEOUT_MS);
207
+ // If the misleading output disabled the watchdog, let this failed proof return
208
+ // rather than leaving a harness parked forever behind fake timers.
209
+ if (!diagnostics.includes('Command made no progress')) releaseTeardown();
210
+ });
211
+ }
212
+ return true;
213
+ };
214
+ process.stderr.write = (chunk) => {
215
+ diagnostics += chunk.toString();
216
+ return true;
217
+ };
218
+ jest.useFakeTimers();
219
+ const result = await runBoundedExec(
220
+ {
221
+ command: 'cargo test --manifest-path active/Cargo.toml --test teardown',
222
+ env: cargoEnvironment(root),
223
+ idleTimeoutMs: BOUNDED_TEST_IDLE_TIMEOUT_MS,
224
+ killAfterMs: 0,
225
+ },
226
+ { root, record: { task: { id: 'cargo:teardown', hash: null }, directory: join(root, 'record') } },
227
+ {
228
+ kill(pid, signal) {
229
+ kills.push(signal);
230
+ return treeKiller.kill(pid, signal);
231
+ },
232
+ },
233
+ null,
234
+ null,
235
+ );
236
+ expect(advanced, result.terminalOutput).toBe(true);
237
+ expect(output).toContain('test completed_body ... ok\n');
238
+ expect(output).toContain('test result: ok. 1 passed;');
239
+ expect(output).toContain('{"reason":"build-finished","success":true}\n');
240
+ expect(released, 'the production watchdog must terminate teardown, not fixture cleanup').toBe(false);
241
+ expect(result.success).toBe(false);
242
+ expect(result.terminalOutput).toContain(`no test finished for ${BOUNDED_TEST_IDLE_TIMEOUT_MS}ms`);
243
+ expect(kills).toContain('SIGTERM');
244
+ const record: unknown = JSON.parse(await readFile(join(root, 'record', VERDICT_FILE), 'utf8'));
245
+ expect(record).toHaveProperty('verdict.outcome', 'wedged');
246
+ expect(record).toHaveProperty('verdict.idleMs', BOUNDED_TEST_IDLE_TIMEOUT_MS);
247
+ expect(socketErrors).toEqual([]);
248
+ } finally {
249
+ jest.useRealTimers();
250
+ process.stdout.write = stdout;
251
+ process.stderr.write = stderr;
252
+ await closeFixture(server, sockets, root);
253
+ }
254
+ });
255
+
256
+ it('preserves Cargo compile-error diagnostics and exit status 101', async () => {
257
+ const root = await ownedSocketDirectory('cargo-progress-', 'runner.sock');
258
+ const stdout = process.stdout.write;
259
+ const stderr = process.stderr.write;
260
+ try {
261
+ await writeCargoPackage(root, 'broken', 'compile_error!("cargo-phase-compile-error");\n');
262
+ process.stdout.write = () => true;
263
+ process.stderr.write = () => true;
264
+ jest.useFakeTimers();
265
+ const result = await runBoundedExec(
266
+ {
267
+ command: 'cargo test --manifest-path broken/Cargo.toml --lib',
268
+ env: cargoEnvironment(root),
269
+ idleTimeoutMs: BOUNDED_TEST_IDLE_TIMEOUT_MS,
270
+ killAfterMs: 0,
271
+ },
272
+ { root },
273
+ createProcessTreeKiller(),
274
+ null,
275
+ null,
276
+ );
277
+ expect(result.success).toBe(false);
278
+ expect(result.terminalOutput).toContain('cargo-phase-compile-error');
279
+ expect(result.terminalOutput).toContain('Command exited with status 101:');
280
+ expect(result.terminalOutput).not.toContain('Command made no progress');
281
+ } finally {
282
+ jest.useRealTimers();
283
+ process.stdout.write = stdout;
284
+ process.stderr.write = stderr;
285
+ await rm(root, { recursive: true, force: true });
286
+ }
287
+ });
288
+ });
289
+
290
+ function cargoEnvironment(root: string): Record<string, string> {
291
+ return {
292
+ CARGO_BUILD_JOBS: '2',
293
+ RUST_TEST_THREADS: '2',
294
+ CARGO_TERM_COLOR: 'never',
295
+ CARGO_NET_OFFLINE: 'true',
296
+ CARGO_TARGET_DIR: join(root, 'target'),
297
+ CARGO_PHASE_SOCKET: join(root, 'runner.sock'),
298
+ };
299
+ }
300
+
301
+ async function writeCargoPackage(root: string, name: string, source: string, targets = ''): Promise<void> {
302
+ const directory = join(root, name);
303
+ await mkdir(join(directory, 'src'), { recursive: true });
304
+ await writeFile(
305
+ join(directory, 'Cargo.toml'),
306
+ `[package]\nname = "${name}"\nversion = "0.0.0"\nedition = "2021"\n\n[workspace]\n${targets}`,
307
+ );
308
+ await writeFile(join(directory, 'src', 'lib.rs'), source);
309
+ }
310
+
311
+ function listen(server: Server, endpoint: string): Promise<void> {
312
+ const { promise, resolve, reject } = Promise.withResolvers<void>();
313
+ server.once('error', reject);
314
+ server.listen(endpoint, () => {
315
+ server.off('error', reject);
316
+ resolve();
317
+ });
318
+ return promise;
319
+ }
320
+
321
+ async function closeFixture(server: Server, sockets: Set<Socket>, root: string): Promise<void> {
322
+ for (const socket of sockets) socket.destroy();
323
+ try {
324
+ if (server.listening) {
325
+ const { promise, resolve, reject } = Promise.withResolvers<void>();
326
+ server.close((error) => (error ? reject(error) : resolve()));
327
+ await promise;
328
+ }
329
+ } finally {
330
+ await rm(root, { recursive: true, force: true });
331
+ }
332
+ }
@@ -1,7 +1,7 @@
1
1
  import { afterAll, afterEach, beforeAll, describe, expect, it, jest } from 'bun:test';
2
2
  import { spawnSync } from 'node:child_process';
3
3
  import { access, mkdir, mkdtemp, open, readFile, rm, writeFile } from 'node:fs/promises';
4
- import { createServer } from 'node:net';
4
+ import { createServer, type Socket } from 'node:net';
5
5
  import { tmpdir } from 'node:os';
6
6
  import { dirname, join } from 'node:path';
7
7
  import { onProgressClock } from '../../__tests__/progress-clock.js';
@@ -330,54 +330,101 @@ describe('@smoothbricks/nx-plugin:bounded-exec', () => {
330
330
 
331
331
  it('discards the previous snapshot when a test finishes and captures the next idle interval', async () => {
332
332
  const workspace = await createWorkspace();
333
- // Only observed child output advances the fake clock, never elapsed host time.
333
+ const socketRoot = await ownedSocketDirectory('snapshot-progress-', 'control.sock');
334
+ workspaces.push(socketRoot);
335
+ const endpoint = join(socketRoot, 'control.sock');
336
+ const connected = Promise.withResolvers<Socket>();
337
+ const publicationHeld = Promise.withResolvers<void>();
338
+ const sockets = new Set<Socket>();
339
+ const server = createServer((socket) => {
340
+ sockets.add(socket);
341
+ socket.once('close', () => sockets.delete(socket));
342
+ connected.resolve(socket);
343
+ });
334
344
  await workspace.write(
335
345
  'snapshot-progress.js',
336
346
  [
337
- "const { existsSync } = require('node:fs');",
338
- 'let progressed = false;',
339
- 'setInterval(() => {',
340
- " if (!progressed && existsSync('progress')) {",
341
- " console.log(' PASS [ 0.010s] (1/1) fake tests::progress');",
342
- ' progressed = true;',
343
- ' }',
344
- " console.log('snapshot-clock');",
345
- '}, 20);',
347
+ "const { connect } = require('node:net');",
348
+ "const { createInterface } = require('node:readline');",
349
+ `const socket = connect(${JSON.stringify(endpoint)}, () => console.log('snapshot-clock\\nsnapshot-stage:initial'));`,
350
+ "socket.on('error', error => { throw error; });",
351
+ "createInterface({ input: socket }).on('line', command => {",
352
+ " if (command === 'hold') console.log('snapshot-clock\\nsnapshot-stage:held');",
353
+ " else if (command === 'progress') console.log(' PASS [ 0.010s] (1/1) fake tests::progress\\nsnapshot-clock\\nsnapshot-stage:progress');",
354
+ " else if (command === 'expire') console.log('snapshot-clock\\nsnapshot-stage:captured');",
355
+ " else throw new Error('unexpected snapshot command: ' + command);",
356
+ '});',
346
357
  '',
347
358
  ].join('\n'),
348
359
  );
360
+ await new Promise<void>((resolve, reject) => {
361
+ server.once('error', reject);
362
+ server.listen(endpoint, resolve);
363
+ });
349
364
  const signals: Array<AbortSignal | undefined> = [];
350
- const { result } = await onProgressClock(
351
- (chunk) => chunk.includes('snapshot-clock\n'),
352
- 5_000,
353
- () =>
354
- runBoundedExec(
355
- { command: 'node snapshot-progress.js', idleTimeoutMs: 10_000, killAfterMs: 0 },
356
- workspace.context,
357
- createProcessTreeKiller(),
358
- null,
359
- null,
360
- null,
361
- async (request) => {
362
- signals.push(request.signal);
363
- const ordinal = signals.length;
364
- if (ordinal === 1) await workspace.write('progress', 'go');
365
- const capturedAtMs = Date.now();
366
- return {
367
- startedAtMs: capturedAtMs,
368
- capturedAtMs,
369
- offsetFromExpiryMs: request.expiresAtMs - capturedAtMs,
370
- outcome: 'holders',
371
- text: `capture-interval-${ordinal}\n`,
372
- };
373
- },
374
- ),
375
- );
376
- expect(result.success).toBe(false);
377
- expect(signals).toHaveLength(2);
378
- expect(signals.every((signal) => signal?.aborted)).toBe(true);
379
- expect(result.terminalOutput).not.toContain('capture-interval-1');
380
- expect(result.terminalOutput).toContain('capture-interval-2');
365
+ let partial = '';
366
+ try {
367
+ const { result, movedMs } = await onProgressClock(
368
+ (chunk) => {
369
+ const lines = (partial + chunk).split('\n');
370
+ partial = lines.pop() ?? '';
371
+ let move = false;
372
+ for (const line of lines) {
373
+ if (line === 'snapshot-stage:held') publicationHeld.resolve();
374
+ if (
375
+ line === 'snapshot-stage:initial' ||
376
+ line === 'snapshot-stage:progress' ||
377
+ line === 'snapshot-stage:captured'
378
+ )
379
+ move = true;
380
+ }
381
+ return move;
382
+ },
383
+ 5_000,
384
+ () =>
385
+ runBoundedExec(
386
+ { command: 'node snapshot-progress.js', idleTimeoutMs: 10_000, killAfterMs: 0 },
387
+ workspace.context,
388
+ createProcessTreeKiller(),
389
+ null,
390
+ null,
391
+ null,
392
+ async (request) => {
393
+ signals.push(request.signal);
394
+ const ordinal = signals.length;
395
+ const socket = await connected.promise;
396
+ if (ordinal === 1) {
397
+ // Deliberately deliver another heartbeat while publication is held. It cannot
398
+ // spend the next interval: only the observed PASS releases that clock step.
399
+ socket.write('hold\n');
400
+ await publicationHeld.promise;
401
+ socket.write('progress\n');
402
+ } else {
403
+ socket.write('expire\n');
404
+ }
405
+ const capturedAtMs = Date.now();
406
+ return {
407
+ startedAtMs: capturedAtMs,
408
+ capturedAtMs,
409
+ offsetFromExpiryMs: request.expiresAtMs - capturedAtMs,
410
+ outcome: 'holders',
411
+ text: `capture-interval-${ordinal}\n`,
412
+ };
413
+ },
414
+ ),
415
+ );
416
+ expect(result.success).toBe(false);
417
+ expect(signals).toHaveLength(2);
418
+ expect(signals.every((signal) => signal?.aborted)).toBe(true);
419
+ expect(result.terminalOutput).not.toContain('capture-interval-1');
420
+ expect(result.terminalOutput).toContain('capture-interval-2');
421
+ expect(result.terminalOutput).toContain('snapshot-stage:held');
422
+ expect(movedMs).toBe(15_000);
423
+ } finally {
424
+ publicationHeld.resolve();
425
+ for (const socket of sockets) socket.destroy();
426
+ await new Promise<void>((resolve, reject) => server.close((error) => (error ? reject(error) : resolve())));
427
+ }
381
428
  });
382
429
 
383
430
  it('kills a command whose only output is a heartbeat', async () => {
@@ -2,6 +2,8 @@ import { type ChildProcess, spawn } from 'node:child_process';
2
2
  import { accessSync, constants } from 'node:fs';
3
3
  import { availableParallelism, userInfo } from 'node:os';
4
4
  import { delimiter, isAbsolute, join, resolve as resolvePath } from 'node:path';
5
+ import { createInterface } from 'node:readline';
6
+ import { Duplex } from 'node:stream';
5
7
  import { workspaceDataDirectoryForWorkspace } from 'nx/src/utils/cache-directory.js';
6
8
 
7
9
  import {
@@ -34,6 +36,7 @@ import {
34
36
  RamTempVolume,
35
37
  ramTempPaths,
36
38
  } from './ram-temp.js';
39
+ import { createRunnerLifecycle, withRunnerLifecycle } from './runner-lifecycle.js';
37
40
  import type { BoundedExecOptions } from './schema.js';
38
41
  import {
39
42
  type BoundedExecRecord,
@@ -458,13 +461,18 @@ export async function runBoundedExec(
458
461
  ...sized.env,
459
462
  ...(pool?.environment ?? {}),
460
463
  };
464
+ const hooked = process.platform === 'win32' ? null : withRunnerLifecycle(command);
465
+ const lifecycle = hooked === null ? null : createRunnerLifecycle(hooked.runners);
466
+ let lifecycleFailure: string | null = null;
467
+ const isTesting = (): boolean => lifecycleFailure !== null || (lifecycle?.isTesting() ?? true);
461
468
  const startedAt = Date.now();
462
469
  let child: ChildProcess;
463
470
  try {
464
- child = spawn(command, [], {
471
+ child = spawn(hooked?.command ?? command, [], {
465
472
  cwd,
466
473
  env: mergeEnv(lease, env),
467
474
  shell: true,
475
+ stdio: hooked === null ? 'pipe' : ['pipe', 'pipe', 'pipe', 'pipe'],
468
476
  detached: process.platform !== 'win32',
469
477
  windowsHide: true,
470
478
  });
@@ -542,7 +550,7 @@ export async function runBoundedExec(
542
550
  idleTimer = setTimeout(() => expire({ kind: 'idle', limitMs, tests: progress.inFlight() }, queuedMs), leftMs);
543
551
  };
544
552
  const armIdleTimer = (): void => {
545
- if (idleTimeoutMs === undefined || state.settled || state.expiry !== null || !progress.isTesting()) {
553
+ if (idleTimeoutMs === undefined || state.settled || state.expiry !== null || !isTesting()) {
546
554
  return;
547
555
  }
548
556
  idleLeftMs = idleTimeoutMs;
@@ -577,7 +585,7 @@ export async function runBoundedExec(
577
585
  const waitedMs = Date.now() - queuedSinceMs;
578
586
  queuedMs += waitedMs;
579
587
  queuedSinceMs = null;
580
- if (!progress.isTesting()) {
588
+ if (!isTesting()) {
581
589
  appendStderr(`bounded-exec: host CPU wait ended after ${waitedMs}ms; no test runner is active\n`);
582
590
  return;
583
591
  }
@@ -591,10 +599,8 @@ export async function runBoundedExec(
591
599
  // Only the CHILD's own output is read for progress. The diagnostics below go
592
600
  // through appendStderr as well, and re-arming on those would let a teardown
593
601
  // message extend the very bound that just fired.
594
- const scanProgress = (stream: 'stdout' | 'stderr', text: string): void => {
595
- const wasTesting = progress.isTesting();
596
- const finished = progress.scan(stream, text);
597
- if (!progress.isTesting()) {
602
+ const updateIdle = (finished: boolean, wasTesting: boolean): void => {
603
+ if (!isTesting()) {
598
604
  clearTimeout(idleTimer);
599
605
  cancelCapture();
600
606
  idleCapture = undefined;
@@ -602,6 +608,34 @@ export async function runBoundedExec(
602
608
  armIdleTimer();
603
609
  }
604
610
  };
611
+ const scanProgress = (stream: 'stdout' | 'stderr', text: string): void => {
612
+ updateIdle(progress.scan(stream, text), isTesting());
613
+ };
614
+ if (lifecycle !== null) {
615
+ const channel = child.stdio[3];
616
+ if (!(channel instanceof Duplex)) throw new Error('runner lifecycle requires a duplex child descriptor');
617
+ const failLifecycle = (detail: string, wasTesting = isTesting()): void => {
618
+ if (lifecycleFailure !== null || state.expiry !== null) return;
619
+ lifecycleFailure = detail;
620
+ appendStderr(`bounded-exec: runner lifecycle failed: ${detail}\n`);
621
+ updateIdle(false, wasTesting);
622
+ };
623
+ createInterface({ input: channel }).on('line', (line) => {
624
+ const wasTesting = isTesting();
625
+ const event = lifecycle.accept(line);
626
+ if (event === null) {
627
+ failLifecycle(lifecycle.error() ?? 'invalid lifecycle event', wasTesting);
628
+ return;
629
+ }
630
+ updateIdle(false, wasTesting);
631
+ // Tool startup and native lifecycle-bearing stdout wait for their phase acknowledgement.
632
+ channel.write(`${JSON.stringify(event)}\n`);
633
+ });
634
+ channel.on('error', (error) => failLifecycle(error.message));
635
+ channel.on('end', () => {
636
+ if (!lifecycle.finished()) failLifecycle('supervisor channel ended without an owned terminal event');
637
+ });
638
+ }
605
639
  child.stdout?.on('data', (chunk: Buffer | string) => {
606
640
  const text = chunk.toString();
607
641
  scanProgress('stdout', text);
@@ -661,7 +695,7 @@ export async function runBoundedExec(
661
695
  state.settled = true;
662
696
  resolve({ code: 1, signal: null });
663
697
  });
664
- child.once('exit', (code, signal) => {
698
+ child.once(hooked === null ? 'exit' : 'close', (code: number | null, signal: NodeJS.Signals | null) => {
665
699
  state.settled = true;
666
700
  resolve({ code, signal });
667
701
  });
@@ -806,9 +840,9 @@ export async function runBoundedExec(
806
840
  }
807
841
  await releaseLease();
808
842
 
809
- // A run whose sub-pool did not close cleanly fails even when the command exited 0, and whatever bound also fired:
810
- // its verdict is an ordinary failure, never a bound.
811
- const operationalFailure = state.poolCloseError !== null;
843
+ // Broken resource ownership or a lost native lifecycle fails even if a shell branch later exited 0:
844
+ // its verdict is an ordinary operational failure, never a successful or silently unbounded run.
845
+ const operationalFailure = state.poolCloseError !== null || lifecycleFailure !== null;
812
846
  await recordRun(code, state.expiry, Date.now() - startedAt, operationalFailure, seen);
813
847
 
814
848
  return {
@@ -25,26 +25,20 @@ describe('run progress', () => {
25
25
  expect(progress.inFlight()).toEqual(['example/second TestHang']);
26
26
  });
27
27
 
28
- it('counts only active Go package intervals, including startup and parallel packages', () => {
28
+ it('tracks Go package startup and parallel packages until their terminal reports', () => {
29
29
  const progress = createRunProgress('go test ./...');
30
- expect(progress.isTesting()).toBe(false);
31
30
  progress.scan('stdout', '{"Action":"start","Package":"first"}\n{"Action":"start","Package":"second"}\n');
32
- expect(progress.isTesting()).toBe(true);
33
31
  expect(progress.inFlight()).toEqual(['first', 'second']);
34
32
  progress.scan('stdout', '{"Action":"pass","Package":"first"}\n');
35
- expect(progress.isTesting()).toBe(true);
36
33
  expect(progress.inFlight()).toEqual(['second']);
37
34
  progress.scan('stdout', '{"Action":"fail","Package":"second"}\n');
38
- expect(progress.isTesting()).toBe(false);
39
35
  expect(progress.inFlight()).toEqual([]);
40
36
  });
41
37
 
42
38
  it('buffers split Go events and does not mistake test output for lifecycle events', () => {
43
39
  const progress = createRunProgress('go test ./...');
44
40
  expect(progress.scan('stdout', '{"Action":"sta')).toBe(false);
45
- expect(progress.isTesting()).toBe(false);
46
41
  expect(progress.scan('stdout', 'rt","Package":"example"}\n')).toBe(false);
47
- expect(progress.isTesting()).toBe(true);
48
42
  expect(progress.scan('stdout', '{"Action":"run","Package":"example","Test":"TestHang"}\n')).toBe(false);
49
43
  expect(progress.scan('stdout', '{"Action":"pause","Package":"example","Test":"TestHang"}\n')).toBe(false);
50
44
  expect(progress.inFlight()).toEqual(['example TestHang']);
@@ -57,13 +51,6 @@ describe('run progress', () => {
57
51
  expect(progress.scan('stdout', '{"Action":"skip","Package":"example","Test":"TestHang"}\n')).toBe(true);
58
52
  expect(progress.inFlight()).toEqual(['example']);
59
53
  expect(progress.scan('stdout', '{"Action":"pass","Package":"example"}\n')).toBe(true);
60
- expect(progress.isTesting()).toBe(false);
61
- });
62
-
63
- it('keeps the existing watchdog for commands without a complete Go lifecycle contract', () => {
64
- for (const command of ['bun test src', 'cargo nextest run', 'cargo test', 'go test ./... && bun test src']) {
65
- expect(createRunProgress(command).isTesting()).toBe(true);
66
- }
67
54
  });
68
55
 
69
56
  it('counts a nextest status line with its run counter as a finished test, and a SLOW line as none', () => {