@smoothbricks/nx-plugin 0.3.11 → 0.3.13

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 +10 -5
  16. package/dist/index.d.ts.map +1 -1
  17. package/dist/index.js +455 -95
  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 +79 -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 +531 -99
  34. package/src/index.ts +527 -104
  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
@@ -0,0 +1,243 @@
1
+ import { existsSync, readFileSync } from 'node:fs';
2
+ import { readFile } from 'node:fs/promises';
3
+ import { join, normalize, posix, relative, sep } from 'node:path';
4
+ import { parse as parseToml } from 'smol-toml';
5
+
6
+ export interface CargoWorkspacePackage {
7
+ name: string;
8
+ dir: string;
9
+ /**
10
+ * How many bounded Nx targets this crate's tests are split across. See
11
+ * `readTestShards` for why this is declared per crate rather than inferred.
12
+ */
13
+ testShards: number;
14
+ }
15
+
16
+ export function listCargoWorkspacePackages(absoluteProjectRoot: string): CargoWorkspacePackage[] {
17
+ const workspaceTomlPath = join(absoluteProjectRoot, 'Cargo.toml');
18
+ if (!existsSync(workspaceTomlPath)) {
19
+ return [];
20
+ }
21
+ const parsed: unknown = parseToml(readFileSync(workspaceTomlPath, 'utf-8'));
22
+ if (!isRecord(parsed) || !isRecord(parsed.workspace) || !Array.isArray(parsed.workspace.members)) {
23
+ return [];
24
+ }
25
+ const packages: CargoWorkspacePackage[] = [];
26
+ for (const member of parsed.workspace.members) {
27
+ if (typeof member !== 'string' || member.length === 0) {
28
+ continue;
29
+ }
30
+ const crateTomlPath = join(absoluteProjectRoot, member, 'Cargo.toml');
31
+ if (!existsSync(crateTomlPath)) {
32
+ continue;
33
+ }
34
+ const crateParsed: unknown = parseToml(readFileSync(crateTomlPath, 'utf-8'));
35
+ if (!isRecord(crateParsed) || !isRecord(crateParsed.package) || typeof crateParsed.package.name !== 'string') {
36
+ continue;
37
+ }
38
+ packages.push({
39
+ name: crateParsed.package.name,
40
+ dir: member.split('\\').join('/'),
41
+ testShards: readTestShards(crateParsed.package, crateTomlPath),
42
+ });
43
+ }
44
+ packages.sort((left, right) => left.name.localeCompare(right.name));
45
+ return packages;
46
+ }
47
+
48
+ export async function cargoPackageTestInputs(absoluteProjectRoot: string, memberDir: string): Promise<string[]> {
49
+ const workspacePathDeps = await workspacePathDependencies(absoluteProjectRoot);
50
+ const dirs = new Set<string>([memberDir.split('\\').join('/')]);
51
+ const crateParsed: unknown = parseToml(await readFile(join(absoluteProjectRoot, memberDir, 'Cargo.toml'), 'utf-8'));
52
+ if (isRecord(crateParsed)) {
53
+ for (const tableName of ['dependencies', 'dev-dependencies', 'build-dependencies'] as const) {
54
+ const table = crateParsed[tableName];
55
+ if (!isRecord(table)) {
56
+ continue;
57
+ }
58
+ for (const [depName, spec] of Object.entries(table)) {
59
+ const pathDep = pathDependencyDir(memberDir, depName, spec, workspacePathDeps);
60
+ if (pathDep !== null) {
61
+ dirs.add(pathDep);
62
+ }
63
+ }
64
+ }
65
+ }
66
+ const inputs: string[] = ['{projectRoot}/Cargo.toml', '{projectRoot}/Cargo.lock'];
67
+ for (const dir of [...dirs].sort()) {
68
+ inputs.push(`{projectRoot}/${dir}/**/*.rs`, `{projectRoot}/${dir}/Cargo.toml`);
69
+ }
70
+ inputs.push('{projectRoot}/**/.cargo/config.toml', '{projectRoot}/scripts/*.sh', '!{projectRoot}/**/target/**');
71
+ return inputs;
72
+ }
73
+
74
+ async function workspacePathDependencies(absoluteProjectRoot: string): Promise<Map<string, string>> {
75
+ const parsed: unknown = parseToml(await readFile(join(absoluteProjectRoot, 'Cargo.toml'), 'utf-8'));
76
+ const deps = new Map<string, string>();
77
+ if (!isRecord(parsed) || !isRecord(parsed.workspace) || !isRecord(parsed.workspace.dependencies)) {
78
+ return deps;
79
+ }
80
+ for (const [name, spec] of Object.entries(parsed.workspace.dependencies)) {
81
+ if (isRecord(spec) && typeof spec.path === 'string') {
82
+ deps.set(name, spec.path.split('\\').join('/'));
83
+ }
84
+ }
85
+ return deps;
86
+ }
87
+
88
+ function pathDependencyDir(
89
+ memberDir: string,
90
+ depName: string,
91
+ spec: unknown,
92
+ workspacePathDeps: Map<string, string>,
93
+ ): string | null {
94
+ if (spec === undefined || spec === null) {
95
+ return null;
96
+ }
97
+ if (typeof spec === 'string') {
98
+ return null;
99
+ }
100
+ if (!isRecord(spec)) {
101
+ return null;
102
+ }
103
+ if (spec.workspace === true) {
104
+ return workspacePathDeps.get(depName) ?? null;
105
+ }
106
+ if (typeof spec.path !== 'string') {
107
+ return null;
108
+ }
109
+ const resolved = normalize(join(memberDir, spec.path));
110
+ if (resolved.startsWith('..')) {
111
+ return null;
112
+ }
113
+ return resolved.split(sep).join(posix.sep);
114
+ }
115
+
116
+ function isRecord(value: unknown): value is Record<string, unknown> {
117
+ return typeof value === 'object' && value !== null && !Array.isArray(value);
118
+ }
119
+
120
+ /**
121
+ * `[package.metadata.smoothbricks.test] shards = N` splits a crate's tests
122
+ * across N bounded targets, each running `--partition hash:i/N`.
123
+ *
124
+ * The COUNT is declared and the MEMBERSHIP is derived: nextest hashes each test
125
+ * name into a shard, so a new test — or a whole new test binary — is absorbed
126
+ * with no edit here and no list to forget to update. A stale count can only
127
+ * make a target slow, never make a test disappear, because the shards remain an
128
+ * exact partition of whatever the crate currently contains.
129
+ *
130
+ * It is declared rather than inferred because the only honest input is how long
131
+ * the suite takes, which target inference cannot measure. Defaulting to 1 keeps
132
+ * small crates on a single target: cowshed-cli's 226 tests run in 6.5s, so
133
+ * sharding them would buy nothing and pay a per-target cargo freshness check.
134
+ */
135
+ function readTestShards(cratePackage: Record<string, unknown>, crateTomlPath: string): number {
136
+ const metadata = isRecord(cratePackage.metadata) ? cratePackage.metadata : null;
137
+ const smoothbricks = metadata && isRecord(metadata.smoothbricks) ? metadata.smoothbricks : null;
138
+ const test = smoothbricks && isRecord(smoothbricks.test) ? smoothbricks.test : null;
139
+ const shards = test?.shards;
140
+ if (shards === undefined) {
141
+ return 1;
142
+ }
143
+ if (typeof shards !== 'number' || !Number.isInteger(shards) || shards < 1) {
144
+ throw new Error(`${crateTomlPath}: smoothbricks.test.shards must be an integer >= 1, got ${String(shards)}`);
145
+ }
146
+ return shards;
147
+ }
148
+
149
+ /**
150
+ * The aggregate that actually runs a cargo workspace's tests. Named here because
151
+ * both target inference and the policy that verifies `test` reaches it need the
152
+ * same string, and a drifted copy would make the policy vacuous.
153
+ */
154
+ export const CARGO_TEST_TARGET = 'cargo-test';
155
+
156
+ /** Prerequisite that compiles the workspace; not a per-crate runner. */
157
+ export const CARGO_TEST_COMPILE_TARGET = 'cargo-test-compile';
158
+
159
+ /**
160
+ * Tests that nextest.toml singles out are pinned to this suffix instead of
161
+ * being sharded. Only a sharded crate has one: an unsharded crate runs its
162
+ * whole suite in a single nextest process, which is all the pin restores.
163
+ */
164
+ export const CARGO_TEST_EXCEPTIONS_SUFFIX = 'exceptions';
165
+
166
+ /**
167
+ * A crate on one target keeps the bare name; a split crate suffixes the piece,
168
+ * so `cargo-test-cowshed-core-shard2` still reads as "cowshed-core's tests".
169
+ */
170
+ export function cargoTestPackageTargetName(packageName: string, piece?: string): string {
171
+ const base = `cargo-test-${packageName}`;
172
+ return piece === undefined ? base : `${base}-${piece}`;
173
+ }
174
+
175
+ /**
176
+ * Inverse of `cargoTestPackageTargetName`, used by the reachability policy to
177
+ * check that the per-crate targets cover every workspace member. Pieces of one
178
+ * crate collapse back to that crate, so a split crate counts as covered once.
179
+ */
180
+ export function packageNameFromCargoTestTarget(targetName: string): string | null {
181
+ if (!targetName.startsWith('cargo-test-') || targetName === CARGO_TEST_COMPILE_TARGET) {
182
+ return null;
183
+ }
184
+ const name = targetName
185
+ .slice('cargo-test-'.length)
186
+ .replace(new RegExp(`-(shard[1-9][0-9]*|${CARGO_TEST_EXCEPTIONS_SUFFIX})$`), '');
187
+ return name.length === 0 ? null : name;
188
+ }
189
+
190
+ /**
191
+ * The tests nextest.toml singles out with an override, as one filterset.
192
+ *
193
+ * An override is the config author saying "this class does not behave like the
194
+ * rest of the suite", and both classes declared today break a shard, each in a
195
+ * different way:
196
+ *
197
+ * - a `test-group` is scoped to a single nextest RUN, so letting the hash
198
+ * scatter its members across shards silently dissolves it. The real-APFS
199
+ * group exists because those tests contend on Disk Arbitration
200
+ * machine-wide, where an unscoped `diskutil apfs list -plist` measured 0.7s
201
+ * idle against 14.4s while another process is attaching.
202
+ * - a raised `slow-timeout` marks a test whose cost is not the suite's cost.
203
+ * `lesser_capabilities_fail_to_compile_...` rustc's a fixture that
204
+ * `cargo test --no-run` cannot pre-build, so it costs 25.6s on a cold
205
+ * target directory against 1.8s warm. Every CI runner is cold, and left in
206
+ * a shard that spike lands on whichever 250-odd tests share it.
207
+ *
208
+ * One target for all of them rather than one each: they sit in different groups
209
+ * so they run concurrently, making the combined wall the max of the classes
210
+ * rather than their sum.
211
+ *
212
+ * Deriving this from the config that declares the classes, rather than
213
+ * restating their filters, means adding an override there is the whole change —
214
+ * the pin follows and cannot drift from what it protects.
215
+ *
216
+ * Returns null when nothing is singled out, the "nothing to pin" case.
217
+ */
218
+ export function exceptionalTestFilter(nextestConfigPath: string): string | null {
219
+ const parsed: unknown = parseToml(readFileSync(nextestConfigPath, 'utf-8'));
220
+ if (!isRecord(parsed)) {
221
+ return null;
222
+ }
223
+ const profiles = isRecord(parsed.profile) ? parsed.profile : {};
224
+ const filters: string[] = [];
225
+ for (const profile of Object.values(profiles)) {
226
+ const overrides = isRecord(profile) ? profile.overrides : undefined;
227
+ if (!Array.isArray(overrides)) {
228
+ continue;
229
+ }
230
+ for (const override of overrides) {
231
+ if (isRecord(override) && typeof override.filter === 'string' && override.filter.length > 0) {
232
+ filters.push(override.filter);
233
+ }
234
+ }
235
+ }
236
+ const unique = [...new Set(filters)];
237
+ return unique.length === 0 ? null : unique.map((filter) => `(${filter})`).join(' or ');
238
+ }
239
+
240
+ export function nextestConfigRelPath(workspaceRoot: string, projectRoot: string, configAbs: string): string {
241
+ const rel = relative(join(workspaceRoot, projectRoot), configAbs);
242
+ return rel.length === 0 ? configAbs : rel.split(sep).join('/');
243
+ }
@@ -3,10 +3,13 @@ import { describe, expect, it } from 'bun:test';
3
3
  import {
4
4
  CARGO_CROSS_LINT_COMMAND,
5
5
  CARGO_CROSS_LINT_TARGET,
6
+ CARGO_LINT_CLIPPY_COMMAND,
6
7
  CARGO_LINUX_TRIPLE,
7
8
  CROSS_CHECK_SCRIPT_COMMAND,
8
9
  CROSS_CHECK_SCRIPT_NAME,
10
+ cargoFrozen,
9
11
  DEVENV_CROSS_PROFILE,
12
+ withProjectCargoHome,
10
13
  } from './cross-check-policy.js';
11
14
  import { BUILD_OUTPUT_DEPENDENCIES, PLATFORM_TARGET_GLOBS } from './workspace-config-policy.js';
12
15
 
@@ -56,6 +59,14 @@ describe('Linux cross-check policy', () => {
56
59
  expect(CARGO_CROSS_LINT_COMMAND).toContain(`--target ${CARGO_LINUX_TRIPLE}`);
57
60
  });
58
61
 
62
+ it('fails closed on Darwin without the linux-cross compiler instead of compiling', () => {
63
+ // Without this guard a Darwin cache miss sits in ring's cc-rs looking for
64
+ // x86_64-linux-gnu-gcc. The linux-cross profile exports
65
+ // CC_x86_64_unknown_linux_gnu; Linux CI is already the target.
66
+ expect(CARGO_CROSS_LINT_COMMAND).toContain('CC_x86_64_unknown_linux_gnu');
67
+ expect(CARGO_CROSS_LINT_COMMAND).toContain('uname -s');
68
+ });
69
+
59
70
  it('carries the triple explicitly so a host lint cannot pass for a cross one', () => {
60
71
  // The devenv profile deliberately exports no CARGO_BUILD_TARGET. Were the
61
72
  // triple ambient instead, running this target outside the cross shell would
@@ -63,6 +74,19 @@ describe('Linux cross-check policy', () => {
63
74
  expect(CARGO_CROSS_LINT_COMMAND).toContain('--target ');
64
75
  });
65
76
 
77
+ it('gives each project its own CARGO_HOME so parallel clippy does not flock ~/.cargo', () => {
78
+ expect(CARGO_CROSS_LINT_COMMAND).toContain('CARGO_HOME="$PWD/target/cargo-lint-cross-home"');
79
+ expect(CARGO_CROSS_LINT_COMMAND).toContain('ln -sfn "$host_cargo_home/registry"');
80
+ expect(CARGO_LINT_CLIPPY_COMMAND).toContain('CARGO_HOME="$PWD/target/cargo-lint-home"');
81
+ expect(withProjectCargoHome('target/h', 'cargo clippy')).toContain('CARGO_HOME="$PWD/target/h" cargo clippy');
82
+ });
83
+
84
+ it('pins clippy to the committed lockfile and local cache', () => {
85
+ expect(cargoFrozen('clippy --workspace')).toBe('cargo --frozen clippy --workspace');
86
+ expect(CARGO_CROSS_LINT_COMMAND).toContain('cargo --frozen clippy');
87
+ expect(CARGO_LINT_CLIPPY_COMMAND).toContain('cargo --frozen clippy');
88
+ });
89
+
66
90
  it('drives the Nx target through the cross profile from the root script', () => {
67
91
  expect(CROSS_CHECK_SCRIPT_NAME).toBe('check:linux');
68
92
  // Activates the profile...
@@ -38,6 +38,69 @@ export const DEVENV_CROSS_PROFILE = 'linux-cross';
38
38
  */
39
39
  export const CARGO_CROSS_LINT_TARGET = 'cargo-lint-cross';
40
40
 
41
+ const CARGO_CROSS_LINT_GUARD = `[ -n "\${CC_x86_64_unknown_linux_gnu:-}" ] || [ "$(uname -s)" = Linux ] || { echo 'cargo-lint-cross needs the linux-cross C toolchain; run: bun run check:linux' >&2; exit 2; }`;
42
+
43
+ /**
44
+ * Point this cargo at a project-local home so parallel Nx cargos do not
45
+ * exclusive-lock `~/.cargo/.package-cache`. Downloaded crates stay shared.
46
+ */
47
+ export function withProjectCargoHome(homeRel: string, command: string): string {
48
+ return `host_cargo_home="\${CARGO_HOME:-$HOME/.cargo}"; mkdir -p ${homeRel}; if [ -d "$host_cargo_home/registry" ]; then ln -sfn "$host_cargo_home/registry" ${homeRel}/registry; fi; if [ -d "$host_cargo_home/git" ]; then ln -sfn "$host_cargo_home/git" ${homeRel}/git; fi; CARGO_HOME="$PWD/${homeRel}" ${command}`;
49
+ }
50
+
51
+ /**
52
+ * The prefix `cargoFrozen` writes. Exported so target inference can recognize
53
+ * the commands whose precondition `CARGO_FETCH_TARGET` supplies without
54
+ * re-spelling the flag; a second copy would silently stop matching.
55
+ */
56
+ export const CARGO_FROZEN_PREFIX = 'cargo --frozen ';
57
+
58
+ /**
59
+ * `--frozen` is cargo's `--locked` + `--offline`. Cargo.lock and the registry
60
+ * cache are inputs: inferred cargo must not rewrite the lockfile or fetch.
61
+ * The flag sits on `cargo` so nextest sees a cargo-level option, not its own.
62
+ *
63
+ * The registry cache being an INPUT is a precondition, and offline cargo
64
+ * enforces it at RESOLUTION, before it selects a single target: an offline
65
+ * registry source only reports packages already downloaded, so one absent
66
+ * member of the lockfile's graph makes the whole workspace unresolvable with
67
+ * `no matching package named <dep> found`. The failure therefore has nothing to
68
+ * do with which targets a command builds — `build`, `check`, `test --lib` and
69
+ * `test --no-run` all fail identically on a dependency only a bench needs — and
70
+ * narrowing a command's target selection cannot cure it.
71
+ *
72
+ * Dev- and bench-only dependencies are the exposed class. A build downloads the
73
+ * normal dependency graph, so on a runner that builds before it tests those are
74
+ * warm by accident; nothing downloads a dev-dependency until something compiles
75
+ * a test target, and by then `--frozen` refuses to. `CARGO_FETCH_TARGET` below
76
+ * turns that accident into a stated dependency.
77
+ */
78
+ export function cargoFrozen(args: string): string {
79
+ return `${CARGO_FROZEN_PREFIX}${args}`;
80
+ }
81
+
82
+ /**
83
+ * Nx target name. Downloads one Cargo workspace's locked dependency graph so
84
+ * every `cargoFrozen` command in that workspace can resolve offline. No colons,
85
+ * per the repo's target-naming rule, and outside the `-js`/`-napi`/`-wasm` and
86
+ * platform-suffix families so no aggregate sweeps it up.
87
+ */
88
+ export const CARGO_FETCH_TARGET = 'cargo-fetch';
89
+
90
+ /**
91
+ * `--locked` rather than bare `cargo fetch`: this downloads exactly the graph
92
+ * Cargo.lock pins and fails loudly on a stale lockfile. Without it a fetch could
93
+ * re-resolve and hand `--frozen` a cache built from a different graph than the
94
+ * lockfile names, which is the guarantee `--frozen` exists to give.
95
+ *
96
+ * `fetch` and nothing else, because fetching is the only act that needs the
97
+ * network; every compile downstream stays frozen.
98
+ *
99
+ * No `--target`: with no triple named cargo fetches every target's
100
+ * dependencies, so one run also covers `CARGO_CROSS_LINT_TARGET`'s Linux triple.
101
+ */
102
+ export const CARGO_FETCH_COMMAND = 'cargo fetch --locked';
103
+
41
104
  /**
42
105
  * Clippy rather than `cargo check`, and `-D warnings`, because that is exactly
43
106
  * what CI's `lint` runs; a weaker local command would pass where CI fails.
@@ -55,8 +118,24 @@ export const CARGO_CROSS_LINT_TARGET = 'cargo-lint-cross';
55
118
  *
56
119
  * `cargo fmt` is absent on purpose: formatting is target-independent and already
57
120
  * covered by `cargo-lint`, so repeating it here would only cost time.
121
+ *
122
+ * `CARGO_HOME` is per-project (`$PWD/target/cargo-lint-cross-home`). Cargo's
123
+ * package-cache flock lives in CARGO_HOME; three workspaces otherwise serialize
124
+ * on `~/.cargo/.package-cache` even with distinct `--target-dir`. Registry and
125
+ * git are linked to the host home so crates are not re-fetched. `--frozen`
126
+ * keeps that shared registry read-only: lockfile and cache are inputs.
58
127
  */
59
- export const CARGO_CROSS_LINT_COMMAND = `cargo clippy --workspace --all-targets --target ${CARGO_LINUX_TRIPLE} -- -D warnings`;
128
+ export const CARGO_CROSS_LINT_COMMAND = `${CARGO_CROSS_LINT_GUARD}; ${withProjectCargoHome(
129
+ 'target/cargo-lint-cross-home',
130
+ cargoFrozen(
131
+ `clippy --workspace --all-targets --target ${CARGO_LINUX_TRIPLE} --target-dir target/cargo-lint-cross -- -D warnings`,
132
+ ),
133
+ )}`;
134
+
135
+ export const CARGO_LINT_CLIPPY_COMMAND = withProjectCargoHome(
136
+ 'target/cargo-lint-home',
137
+ cargoFrozen('clippy --workspace --all-targets --target-dir target/cargo-lint -- -D warnings'),
138
+ );
60
139
 
61
140
  /** Root `package.json` script name, in the repo's `verb:qualifier` style. */
62
141
  export const CROSS_CHECK_SCRIPT_NAME = 'check:linux';
@@ -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