@smoothbricks/nx-plugin 0.4.28 → 0.4.30

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 (158) hide show
  1. package/README.md +277 -89
  2. package/dist/bin/smoo-nx-test-clocks.d.ts +3 -0
  3. package/dist/bin/smoo-nx-test-clocks.d.ts.map +1 -0
  4. package/dist/bin/smoo-nx-test-clocks.js +28 -0
  5. package/dist/bounded-test-policy.d.ts +8 -8
  6. package/dist/bounded-test-policy.d.ts.map +1 -1
  7. package/dist/bounded-test-policy.js +18 -30
  8. package/dist/cargo-closure-input.d.ts +23 -0
  9. package/dist/cargo-closure-input.d.ts.map +1 -1
  10. package/dist/cargo-closure-input.js +41 -9
  11. package/dist/cargo-source-hash.d.ts.map +1 -1
  12. package/dist/cargo-source-hash.js +42 -19
  13. package/dist/ensure-built-receipt.d.ts +40 -6
  14. package/dist/ensure-built-receipt.d.ts.map +1 -1
  15. package/dist/ensure-built-receipt.js +297 -159
  16. package/dist/ensure-built-witness.d.ts +65 -4
  17. package/dist/ensure-built-witness.d.ts.map +1 -1
  18. package/dist/ensure-built-witness.js +2237 -23
  19. package/dist/ensure-built.d.ts +99 -1
  20. package/dist/ensure-built.d.ts.map +1 -1
  21. package/dist/ensure-built.js +45 -56
  22. package/dist/executors/bounded-exec/cpu-pool.d.ts +22 -7
  23. package/dist/executors/bounded-exec/cpu-pool.d.ts.map +1 -1
  24. package/dist/executors/bounded-exec/cpu-pool.js +51 -22
  25. package/dist/executors/bounded-exec/executor.d.ts +12 -3
  26. package/dist/executors/bounded-exec/executor.d.ts.map +1 -1
  27. package/dist/executors/bounded-exec/executor.js +174 -51
  28. package/dist/executors/bounded-exec/no-space.d.ts +8 -0
  29. package/dist/executors/bounded-exec/no-space.d.ts.map +1 -0
  30. package/dist/executors/bounded-exec/no-space.js +31 -0
  31. package/dist/executors/bounded-exec/ram-temp.d.ts +20 -11
  32. package/dist/executors/bounded-exec/ram-temp.d.ts.map +1 -1
  33. package/dist/executors/bounded-exec/ram-temp.js +42 -17
  34. package/dist/executors/bounded-exec/verdict.d.ts +34 -4
  35. package/dist/executors/bounded-exec/verdict.d.ts.map +1 -1
  36. package/dist/executors/bounded-exec/verdict.js +54 -3
  37. package/dist/generators/bun-test-tracing/generator.js +2 -2
  38. package/dist/graph-input-contract.d.ts +102 -0
  39. package/dist/graph-input-contract.d.ts.map +1 -0
  40. package/dist/graph-input-contract.js +637 -0
  41. package/dist/index.d.ts +12 -5
  42. package/dist/index.d.ts.map +1 -1
  43. package/dist/index.js +17 -3
  44. package/dist/managed-files/ci-workflow.d.ts.map +1 -1
  45. package/dist/managed-files/ci-workflow.js +5 -2
  46. package/dist/managed-files/publish-workflow.d.ts +11 -1
  47. package/dist/managed-files/publish-workflow.d.ts.map +1 -1
  48. package/dist/owned-installation.d.ts +14 -0
  49. package/dist/owned-installation.d.ts.map +1 -0
  50. package/dist/owned-installation.js +324 -0
  51. package/dist/producer-receipts.d.ts +19 -0
  52. package/dist/producer-receipts.d.ts.map +1 -0
  53. package/dist/producer-receipts.js +926 -0
  54. package/dist/test-backstop.d.ts +9 -0
  55. package/dist/test-backstop.d.ts.map +1 -0
  56. package/dist/test-backstop.js +10 -0
  57. package/dist/test-clock-policy.d.ts +30 -0
  58. package/dist/test-clock-policy.d.ts.map +1 -0
  59. package/dist/test-clock-policy.js +686 -0
  60. package/dist/testing.d.ts +8 -0
  61. package/dist/testing.d.ts.map +1 -1
  62. package/dist/testing.js +66 -1
  63. package/managed/raw/patches/nx@23.2.1.patch +328 -50
  64. package/managed/raw/tooling/direnv/developer-links.ts +148 -29
  65. package/managed/raw/tooling/direnv/devenv.smoo.nix +7 -7
  66. package/managed/raw/tooling/direnv/github-actions-bootstrap.sh +14 -5
  67. package/managed/raw/tooling/direnv/linked-snapshots.ts +206 -40
  68. package/managed/raw/tooling/direnv/repo-path +15 -2
  69. package/managed/raw/tooling/direnv/setup-environment.ts +179 -115
  70. package/managed/templates/github/actions/cache-node-modules/action.yml +9 -0
  71. package/managed/templates/github/actions/setup-devenv/action.yml +63 -7
  72. package/nextest.toml +1 -1
  73. package/package.json +25 -9
  74. package/src/__tests__/cargo-closure-fixture.ts +113 -0
  75. package/src/__tests__/counted-cargo.ts +8 -25
  76. package/src/__tests__/directory-change.ts +27 -0
  77. package/src/__tests__/fixture-nx-env.ts +7 -0
  78. package/src/__tests__/owned-installation.ts +14 -0
  79. package/src/__tests__/past-tick.ts +12 -0
  80. package/src/bin/smoo-nx-bound-failures.test.ts +1 -1
  81. package/src/bin/smoo-nx-test-clocks.ts +29 -0
  82. package/src/bounded-test-policy.test.ts +36 -22
  83. package/src/bounded-test-policy.ts +18 -32
  84. package/src/bun-bootstrap.test.ts +1 -1
  85. package/src/cargo-closure-input.property.test.ts +50 -0
  86. package/src/cargo-closure-input.test.ts +73 -121
  87. package/src/cargo-closure-input.ts +47 -8
  88. package/src/cargo-closure-key.test.ts +239 -0
  89. package/src/cargo-resolve-cache.test.ts +168 -31
  90. package/src/cargo-source-hash.test.ts +78 -3
  91. package/src/cargo-source-hash.ts +48 -30
  92. package/src/cross-check-policy.test.ts +2 -2
  93. package/src/ensure-built-receipt.test.ts +111 -16
  94. package/src/ensure-built-receipt.ts +294 -149
  95. package/src/ensure-built-witness.test.ts +306 -13
  96. package/src/ensure-built-witness.ts +255 -24
  97. package/src/ensure-built.test.ts +228 -102
  98. package/src/ensure-built.ts +127 -66
  99. package/src/executors/bounded-exec/cpu-pool.test.ts +134 -32
  100. package/src/executors/bounded-exec/cpu-pool.ts +64 -20
  101. package/src/executors/bounded-exec/cpu-tokens.test.ts +2 -2
  102. package/src/executors/bounded-exec/disk-lease.test.ts +47 -13
  103. package/src/executors/bounded-exec/executor-host.test.ts +74 -52
  104. package/src/executors/bounded-exec/executor.test.ts +343 -97
  105. package/src/executors/bounded-exec/executor.ts +192 -56
  106. package/src/executors/bounded-exec/hang-diagnostics.test.ts +14 -8
  107. package/src/executors/bounded-exec/no-space.ts +40 -0
  108. package/src/executors/bounded-exec/progress.test.ts +1 -1
  109. package/src/executors/bounded-exec/ram-temp-host.test.ts +51 -21
  110. package/src/executors/bounded-exec/ram-temp.test.ts +92 -16
  111. package/src/executors/bounded-exec/ram-temp.ts +49 -20
  112. package/src/executors/bounded-exec/verdict.test.ts +127 -22
  113. package/src/executors/bounded-exec/verdict.ts +78 -3
  114. package/src/generators/bounded-test-targets/generator.test.ts +1 -3
  115. package/src/generators/bun-test-tracing/generator.test.ts +2 -2
  116. package/src/generators/bun-test-tracing/generator.ts +2 -2
  117. package/src/graph-input-contract.test.ts +379 -0
  118. package/src/graph-input-contract.ts +627 -0
  119. package/src/index.test.ts +3 -3
  120. package/src/index.ts +24 -3
  121. package/src/linked-snapshots.nx.test.ts +112 -30
  122. package/src/managed-files/ci-workflow.ts +5 -2
  123. package/src/managed-files/publish-workflow.ts +12 -1
  124. package/src/managed-files/repo-path.test.ts +44 -1
  125. package/src/managed-files/setup-devenv-free-disk.test.ts +95 -0
  126. package/src/nextest-code-bound.test.ts +23 -17
  127. package/src/nextest-extraction.test.ts +114 -4
  128. package/src/nextest-partitions.test.ts +7 -1
  129. package/src/nx-cache-restore-mtime.test.ts +2 -2
  130. package/src/nx-daemon-claim.test.ts +10 -8
  131. package/src/nx-daemon-idle-cpu.test.ts +0 -1
  132. package/src/nx-daemon-idle-policy.test.ts +61 -1
  133. package/src/nx-daemon-ignore-files.test.ts +2 -2
  134. package/src/nx-daemon-validity.test.ts +36 -22
  135. package/src/nx-dot-input-policy.test.ts +5 -5
  136. package/src/nx-files-archive-racy.test.ts +1 -1
  137. package/src/nx-incomplete-run-summary.test.ts +1 -1
  138. package/src/nx-plugin-load-errors.test.ts +2 -2
  139. package/src/nx-project-graph-errors.test.ts +70 -0
  140. package/src/nx-skipped-task-exit.test.ts +2 -2
  141. package/src/nx-store-resolution.test.ts +7 -25
  142. package/src/nx-task-output-lease.test.ts +3 -3
  143. package/src/nx-use-local.test.ts +7 -5
  144. package/src/owned-installation.ts +174 -0
  145. package/src/producer-receipts.test.ts +164 -0
  146. package/src/producer-receipts.ts +636 -0
  147. package/src/runtime-input.test.ts +1 -1
  148. package/src/serial-group-wiring.test.ts +4 -4
  149. package/src/task-results.test.ts +18 -16
  150. package/src/test-backstop.ts +11 -0
  151. package/src/test-clock-policy.test.ts +338 -0
  152. package/src/test-clock-policy.ts +708 -0
  153. package/src/test-timings.test.ts +2 -2
  154. package/src/testing.test.ts +5 -5
  155. package/src/testing.ts +67 -1
  156. package/src/typecheck-test-policy.test.ts +1 -1
  157. package/src/typescript-resolution.test.ts +76 -0
  158. package/test-timings.json +8 -0
@@ -25,6 +25,7 @@ export const CPU_POOL_ENV = 'BOUNDED_EXEC_CPU_POOL';
25
25
  /** An inherited pool descriptor; only a grant from its endpoint proves a live allocation. */
26
26
  export interface InheritedCpuPool {
27
27
  readonly socket: string;
28
+ /** The count the parent names beside its endpoint: 0 for a resident's relay pool, which holds no token. */
28
29
  readonly tokens: number;
29
30
  }
30
31
 
@@ -42,21 +43,45 @@ export interface CpuPool {
42
43
 
43
44
  type CloseOutcome = { ok: true } | { ok: false; error: string };
44
45
 
46
+ /**
47
+ * The run's count of descendant work queued for host CPU tokens (specs/cowshed/05_gateway.md, "The bounds start at
48
+ * the grant"). A queued job is not an idle one: its owner stops the run's idle clock while any wait is held. Every
49
+ * pool of one run, its sub-pools included, reports to the same one.
50
+ */
51
+ export interface HostWaits {
52
+ begin(): void;
53
+ end(): void;
54
+ }
55
+
45
56
  /** The one line a child sends: a count of whole tokens, at least one. */
46
57
  interface CpuPoolRequest {
47
58
  op: 'cpu-pool';
48
59
  want: number & typia.tags.Type<'uint32'> & typia.tags.Minimum<1>;
49
60
  /** Ask the owning pool to serve this allocation as its own child pool (Cargo's host launcher). */
50
61
  children?: boolean;
62
+ /**
63
+ * Cargo's launcher asks a compile-only build to grow. This pool lends a fixed count, so it accepts the field and never
64
+ * grows a child; the gateway's pools do (specs/cowshed/05_gateway.md, "A short build grows").
65
+ */
66
+ grow?: boolean;
67
+ }
68
+
69
+ /**
70
+ * A descendant's word that work it started waits in a host CPU queue (a cowshed job the gateway holds `waiting`). It
71
+ * is held like a grant: the wait lasts while its connection does, so a descendant that dies ends its wait.
72
+ */
73
+ interface HostWaitRequest {
74
+ op: 'host-wait';
51
75
  }
52
76
 
53
77
  /** The pool's answers, in `holdLease`'s framing. */
54
78
  type CpuPoolReply =
55
79
  | { ok: true; lease: 'queued' }
56
80
  | { ok: true; lease: 'granted'; tokens: number; pool?: string }
81
+ | { ok: true; wait: 'held' }
57
82
  | { ok: false; code: 'invalid-request'; error: string };
58
83
 
59
- const parseRequest = typia.json.createIsParse<CpuPoolRequest>();
84
+ const parseRequest = typia.json.createIsParse<CpuPoolRequest | HostWaitRequest>();
60
85
  const stringifyReply = typia.json.createStringify<CpuPoolReply>();
61
86
 
62
87
  /** A request over this many bytes is refused as `invalid-request` (specs/cowshed/05_gateway.md, "Wire"). */
@@ -69,11 +94,13 @@ const POOL_DIRECTORY = 'cpu-pool-';
69
94
  const POOL_SOCKET = 'p.sock';
70
95
  const CLOSED_WHILE_MADE = 'the grant that lends this CPU pool closed while the pool was being made';
71
96
 
72
- /** A child's phase. Tokens are owed back only while `holding`; a queued child holds nothing. */
97
+ /** A child's phase. Tokens are owed back only while `holding`; a queued child holds nothing; a `waiting` child holds a
98
+ * host wait on the run's idle clock. */
73
99
  type Phase =
74
100
  | { readonly kind: 'reading' }
75
101
  | { readonly kind: 'queued' }
76
102
  | { readonly kind: 'holding'; readonly tokens: number }
103
+ | { readonly kind: 'waiting' }
77
104
  | { readonly kind: 'done' };
78
105
 
79
106
  /** A child connection and what the pool owes it. Its grant, or its place in line, ends with the connection. */
@@ -94,11 +121,16 @@ const frame = (reply: CpuPoolReply): string => `${stringifyReply(reply)}\n`;
94
121
 
95
122
  /**
96
123
  * The inherited descriptor, or null when malformed or absent. A missing or bare count, or an empty
97
- * or NUL-bearing endpoint, proves no allocation. A valid descriptor still needs a live endpoint grant.
124
+ * or NUL-bearing endpoint, proves no allocation. A resident's relay pool names a count of 0 beside its
125
+ * endpoint: it holds no token for its lifetime and asks the host for each child that asks it, so zero is
126
+ * valid only with the endpoint, and every other count is a positive whole number. A valid descriptor still
127
+ * needs a live endpoint grant (specs/cowshed/05_gateway.md, "Host CPU budget", "A resident holds no token").
98
128
  */
99
129
  export function readInheritedCpuPool(environment: Readonly<NodeJS.ProcessEnv>): InheritedCpuPool | null {
100
130
  const socket = environment[CPU_POOL_ENV];
101
- const tokens = parseInheritedTokens(environment[CPU_TOKENS_ENV]);
131
+ const count = environment[CPU_TOKENS_ENV];
132
+ // Zero is valid only as the literal 0 a relay pool names; any other count must be a positive safe integer.
133
+ const tokens = count === '0' ? 0 : parseInheritedTokens(count);
102
134
  if (socket === undefined || socket.length === 0 || socket.includes('\0') || tokens === null) {
103
135
  return null;
104
136
  }
@@ -106,17 +138,15 @@ export function readInheritedCpuPool(environment: Readonly<NodeJS.ProcessEnv>):
106
138
  }
107
139
 
108
140
  /**
109
- * Ask the immediate parent pool for `min(want, parent.tokens, ceil(cores / 2))` tokens. A granted
110
- * lease is the child's local allocation: its connection is the grant, and closing it returns only
141
+ * Ask the immediate parent pool for `want` tokens. Neither the inherited count nor the host's core count bounds the
142
+ * ask: a lent pool grants at most what it holds, and a gate's or a resident's relay pool asks the host for each
143
+ * child. A granted lease is the child's local allocation: its connection is the grant, and closing it returns only
111
144
  * that allocation. An absent endpoint allows normal host acquisition; a connected endpoint's refusal
112
145
  * or protocol failure must fail admission rather than taking a second grant above its parent.
113
146
  */
114
- export async function takeParentCpuTokens(parent: InheritedCpuPool, want: number, cores: number): Promise<TokenGrant> {
115
- const ask = Math.min(want, parent.tokens, Math.ceil(cores / 2));
116
- if (!Number.isSafeInteger(ask) || ask < 1) {
117
- throw new RangeError(
118
- `a child asks its parent pool for a positive count, not ${ask} (want ${want}, pool ${parent.tokens}, cores ${cores})`,
119
- );
147
+ export async function takeParentCpuTokens(parent: InheritedCpuPool, want: number): Promise<TokenGrant> {
148
+ if (!Number.isSafeInteger(want) || want < 1) {
149
+ throw new RangeError(`a child asks its parent pool for a positive count, not ${want} (pool ${parent.tokens})`);
120
150
  }
121
151
  if (parent.socket.length === 0 || parent.socket.includes('\0')) {
122
152
  return {
@@ -125,7 +155,7 @@ export async function takeParentCpuTokens(parent: InheritedCpuPool, want: number
125
155
  reason: `the parent CPU pool's endpoint ${JSON.stringify(parent.socket)} is not a socket path`,
126
156
  };
127
157
  }
128
- const lease = await holdLease(parent.socket, { op: 'cpu-pool', want: ask }, 'the parent CPU pool', {
158
+ const lease = await holdLease(parent.socket, { op: 'cpu-pool', want }, 'the parent CPU pool', {
129
159
  ackMs: 2_000,
130
160
  afterCloseMs: 5_000,
131
161
  grantMs: null,
@@ -134,12 +164,12 @@ export async function takeParentCpuTokens(parent: InheritedCpuPool, want: number
134
164
  return lease;
135
165
  }
136
166
  const tokens = lease.answer.tokens;
137
- if (typeof tokens !== 'number' || !Number.isInteger(tokens) || tokens < 1 || tokens > ask) {
167
+ if (typeof tokens !== 'number' || !Number.isInteger(tokens) || tokens < 1 || tokens > want) {
138
168
  lease.release();
139
169
  return {
140
170
  granted: false,
141
171
  cause: 'other',
142
- reason: `the parent CPU pool granted ${JSON.stringify(tokens)} tokens for ${ask}`,
172
+ reason: `the parent CPU pool granted ${JSON.stringify(tokens)} tokens for ${want}`,
143
173
  };
144
174
  }
145
175
  return { granted: true, tokens, release: lease.release, closed: lease.closed, inherited: true };
@@ -147,12 +177,14 @@ export async function takeParentCpuTokens(parent: InheritedCpuPool, want: number
147
177
 
148
178
  /**
149
179
  * Make a pool of `tokens` that lives while `closed` does: when `closed` resolves the pool refuses
150
- * grants, ends its child connections and removes its directory. Setup failures are values; a
151
- * non-positive or unsafe count is a programmer bug and throws.
180
+ * grants, ends its child connections and removes its directory. A child's `host-wait` is reported
181
+ * to `waits` for as long as its connection holds it; a sub-pool reports to the same `waits`.
182
+ * Setup failures are values; a non-positive or unsafe count is a programmer bug and throws.
152
183
  */
153
184
  export async function createCpuPool(
154
185
  tokens: number,
155
186
  closed: Promise<void>,
187
+ waits: HostWaits,
156
188
  ): Promise<{ ok: true; pool: CpuPool } | { ok: false; error: string }> {
157
189
  if (!Number.isSafeInteger(tokens) || tokens < 1) {
158
190
  throw new RangeError(`a CPU pool holds a positive safe integer of tokens, not ${tokens}`);
@@ -206,7 +238,7 @@ export async function createCpuPool(
206
238
  if (waiter.child.socket.destroyed) done();
207
239
  else waiter.child.socket.once('close', done);
208
240
  });
209
- const creation = createCpuPool(granted, ended).then(async (made) => {
241
+ const creation = createCpuPool(granted, ended, waits).then(async (made) => {
210
242
  if (!made.ok) {
211
243
  failures.push(made.error);
212
244
  // Keep holding until close credits exactly this allocation once.
@@ -253,6 +285,8 @@ export async function createCpuPool(
253
285
  }
254
286
  } else if (child.phase.kind === 'holding') {
255
287
  free += child.phase.tokens;
288
+ } else if (child.phase.kind === 'waiting') {
289
+ waits.end();
256
290
  }
257
291
  child.phase = { kind: 'done' };
258
292
  drain();
@@ -296,7 +330,7 @@ export async function createCpuPool(
296
330
  }
297
331
  const line = buffered.subarray(0, newline).toString('utf8');
298
332
  buffered = EMPTY;
299
- let request: CpuPoolRequest | null;
333
+ let request: CpuPoolRequest | HostWaitRequest | null;
300
334
  try {
301
335
  request = parseRequest(line);
302
336
  } catch (error) {
@@ -305,7 +339,17 @@ export async function createCpuPool(
305
339
  request = null;
306
340
  }
307
341
  if (request === null) {
308
- refuse(child, 'a request is {"op":"cpu-pool","want":<a whole number of tokens, at least 1>}');
342
+ refuse(
343
+ child,
344
+ 'a request is {"op":"cpu-pool","want":<a whole number of tokens, at least 1>} or {"op":"host-wait"}',
345
+ );
346
+ return;
347
+ }
348
+ if (request.op === 'host-wait') {
349
+ // Counted from here until the connection closes, which `close` reports however it ends.
350
+ child.phase = { kind: 'waiting' };
351
+ waits.begin();
352
+ socket.write(frame({ ok: true, wait: 'held' }));
309
353
  return;
310
354
  }
311
355
  child.phase = { kind: 'queued' };
@@ -54,7 +54,7 @@ describe('cowshed CPU budget client', () => {
54
54
  want: 12,
55
55
  });
56
56
  expect(runnerDemand('cargo nextest run --test-threads=4', undefined, 18, {})).toEqual({ kind: 'nextest', want: 4 });
57
- expect(runnerDemand('bun test --timeout=30000 --parallel=3', undefined, 18, {})).toEqual({
57
+ expect(runnerDemand('bun test --timeout=0 --parallel=3', undefined, 18, {})).toEqual({
58
58
  kind: 'bun-parallel',
59
59
  want: 3,
60
60
  });
@@ -68,7 +68,7 @@ describe('cowshed CPU budget client', () => {
68
68
  want: 5,
69
69
  });
70
70
  // One bun process: `test.concurrent` inside it is not another CPU.
71
- expect(runnerDemand('bun test --timeout=30000 native.test.ts', undefined, 18, {})).toEqual({
71
+ expect(runnerDemand('bun test --timeout=0 native.test.ts', undefined, 18, {})).toEqual({
72
72
  kind: 'process',
73
73
  want: 1,
74
74
  });
@@ -1,22 +1,31 @@
1
- import { afterEach, describe, expect, it } from 'bun:test';
1
+ import { afterEach, describe, expect, it, jest } from 'bun:test';
2
2
  import { rmSync } from 'node:fs';
3
3
  import { mkdtemp, readFile } from 'node:fs/promises';
4
4
  import { createServer, type Server, type Socket } from 'node:net';
5
5
  import { tmpdir } from 'node:os';
6
6
  import { join } from 'node:path';
7
+ import { BOUNDED_TEST_IDLE_TIMEOUT_MS } from '../../bounded-test-policy.js';
7
8
  import { ownedSocketDirectory } from '../../testing.js';
8
9
  import { diskClassOf, takeDiskLease, unixMicros, waitRecord } from './disk-lease.js';
10
+ import { holdLease, type LeaseBounds } from './gateway-lease.js';
9
11
 
10
12
  /**
11
- * Real, short bounds: the client's half-close and grant expiry are socket behavior against the
12
- * platform clock, which fake timers would not drive through a real Unix socket.
13
+ * The leg's backstop for each lease bound these cases are not about. The stand-ins answer at once, refuse at once, or
14
+ * are absent, so no such bound decides a verdict; a case that did wait on one would hang, and the leg's idle timeout
15
+ * ends the run (packages/nx-plugin/README.md, "Bounded Test Targets"). `grantMs: null` waits for as long as the gateway
16
+ * keeps the request queued.
13
17
  */
14
- const QUICK = { ackMs: 200, afterCloseMs: 5_000, grantMs: 500 };
18
+ const BACKSTOP: LeaseBounds = {
19
+ ackMs: BOUNDED_TEST_IDLE_TIMEOUT_MS,
20
+ afterCloseMs: BOUNDED_TEST_IDLE_TIMEOUT_MS,
21
+ grantMs: null,
22
+ };
15
23
 
16
24
  const servers: Server[] = [];
17
25
  const directories: string[] = [];
18
26
 
19
27
  afterEach(() => {
28
+ jest.useRealTimers();
20
29
  for (const server of servers.splice(0)) {
21
30
  server.close();
22
31
  }
@@ -78,7 +87,7 @@ describe('cowshed disk-lifecycle lease client', () => {
78
87
  // after the peer closes until the server ends it, so 'close' is not that signal on Linux.
79
88
  client.on('end', () => close());
80
89
  });
81
- const lease = await takeDiskLease(socket, 'storage', '/usr/bin/hdiutil detach /dev/disk9', QUICK, 'shared');
90
+ const lease = await takeDiskLease(socket, 'storage', '/usr/bin/hdiutil detach /dev/disk9', BACKSTOP, 'shared');
82
91
  expect(await request).toBe(
83
92
  '{"op":"disk-lease","class":"storage","command":"/usr/bin/hdiutil detach /dev/disk9"}\n',
84
93
  );
@@ -90,14 +99,22 @@ describe('cowshed disk-lifecycle lease client', () => {
90
99
  });
91
100
 
92
101
  it('recognizes a gateway that predates leases by its answer to the half-close', async () => {
102
+ // The ack bound is the subject: this gateway answers nothing until it reads EOF, so only the expiry of the ack bound
103
+ // half-closes the request. The bound is armed on connect, right after the request is written, and the read below
104
+ // happens before the advance, so the advance expires an armed ack bound and no other bound is due.
105
+ jest.useFakeTimers();
106
+ const { promise: requested, resolve: sawRequest } = Promise.withResolvers<void>();
93
107
  const socket = await gateway((client) => {
94
- client.resume();
108
+ client.once('data', () => sawRequest());
95
109
  client.on('end', () =>
96
110
  client.end('{"ok":false,"code":"invalid-request","error":"unknown gateway control operation"}\n'),
97
111
  );
98
112
  });
99
- const lease = await takeDiskLease(socket, 'namespace', '/sbin/umount /x', QUICK, 'shared');
100
- expect(lease).toEqual({
113
+ const ackMs = 200;
114
+ const lease = takeDiskLease(socket, 'namespace', '/sbin/umount /x', { ...BACKSTOP, ackMs }, 'shared');
115
+ await requested;
116
+ jest.advanceTimersByTime(ackMs);
117
+ expect(await lease).toEqual({
101
118
  granted: false,
102
119
  cause: 'predates',
103
120
  reason:
@@ -113,7 +130,7 @@ describe('cowshed disk-lifecycle lease client', () => {
113
130
  client.write('{"ok":true,"lease":"queued"}\n{"ok":true,"lease":"granted"}\n');
114
131
  });
115
132
  });
116
- const lease = await takeDiskLease(socket, 'storage', '/usr/bin/hdiutil detach /dev/disk9', QUICK, 'alone');
133
+ const lease = await takeDiskLease(socket, 'storage', '/usr/bin/hdiutil detach /dev/disk9', BACKSTOP, 'alone');
117
134
  expect(await request).toBe(
118
135
  '{"op":"disk-lease","class":"storage","share":"alone","command":"/usr/bin/hdiutil detach /dev/disk9"}\n',
119
136
  );
@@ -131,21 +148,38 @@ describe('cowshed disk-lifecycle lease client', () => {
131
148
  ),
132
149
  );
133
150
  });
134
- const lease = await takeDiskLease(socket, 'storage', 'x', QUICK, 'alone');
151
+ const lease = await takeDiskLease(socket, 'storage', 'x', BACKSTOP, 'alone');
135
152
  expect(lease.granted === false && lease.cause).toBe('predates-field');
136
153
  });
137
154
 
138
155
  it('runs unleased, said as absent, when no gateway listens', async () => {
139
- const lease = await takeDiskLease(await socketPath('none.sock'), 'storage', 'x', QUICK, 'shared');
156
+ const lease = await takeDiskLease(await socketPath('none.sock'), 'storage', 'x', BACKSTOP, 'shared');
140
157
  expect(lease.granted === false && lease.cause).toBe('absent');
141
158
  });
142
159
 
143
160
  it('runs unleased when the grant never comes', async () => {
161
+ // The grant bound is the subject, so this case advances a clock it owns. `holdLease` arms the grant bound when the
162
+ // gateway's `queued` is read, and `sawQueued` runs in that same read, so the advance below expires an armed grant
163
+ // bound and nothing else is due: the grant never comes, so only that expiry ends the wait.
164
+ jest.useFakeTimers();
165
+ const { promise: queued, resolve: sawQueued } = Promise.withResolvers<void>();
144
166
  const socket = await gateway((client) => {
145
167
  client.once('data', () => client.write('{"ok":true,"lease":"queued"}\n'));
146
168
  });
147
- const lease = await takeDiskLease(socket, 'storage', 'x', QUICK, 'shared');
148
- expect(lease).toEqual({ granted: false, cause: 'other', reason: 'the gateway granted nothing within 500 ms' });
169
+ const lease = holdLease(
170
+ socket,
171
+ { op: 'disk-lease', class: 'storage', command: 'x' },
172
+ 'disk leases',
173
+ { ...BACKSTOP, grantMs: 500 },
174
+ () => sawQueued(),
175
+ );
176
+ await queued;
177
+ jest.advanceTimersByTime(500);
178
+ expect(await lease).toEqual({
179
+ granted: false,
180
+ cause: 'other',
181
+ reason: 'the gateway granted nothing within 500 ms',
182
+ });
149
183
  });
150
184
 
151
185
  it('appends a start and an end line per wait, numbered per record and stamped in microseconds', async () => {
@@ -1,10 +1,11 @@
1
1
  import { afterEach, describe, expect, it } from 'bun:test';
2
- import { spawn } from 'node:child_process';
2
+ import { spawn, spawnSync } from 'node:child_process';
3
3
  import { chmod, mkdir, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises';
4
4
  import { createServer, type Socket } from 'node:net';
5
5
  import { tmpdir } from 'node:os';
6
6
  import { basename, dirname, join } from 'node:path';
7
7
  import { fileURLToPath } from 'node:url';
8
+ import { BOUNDED_TEST_IDLE_TIMEOUT_MS, BOUNDED_TEST_KILL_AFTER_MS } from '../../bounded-test-policy.js';
8
9
  import { CPU_POOL_ENV, type CpuPool, createCpuPool } from './cpu-pool.js';
9
10
  import { CPU_TOKENS_ENV } from './cpu-tokens.js';
10
11
  import type { BoundedExecOptions } from './schema.js';
@@ -12,7 +13,7 @@ import { VERDICT_FILE } from './verdict.js';
12
13
 
13
14
  /**
14
15
  * The production runner's built Node artifact, run in its own process, with the real RAM volume and CPU-budget owners.
15
- * `test-host` already depends on this package's build; these peers must not compile TypeScript inside a test's guard.
16
+ * `test-host` already depends on this package's build; these peers must not compile TypeScript inside a test.
16
17
  * These proofs need the host (`hdiutil` on darwin), so the pure shards exclude this file.
17
18
  */
18
19
  const PACKAGE_ROOT = fileURLToPath(new URL('../../..', import.meta.url));
@@ -37,8 +38,11 @@ const ACTIVE_RESOURCES = 'cowshed-test active-resources ';
37
38
  const CHURN_NAMES = ['churna', 'churnb'] as const;
38
39
  /** Written into a case's root to stop its churn workers; each checks it between runs, never mid-run. */
39
40
  const CHURN_STOP = 'churn.stop';
40
- /** One churn run: `true` on one token, so each lease is a real acquire, run and release. */
41
- const CHURN_OPTIONS: BoundedExecOptions = { command: 'true', timeoutMs: 20_000, parallelism: 1 };
41
+ /**
42
+ * One churn run: `true` on one token, so each lease is a real acquire, run and release. Its `timeoutMs` is the leg
43
+ * backstop: a run ends on its own exit, and only a hung run reaches the bound (`specs/cowshed/08_testing.md:511-514`).
44
+ */
45
+ const CHURN_OPTIONS: BoundedExecOptions = { command: 'true', timeoutMs: BOUNDED_TEST_IDLE_TIMEOUT_MS, parallelism: 1 };
42
46
  /** A churn worker's stdout: a `begin` line before each run it starts, a `ran` line after each run that succeeded. */
43
47
  const CHURN_BEGIN = /^cowshed-test churn [a-z]+ begin [0-9]+$/gm;
44
48
  const CHURN_RAN = /^cowshed-test churn [a-z]+ ran [0-9]+$/gm;
@@ -60,7 +64,9 @@ interface ChildRun {
60
64
  askResources(): void;
61
65
  /** Resolves once `marker` has been printed to stdout; rejects if the child exits without printing it. */
62
66
  printed(marker: string): Promise<void>;
63
- /** Stops a surviving runner after its test-owned pools have revoked all borrowed commands. */
67
+ /** Resolves once `marker` has been written to stderr; rejects if the child exits without writing it. */
68
+ said(marker: string): Promise<void>;
69
+ /** Stops a surviving runner with SIGTERM, which it forwards to its command's process tree, and awaits its exit. */
64
70
  stop(): Promise<void>;
65
71
  }
66
72
 
@@ -93,16 +99,16 @@ const children = new Set<ChildRun>();
93
99
 
94
100
  describe('@smoothbricks/nx-plugin:bounded-exec host proofs', () => {
95
101
  afterEach(async () => {
96
- // A child still open here was not awaited by its case: its guard fired, or the case failed before awaiting it. Its
97
- // cumulative output is written before anything waits, so a hook timeout below cannot lose it. Each live runner is
102
+ // A child still open here was not awaited by its case: the case failed before awaiting it. Its cumulative output is
103
+ // written before anything waits, so a kill of the run during the waits below cannot lose it. Each live runner is
98
104
  // also asked, by SIGUSR2, what Node still has active; the answer lands in its stdout before the child closes.
99
105
  const live = [...children].filter((child) => !child.closed());
100
106
  for (const child of live) {
101
- process.stderr.write(attribution(child, 'at guard teardown'));
107
+ process.stderr.write(attribution(child, 'before teardown'));
102
108
  child.askResources();
103
109
  }
104
110
 
105
- // Pools close first: a command still running under a closed pool is stopped as a lost grant, so its runner exits.
111
+ // Pools close first, then every runner still open is stopped: a closed grant never stops a command on its own.
106
112
  const outcomes = await Promise.all([...pools].map((pool) => pool.close()));
107
113
  pools.clear();
108
114
  await Promise.all([...children].map((child) => child.stop()));
@@ -126,7 +132,7 @@ describe('@smoothbricks/nx-plugin:bounded-exec host proofs', () => {
126
132
  const leafScript = await writeRunner(
127
133
  root,
128
134
  'leaf',
129
- { command: `node -e "console.log('leaf')"`, timeoutMs: 20_000, parallelism: 1 },
135
+ { command: `node -e "console.log('leaf')"`, timeoutMs: BOUNDED_TEST_IDLE_TIMEOUT_MS, parallelism: 1 },
130
136
  leafDirectory,
131
137
  );
132
138
  const nestedDirectory = join(root, 'nested');
@@ -136,7 +142,7 @@ describe('@smoothbricks/nx-plugin:bounded-exec host proofs', () => {
136
142
  const run = await startRunner(
137
143
  root,
138
144
  'nested',
139
- { command: nestedCommand, cwd: PACKAGE_ROOT, timeoutMs: 20_000, parallelism: 2 },
145
+ { command: nestedCommand, cwd: PACKAGE_ROOT, timeoutMs: BOUNDED_TEST_IDLE_TIMEOUT_MS, parallelism: 2 },
140
146
  nestedDirectory,
141
147
  childEnv(pool.environment),
142
148
  );
@@ -159,7 +165,7 @@ describe('@smoothbricks/nx-plugin:bounded-exec host proofs', () => {
159
165
  }
160
166
  await expectChurn(churn);
161
167
  });
162
- }, 30_000);
168
+ });
163
169
 
164
170
  it('admits five concurrent RAM leases without blocking the lock holder behind filesystem workers', async () => {
165
171
  const root = await createRoot();
@@ -173,7 +179,7 @@ describe('@smoothbricks/nx-plugin:bounded-exec host proofs', () => {
173
179
  `const options = ${JSON.stringify({
174
180
  command: `node -e "console.log('lease')"`,
175
181
  cwd: PACKAGE_ROOT,
176
- timeoutMs: 20_000,
182
+ timeoutMs: BOUNDED_TEST_IDLE_TIMEOUT_MS,
177
183
  parallelism: 1,
178
184
  })};`,
179
185
  `const context = ${JSON.stringify({ root })};`,
@@ -192,7 +198,7 @@ describe('@smoothbricks/nx-plugin:bounded-exec host proofs', () => {
192
198
  expect(run.stdout()).toContain('leases-complete');
193
199
  expect([...run.stderr().matchAll(/inherited=true tokens=1\/1/g)]).toHaveLength(5);
194
200
  });
195
- }, 30_000);
201
+ });
196
202
 
197
203
  it('holds a parent pool to its count: three siblings on a pool of two never hold more than two', async () => {
198
204
  const root = await createRoot();
@@ -207,7 +213,11 @@ describe('@smoothbricks/nx-plugin:bounded-exec host proofs', () => {
207
213
  startRunner(
208
214
  root,
209
215
  name,
210
- { command: siblingCommand(barrier.port, name), timeoutMs: 20_000, parallelism: 1 },
216
+ {
217
+ command: siblingCommand(barrier.port, name),
218
+ timeoutMs: BOUNDED_TEST_IDLE_TIMEOUT_MS,
219
+ parallelism: 1,
220
+ },
211
221
  join(root, name),
212
222
  childEnv(pool.environment),
213
223
  ),
@@ -256,7 +266,7 @@ describe('@smoothbricks/nx-plugin:bounded-exec host proofs', () => {
256
266
  await barrier.close();
257
267
  }
258
268
  });
259
- }, 30_000);
269
+ });
260
270
 
261
271
  it('uses a real backing budget after its named pool is absent and returns the allocation', async () => {
262
272
  const root = await createRoot();
@@ -264,7 +274,7 @@ describe('@smoothbricks/nx-plugin:bounded-exec host proofs', () => {
264
274
  await withPool(1, async (outer) => {
265
275
  const options: BoundedExecOptions = {
266
276
  command: `node -e "console.log('actual-tokens=' + process.env.${CPU_TOKENS_ENV})"`,
267
- timeoutMs: 20_000,
277
+ timeoutMs: BOUNDED_TEST_IDLE_TIMEOUT_MS,
268
278
  parallelism: 1,
269
279
  };
270
280
  const directory = join(root, 'stale');
@@ -274,7 +284,6 @@ describe('@smoothbricks/nx-plugin:bounded-exec host proofs', () => {
274
284
  script,
275
285
  [
276
286
  `import assert from 'node:assert/strict';`,
277
- `import { availableParallelism } from 'node:os';`,
278
287
  `import { runBoundedExec, createProcessTreeKiller } from ${JSON.stringify(EXECUTOR)};`,
279
288
  `import { readInheritedCpuPool, takeParentCpuTokens } from ${JSON.stringify(join(dirname(EXECUTOR), 'cpu-pool.js'))};`,
280
289
  RESOURCES_HANDLER,
@@ -288,7 +297,7 @@ describe('@smoothbricks/nx-plugin:bounded-exec host proofs', () => {
288
297
  'let released = 0;',
289
298
  'const budget = { async take(want, checkout, command) {',
290
299
  ' asked.push({ want, checkout, command });',
291
- ' const grant = await takeParentCpuTokens(backing, want, availableParallelism());',
300
+ ' const grant = await takeParentCpuTokens(backing, want);',
292
301
  ' assert(grant.granted, grant.reason);',
293
302
  ' allocations.push(grant);',
294
303
  ' return { ...grant, release() { released++; grant.release(); } };',
@@ -298,7 +307,7 @@ describe('@smoothbricks/nx-plugin:bounded-exec host proofs', () => {
298
307
  'assert.deepEqual(asked, [{ want: 1, checkout: context.root, command: options.command }]);',
299
308
  'assert.equal(released, 1);',
300
309
  'await allocations[0].closed;',
301
- 'const returned = await takeParentCpuTokens(backing, 1, availableParallelism());',
310
+ 'const returned = await takeParentCpuTokens(backing, 1);',
302
311
  'assert(returned.granted, returned.reason);',
303
312
  'assert.equal(returned.tokens, 1);',
304
313
  'returned.release();',
@@ -316,31 +325,36 @@ describe('@smoothbricks/nx-plugin:bounded-exec host proofs', () => {
316
325
  expect(run.stdout()).not.toContain('977');
317
326
  expect(await readFile(join(directory, VERDICT_FILE), 'utf8')).toMatch(/"tokens":\s*1/);
318
327
  });
319
- }, 30_000);
328
+ });
320
329
 
321
- it('stops a command whose parent pool closes under it, and fails its run', async () => {
330
+ it('keeps a command running when its parent pool closes under it, and judges it by its own exit', async () => {
322
331
  const root = await createRoot();
332
+ const go = join(root, 'go.fifo');
333
+ expect(spawnSync('mkfifo', [go]).status).toBe(0);
323
334
  await withPool(2, async (pool) => {
324
335
  const run = await startRunner(
325
336
  root,
326
- 'orphan',
337
+ 'outlives',
327
338
  {
328
- command: `node -e "console.log('started'); require('node:http').createServer().listen(0)"`,
329
- timeoutMs: 30_000,
330
- killAfterMs: 2_000,
339
+ command: `sh -c 'echo started; read -r line < "$0"; echo finished' '${go}'`,
340
+ timeoutMs: BOUNDED_TEST_IDLE_TIMEOUT_MS,
341
+ killAfterMs: BOUNDED_TEST_KILL_AFTER_MS,
331
342
  parallelism: 1,
332
343
  },
333
- join(root, 'orphan'),
344
+ join(root, 'outlives'),
334
345
  childEnv(pool.environment),
335
346
  );
336
347
  await run.printed('started');
337
348
  const outcome = await pool.close();
338
349
  expect(outcome.ok).toBe(true);
339
- expect(await run.exited, run.stderr()).toBe(1);
340
- expect(run.stderr()).toContain('cowshed: cpu-tokens grant lost');
341
- expect(await readFile(join(root, 'orphan', VERDICT_FILE), 'utf8')).toMatch(/"outcome":\s*"failed"/);
350
+ await run.said('cowshed: cpu-tokens grant ended: the parent pool closed its 1-token grant');
351
+ expect(run.closed()).toBe(false);
352
+ await writeFile(go, 'go\n');
353
+ expect(await run.exited, run.stderr()).toBe(0);
354
+ expect(run.stdout()).toContain('finished');
355
+ expect(await readFile(join(root, 'outlives', VERDICT_FILE), 'utf8')).toMatch(/"outcome":\s*"passed"/);
342
356
  });
343
- }, 30_000);
357
+ });
344
358
 
345
359
  it('fails a run whose sub-pool does not close cleanly, even when its command exits 0', async () => {
346
360
  const root = await createRoot();
@@ -350,7 +364,7 @@ describe('@smoothbricks/nx-plugin:bounded-exec host proofs', () => {
350
364
  const unremovable = await startRunner(
351
365
  root,
352
366
  'unremovable',
353
- { command: UNREMOVABLE_COMMAND, timeoutMs: 20_000, parallelism: 1 },
367
+ { command: UNREMOVABLE_COMMAND, timeoutMs: BOUNDED_TEST_IDLE_TIMEOUT_MS, parallelism: 1 },
354
368
  join(root, 'unremovable'),
355
369
  childEnv(pool.environment),
356
370
  );
@@ -365,7 +379,11 @@ describe('@smoothbricks/nx-plugin:bounded-exec host proofs', () => {
365
379
  const next = await startRunner(
366
380
  root,
367
381
  'next',
368
- { command: `node -e "console.log(process.env.${CPU_TOKENS_ENV})"`, timeoutMs: 20_000, parallelism: 1 },
382
+ {
383
+ command: `node -e "console.log(process.env.${CPU_TOKENS_ENV})"`,
384
+ timeoutMs: BOUNDED_TEST_IDLE_TIMEOUT_MS,
385
+ parallelism: 1,
386
+ },
369
387
  join(root, 'next'),
370
388
  childEnv(pool.environment),
371
389
  );
@@ -379,7 +397,7 @@ describe('@smoothbricks/nx-plugin:bounded-exec host proofs', () => {
379
397
  await rm(dirname(stuck), { recursive: true, force: true });
380
398
  }
381
399
  }
382
- }, 30_000);
400
+ });
383
401
  });
384
402
 
385
403
  async function createRoot(): Promise<string> {
@@ -399,9 +417,10 @@ function childEnv(extra: Readonly<Record<string, string>>): NodeJS.ProcessEnv {
399
417
  return { ...env, ...extra };
400
418
  }
401
419
 
402
- /** A parent's CPU pool of `tokens`, open while `body` runs. Its upstream never closes on its own. */
420
+ /** A parent's CPU pool of `tokens`, open while `body` runs. Its upstream never closes on its own, and no run owns the
421
+ * host waits its children report. */
403
422
  async function withPool<T>(tokens: number, body: (pool: CpuPool) => Promise<T>): Promise<T> {
404
- const created = await createCpuPool(tokens, new Promise<void>(() => {}));
423
+ const created = await createCpuPool(tokens, new Promise<void>(() => {}), { begin() {}, end() {} });
405
424
  if (!created.ok) {
406
425
  throw new Error(`cpu pool: ${created.error}`);
407
426
  }
@@ -488,6 +507,7 @@ function startChild(script: string, env: NodeJS.ProcessEnv): ChildRun {
488
507
  });
489
508
  child.stderr.on('data', (chunk: Buffer) => {
490
509
  err += chunk.toString('utf8');
510
+ wake();
491
511
  });
492
512
  const exited = new Promise<number | null>((resolve) => {
493
513
  const done = (code: number | null): void => {
@@ -501,6 +521,21 @@ function startChild(script: string, env: NodeJS.ProcessEnv): ChildRun {
501
521
  });
502
522
  child.once('close', (code) => done(code));
503
523
  });
524
+ const appeared = (text: () => string, marker: string, what: string): Promise<void> => {
525
+ const { promise, resolve, reject } = Promise.withResolvers<void>();
526
+ const check = (): void => {
527
+ if (text().includes(marker)) {
528
+ waiters.delete(check);
529
+ resolve();
530
+ } else if (closed) {
531
+ waiters.delete(check);
532
+ reject(new Error(`child exited before ${what} ${JSON.stringify(marker)}; stderr:\n${err}`));
533
+ }
534
+ };
535
+ waiters.add(check);
536
+ check();
537
+ return promise;
538
+ };
504
539
  const run: ChildRun = {
505
540
  label: basename(script),
506
541
  exited,
@@ -510,21 +545,8 @@ function startChild(script: string, env: NodeJS.ProcessEnv): ChildRun {
510
545
  askResources(): void {
511
546
  child.kill('SIGUSR2');
512
547
  },
513
- printed(marker: string): Promise<void> {
514
- return new Promise<void>((resolve, reject) => {
515
- const check = (): void => {
516
- if (out.includes(marker)) {
517
- waiters.delete(check);
518
- resolve();
519
- } else if (closed) {
520
- waiters.delete(check);
521
- reject(new Error(`child exited before printing ${JSON.stringify(marker)}; stderr:\n${err}`));
522
- }
523
- };
524
- waiters.add(check);
525
- check();
526
- });
527
- },
548
+ printed: (marker) => appeared(() => out, marker, 'printing'),
549
+ said: (marker) => appeared(() => err, marker, 'writing to stderr'),
528
550
  async stop(): Promise<void> {
529
551
  if (!closed) child.kill('SIGTERM');
530
552
  await exited;
@@ -536,7 +558,7 @@ function startChild(script: string, env: NodeJS.ProcessEnv): ChildRun {
536
558
 
537
559
  /**
538
560
  * What a runner has printed and written so far, named by its script and the stage it is attributed at. Cumulative and
539
- * untrimmed: a guard flap says what the runner was doing, not just that it was late.
561
+ * untrimmed: a runner still open at teardown says what it was doing, not just that it never finished.
540
562
  */
541
563
  function attribution(child: ChildRun, stage: string): string {
542
564
  return [