@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
@@ -1,4 +1,6 @@
1
1
  import { describe, expect, it } from 'bun:test';
2
+ import { spawn } from 'node:child_process';
3
+ import { once } from 'node:events';
2
4
  import {
3
5
  chmodSync,
4
6
  existsSync,
@@ -17,7 +19,9 @@ import { join } from 'node:path';
17
19
  import { fileURLToPath } from 'node:url';
18
20
  import type { OpContextBinding } from '@smoothbricks/lmao';
19
21
  import { useTestSpan } from '@smoothbricks/lmao/testing/bun';
20
- import { pidsWorkingIn, processTable, terminate } from '../../testing.js';
22
+ import { whenChanged } from '../../__tests__/directory-change.js';
23
+ import { BOUNDED_TEST_IDLE_TIMEOUT_MS } from '../../bounded-test-policy.js';
24
+ import { processTable, terminate } from '../../testing.js';
21
25
  import {
22
26
  type CommandOutput,
23
27
  createHostCommands,
@@ -25,6 +29,7 @@ import {
25
29
  leaseProcessesOn,
26
30
  mountsBelow,
27
31
  operationDisk,
32
+ pidsInLease,
28
33
  type RamDisk,
29
34
  type RamTempError,
30
35
  type RamTempPaths,
@@ -121,7 +126,7 @@ async function nativeRamVolume(): Promise<void> {
121
126
  async workingIn(directory) {
122
127
  const startedAt = Date.now();
123
128
  const starting = JSON.stringify({
124
- phase: 'cwd-census-start',
129
+ phase: 'lease-census-start',
125
130
  directory,
126
131
  pid: process.pid,
127
132
  wallAtMs: startedAt,
@@ -130,10 +135,10 @@ async function nativeRamVolume(): Promise<void> {
130
135
  trace.log.info(starting);
131
136
  process.stderr.write(`${starting}\n`);
132
137
  try {
133
- const pids = await pidsWorkingIn(directory);
138
+ const pids = await pidsInLease(directory);
134
139
  const endedAt = Date.now();
135
140
  const finished = JSON.stringify({
136
- phase: 'cwd-census-end',
141
+ phase: 'lease-census-end',
137
142
  directory,
138
143
  pid: process.pid,
139
144
  wallAtMs: endedAt,
@@ -149,7 +154,7 @@ async function nativeRamVolume(): Promise<void> {
149
154
  const details = typeof error === 'object' && error !== null ? error : {};
150
155
  const failed = JSON.stringify(
151
156
  {
152
- phase: 'cwd-census-error',
157
+ phase: 'lease-census-error',
153
158
  directory,
154
159
  pid: process.pid,
155
160
  wallAtMs: endedAt,
@@ -364,17 +369,44 @@ async function nativeRamVolume(): Promise<void> {
364
369
  expect(statSync(join(leaseB.directory, 'tree')).mode & 0o777).toBe(0o555);
365
370
 
366
371
  // Once its owner has detached the image, the next task reclaims the lease, and its release,
367
- // the last, detaches the volume.
372
+ // the last, detaches the volume. The dead task left a writer behind, as a test's ttsc plugin
373
+ // build is left: working elsewhere, writing into the lease through its TMPDIR, with a child.
374
+ // The pid the lease is named after is dead; the writer is stopped before the lease goes, so
375
+ // its removal never meets a file made after the lease was judged dead. A second dead lease,
376
+ // that nothing works in, is reclaimed beside it.
368
377
  attachedBelow.image = null;
369
- const fourth = new RamTempVolume(paths, capacity, commands, 101, (pid) => alive.has(pid), processes);
370
- const d = await fourth.acquire();
371
- if (!d.ok || d.value.kind !== 'leased') {
372
- throw new Error(`fourth lease failed: ${JSON.stringify(d)}`);
378
+ const writer = spawn('sh', ['-c', 'sleep 600 & echo $!; i=0; while :; do i=$((i+1)); : > "$TMPDIR/w$i"; done'], {
379
+ cwd: parent,
380
+ env: { ...process.env, TMPDIR: leaseB.directory },
381
+ detached: true,
382
+ stdio: ['ignore', 'pipe', 'ignore'],
383
+ });
384
+ const writerExited = once(writer, 'close');
385
+ const writerChild = Number(String((await once(writer.stdout, 'data'))[0]).trim());
386
+ const quiet = join(paths.mountpoint, '303-quiet1');
387
+ mkdirSync(quiet);
388
+ writeFileSync(join(quiet, 'left'), 'by a task nothing of which still runs');
389
+ try {
390
+ // Ends on the watch event that creates the writer's `w1` in the lease, not on a poll.
391
+ await whenChanged(leaseB.directory, () => existsSync(join(leaseB.directory, 'w1')));
392
+ const fourth = new RamTempVolume(paths, capacity, commands, 101, (pid) => alive.has(pid), processes);
393
+ const d = await fourth.acquire();
394
+ if (!d.ok || d.value.kind !== 'leased') {
395
+ throw new Error(`fourth lease failed: ${JSON.stringify(d)}`);
396
+ }
397
+ expect(d.value.held).toEqual([]);
398
+ expect(d.value.reaped.map((entry) => Number(entry.split(' ')[0])).sort((a, b) => a - b)).toEqual(
399
+ [writer.pid ?? 0, writerChild].sort((a, b) => a - b),
400
+ );
401
+ expect(existsSync(leaseB.directory)).toBe(false);
402
+ expect(existsSync(quiet)).toBe(false);
403
+ expectUntouched(outside);
404
+ expect(await fourth.release(d.value.lease)).toEqual({ ok: true, value: undefined });
405
+ } finally {
406
+ terminate(writer.pid ?? 0, 'SIGKILL');
407
+ terminate(writerChild, 'SIGKILL');
408
+ await writerExited;
373
409
  }
374
- expect(d.value.held).toEqual([]);
375
- expect(existsSync(leaseB.directory)).toBe(false);
376
- expectUntouched(outside);
377
- expect(await fourth.release(d.value.lease)).toEqual({ ok: true, value: undefined });
378
410
  expect(existsSync(paths.mountpoint)).toBe(false);
379
411
  expect(await attachedDisks(paths, capacity)).toEqual([]);
380
412
  } finally {
@@ -444,11 +476,12 @@ async function aFailedStepLeavesNoNamedVolume(): Promise<void> {
444
476
  (call) => call.file === '/usr/bin/hdiutil' && call.args[0] === 'detach' && call.output.status !== 0,
445
477
  );
446
478
  if (failedDetach !== undefined) {
447
- const mountTable = await host.run('/sbin/mount', [], 1_000);
479
+ // Diagnostic probes explain a failed detach: each waits for the host to answer, bounded by the leg backstop.
480
+ const mountTable = await host.run('/sbin/mount', [], BOUNDED_TEST_IDLE_TIMEOUT_MS);
448
481
  const filesystemHolders = await host.run(
449
482
  '/usr/bin/sudo',
450
483
  ['-n', '/usr/sbin/lsof', '-nP', '+f', '--', paths.staging],
451
- 1_000,
484
+ BOUNDED_TEST_IDLE_TIMEOUT_MS,
452
485
  );
453
486
  const added = calls.find(
454
487
  (call) => call.file === '/usr/sbin/diskutil' && call.args[0] === 'apfs' && call.args[1] === 'addVolume',
@@ -462,7 +495,7 @@ async function aFailedStepLeavesNoNamedVolume(): Promise<void> {
462
495
  const deviceHolders = await host.run(
463
496
  '/usr/bin/sudo',
464
497
  ['-n', '/usr/sbin/lsof', '-nP', '--', ...devicePaths],
465
- 1_000,
498
+ BOUNDED_TEST_IDLE_TIMEOUT_MS,
466
499
  );
467
500
  const observations = JSON.stringify(
468
501
  {
@@ -545,7 +578,7 @@ async function inheritedTempDirectory(): Promise<void> {
545
578
  for (const directory of [inherited, supplied]) {
546
579
  const options = {
547
580
  command: `node ${JSON.stringify(probe)}`,
548
- timeoutMs: 5_000,
581
+ timeoutMs: BOUNDED_TEST_IDLE_TIMEOUT_MS,
549
582
  parallelism: 1,
550
583
  env: {
551
584
  // If the default runner incorrectly provisions an APFS volume here, it must fail,
@@ -588,18 +621,15 @@ describe('temporary-directory policy on this host', () => {
588
621
  it(
589
622
  'reclaims what a dead creator left, shares the volume between leases, and detaches it at the last release, never mounting beside it as "<name> 1"',
590
623
  nativeRamVolume,
591
- 60_000,
592
624
  );
593
625
  it(
594
626
  'leaves no volume under its final name, and no staging directory, when any provisioning step fails',
595
627
  aFailedStepLeavesNoNamedVolume,
596
- 180_000,
597
628
  );
598
629
  } else {
599
630
  it(
600
631
  'keeps inherited temporary storage, honors a target override, and never provisions an APFS disk',
601
632
  inheritedTempDirectory,
602
- 30_000,
603
633
  );
604
634
  }
605
635
  });
@@ -1,12 +1,14 @@
1
1
  import { describe, expect, it } from 'bun:test';
2
2
  import { execFileSync, spawn } from 'node:child_process';
3
3
  import { once } from 'node:events';
4
- import { mkdtempSync, realpathSync, rmSync } from 'node:fs';
4
+ import { mkdirSync, mkdtempSync, readdirSync, realpathSync, rmSync } from 'node:fs';
5
5
  import { mkdir, readFile, rm, writeFile } from 'node:fs/promises';
6
6
  import { createServer, type Socket } from 'node:net';
7
7
  import { tmpdir } from 'node:os';
8
8
  import { join } from 'node:path';
9
9
  import { createInterface } from 'node:readline';
10
+ import { whenChanged } from '../../__tests__/directory-change.js';
11
+ import { BOUNDED_TEST_IDLE_TIMEOUT_MS } from '../../bounded-test-policy.js';
10
12
  import { awaitExit, ownedSocketDirectory, pidsWorkingIn, processTable, terminate } from '../../testing.js';
11
13
  import {
12
14
  COWSHED_EXECUTABLE_ENV,
@@ -179,15 +181,29 @@ async function ownedDiskFacadeFixture() {
179
181
 
180
182
  describe('RAM temp volume on this host', () => {
181
183
  it('kills a deadlined command together with everything it spawned', async () => {
182
- // Ordinary helpers remain deadline-bound; disk-image tools are owned by cowshed instead.
183
- const result = await createHostCommands(500).run('/bin/sh', ['-c', 'sleep 60 & echo $!; wait']);
184
- expect(result.status).not.toBe(0);
185
- expect(result.stderr).toMatch(/^no answer within 500 ms; killed process group \d+$/);
186
- const spawned = result.stdout.trim();
187
- expect(spawned).toMatch(/^\d+$/);
188
- // Gone, or a zombie its new parent has yet to reap: either way no longer running.
189
- const state = await createHostCommands().run('/bin/ps', ['-o', 'stat=', '-p', spawned]);
190
- expect(state.status === 1 || state.stdout.trim().startsWith('Z')).toBe(true);
184
+ // Ordinary helpers remain deadline-bound; disk-image tools are owned by cowshed instead. The command never answers:
185
+ // its shell opens `lifetime`, hands it to a background `tail -f /dev/null` that never ends on its own, closes its
186
+ // own copy and only then prints the tail's pid. So only the deadline ends the run, a printed pid means the FIFO
187
+ // has a writer, and EOF on it is that descendant's death. The bound returns once it has signalled the group, not
188
+ // once the kernel has torn it down, so a `ps` read straight after raced the kill (Linux CI saw it still running).
189
+ const directory = mkdtempSync(join(realpathSync(tmpdir()), 'deadline-group-'));
190
+ try {
191
+ const lifetime = join(directory, 'lifetime.fifo');
192
+ execFileSync('mkfifo', [lifetime]);
193
+ const ended = readFile(lifetime, 'utf8');
194
+ const result = await createHostCommands(500).run('/bin/sh', [
195
+ '-c',
196
+ 'exec 3>"$1"; tail -f /dev/null & exec 3>&-; echo $!; wait',
197
+ 'sh',
198
+ lifetime,
199
+ ]);
200
+ expect(result.status).not.toBe(0);
201
+ expect(result.stderr).toMatch(/^no answer within 500 ms; killed process group \d+$/);
202
+ expect(result.stdout.trim()).toMatch(/^\d+$/);
203
+ expect(await ended).toBe('');
204
+ } finally {
205
+ rmSync(directory, { recursive: true, force: true });
206
+ }
191
207
  });
192
208
 
193
209
  it('routes disk tools and their lifetime together without shell interpolation', () => {
@@ -301,9 +317,9 @@ describe('RAM temp volume on this host', () => {
301
317
  const directory = await ownedSocketDirectory('rt-', 'missing-cowshed');
302
318
  try {
303
319
  const executable = join(directory, 'missing-cowshed');
304
- const output = await createHostCommands(300, undefined, undefined, executable).run('/usr/sbin/diskutil', [
305
- 'info',
306
- ]);
320
+ // The bound is the leg backstop: a missing facade fails at spawn, so the run ends at once.
321
+ const host = createHostCommands(BOUNDED_TEST_IDLE_TIMEOUT_MS, undefined, undefined, executable);
322
+ const output = await host.run('/usr/sbin/diskutil', ['info']);
307
323
  expect(output.status).toBe(1);
308
324
  expect(output.stdout).toBe('');
309
325
  expect(output.stderr).toContain(executable);
@@ -334,7 +350,7 @@ describe('RAM temp volume on this host', () => {
334
350
  server.listen(socket, listened);
335
351
  await listening;
336
352
  server.off('error', failed);
337
- const host = createHostCommands(10_000, socket);
353
+ const host = createHostCommands(BOUNDED_TEST_IDLE_TIMEOUT_MS, socket);
338
354
  // `umount` with no operand only prints its usage, and it is a disk tool that takes a lease.
339
355
  expect((await host.run('/sbin/umount', [])).status).not.toBe(0);
340
356
  expect((await host.run('/sbin/umount', [])).status).not.toBe(0);
@@ -368,7 +384,7 @@ describe('RAM temp volume on this host', () => {
368
384
  server.listen(socket, listened);
369
385
  await listening;
370
386
  server.off('error', failed);
371
- const host = createHostCommands(10_000, socket, 'alone');
387
+ const host = createHostCommands(BOUNDED_TEST_IDLE_TIMEOUT_MS, socket, 'alone');
372
388
  // `umount` with no operand only prints its usage, and it is a disk tool that takes a lease.
373
389
  expect((await host.run('/sbin/umount', [])).status).not.toBe(0);
374
390
  expect((await host.run('/sbin/umount', [])).status).not.toBe(0);
@@ -658,15 +674,75 @@ describe('processes a task left working in its RAM lease', () => {
658
674
  }
659
675
  });
660
676
 
677
+ it('stops what writes into the lease through its TMPDIR from elsewhere, so the lease can be removed', async () => {
678
+ const lease = scratch();
679
+ const elsewhere = scratch();
680
+ // As a dead task's ttsc plugin build leaves it: working in its project, writing into `$TMPDIR`
681
+ // as fast as it can (shell builtins only, so it forks nothing), with a child of its own. macOS
682
+ // spells TMPDIR with a trailing slash.
683
+ const writer = spawn('sh', ['-c', 'sleep 600 & echo $!; i=0; while :; do i=$((i+1)); : > "$TMPDIR/w$i"; done'], {
684
+ cwd: elsewhere,
685
+ env: { ...process.env, TMPDIR: `${lease}/` },
686
+ detached: true,
687
+ stdio: ['ignore', 'pipe', 'ignore'],
688
+ });
689
+ // Another, started on its own with a TMPDIR beneath the lease.
690
+ mkdirSync(join(lease, 'nested'));
691
+ const nested = spawn('sleep', ['600'], {
692
+ cwd: elsewhere,
693
+ env: { ...process.env, TMPDIR: join(lease, 'nested') },
694
+ detached: true,
695
+ stdio: 'ignore',
696
+ });
697
+ const exited = [once(writer, 'close'), once(nested, 'close')];
698
+ const pids = [writer.pid ?? 0, nested.pid ?? 0];
699
+ let child = 0;
700
+ try {
701
+ child = Number(String((await once(writer.stdout, 'data'))[0]).trim());
702
+ // Ends on the watch event that brings the lease to two entries: `nested`, and the writer's first file.
703
+ await whenChanged(lease, () => readdirSync(lease).length >= 2);
704
+ // None of them works in the lease by its working directory.
705
+ expect(await pidsWorkingIn(lease)).toEqual([]);
706
+
707
+ const stopped = await hostLeaseProcesses.stopWorkingIn(lease);
708
+
709
+ expect(stopped.map((entry) => Number(entry.split(' ')[0])).sort((a, b) => a - b)).toEqual(
710
+ [...pids, child].sort((a, b) => a - b),
711
+ );
712
+ expect(await liveAmong([...pids, child])).toEqual([]);
713
+ // Nothing writes there any more: the lease goes whole, with no ENOTEMPTY.
714
+ await rm(lease, { recursive: true });
715
+ } finally {
716
+ for (const pid of [...pids, child]) {
717
+ if (pid !== 0) {
718
+ terminate(pid, 'SIGKILL');
719
+ }
720
+ }
721
+ await Promise.all(exited);
722
+ rmSync(lease, { recursive: true, force: true });
723
+ rmSync(elsewhere, { recursive: true, force: true });
724
+ }
725
+ });
726
+
661
727
  it('stops nothing in a lease no process works in, and nothing that works elsewhere', async () => {
662
728
  const lease = scratch();
663
729
  const elsewhere = scratch();
664
730
  const bystander = spawn('sleep', ['600'], { cwd: elsewhere, detached: true, stdio: 'ignore' });
731
+ // A TMPDIR that only starts with the lease's name is another directory.
732
+ const sibling = spawn('sleep', ['600'], {
733
+ cwd: elsewhere,
734
+ env: { ...process.env, TMPDIR: `${lease}x` },
735
+ detached: true,
736
+ stdio: 'ignore',
737
+ });
665
738
  try {
666
739
  expect(await hostLeaseProcesses.stopWorkingIn(lease)).toEqual([]);
667
- expect(bystander.exitCode === null && bystander.signalCode === null).toBe(true);
740
+ for (const left of [bystander, sibling]) {
741
+ expect(left.exitCode === null && left.signalCode === null).toBe(true);
742
+ }
668
743
  } finally {
669
744
  bystander.kill('SIGKILL');
745
+ sibling.kill('SIGKILL');
670
746
  rmSync(lease, { recursive: true, force: true });
671
747
  rmSync(elsewhere, { recursive: true, force: true });
672
748
  }
@@ -22,6 +22,7 @@ import { setTimeout as sleep } from 'node:timers/promises';
22
22
  import {
23
23
  awaitExit,
24
24
  type ProcessEntry,
25
+ pidsWithTempIn,
25
26
  pidsWorkingIn,
26
27
  processTable,
27
28
  terminate,
@@ -38,6 +39,7 @@ import {
38
39
  parseHeldVerdict,
39
40
  validateHeldVerdict,
40
41
  } from './ram-temp-held.js';
42
+ import type { TempVolumeSpace } from './verdict.js';
41
43
 
42
44
  /**
43
45
  * One RAM-backed APFS volume per host user, shared by every test task that runs
@@ -80,7 +82,11 @@ import {
80
82
  * checks that the volume, the lease directory and its own claim are still as
81
83
  * it claimed them. A lease is a directory named after the task process's pid;
82
84
  * a lease whose pid is gone is reclaimed by the next task, and the volume is
83
- * detached when the last lease ends.
85
+ * detached when the last lease ends. The pid's death proves only that the task
86
+ * process is gone, not what it started: its command runs in a process group of
87
+ * its own, and every process started with the lease as its `TMPDIR` writes
88
+ * there wherever it works. So a dead lease is removed only once every process
89
+ * working in it, by working directory or by `TMPDIR`, has been stopped.
84
90
  *
85
91
  * The volume is created under its final name and never renamed. It is mounted by `mount_apfs` at a
86
92
  * private staging directory and marked there; the kernel then unmounts it (`umount`) and `diskutil mount`, the
@@ -180,16 +186,16 @@ const LEFTOVER_GRACE_MS = 5_000;
180
186
  */
181
187
  export interface LeaseProcesses {
182
188
  /**
183
- * End every process whose working directory is in `directory`, and everything those started,
184
- * and answer each as `<pid> (<command>)`. Every one has exited when this settles; one that
185
- * would not rejects with its name.
189
+ * End every process that works in `directory` — its working directory or the `TMPDIR` it was
190
+ * started with is there — and everything those started, and answer each as `<pid> (<command>)`.
191
+ * Every one has exited when this settles; one that would not rejects with its name.
186
192
  */
187
193
  stopWorkingIn(directory: string): Promise<string[]>;
188
194
  }
189
195
 
190
196
  /** The host reads and signals cleanup uses; a seam for process ordering and signal failures. */
191
197
  export interface ProcessHost {
192
- /** The processes whose working directory is `directory` or lies beneath it. */
198
+ /** The processes whose working directory, or the `TMPDIR` they were started with, is `directory` or lies beneath it. */
193
199
  workingIn(directory: string): Promise<number[]>;
194
200
  /** Every process on the host. */
195
201
  table(): Promise<ProcessEntry[]>;
@@ -201,13 +207,17 @@ const STARTS_NOTHING = /^[TtZ]/;
201
207
 
202
208
  /**
203
209
  * The processes `host` reads. A lease belongs to one task and its command is gone by the time the
204
- * lease ends, so what still works in it is what the command left behind. A fixture's Nx daemon is
205
- * the usual one: Nx detaches it into a session of its own, so the process-group kill that ends a
206
- * timed-out command never reaches it, and a test process that is killed runs no teardown of its
207
- * own. Deleting the lease asks such a daemon to leave only by deleting its record, which it
208
- * notices within a second when it is idle and not at all while it is wedged; a signal does not
209
- * depend on the daemon noticing, and a process left on the volume is what keeps it from
210
- * detaching.
210
+ * lease ends, so what still works in it is what the command left behind. Most of that works in the
211
+ * lease only through `TMPDIR`, never by working directory: a test's ttsc plugin build copies the
212
+ * typescript-go sources into `$TMPDIR` from a process working in its project, and a Bun or Node
213
+ * process writes its compile cache there as it exits. Neither is known to the task's pid, and
214
+ * whichever outlived that pid would write into a lease being removed (measured 2026-10-18: `rm`
215
+ * failing ENOTEMPTY under a ttsc copy whose task had died). A fixture's Nx daemon is another: Nx
216
+ * detaches it into a session of its own, so the process-group kill that ends a timed-out command
217
+ * never reaches it, and a test process that is killed runs no teardown of its own. Deleting the
218
+ * lease asks such a daemon to leave only by deleting its record, which it notices within a second
219
+ * when it is idle and not at all while it is wedged; a signal does not depend on the daemon
220
+ * noticing, and a process left on the volume is what keeps it from detaching.
211
221
  *
212
222
  * A leftover keeps running while it is looked for, and a child it starts after the process table
213
223
  * was read is in no table that was. So each one found is held with SIGSTOP and the table is read
@@ -290,8 +300,14 @@ export function leaseProcessesOn(host: ProcessHost): LeaseProcesses {
290
300
  };
291
301
  }
292
302
 
303
+ /** What works in `directory` on this host: by working directory, and by the `TMPDIR` it was started with. */
304
+ export async function pidsInLease(directory: string): Promise<number[]> {
305
+ const [working, temp] = await Promise.all([pidsWorkingIn(directory), pidsWithTempIn(directory)]);
306
+ return [...new Set([...working, ...temp])];
307
+ }
308
+
293
309
  export const hostLeaseProcesses: LeaseProcesses = leaseProcessesOn({
294
- workingIn: pidsWorkingIn,
310
+ workingIn: pidsInLease,
295
311
  table: processTable,
296
312
  signal: terminate,
297
313
  });
@@ -841,12 +857,24 @@ export class RamTempVolume {
841
857
 
842
858
  /** After a failed task: whether the volume ran out of space, which is then the error to report. */
843
859
  async fullness(lease: RamTempLease): Promise<RamTempError | null> {
860
+ const space = await this.space(lease);
861
+ if (space === null || space.freeBytes >= FULL_FREE_BYTES) {
862
+ return null;
863
+ }
864
+ const { mountpoint, capacityBytes, freeBytes } = space;
865
+ return { kind: 'volume-full', mountpoint, capacityBytes, freeBytes };
866
+ }
867
+
868
+ /** The lease's volume and the space left on it now; `null` when the volume cannot be read. */
869
+ async space(lease: RamTempLease): Promise<TempVolumeSpace | null> {
844
870
  try {
845
871
  const fs = await statfs(lease.mountpoint);
846
- const freeBytes = fs.bavail * fs.bsize;
847
- return freeBytes < FULL_FREE_BYTES
848
- ? { kind: 'volume-full', mountpoint: lease.mountpoint, capacityBytes: lease.capacityBytes, freeBytes }
849
- : null;
872
+ return {
873
+ lease: lease.directory,
874
+ mountpoint: lease.mountpoint,
875
+ capacityBytes: lease.capacityBytes,
876
+ freeBytes: fs.bavail * fs.bsize,
877
+ };
850
878
  } catch {
851
879
  return null;
852
880
  }
@@ -1054,9 +1082,10 @@ export class RamTempVolume {
1054
1082
  }
1055
1083
  const reaped: string[] = [];
1056
1084
  for (const candidate of candidates) {
1057
- // Its task is gone, so what works in it is what that task left: stopped before the lease
1058
- // is removed, even where the removal is refused below. A task reaps its own (`reap`).
1059
- if (!candidate.own && (await readdir(candidate.directory)).length > 0) {
1085
+ // Its task is gone, so what works in it is what that task left: stopped before the lease is
1086
+ // removed, even where the removal is refused below. An empty lease is asked too: a leftover
1087
+ // that has not written yet writes once it runs again. A task reaps its own (`reap`).
1088
+ if (!candidate.own) {
1060
1089
  reaped.push(...(await this.processes.stopWorkingIn(candidate.directory)));
1061
1090
  }
1062
1091
  }