@smoothbricks/nx-plugin 0.4.29 → 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 (95) hide show
  1. package/README.md +83 -15
  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 +1 -8
  6. package/dist/bounded-test-policy.d.ts.map +1 -1
  7. package/dist/bounded-test-policy.js +2 -8
  8. package/dist/cargo-closure-input.d.ts +11 -0
  9. package/dist/cargo-closure-input.d.ts.map +1 -1
  10. package/dist/cargo-closure-input.js +16 -6
  11. package/dist/ensure-built-receipt.d.ts +8 -2
  12. package/dist/ensure-built-receipt.d.ts.map +1 -1
  13. package/dist/ensure-built-receipt.js +45 -13
  14. package/dist/ensure-built-witness.d.ts +40 -4
  15. package/dist/ensure-built-witness.d.ts.map +1 -1
  16. package/dist/ensure-built-witness.js +68 -15
  17. package/dist/ensure-built.d.ts.map +1 -1
  18. package/dist/ensure-built.js +11 -3
  19. package/dist/executors/bounded-exec/cpu-pool.d.ts +7 -3
  20. package/dist/executors/bounded-exec/cpu-pool.d.ts.map +1 -1
  21. package/dist/executors/bounded-exec/cpu-pool.js +9 -4
  22. package/dist/executors/bounded-exec/executor.d.ts +1 -1
  23. package/dist/executors/bounded-exec/executor.d.ts.map +1 -1
  24. package/dist/executors/bounded-exec/executor.js +35 -12
  25. package/dist/executors/bounded-exec/no-space.d.ts +8 -0
  26. package/dist/executors/bounded-exec/no-space.d.ts.map +1 -0
  27. package/dist/executors/bounded-exec/no-space.js +31 -0
  28. package/dist/executors/bounded-exec/ram-temp.d.ts +3 -0
  29. package/dist/executors/bounded-exec/ram-temp.d.ts.map +1 -1
  30. package/dist/executors/bounded-exec/ram-temp.js +15 -4
  31. package/dist/executors/bounded-exec/verdict.d.ts +34 -4
  32. package/dist/executors/bounded-exec/verdict.d.ts.map +1 -1
  33. package/dist/executors/bounded-exec/verdict.js +54 -3
  34. package/dist/graph-input-contract.d.ts +4 -0
  35. package/dist/graph-input-contract.d.ts.map +1 -1
  36. package/dist/graph-input-contract.js +17 -4
  37. package/dist/owned-installation.d.ts +14 -0
  38. package/dist/owned-installation.d.ts.map +1 -0
  39. package/dist/owned-installation.js +324 -0
  40. package/dist/producer-receipts.d.ts.map +1 -1
  41. package/dist/producer-receipts.js +15 -10
  42. package/dist/test-backstop.d.ts +9 -0
  43. package/dist/test-backstop.d.ts.map +1 -0
  44. package/dist/test-backstop.js +10 -0
  45. package/dist/test-clock-policy.d.ts +30 -0
  46. package/dist/test-clock-policy.d.ts.map +1 -0
  47. package/dist/test-clock-policy.js +686 -0
  48. package/dist/testing.d.ts.map +1 -1
  49. package/dist/testing.js +8 -1
  50. package/managed/raw/tooling/direnv/developer-links.ts +1 -8
  51. package/managed/raw/tooling/direnv/setup-environment.ts +0 -1
  52. package/package.json +15 -4
  53. package/src/__tests__/cargo-closure-fixture.ts +113 -0
  54. package/src/__tests__/counted-cargo.ts +8 -25
  55. package/src/__tests__/directory-change.ts +27 -0
  56. package/src/__tests__/owned-installation.ts +4 -164
  57. package/src/__tests__/past-tick.ts +12 -0
  58. package/src/bin/smoo-nx-test-clocks.ts +29 -0
  59. package/src/bounded-test-policy.ts +2 -8
  60. package/src/cargo-closure-input.property.test.ts +50 -0
  61. package/src/cargo-closure-input.test.ts +15 -112
  62. package/src/cargo-closure-input.ts +23 -6
  63. package/src/cargo-closure-key.test.ts +239 -0
  64. package/src/cargo-resolve-cache.test.ts +29 -7
  65. package/src/ensure-built-receipt.ts +56 -13
  66. package/src/ensure-built-witness.test.ts +113 -4
  67. package/src/ensure-built-witness.ts +75 -16
  68. package/src/ensure-built.test.ts +74 -2
  69. package/src/ensure-built.ts +13 -2
  70. package/src/executors/bounded-exec/cpu-pool.test.ts +18 -0
  71. package/src/executors/bounded-exec/cpu-pool.ts +10 -4
  72. package/src/executors/bounded-exec/disk-lease.test.ts +47 -13
  73. package/src/executors/bounded-exec/executor-host.test.ts +23 -11
  74. package/src/executors/bounded-exec/executor.test.ts +189 -63
  75. package/src/executors/bounded-exec/executor.ts +36 -11
  76. package/src/executors/bounded-exec/hang-diagnostics.test.ts +14 -8
  77. package/src/executors/bounded-exec/no-space.ts +40 -0
  78. package/src/executors/bounded-exec/ram-temp-host.test.ts +9 -8
  79. package/src/executors/bounded-exec/ram-temp.test.ts +32 -18
  80. package/src/executors/bounded-exec/ram-temp.ts +17 -4
  81. package/src/executors/bounded-exec/verdict.test.ts +125 -25
  82. package/src/executors/bounded-exec/verdict.ts +78 -3
  83. package/src/graph-input-contract.test.ts +8 -0
  84. package/src/graph-input-contract.ts +26 -4
  85. package/src/nextest-extraction.test.ts +114 -4
  86. package/src/nx-daemon-claim.test.ts +5 -3
  87. package/src/nx-daemon-idle-policy.test.ts +22 -18
  88. package/src/nx-daemon-validity.test.ts +5 -13
  89. package/src/owned-installation.ts +174 -0
  90. package/src/producer-receipts.ts +15 -10
  91. package/src/test-backstop.ts +11 -0
  92. package/src/test-clock-policy.test.ts +338 -0
  93. package/src/test-clock-policy.ts +708 -0
  94. package/src/testing.ts +8 -1
  95. package/test-timings.json +3 -0
@@ -1,7 +1,16 @@
1
1
  import { spawnSync } from 'node:child_process';
2
2
  import { createHash } from 'node:crypto';
3
- import { closeSync, lstatSync, openSync, readdirSync, readlinkSync, readSync, realpathSync } from 'node:fs';
4
- import { isAbsolute, join } from 'node:path';
3
+ import {
4
+ closeSync,
5
+ lstatSync,
6
+ openSync,
7
+ readdirSync,
8
+ readFileSync,
9
+ readlinkSync,
10
+ readSync,
11
+ realpathSync,
12
+ } from 'node:fs';
13
+ import { isAbsolute, join, relative } from 'node:path';
5
14
  import type { Task } from 'nx/src/config/task-graph';
6
15
  import type { expandOutputs, HashInputs } from 'nx/src/native';
7
16
  import { CARGO_TOOLCHAIN_PIN_INPUTS, cargoRuntimeInput } from './cargo-toolchain-policy.js';
@@ -12,7 +21,7 @@ import type {
12
21
  EnsureBuiltProducerReceipt,
13
22
  EnsureBuiltReceipt,
14
23
  } from './ensure-built.js';
15
- import { movedNodes, type NodeWitness, nodeStamp, nodesUnchanged } from './ensure-built-witness.js';
24
+ import { censusStart, movedNodes, type NodeWitness, recordedStamp } from './ensure-built-witness.js';
16
25
 
17
26
  export type ReceiptEvidence<T> =
18
27
  | { readonly ok: true; readonly value: T }
@@ -130,10 +139,17 @@ function sourceFileDigests(
130
139
  }
131
140
  return { gitBlob: blob.digest('hex'), sha256: raw.digest('hex') };
132
141
  }
133
- /** One independently read node; absolute paths also serve tool/package closures. */
134
- export function receiptNode(path: string, name: string, observed: NodeWitness[]): EnsureBuiltOutputReceipt {
142
+ /**
143
+ * One independently read node; absolute paths also serve tool/package closures. A census records the node's stamp as
144
+ * of its `start` (recordedStamp); a re-read against a record records none.
145
+ */
146
+ export function receiptNode(
147
+ path: string,
148
+ name: string,
149
+ census?: { readonly start: bigint; readonly observed: NodeWitness[] },
150
+ ): EnsureBuiltOutputReceipt {
135
151
  const stats = lstatSync(path, { bigint: true });
136
- observed.push({ path, stamp: nodeStamp(stats) });
152
+ census?.observed.push({ path, stamp: recordedStamp(stats, census.start) });
137
153
  const mode = Number(stats.mode & 0o7777n);
138
154
  if (stats.isFile())
139
155
  return { path: name, type: 'file', mode, sha256: hashFile(path, Number(stats.size), ['sha256'], false)[0] };
@@ -150,6 +166,18 @@ export function receiptNode(path: string, name: string, observed: NodeWitness[])
150
166
  }
151
167
  throw new Error(`Unsupported receipt node ${path}`);
152
168
  }
169
+ /**
170
+ * Whether a tracked source node whose stamp does not prove it (its ctime alone moved, or it was read in the tick it
171
+ * changed in) still holds the bytes or link text `files` recorded for it.
172
+ */
173
+ function recordedSource(root: string, files: readonly EnsureBuiltProducerFileInput[]): (path: string) => boolean {
174
+ const recorded = new Map(files.map((file) => [join(root, file.path), file.sha256]));
175
+ return (path) =>
176
+ recorded.get(path) ===
177
+ createHash('sha256')
178
+ .update(lstatSync(path).isSymbolicLink() ? readlinkSync(path) : readFileSync(path))
179
+ .digest('hex');
180
+ }
153
181
  /** Capture every tracked source byte at pre. Unrelated untracked files are not source basis. */
154
182
  export function captureProducerSource(
155
183
  root: string,
@@ -171,6 +199,7 @@ export function captureProducerSource(
171
199
  const excluded = new Set(outputs.filter((path) => !graphConfiguration(path)));
172
200
  const nodes: NodeWitness[] = [];
173
201
  const files: EnsureBuiltProducerFileInput[] = [];
202
+ const start = censusStart();
174
203
  for (const entry of listed.value.split('\0')) {
175
204
  if (entry === '') continue;
176
205
  const tab = entry.indexOf('\t');
@@ -182,7 +211,7 @@ export function captureProducerSource(
182
211
  if (excluded.has(file)) continue;
183
212
  const path = join(root, file);
184
213
  const stats = lstatSync(path, { bigint: true });
185
- nodes.push({ path, stamp: nodeStamp(stats) });
214
+ nodes.push({ path, stamp: recordedStamp(stats, start) });
186
215
  const actualMode = stats.isSymbolicLink() ? '120000' : (stats.mode & 0o111n) !== 0n ? '100755' : '100644';
187
216
  if (actualMode !== mode) return { ok: false, detail: `Tracked source mode differs from HEAD: ${file}` };
188
217
  let gitBlob: string;
@@ -207,7 +236,7 @@ export function captureProducerSource(
207
236
  const dirty = dirtyProviderPaths(status.value).filter((path) => !excluded.has(path));
208
237
  if (dirty.length !== 0)
209
238
  return { ok: false, detail: `Tracked source/index differs from ${head}: ${dirty.join(', ')}` };
210
- const moved = movedNodes(nodes);
239
+ const moved = movedNodes(nodes, recordedSource(root, files));
211
240
  if (moved !== null) return { ok: false, detail: `Tracked source moved during producer pre capture: ${moved}` };
212
241
  return { ok: true, value: { head, headTree: tree.value.trim(), files, nodes, outputs } };
213
242
  } catch (error) {
@@ -228,7 +257,7 @@ export function producerSourceUnchanged(root: string, before: ProducerSourceWitn
228
257
  return { ok: false, detail: `${changed}: HEAD's tree is ${tree.value.trim()}, not ${before.headTree}` };
229
258
  const dirty = dirtyProviderPaths(status.value).filter((path) => !outputs.has(path));
230
259
  if (dirty.length !== 0) return { ok: false, detail: `${changed}: ${dirty.join(', ')} differ from HEAD` };
231
- const moved = movedNodes(before.nodes);
260
+ const moved = movedNodes(before.nodes, recordedSource(root, before.files));
232
261
  if (moved !== null) return { ok: false, detail: `${changed}: ${moved}` };
233
262
  return { ok: true, value: true };
234
263
  }
@@ -276,11 +305,13 @@ export function providerReceipt(
276
305
  if (!lstatSync(join(root, file)).isFile()) return failed(`Unbound linked/non-file Nx source: ${file}`);
277
306
  }
278
307
  const observed: NodeWitness[] = [];
308
+ const census = { start: censusStart(), observed };
309
+ const parents = new Set<string>();
279
310
  const outputs = new Map<string, EnsureBuiltOutputReceipt>();
280
311
  function visit(path: string): void {
281
312
  if (outputs.has(path)) return;
282
313
  const absolute = join(root, path);
283
- let node = receiptNode(absolute, path, observed);
314
+ let node = receiptNode(absolute, path, census);
284
315
  if (node.type === 'directory') {
285
316
  for (const entry of node.entries) visit(join(path, entry));
286
317
  const children = node.entries.map((entry) => outputs.get(join(path, entry)));
@@ -300,7 +331,8 @@ export function providerReceipt(
300
331
  for (const segment of entry.split('/').slice(0, -1)) {
301
332
  parent = join(parent, segment);
302
333
  const stats = lstatSync(parent, { bigint: true });
303
- observed.push({ path: parent, stamp: nodeStamp(stats) });
334
+ observed.push({ path: parent, stamp: recordedStamp(stats, census.start) });
335
+ parents.add(parent);
304
336
  if (!stats.isDirectory()) return failed(`Symlinked public output parent ${parent}`);
305
337
  }
306
338
  visit(entry);
@@ -315,8 +347,19 @@ export function providerReceipt(
315
347
  });
316
348
  }
317
349
  const final = producerSourceUnchanged(root, source);
318
- if (!final.ok || !nodesUnchanged(observed))
319
- return failed(final.ok ? 'Public outputs changed during independent census' : final.detail);
350
+ if (!final.ok) return failed(final.detail);
351
+ // An output whose stamp does not prove it (its ctime alone moved, or it was read in the tick it changed in, as a
352
+ // task's last writes are) is re-read against its record. A parent directory binds only the real directory the path
353
+ // runs through, which the device, inode and mode its kept stamp still show.
354
+ const moved = movedNodes(observed, (path) => {
355
+ const record = outputs.get(relative(root, path));
356
+ if (record === undefined) return parents.has(path);
357
+ const now = receiptNode(path, record.path);
358
+ return record.type === 'directory' && now.type === 'directory'
359
+ ? record.mode === now.mode && JSON.stringify(record.entries) === JSON.stringify(now.entries)
360
+ : JSON.stringify(record) === JSON.stringify(now);
361
+ });
362
+ if (moved !== null) return failed(`Public outputs changed during independent census: ${moved}`);
320
363
  const outputSha256 = createHash('sha256').update(JSON.stringify(receipts)).digest('hex');
321
364
  return {
322
365
  disposition: 'hit',
@@ -1,6 +1,6 @@
1
1
  import { afterEach, beforeEach, describe, expect, it } from 'bun:test';
2
2
  import { createHash } from 'node:crypto';
3
- import { chmodSync, lstatSync, writeFileSync } from 'node:fs';
3
+ import { chmodSync, lstatSync, readFileSync, utimesSync, writeFileSync } from 'node:fs';
4
4
  import {
5
5
  chmod,
6
6
  mkdir,
@@ -16,12 +16,14 @@ import {
16
16
  } from 'node:fs/promises';
17
17
  import { tmpdir } from 'node:os';
18
18
  import { join } from 'node:path';
19
+ import fc from 'fast-check';
19
20
  import type { ProjectGraph } from 'nx/src/config/project-graph';
20
21
  import { expandOutputs, WorkspaceContext } from 'nx/src/native';
21
22
  import { workspaceDataDirectoryForWorkspace } from 'nx/src/utils/cache-directory';
22
23
  import type { readModulePackageJsonWithoutFallbacks } from 'nx/src/utils/package-json';
23
24
  import typia from 'typia';
24
25
  import {
26
+ censusStart,
25
27
  type GraphWitness,
26
28
  graphInstallationIdentity,
27
29
  movedNodes,
@@ -29,8 +31,11 @@ import {
29
31
  nodeStamp,
30
32
  nodesUnchanged,
31
33
  readGraphWitness,
34
+ recordedStamp,
32
35
  reusableGraph,
36
+ SAME_TICK_NS,
33
37
  snapshotsEqual,
38
+ stampMove,
34
39
  workspaceSnapshot,
35
40
  writeWitness,
36
41
  } from './ensure-built-witness.js';
@@ -151,6 +156,53 @@ describe('authoritative graph witness', () => {
151
156
  });
152
157
  });
153
158
 
159
+ // A filesystem dates a change by the kernel's clock, and ZFS on Linux by its coarse tick (1 ms on CI's runner), so a
160
+ // same-size rewrite in the tick a stamp was read in leaves the whole stamp as it was: git's racily clean entries. No
161
+ // call sets a ctime, so the tick is constructed rather than raced for: each write is dated ahead with utimes, so every
162
+ // census reads it inside its change's tick however long the census takes, and the rewrite keeps that date and size.
163
+ // On a coarse clock the rewrite then leaves the stamp as recorded in most rounds, and a reader that trusts it reads
164
+ // the node as unchanged; on a fine one (APFS, ext4) its ctime still moves, and only there would trusting it pass.
165
+ describe('a stamp read in the tick its node changed in', () => {
166
+ const rounds = 16;
167
+ const source = 'packages/app/source.txt';
168
+
169
+ it('is re-hashed by the next capture, which a same-size rewrite in that tick would otherwise escape', () => {
170
+ const path = join(root, source);
171
+ const context = new WorkspaceContext(root, data());
172
+ const hashOf = ({ files }: GraphWitness['snapshot']) => files.find(({ file }) => file === source)?.hash;
173
+ for (let round = 0; round < rounds; round++) {
174
+ const ahead = new Date(Date.now() + 60_000);
175
+ writeFileSync(path, `before ${round}`);
176
+ utimesSync(path, ahead, ahead);
177
+ const recorded = workspaceSnapshot(root, context, undefined);
178
+ writeFileSync(path, `after! ${round}`);
179
+ utimesSync(path, ahead, ahead);
180
+ const current = workspaceSnapshot(root, context, recorded);
181
+ // Its own stability check compares what it read; only a later capture distrusts the stamp.
182
+ expect(recorded.stable).toBe(true);
183
+ expect(hashOf(current)).not.toBe(hashOf(recorded));
184
+ }
185
+ });
186
+
187
+ it('is recorded unproven, so a reader re-reads the node and finds the rewrite', () => {
188
+ const path = join(root, 'stamped');
189
+ const reread = (node: string) => readFileSync(node, 'utf8') === 'before';
190
+ for (let round = 0; round < rounds; round++) {
191
+ const ahead = new Date(Date.now() + 60_000);
192
+ writeFileSync(path, 'before');
193
+ utimesSync(path, ahead, ahead);
194
+ const recorded: NodeWitness[] = [
195
+ { path, stamp: recordedStamp(lstatSync(path, { bigint: true }), censusStart()) },
196
+ ];
197
+ expect(movedNodes(recorded, reread)).toBeNull();
198
+ writeFileSync(path, 'after!');
199
+ utimesSync(path, ahead, ahead);
200
+ expect(nodesUnchanged(recorded)).toBe(false);
201
+ expect(movedNodes(recorded, reread)).toBe(path);
202
+ }
203
+ });
204
+ });
205
+
154
206
  describe('runtime graph witness storage', () => {
155
207
  it.each(['relative', 'absolute'])(
156
208
  'uses the %s workspace-data binding for missing and unreadable witnesses',
@@ -478,12 +530,19 @@ describe('installed graph input identity', () => {
478
530
  it('names every moved node, and re-reads only one whose ctime alone moved', async () => {
479
531
  const file = join(root, 'stamped');
480
532
  await writeFile(file, 'before');
481
- const witness = (): NodeWitness[] => [{ path: file, stamp: nodeStamp(lstatSync(file, { bigint: true })) }];
482
- const chmodded = witness();
533
+ // A filesystem that stamps from the kernel's coarse tick (ZFS on Linux: 1 ms) keeps the stamp of a chmod or a
534
+ // same-size rewrite that lands in the tick of the last change. No call sets a ctime, so the file is dated a second
535
+ // back and recorded as it was then, when its ctime was its mtime: each change below moves it on every filesystem.
536
+ const past = new Date(Date.now() - 1000);
537
+ await utimes(file, past, past);
538
+ const recorded = lstatSync(file, { bigint: true });
539
+ const chmodded: NodeWitness[] = [
540
+ { path: file, stamp: nodeStamp(Object.assign(recorded, { ctimeNs: recorded.mtimeNs })) },
541
+ ];
483
542
  chmodSync(file, (await stat(file)).mode & 0o7777);
484
543
  expect(movedNodes(chmodded)).toBe(file);
485
544
  expect(movedNodes(chmodded, () => true)).toBeNull();
486
- const written = witness();
545
+ const written: NodeWitness[] = [{ path: file, stamp: nodeStamp(lstatSync(file, { bigint: true })) }];
487
546
  await writeFile(file, 'after!');
488
547
  expect(movedNodes(written, () => true)).toBe(file);
489
548
  await rm(file);
@@ -526,3 +585,53 @@ describe('installed graph input identity', () => {
526
585
  ).toBeNull();
527
586
  });
528
587
  });
588
+
589
+ // The classification every tool-node reader shares, over generated stat fields rather than a filesystem: no clock
590
+ // tick, timestamp granularity or concurrent install decides these verdicts, so they are the same on every platform.
591
+ describe('a stamp move is judged by the two stamps alone', () => {
592
+ const u64 = fc.bigInt({ min: 0n, max: 2n ** 64n - 1n });
593
+ const fields = fc.record({ dev: u64, ino: u64, mode: u64, size: u64, mtimeNs: u64, ctimeNs: u64 });
594
+ const identity = fc.constantFrom('dev', 'ino', 'mode', 'size', 'mtimeNs');
595
+
596
+ it('reads a node whose stamp is unchanged as the same node', () => {
597
+ fc.assert(fc.property(fields, (stats) => stampMove(nodeStamp(stats), nodeStamp(stats)) === 'same'));
598
+ });
599
+
600
+ it('reads a move of the ctime alone as a ctime move, in either direction and at any distance', () => {
601
+ fc.assert(
602
+ fc.property(fields, u64, (stats, ctimeNs) => {
603
+ fc.pre(ctimeNs !== stats.ctimeNs);
604
+ return stampMove(nodeStamp(stats), nodeStamp({ ...stats, ctimeNs })) === 'ctime';
605
+ }),
606
+ );
607
+ });
608
+
609
+ it('reads a move of device, inode, mode, size or mtime as moved, whatever the ctime did', () => {
610
+ fc.assert(
611
+ fc.property(fields, identity, u64, u64, (stats, field, value, ctimeNs) => {
612
+ fc.pre(value !== stats[field]);
613
+ return stampMove(nodeStamp(stats), nodeStamp({ ...stats, [field]: value, ctimeNs })) === 'moved';
614
+ }),
615
+ );
616
+ });
617
+
618
+ // The racily clean rule: a stamp read within SAME_TICK_NS of its node's change (the later of ctime and mtime) proves
619
+ // nothing, and only such a stamp costs a reader a re-read.
620
+ it('records a stamp as read exactly when its census started a full tick after its node changed', () => {
621
+ fc.assert(
622
+ fc.property(fields, u64, (stats, start) => {
623
+ const proven = start - (stats.ctimeNs > stats.mtimeNs ? stats.ctimeNs : stats.mtimeNs) >= SAME_TICK_NS;
624
+ return proven === (recordedStamp(stats, start) === nodeStamp(stats));
625
+ }),
626
+ );
627
+ });
628
+
629
+ it('makes every reader re-read an unproven stamp, whatever the ctime of the node it reads', () => {
630
+ fc.assert(
631
+ fc.property(fields, u64, u64, (stats, start, ctimeNs) => {
632
+ fc.pre(recordedStamp(stats, start) !== nodeStamp(stats));
633
+ return stampMove(recordedStamp(stats, start), nodeStamp({ ...stats, ctimeNs })) === 'ctime';
634
+ }),
635
+ );
636
+ });
637
+ });
@@ -26,11 +26,64 @@ interface InstalledPackageManifest {
26
26
  readonly packageJson: PackageJson;
27
27
  }
28
28
 
29
- /** Identity of a previously read node, not a timestamp freshness threshold. */
30
- export function nodeStamp(stats: BigIntStats): string {
29
+ /** The stat fields a node's identity is read from. */
30
+ export type StampFields = Pick<BigIntStats, 'dev' | 'ino' | 'mode' | 'size' | 'mtimeNs' | 'ctimeNs'>;
31
+
32
+ /** The stamp a node reads as now: device, inode, mode, size, mtime and ctime. What a recorded stamp is compared with. */
33
+ export function nodeStamp(stats: StampFields): string {
31
34
  return `${stats.dev}:${stats.ino}:${stats.mode}:${stats.size}:${stats.mtimeNs}:${stats.ctimeNs}`;
32
35
  }
33
36
 
37
+ /**
38
+ * How an observed stamp compares with the recorded one: `same`; `ctime` when device, inode, mode, size and mtime are
39
+ * kept and only the ctime moved (a same-mode chmod, which Bun's bin linking does to a shared store file on any
40
+ * checkout's install) or was recorded `unproven` ({@link recordedStamp}); otherwise `moved`. A function of the two
41
+ * stamps alone, so no filesystem or clock decides it.
42
+ */
43
+ export function stampMove(recorded: string, observed: string): 'same' | 'ctime' | 'moved' {
44
+ if (observed === recorded) return 'same';
45
+ // nodeStamp ends with the ctime: everything before it is the identity a same-mode chmod keeps.
46
+ const kept = observed.slice(0, observed.lastIndexOf(':')) === recorded.slice(0, recorded.lastIndexOf(':'));
47
+ return kept ? 'ctime' : 'moved';
48
+ }
49
+
50
+ /**
51
+ * How long after a node's last change a stamp read of it proves nothing about the next change: git's racily clean
52
+ * rule (git's Documentation/technical/racy-git.txt), which the Nx archive repair already applies to Nx's own file
53
+ * hashes (patches/README.md). A filesystem dates a change by the kernel's clock. On Linux, on a filesystem without
54
+ * fine-grained timestamps such as ZFS, that is the coarse clock, which moves once a jiffy: 1 ms at CONFIG_HZ=1000, as
55
+ * on CI's runner, and 10 ms at CONFIG_HZ=100, the slowest. A same-size rewrite in the tick a stamp was read in leaves
56
+ * every field of it as it was. Node can read neither the jiffy nor a filesystem's granularity, and a shorter window
57
+ * reopens the gap on a slower kernel, so this is the slowest tick, and no option.
58
+ */
59
+ export const SAME_TICK_NS = 10_000_000n;
60
+
61
+ /** The time a census starts, read before its first stat: what {@link unproven} judges each node's change by. */
62
+ export function censusStart(): bigint {
63
+ return BigInt(Date.now()) * 1_000_000n;
64
+ }
65
+
66
+ /**
67
+ * Whether a census that started at `start` read a node within {@link SAME_TICK_NS} of its last change (the later of
68
+ * its ctime and mtime, so a node dated ahead counts). Its stamp is then unproven, not unchanged: a rewrite in the same
69
+ * tick could leave it as it is.
70
+ */
71
+ export function unproven(stats: StampFields, start: bigint): boolean {
72
+ return start - (stats.ctimeNs > stats.mtimeNs ? stats.ctimeNs : stats.mtimeNs) < SAME_TICK_NS;
73
+ }
74
+
75
+ /**
76
+ * The stamp a census that started at `start` records for a later reader: its {@link nodeStamp}, or, when that is
77
+ * {@link unproven}, the same with its ctime recorded as `unproven`. No stamp read later equals that, so the reader
78
+ * re-reads the node rather than trust the stamp, as it does a node whose ctime alone moved ({@link stampMove}), or
79
+ * counts it moved when it holds nothing to re-read it against. Only such a node costs a re-read.
80
+ */
81
+ export function recordedStamp(stats: StampFields, start: bigint): string {
82
+ return unproven(stats, start)
83
+ ? `${stats.dev}:${stats.ino}:${stats.mode}:${stats.size}:${stats.mtimeNs}:unproven`
84
+ : nodeStamp(stats);
85
+ }
86
+
34
87
  export interface NodeWitness {
35
88
  readonly path: string;
36
89
  readonly stamp: string;
@@ -75,36 +128,44 @@ export function writeWitness(path: string, value: GraphWitness | ArtifactWitness
75
128
  * Nx's Unix archive uses whole-second mtimes, so allFileData alone cannot
76
129
  * prove an immediate same-second rewrite. An unbound archive hash is never
77
130
  * admitted: on the first capture every file is rehashed through Nx itself.
131
+ * The nodes are recorded for later captures and readers (recordedStamp), so a
132
+ * node this capture read in the tick it changed in is rehashed by the next.
78
133
  */
79
134
  export function workspaceSnapshot(
80
135
  workspaceRoot: string,
81
136
  context: WorkspaceContext,
82
137
  previous: WorkspaceSnapshot | undefined,
83
138
  ): WorkspaceSnapshot {
139
+ const start = censusStart();
84
140
  const files = context.allFileData();
85
141
  const prior = new Map(previous?.nodes.map((node) => [node.path, node.stamp]));
86
142
  const nodes: NodeWitness[] = [];
143
+ // What this capture read of each node it records unproven: its own stability check compares what it read.
144
+ const read = new Map<string, string>();
87
145
  const changed: string[] = [];
88
146
  let complete = true;
147
+ function record(path: string, stats: BigIntStats): string {
148
+ const stamp = nodeStamp(stats);
149
+ if (unproven(stats, start)) read.set(path, stamp);
150
+ nodes.push({ path, stamp: read.has(path) ? recordedStamp(stats, start) : stamp });
151
+ return stamp;
152
+ }
89
153
  for (const { file } of files) {
90
154
  const path = join(workspaceRoot, file);
91
155
  const stats = lstatSync(path, { bigint: true });
92
- const node = { path, stamp: nodeStamp(stats) };
93
- nodes.push(node);
94
- let moved = prior.get(path) !== node.stamp;
156
+ let moved = prior.get(path) !== record(path, stats);
95
157
  if (stats.isSymbolicLink()) {
96
158
  const physical = realpathSync(path);
97
159
  const targetStats = lstatSync(physical, { bigint: true });
98
- const target = { path: physical, stamp: nodeStamp(targetStats) };
99
- nodes.push(target);
100
- moved ||= prior.get(physical) !== target.stamp;
160
+ moved ||= prior.get(physical) !== record(physical, targetStats);
101
161
  // A file table does not prove the membership of an external directory.
102
162
  if (!targetStats.isFile()) complete = false;
103
163
  }
104
164
  if (moved) changed.push(file);
105
165
  }
106
166
  if (changed.length > 0) context.incrementalUpdate(changed, []);
107
- return { files: context.allFileData(), nodes, stable: complete && nodesUnchanged(nodes) };
167
+ const asRead = read.size === 0 ? nodes : nodes.map(({ path, stamp }) => ({ path, stamp: read.get(path) ?? stamp }));
168
+ return { files: context.allFileData(), nodes, stable: complete && nodesUnchanged(asRead) };
108
169
  }
109
170
 
110
171
  export function nodesUnchanged(nodes: readonly NodeWitness[]): boolean {
@@ -123,8 +184,8 @@ export function nodesUnchanged(nodes: readonly NodeWitness[]): boolean {
123
184
  * The witnessed nodes whose stamp is no longer the one read, named for a refusal (the first eight in witness order, a
124
185
  * gone one with its error code, and how many more), or null when none moved. A node whose device, inode, mode, size
125
186
  * and mtime are unchanged and only its ctime moved (a same-mode chmod, which Bun's bin linking does to a shared store
126
- * file on any checkout's install) asks for a comparison, not a verdict: `same` re-reads it, and one it finds as
127
- * recorded has not changed. Without `same` every move counts.
187
+ * file on any checkout's install), or whose ctime was recorded unproven ({@link recordedStamp}), asks for a comparison,
188
+ * not a verdict: `same` re-reads it, and one it finds as recorded has not changed. Without `same` every move counts.
128
189
  */
129
190
  export function movedNodes(
130
191
  nodes: readonly NodeWitness[],
@@ -133,11 +194,9 @@ export function movedNodes(
133
194
  const moved: string[] = [];
134
195
  for (const node of nodes) {
135
196
  try {
136
- const stamp = nodeStamp(lstatSync(node.path, { bigint: true }));
137
- if (stamp === node.stamp) continue;
138
- // nodeStamp ends with the ctime: everything before it is the identity a same-mode chmod keeps.
139
- const kept = stamp.slice(0, stamp.lastIndexOf(':')) === node.stamp.slice(0, node.stamp.lastIndexOf(':'));
140
- if (!kept || !same(node.path)) moved.push(node.path);
197
+ const move = stampMove(node.stamp, nodeStamp(lstatSync(node.path, { bigint: true })));
198
+ if (move === 'same') continue;
199
+ if (move === 'moved' || !same(node.path)) moved.push(node.path);
141
200
  } catch (error) {
142
201
  if (!systemError(error)) throw error;
143
202
  moved.push(`${node.path} (${error.code})`);
@@ -13,6 +13,7 @@ import { signalToCode } from 'nx/src/utils/exit-codes';
13
13
  import typia from 'typia';
14
14
  import { nxFixtureRoot, withNxFixture } from './__tests__/fixture-nx-env.js';
15
15
  import { testFileInstallation } from './__tests__/owned-installation.js';
16
+ import { pastTick } from './__tests__/past-tick.js';
16
17
  import type { EnsureBuiltProbeResult } from './ensure-built.js';
17
18
  import {
18
19
  assignEnvironment,
@@ -246,7 +247,9 @@ describe('unvouched', () => {
246
247
  });
247
248
 
248
249
  describe('compareWithArtifacts', () => {
249
- it('reuses a verified two-tree inventory without expanding or rereading artifact bytes', () => {
250
+ it('reuses a verified two-tree inventory without expanding or rereading artifact bytes', async () => {
251
+ // An inventory recorded in the tick its fixture was written in is unproven, and the next comparison is a full one.
252
+ await pastTick();
250
253
  const path = join(root, 'verified.json');
251
254
  let walks = 0;
252
255
  const expand: typeof expandOutputs = (...args) => {
@@ -1007,12 +1010,25 @@ await writeFile(${JSON.stringify(resultsPath)}, JSON.stringify(results));
1007
1010
  { encoding: 'utf8' },
1008
1011
  );
1009
1012
  expect(sourceCommit.status, sourceCommit.stdout + sourceCommit.stderr).toBe(0);
1013
+ // The receipt binds the producer's executables. A `git` this fixture owns lets the consumer case below move
1014
+ // one tool node's ctime alone, as a same-mode chmod by another checkout's install moves a shared store file.
1015
+ const realGit = Bun.which('git');
1016
+ if (realGit === null) throw new Error('git is not on PATH');
1017
+ const fixtureBin = join(root, 'bin');
1018
+ await mkdir(fixtureBin);
1019
+ const fixtureGit = join(fixtureBin, 'git');
1020
+ await writeExecutable(fixtureGit, `#!/bin/sh\nexec ${JSON.stringify(realGit)} "$@"\n`);
1010
1021
  const published = await run(
1011
1022
  'node',
1012
1023
  nxEntry(workspace),
1013
1024
  workspace,
1014
1025
  ['run', 'app:build', '--outputStyle=static-failures-only'],
1015
- { NX_DAEMON: 'true', NX_WORKSPACE_ROOT_PATH: workspace, ...ownEnv },
1026
+ {
1027
+ NX_DAEMON: 'true',
1028
+ NX_WORKSPACE_ROOT_PATH: workspace,
1029
+ ...ownEnv,
1030
+ PATH: `${fixtureBin}:${process.env.PATH ?? ''}`,
1031
+ },
1016
1032
  );
1017
1033
  expect(published.code, published.stdout + published.stderr).toBe(0);
1018
1034
  expect(await readFile(builds, 'utf-8')).toBe('lib\n');
@@ -1111,6 +1127,62 @@ await new Promise<void>((resolve, reject) => process.stdout.write(JSON.stringify
1111
1127
  expect(environmentResults.sameTree).toEqual(environmentResults.first);
1112
1128
  expect(environmentResults.moduleInspections).toBe(0);
1113
1129
  expect(await readFile(builds, 'utf-8')).toBe('lib\n');
1130
+
1131
+ // Between its readonly read of the receipt's tools and its last check of them, the consumer censuses the
1132
+ // public outputs through Nx's expandOutputs. Moving the fixture `git` there is a tool node moving under the
1133
+ // readonly validation: a same-mode chmod moves its ctime alone and keeps the hit, a byte change refuses and
1134
+ // names the node.
1135
+ const toolMoveEntry = join(root, 'tool-node-move.ts');
1136
+ await writeFile(
1137
+ toolMoveEntry,
1138
+ `
1139
+ import { chmodSync, statSync, writeFileSync } from 'node:fs';
1140
+ import { createRequire } from 'node:module';
1141
+ import { probeBuilt } from ${JSON.stringify(join(packageRoot, 'dist', 'ensure-built.js'))};
1142
+ const git = ${JSON.stringify(fixtureGit)};
1143
+ const native = createRequire(process.cwd() + '/package.json')('nx/src/native');
1144
+ const expand = native.expandOutputs;
1145
+ let move = () => {};
1146
+ let moves = 0;
1147
+ native.expandOutputs = (...args) => {
1148
+ move();
1149
+ moves += 1;
1150
+ return expand(...args);
1151
+ };
1152
+ const before = statSync(git, { bigint: true });
1153
+ move = () => chmodSync(git, 0o755);
1154
+ const chmodded = await probeBuilt({ target: 'app:build', cwd: process.cwd() });
1155
+ const after = statSync(git, { bigint: true });
1156
+ const ctimeAlone = before.ino === after.ino && before.mode === after.mode && before.size === after.size && before.mtimeNs === after.mtimeNs && before.ctimeNs !== after.ctimeNs;
1157
+ const chmodMoves = moves;
1158
+ move = () => writeFileSync(git, '#!/bin/sh\\nexit 1\\n');
1159
+ const rewritten = await probeBuilt({ target: 'app:build', cwd: process.cwd() });
1160
+ await new Promise<void>((resolve, reject) => process.stdout.write(JSON.stringify({ chmodded, ctimeAlone, chmodMoves, rewritten }) + "\\n", error => error ? reject(error) : resolve()));
1161
+ `,
1162
+ );
1163
+ const toolMove = await run('bun', toolMoveEntry, workspace, [], {
1164
+ NX_DAEMON: 'true',
1165
+ NX_USE_LOCAL: 'true',
1166
+ NX_WORKSPACE_ROOT_PATH: workspace,
1167
+ ...ownEnv,
1168
+ });
1169
+ expect(toolMove.code, toolMove.stdout + toolMove.stderr).toBe(0);
1170
+ const toolMoves = typia.json.assertParse<{
1171
+ chmodded: EnsureBuiltProbeResult;
1172
+ ctimeAlone: boolean;
1173
+ chmodMoves: number;
1174
+ rewritten: EnsureBuiltProbeResult;
1175
+ }>(toolMove.stdout);
1176
+ expect(toolMoves.chmodMoves).toBeGreaterThan(0);
1177
+ expect(toolMoves.ctimeAlone).toBe(true);
1178
+ expect(toolMoves.chmodded, toolMove.stderr).toEqual(environmentResults.first);
1179
+ expect(toolMoves.rewritten).toMatchObject({
1180
+ disposition: 'failed',
1181
+ reason: {
1182
+ kind: 'provider-proof',
1183
+ detail: `Producer tool namespace changed during independent product validation: ${fixtureGit}`,
1184
+ },
1185
+ });
1114
1186
  },
1115
1187
  'workspace',
1116
1188
  );
@@ -42,12 +42,15 @@ import type * as NxWorkspaceRoot from 'nx/src/utils/workspace-root';
42
42
  import { boundedReceiptRuntimes, type ProviderInputProof } from './ensure-built-receipt.js';
43
43
  import {
44
44
  type ArtifactWitness,
45
+ censusStart,
45
46
  type GraphWitness,
46
47
  graphInstallationIdentity,
48
+ movedNodes,
47
49
  nodeStamp,
48
50
  nodesUnchanged,
49
51
  readArtifactWitness,
50
52
  readGraphWitness,
53
+ recordedStamp,
51
54
  reusableGraph,
52
55
  snapshotsEqual,
53
56
  workspaceSnapshot,
@@ -1068,7 +1071,9 @@ async function currentProjectGraph(
1068
1071
  expandOutputs,
1069
1072
  graphInputContractIdentity(workspaceRoot, nxJson, after),
1070
1073
  );
1071
- const stableGeneration = snapshotsEqual(before, after) && nodesUnchanged(before.nodes);
1074
+ // `after` re-hashed every node whose stamp does not prove it (its ctime alone moved, or `before` read it in the tick
1075
+ // it changed in), and snapshotsEqual compares those hashes.
1076
+ const stableGeneration = snapshotsEqual(before, after) && movedNodes(before.nodes, () => true) === null;
1072
1077
  if (installation !== null && installation === finalInstallation && stableGeneration) {
1073
1078
  const next: GraphWitness = { version: 1, workspaceRoot, environment, installation, snapshot: after, projectGraph };
1074
1079
  writeWitness(witnessPath, next);
@@ -1292,6 +1297,8 @@ export function compareWithArtifacts(
1292
1297
  if (!isSystemError(error)) throw error;
1293
1298
  }
1294
1299
  }
1300
+ // Read before the comparison's first stat: a node it reads in the tick it changed in is recorded unproven.
1301
+ const start = censusStart();
1295
1302
  const comparison: ArtifactComparison = {
1296
1303
  expandOutputs,
1297
1304
  left: Buffer.allocUnsafe(COMPARE_CHUNK_BYTES),
@@ -1312,7 +1319,11 @@ export function compareWithArtifacts(
1312
1319
  version: 1,
1313
1320
  workspaceRoot,
1314
1321
  claims: binding,
1315
- nodes: comparison.observed.map(({ taskId, path, stats }) => ({ taskId, path, stamp: nodeStamp(stats) })),
1322
+ nodes: comparison.observed.map(({ taskId, path, stats }) => ({
1323
+ taskId,
1324
+ path,
1325
+ stamp: recordedStamp(stats, start),
1326
+ })),
1316
1327
  };
1317
1328
  writeWitness(witnessPath, next);
1318
1329
  }
@@ -365,6 +365,23 @@ describe('the parent-held CPU pool', () => {
365
365
  expect(readInheritedCpuPool({ [CPU_TOKENS_ENV]: '4' })).toBeNull();
366
366
  });
367
367
 
368
+ it("reads a resident's relay pool: zero beside its endpoint, never bare or malformed", () => {
369
+ const relay = '/tmp/relay.sock';
370
+ // The relay pool holds no token for its lifetime and asks the host for each child, so it names 0.
371
+ expect(readInheritedCpuPool({ [CPU_POOL_ENV]: relay, [CPU_TOKENS_ENV]: '0' })).toEqual({
372
+ socket: relay,
373
+ tokens: 0,
374
+ });
375
+ // A bare zero proves nothing, as any bare count does, and zero does not relax the endpoint's rules.
376
+ expect(readInheritedCpuPool({ [CPU_TOKENS_ENV]: '0' })).toBeNull();
377
+ expect(readInheritedCpuPool({ [CPU_POOL_ENV]: '', [CPU_TOKENS_ENV]: '0' })).toBeNull();
378
+ expect(readInheritedCpuPool({ [CPU_POOL_ENV]: 'a\0b', [CPU_TOKENS_ENV]: '0' })).toBeNull();
379
+ // Zero is the one non-positive count: its other spellings, negatives and unsafe integers stay malformed.
380
+ for (const malformed of ['-1', '-0', '+0', '00', '0.0', ' 0', '0 ', '', '9007199254740993']) {
381
+ expect(readInheritedCpuPool({ [CPU_POOL_ENV]: relay, [CPU_TOKENS_ENV]: malformed })).toBeNull();
382
+ }
383
+ });
384
+
368
385
  it('names the endpoint and count it holds, in private owned paths, and removes them on close', async () => {
369
386
  const made = await createCpuPool(3, NEVER, countedWaits());
370
387
  if (!made.ok) {
@@ -489,6 +506,7 @@ describe('the parent-held CPU pool', () => {
489
506
  { want: 8, inherited: 2, tokens: 8 },
490
507
  { want: 8, inherited: 8, tokens: 8 },
491
508
  { want: 3, inherited: 1, tokens: 3 },
509
+ { want: 4, inherited: 0, tokens: 4 }, // a resident's relay pool names 0: the count bounds no ask
492
510
  ];
493
511
  for (const check of cases) {
494
512
  // A fresh full pool isolates each ask from delivery of the previous client's close event.