@smoothbricks/nx-plugin 0.5.4 → 0.5.6

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 (48) hide show
  1. package/README.md +81 -16
  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/bounded-exec/executor.d.ts.map +1 -1
  12. package/dist/executors/bounded-exec/executor.js +48 -11
  13. package/dist/executors/bounded-exec/progress.d.ts +1 -3
  14. package/dist/executors/bounded-exec/progress.d.ts.map +1 -1
  15. package/dist/executors/bounded-exec/progress.js +1 -2
  16. package/dist/executors/bounded-exec/runner-hook.d.ts +2 -0
  17. package/dist/executors/bounded-exec/runner-hook.d.ts.map +1 -0
  18. package/dist/executors/bounded-exec/runner-hook.js +301 -0
  19. package/dist/executors/bounded-exec/runner-lifecycle.d.ts +18 -0
  20. package/dist/executors/bounded-exec/runner-lifecycle.d.ts.map +1 -0
  21. package/dist/executors/bounded-exec/runner-lifecycle.js +92 -0
  22. package/dist/producer-receipts.d.ts +24 -1
  23. package/dist/producer-receipts.d.ts.map +1 -1
  24. package/dist/producer-receipts.js +91 -60
  25. package/dist/runtime-input-policy.d.ts +60 -0
  26. package/dist/runtime-input-policy.d.ts.map +1 -0
  27. package/dist/runtime-input-policy.js +379 -0
  28. package/managed/raw/git-format-staged.yml +16 -3
  29. package/managed/raw/patches/nx@23.2.1.patch +397 -33
  30. package/package.json +7 -2
  31. package/src/ensure-built-receipt.ts +8 -2
  32. package/src/ensure-built-witness.ts +29 -1
  33. package/src/ensure-built.ts +39 -7
  34. package/src/executors/bounded-exec/cargo-progress-host.test.ts +332 -0
  35. package/src/executors/bounded-exec/executor.test.ts +89 -42
  36. package/src/executors/bounded-exec/executor.ts +45 -11
  37. package/src/executors/bounded-exec/progress.test.ts +1 -14
  38. package/src/executors/bounded-exec/progress.ts +2 -5
  39. package/src/executors/bounded-exec/runner-hook.test.ts +193 -0
  40. package/src/executors/bounded-exec/runner-hook.ts +292 -0
  41. package/src/executors/bounded-exec/runner-lifecycle.test.ts +82 -0
  42. package/src/executors/bounded-exec/runner-lifecycle.ts +102 -0
  43. package/src/nx-daemon-start-never.test.ts +137 -0
  44. package/src/producer-receipts.test.ts +99 -0
  45. package/src/producer-receipts.ts +111 -58
  46. package/src/runtime-input-policy.ts +395 -0
  47. package/src/runtime-input.test.ts +160 -3
  48. package/test-timings.json +4 -0
@@ -21,7 +21,7 @@ import type {
21
21
  EnsureBuiltProducerReceipt,
22
22
  EnsureBuiltReceipt,
23
23
  } from './ensure-built.js';
24
- import { censusStart, movedNodes, type NodeWitness, recordedStamp } from './ensure-built-witness.js';
24
+ import { censusStart, movedNodes, type NodeWitness, recordedStamp, type StampFields } from './ensure-built-witness.js';
25
25
 
26
26
  export type ReceiptEvidence<T> =
27
27
  | { readonly ok: true; readonly value: T }
@@ -147,12 +147,18 @@ export function receiptNode(
147
147
  path: string,
148
148
  name: string,
149
149
  census?: { readonly start: bigint; readonly observed: NodeWitness[] },
150
+ recorded?: (path: string, stats: StampFields) => string | undefined,
150
151
  ): EnsureBuiltOutputReceipt {
151
152
  const stats = lstatSync(path, { bigint: true });
152
153
  census?.observed.push({ path, stamp: recordedStamp(stats, census.start) });
153
154
  const mode = Number(stats.mode & 0o7777n);
154
155
  if (stats.isFile())
155
- return { path: name, type: 'file', mode, sha256: hashFile(path, Number(stats.size), ['sha256'], false)[0] };
156
+ return {
157
+ path: name,
158
+ type: 'file',
159
+ mode,
160
+ sha256: recorded?.(path, stats) ?? hashFile(path, Number(stats.size), ['sha256'], false)[0],
161
+ };
156
162
  if (stats.isSymbolicLink()) return { path: name, type: 'symlink', mode, linkText: readlinkSync(path) };
157
163
  if (stats.isDirectory()) {
158
164
  const entries = readdirSync(path).sort();
@@ -121,7 +121,7 @@ export function readGraphWitness(path: string): GraphWitness | null {
121
121
  }
122
122
  }
123
123
 
124
- export function writeWitness(path: string, value: GraphWitness | ArtifactWitness): void {
124
+ export function writeWitness(path: string, value: GraphWitness | ArtifactWitness | GraphInstallationCensus): void {
125
125
  const temporary = `${path}.${randomUUID()}`;
126
126
  try {
127
127
  writeFileSync(temporary, JSON.stringify(value), { flag: 'wx', mode: 0o600 });
@@ -296,6 +296,34 @@ export function recordedInstallationCensus(
296
296
  }
297
297
  }
298
298
 
299
+ /** The census a caller recorded at `path`, `unproven` included: a hash source for {@link graphInstallationCensus}, never a
300
+ * proof. An absent, unreadable or malformed record is no hash source, and the next census reads every file.
301
+ */
302
+ export function readInstallationCensus(path: string): GraphInstallationCensus | undefined {
303
+ try {
304
+ return typia.json.isParse<GraphInstallationCensus>(readFileSync(path, 'utf8')) ?? undefined;
305
+ } catch (error) {
306
+ if (error instanceof SyntaxError || systemError(error)) return undefined;
307
+ throw error;
308
+ }
309
+ }
310
+
311
+ /**
312
+ * The hash `census` read of the file at `path`, while `stats` is the stamp it read the file at and it did not read the
313
+ * file in the tick it changed in ({@link unproven}); otherwise `undefined`, and the caller reads the bytes. A second
314
+ * reader of a file the census holds takes this instead of reading it again: a write landing between two reads could
315
+ * make them disagree, and a write since the census moves the stamp, so it is read again.
316
+ */
317
+ export function censusFileHashes(
318
+ census: GraphInstallationCensus,
319
+ ): (path: string, stats: StampFields) => string | undefined {
320
+ const unprovenFiles = new Set(census.unproven);
321
+ const stamps = new Map(census.nodes.map(({ path, stamp }) => [path, stamp]));
322
+ const hashes = new Map(census.files.map(({ path, sha256 }) => [path, sha256]));
323
+ return (path, stats) =>
324
+ unprovenFiles.has(path) || stamps.get(path) !== nodeStamp(stats) ? undefined : hashes.get(path);
325
+ }
326
+
299
327
  /**
300
328
  * A {@link graphInstallationCensus} that no node moved during: every node is read again after the census, and one
301
329
  * whose stamp moved fails it, unless only its ctime moved and its bytes are the ones hashed. `previous` is what the
@@ -20,8 +20,8 @@ import type { NxJsonConfiguration } from 'nx/src/config/nx-json';
20
20
  import type { ProjectGraph, ProjectGraphProjectNode } from 'nx/src/config/project-graph';
21
21
  import type { Task, TaskGraph } from 'nx/src/config/task-graph';
22
22
  import type * as NxDaemonClient from 'nx/src/daemon/client/client';
23
+ import type * as NxCreateTaskHasher from 'nx/src/hasher/create-task-hasher';
23
24
  import type * as NxHashTask from 'nx/src/hasher/hash-task';
24
- import type * as NxTaskHasher from 'nx/src/hasher/task-hasher';
25
25
  import type * as NxNative from 'nx/src/native';
26
26
  import type * as NxTransformObjects from 'nx/src/native/transform-objects';
27
27
  import type * as NxExecutionHooks from 'nx/src/project-graph/plugins/tasks-execution-hooks';
@@ -67,7 +67,7 @@ interface NxRuntimeModules {
67
67
  readonly 'nx/src/config/nx-json': typeof NxConfiguration;
68
68
  readonly 'nx/src/daemon/client/client': typeof NxDaemonClient;
69
69
  readonly 'nx/src/hasher/hash-task': typeof NxHashTask;
70
- readonly 'nx/src/hasher/task-hasher': typeof NxTaskHasher;
70
+ readonly 'nx/src/hasher/create-task-hasher': typeof NxCreateTaskHasher;
71
71
  readonly 'nx/src/native': typeof NxNative;
72
72
  readonly 'nx/src/native/transform-objects': typeof NxTransformObjects;
73
73
  readonly 'nx/src/project-graph/utils/find-project-for-path': typeof NxProjectRootMappings;
@@ -834,8 +834,32 @@ async function withTaskHooks<Result>(
834
834
  return result;
835
835
  }
836
836
 
837
+ /**
838
+ * `work`, or `withoutDaemon` when the daemon it was using stopped under a client
839
+ * that may never start one (NX_DAEMON_START=never): the patched client's error
840
+ * carries `daemonStartForbidden`, and has already turned the daemon off for
841
+ * this process. The probe then answers as it does without a daemon.
842
+ */
843
+ async function orNoDaemon<Result>(work: () => Promise<Result>, withoutDaemon: () => Result): Promise<Result> {
844
+ try {
845
+ return await work();
846
+ } catch (error) {
847
+ if (error instanceof Error && 'daemonStartForbidden' in error && error.daemonStartForbidden === true) {
848
+ return withoutDaemon();
849
+ }
850
+ throw error;
851
+ }
852
+ }
853
+
854
+ function noDaemonProbe(selector: TargetSelector): Extract<PreparedWorkspaceProbe, { kind: 'no-daemon' }> {
855
+ return { kind: 'no-daemon', reason: { kind: 'no-daemon', taskId: selectorTaskId(selector) } };
856
+ }
857
+
837
858
  async function ensureBuiltInWorkspace(workspaceRoot: string, selector: TargetSelector): Promise<EnsureBuiltResult> {
838
- const prepared = await prepareWorkspaceProbe(workspaceRoot, selector, 'build');
859
+ const prepared = await orNoDaemon(
860
+ () => prepareWorkspaceProbe(workspaceRoot, selector, 'build'),
861
+ (): PreparedWorkspaceProbe => noDaemonProbe(selector),
862
+ );
839
863
  if (prepared.kind === 'no-daemon') return runViaCli(workspaceRoot, selector, prepared.reason);
840
864
  return withTaskHooks(workspaceRoot, selector, prepared, buildPreparedTarget);
841
865
  }
@@ -849,7 +873,10 @@ async function buildPreparedTarget(
849
873
  performance.mark('ensureBuilt:probe:start');
850
874
  const reason =
851
875
  missBeforeProbe(process.env, selectorTaskId(selector)) ??
852
- (await probe(workspaceRoot, nxJson, nxArgs, projectGraph, taskGraph, tasks, requireNx));
876
+ (await orNoDaemon(
877
+ () => probe(workspaceRoot, nxJson, nxArgs, projectGraph, taskGraph, tasks, requireNx),
878
+ (): MissReason => noDaemonProbe(selector).reason,
879
+ ));
853
880
  performance.measure('ensureBuilt:probe', 'ensureBuilt:probe:start');
854
881
  return reason === null ? HIT : runViaCli(workspaceRoot, selector, reason);
855
882
  }
@@ -861,7 +888,10 @@ async function probeBuiltInWorkspace(workspaceRoot: string, selector: TargetSele
861
888
  // A failed publication proof asks a serving Nx daemon for the authoritative
862
889
  // generation, but never hashes against the consumer environment, starts a
863
890
  // provider runner or starts the provider's daemon.
864
- const prepared = await prepareWorkspaceProbe(workspaceRoot, selector, 'read-only');
891
+ const prepared = await orNoDaemon(
892
+ () => prepareWorkspaceProbe(workspaceRoot, selector, 'read-only'),
893
+ (): PreparedWorkspaceProbe => noDaemonProbe(selector),
894
+ );
865
895
  const detail =
866
896
  prepared.kind === 'no-daemon'
867
897
  ? `${receipt.detail}; authoritative graph unavailable: ${describeMiss(prepared.reason)}`
@@ -1167,7 +1197,7 @@ async function probe(
1167
1197
  verifyAllArtifacts = false,
1168
1198
  ): Promise<MissReason | null> {
1169
1199
  const { daemonClient } = requireNx('nx/src/daemon/client/client');
1170
- const { DaemonBasedTaskHasher } = requireNx('nx/src/hasher/task-hasher');
1200
+ const { createTaskHasher } = requireNx('nx/src/hasher/create-task-hasher');
1171
1201
  const { getTaskDetails, hashTasks } = requireNx('nx/src/hasher/hash-task');
1172
1202
  const { getTaskSpecificEnv } = requireNx('nx/src/tasks-runner/task-env');
1173
1203
  const { getCache } = requireNx('nx/src/tasks-runner/cache');
@@ -1180,7 +1210,9 @@ async function probe(
1180
1210
  // flag would add belong to the cloud runner, and the run path calls Nx's own
1181
1211
  // `getRunner` to obtain them.
1182
1212
  const runnerOptions = getRunnerOptions('default', nxJson, nxArgs, false);
1183
- const hasher = new DaemonBasedTaskHasher(daemonClient, runnerOptions);
1213
+ // Nx's own composition: the daemon's hasher, which continues in this process
1214
+ // if the daemon stops under NX_DAEMON_START=never.
1215
+ const hasher = createTaskHasher(projectGraph, nxJson, runnerOptions);
1184
1216
  performance.mark('ensureBuilt:hash:start');
1185
1217
  // Every task, each against its own environment — per-project and per-target
1186
1218
  // `.env` files and custom hashers that read env participate in the hash, so
@@ -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 () => {