@smoothbricks/nx-plugin 0.3.11 → 0.3.12

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 (38) hide show
  1. package/README.md +23 -2
  2. package/dist/bounded-test-policy.d.ts +26 -0
  3. package/dist/bounded-test-policy.d.ts.map +1 -1
  4. package/dist/bounded-test-policy.js +194 -0
  5. package/dist/cargo-workspace.d.ts +67 -0
  6. package/dist/cargo-workspace.d.ts.map +1 -0
  7. package/dist/cargo-workspace.js +215 -0
  8. package/dist/cross-check-policy.d.ts +60 -1
  9. package/dist/cross-check-policy.d.ts.map +1 -1
  10. package/dist/cross-check-policy.js +65 -1
  11. package/dist/executors/bounded-exec/executor.d.ts.map +1 -1
  12. package/dist/executors/bounded-exec/executor.js +86 -51
  13. package/dist/executors/bounded-exec/schema.d.ts +13 -0
  14. package/dist/executors/bounded-exec/schema.d.ts.map +1 -1
  15. package/dist/index.d.ts +2 -0
  16. package/dist/index.d.ts.map +1 -1
  17. package/dist/index.js +344 -57
  18. package/dist/package-target-policy.d.ts +2 -1
  19. package/dist/package-target-policy.d.ts.map +1 -1
  20. package/dist/package-target-policy.js +54 -13
  21. package/dist/workspace-config-policy.js +32 -35
  22. package/nextest.toml +42 -0
  23. package/package.json +2 -1
  24. package/src/bounded-test-policy.test.ts +304 -0
  25. package/src/bounded-test-policy.ts +218 -0
  26. package/src/cargo-workspace.ts +243 -0
  27. package/src/cross-check-policy.test.ts +24 -0
  28. package/src/cross-check-policy.ts +80 -1
  29. package/src/executors/bounded-exec/executor.test.ts +147 -3
  30. package/src/executors/bounded-exec/executor.ts +103 -53
  31. package/src/executors/bounded-exec/schema.json +7 -2
  32. package/src/executors/bounded-exec/schema.ts +13 -0
  33. package/src/index.test.ts +373 -25
  34. package/src/index.ts +390 -65
  35. package/src/package-target-policy.test.ts +50 -0
  36. package/src/package-target-policy.ts +58 -9
  37. package/src/workspace-config-policy.test.ts +15 -30
  38. package/src/workspace-config-policy.ts +32 -39
@@ -92,6 +92,106 @@ describe('@smoothbricks/nx-plugin:bounded-exec', () => {
92
92
  expect(result.terminalOutput).toContain(`cwd=${workspace.root}`);
93
93
  });
94
94
 
95
+ it('kills a silent command on the progress bound while the absolute ceiling is far away', async () => {
96
+ const workspace = await createWorkspace();
97
+
98
+ const result = await runBoundedExec(
99
+ { command: 'node -e "setTimeout(() => {}, 30000)"', timeoutMs: 30_000, idleTimeoutMs: 100, killAfterMs: 0 },
100
+ workspace.context,
101
+ createProcessTreeKiller(),
102
+ );
103
+
104
+ expect(result.success).toBe(false);
105
+ expect(result.terminalOutput).toContain('Command made no progress: no output for 100ms');
106
+ expect(result.terminalOutput).toContain('idleTimeoutMs=100');
107
+ // The bound that fired is named, so a wedge is never reported as a slow run.
108
+ expect(result.terminalOutput).not.toContain('Command timed out after');
109
+ });
110
+
111
+ it('lets a slow but talking command outlive its progress bound many times over', async () => {
112
+ const workspace = await createWorkspace();
113
+ // Six 120ms gaps under a 400ms progress bound: total runtime is well past
114
+ // idleTimeoutMs, so passing proves the bound measures silence, not duration.
115
+ await workspace.write(
116
+ 'chatty.js',
117
+ [
118
+ 'let ticks = 0;',
119
+ 'const timer = setInterval(() => {',
120
+ ' console.log(`tick ${++ticks}`);',
121
+ ' if (ticks === 6) {',
122
+ ' clearInterval(timer);',
123
+ ' }',
124
+ '}, 120);',
125
+ '',
126
+ ].join('\n'),
127
+ );
128
+
129
+ const result = await runBoundedExec(
130
+ { command: 'node chatty.js', timeoutMs: 30_000, idleTimeoutMs: 400 },
131
+ workspace.context,
132
+ createProcessTreeKiller(),
133
+ );
134
+
135
+ expect(result.success).toBe(true);
136
+ expect(result.terminalOutput).toContain('tick 6');
137
+ expect(result.terminalOutput).not.toContain('made no progress');
138
+ });
139
+
140
+ it('treats output on stderr alone as progress', async () => {
141
+ const workspace = await createWorkspace();
142
+ await workspace.write(
143
+ 'chatty-stderr.js',
144
+ [
145
+ 'let ticks = 0;',
146
+ 'const timer = setInterval(() => {',
147
+ ' console.error(`tick ${++ticks}`);',
148
+ ' if (ticks === 5) {',
149
+ ' clearInterval(timer);',
150
+ ' }',
151
+ '}, 120);',
152
+ '',
153
+ ].join('\n'),
154
+ );
155
+
156
+ const result = await runBoundedExec(
157
+ { command: 'node chatty-stderr.js', timeoutMs: 30_000, idleTimeoutMs: 400 },
158
+ workspace.context,
159
+ createProcessTreeKiller(),
160
+ );
161
+
162
+ expect(result.success).toBe(true);
163
+ expect(result.terminalOutput).toContain('tick 5');
164
+ });
165
+
166
+ it('applies the absolute ceiling to a command that keeps talking forever', async () => {
167
+ const workspace = await createWorkspace();
168
+ await workspace.write('runaway.js', ["setInterval(() => console.log('still here'), 10);", ''].join('\n'));
169
+
170
+ const result = await runBoundedExec(
171
+ { command: 'node runaway.js', timeoutMs: 300, idleTimeoutMs: 30_000, killAfterMs: 0 },
172
+ workspace.context,
173
+ createProcessTreeKiller(),
174
+ );
175
+
176
+ expect(result.success).toBe(false);
177
+ expect(result.terminalOutput).toContain('Command timed out after');
178
+ expect(result.terminalOutput).toContain('timeoutMs=300');
179
+ expect(result.terminalOutput).not.toContain('made no progress');
180
+ });
181
+
182
+ it('imposes no progress bound when idleTimeoutMs is omitted', async () => {
183
+ const workspace = await createWorkspace();
184
+
185
+ const result = await runBoundedExec(
186
+ { command: 'node -e "setTimeout(() => console.log(\'late\'), 400)"', timeoutMs: 30_000 },
187
+ workspace.context,
188
+ createProcessTreeKiller(),
189
+ );
190
+
191
+ expect(result.success).toBe(true);
192
+ expect(result.terminalOutput).toContain('late');
193
+ });
194
+
95
195
  it('uses graceful timeout termination before force-killing on POSIX', async () => {
96
196
  if (process.platform === 'win32') {
97
197
  return;
@@ -135,11 +235,19 @@ describe('@smoothbricks/nx-plugin:bounded-exec', () => {
135
235
  },
136
236
  };
137
237
 
238
+ // `trap "" TERM` is a shell builtin, so the ignore disposition is in place
239
+ // within a few ms of `sh` starting and is inherited by `sleep`. The previous
240
+ // `node -e "process.on('SIGTERM', ...)"` needed p50 27.8ms just to reach
241
+ // handler-installed, against a 50ms timeout — 1.8x margin on an idle
242
+ // 18-core host, and none at all on a loaded or 3-core runner. When SIGTERM
243
+ // arrives first the child dies on the default disposition, the run settles,
244
+ // and the SIGKILL branch never executes, so `calls` is ['SIGTERM'] and the
245
+ // assertion below fails for a reason that has nothing to do with escalation.
138
246
  const result = await runBoundedExec(
139
247
  {
140
- command: 'node -e "process.on(\'SIGTERM\', () => {}); setTimeout(() => {}, 5000)"',
141
- timeoutMs: 50,
142
- killAfterMs: 10,
248
+ command: 'sh -c \'trap "" TERM; sleep 5\'',
249
+ timeoutMs: 250,
250
+ killAfterMs: 50,
143
251
  },
144
252
  workspace.context,
145
253
  killer,
@@ -149,6 +257,42 @@ describe('@smoothbricks/nx-plugin:bounded-exec', () => {
149
257
  expect(calls).toEqual(['SIGTERM', 'SIGKILL']);
150
258
  });
151
259
 
260
+ it('returns after force-kill even when the process tree ignores SIGKILL', async () => {
261
+ // Real clock: this is the platform kill/reap path. Fake timers cannot
262
+ // drive child_process exit or SIGKILL delivery.
263
+ if (process.platform === 'win32') {
264
+ return;
265
+ }
266
+ const workspace = await createWorkspace();
267
+ let leaked: number | undefined;
268
+ const killer: ProcessTreeKiller = {
269
+ async kill(pid) {
270
+ leaked = pid;
271
+ },
272
+ };
273
+ const started = Date.now();
274
+ const result = await runBoundedExec(
275
+ { command: 'node -e "setTimeout(() => {}, 30000)"', timeoutMs: 50, killAfterMs: 10 },
276
+ workspace.context,
277
+ killer,
278
+ );
279
+ expect(Date.now() - started).toBeLessThan(5_000);
280
+ expect(result.success).toBe(false);
281
+ expect(result.terminalOutput).toContain('Force-killing timed out command');
282
+ if (leaked !== undefined) {
283
+ try {
284
+ process.kill(-leaked, 'SIGKILL');
285
+ } catch {
286
+ // Already gone.
287
+ }
288
+ try {
289
+ process.kill(leaked, 'SIGKILL');
290
+ } catch {
291
+ // Already gone.
292
+ }
293
+ }
294
+ });
295
+
152
296
  it('forwards args by default and can suppress unparsed args', async () => {
153
297
  const workspace = await createWorkspace();
154
298
 
@@ -4,6 +4,7 @@ import { isAbsolute, join } from 'node:path';
4
4
  import type { BoundedExecOptions } from './schema.js';
5
5
 
6
6
  const DEFAULT_KILL_AFTER_MS = 10_000;
7
+ const REAP_AFTER_FORCE_KILL_MS = 2_000;
7
8
  const EXIT_CODE_BY_SIGNAL: Partial<Record<NodeJS.Signals, number>> = {
8
9
  SIGHUP: 129,
9
10
  SIGINT: 130,
@@ -19,9 +20,22 @@ export interface BoundedExecResult {
19
20
  terminalOutput: string;
20
21
  }
21
22
 
23
+ /**
24
+ * Which bound ended the run.
25
+ *
26
+ * `total` is a wall-clock ceiling: it answers "is this run unbounded?".
27
+ * `idle` is a no-progress bound: it answers "is this run wedged?".
28
+ *
29
+ * These are different questions and one number cannot answer both. A ceiling
30
+ * tight enough to catch a wedge promptly is also tight enough to fail a correct
31
+ * run on a busy machine; a ceiling loose enough never to do that cannot catch a
32
+ * wedge promptly. Keeping the two bounds separate is what makes each honest.
33
+ */
34
+ type ExpiredBound = { kind: 'total' | 'idle'; limitMs: number };
35
+
22
36
  interface RunState {
23
37
  settled: boolean;
24
- timedOut: boolean;
38
+ expiry: ExpiredBound | null;
25
39
  forceKillNeeded: boolean;
26
40
  }
27
41
 
@@ -44,10 +58,11 @@ export async function runBoundedExec(
44
58
  const cwd = resolveCwd(options.cwd, context.root);
45
59
  const command = buildCommand(options);
46
60
  const timeoutMs = options.timeoutMs;
61
+ const idleTimeoutMs = options.idleTimeoutMs;
47
62
  const killAfterMs = options.killAfterMs ?? DEFAULT_KILL_AFTER_MS;
48
63
  const startedAt = Date.now();
49
64
  const outputChunks: string[] = [];
50
- const state: RunState = { settled: false, timedOut: false, forceKillNeeded: false };
65
+ const state: RunState = { settled: false, expiry: null, forceKillNeeded: false };
51
66
 
52
67
  const child = spawn(command, [], {
53
68
  cwd,
@@ -68,8 +83,26 @@ export async function runBoundedExec(
68
83
  process.stderr.write(text);
69
84
  };
70
85
 
71
- child.stdout?.on('data', appendStdout);
72
- child.stderr?.on('data', appendStderr);
86
+ let idleTimer: NodeJS.Timeout | undefined;
87
+ const armIdleTimer = (): void => {
88
+ if (idleTimeoutMs === undefined || state.settled || state.expiry !== null) {
89
+ return;
90
+ }
91
+ clearTimeout(idleTimer);
92
+ idleTimer = setTimeout(() => expire({ kind: 'idle', limitMs: idleTimeoutMs }), idleTimeoutMs);
93
+ };
94
+
95
+ // Only the CHILD's own output counts as progress. The diagnostics below go
96
+ // through appendStderr as well, and re-arming on those would let a teardown
97
+ // message extend the very bound that just fired.
98
+ child.stdout?.on('data', (chunk: Buffer | string) => {
99
+ armIdleTimer();
100
+ appendStdout(chunk);
101
+ });
102
+ child.stderr?.on('data', (chunk: Buffer | string) => {
103
+ armIdleTimer();
104
+ appendStderr(chunk);
105
+ });
73
106
 
74
107
  const killChildTree = async (force: boolean): Promise<void> => {
75
108
  const pid = child.pid;
@@ -101,61 +134,75 @@ export async function runBoundedExec(
101
134
  process.on('SIGTERM', onSigterm);
102
135
  process.on('SIGHUP', onSighup);
103
136
 
104
- let timeout: NodeJS.Timeout | undefined;
105
- const timeoutPromise = new Promise<void>((resolve) => {
106
- timeout = setTimeout(() => {
107
- state.timedOut = true;
108
- const elapsedMs = Date.now() - startedAt;
109
- appendStderr(`\nCommand timed out after ${elapsedMs}ms (timeoutMs=${timeoutMs}, cwd=${cwd}): ${command}\n`);
110
- void (async () => {
111
- await ignoreKillError(killChildTree(false));
112
- if (!state.settled && killAfterMs > 0) {
113
- await delay(killAfterMs);
114
- }
115
- if (!state.settled) {
116
- state.forceKillNeeded = true;
117
- appendStderr(`Force-killing timed out command after killAfterMs=${killAfterMs}: ${command}\n`);
118
- await ignoreKillError(killChildTree(true));
119
- }
120
- resolve();
121
- })();
122
- }, timeoutMs);
123
- });
124
-
137
+ // `Promise.withResolvers` would read better here but needs lib es2024; this
138
+ // package inherits lib es2022 from tsconfig.base.json.
139
+ let resolveExit!: (value: { code: number | null; signal: NodeJS.Signals | null }) => void;
125
140
  const exitPromise = new Promise<{ code: number | null; signal: NodeJS.Signals | null }>((resolve) => {
141
+ resolveExit = resolve;
126
142
  child.once('error', (error) => {
127
143
  appendStderr(`${error.message}\n`);
144
+ state.settled = true;
128
145
  resolve({ code: 1, signal: null });
129
146
  });
130
147
  child.once('exit', (code, signal) => {
148
+ state.settled = true;
131
149
  resolve({ code, signal });
132
150
  });
133
151
  });
134
152
 
135
- const exit = await exitPromise;
136
- const exitedGracefullyAfterTimeout = state.timedOut && exit.code === 0 && exit.signal === null;
137
- state.settled = !state.timedOut || exitedGracefullyAfterTimeout;
138
- if (timeout) {
139
- clearTimeout(timeout);
140
- }
141
- removeSignalHandlers();
153
+ // Both bounds escalate through one path, so a run expires at most once and
154
+ // the report always names which bound did it. A bare "timed out" would leave
155
+ // the reader unable to tell a wedged toolchain from a loaded machine — the
156
+ // two have opposite fixes.
157
+ const expire = (bound: ExpiredBound): void => {
158
+ if (state.settled || state.expiry !== null) {
159
+ return;
160
+ }
161
+ state.expiry = bound;
162
+ clearTimeout(totalTimer);
163
+ clearTimeout(idleTimer);
164
+ const elapsedMs = Date.now() - startedAt;
165
+ appendStderr(
166
+ bound.kind === 'idle'
167
+ ? `\nCommand made no progress: no output for ${bound.limitMs}ms (idleTimeoutMs=${bound.limitMs}) after ${elapsedMs}ms of runtime (cwd=${cwd}): ${command}\n`
168
+ : `\nCommand timed out after ${elapsedMs}ms (timeoutMs=${bound.limitMs}, cwd=${cwd}): ${command}\n`,
169
+ );
170
+ void (async () => {
171
+ await ignoreKillError(killChildTree(false));
172
+ if (!state.settled && killAfterMs > 0) {
173
+ await delay(killAfterMs);
174
+ }
175
+ if (!state.settled) {
176
+ state.forceKillNeeded = true;
177
+ appendStderr(`Force-killing timed out command after killAfterMs=${killAfterMs}: ${command}\n`);
178
+ await ignoreKillError(killChildTree(true));
179
+ await delay(REAP_AFTER_FORCE_KILL_MS);
180
+ }
181
+ if (!state.settled) {
182
+ resolveExit({ code: 1, signal: 'SIGKILL' });
183
+ }
184
+ })();
185
+ };
142
186
 
143
- if (state.timedOut) {
144
- await timeoutPromise;
145
- state.settled = true;
146
- }
187
+ const totalTimer = setTimeout(() => expire({ kind: 'total', limitMs: timeoutMs }), timeoutMs);
188
+ armIdleTimer();
189
+
190
+ const { code: exitCode, signal: exitSignal } = await exitPromise;
191
+ clearTimeout(totalTimer);
192
+ clearTimeout(idleTimer);
193
+ removeSignalHandlers();
147
194
 
148
- const code = exit.code ?? signalToExitCode(exit.signal);
149
- if (code !== 0 && !state.timedOut) {
195
+ const code = exitCode ?? signalToExitCode(exitSignal);
196
+ if (code !== 0 && state.expiry === null) {
150
197
  appendStderr(`Command exited with status ${code}: ${command}\n`);
151
198
  }
152
199
 
153
- if (state.timedOut && !state.forceKillNeeded) {
200
+ if (state.expiry !== null && !state.forceKillNeeded) {
154
201
  appendStderr(`Timed out command exited after graceful termination: ${command}\n`);
155
202
  }
156
203
 
157
204
  return {
158
- success: !state.timedOut && code === 0,
205
+ success: state.expiry === null && code === 0,
159
206
  terminalOutput: outputChunks.join(''),
160
207
  };
161
208
  }
@@ -169,19 +216,22 @@ export function createProcessTreeKiller(): ProcessTreeKiller {
169
216
  }
170
217
 
171
218
  function killProcessGroup(pid: number, signal: NodeJS.Signals): Promise<void> {
172
- return new Promise((resolve) => {
173
- try {
174
- // Send signal to the entire process group (negative PID).
175
- // The executor spawns with detached: true on POSIX, which creates a
176
- // dedicated process group. Signaling -pid is atomic and catches all
177
- // descendants, including grandchildren that tree-walk libraries miss
178
- // when parents die and children get reparented to init.
179
- process.kill(-pid, signal);
180
- } catch {
181
- // ESRCH: process group already exited between timeout and kill.
182
- }
183
- resolve();
184
- });
219
+ try {
220
+ // Send signal to the entire process group (negative PID).
221
+ // The executor spawns with detached: true on POSIX, which creates a
222
+ // dedicated process group. Signaling -pid is atomic and catches all
223
+ // descendants, including grandchildren that tree-walk libraries miss
224
+ // when parents die and children get reparented to init.
225
+ process.kill(-pid, signal);
226
+ } catch {
227
+ // ESRCH: process group already exited, or this pid is not a group leader.
228
+ }
229
+ try {
230
+ process.kill(pid, signal);
231
+ } catch {
232
+ // Already reaped.
233
+ }
234
+ return Promise.resolve();
185
235
  }
186
236
 
187
237
  function resolveCwd(cwd: string | undefined, root: string): string {
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 2,
3
3
  "title": "Bounded Exec",
4
- "description": "Run one shell command with a hard timeout and process-tree cleanup.",
4
+ "description": "Run one shell command under an absolute ceiling and a no-progress bound, with process-tree cleanup.",
5
5
  "type": "object",
6
6
  "cli": "nx",
7
7
  "outputCapture": "pipe",
@@ -26,7 +26,12 @@
26
26
  "timeoutMs": {
27
27
  "type": "number",
28
28
  "minimum": 1,
29
- "description": "Hard wall-clock timeout in milliseconds."
29
+ "description": "Absolute wall-clock ceiling in milliseconds. A backstop against an unbounded run, not a performance budget."
30
+ },
31
+ "idleTimeoutMs": {
32
+ "type": "number",
33
+ "minimum": 1,
34
+ "description": "Kill the process tree after this many milliseconds with no stdout or stderr. Detects a wedged process independently of machine speed and load. Omit for no progress bound."
30
35
  },
31
36
  "killAfterMs": {
32
37
  "type": "number",
@@ -3,6 +3,19 @@ export interface BoundedExecOptions {
3
3
  cwd?: string;
4
4
  env?: Record<string, string>;
5
5
  timeoutMs: number;
6
+ /**
7
+ * Kill the process tree when it has produced no stdout or stderr for this
8
+ * long. Absent means no progress bound.
9
+ *
10
+ * This is the bound that catches a hang. A wedged process — a flock deadlock
11
+ * between two cargo writers, a transformer waiting on a lock nobody will
12
+ * release, a runner that ignores its own per-test timeout — emits nothing at
13
+ * all, so silence is the signal. Unlike `timeoutMs` it does not scale with
14
+ * machine speed or load: a machine ten times slower still prints progress,
15
+ * just further apart, and each gap is bounded by the slowest single unit of
16
+ * work rather than by the total.
17
+ */
18
+ idleTimeoutMs?: number;
6
19
  killAfterMs?: number;
7
20
  forwardAllArgs?: boolean;
8
21
  args?: string | string[];