@smoothbricks/nx-plugin 0.5.0 → 0.5.2

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 (79) hide show
  1. package/README.md +107 -46
  2. package/dist/bin/smoo-nx-test-clocks.js +2 -2
  3. package/dist/bin/smoo-nx-test-walks.js +6 -9
  4. package/dist/cargo-closure-input.d.ts.map +1 -1
  5. package/dist/cargo-closure-input.js +25 -15
  6. package/dist/ensure-built-witness.d.ts +33 -5
  7. package/dist/ensure-built-witness.d.ts.map +1 -1
  8. package/dist/ensure-built-witness.js +84 -18
  9. package/dist/ensure-built.d.ts.map +1 -1
  10. package/dist/ensure-built.js +33 -14
  11. package/dist/executors/bounded-exec/cpu-pool.d.ts +14 -7
  12. package/dist/executors/bounded-exec/cpu-pool.d.ts.map +1 -1
  13. package/dist/executors/bounded-exec/cpu-pool.js +297 -38
  14. package/dist/executors/bounded-exec/cpu-tokens.d.ts +19 -7
  15. package/dist/executors/bounded-exec/cpu-tokens.d.ts.map +1 -1
  16. package/dist/executors/bounded-exec/cpu-tokens.js +21 -7
  17. package/dist/executors/bounded-exec/executor.d.ts.map +1 -1
  18. package/dist/executors/bounded-exec/executor.js +12 -6
  19. package/dist/executors/bounded-exec/run-cpu-pool.d.ts.map +1 -1
  20. package/dist/executors/bounded-exec/run-cpu-pool.js +5 -4
  21. package/dist/index.d.ts.map +1 -1
  22. package/dist/index.js +4 -0
  23. package/dist/owned-installation.d.ts +6 -5
  24. package/dist/owned-installation.d.ts.map +1 -1
  25. package/dist/owned-installation.js +324 -201
  26. package/dist/producer-receipts.d.ts +10 -0
  27. package/dist/producer-receipts.d.ts.map +1 -1
  28. package/dist/producer-receipts.js +30 -5
  29. package/dist/test-walk-policy.d.ts +8 -0
  30. package/dist/test-walk-policy.d.ts.map +1 -1
  31. package/dist/test-walk-policy.js +34 -0
  32. package/dist/workspace-manifest.d.ts +5 -5
  33. package/dist/workspace-manifest.d.ts.map +1 -1
  34. package/managed/raw/patches/nx@23.2.1.patch +14 -3
  35. package/managed/raw/tooling/direnv/developer-links.ts +37 -10
  36. package/managed/raw/tooling/direnv/devenv.smoo.nix +1 -3
  37. package/managed/raw/tooling/direnv/linked-snapshots.ts +49 -12
  38. package/managed/raw/tooling/direnv/repo-path +1 -1
  39. package/managed/raw/tooling/direnv/setup-environment.ts +2 -0
  40. package/managed/raw/tooling/git-hooks/pre-push.sh +34 -11
  41. package/package.json +3 -3
  42. package/runtime-input.sh +21 -9
  43. package/src/__tests__/cargo-closure-filesets.ts +94 -0
  44. package/src/__tests__/counted-cargo.ts +1 -1
  45. package/src/__tests__/owned-installation.ts +5 -6
  46. package/src/bin/smoo-nx-test-clocks.ts +2 -2
  47. package/src/bin/smoo-nx-test-walks.ts +6 -9
  48. package/src/cargo-closure-filesets.test.ts +63 -0
  49. package/src/cargo-closure-input.test.ts +2 -2
  50. package/src/cargo-closure-input.ts +23 -15
  51. package/src/cargo-closure-key.test.ts +1 -1
  52. package/src/cargo-resolve-cache.test.ts +1 -1
  53. package/src/cross-check-policy.test.ts +53 -12
  54. package/src/ensure-built-witness.test.ts +53 -2
  55. package/src/ensure-built-witness.ts +103 -18
  56. package/src/ensure-built.test.ts +79 -5
  57. package/src/ensure-built.ts +51 -15
  58. package/src/executors/bounded-exec/cpu-pool.test.ts +132 -4
  59. package/src/executors/bounded-exec/cpu-pool.ts +300 -46
  60. package/src/executors/bounded-exec/cpu-tokens.test.ts +60 -15
  61. package/src/executors/bounded-exec/cpu-tokens.ts +34 -11
  62. package/src/executors/bounded-exec/executor.test.ts +72 -4
  63. package/src/executors/bounded-exec/executor.ts +12 -6
  64. package/src/executors/bounded-exec/run-cpu-pool.test.ts +3 -0
  65. package/src/executors/bounded-exec/run-cpu-pool.ts +5 -4
  66. package/src/index.ts +4 -0
  67. package/src/linked-snapshots.nx.test.ts +1 -1
  68. package/src/nextest-partitions.test.ts +1 -1
  69. package/src/nx-daemon-validity.test.ts +51 -0
  70. package/src/owned-installation.test.ts +105 -0
  71. package/src/owned-installation.ts +335 -93
  72. package/src/producer-receipts.test.ts +37 -15
  73. package/src/producer-receipts.ts +29 -5
  74. package/src/runtime-input.test.ts +22 -1
  75. package/src/task-results.test.ts +1 -1
  76. package/src/test-walk-policy.test.ts +35 -0
  77. package/src/test-walk-policy.ts +32 -0
  78. package/src/workspace-manifest.ts +5 -5
  79. package/test-timings.json +94 -92
@@ -22,15 +22,20 @@ import { expandOutputs, WorkspaceContext } from 'nx/src/native';
22
22
  import { workspaceDataDirectoryForWorkspace } from 'nx/src/utils/cache-directory';
23
23
  import type { readModulePackageJsonWithoutFallbacks } from 'nx/src/utils/package-json';
24
24
  import typia from 'typia';
25
+ import { pastTick } from './__tests__/past-tick.js';
25
26
  import {
26
27
  censusStart,
28
+ type GraphInstallationCensus,
27
29
  type GraphWitness,
28
30
  graphInstallationIdentity,
31
+ installationIdentityOf,
29
32
  movedNodes,
30
33
  type NodeWitness,
31
34
  nodeStamp,
32
35
  nodesUnchanged,
36
+ provenInstallationCensus,
33
37
  readGraphWitness,
38
+ recordedInstallationCensus,
34
39
  recordedStamp,
35
40
  reusableGraph,
36
41
  SAME_TICK_NS,
@@ -68,10 +73,11 @@ afterEach(async () => {
68
73
 
69
74
  function witness(): GraphWitness {
70
75
  return {
71
- version: 1,
76
+ version: 2,
72
77
  workspaceRoot: root,
73
78
  environment: 'env',
74
79
  installation: 'installed',
80
+ installationUnproven: [],
75
81
  snapshot: snapshot(),
76
82
  projectGraph: graph,
77
83
  };
@@ -151,7 +157,7 @@ describe('authoritative graph witness', () => {
151
157
  } finally {
152
158
  await chmod(path, 0o600);
153
159
  }
154
- await writeFile(path, JSON.stringify({ ...witness(), version: 2 }));
160
+ await writeFile(path, JSON.stringify({ ...witness(), version: 1 }));
155
161
  expect(readGraphWitness(path)).toBeNull();
156
162
  });
157
163
  });
@@ -339,6 +345,51 @@ describe('installed graph input identity', () => {
339
345
  original.files.find(({ path }) => path === output)?.sha256,
340
346
  );
341
347
  });
348
+
349
+ it('takes only a proven, unmoved file’s hash from a previous census and reads every other file again', async () => {
350
+ const { plugin, resolver } = await packages();
351
+ const output = join(plugin, 'dist', 'index.js');
352
+ const census = (previous?: GraphInstallationCensus) =>
353
+ typia.assert<GraphInstallationCensus>(
354
+ provenInstallationCensus(
355
+ root,
356
+ { plugins: ['@smoothbricks/nx-plugin'] },
357
+ resolver,
358
+ expandOutputs,
359
+ null,
360
+ undefined,
361
+ previous,
362
+ ),
363
+ );
364
+ const hashOf = ({ files }: GraphInstallationCensus) => files.find(({ path }) => path === output)?.sha256;
365
+ const bytes = (text: string) => createHash('sha256').update(text).digest('hex');
366
+ await pastTick();
367
+ const first = census();
368
+ expect(first.unproven).toEqual([]);
369
+ // A recorded hash that is not the file's: a census that takes it did not read the file.
370
+ const recorded = {
371
+ ...first,
372
+ files: first.files.map((file) => (file.path === output ? { ...file, sha256: 'recorded' } : file)),
373
+ };
374
+ expect(hashOf(census(recorded))).toBe('recorded');
375
+ // The witness keeps the census as its identity and its unproven files beside it.
376
+ expect(recordedInstallationCensus(installationIdentityOf(recorded, null), [])).toEqual(recorded);
377
+ expect(hashOf(census({ ...recorded, unproven: [output] }))).toBe(bytes('exports.value = 1;'));
378
+ // Only its ctime moved: a same-mode chmod, as Bun's bin linking makes.
379
+ chmodSync(output, lstatSync(output).mode & 0o7777);
380
+ expect(hashOf(census(recorded))).toBe(bytes('exports.value = 1;'));
381
+ // A same-size rewrite that keeps its mtime moves its ctime: read again.
382
+ const before = await stat(output);
383
+ await writeFile(output, 'exports.value = 2;');
384
+ await utimes(output, before.atime, before.mtime);
385
+ expect(hashOf(census(recorded))).toBe(bytes('exports.value = 2;'));
386
+ // Dated ahead of the census, as a change in its tick is: recorded unproven, and read again by the next census.
387
+ const ahead = new Date(Date.now() + 60_000);
388
+ await utimes(output, ahead, ahead);
389
+ const dated = census();
390
+ expect(dated.unproven).toEqual([output]);
391
+ expect(hashOf(census({ ...dated, files: recorded.files }))).toBe(bytes('exports.value = 2;'));
392
+ });
342
393
  it('proves a declared dist link by its physical output tree and rejects changed public bytes', async () => {
343
394
  const { plugin, resolver } = await packages();
344
395
  const published = join(root, 'published-build-volume');
@@ -34,6 +34,11 @@ export function nodeStamp(stats: StampFields): string {
34
34
  return `${stats.dev}:${stats.ino}:${stats.mode}:${stats.size}:${stats.mtimeNs}:${stats.ctimeNs}`;
35
35
  }
36
36
 
37
+ /** The file type and permission bits a {@link nodeStamp} records: what a copy of the node keeps of its stamp. */
38
+ export function stampMode(stamp: string): string {
39
+ return stamp.split(':')[2];
40
+ }
41
+
37
42
  /**
38
43
  * How an observed stamp compares with the recorded one: `same`; `ctime` when device, inode, mode, size and mtime are
39
44
  * kept and only the ctime moved (a same-mode chmod, which Bun's bin linking does to a shared store file on any
@@ -96,10 +101,12 @@ export interface WorkspaceSnapshot {
96
101
  }
97
102
 
98
103
  export interface GraphWitness {
99
- readonly version: 1;
104
+ readonly version: 2;
100
105
  readonly workspaceRoot: string;
101
106
  readonly environment: string;
102
107
  readonly installation: string;
108
+ /** The census behind `installation` read these files in the tick they changed in: {@link GraphInstallationCensus}. */
109
+ readonly installationUnproven: readonly string[];
103
110
  readonly snapshot: WorkspaceSnapshot;
104
111
  readonly projectGraph: ProjectGraph;
105
112
  }
@@ -223,7 +230,7 @@ export function reusableGraph(
223
230
  snapshot: WorkspaceSnapshot,
224
231
  ): ProjectGraph | null {
225
232
  return witness !== null &&
226
- witness.version === 1 &&
233
+ witness.version === 2 &&
227
234
  witness.workspaceRoot === workspaceRoot &&
228
235
  installation !== null &&
229
236
  witness.installation === installation &&
@@ -233,13 +240,15 @@ export function reusableGraph(
233
240
  : null;
234
241
  }
235
242
 
236
- /** What {@link graphInstallationIdentity} binds: resolution edges `[from, name, physical manifest]`, node stamps and
237
- * file hashes, read once.
243
+ /** What {@link graphInstallationIdentity} binds: resolution edges `[from, name, physical manifest]`, node stamps as
244
+ * read and file hashes, read once. `unproven` names the files this census read in the tick they changed in
245
+ * ({@link unproven}); it is not part of the identity, only what a later census that reuses these hashes must re-read.
238
246
  */
239
247
  export interface GraphInstallationCensus {
240
248
  readonly resolutions: readonly (readonly [string, string, string | null])[];
241
249
  readonly nodes: readonly NodeWitness[];
242
250
  readonly files: readonly { readonly path: string; readonly sha256: string }[];
251
+ readonly unproven: readonly string[];
243
252
  }
244
253
 
245
254
  /** Plugins with opaque external graph inputs cannot borrow this proof.
@@ -257,7 +266,59 @@ export function graphInstallationIdentity(
257
266
  localInputs: GraphInputContractProof | null,
258
267
  onUnproved?: (detail: string) => void,
259
268
  ): string | null {
260
- const census = graphInstallationCensus(workspaceRoot, nxJson, resolvePackage, expand, localInputs, onUnproved);
269
+ const census = provenInstallationCensus(workspaceRoot, nxJson, resolvePackage, expand, localInputs, onUnproved);
270
+ return census === null ? null : installationIdentityOf(census, localInputs);
271
+ }
272
+
273
+ /** The identity {@link graphInstallationIdentity} names for a proven census: what it read, never what it reused. */
274
+ export function installationIdentityOf(
275
+ { resolutions, nodes, files }: GraphInstallationCensus,
276
+ localInputs: GraphInputContractProof | null,
277
+ ): string {
278
+ return JSON.stringify({ resolutions, nodes, files, localInputs: localInputs?.identity ?? null });
279
+ }
280
+
281
+ /** The census behind a recorded identity ({@link installationIdentityOf}) and its unproven files, or `undefined` for
282
+ * one that does not parse: a hash source for {@link graphInstallationCensus}, never a proof.
283
+ */
284
+ export function recordedInstallationCensus(
285
+ identity: string,
286
+ unproven: readonly string[],
287
+ ): GraphInstallationCensus | undefined {
288
+ try {
289
+ const recorded = typia.json.isParse<Omit<GraphInstallationCensus, 'unproven'>>(identity);
290
+ return recorded === null
291
+ ? undefined
292
+ : { resolutions: recorded.resolutions, nodes: recorded.nodes, files: recorded.files, unproven };
293
+ } catch (error) {
294
+ if (error instanceof SyntaxError) return undefined;
295
+ throw error;
296
+ }
297
+ }
298
+
299
+ /**
300
+ * A {@link graphInstallationCensus} that no node moved during: every node is read again after the census, and one
301
+ * whose stamp moved fails it, unless only its ctime moved and its bytes are the ones hashed. `previous` is what the
302
+ * census may take hashes from. `null`, with `onUnproved` told why, for a closure that cannot be read or that moved.
303
+ */
304
+ export function provenInstallationCensus(
305
+ workspaceRoot: string,
306
+ nxJson: NxJsonConfiguration,
307
+ resolvePackage: typeof readModulePackageJsonWithoutFallbacks,
308
+ expand: typeof expandOutputs,
309
+ localInputs: GraphInputContractProof | null,
310
+ onUnproved?: (detail: string) => void,
311
+ previous?: GraphInstallationCensus,
312
+ ): GraphInstallationCensus | null {
313
+ const census = graphInstallationCensus(
314
+ workspaceRoot,
315
+ nxJson,
316
+ resolvePackage,
317
+ expand,
318
+ localInputs,
319
+ onUnproved,
320
+ previous,
321
+ );
261
322
  if (census === null) return null;
262
323
  try {
263
324
  // A file whose ctime alone moved is re-hashed: Bun's bin linking on any checkout's install re-chmods a shared
@@ -279,12 +340,22 @@ export function graphInstallationIdentity(
279
340
  }
280
341
  throw error;
281
342
  }
282
- return JSON.stringify({ ...census, localInputs: localInputs?.identity ?? null });
343
+ return census;
283
344
  }
284
345
 
285
- /** One read of the installation closure {@link graphInstallationIdentity} binds, without its final check that no node
346
+ /**
347
+ * One read of the installation closure {@link graphInstallationIdentity} binds, without its final check that no node
286
348
  * moved while it was read: what a caller that needs the closure, not a proof of it, reads. `null`, with `onUnproved`
287
349
  * told why, for a closure that cannot be read.
350
+ *
351
+ * Every census resolves every edge, lists every directory and stats every node; only file bytes are reused. A file
352
+ * takes its hash from `previous` when its stamp (device, inode, mode, size, and mtime and ctime in nanoseconds) is
353
+ * the one `previous` read and `previous` did not read it in the tick it changed in. A write moves a file's ctime to
354
+ * the tick it happens in, so a file written since `previous` stat-ed it, even between that stat and its read, one
355
+ * whose stamp `previous` lacks, and one `previous` recorded unproven are read again; `previous` need not be proven.
356
+ * The key is the stamp, not a cheaper generation (a resolution, a manifest, an install marker): a rebuild of a
357
+ * developer-linked package, this plugin's own `dist`, rewrites its files in place and moves none of those, so a census
358
+ * keyed on them would describe a tree that is no longer there.
288
359
  */
289
360
  export function graphInstallationCensus(
290
361
  workspaceRoot: string,
@@ -293,6 +364,7 @@ export function graphInstallationCensus(
293
364
  expand: typeof expandOutputs,
294
365
  localInputs: GraphInputContractProof | null,
295
366
  onUnproved?: (detail: string) => void,
367
+ previous?: GraphInstallationCensus,
296
368
  ): GraphInstallationCensus | null {
297
369
  const plugins = (nxJson.plugins ?? []).map((plugin) => (typeof plugin === 'string' ? plugin : plugin.plugin));
298
370
  const unproved = plugins.filter(
@@ -302,8 +374,15 @@ export function graphInstallationCensus(
302
374
  onUnproved?.(`Unproved configured graph plugins: ${unproved.join(', ')}`);
303
375
  return null;
304
376
  }
377
+ const start = censusStart();
305
378
  const nodes: NodeWitness[] = [];
306
379
  const files: { path: string; sha256: string }[] = [];
380
+ const unprovenFiles: string[] = [];
381
+ // The stamps `previous` read its files at, those it read in their change tick left out, and the hashes it read.
382
+ const recordedUnproven = new Set(previous?.unproven);
383
+ const recordedStamps = new Map<string, string>();
384
+ for (const { path, stamp } of previous?.nodes ?? []) if (!recordedUnproven.has(path)) recordedStamps.set(path, stamp);
385
+ const recordedHashes = new Map(previous?.files.map(({ path, sha256 }) => [path, sha256]));
307
386
  const buffer = Buffer.allocUnsafe(64 * 1024);
308
387
  const resolutions: [string, string, string | null][] = [];
309
388
  const visited = new Set<string>();
@@ -311,7 +390,8 @@ export function graphInstallationCensus(
311
390
  const expandedRoots = new Set<string>();
312
391
  function walk(path: string, selected?: ReadonlySet<string>): void {
313
392
  const stats = lstatSync(path, { bigint: true });
314
- nodes.push({ path, stamp: nodeStamp(stats) });
393
+ const stamp = nodeStamp(stats);
394
+ nodes.push({ path, stamp });
315
395
  if (stats.isSymbolicLink()) {
316
396
  const physical = realpathSync(path);
317
397
  if (lstatSync(physical).isDirectory() && !declaredDirectoryLinks.has(path)) {
@@ -333,17 +413,22 @@ export function graphInstallationCensus(
333
413
  } else if (!stats.isFile()) {
334
414
  throw new Error(`Unproved graph package node: ${path}`);
335
415
  } else {
336
- const hash = createHash('sha256');
337
- const fd = openSync(path, 'r');
338
- try {
339
- for (let count = readSync(fd, buffer); count > 0; count = readSync(fd, buffer)) {
340
- hash.update(buffer.subarray(0, count));
341
- }
342
- } finally {
343
- closeSync(fd);
416
+ if (unproven(stats, start)) unprovenFiles.push(path);
417
+ const recorded = recordedStamps.get(path) === stamp ? recordedHashes.get(path) : undefined;
418
+ files.push({ path, sha256: recorded ?? fileHash(path) });
419
+ }
420
+ }
421
+ function fileHash(path: string): string {
422
+ const hash = createHash('sha256');
423
+ const fd = openSync(path, 'r');
424
+ try {
425
+ for (let count = readSync(fd, buffer); count > 0; count = readSync(fd, buffer)) {
426
+ hash.update(buffer.subarray(0, count));
344
427
  }
345
- files.push({ path, sha256: hash.digest('hex') });
428
+ } finally {
429
+ closeSync(fd);
346
430
  }
431
+ return hash.digest('hex');
347
432
  }
348
433
  function installedManifest(name: string, from: string): InstalledPackageManifest {
349
434
  try {
@@ -449,7 +534,7 @@ export function graphInstallationCensus(
449
534
  visit(name, workspaceRoot, false);
450
535
  for (const input of localInputs?.packages ?? []) visit(input.name, input.from, false);
451
536
  visit('typescript', workspaceRoot, true);
452
- return { resolutions, nodes, files };
537
+ return { resolutions, nodes, files, unproven: unprovenFiles };
453
538
  } catch (error) {
454
539
  if (systemError(error) && ['EACCES', 'EPERM', 'EIO'].includes(error.code)) throw error;
455
540
  // Installation uncertainty disables only graph reuse. Nx's authoritative
@@ -1,6 +1,6 @@
1
1
  import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it } from 'bun:test';
2
2
  import { spawn, spawnSync } from 'node:child_process';
3
- import { chmod, mkdir, mkdtemp, readdir, readFile, realpath, rm, symlink, writeFile } from 'node:fs/promises';
3
+ import { chmod, cp, mkdir, mkdtemp, readdir, readFile, realpath, rm, symlink, writeFile } from 'node:fs/promises';
4
4
  import { tmpdir } from 'node:os';
5
5
  import { dirname, isAbsolute, join } from 'node:path';
6
6
  import { fileURLToPath } from 'node:url';
@@ -727,8 +727,8 @@ const FIXTURE_NX_JSON = {
727
727
  plugins: ['@smoothbricks/nx-plugin'],
728
728
  };
729
729
 
730
- /** The Nx installation this file's fixtures share: one copy per run of the file, not one per fixture. */
731
- const installation = testFileInstallation('ensure-built-installation-');
730
+ /** The Nx installation this file's fixtures share: one copy kept for this file, not one per fixture. */
731
+ const installation = testFileInstallation('ensure-built-installation');
732
732
 
733
733
  /**
734
734
  * Two projects, `app:build` reading `lib:build`'s outputs through
@@ -834,6 +834,17 @@ describe('smoo-nx-exec with a fresh daemon', () => {
834
834
  async ({ root, workspace, socketDirectory }) => {
835
835
  const builds = join(root, 'lib-builds.log');
836
836
  await writeTwoProjectWorkspace(workspace, builds);
837
+ // The workspace takes its own copy of TypeScript, which the census resolves from the workspace root, so the
838
+ // installation the script below writes into is the fixture's, not the copy this file's fixtures share.
839
+ const shared = await realpath(join(workspace, 'node_modules'));
840
+ await rm(join(workspace, 'node_modules'));
841
+ await mkdir(join(workspace, 'node_modules'));
842
+ for (const entry of await readdir(shared)) {
843
+ if (entry !== 'typescript') await symlink(join(shared, entry), join(workspace, 'node_modules', entry));
844
+ }
845
+ await cp(await realpath(join(shared, 'typescript')), join(workspace, 'node_modules', 'typescript'), {
846
+ recursive: true,
847
+ });
837
848
  const built = await run(
838
849
  'node',
839
850
  nxEntry(workspace),
@@ -850,7 +861,7 @@ describe('smoo-nx-exec with a fresh daemon', () => {
850
861
  import assert from 'node:assert/strict';
851
862
  import { existsSync } from 'node:fs';
852
863
  import { createRequire } from 'node:module';
853
- import { chmod, readFile, rename, rm, stat, writeFile } from 'node:fs/promises';
864
+ import { chmod, readFile, rename, rm, stat, utimes, writeFile } from 'node:fs/promises';
854
865
  import { join } from 'node:path';
855
866
  import { ensureBuilt } from ${JSON.stringify(join(packageRoot, 'dist', 'ensure-built.js'))};
856
867
  const requireNx = createRequire(process.cwd() + '/package.json');
@@ -894,6 +905,17 @@ await writeFile(project, JSON.stringify(config));
894
905
  await check('dependency');
895
906
  await writeFile(project, original);
896
907
  await check('dependency-restored');
908
+ // Every call censuses the installation again, taking the hashes of files whose stamps have not moved from the
909
+ // witness. A same-size rewrite that keeps its file's mtime moves its ctime: the next call reads it and sees the
910
+ // changed installation, and the call after that reuses the graph the changed installation was proved with.
911
+ const installed = join(process.cwd(), 'node_modules/typescript/README.md');
912
+ const readme = await readFile(installed);
913
+ const { atime, mtime } = await stat(installed);
914
+ readme[0] ^= 1;
915
+ await writeFile(installed, readme);
916
+ await utimes(installed, atime, mtime);
917
+ await check('installation');
918
+ await check('installation-reuse');
897
919
  // Save only evidence the real daemon generation actually authorized, outside
898
920
  // the workspace: the later missing-witness RPC is not obliged to replace it.
899
921
  const authoritativeWitness = await readFile(witness, 'utf8');
@@ -937,7 +959,14 @@ await writeFile(${JSON.stringify(resultsPath)}, JSON.stringify(results));
937
959
  disposition: 'hit',
938
960
  witnessExists: true,
939
961
  });
940
- for (const label of ['created', 'deleted', 'dependency', 'dependency-restored', 'unreadable-witness']) {
962
+ for (const label of [
963
+ 'created',
964
+ 'deleted',
965
+ 'dependency',
966
+ 'dependency-restored',
967
+ 'installation',
968
+ 'unreadable-witness',
969
+ ]) {
941
970
  expect(graphChecks.find((check) => check.label === label)?.requests, label).toBeGreaterThan(0);
942
971
  }
943
972
  expect(graphChecks.find(({ label }) => label === 'missing-witness')).toEqual({
@@ -946,6 +975,12 @@ await writeFile(${JSON.stringify(resultsPath)}, JSON.stringify(results));
946
975
  disposition: 'hit',
947
976
  witnessExists: false,
948
977
  });
978
+ expect(graphChecks.find(({ label }) => label === 'installation-reuse')).toEqual({
979
+ label: 'installation-reuse',
980
+ requests: 0,
981
+ disposition: 'hit',
982
+ witnessExists: true,
983
+ });
949
984
  expect(graphChecks.find(({ label }) => label === 'unreadable-witness')?.disposition).toBe('hit');
950
985
  expect(await readFile(builds, 'utf8')).toBe('lib\n');
951
986
  expect(await readFile(join(workspace, 'packages/app/dist/marker.txt'), 'utf8')).toBe('built\nlib\n');
@@ -1191,6 +1226,45 @@ await new Promise<void>((resolve, reject) => process.stdout.write(JSON.stringify
1191
1226
  }
1192
1227
  });
1193
1228
 
1229
+ // A sandboxed consumer is granted its provider read-only, and starting a daemon writes the provider's workspace
1230
+ // data (`.nx/workspace-data/d`): the read-only probe connects to a serving daemon or reports that none serves.
1231
+ it('reports no-daemon and writes nothing when no daemon serves a provider without a receipt', async () => {
1232
+ await withNxFixture(
1233
+ 'ensure-built-probe-no-daemon-',
1234
+ async ({ root, workspace, socketDirectory }) => {
1235
+ await writeTwoProjectWorkspace(workspace, join(root, 'lib-builds.log'), true);
1236
+ const listing = async () =>
1237
+ (await readdir(workspace, { recursive: true })).filter((entry) => !entry.startsWith('node_modules')).sort();
1238
+ const before = await listing();
1239
+ const probeEntry = join(root, 'probe-no-daemon.ts');
1240
+ await writeFile(
1241
+ probeEntry,
1242
+ `
1243
+ import { probeBuilt } from ${JSON.stringify(join(packageRoot, 'dist', 'ensure-built.js'))};
1244
+ const result = await probeBuilt({ target: 'app:build', cwd: ${JSON.stringify(workspace)} });
1245
+ await new Promise<void>((resolve, reject) => process.stdout.write(JSON.stringify(result) + "\\n", error => error ? reject(error) : resolve()));
1246
+ `,
1247
+ );
1248
+ // Run from the fixture root, outside the provider workspace, as a consumer calls it.
1249
+ const probed = await run('bun', probeEntry, root, [], { NX_DAEMON: 'true', NX_SOCKET_DIR: socketDirectory });
1250
+ expect(probed.code, probed.stdout + probed.stderr).toBe(0);
1251
+ const result = typia.json.assertParse<EnsureBuiltProbeResult>(probed.stdout);
1252
+ expect(result).toMatchObject({
1253
+ disposition: 'failed',
1254
+ reason: { kind: 'provider-proof', taskId: 'app:build' },
1255
+ });
1256
+ if (result.disposition !== 'failed' || result.reason.kind !== 'provider-proof') {
1257
+ throw new Error(`the probe must refuse with its provider proof, not ${probed.stdout}`);
1258
+ }
1259
+ expect(result.reason.detail).toEndWith(
1260
+ `authoritative graph unavailable: ${describeMiss({ kind: 'no-daemon', taskId: 'app:build' })}`,
1261
+ );
1262
+ expect(await listing()).toEqual(before);
1263
+ },
1264
+ 'workspace',
1265
+ );
1266
+ });
1267
+
1194
1268
  // The graph/exec case runs real Nx because its subject is the daemon:
1195
1269
  // the daemon a probe starts holds no output records, so every output must be
1196
1270
  // vouched for by comparing the working tree with Nx's cache artifact. The
@@ -45,11 +45,14 @@ import {
45
45
  censusStart,
46
46
  type GraphWitness,
47
47
  graphInstallationIdentity,
48
+ installationIdentityOf,
48
49
  movedNodes,
49
50
  nodeStamp,
50
51
  nodesUnchanged,
52
+ provenInstallationCensus,
51
53
  readArtifactWitness,
52
54
  readGraphWitness,
55
+ recordedInstallationCensus,
53
56
  recordedStamp,
54
57
  reusableGraph,
55
58
  snapshotsEqual,
@@ -302,7 +305,7 @@ export function describeMiss(reason: MissReason): string {
302
305
  case 'cache-disabled':
303
306
  return `the Nx cache is disabled, so ${reason.taskId} must run`;
304
307
  case 'no-daemon':
305
- return `the Nx daemon is disabled, so ${reason.taskId} cannot be checked without running it`;
308
+ return `no Nx daemon serves this workspace, so ${reason.taskId} cannot be checked without running it`;
306
309
  }
307
310
  }
308
311
 
@@ -735,23 +738,36 @@ type PreparedWorkspaceProbe =
735
738
  | ReadyWorkspaceProbe
736
739
  | { readonly kind: 'no-daemon'; readonly reason: Extract<MissReason, { kind: 'no-daemon' }> };
737
740
 
741
+ /**
742
+ * The two operations a probe is prepared for. `build` may start the workspace's Nx daemon, as `nx` itself does.
743
+ * `read-only` only connects to a daemon already serving the workspace, and asks it for the authoritative graph:
744
+ * starting one writes the workspace's data directory (`.nx/workspace-data/d`), and a read-only caller may not
745
+ * write it at all — a sandboxed consumer is granted its provider read-only. A daemon that exits between that
746
+ * connection check and the first request is the one window in which Nx's client still starts a successor.
747
+ */
748
+ type ProbeOperation = 'build' | 'read-only';
749
+
738
750
  async function prepareWorkspaceProbe(
739
751
  workspaceRoot: string,
740
752
  selector: TargetSelector,
741
- forceGraphRpc = false,
753
+ operation: ProbeOperation,
742
754
  ): Promise<PreparedWorkspaceProbe> {
743
755
  const taskId = selectorTaskId(selector);
756
+ const noDaemon: PreparedWorkspaceProbe = { kind: 'no-daemon', reason: { kind: 'no-daemon', taskId } };
744
757
  // This explicit fallback path loads no Nx. The build operation may run the
745
758
  // checkout-local CLI; the read-only operation can only report the miss.
746
759
  if (process.env.NX_DAEMON === 'false') {
747
- return { kind: 'no-daemon', reason: { kind: 'no-daemon', taskId } };
760
+ return noDaemon;
748
761
  }
749
762
  // Runtime modules always belong to the provider's installed Nx version.
750
763
  const requireNx = createWorkspaceNxRequire(workspaceRoot);
751
764
  bindWorkspaceRoot(workspaceRoot, requireNx);
752
765
  const { daemonClient } = requireNx('nx/src/daemon/client/client');
753
766
  if (!daemonClient.enabled()) {
754
- return { kind: 'no-daemon', reason: { kind: 'no-daemon', taskId } };
767
+ return noDaemon;
768
+ }
769
+ if (operation === 'read-only' && !(await daemonClient.isServerAvailable())) {
770
+ return noDaemon;
755
771
  }
756
772
  assignEnvironment(process.env, withoutOuterTaskCacheBypass(process.env));
757
773
 
@@ -771,7 +787,7 @@ async function prepareWorkspaceProbe(
771
787
  setEnvVarsBasedOnArgs(nxArgs, loadDotEnvFiles);
772
788
 
773
789
  performance.mark('ensureBuilt:graph:start');
774
- const projectGraph = await currentProjectGraph(workspaceRoot, nxJson, requireNx, forceGraphRpc);
790
+ const projectGraph = await currentProjectGraph(workspaceRoot, nxJson, requireNx, operation === 'read-only');
775
791
  nxJson = readNxJson();
776
792
  requireProject(projectGraph, selector.project);
777
793
  const taskGraph = createTaskGraph(
@@ -819,7 +835,7 @@ async function withTaskHooks<Result>(
819
835
  }
820
836
 
821
837
  async function ensureBuiltInWorkspace(workspaceRoot: string, selector: TargetSelector): Promise<EnsureBuiltResult> {
822
- const prepared = await prepareWorkspaceProbe(workspaceRoot, selector);
838
+ const prepared = await prepareWorkspaceProbe(workspaceRoot, selector, 'build');
823
839
  if (prepared.kind === 'no-daemon') return runViaCli(workspaceRoot, selector, prepared.reason);
824
840
  return withTaskHooks(workspaceRoot, selector, prepared, buildPreparedTarget);
825
841
  }
@@ -842,9 +858,10 @@ async function probeBuiltInWorkspace(workspaceRoot: string, selector: TargetSele
842
858
  const taskId = selectorTaskId(selector);
843
859
  const receipt = readProducerReceipt(workspaceRoot, taskId);
844
860
  if (receipt.ok) return { disposition: 'hit', receipt: receipt.value };
845
- // A failed publication proof asks Nx for the authoritative generation, but
846
- // never hashes against the consumer environment or starts a provider runner.
847
- const prepared = await prepareWorkspaceProbe(workspaceRoot, selector, true);
861
+ // A failed publication proof asks a serving Nx daemon for the authoritative
862
+ // generation, but never hashes against the consumer environment, starts a
863
+ // provider runner or starts the provider's daemon.
864
+ const prepared = await prepareWorkspaceProbe(workspaceRoot, selector, 'read-only');
848
865
  const detail =
849
866
  prepared.kind === 'no-daemon'
850
867
  ? `${receipt.detail}; authoritative graph unavailable: ${describeMiss(prepared.reason)}`
@@ -1051,31 +1068,50 @@ async function currentProjectGraph(
1051
1068
  await daemonClient.updateWorkspaceContext(created, updated, deleted);
1052
1069
  }
1053
1070
  performance.measure('ensureBuilt:inputs', 'ensureBuilt:inputs:start');
1054
- const installation = graphInstallationIdentity(
1071
+ // The witness's census is a hash source: a file whose proven stamp has not moved since it was read is not read again.
1072
+ const recorded =
1073
+ witness === null ? undefined : recordedInstallationCensus(witness.installation, witness.installationUnproven);
1074
+ const localInputs = graphInputContractIdentity(workspaceRoot, nxJson, before);
1075
+ const census = provenInstallationCensus(
1055
1076
  workspaceRoot,
1056
1077
  nxJson,
1057
1078
  packageResolution.readModulePackageJson,
1058
1079
  expandOutputs,
1059
- graphInputContractIdentity(workspaceRoot, nxJson, before),
1080
+ localInputs,
1081
+ undefined,
1082
+ recorded,
1060
1083
  );
1084
+ const installation = census === null ? null : installationIdentityOf(census, localInputs);
1061
1085
  const cached = nodesUnchanged(before.nodes)
1062
1086
  ? reusableGraph(witness, workspaceRoot, environment, installation, before)
1063
1087
  : null;
1064
1088
  if (!forceGraphRpc && cached !== null) return cached;
1065
1089
  const { projectGraph } = await daemonClient.getProjectGraphAndSourceMaps();
1066
1090
  const after = workspaceSnapshot(workspaceRoot, new WorkspaceContext(workspaceRoot, data), before);
1067
- const finalInstallation = graphInstallationIdentity(
1091
+ const finalInputs = graphInputContractIdentity(workspaceRoot, nxJson, after);
1092
+ const finalCensus = provenInstallationCensus(
1068
1093
  workspaceRoot,
1069
1094
  nxJson,
1070
1095
  packageResolution.readModulePackageJson,
1071
1096
  expandOutputs,
1072
- graphInputContractIdentity(workspaceRoot, nxJson, after),
1097
+ finalInputs,
1098
+ undefined,
1099
+ census ?? recorded,
1073
1100
  );
1101
+ const finalInstallation = finalCensus === null ? null : installationIdentityOf(finalCensus, finalInputs);
1074
1102
  // `after` re-hashed every node whose stamp does not prove it (its ctime alone moved, or `before` read it in the tick
1075
1103
  // it changed in), and snapshotsEqual compares those hashes.
1076
1104
  const stableGeneration = snapshotsEqual(before, after) && movedNodes(before.nodes, () => true) === null;
1077
- if (installation !== null && installation === finalInstallation && stableGeneration) {
1078
- const next: GraphWitness = { version: 1, workspaceRoot, environment, installation, snapshot: after, projectGraph };
1105
+ if (installation !== null && finalCensus !== null && installation === finalInstallation && stableGeneration) {
1106
+ const next: GraphWitness = {
1107
+ version: 2,
1108
+ workspaceRoot,
1109
+ environment,
1110
+ installation,
1111
+ installationUnproven: finalCensus.unproven,
1112
+ snapshot: after,
1113
+ projectGraph,
1114
+ };
1079
1115
  writeWitness(witnessPath, next);
1080
1116
  }
1081
1117
  // A write during the await cannot authorize a saved graph. Feed the final