@smoothbricks/nx-plugin 0.6.0 → 0.6.1

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 (51) hide show
  1. package/README.md +13 -2
  2. package/dist/.build-inputs.json +13 -10
  3. package/dist/ensure-built-receipt.d.ts +9 -0
  4. package/dist/ensure-built-receipt.d.ts.map +1 -1
  5. package/dist/ensure-built-receipt.js +42 -1
  6. package/dist/executors/bounded-exec/cpu-pool-thread.d.ts +28 -0
  7. package/dist/executors/bounded-exec/cpu-pool-thread.d.ts.map +1 -0
  8. package/dist/executors/bounded-exec/cpu-pool-thread.js +20 -0
  9. package/dist/executors/bounded-exec/cpu-pool.d.ts +4 -4
  10. package/dist/executors/bounded-exec/cpu-pool.d.ts.map +1 -1
  11. package/dist/executors/bounded-exec/cpu-pool.js +7 -6
  12. package/dist/executors/bounded-exec/executor.d.ts.map +1 -1
  13. package/dist/executors/bounded-exec/executor.js +20 -2
  14. package/dist/executors/bounded-exec/gateway-lease.d.ts +3 -2
  15. package/dist/executors/bounded-exec/gateway-lease.d.ts.map +1 -1
  16. package/dist/executors/bounded-exec/gateway-lease.js +23 -3
  17. package/dist/executors/bounded-exec/lease-wait.d.ts +23 -2
  18. package/dist/executors/bounded-exec/lease-wait.d.ts.map +1 -1
  19. package/dist/executors/bounded-exec/lease-wait.js +15 -11
  20. package/dist/executors/bounded-exec/mbx-pool.d.ts +31 -0
  21. package/dist/executors/bounded-exec/mbx-pool.d.ts.map +1 -0
  22. package/dist/executors/bounded-exec/mbx-pool.js +24 -0
  23. package/dist/executors/bounded-exec/run-cpu-pool.d.ts +9 -3
  24. package/dist/executors/bounded-exec/run-cpu-pool.d.ts.map +1 -1
  25. package/dist/executors/bounded-exec/run-cpu-pool.js +109 -12
  26. package/dist/executors/bounded-exec/verdict.d.ts +8 -0
  27. package/dist/executors/bounded-exec/verdict.d.ts.map +1 -1
  28. package/dist/executors/bounded-exec/verdict.js +1 -1
  29. package/dist/executors/typescript-emit/emit-overrides.d.ts +14 -0
  30. package/dist/executors/typescript-emit/emit-overrides.d.ts.map +1 -0
  31. package/dist/executors/typescript-emit/emit-overrides.js +24 -0
  32. package/dist/executors/typescript-emit/executor.d.ts.map +1 -1
  33. package/dist/executors/typescript-emit/executor.js +1 -19
  34. package/dist/managed-files/publish-workflow.js +14 -11
  35. package/dist/producer-receipts.d.ts.map +1 -1
  36. package/dist/producer-receipts.js +441 -1
  37. package/package.json +19 -2
  38. package/src/ensure-built-receipt.ts +41 -0
  39. package/src/executors/bounded-exec/cpu-pool-thread.ts +43 -0
  40. package/src/executors/bounded-exec/cpu-pool.ts +7 -6
  41. package/src/executors/bounded-exec/executor.ts +20 -2
  42. package/src/executors/bounded-exec/gateway-lease.ts +17 -7
  43. package/src/executors/bounded-exec/lease-wait.ts +39 -12
  44. package/src/executors/bounded-exec/mbx-pool.ts +48 -0
  45. package/src/executors/bounded-exec/run-cpu-pool.ts +117 -15
  46. package/src/executors/bounded-exec/verdict.ts +8 -1
  47. package/src/executors/typescript-emit/emit-overrides.ts +24 -0
  48. package/src/executors/typescript-emit/executor.ts +1 -20
  49. package/src/managed-files/publish-workflow.ts +20 -15
  50. package/src/producer-receipts.ts +28 -0
  51. package/test-timings.json +119 -118
@@ -0,0 +1,43 @@
1
+ import { parentPort, workerData } from 'node:worker_threads';
2
+ import { type CloseOutcome, createCpuPool } from './cpu-pool.js';
3
+
4
+ /**
5
+ * The thread an Nx run's CPU pool runs on (`run-cpu-pool.ts`; specs/cowshed/05_gateway.md, "Every Nx run budgets
6
+ * itself"). The process that serves a run's pool, the Nx daemon when installed plugins load in it or the Nx CLI without
7
+ * one, hashes tasks and builds the project graph in synchronous calls on its main thread. A pool there writes no
8
+ * heartbeat while one runs, and every runner queued on it fails `SILENT` while it still works. Here the pool's socket,
9
+ * jobserver fifo and heartbeat share an event loop that runs nothing else, so the heartbeat proves that the actor which
10
+ * admits and grants is turning.
11
+ *
12
+ * The thread is started with the pool's count. It says once that the pool serves, with the environment a task inherits,
13
+ * or why it cannot; the host's one message is its close, after which the thread says how the pool closed.
14
+ */
15
+
16
+ /** What the thread is started with. */
17
+ export interface CpuPoolThreadData {
18
+ readonly tokens: number;
19
+ }
20
+
21
+ /** What the thread says, in order: the pool serves or failed; after the host's close, how the pool closed. */
22
+ export type CpuPoolThreadReport =
23
+ | { readonly kind: 'serving'; readonly environment: Record<string, string> }
24
+ | { readonly kind: 'failed'; readonly error: string }
25
+ | { readonly kind: 'closed'; readonly outcome: CloseOutcome };
26
+
27
+ const host = parentPort;
28
+ if (host === null) {
29
+ throw new Error('cpu-pool-thread runs as the worker thread run-cpu-pool starts, never as a module of its own');
30
+ }
31
+ const report = (message: CpuPoolThreadReport): void => host.postMessage(message);
32
+ const data: CpuPoolThreadData = workerData;
33
+ // The pool lives until its host closes it, or with the thread.
34
+ const made = await createCpuPool(data.tokens, new Promise<void>(() => {}));
35
+ if (made.ok) {
36
+ const { pool } = made;
37
+ host.once('message', () => {
38
+ void pool.close().then((outcome) => report({ kind: 'closed', outcome }));
39
+ });
40
+ report({ kind: 'serving', environment: pool.environment });
41
+ } else {
42
+ report({ kind: 'failed', error: made.error });
43
+ }
@@ -7,14 +7,15 @@ import { promisify } from 'node:util';
7
7
  import typia from 'typia';
8
8
  import { ownedSocketDirectory } from '../../testing.js';
9
9
  import { holdLease, type Unleased } from './gateway-lease.js';
10
- import { type CpuLeaseProgress, CpuLeaseWait, HEARTBEAT_MS } from './lease-wait.js';
10
+ import { type CpuLeaseProgress, type CpuLeaseQueued, CpuLeaseWait, HEARTBEAT_MS } from './lease-wait.js';
11
11
  import { CPU_TOKENS_ENV, parseInheritedTokens } from './runner-sizing.js';
12
12
 
13
13
  /**
14
14
  * A CPU pool: the tokens a process lends the runners it starts, held live rather than inherited as a
15
15
  * bare count (specs/cowshed/05_gateway.md, "Host scheduling", "Parallelism inside a job"). An Nx run
16
- * serves one (`run-cpu-pool.ts`), and every granted bounded-exec serves a sub-pool of its grant to
17
- * its own descendants, so the runners of one build tree together stay within the tree's count.
16
+ * serves one on a thread of its own (`run-cpu-pool.ts`), and every granted bounded-exec serves a
17
+ * sub-pool of its grant to its own descendants, so the runners of one build tree together stay
18
+ * within the tree's count.
18
19
  *
19
20
  * The pool is a private Unix socket in a directory the process owns: the directory 0700, the socket
20
21
  * 0600. A child asks it for a count with one request line, in `holdLease`'s framing. The pool
@@ -52,7 +53,7 @@ export interface CpuPool {
52
53
  close(): Promise<CloseOutcome>;
53
54
  }
54
55
 
55
- type CloseOutcome = { ok: true } | { ok: false; error: string };
56
+ export type CloseOutcome = { ok: true } | { ok: false; error: string };
56
57
 
57
58
  /** The one line a child sends: a count of whole tokens, at least one. */
58
59
  interface CpuPoolRequest {
@@ -63,7 +64,7 @@ interface CpuPoolRequest {
63
64
  /** The pool's answers, in `holdLease`'s framing. */
64
65
  type CpuPoolReply =
65
66
  | CpuLeaseProgress
66
- | { ok: true; lease: 'queued' }
67
+ | CpuLeaseQueued
67
68
  | { ok: true; lease: 'granted'; tokens: number }
68
69
  | { ok: false; code: 'invalid-request'; error: string };
69
70
 
@@ -528,7 +529,7 @@ export async function createCpuPool(
528
529
  const request = parsed;
529
530
  child.phase = { kind: 'queued' };
530
531
  queue.push({ child, want: request.want });
531
- socket.write(frame({ ok: true, lease: 'queued' }));
532
+ socket.write(frame({ ok: true, lease: 'queued', server: process.pid }));
532
533
  drain(0);
533
534
  });
534
535
  };
@@ -26,7 +26,9 @@ import {
26
26
  import { GATEWAY_SOCKET } from './gateway-lease.js';
27
27
  import type { HangDiagnostics } from './hang-diagnostics.js';
28
28
  import { captureHangDiagnostics, HANG_CAPTURE_MS } from './hang-diagnostics.js';
29
+ import type { CpuWaitRow } from './lease-wait.js';
29
30
  import type { LockWait } from './lock-wait.js';
31
+ import { mbxLeaf, mbxPool, withMbxLeaf } from './mbx-pool.js';
30
32
  import { createNoSpaceWatch } from './no-space.js';
31
33
  import {
32
34
  COWSHED_EXECUTABLE_ENV,
@@ -515,6 +517,7 @@ export async function runBoundedExec(
515
517
  // instead. A pool whose endpoint is gone (its holder exited) lends nothing, and the run goes on as asked.
516
518
  let grant: GrantedTokens | null = null;
517
519
  let parentRefusal: string | null = null;
520
+ let parentWait: CpuWaitRow | undefined;
518
521
  if (inherited !== null) {
519
522
  const fromParent = await takeParentCpuTokens(inherited, demand.want);
520
523
  if (fromParent.granted) {
@@ -523,17 +526,25 @@ export async function runBoundedExec(
523
526
  appendStderr(`cowshed: cpu-pool parent pool absent: ${fromParent.reason}; running the command as asked\n`);
524
527
  } else {
525
528
  parentRefusal = fromParent.reason;
529
+ parentWait = fromParent.wait;
526
530
  }
527
531
  }
528
532
  const waitedMs = Date.now() - asked;
529
533
  let cpu: BoundedExecRecord['cpu'];
530
534
  let sized: { command: string; env: Record<string, string> } = { command: requested, env: {} };
535
+ // With mbx the leaf's admission is mbx's one pool, not a parent's tokens: its runner is sized to the job's count,
536
+ // and a non-Cargo leaf holds that much of the pool for its run (specs/cowshed/05_gateway.md, "Parallelism inside a
537
+ // job"). No grant and no sub-pool beside it.
538
+ const mbx = mbxPool(process.env);
539
+ const leaf = mbx === null ? null : mbxLeaf(mbx, demand.kind, demand.want);
531
540
  if (grant !== null) {
532
541
  sized = sizeRunner(demand, requested, grant.tokens);
533
542
  cpu = { tokens: grant.tokens, want: demand.want, waitedMs };
534
543
  appendStderr(
535
544
  `cowshed: cpu-pool wait done elapsed=${waitedMs}ms status=ok tokens=${grant.tokens}/${demand.want} runner=${demand.kind}\n`,
536
545
  );
546
+ } else if (leaf !== null) {
547
+ sized = sizeRunner(demand, requested, demand.want);
537
548
  }
538
549
  // The run's sub-pool and grant are given back exactly once, however the run ends. The sub-pool closes first, so no
539
550
  // descendant still draws on tokens the grant has returned.
@@ -575,7 +586,13 @@ export async function runBoundedExec(
575
586
  if (verdict.outcome === 'bound' && verdict.bound === 'test') {
576
587
  appendStderr(`Every failing test failed only on its per-test timeout: ${verdict.tests.join(', ')}\n`);
577
588
  }
578
- await writeRecord(recordDirectory, { task: task.id, hash: task.hash, verdict, ...(cpu ? { cpu } : {}) });
589
+ await writeRecord(recordDirectory, {
590
+ task: task.id,
591
+ hash: task.hash,
592
+ verdict,
593
+ ...(cpu ? { cpu } : {}),
594
+ ...(parentWait ? { cpuWait: parentWait } : {}),
595
+ });
579
596
  };
580
597
  // A run refused before its command starts says why, gives back its grant and RAM, and records the failure.
581
598
  const refuse = async (message: string): Promise<BoundedExecResult> => {
@@ -627,7 +644,8 @@ export async function runBoundedExec(
627
644
  const startedAt = Date.now();
628
645
  // With a supervisor, `cowshed subtree-root` asks it for a handle on itself, writes the answer to the handle
629
646
  // descriptor, and execs `/bin/sh -c command` in the same process: the command's pid, group and life are unchanged.
630
- const shellCommand = hooked?.command ?? (fixture === null ? command : `${fixture.command} && ${command}`);
647
+ const unadmitted = hooked?.command ?? (fixture === null ? command : `${fixture.command} && ${command}`);
648
+ const shellCommand = leaf === null ? unadmitted : withMbxLeaf(unadmitted, leaf);
631
649
  const root = owner?.rootArgv(shellCommand);
632
650
  let child: ChildProcess;
633
651
  try {
@@ -1,8 +1,9 @@
1
1
  import { connect } from 'node:net';
2
2
  import typia from 'typia';
3
- import { type CpuLeaseProgress, CpuLeaseWait } from './lease-wait.js';
3
+ import { type CpuLeaseProgress, type CpuLeaseQueued, CpuLeaseWait, type CpuWaitRow } from './lease-wait.js';
4
4
 
5
5
  const isCpuProgress = typia.createEquals<CpuLeaseProgress>();
6
+ const isCpuQueued = typia.createEquals<CpuLeaseQueued>();
6
7
 
7
8
  /**
8
9
  * The client half of a lease held on a unix socket: the gateway's host disk-lifecycle lease
@@ -35,8 +36,8 @@ export interface LeaseBounds {
35
36
  */
36
37
  export type UnleasedCause = 'absent' | 'predates' | 'predates-field' | 'other';
37
38
 
38
- /** A refusal: the lease is not held, and why. */
39
- export type Unleased = { granted: false; cause: UnleasedCause; reason: string; code?: string };
39
+ /** A refusal: the lease is not held, and why. A CPU wait that expired carries its typed row. */
40
+ export type Unleased = { granted: false; cause: UnleasedCause; reason: string; code?: string; wait?: CpuWaitRow };
40
41
 
41
42
  /**
42
43
  * A granted lease with its `granted` answer, held until released, or why there is none. `closed`
@@ -89,8 +90,14 @@ export function holdLease(
89
90
  }
90
91
  resolve(outcome);
91
92
  };
92
- const unleased = (reason: string, cause: UnleasedCause = 'other', code?: string) =>
93
- settle({ granted: false, cause, reason, ...(code === undefined ? {} : { code }) });
93
+ const unleased = (reason: string, cause: UnleasedCause = 'other', code?: string, wait?: CpuWaitRow) =>
94
+ settle({
95
+ granted: false,
96
+ cause,
97
+ reason,
98
+ ...(code === undefined ? {} : { code }),
99
+ ...(wait === undefined ? {} : { wait }),
100
+ });
94
101
  const wait = (ms: number, onExpiry: () => void) => {
95
102
  clearTimeout(timer);
96
103
  timer = setTimeout(onExpiry, ms);
@@ -104,10 +111,12 @@ export function holdLease(
104
111
  armCpu();
105
112
  return;
106
113
  }
114
+ const peer = failure.server === null ? socket : `${socket} (pid ${failure.server})`;
107
115
  unleased(
108
- `${failure.code}: cpu-pool: the lease endpoint ${socket} exceeded ${failure.boundMs} ms (waited ${failure.elapsedMs} ms; last progress: ${failure.last})`,
116
+ `${failure.code}: cpu-pool: the lease endpoint ${peer} exceeded ${failure.boundMs} ms (waited ${failure.elapsedMs} ms; last progress: ${failure.last})`,
109
117
  'other',
110
118
  failure.code,
119
+ { ...failure, socket },
111
120
  );
112
121
  });
113
122
  };
@@ -161,7 +170,8 @@ export function holdLease(
161
170
  state = 'queued';
162
171
  onQueued?.();
163
172
  if (bounds instanceof CpuLeaseWait) {
164
- bounds.queued();
173
+ // A pool from before its acknowledgement named its process names none.
174
+ bounds.queued(isCpuQueued(parsed) ? parsed.server : null);
165
175
  armCpu();
166
176
  } else if (bounds.grantMs === null) {
167
177
  clearTimeout(timer);
@@ -1,3 +1,5 @@
1
+ import type { tags } from 'typia';
2
+
1
3
  /** Peer wait classes from specs/cowshed/05_gateway.md, "Deadlines". */
2
4
  export const CONTROL_MS = 5_000;
3
5
  export const HEARTBEAT_MS = 5_000;
@@ -9,7 +11,19 @@ export interface CpuLeaseProgress {
9
11
  progress: number;
10
12
  }
11
13
 
12
- type CpuWaitState = { kind: 'sending' } | { kind: 'waiting'; at: number; last: string; sequence: number };
14
+ /**
15
+ * The CPU pool's queue acknowledgement, shared by its server and lease client. `server` is the pid of the process the
16
+ * pool runs in, so a wait that falls silent names the process to look at.
17
+ */
18
+ export interface CpuLeaseQueued {
19
+ ok: true;
20
+ lease: 'queued';
21
+ server: number & tags.Type<'uint32'>;
22
+ }
23
+
24
+ type CpuWaitState =
25
+ | { kind: 'sending' }
26
+ | { kind: 'waiting'; at: number; last: string; sequence: number; server: number | null };
13
27
 
14
28
  /** A failed wait, not a failed command: expiry ends this client's admission only. */
15
29
  export interface CpuWaitFailure {
@@ -17,6 +31,15 @@ export interface CpuWaitFailure {
17
31
  boundMs: number;
18
32
  elapsedMs: number;
19
33
  last: string;
34
+ /** The last advancing heartbeat sequence the client read; 0 before the first. */
35
+ sequence: number;
36
+ /** The pool's pid as its queue acknowledgement named it; null before one, or from a pool that names none. */
37
+ server: number | null;
38
+ }
39
+
40
+ /** An expired CPU wait as its client keeps it: the failure, and the socket of the pool it waited on. */
41
+ export interface CpuWaitRow extends CpuWaitFailure {
42
+ socket: string;
20
43
  }
21
44
 
22
45
  /** Pure CPU admission policy; its injected monotonic clock performs no I/O or scheduling. */
@@ -35,15 +58,18 @@ export class CpuLeaseWait {
35
58
  sent(): void {
36
59
  this.looked = this.now();
37
60
  if (this.state.kind === 'sending') {
38
- this.state = { kind: 'waiting', at: this.now(), last: 'request sent', sequence: 0 };
61
+ this.state = { kind: 'waiting', at: this.now(), last: 'request sent', sequence: 0, server: null };
39
62
  }
40
63
  }
41
64
 
42
- /** A queue entry proves actor progress, not a deadline for returning a token. */
43
- queued(): void {
65
+ /**
66
+ * A queue entry proves actor progress, not a deadline for returning a token. It names the pool's pid when the pool
67
+ * says it.
68
+ */
69
+ queued(server: number | null): void {
44
70
  this.sent();
45
71
  if (this.state.kind === 'waiting') {
46
- this.state = { ...this.state, at: this.now(), last: 'queued' };
72
+ this.state = { ...this.state, at: this.now(), last: 'queued', server };
47
73
  }
48
74
  }
49
75
 
@@ -52,7 +78,7 @@ export class CpuLeaseWait {
52
78
  this.sent();
53
79
  if (this.state.kind !== 'waiting' || !Number.isSafeInteger(sequence) || sequence <= this.state.sequence)
54
80
  return false;
55
- this.state = { kind: 'waiting', at: this.now(), last: `heartbeat ${sequence}`, sequence };
81
+ this.state = { ...this.state, at: this.now(), last: `heartbeat ${sequence}`, sequence };
56
82
  return true;
57
83
  }
58
84
 
@@ -79,11 +105,12 @@ export class CpuLeaseWait {
79
105
  /** No total queue age exists: an advancing heartbeat keeps the wait alive indefinitely. */
80
106
  expired(): CpuWaitFailure | null {
81
107
  if (this.remainingMs() > 0) return null;
82
- return {
83
- code: this.state.kind === 'sending' ? 'TIMEOUT' : 'SILENT',
84
- boundMs: this.state.kind === 'sending' ? CONTROL_MS : HEARTBEAT_SILENCE_MS,
85
- elapsedMs: this.now() - this.started,
86
- last: this.state.kind === 'sending' ? 'connecting or sending request' : this.state.last,
87
- };
108
+ const elapsedMs = this.now() - this.started;
109
+ if (this.state.kind === 'sending') {
110
+ const last = 'connecting or sending request';
111
+ return { code: 'TIMEOUT', boundMs: CONTROL_MS, elapsedMs, last, sequence: 0, server: null };
112
+ }
113
+ const { last, sequence, server } = this.state;
114
+ return { code: 'SILENT', boundMs: HEARTBEAT_SILENCE_MS, elapsedMs, last, sequence, server };
88
115
  }
89
116
  }
@@ -0,0 +1,48 @@
1
+ import type { RunnerKind } from './runner-sizing.js';
2
+ import { shellQuote } from './verdict.js';
3
+
4
+ /**
5
+ * mbx's machine-wide pool as a leaf runner meets it (specs/cowshed/05_gateway.md, "Host scheduling", "Parallelism inside
6
+ * a job"). With the mbx capability the canonical environment names the one pool (`MBX_SCHEDULER=1` beside the shared
7
+ * `MBX_CACHE_DIR`) and the job's priority in it; that pool is the host's CPU and memory ledger, so no Nx run or leaf
8
+ * serves a pool of its own beside it.
9
+ */
10
+ export interface MbxPool {
11
+ readonly priority: 'normal' | 'low';
12
+ }
13
+
14
+ /** The pool the environment names, or `null` where the project has no mbx and cowshed's own pools apply unchanged. */
15
+ export function mbxPool(environment: Readonly<NodeJS.ProcessEnv>): MbxPool | null {
16
+ if (environment.MBX_SCHEDULER !== '1' || !environment.MBX_CACHE_DIR) {
17
+ return null;
18
+ }
19
+ return { priority: environment.MBX_SCHEDULER_PRIORITY === 'normal' ? 'normal' : 'low' };
20
+ }
21
+
22
+ /**
23
+ * How a leaf enters the pool once: a Cargo leaf runs through mbx, which admits its compiler and supported test work
24
+ * itself, so wrapping it would charge it twice; any other leaf holds `cpus` of the pool for its whole run through
25
+ * `mbx reserve`, `cpus` being the job's own count.
26
+ */
27
+ export type MbxLeaf =
28
+ | { readonly kind: 'cargo' }
29
+ | { readonly kind: 'reserve'; readonly cpus: number; readonly priority: 'normal' | 'low' };
30
+
31
+ const CARGO_LEAVES: ReadonlySet<RunnerKind> = new Set<RunnerKind>(['cargo', 'cargo-mutants', 'nextest']);
32
+
33
+ export function mbxLeaf(pool: MbxPool, kind: RunnerKind, want: number): MbxLeaf {
34
+ return CARGO_LEAVES.has(kind)
35
+ ? { kind: 'cargo' }
36
+ : { kind: 'reserve', cpus: Math.max(1, want), priority: pool.priority };
37
+ }
38
+
39
+ /**
40
+ * `command` as the shell runs it under its leaf's admission. `mbx` is the canonical environment's, whose realpath
41
+ * cowshed's admission proved to be the host's pinned mbx.
42
+ */
43
+ export function withMbxLeaf(command: string, leaf: MbxLeaf): string {
44
+ if (leaf.kind === 'cargo') {
45
+ return command;
46
+ }
47
+ return `mbx reserve --cpus ${leaf.cpus} --priority ${leaf.priority} -- /bin/sh -c ${shellQuote(command)}`;
48
+ }
@@ -1,7 +1,11 @@
1
1
  import { availableParallelism } from 'node:os';
2
+ import { fileURLToPath } from 'node:url';
3
+ import { Worker } from 'node:worker_threads';
2
4
  import { logger } from 'nx/src/devkit-exports.js';
3
5
  import type { PostTasksExecutionContext, PreTasksExecutionContext } from 'nx/src/project-graph/plugins/public-api.js';
4
- import { type CpuPool, createCpuPool, readInheritedCpuPool } from './cpu-pool.js';
6
+ import { type CloseOutcome, type CpuPool, readInheritedCpuPool } from './cpu-pool.js';
7
+ import type { CpuPoolThreadData, CpuPoolThreadReport } from './cpu-pool-thread.js';
8
+ import { mbxPool } from './mbx-pool.js';
5
9
  import { CPU_TOKENS_ENV, parseInheritedTokens } from './runner-sizing.js';
6
10
 
7
11
  /**
@@ -18,42 +22,140 @@ import { CPU_TOKENS_ENV, parseInheritedTokens } from './runner-sizing.js';
18
22
  * A run that inherits a pool (an enclosing executor's) takes from that one and serves none. Otherwise its pool holds
19
23
  * one token per core, or the inherited positive `BOUNDED_EXEC_CPU_TOKENS` when that is fewer: inside a cowshed job
20
24
  * that count is the job's own, so the job's `--want` bounds the run.
25
+ *
26
+ * The pool runs on a thread of its own (`cpu-pool-thread.ts`). The process that serves it, the Nx daemon when installed
27
+ * plugins load in it or the Nx CLI without one, hashes the run's tasks and builds its project graph in synchronous calls
28
+ * on its main thread. In a gate on 2026-10-28 one `hashPlans` ran for 14.2 s, and sixteen runners queued on a pool on
29
+ * that thread failed `SILENT` together while it was alive (05_gateway.md, "Deadlines"). The thread's event loop runs the
30
+ * pool alone; this module starts it, publishes the environment it reports and closes it.
21
31
  */
22
32
 
23
33
  /** The pools this process serves, by Nx run id, until that run's `postTasksExecution`. */
24
34
  const pools = new Map<string, CpuPool>();
25
35
 
26
36
  /**
27
- * Serve `context`'s run a pool of `min(inherited count, cores)` tokens and publish it in `env`, which every task
28
- * inherits, unless the run inherits a pool. A pool that cannot be made is said, and the run's tasks are then sized only
29
- * by the ceilings they inherit.
37
+ * The pool thread's module, in the form this one was loaded in: compiled JavaScript when Nx loads the plugin, its
38
+ * TypeScript source under Bun. A Bun worker starts without its parent's preloads, so from source the thread takes the
39
+ * transform preload this package's source needs, the one its bunfig names; compiled, it needs none.
40
+ */
41
+ const FROM_SOURCE = import.meta.url.endsWith('.ts');
42
+ const THREAD_ENTRY = new URL(FROM_SOURCE ? './cpu-pool-thread.ts' : './cpu-pool-thread.js', import.meta.url);
43
+ const THREAD_PRELOAD: { readonly preload?: string[] } = FROM_SOURCE
44
+ ? { preload: [fileURLToPath(import.meta.resolve('@smoothbricks/validation/bun/preload'))] }
45
+ : {};
46
+
47
+ type ServedPool = { ok: true; pool: CpuPool } | { ok: false; error: string };
48
+
49
+ /** Where a pool thread is, and who waits on it: its start, its close, or nobody. */
50
+ type ThreadState =
51
+ | { readonly kind: 'starting'; readonly served: (served: ServedPool) => void }
52
+ | { readonly kind: 'serving' }
53
+ | { readonly kind: 'closing'; readonly closed: (outcome: CloseOutcome) => void }
54
+ | { readonly kind: 'ended'; readonly outcome: CloseOutcome };
55
+
56
+ /**
57
+ * The count of the pool a run is served, or null when it is served none. A run that inherits a pool (an enclosing
58
+ * executor's) takes from that one; with mbx, its pool is the host's one ledger, and a run pool beside it would count
59
+ * every leaf twice. Otherwise one token per core, or the inherited positive count when that is fewer.
60
+ */
61
+ export function runPoolTokens(env: Readonly<NodeJS.ProcessEnv>, cores: number): number | null {
62
+ if (readInheritedCpuPool(env) !== null || mbxPool(env) !== null) {
63
+ return null;
64
+ }
65
+ const inherited = parseInheritedTokens(env[CPU_TOKENS_ENV]);
66
+ return inherited === null ? cores : Math.min(inherited, cores);
67
+ }
68
+
69
+ /**
70
+ * Serve `context`'s run a pool of `runPoolTokens` and publish it in `env`, which every task inherits, unless the run
71
+ * is served none. A pool that cannot be made is said, and the run's tasks are then sized only by the ceilings they
72
+ * inherit.
30
73
  */
31
74
  export async function serveRunCpuPool(
32
75
  context: Pick<PreTasksExecutionContext, 'id'>,
33
76
  env: NodeJS.ProcessEnv = process.env,
34
77
  cores: number = availableParallelism(),
35
78
  ): Promise<void> {
36
- if (readInheritedCpuPool(env) !== null) {
79
+ const tokens = runPoolTokens(env, cores);
80
+ if (tokens === null) {
37
81
  return;
38
82
  }
39
- const inherited = parseInheritedTokens(env[CPU_TOKENS_ENV]);
40
- // The pool lives until the run's `postTasksExecution` closes it, or with this process.
41
- const made = await createCpuPool(
42
- inherited === null ? cores : Math.min(inherited, cores),
43
- new Promise<void>(() => {}),
44
- );
45
- if (!made.ok) {
83
+ const served = await servePoolThread(tokens);
84
+ if (!served.ok) {
46
85
  logger.warn(
47
- `bounded-exec: this run's CPU pool failed (${made.error}); its tasks are sized only by the ceilings they inherit`,
86
+ `bounded-exec: this run's CPU pool failed (${served.error}); its tasks are sized only by the ceilings they inherit`,
48
87
  );
49
88
  return;
50
89
  }
51
- pools.set(context.id, made.pool);
52
- for (const [name, value] of Object.entries(made.pool.environment)) {
90
+ pools.set(context.id, served.pool);
91
+ for (const [name, value] of Object.entries(served.pool.environment)) {
53
92
  env[name] = value;
54
93
  }
55
94
  }
56
95
 
96
+ /**
97
+ * Start a pool of `tokens` on a thread of its own, and wait until it serves or says why it cannot. The first `close()`
98
+ * closes the pool and ends the thread; every call returns that close's outcome. A thread that throws or exits before it
99
+ * has said how its pool closed fails whichever wait is open, a later close returns that failure, and a thread lost while
100
+ * its pool serves is said at once.
101
+ */
102
+ function servePoolThread(tokens: number): Promise<ServedPool> {
103
+ const thread = new Worker(THREAD_ENTRY, { workerData: { tokens } satisfies CpuPoolThreadData, ...THREAD_PRELOAD });
104
+ return new Promise<ServedPool>((served) => {
105
+ let state: ThreadState = { kind: 'starting', served };
106
+ let closing: Promise<CloseOutcome> | undefined;
107
+ // The pool is done with its thread once it has said how it ended: nothing of the pool's is left to run there.
108
+ const finish = (outcome: CloseOutcome): ThreadState => {
109
+ const was = state;
110
+ state = { kind: 'ended', outcome };
111
+ void thread.terminate();
112
+ return was;
113
+ };
114
+ const lose = (why: string): void => {
115
+ if (state.kind === 'ended') {
116
+ // The exit that finishing the thread caused.
117
+ return;
118
+ }
119
+ const error = `the CPU pool's thread ${why}`;
120
+ const was = finish({ ok: false, error });
121
+ if (was.kind === 'starting') {
122
+ was.served({ ok: false, error });
123
+ } else if (was.kind === 'closing') {
124
+ was.closed({ ok: false, error });
125
+ } else {
126
+ logger.warn(`bounded-exec: this run's CPU pool stopped serving: ${error}`);
127
+ }
128
+ };
129
+ const close = (): Promise<CloseOutcome> => {
130
+ closing ??=
131
+ state.kind === 'ended'
132
+ ? Promise.resolve(state.outcome)
133
+ : new Promise<CloseOutcome>((closed) => {
134
+ state = { kind: 'closing', closed };
135
+ thread.postMessage(null);
136
+ });
137
+ return closing;
138
+ };
139
+ thread.on('message', (report: CpuPoolThreadReport) => {
140
+ const now = state;
141
+ if (now.kind === 'starting' && report.kind === 'serving') {
142
+ state = { kind: 'serving' };
143
+ now.served({ ok: true, pool: { environment: report.environment, close } });
144
+ } else if (now.kind === 'starting' && report.kind === 'failed') {
145
+ finish({ ok: false, error: report.error });
146
+ now.served({ ok: false, error: report.error });
147
+ } else if (now.kind === 'closing' && report.kind === 'closed') {
148
+ finish(report.outcome);
149
+ now.closed(report.outcome);
150
+ } else {
151
+ lose(`said ${report.kind} while ${now.kind}`);
152
+ }
153
+ });
154
+ thread.on('error', (error) => lose(`failed: ${error instanceof Error ? error.message : String(error)}`));
155
+ thread.on('exit', (code) => lose(`exited with code ${code}`));
156
+ });
157
+ }
158
+
57
159
  /** Close the pool `context`'s run was served, if it was served one. */
58
160
  export async function closeRunCpuPool(context: Pick<PostTasksExecutionContext, 'id'>): Promise<void> {
59
161
  const pool = pools.get(context.id);
@@ -1,5 +1,6 @@
1
1
  import { mkdir, readFile, rename, rm, writeFile } from 'node:fs/promises';
2
2
  import { join } from 'node:path';
3
+ import type { CpuWaitRow } from './lease-wait.js';
3
4
  import { describeHolders, type LockWait, type Opener } from './lock-wait.js';
4
5
 
5
6
  /**
@@ -79,6 +80,12 @@ export interface BoundedExecRecord {
79
80
  readonly want: number;
80
81
  readonly waitedMs: number;
81
82
  };
83
+ /**
84
+ * The parent CPU pool's wait that expired and refused the run: its code, bound and elapsed time, the last heartbeat
85
+ * sequence it read, and the pool's socket and pid (specs/cowshed/05_gateway.md, "Deadlines"). Absent when no wait
86
+ * expired; written for the reader of a refused run, never read back.
87
+ */
88
+ readonly cpuWait?: CpuWaitRow;
82
89
  }
83
90
 
84
91
  /** The Nx task a run executes as. Nx gives an executor no task id, only its parts. */
@@ -350,7 +357,7 @@ export async function withJunitReport(command: string, directory: string): Promi
350
357
  return command.replace(NEXTEST_RUN, () => `nextest run --tool-config-file ${shellQuote(`smoo-report:${config}`)}`);
351
358
  }
352
359
 
353
- function shellQuote(value: string): string {
360
+ export function shellQuote(value: string): string {
354
361
  return `'${value.replaceAll("'", "'\\''")}'`;
355
362
  }
356
363
 
@@ -0,0 +1,24 @@
1
+ /**
2
+ * What the emit changes about the project's own options: JavaScript and declarations from one non-incremental program.
3
+ * They ride the command line onto the project's tsconfig. A synthesized config would have to sit beside that tsconfig,
4
+ * because `${configDir}` and `paths` anchor to the directory of the config that consumes them, and a transient file there
5
+ * changes the directory listing a concurrent ttsc transform of another project proves while it compiles; that
6
+ * transform then refuses with `graph/accessible-entries-changed`.
7
+ *
8
+ * The one definition of how a package's TypeScript dist is emitted: the typescript-emit executor passes these, and so
9
+ * does every bootstrap that emits a dist before Nx can run (tooling/ttsc-emit.ts, the shell entry). A bootstrap that
10
+ * emitted incrementally wrote the tsconfig's tsBuildInfoFile into dist, a file no Nx output produces, and a release
11
+ * refused the verified dist at its merge boundary for it.
12
+ */
13
+ export const EMIT_OVERRIDES: readonly string[] = [
14
+ '--composite',
15
+ 'false',
16
+ '--declaration',
17
+ 'true',
18
+ '--declarationMap',
19
+ 'true',
20
+ '--emitDeclarationOnly',
21
+ 'false',
22
+ '--incremental',
23
+ 'false',
24
+ ];
@@ -15,6 +15,7 @@ import {
15
15
  writeBuildInputs,
16
16
  } from '@smoothbricks/build-inputs';
17
17
  import { readJsonFile } from 'nx/src/devkit-exports.js';
18
+ import { EMIT_OVERRIDES } from './emit-overrides.js';
18
19
  import type { TypeScriptEmitOptions } from './schema.js';
19
20
 
20
21
  interface TypeScriptEmitContext {
@@ -39,26 +40,6 @@ export interface CompilerInvocation {
39
40
 
40
41
  export type CompilerRunner = (invocation: CompilerInvocation) => Promise<boolean>;
41
42
 
42
- /**
43
- * What the emit changes about the project's own options: JavaScript and declarations from one non-incremental program.
44
- * They ride the command line onto the project's tsconfig. A synthesized config would have to sit beside that tsconfig,
45
- * because `${configDir}` and `paths` anchor to the directory of the config that consumes them, and a transient file there
46
- * changes the directory listing a concurrent ttsc transform of another project proves while it compiles; that
47
- * transform then refuses with `graph/accessible-entries-changed`.
48
- */
49
- const EMIT_OVERRIDES = [
50
- '--composite',
51
- 'false',
52
- '--declaration',
53
- 'true',
54
- '--declarationMap',
55
- 'true',
56
- '--emitDeclarationOnly',
57
- 'false',
58
- '--incremental',
59
- 'false',
60
- ];
61
-
62
43
  export default function typescriptEmitExecutor(
63
44
  options: TypeScriptEmitOptions,
64
45
  context: TypeScriptEmitContext,