@smoothbricks/nx-plugin 0.4.28 → 0.4.30

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (158) hide show
  1. package/README.md +277 -89
  2. package/dist/bin/smoo-nx-test-clocks.d.ts +3 -0
  3. package/dist/bin/smoo-nx-test-clocks.d.ts.map +1 -0
  4. package/dist/bin/smoo-nx-test-clocks.js +28 -0
  5. package/dist/bounded-test-policy.d.ts +8 -8
  6. package/dist/bounded-test-policy.d.ts.map +1 -1
  7. package/dist/bounded-test-policy.js +18 -30
  8. package/dist/cargo-closure-input.d.ts +23 -0
  9. package/dist/cargo-closure-input.d.ts.map +1 -1
  10. package/dist/cargo-closure-input.js +41 -9
  11. package/dist/cargo-source-hash.d.ts.map +1 -1
  12. package/dist/cargo-source-hash.js +42 -19
  13. package/dist/ensure-built-receipt.d.ts +40 -6
  14. package/dist/ensure-built-receipt.d.ts.map +1 -1
  15. package/dist/ensure-built-receipt.js +297 -159
  16. package/dist/ensure-built-witness.d.ts +65 -4
  17. package/dist/ensure-built-witness.d.ts.map +1 -1
  18. package/dist/ensure-built-witness.js +2237 -23
  19. package/dist/ensure-built.d.ts +99 -1
  20. package/dist/ensure-built.d.ts.map +1 -1
  21. package/dist/ensure-built.js +45 -56
  22. package/dist/executors/bounded-exec/cpu-pool.d.ts +22 -7
  23. package/dist/executors/bounded-exec/cpu-pool.d.ts.map +1 -1
  24. package/dist/executors/bounded-exec/cpu-pool.js +51 -22
  25. package/dist/executors/bounded-exec/executor.d.ts +12 -3
  26. package/dist/executors/bounded-exec/executor.d.ts.map +1 -1
  27. package/dist/executors/bounded-exec/executor.js +174 -51
  28. package/dist/executors/bounded-exec/no-space.d.ts +8 -0
  29. package/dist/executors/bounded-exec/no-space.d.ts.map +1 -0
  30. package/dist/executors/bounded-exec/no-space.js +31 -0
  31. package/dist/executors/bounded-exec/ram-temp.d.ts +20 -11
  32. package/dist/executors/bounded-exec/ram-temp.d.ts.map +1 -1
  33. package/dist/executors/bounded-exec/ram-temp.js +42 -17
  34. package/dist/executors/bounded-exec/verdict.d.ts +34 -4
  35. package/dist/executors/bounded-exec/verdict.d.ts.map +1 -1
  36. package/dist/executors/bounded-exec/verdict.js +54 -3
  37. package/dist/generators/bun-test-tracing/generator.js +2 -2
  38. package/dist/graph-input-contract.d.ts +102 -0
  39. package/dist/graph-input-contract.d.ts.map +1 -0
  40. package/dist/graph-input-contract.js +637 -0
  41. package/dist/index.d.ts +12 -5
  42. package/dist/index.d.ts.map +1 -1
  43. package/dist/index.js +17 -3
  44. package/dist/managed-files/ci-workflow.d.ts.map +1 -1
  45. package/dist/managed-files/ci-workflow.js +5 -2
  46. package/dist/managed-files/publish-workflow.d.ts +11 -1
  47. package/dist/managed-files/publish-workflow.d.ts.map +1 -1
  48. package/dist/owned-installation.d.ts +14 -0
  49. package/dist/owned-installation.d.ts.map +1 -0
  50. package/dist/owned-installation.js +324 -0
  51. package/dist/producer-receipts.d.ts +19 -0
  52. package/dist/producer-receipts.d.ts.map +1 -0
  53. package/dist/producer-receipts.js +926 -0
  54. package/dist/test-backstop.d.ts +9 -0
  55. package/dist/test-backstop.d.ts.map +1 -0
  56. package/dist/test-backstop.js +10 -0
  57. package/dist/test-clock-policy.d.ts +30 -0
  58. package/dist/test-clock-policy.d.ts.map +1 -0
  59. package/dist/test-clock-policy.js +686 -0
  60. package/dist/testing.d.ts +8 -0
  61. package/dist/testing.d.ts.map +1 -1
  62. package/dist/testing.js +66 -1
  63. package/managed/raw/patches/nx@23.2.1.patch +328 -50
  64. package/managed/raw/tooling/direnv/developer-links.ts +148 -29
  65. package/managed/raw/tooling/direnv/devenv.smoo.nix +7 -7
  66. package/managed/raw/tooling/direnv/github-actions-bootstrap.sh +14 -5
  67. package/managed/raw/tooling/direnv/linked-snapshots.ts +206 -40
  68. package/managed/raw/tooling/direnv/repo-path +15 -2
  69. package/managed/raw/tooling/direnv/setup-environment.ts +179 -115
  70. package/managed/templates/github/actions/cache-node-modules/action.yml +9 -0
  71. package/managed/templates/github/actions/setup-devenv/action.yml +63 -7
  72. package/nextest.toml +1 -1
  73. package/package.json +25 -9
  74. package/src/__tests__/cargo-closure-fixture.ts +113 -0
  75. package/src/__tests__/counted-cargo.ts +8 -25
  76. package/src/__tests__/directory-change.ts +27 -0
  77. package/src/__tests__/fixture-nx-env.ts +7 -0
  78. package/src/__tests__/owned-installation.ts +14 -0
  79. package/src/__tests__/past-tick.ts +12 -0
  80. package/src/bin/smoo-nx-bound-failures.test.ts +1 -1
  81. package/src/bin/smoo-nx-test-clocks.ts +29 -0
  82. package/src/bounded-test-policy.test.ts +36 -22
  83. package/src/bounded-test-policy.ts +18 -32
  84. package/src/bun-bootstrap.test.ts +1 -1
  85. package/src/cargo-closure-input.property.test.ts +50 -0
  86. package/src/cargo-closure-input.test.ts +73 -121
  87. package/src/cargo-closure-input.ts +47 -8
  88. package/src/cargo-closure-key.test.ts +239 -0
  89. package/src/cargo-resolve-cache.test.ts +168 -31
  90. package/src/cargo-source-hash.test.ts +78 -3
  91. package/src/cargo-source-hash.ts +48 -30
  92. package/src/cross-check-policy.test.ts +2 -2
  93. package/src/ensure-built-receipt.test.ts +111 -16
  94. package/src/ensure-built-receipt.ts +294 -149
  95. package/src/ensure-built-witness.test.ts +306 -13
  96. package/src/ensure-built-witness.ts +255 -24
  97. package/src/ensure-built.test.ts +228 -102
  98. package/src/ensure-built.ts +127 -66
  99. package/src/executors/bounded-exec/cpu-pool.test.ts +134 -32
  100. package/src/executors/bounded-exec/cpu-pool.ts +64 -20
  101. package/src/executors/bounded-exec/cpu-tokens.test.ts +2 -2
  102. package/src/executors/bounded-exec/disk-lease.test.ts +47 -13
  103. package/src/executors/bounded-exec/executor-host.test.ts +74 -52
  104. package/src/executors/bounded-exec/executor.test.ts +343 -97
  105. package/src/executors/bounded-exec/executor.ts +192 -56
  106. package/src/executors/bounded-exec/hang-diagnostics.test.ts +14 -8
  107. package/src/executors/bounded-exec/no-space.ts +40 -0
  108. package/src/executors/bounded-exec/progress.test.ts +1 -1
  109. package/src/executors/bounded-exec/ram-temp-host.test.ts +51 -21
  110. package/src/executors/bounded-exec/ram-temp.test.ts +92 -16
  111. package/src/executors/bounded-exec/ram-temp.ts +49 -20
  112. package/src/executors/bounded-exec/verdict.test.ts +127 -22
  113. package/src/executors/bounded-exec/verdict.ts +78 -3
  114. package/src/generators/bounded-test-targets/generator.test.ts +1 -3
  115. package/src/generators/bun-test-tracing/generator.test.ts +2 -2
  116. package/src/generators/bun-test-tracing/generator.ts +2 -2
  117. package/src/graph-input-contract.test.ts +379 -0
  118. package/src/graph-input-contract.ts +627 -0
  119. package/src/index.test.ts +3 -3
  120. package/src/index.ts +24 -3
  121. package/src/linked-snapshots.nx.test.ts +112 -30
  122. package/src/managed-files/ci-workflow.ts +5 -2
  123. package/src/managed-files/publish-workflow.ts +12 -1
  124. package/src/managed-files/repo-path.test.ts +44 -1
  125. package/src/managed-files/setup-devenv-free-disk.test.ts +95 -0
  126. package/src/nextest-code-bound.test.ts +23 -17
  127. package/src/nextest-extraction.test.ts +114 -4
  128. package/src/nextest-partitions.test.ts +7 -1
  129. package/src/nx-cache-restore-mtime.test.ts +2 -2
  130. package/src/nx-daemon-claim.test.ts +10 -8
  131. package/src/nx-daemon-idle-cpu.test.ts +0 -1
  132. package/src/nx-daemon-idle-policy.test.ts +61 -1
  133. package/src/nx-daemon-ignore-files.test.ts +2 -2
  134. package/src/nx-daemon-validity.test.ts +36 -22
  135. package/src/nx-dot-input-policy.test.ts +5 -5
  136. package/src/nx-files-archive-racy.test.ts +1 -1
  137. package/src/nx-incomplete-run-summary.test.ts +1 -1
  138. package/src/nx-plugin-load-errors.test.ts +2 -2
  139. package/src/nx-project-graph-errors.test.ts +70 -0
  140. package/src/nx-skipped-task-exit.test.ts +2 -2
  141. package/src/nx-store-resolution.test.ts +7 -25
  142. package/src/nx-task-output-lease.test.ts +3 -3
  143. package/src/nx-use-local.test.ts +7 -5
  144. package/src/owned-installation.ts +174 -0
  145. package/src/producer-receipts.test.ts +164 -0
  146. package/src/producer-receipts.ts +636 -0
  147. package/src/runtime-input.test.ts +1 -1
  148. package/src/serial-group-wiring.test.ts +4 -4
  149. package/src/task-results.test.ts +18 -16
  150. package/src/test-backstop.ts +11 -0
  151. package/src/test-clock-policy.test.ts +338 -0
  152. package/src/test-clock-policy.ts +708 -0
  153. package/src/test-timings.test.ts +2 -2
  154. package/src/testing.test.ts +5 -5
  155. package/src/testing.ts +67 -1
  156. package/src/typecheck-test-policy.test.ts +1 -1
  157. package/src/typescript-resolution.test.ts +76 -0
  158. package/test-timings.json +8 -0
@@ -25,6 +25,7 @@ import type * as NxTaskHasher from 'nx/src/hasher/task-hasher';
25
25
  import type * as NxNative from 'nx/src/native';
26
26
  import type * as NxTransformObjects from 'nx/src/native/transform-objects';
27
27
  import type * as NxExecutionHooks from 'nx/src/project-graph/plugins/tasks-execution-hooks';
28
+ import type * as NxProjectGraphRuntime from 'nx/src/project-graph/project-graph';
28
29
  import type * as NxProjectRootMappings from 'nx/src/project-graph/utils/find-project-for-path';
29
30
  import type * as NxCache from 'nx/src/tasks-runner/cache';
30
31
  import type * as NxCreateTaskGraph from 'nx/src/tasks-runner/create-task-graph';
@@ -38,21 +39,26 @@ import type { NxArgs } from 'nx/src/utils/command-line-utils';
38
39
  import type * as NxPackageJson from 'nx/src/utils/package-json';
39
40
  import type * as NxPerfLogging from 'nx/src/utils/perf-logging';
40
41
  import type * as NxWorkspaceRoot from 'nx/src/utils/workspace-root';
41
- import { boundedReceiptRuntimes, type ProviderInputProof, providerReceipt } from './ensure-built-receipt.js';
42
+ import { boundedReceiptRuntimes, type ProviderInputProof } from './ensure-built-receipt.js';
42
43
  import {
43
44
  type ArtifactWitness,
45
+ censusStart,
44
46
  type GraphWitness,
45
47
  graphInstallationIdentity,
48
+ movedNodes,
46
49
  nodeStamp,
47
50
  nodesUnchanged,
48
51
  readArtifactWitness,
49
52
  readGraphWitness,
53
+ recordedStamp,
50
54
  reusableGraph,
51
55
  snapshotsEqual,
52
56
  workspaceSnapshot,
53
57
  writeWitness,
54
58
  } from './ensure-built-witness.js';
59
+ import { graphInputContractIdentity } from './graph-input-contract.js';
55
60
  import { managedAssetsRoot } from './managed-assets.js';
61
+ import { readProducerReceipt } from './producer-receipts.js';
56
62
 
57
63
  interface NxRuntimeModules {
58
64
  readonly 'nx/src/config/nx-json': typeof NxConfiguration;
@@ -62,6 +68,7 @@ interface NxRuntimeModules {
62
68
  readonly 'nx/src/native': typeof NxNative;
63
69
  readonly 'nx/src/native/transform-objects': typeof NxTransformObjects;
64
70
  readonly 'nx/src/project-graph/utils/find-project-for-path': typeof NxProjectRootMappings;
71
+ readonly 'nx/src/project-graph/project-graph': typeof NxProjectGraphRuntime;
65
72
  readonly 'nx/src/utils/package-json': typeof NxPackageJson;
66
73
  readonly 'nx/src/project-graph/plugins/tasks-execution-hooks': typeof NxExecutionHooks;
67
74
  readonly 'nx/src/tasks-runner/cache': typeof NxCache;
@@ -79,7 +86,7 @@ type WorkspaceNxRequire = <Specifier extends keyof NxRuntimeModules>(
79
86
  specifier: Specifier,
80
87
  ) => NxRuntimeModules[Specifier];
81
88
 
82
- function createWorkspaceNxRequire(workspaceRoot: string): WorkspaceNxRequire {
89
+ export function createWorkspaceNxRequire(workspaceRoot: string): WorkspaceNxRequire {
83
90
  const requireFromWorkspace = createRequire(join(workspaceRoot, 'package.json'));
84
91
  return function requireNx<Specifier extends keyof NxRuntimeModules>(
85
92
  specifier: Specifier,
@@ -186,6 +193,50 @@ export type EnsureBuiltOutputReceipt =
186
193
  readonly sha256: string;
187
194
  };
188
195
 
196
+ export type EnsureBuiltProducerExpandedInputs = {
197
+ readonly [Key in keyof NxNative.HashInputs]: readonly NxNative.HashInputs[Key][number][];
198
+ };
199
+ export interface EnsureBuiltProducerFileInput {
200
+ readonly path: string;
201
+ readonly mode: number;
202
+ readonly gitBlob: string;
203
+ readonly sha256: string;
204
+ }
205
+ export interface EnsureBuiltProducerTaskInputs {
206
+ readonly taskId: string;
207
+ readonly overrides: Readonly<Task['overrides']>;
208
+ readonly declared: readonly NonNullable<
209
+ NonNullable<ProjectGraphProjectNode['data']['targets']>[string]['inputs']
210
+ >[number][];
211
+ readonly expanded: EnsureBuiltProducerExpandedInputs;
212
+ readonly hashDetails: Readonly<NxNative.TaskHashDetails>;
213
+ readonly files: readonly EnsureBuiltProducerFileInput[];
214
+ }
215
+ export interface EnsureBuiltProducerTool {
216
+ readonly kind: 'module' | 'executable';
217
+ readonly name: string;
218
+ readonly from: string;
219
+ readonly path: string;
220
+ readonly physicalPath: string;
221
+ readonly missing: readonly string[];
222
+ readonly resolutions: readonly { readonly name: string; readonly from: string; readonly target: string | null }[];
223
+ /** Absolute node paths bind the physical closure, including package-owned build-volume aliases. */
224
+ readonly nodes: readonly EnsureBuiltOutputReceipt[];
225
+ readonly sha256: string;
226
+ }
227
+ export interface EnsureBuiltProducerReceipt {
228
+ readonly version: 1;
229
+ readonly target: string;
230
+ readonly argv: readonly string[];
231
+ readonly trackedTreeClean: true;
232
+ /** Declared runtime/environment identity from actual Nx completion hashes, never a consumer environment hash. */
233
+ readonly environmentIdentity: string;
234
+ readonly inputs: readonly EnsureBuiltProducerTaskInputs[];
235
+ readonly tools: readonly EnsureBuiltProducerTool[];
236
+ /** SHA-256 of the independently censused task/public-output records, never terminal output. */
237
+ readonly outputSha256: string;
238
+ readonly wallTimeMs: number;
239
+ }
189
240
  /** Git provenance covers file inputs only; runtime/env identity remains Nx's current task hash. */
190
241
  export interface EnsureBuiltReceipt {
191
242
  readonly workspaceRoot: string;
@@ -193,6 +244,7 @@ export interface EnsureBuiltReceipt {
193
244
  readonly headTree: string;
194
245
  readonly fileInputProvenance: 'exact-file-inputs' | 'conservative-provider-source';
195
246
  readonly runtimeEnvironmentProvenance: 'nx-task-hash-only';
247
+ readonly producer: EnsureBuiltProducerReceipt;
196
248
  readonly tasks: readonly {
197
249
  readonly id: string;
198
250
  readonly hash: string;
@@ -683,7 +735,11 @@ type PreparedWorkspaceProbe =
683
735
  | ReadyWorkspaceProbe
684
736
  | { readonly kind: 'no-daemon'; readonly reason: Extract<MissReason, { kind: 'no-daemon' }> };
685
737
 
686
- async function prepareWorkspaceProbe(workspaceRoot: string, selector: TargetSelector): Promise<PreparedWorkspaceProbe> {
738
+ async function prepareWorkspaceProbe(
739
+ workspaceRoot: string,
740
+ selector: TargetSelector,
741
+ forceGraphRpc = false,
742
+ ): Promise<PreparedWorkspaceProbe> {
687
743
  const taskId = selectorTaskId(selector);
688
744
  // This explicit fallback path loads no Nx. The build operation may run the
689
745
  // checkout-local CLI; the read-only operation can only report the miss.
@@ -715,7 +771,7 @@ async function prepareWorkspaceProbe(workspaceRoot: string, selector: TargetSele
715
771
  setEnvVarsBasedOnArgs(nxArgs, loadDotEnvFiles);
716
772
 
717
773
  performance.mark('ensureBuilt:graph:start');
718
- const projectGraph = await currentProjectGraph(workspaceRoot, nxJson, requireNx);
774
+ const projectGraph = await currentProjectGraph(workspaceRoot, nxJson, requireNx, forceGraphRpc);
719
775
  nxJson = readNxJson();
720
776
  requireProject(projectGraph, selector.project);
721
777
  const taskGraph = createTaskGraph(
@@ -783,64 +839,31 @@ async function buildPreparedTarget(
783
839
  }
784
840
 
785
841
  async function probeBuiltInWorkspace(workspaceRoot: string, selector: TargetSelector): Promise<EnsureBuiltProbeResult> {
786
- const prepared = await prepareWorkspaceProbe(workspaceRoot, selector);
787
- if (prepared.kind === 'no-daemon') return { disposition: 'miss', reason: prepared.reason };
788
- const probeSource = captureProbeSource(workspaceRoot, prepared.requireNx);
789
- return withTaskHooks(workspaceRoot, selector, prepared, async (root, selected, current) => {
790
- const { nxJson, nxArgs, projectGraph, taskGraph, tasks, requireNx } = current;
791
- const taskId = selectorTaskId(selected);
792
- performance.mark('ensureBuilt:probe:start');
793
- const reason =
794
- missBeforeProbe(process.env, taskId) ??
795
- (await probe(root, nxJson, nxArgs, projectGraph, taskGraph, tasks, requireNx, true));
796
- performance.measure('ensureBuilt:probe', 'ensureBuilt:probe:start');
797
- if (reason !== null) return { disposition: 'miss', reason } satisfies EnsureBuiltProbeResult;
798
- let result = providerReceipt(
799
- root,
800
- taskId,
801
- tasks,
802
- providerInputProof(root, nxJson, projectGraph, taskGraph, requireNx),
803
- requireNx('nx/src/native').expandOutputs,
804
- );
805
- if (result.disposition === 'hit') {
806
- const after = captureProbeSource(root, requireNx);
807
- if (!snapshotsEqual(probeSource, after) || !nodesUnchanged(probeSource.nodes)) {
808
- result = {
809
- disposition: 'failed',
810
- reason: {
811
- kind: 'provider-proof',
812
- taskId,
813
- workspaceRoot: root,
814
- detail: 'Provider source nodes or membership moved across Nx hashing/receipt capture',
815
- },
816
- exitCode: 1,
817
- signal: null,
818
- };
819
- }
820
- }
821
- if (result.disposition === 'failed') {
822
- process.stderr.write(`probeBuilt: ${result.reason.workspaceRoot}: ${result.reason.detail}\n`);
823
- }
824
- return result;
825
- });
842
+ const taskId = selectorTaskId(selector);
843
+ const receipt = readProducerReceipt(workspaceRoot, taskId);
844
+ 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);
848
+ const detail =
849
+ prepared.kind === 'no-daemon'
850
+ ? `${receipt.detail}; authoritative graph unavailable: ${describeMiss(prepared.reason)}`
851
+ : receipt.detail;
852
+ process.stderr.write(`probeBuilt: ${workspaceRoot}: ${taskId}: ${detail}\n`);
853
+ return {
854
+ disposition: 'failed',
855
+ reason: { kind: 'provider-proof', taskId, workspaceRoot, detail },
856
+ exitCode: 1,
857
+ signal: null,
858
+ };
826
859
  }
827
860
 
828
- function captureProbeSource(
829
- workspaceRoot: string,
830
- requireNx: WorkspaceNxRequire,
831
- ): ReturnType<typeof workspaceSnapshot> {
832
- const { WorkspaceContext } = requireNx('nx/src/native');
833
- const { workspaceDataDirectoryForWorkspace } = requireNx('nx/src/utils/cache-directory');
834
- const data = workspaceDataDirectoryForWorkspace(workspaceRoot);
835
- const previous = readGraphWitness(join(data, 'ensure-built-graph.json'));
836
- return workspaceSnapshot(workspaceRoot, new WorkspaceContext(workspaceRoot, data), previous?.snapshot);
837
- }
838
861
  /** Nx's native plan is the FILE selection oracle, not a second glob planner.
839
862
  * Config identities skipped by the inspector are included explicitly. A
840
863
  * runtime identity is bounded only by the owning toolchain recipe + a declared
841
864
  * pin; opaque commands and unavailable native proof categorically refuse.
842
865
  */
843
- function providerInputProof(
866
+ export function providerInputProof(
844
867
  workspaceRoot: string,
845
868
  nxJson: NxJsonConfiguration,
846
869
  projectGraph: ProjectGraph,
@@ -886,6 +909,7 @@ function providerInputProof(
886
909
  for (const dependency of projectGraph.dependencies[name] ?? []) includeProject(dependency.target);
887
910
  }
888
911
  let conservative = false;
912
+ let hasRuntime = false;
889
913
  const projectConfigs = Object.fromEntries(
890
914
  Object.entries(projectGraph.nodes).map(([name, node]) => [name, node.data]),
891
915
  );
@@ -901,6 +925,7 @@ function providerInputProof(
901
925
  };
902
926
  }
903
927
  for (const file of input.files) files.add(file);
928
+ hasRuntime ||= input.runtime.length > 0;
904
929
  if (input.runtime.length > 0 || input.environment.length > 0) conservative = true;
905
930
  const node = projectGraph.nodes[task.target.project];
906
931
  if (node === undefined) throw new Error(`Lost project for receipt ${task.id}`);
@@ -933,11 +958,26 @@ function providerInputProof(
933
958
  ]) {
934
959
  if (existsSync(join(workspaceRoot, path))) files.add(path);
935
960
  }
936
- const installation = graphInstallationIdentity(
937
- workspaceRoot,
938
- nxJson,
939
- requireNx('nx/src/utils/package-json').readModulePackageJson,
940
- );
961
+ const installation = hasRuntime
962
+ ? graphInstallationIdentity(
963
+ workspaceRoot,
964
+ nxJson,
965
+ requireNx('nx/src/utils/package-json').readModulePackageJson,
966
+ requireNx('nx/src/native').expandOutputs,
967
+ graphInputContractIdentity(
968
+ workspaceRoot,
969
+ nxJson,
970
+ workspaceSnapshot(
971
+ workspaceRoot,
972
+ new (requireNx('nx/src/native').WorkspaceContext)(
973
+ workspaceRoot,
974
+ requireNx('nx/src/utils/cache-directory').workspaceDataDirectoryForWorkspace(workspaceRoot),
975
+ ),
976
+ undefined,
977
+ ),
978
+ ),
979
+ )
980
+ : null;
941
981
  const runtimeSource = boundedReceiptRuntimes(inputs, [...directories], [...files], installation !== null);
942
982
  return {
943
983
  kind: conservative ? 'conservative-provider-source' : 'exact-file-inputs',
@@ -989,9 +1029,10 @@ async function currentProjectGraph(
989
1029
  workspaceRoot: string,
990
1030
  nxJson: NxJsonConfiguration,
991
1031
  requireNx: WorkspaceNxRequire,
1032
+ forceGraphRpc: boolean,
992
1033
  ): Promise<ProjectGraph> {
993
1034
  const { daemonClient } = requireNx('nx/src/daemon/client/client');
994
- const { WorkspaceContext } = requireNx('nx/src/native');
1035
+ const { WorkspaceContext, expandOutputs } = requireNx('nx/src/native');
995
1036
  const { workspaceDataDirectoryForWorkspace } = requireNx('nx/src/utils/cache-directory');
996
1037
  const packageResolution = requireNx('nx/src/utils/package-json');
997
1038
  const data = workspaceDataDirectoryForWorkspace(workspaceRoot);
@@ -1010,15 +1051,29 @@ async function currentProjectGraph(
1010
1051
  await daemonClient.updateWorkspaceContext(created, updated, deleted);
1011
1052
  }
1012
1053
  performance.measure('ensureBuilt:inputs', 'ensureBuilt:inputs:start');
1013
- const installation = graphInstallationIdentity(workspaceRoot, nxJson, packageResolution.readModulePackageJson);
1054
+ const installation = graphInstallationIdentity(
1055
+ workspaceRoot,
1056
+ nxJson,
1057
+ packageResolution.readModulePackageJson,
1058
+ expandOutputs,
1059
+ graphInputContractIdentity(workspaceRoot, nxJson, before),
1060
+ );
1014
1061
  const cached = nodesUnchanged(before.nodes)
1015
1062
  ? reusableGraph(witness, workspaceRoot, environment, installation, before)
1016
1063
  : null;
1017
- if (cached !== null) return cached;
1064
+ if (!forceGraphRpc && cached !== null) return cached;
1018
1065
  const { projectGraph } = await daemonClient.getProjectGraphAndSourceMaps();
1019
1066
  const after = workspaceSnapshot(workspaceRoot, new WorkspaceContext(workspaceRoot, data), before);
1020
- const finalInstallation = graphInstallationIdentity(workspaceRoot, nxJson, packageResolution.readModulePackageJson);
1021
- const stableGeneration = snapshotsEqual(before, after) && nodesUnchanged(before.nodes);
1067
+ const finalInstallation = graphInstallationIdentity(
1068
+ workspaceRoot,
1069
+ nxJson,
1070
+ packageResolution.readModulePackageJson,
1071
+ expandOutputs,
1072
+ graphInputContractIdentity(workspaceRoot, nxJson, after),
1073
+ );
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;
1022
1077
  if (installation !== null && installation === finalInstallation && stableGeneration) {
1023
1078
  const next: GraphWitness = { version: 1, workspaceRoot, environment, installation, snapshot: after, projectGraph };
1024
1079
  writeWitness(witnessPath, next);
@@ -1242,6 +1297,8 @@ export function compareWithArtifacts(
1242
1297
  if (!isSystemError(error)) throw error;
1243
1298
  }
1244
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();
1245
1302
  const comparison: ArtifactComparison = {
1246
1303
  expandOutputs,
1247
1304
  left: Buffer.allocUnsafe(COMPARE_CHUNK_BYTES),
@@ -1262,7 +1319,11 @@ export function compareWithArtifacts(
1262
1319
  version: 1,
1263
1320
  workspaceRoot,
1264
1321
  claims: binding,
1265
- 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
+ })),
1266
1327
  };
1267
1328
  writeWitness(witnessPath, next);
1268
1329
  }
@@ -9,6 +9,7 @@ import {
9
9
  CPU_POOL_ENV,
10
10
  type CpuPool,
11
11
  createCpuPool,
12
+ type HostWaits,
12
13
  type InheritedCpuPool,
13
14
  readInheritedCpuPool,
14
15
  takeParentCpuTokens,
@@ -19,10 +20,49 @@ import { CPU_TOKENS_ENV, type TokenGrant } from './cpu-tokens.js';
19
20
  type PoolReply =
20
21
  | { ok: true; lease: 'queued' }
21
22
  | { ok: true; lease: 'granted'; tokens: number; pool?: string }
23
+ | { ok: true; wait: 'held' }
22
24
  | { ok: false; code: 'invalid-request'; error: string };
23
25
 
24
26
  const parseReply = typia.json.createAssertParse<PoolReply>();
25
27
 
28
+ /**
29
+ * A run's host waits as the pool reports them: how many are held now, every begin and end in order, and `reaches(n)`,
30
+ * which resolves once the count is `n` (the pool hears a close after its client does).
31
+ */
32
+ interface CountedWaits extends HostWaits {
33
+ readonly held: () => number;
34
+ readonly events: ('begin' | 'end')[];
35
+ reaches(count: number): Promise<void>;
36
+ }
37
+
38
+ function countedWaits(): CountedWaits {
39
+ const events: ('begin' | 'end')[] = [];
40
+ const watchers: { count: number; done: () => void }[] = [];
41
+ let held = 0;
42
+ const changed = (): void => {
43
+ for (const watcher of watchers.filter((each) => each.count === held)) {
44
+ watchers.splice(watchers.indexOf(watcher), 1);
45
+ watcher.done();
46
+ }
47
+ };
48
+ return {
49
+ held: () => held,
50
+ events,
51
+ reaches: (count) =>
52
+ count === held ? Promise.resolve() : new Promise<void>((done) => watchers.push({ count, done })),
53
+ begin: () => {
54
+ held += 1;
55
+ events.push('begin');
56
+ changed();
57
+ },
58
+ end: () => {
59
+ held -= 1;
60
+ events.push('end');
61
+ changed();
62
+ },
63
+ };
64
+ }
65
+
26
66
  type Granted = Extract<TokenGrant, { granted: true }>;
27
67
 
28
68
  /** A raw child connection: its replies come one line at a time, and `closed` resolves when it ends. */
@@ -57,8 +97,12 @@ afterEach(async () => {
57
97
  });
58
98
 
59
99
  /** Make a pool of `tokens` and read its endpoint back through the same path a child uses. */
60
- async function openPool(tokens: number, closed: Promise<void> = NEVER): Promise<Opened> {
61
- const made = await createCpuPool(tokens, closed);
100
+ async function openPool(
101
+ tokens: number,
102
+ closed: Promise<void> = NEVER,
103
+ waits: HostWaits = countedWaits(),
104
+ ): Promise<Opened> {
105
+ const made = await createCpuPool(tokens, closed, waits);
62
106
  if (!made.ok) {
63
107
  throw new Error(made.error);
64
108
  }
@@ -72,7 +116,7 @@ async function openPool(tokens: number, closed: Promise<void> = NEVER): Promise<
72
116
 
73
117
  /** A granted lease for a child that the pool must admit: a refusal here is a test failure. */
74
118
  async function takeOrThrow(parent: InheritedCpuPool, want: number): Promise<Granted> {
75
- const grant = await takeParentCpuTokens(parent, want, 8);
119
+ const grant = await takeParentCpuTokens(parent, want);
76
120
  if (!grant.granted) {
77
121
  throw new Error(`the pool refused ${want} tokens: ${grant.reason}`);
78
122
  }
@@ -80,11 +124,11 @@ async function takeOrThrow(parent: InheritedCpuPool, want: number): Promise<Gran
80
124
  }
81
125
 
82
126
  /**
83
- * The count a child is granted for `want` at `cores`. The grant is released and its close awaited before
127
+ * The count a child is granted for `want`. The grant is released and its close awaited before
84
128
  * returning, so the next ask starts from the pool as this one left it.
85
129
  */
86
- async function grantedFor(parent: InheritedCpuPool, want: number, cores: number): Promise<number> {
87
- const grant = await takeParentCpuTokens(parent, want, cores);
130
+ async function grantedFor(parent: InheritedCpuPool, want: number): Promise<number> {
131
+ const grant = await takeParentCpuTokens(parent, want);
88
132
  if (!grant.granted) {
89
133
  throw new Error(`the pool refused ${want} tokens: ${grant.reason}`);
90
134
  }
@@ -182,7 +226,7 @@ describe('the parent-held CPU pool', () => {
182
226
  const cargo = childOf(parent.socket, JSON.stringify({ op: 'cpu-pool', want: 4, children: true }));
183
227
  expect(await cargo.next()).toEqual(QUEUED);
184
228
  const granted = await cargo.next();
185
- if (!granted.ok || granted.lease !== 'granted' || granted.pool === undefined)
229
+ if (!granted.ok || !('lease' in granted) || granted.lease !== 'granted' || granted.pool === undefined)
186
230
  throw new Error('no live child pool granted');
187
231
  expect(granted.tokens).toBe(4);
188
232
  expect(granted.pool).not.toBe(parent.socket);
@@ -296,23 +340,23 @@ describe('the parent-held CPU pool', () => {
296
340
  holder.kill('SIGKILL');
297
341
  await holder.exited;
298
342
  }
299
- }, 20_000);
343
+ });
300
344
 
301
345
  it('treats a dead or non-socket endpoint as absent, and a malformed endpoint as no pool', async () => {
302
346
  const { pool, parent } = await openPool(2);
303
347
  expect(await pool.close()).toEqual({ ok: true });
304
- expect(await takeParentCpuTokens(parent, 1, 2)).toMatchObject({ granted: false, cause: 'absent' });
348
+ expect(await takeParentCpuTokens(parent, 1)).toMatchObject({ granted: false, cause: 'absent' });
305
349
 
306
350
  const stale = await ownedSocketDirectory('cpu-stale-', 'p.sock');
307
351
  directories.push(stale);
308
352
  await writeFile(join(stale, 'p.sock'), 'not a socket');
309
- expect(await takeParentCpuTokens({ socket: join(stale, 'p.sock'), tokens: 2 }, 1, 2)).toMatchObject({
353
+ expect(await takeParentCpuTokens({ socket: join(stale, 'p.sock'), tokens: 2 }, 1)).toMatchObject({
310
354
  granted: false,
311
355
  cause: 'absent',
312
356
  });
313
357
 
314
358
  expect(readInheritedCpuPool({ [CPU_POOL_ENV]: 'a\0b', [CPU_TOKENS_ENV]: '2' })).toBeNull();
315
- expect(await takeParentCpuTokens({ socket: 'a\0b', tokens: 2 }, 1, 2)).toMatchObject({
359
+ expect(await takeParentCpuTokens({ socket: 'a\0b', tokens: 2 }, 1)).toMatchObject({
316
360
  granted: false,
317
361
  cause: 'other',
318
362
  });
@@ -321,8 +365,25 @@ describe('the parent-held CPU pool', () => {
321
365
  expect(readInheritedCpuPool({ [CPU_TOKENS_ENV]: '4' })).toBeNull();
322
366
  });
323
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
+
324
385
  it('names the endpoint and count it holds, in private owned paths, and removes them on close', async () => {
325
- const made = await createCpuPool(3, NEVER);
386
+ const made = await createCpuPool(3, NEVER, countedWaits());
326
387
  if (!made.ok) {
327
388
  throw new Error(made.error);
328
389
  }
@@ -357,6 +418,49 @@ describe('the parent-held CPU pool', () => {
357
418
  expect((await takeOrThrow(parent, 2)).tokens).toBe(2);
358
419
  });
359
420
 
421
+ it("counts a descendant's host wait for as long as its connection holds it, through a child pool too", async () => {
422
+ const waits = countedWaits();
423
+ const { parent } = await openPool(4, NEVER, waits);
424
+ const held: PoolReply = { ok: true, wait: 'held' };
425
+ const direct = childOf(parent.socket, JSON.stringify({ op: 'host-wait' }));
426
+ expect(await direct.next()).toEqual(held);
427
+ expect(waits.held()).toBe(1);
428
+ // A wait holds no token: the whole pool is still there to grant.
429
+ expect(await grantedFor(parent, 4)).toBe(4);
430
+
431
+ // Cargo's launcher is served a child pool; the test it runs reports its waits there, to the same run.
432
+ const cargo = childOf(parent.socket, JSON.stringify({ op: 'cpu-pool', want: 2, children: true }));
433
+ expect(await cargo.next()).toEqual(QUEUED);
434
+ const granted = await cargo.next();
435
+ if (!granted.ok || !('lease' in granted) || granted.lease !== 'granted' || granted.pool === undefined) {
436
+ throw new Error(`no live child pool granted: ${JSON.stringify(granted)}`);
437
+ }
438
+ const nested = childOf(granted.pool, JSON.stringify({ op: 'host-wait' }));
439
+ expect(await nested.next()).toEqual(held);
440
+ expect(waits.held()).toBe(2);
441
+
442
+ direct.socket.destroy();
443
+ await direct.closed;
444
+ // The server sees the close after the client does; the count follows it.
445
+ await waits.reaches(1);
446
+ // The child pool's close ends the waits its children held.
447
+ cargo.socket.destroy();
448
+ await nested.closed;
449
+ await waits.reaches(0);
450
+ expect(waits.events).toEqual(['begin', 'begin', 'end', 'end']);
451
+ });
452
+
453
+ it('ends a host wait whose holder writes past its one request', async () => {
454
+ const waits = countedWaits();
455
+ const { parent } = await openPool(1, NEVER, waits);
456
+ const waiter = childOf(parent.socket, JSON.stringify({ op: 'host-wait' }));
457
+ expect(await waiter.next()).toEqual({ ok: true, wait: 'held' });
458
+ waiter.socket.write(`${JSON.stringify({ op: 'host-wait' })}\n`);
459
+ await waiter.closed;
460
+ await waits.reaches(0);
461
+ expect(waits.events).toEqual(['begin', 'end']);
462
+ });
463
+
360
464
  it('destroys a granted child that sends a second request, returning its tokens', async () => {
361
465
  const { parent } = await openPool(2);
362
466
  const first = childOf(parent.socket, JSON.stringify({ op: 'cpu-pool', want: 2 }));
@@ -380,47 +484,45 @@ describe('the parent-held CPU pool', () => {
380
484
  const upstream = await openPool(4);
381
485
  const lent = await takeOrThrow(upstream.parent, 2);
382
486
  const sub = await openPool(lent.tokens, lent.closed);
383
- const grandchild = await takeParentCpuTokens(sub.parent, 2, 2);
487
+ const grandchild = await takeParentCpuTokens(sub.parent, 2);
384
488
  if (!grandchild.granted) {
385
489
  throw new Error(`the sub-pool refused its grandchild: ${grandchild.reason}`);
386
490
  }
387
- expect(grandchild.tokens).toBe(1);
491
+ // The grandchild asks its whole want and the sub-pool grants all it was lent.
492
+ expect(grandchild.tokens).toBe(2);
388
493
 
389
494
  expect(await upstream.pool.close()).toEqual({ ok: true });
390
495
  await lent.closed;
391
496
  await grandchild.closed;
392
497
  expect(await sub.pool.close()).toEqual({ ok: true });
393
498
  expect(existsSync(dirname(sub.parent.socket))).toBe(false);
394
- expect(await takeParentCpuTokens(sub.parent, 1, 2)).toMatchObject({ granted: false, cause: 'absent' });
395
- expect(await takeParentCpuTokens(upstream.parent, 1, 8)).toMatchObject({ granted: false, cause: 'absent' });
499
+ expect(await takeParentCpuTokens(sub.parent, 1)).toMatchObject({ granted: false, cause: 'absent' });
500
+ expect(await takeParentCpuTokens(upstream.parent, 1)).toMatchObject({ granted: false, cause: 'absent' });
396
501
  });
397
502
 
398
- it('asks for min(want, inherited count, ceil(cores / 2)), and the grant follows that ask', async () => {
503
+ it('asks for its own want whatever count it inherited, and the grant follows that ask', async () => {
399
504
  const cases = [
400
- { want: 1, inherited: 8, cores: 8, tokens: 1 },
401
- { want: 8, inherited: 2, cores: 8, tokens: 2 },
402
- { want: 8, inherited: 8, cores: 8, tokens: 4 },
403
- { want: 8, inherited: 8, cores: 5, tokens: 3 },
404
- { want: 8, inherited: 8, cores: 3, tokens: 2 },
405
- { want: 8, inherited: 8, cores: 1, tokens: 1 },
505
+ { want: 1, inherited: 8, tokens: 1 },
506
+ { want: 8, inherited: 2, tokens: 8 },
507
+ { want: 8, inherited: 8, tokens: 8 },
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
406
510
  ];
407
511
  for (const check of cases) {
408
- // A fresh full pool isolates the clamp from delivery of the previous client's close event.
512
+ // A fresh full pool isolates each ask from delivery of the previous client's close event.
409
513
  const { parent } = await openPool(8);
410
- expect(await grantedFor({ socket: parent.socket, tokens: check.inherited }, check.want, check.cores)).toBe(
411
- check.tokens,
412
- );
514
+ expect(await grantedFor({ socket: parent.socket, tokens: check.inherited }, check.want)).toBe(check.tokens);
413
515
  }
414
516
  });
415
517
 
416
518
  it('never grants more than the live pool holds, even when the inherited count claims more', async () => {
417
519
  const { parent } = await openPool(2);
418
- // The child's descriptor claims 8 tokens: the ask is min(8, 8, 8), but the live pool holds 2 and grants 2.
419
- expect(await grantedFor({ socket: parent.socket, tokens: 8 }, 8, 16)).toBe(2);
520
+ // The child asks 8, but the live pool holds 2 and grants 2.
521
+ expect(await grantedFor({ socket: parent.socket, tokens: 8 }, 8)).toBe(2);
420
522
  });
421
523
 
422
524
  it('reports a teardown that cannot remove its directory, and reports the same outcome on every close', async () => {
423
- const made = await createCpuPool(2, NEVER);
525
+ const made = await createCpuPool(2, NEVER, countedWaits());
424
526
  if (!made.ok) {
425
527
  throw new Error(made.error);
426
528
  }
@@ -446,9 +548,9 @@ describe('the parent-held CPU pool', () => {
446
548
 
447
549
  it('makes no pool when its grant closes during setup, and refuses a non-positive count', async () => {
448
550
  const before = cpuPoolDirectories();
449
- const made = await createCpuPool(2, Promise.resolve());
551
+ const made = await createCpuPool(2, Promise.resolve(), countedWaits());
450
552
  expect(made.ok).toBe(false);
451
553
  expect(cpuPoolDirectories()).toEqual(before);
452
- await expect(createCpuPool(0, NEVER)).rejects.toBeInstanceOf(RangeError);
554
+ await expect(createCpuPool(0, NEVER, countedWaits())).rejects.toBeInstanceOf(RangeError);
453
555
  });
454
556
  });