@smoothbricks/nx-plugin 0.4.28 → 0.4.30

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (158) hide show
  1. package/README.md +277 -89
  2. package/dist/bin/smoo-nx-test-clocks.d.ts +3 -0
  3. package/dist/bin/smoo-nx-test-clocks.d.ts.map +1 -0
  4. package/dist/bin/smoo-nx-test-clocks.js +28 -0
  5. package/dist/bounded-test-policy.d.ts +8 -8
  6. package/dist/bounded-test-policy.d.ts.map +1 -1
  7. package/dist/bounded-test-policy.js +18 -30
  8. package/dist/cargo-closure-input.d.ts +23 -0
  9. package/dist/cargo-closure-input.d.ts.map +1 -1
  10. package/dist/cargo-closure-input.js +41 -9
  11. package/dist/cargo-source-hash.d.ts.map +1 -1
  12. package/dist/cargo-source-hash.js +42 -19
  13. package/dist/ensure-built-receipt.d.ts +40 -6
  14. package/dist/ensure-built-receipt.d.ts.map +1 -1
  15. package/dist/ensure-built-receipt.js +297 -159
  16. package/dist/ensure-built-witness.d.ts +65 -4
  17. package/dist/ensure-built-witness.d.ts.map +1 -1
  18. package/dist/ensure-built-witness.js +2237 -23
  19. package/dist/ensure-built.d.ts +99 -1
  20. package/dist/ensure-built.d.ts.map +1 -1
  21. package/dist/ensure-built.js +45 -56
  22. package/dist/executors/bounded-exec/cpu-pool.d.ts +22 -7
  23. package/dist/executors/bounded-exec/cpu-pool.d.ts.map +1 -1
  24. package/dist/executors/bounded-exec/cpu-pool.js +51 -22
  25. package/dist/executors/bounded-exec/executor.d.ts +12 -3
  26. package/dist/executors/bounded-exec/executor.d.ts.map +1 -1
  27. package/dist/executors/bounded-exec/executor.js +174 -51
  28. package/dist/executors/bounded-exec/no-space.d.ts +8 -0
  29. package/dist/executors/bounded-exec/no-space.d.ts.map +1 -0
  30. package/dist/executors/bounded-exec/no-space.js +31 -0
  31. package/dist/executors/bounded-exec/ram-temp.d.ts +20 -11
  32. package/dist/executors/bounded-exec/ram-temp.d.ts.map +1 -1
  33. package/dist/executors/bounded-exec/ram-temp.js +42 -17
  34. package/dist/executors/bounded-exec/verdict.d.ts +34 -4
  35. package/dist/executors/bounded-exec/verdict.d.ts.map +1 -1
  36. package/dist/executors/bounded-exec/verdict.js +54 -3
  37. package/dist/generators/bun-test-tracing/generator.js +2 -2
  38. package/dist/graph-input-contract.d.ts +102 -0
  39. package/dist/graph-input-contract.d.ts.map +1 -0
  40. package/dist/graph-input-contract.js +637 -0
  41. package/dist/index.d.ts +12 -5
  42. package/dist/index.d.ts.map +1 -1
  43. package/dist/index.js +17 -3
  44. package/dist/managed-files/ci-workflow.d.ts.map +1 -1
  45. package/dist/managed-files/ci-workflow.js +5 -2
  46. package/dist/managed-files/publish-workflow.d.ts +11 -1
  47. package/dist/managed-files/publish-workflow.d.ts.map +1 -1
  48. package/dist/owned-installation.d.ts +14 -0
  49. package/dist/owned-installation.d.ts.map +1 -0
  50. package/dist/owned-installation.js +324 -0
  51. package/dist/producer-receipts.d.ts +19 -0
  52. package/dist/producer-receipts.d.ts.map +1 -0
  53. package/dist/producer-receipts.js +926 -0
  54. package/dist/test-backstop.d.ts +9 -0
  55. package/dist/test-backstop.d.ts.map +1 -0
  56. package/dist/test-backstop.js +10 -0
  57. package/dist/test-clock-policy.d.ts +30 -0
  58. package/dist/test-clock-policy.d.ts.map +1 -0
  59. package/dist/test-clock-policy.js +686 -0
  60. package/dist/testing.d.ts +8 -0
  61. package/dist/testing.d.ts.map +1 -1
  62. package/dist/testing.js +66 -1
  63. package/managed/raw/patches/nx@23.2.1.patch +328 -50
  64. package/managed/raw/tooling/direnv/developer-links.ts +148 -29
  65. package/managed/raw/tooling/direnv/devenv.smoo.nix +7 -7
  66. package/managed/raw/tooling/direnv/github-actions-bootstrap.sh +14 -5
  67. package/managed/raw/tooling/direnv/linked-snapshots.ts +206 -40
  68. package/managed/raw/tooling/direnv/repo-path +15 -2
  69. package/managed/raw/tooling/direnv/setup-environment.ts +179 -115
  70. package/managed/templates/github/actions/cache-node-modules/action.yml +9 -0
  71. package/managed/templates/github/actions/setup-devenv/action.yml +63 -7
  72. package/nextest.toml +1 -1
  73. package/package.json +25 -9
  74. package/src/__tests__/cargo-closure-fixture.ts +113 -0
  75. package/src/__tests__/counted-cargo.ts +8 -25
  76. package/src/__tests__/directory-change.ts +27 -0
  77. package/src/__tests__/fixture-nx-env.ts +7 -0
  78. package/src/__tests__/owned-installation.ts +14 -0
  79. package/src/__tests__/past-tick.ts +12 -0
  80. package/src/bin/smoo-nx-bound-failures.test.ts +1 -1
  81. package/src/bin/smoo-nx-test-clocks.ts +29 -0
  82. package/src/bounded-test-policy.test.ts +36 -22
  83. package/src/bounded-test-policy.ts +18 -32
  84. package/src/bun-bootstrap.test.ts +1 -1
  85. package/src/cargo-closure-input.property.test.ts +50 -0
  86. package/src/cargo-closure-input.test.ts +73 -121
  87. package/src/cargo-closure-input.ts +47 -8
  88. package/src/cargo-closure-key.test.ts +239 -0
  89. package/src/cargo-resolve-cache.test.ts +168 -31
  90. package/src/cargo-source-hash.test.ts +78 -3
  91. package/src/cargo-source-hash.ts +48 -30
  92. package/src/cross-check-policy.test.ts +2 -2
  93. package/src/ensure-built-receipt.test.ts +111 -16
  94. package/src/ensure-built-receipt.ts +294 -149
  95. package/src/ensure-built-witness.test.ts +306 -13
  96. package/src/ensure-built-witness.ts +255 -24
  97. package/src/ensure-built.test.ts +228 -102
  98. package/src/ensure-built.ts +127 -66
  99. package/src/executors/bounded-exec/cpu-pool.test.ts +134 -32
  100. package/src/executors/bounded-exec/cpu-pool.ts +64 -20
  101. package/src/executors/bounded-exec/cpu-tokens.test.ts +2 -2
  102. package/src/executors/bounded-exec/disk-lease.test.ts +47 -13
  103. package/src/executors/bounded-exec/executor-host.test.ts +74 -52
  104. package/src/executors/bounded-exec/executor.test.ts +343 -97
  105. package/src/executors/bounded-exec/executor.ts +192 -56
  106. package/src/executors/bounded-exec/hang-diagnostics.test.ts +14 -8
  107. package/src/executors/bounded-exec/no-space.ts +40 -0
  108. package/src/executors/bounded-exec/progress.test.ts +1 -1
  109. package/src/executors/bounded-exec/ram-temp-host.test.ts +51 -21
  110. package/src/executors/bounded-exec/ram-temp.test.ts +92 -16
  111. package/src/executors/bounded-exec/ram-temp.ts +49 -20
  112. package/src/executors/bounded-exec/verdict.test.ts +127 -22
  113. package/src/executors/bounded-exec/verdict.ts +78 -3
  114. package/src/generators/bounded-test-targets/generator.test.ts +1 -3
  115. package/src/generators/bun-test-tracing/generator.test.ts +2 -2
  116. package/src/generators/bun-test-tracing/generator.ts +2 -2
  117. package/src/graph-input-contract.test.ts +379 -0
  118. package/src/graph-input-contract.ts +627 -0
  119. package/src/index.test.ts +3 -3
  120. package/src/index.ts +24 -3
  121. package/src/linked-snapshots.nx.test.ts +112 -30
  122. package/src/managed-files/ci-workflow.ts +5 -2
  123. package/src/managed-files/publish-workflow.ts +12 -1
  124. package/src/managed-files/repo-path.test.ts +44 -1
  125. package/src/managed-files/setup-devenv-free-disk.test.ts +95 -0
  126. package/src/nextest-code-bound.test.ts +23 -17
  127. package/src/nextest-extraction.test.ts +114 -4
  128. package/src/nextest-partitions.test.ts +7 -1
  129. package/src/nx-cache-restore-mtime.test.ts +2 -2
  130. package/src/nx-daemon-claim.test.ts +10 -8
  131. package/src/nx-daemon-idle-cpu.test.ts +0 -1
  132. package/src/nx-daemon-idle-policy.test.ts +61 -1
  133. package/src/nx-daemon-ignore-files.test.ts +2 -2
  134. package/src/nx-daemon-validity.test.ts +36 -22
  135. package/src/nx-dot-input-policy.test.ts +5 -5
  136. package/src/nx-files-archive-racy.test.ts +1 -1
  137. package/src/nx-incomplete-run-summary.test.ts +1 -1
  138. package/src/nx-plugin-load-errors.test.ts +2 -2
  139. package/src/nx-project-graph-errors.test.ts +70 -0
  140. package/src/nx-skipped-task-exit.test.ts +2 -2
  141. package/src/nx-store-resolution.test.ts +7 -25
  142. package/src/nx-task-output-lease.test.ts +3 -3
  143. package/src/nx-use-local.test.ts +7 -5
  144. package/src/owned-installation.ts +174 -0
  145. package/src/producer-receipts.test.ts +164 -0
  146. package/src/producer-receipts.ts +636 -0
  147. package/src/runtime-input.test.ts +1 -1
  148. package/src/serial-group-wiring.test.ts +4 -4
  149. package/src/task-results.test.ts +18 -16
  150. package/src/test-backstop.ts +11 -0
  151. package/src/test-clock-policy.test.ts +338 -0
  152. package/src/test-clock-policy.ts +708 -0
  153. package/src/test-timings.test.ts +2 -2
  154. package/src/testing.test.ts +5 -5
  155. package/src/testing.ts +67 -1
  156. package/src/typecheck-test-policy.test.ts +1 -1
  157. package/src/typescript-resolution.test.ts +76 -0
  158. package/test-timings.json +8 -0
@@ -1,28 +1,53 @@
1
1
  import { spawnSync } from 'node:child_process';
2
2
  import { createHash } from 'node:crypto';
3
- import type { BigIntStats } from 'node:fs';
4
- import { closeSync, lstatSync, openSync, readdirSync, readlinkSync, readSync, realpathSync } from 'node:fs';
3
+ import {
4
+ closeSync,
5
+ lstatSync,
6
+ openSync,
7
+ readdirSync,
8
+ readFileSync,
9
+ readlinkSync,
10
+ readSync,
11
+ realpathSync,
12
+ } from 'node:fs';
5
13
  import { isAbsolute, join, relative } from 'node:path';
6
14
  import type { Task } from 'nx/src/config/task-graph';
7
15
  import type { expandOutputs, HashInputs } from 'nx/src/native';
8
16
  import { CARGO_TOOLCHAIN_PIN_INPUTS, cargoRuntimeInput } from './cargo-toolchain-policy.js';
9
- import type { EnsureBuiltOutputReceipt, EnsureBuiltProbeFailure, EnsureBuiltReceipt } from './ensure-built.js';
10
- import { type NodeWitness, nodeStamp, nodesUnchanged } from './ensure-built-witness.js';
11
-
12
- type Evidence<T> = { readonly ok: true; readonly value: T } | { readonly ok: false; readonly detail: string };
17
+ import type {
18
+ EnsureBuiltOutputReceipt,
19
+ EnsureBuiltProbeFailure,
20
+ EnsureBuiltProducerFileInput,
21
+ EnsureBuiltProducerReceipt,
22
+ EnsureBuiltReceipt,
23
+ } from './ensure-built.js';
24
+ import { censusStart, movedNodes, type NodeWitness, recordedStamp } from './ensure-built-witness.js';
13
25
 
26
+ export type ReceiptEvidence<T> =
27
+ | { readonly ok: true; readonly value: T }
28
+ | { readonly ok: false; readonly detail: string };
14
29
  export interface ProviderInputProof {
15
30
  readonly kind: 'exact-file-inputs' | 'conservative-provider-source';
16
31
  readonly files: readonly string[];
17
32
  readonly inputs: Readonly<Record<string, HashInputs>>;
18
33
  readonly runtimeSource: { readonly kind: 'bounded' } | { readonly kind: 'opaque'; readonly detail: string };
19
34
  }
20
-
21
- function git(root: string, args: string[]): Evidence<string> {
22
- // Provenance belongs to this provider, not a Git directory/index/object
23
- // namespace an outer build lent the process. Nx's hashing environment is
24
- // unchanged; only these read-only evidence children bind their own Git state.
25
- const env = { ...process.env };
35
+ export interface ProducerSourceWitness {
36
+ readonly head: string;
37
+ readonly headTree: string;
38
+ readonly files: readonly EnsureBuiltProducerFileInput[];
39
+ readonly nodes: readonly NodeWitness[];
40
+ readonly outputs: readonly string[];
41
+ }
42
+ /**
43
+ * Git as receipts read it: the checkout and nothing of the operator's home. The producer hooks run in whichever process
44
+ * Nx runs plugins in, a workspace's sandboxed daemon included, which adopts a host client's HOME; git with that HOME
45
+ * reads the operator's global configuration, the sandbox denies it, and git dies (specs/cowshed/04_sandbox.md, the
46
+ * runtime-input contract). A receipt's tree, HEAD and tracked-file status are read under the repository's own
47
+ * configuration alone, so a host run and a sandboxed run agree on them.
48
+ */
49
+ export function receiptGit(root: string, args: string[]): ReceiptEvidence<string> {
50
+ const env: NodeJS.ProcessEnv = { ...process.env, GIT_CONFIG_GLOBAL: '/dev/null', GIT_CONFIG_NOSYSTEM: '1' };
26
51
  for (const key of [
27
52
  'GIT_DIR',
28
53
  'GIT_COMMON_DIR',
@@ -41,8 +66,7 @@ function git(root: string, args: string[]): Evidence<string> {
41
66
  detail: `git ${args.join(' ')}: ${child.error?.message ?? child.stderr.trim()} (exit ${child.status}, signal ${child.signal})`,
42
67
  };
43
68
  }
44
-
45
- /** NUL-delimited Git paths preserve whitespace, rename sources and index-only changes. */
69
+ /** NUL-delimited paths preserve whitespace, rename sources and index-only changes. */
46
70
  export function dirtyProviderPaths(status: string): string[] {
47
71
  const entries = status.split('\0');
48
72
  const paths: string[] = [];
@@ -57,15 +81,214 @@ export function dirtyProviderPaths(status: string): string[] {
57
81
  }
58
82
  return paths;
59
83
  }
60
-
84
+ function graphConfiguration(path: string): boolean {
85
+ const name = path.slice(path.lastIndexOf('/') + 1);
86
+ return [
87
+ 'nx.json',
88
+ 'project.json',
89
+ 'package.json',
90
+ 'bun.lock',
91
+ 'bun.lockb',
92
+ 'package-lock.json',
93
+ 'pnpm-lock.yaml',
94
+ 'yarn.lock',
95
+ ].includes(name);
96
+ }
97
+ let readBuffer: Buffer | undefined;
98
+ function fileReadBuffer(): Buffer {
99
+ if (readBuffer === undefined) readBuffer = Buffer.allocUnsafe(1 << 20);
100
+ return readBuffer;
101
+ }
102
+ function hashFile(path: string, size: number, algorithms: readonly ('sha1' | 'sha256')[], blob: boolean): string[] {
103
+ const hashes = algorithms.map((algorithm) => createHash(algorithm));
104
+ if (blob) for (const hash of hashes) hash.update(`blob ${size}\0`);
105
+ const buffer = fileReadBuffer();
106
+ const descriptor = openSync(path, 'r');
107
+ try {
108
+ for (let offset = 0; offset < size; offset += buffer.length) {
109
+ const length = Math.min(buffer.length, size - offset);
110
+ if (readSync(descriptor, buffer, 0, length, offset) !== length)
111
+ throw new Error(`File moved while reading ${path}`);
112
+ for (const hash of hashes) hash.update(buffer.subarray(0, length));
113
+ }
114
+ } finally {
115
+ closeSync(descriptor);
116
+ }
117
+ return hashes.map((hash) => hash.digest('hex'));
118
+ }
119
+ function sourceFileDigests(
120
+ path: string,
121
+ size: number,
122
+ algorithm: 'sha1' | 'sha256',
123
+ ): { readonly gitBlob: string; readonly sha256: string } {
124
+ const blob = createHash(algorithm).update(`blob ${size}\0`);
125
+ const raw = createHash('sha256');
126
+ const buffer = fileReadBuffer();
127
+ const descriptor = openSync(path, 'r');
128
+ try {
129
+ for (let offset = 0; offset < size; offset += buffer.length) {
130
+ const length = Math.min(buffer.length, size - offset);
131
+ if (readSync(descriptor, buffer, 0, length, offset) !== length)
132
+ throw new Error(`Source moved while reading ${path}`);
133
+ const bytes = buffer.subarray(0, length);
134
+ blob.update(bytes);
135
+ raw.update(bytes);
136
+ }
137
+ } finally {
138
+ closeSync(descriptor);
139
+ }
140
+ return { gitBlob: blob.digest('hex'), sha256: raw.digest('hex') };
141
+ }
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 {
151
+ const stats = lstatSync(path, { bigint: true });
152
+ census?.observed.push({ path, stamp: recordedStamp(stats, census.start) });
153
+ const mode = Number(stats.mode & 0o7777n);
154
+ if (stats.isFile())
155
+ return { path: name, type: 'file', mode, sha256: hashFile(path, Number(stats.size), ['sha256'], false)[0] };
156
+ if (stats.isSymbolicLink()) return { path: name, type: 'symlink', mode, linkText: readlinkSync(path) };
157
+ if (stats.isDirectory()) {
158
+ const entries = readdirSync(path).sort();
159
+ return {
160
+ path: name,
161
+ type: 'directory',
162
+ mode,
163
+ entries,
164
+ sha256: createHash('sha256').update(JSON.stringify(entries)).digest('hex'),
165
+ };
166
+ }
167
+ throw new Error(`Unsupported receipt node ${path}`);
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
+ }
181
+ /** Capture every tracked source byte at pre. Unrelated untracked files are not source basis. */
182
+ export function captureProducerSource(
183
+ root: string,
184
+ outputs: readonly string[],
185
+ ): ReceiptEvidence<ProducerSourceWitness> {
186
+ try {
187
+ const revision = receiptGit(root, ['rev-parse', '--verify', 'HEAD']);
188
+ if (!revision.ok) return revision;
189
+ const head = revision.value.trim();
190
+ const tree = receiptGit(root, ['rev-parse', '--verify', `${head}^{tree}`]);
191
+ if (!tree.ok) return tree;
192
+ const listed = receiptGit(root, ['ls-tree', '-r', '-z', '--full-tree', head]);
193
+ if (!listed.ok) return listed;
194
+ const format = receiptGit(root, ['rev-parse', '--show-object-format']);
195
+ if (!format.ok) return format;
196
+ const algorithm = format.value.trim();
197
+ if (algorithm !== 'sha1' && algorithm !== 'sha256')
198
+ return { ok: false, detail: `Unproved Git object format ${algorithm}` };
199
+ const excluded = new Set(outputs.filter((path) => !graphConfiguration(path)));
200
+ const nodes: NodeWitness[] = [];
201
+ const files: EnsureBuiltProducerFileInput[] = [];
202
+ const start = censusStart();
203
+ for (const entry of listed.value.split('\0')) {
204
+ if (entry === '') continue;
205
+ const tab = entry.indexOf('\t');
206
+ const [mode, kind, oid] = entry.slice(0, tab).split(' ');
207
+ if (tab < 0 || oid === undefined || mode === undefined)
208
+ return { ok: false, detail: `Unproved HEAD tree entry ${entry}` };
209
+ if (kind !== 'blob') return { ok: false, detail: `Unbound tracked source namespace ${entry}` };
210
+ const file = entry.slice(tab + 1);
211
+ if (excluded.has(file)) continue;
212
+ const path = join(root, file);
213
+ const stats = lstatSync(path, { bigint: true });
214
+ nodes.push({ path, stamp: recordedStamp(stats, start) });
215
+ const actualMode = stats.isSymbolicLink() ? '120000' : (stats.mode & 0o111n) !== 0n ? '100755' : '100644';
216
+ if (actualMode !== mode) return { ok: false, detail: `Tracked source mode differs from HEAD: ${file}` };
217
+ let gitBlob: string;
218
+ let sha256: string;
219
+ if (stats.isFile()) {
220
+ const digests = sourceFileDigests(path, Number(stats.size), algorithm);
221
+ gitBlob = digests.gitBlob;
222
+ sha256 = digests.sha256;
223
+ } else if (stats.isSymbolicLink()) {
224
+ const text = readlinkSync(path);
225
+ gitBlob = createHash(algorithm)
226
+ .update(`blob ${Buffer.byteLength(text)}\0`)
227
+ .update(text)
228
+ .digest('hex');
229
+ sha256 = createHash('sha256').update(text).digest('hex');
230
+ } else return { ok: false, detail: `Unbound tracked source node: ${file}` };
231
+ if (gitBlob !== oid) return { ok: false, detail: `Tracked source bytes differ from HEAD: ${file}` };
232
+ files.push({ path: file, mode: Number(stats.mode & 0o7777n), gitBlob, sha256 });
233
+ }
234
+ const status = receiptGit(root, ['status', '--porcelain=v1', '-z', '--untracked-files=no']);
235
+ if (!status.ok) return status;
236
+ const dirty = dirtyProviderPaths(status.value).filter((path) => !excluded.has(path));
237
+ if (dirty.length !== 0)
238
+ return { ok: false, detail: `Tracked source/index differs from ${head}: ${dirty.join(', ')}` };
239
+ const moved = movedNodes(nodes, recordedSource(root, files));
240
+ if (moved !== null) return { ok: false, detail: `Tracked source moved during producer pre capture: ${moved}` };
241
+ return { ok: true, value: { head, headTree: tree.value.trim(), files, nodes, outputs } };
242
+ } catch (error) {
243
+ if (error instanceof Error) return { ok: false, detail: `Cannot prove tracked producer tree: ${error.message}` };
244
+ throw error;
245
+ }
246
+ }
247
+ export function producerSourceUnchanged(root: string, before: ProducerSourceWitness): ReceiptEvidence<true> {
248
+ const revision = receiptGit(root, ['rev-parse', '--verify', 'HEAD']);
249
+ if (!revision.ok) return revision;
250
+ const status = receiptGit(root, ['status', '--porcelain=v1', '-z', '--untracked-files=no']);
251
+ if (!status.ok) return status;
252
+ const outputs = new Set(before.outputs.filter((path) => !graphConfiguration(path)));
253
+ const tree = receiptGit(root, ['rev-parse', '--verify', `${revision.value.trim()}^{tree}`]);
254
+ if (!tree.ok) return tree;
255
+ const changed = 'Producer HEAD/tracked source nodes changed across actual task execution';
256
+ if (tree.value.trim() !== before.headTree)
257
+ return { ok: false, detail: `${changed}: HEAD's tree is ${tree.value.trim()}, not ${before.headTree}` };
258
+ const dirty = dirtyProviderPaths(status.value).filter((path) => !outputs.has(path));
259
+ if (dirty.length !== 0) return { ok: false, detail: `${changed}: ${dirty.join(', ')} differ from HEAD` };
260
+ const moved = movedNodes(before.nodes, recordedSource(root, before.files));
261
+ if (moved !== null) return { ok: false, detail: `${changed}: ${moved}` };
262
+ return { ok: true, value: true };
263
+ }
264
+ /** Enumerate exact public nodes; never exempt a broad directory of tracked source. */
265
+ export function producerOutputPaths(root: string, tasks: readonly Task[], expand: typeof expandOutputs): string[] {
266
+ const paths = new Set<string>();
267
+ function visit(path: string): void {
268
+ if (isAbsolute(path) || path.split('/').includes('..')) throw new Error(`Output escapes provider: ${path}`);
269
+ if (paths.has(path)) return;
270
+ paths.add(path);
271
+ let stats: ReturnType<typeof lstatSync>;
272
+ try {
273
+ stats = lstatSync(join(root, path));
274
+ } catch (error) {
275
+ if (error instanceof Error && 'code' in error && error.code === 'ENOENT') return;
276
+ throw error;
277
+ }
278
+ if (stats.isDirectory()) for (const name of readdirSync(join(root, path))) visit(join(path, name));
279
+ }
280
+ for (const task of tasks) for (const path of expand(root, task.outputs)) visit(path);
281
+ return [...paths].sort();
282
+ }
61
283
  export function providerReceipt(
62
- workspaceRoot: string,
284
+ root: string,
63
285
  taskId: string,
64
286
  tasks: readonly Task[],
65
287
  proof: ProviderInputProof,
66
288
  expand: typeof expandOutputs,
289
+ source: ProducerSourceWitness,
290
+ producer: Omit<EnsureBuiltProducerReceipt, 'outputSha256'>,
67
291
  ): { readonly disposition: 'hit'; readonly receipt: EnsureBuiltReceipt } | EnsureBuiltProbeFailure {
68
- const root = realpathSync(workspaceRoot);
69
292
  const failed = (detail: string): EnsureBuiltProbeFailure => ({
70
293
  disposition: 'failed',
71
294
  reason: { kind: 'provider-proof', taskId, workspaceRoot: root, detail },
@@ -73,167 +296,89 @@ export function providerReceipt(
73
296
  signal: null,
74
297
  });
75
298
  if (proof.runtimeSource.kind === 'opaque') return failed(proof.runtimeSource.detail);
76
- const revision = git(root, ['rev-parse', '--verify', 'HEAD']);
77
- if (!revision.ok) return failed(revision.detail);
78
- const head = revision.value.trim();
79
- const tree = git(root, ['rev-parse', '--verify', `${head}^{tree}`]);
80
- if (!tree.ok) return failed(tree.detail);
81
- const status = git(root, ['status', '--porcelain=v1', '-z', '--untracked-files=all']);
82
- if (!status.ok) return failed(status.detail);
83
- const listed = git(root, ['ls-tree', '-r', '-z', '--full-tree', head]);
84
- if (!listed.ok) return failed(listed.detail);
85
- const format = git(root, ['rev-parse', '--show-object-format']);
86
- if (!format.ok) return failed(format.detail);
87
- const algorithm = format.value.trim();
88
- if (algorithm !== 'sha1' && algorithm !== 'sha256') return failed(`Unproved Git object format ${algorithm}`);
89
- const tracked = new Map<string, { mode: string; oid: string }>();
90
- for (const entry of listed.value.split('\0')) {
91
- if (entry === '') continue;
92
- const tab = entry.indexOf('\t');
93
- const [mode, kind, oid] = entry.slice(0, tab).split(' ');
94
- if (tab < 0 || oid === undefined) return failed(`Unproved HEAD tree entry ${entry}`);
95
- if (kind !== 'blob') continue;
96
- tracked.set(entry.slice(tab + 1), { mode, oid });
299
+ const stable = producerSourceUnchanged(root, source);
300
+ if (!stable.ok) return failed(stable.detail);
301
+ const sourceFiles = new Map(source.files.map((file) => [file.path, file]));
302
+ for (const file of proof.files) {
303
+ const record = sourceFiles.get(file);
304
+ if (record === undefined) return failed(`Actual Nx FILE input is not bound to provider HEAD: ${file}`);
305
+ if (!lstatSync(join(root, file)).isFile()) return failed(`Unbound linked/non-file Nx source: ${file}`);
97
306
  }
98
307
  const observed: NodeWitness[] = [];
99
- const buffer = Buffer.allocUnsafe(1 << 20);
100
- const outputNodes = new Map<string, EnsureBuiltOutputReceipt>();
101
- function observe(path: string): BigIntStats {
102
- const stats = lstatSync(path, { bigint: true });
103
- observed.push({ path, stamp: nodeStamp(stats) });
104
- return stats;
105
- }
106
- function fileDigest(path: string, size: number, algorithm: 'sha1' | 'sha256', gitBlob: boolean): string {
107
- const hash = createHash(algorithm);
108
- if (gitBlob) hash.update(`blob ${size}\0`);
109
- const fd = openSync(path, 'r');
110
- try {
111
- for (let offset = 0; offset < size; offset += buffer.length) {
112
- const count = Math.min(buffer.length, size - offset);
113
- if (readSync(fd, buffer, 0, count, offset) !== count) throw new Error(`File moved while reading ${path}`);
114
- hash.update(buffer.subarray(0, count));
115
- }
116
- } finally {
117
- closeSync(fd);
118
- }
119
- return hash.digest('hex');
120
- }
121
- function visit(path: string): EnsureBuiltOutputReceipt {
122
- const rel = relative(root, path);
123
- if (rel === '..' || rel.startsWith('../') || isAbsolute(rel)) throw new Error(`Output escapes provider: ${path}`);
124
- const previous = outputNodes.get(rel);
125
- if (previous !== undefined) return previous;
126
- const stats = observe(path);
127
- const mode = Number(stats.mode & 0o7777n);
128
- let node: EnsureBuiltOutputReceipt;
129
- if (stats.isFile()) {
130
- node = { path: rel, type: 'file', mode, sha256: fileDigest(path, Number(stats.size), 'sha256', false) };
131
- } else if (stats.isSymbolicLink()) {
132
- node = { path: rel, type: 'symlink', mode, linkText: readlinkSync(path) };
133
- } else if (stats.isDirectory()) {
134
- const children = readdirSync(path)
135
- .sort()
136
- .map((name) => visit(join(path, name)));
137
- const entries = children.map(({ path: child }) => relative(rel, child));
138
- node = {
139
- path: rel,
140
- type: 'directory',
141
- mode,
142
- entries,
143
- sha256: createHash('sha256').update(JSON.stringify(children)).digest('hex'),
144
- };
145
- } else {
146
- throw new Error(`Unsupported public output node ${path}`);
308
+ const census = { start: censusStart(), observed };
309
+ const parents = new Set<string>();
310
+ const outputs = new Map<string, EnsureBuiltOutputReceipt>();
311
+ function visit(path: string): void {
312
+ if (outputs.has(path)) return;
313
+ const absolute = join(root, path);
314
+ let node = receiptNode(absolute, path, census);
315
+ if (node.type === 'directory') {
316
+ for (const entry of node.entries) visit(join(path, entry));
317
+ const children = node.entries.map((entry) => outputs.get(join(path, entry)));
318
+ node = { ...node, sha256: createHash('sha256').update(JSON.stringify(children)).digest('hex') };
147
319
  }
148
- outputNodes.set(rel, node);
149
- return node;
320
+ outputs.set(path, node);
150
321
  }
151
322
  try {
152
323
  const receipts: EnsureBuiltReceipt['tasks'][number][] = [];
153
324
  for (const task of tasks) {
154
- if (task.hash === undefined) return failed(`No current hash for ${task.id}`);
325
+ if (task.hash === undefined) return failed(`Actual completion supplied no hash for ${task.id}`);
155
326
  const entries = task.outputs.length === 0 ? [] : expand(root, task.outputs);
156
- if (task.outputs.length > 0 && entries.length === 0) return failed(`No public outputs for ${task.id}`);
157
- const selected = new Set<string>();
327
+ if (task.outputs.length !== 0 && entries.length === 0) return failed(`No public outputs for ${task.id}`);
158
328
  for (const entry of entries) {
159
- if (isAbsolute(entry) || entry.split('/').includes('..'))
160
- return failed(`Invalid public output ${entry} for ${task.id}`);
161
- // Root-to-entry parent directories must be real; do not follow a
162
- // parent link into an unrelated tree and sign its bytes.
329
+ if (isAbsolute(entry) || entry.split('/').includes('..')) return failed(`Output escapes provider: ${entry}`);
163
330
  let parent = root;
164
331
  for (const segment of entry.split('/').slice(0, -1)) {
165
332
  parent = join(parent, segment);
166
- if (!observe(parent).isDirectory()) return failed(`Symlinked public output parent ${parent}`);
167
- }
168
- visit(join(root, entry));
169
- for (const path of outputNodes.keys()) {
170
- if (path === entry || path.startsWith(`${entry}/`)) selected.add(path);
333
+ const stats = lstatSync(parent, { bigint: true });
334
+ observed.push({ path: parent, stamp: recordedStamp(stats, census.start) });
335
+ parents.add(parent);
336
+ if (!stats.isDirectory()) return failed(`Symlinked public output parent ${parent}`);
171
337
  }
338
+ visit(entry);
172
339
  }
173
340
  receipts.push({
174
341
  id: task.id,
175
342
  hash: task.hash,
176
- outputs: [...selected].sort().map((path) => {
177
- const node = outputNodes.get(path);
178
- if (node === undefined) throw new Error(`Lost output census node ${path}`);
179
- return node;
180
- }),
343
+ outputs: [...outputs]
344
+ .filter(([path]) => entries.some((entry) => path === entry || path.startsWith(`${entry}/`)))
345
+ .sort(([left], [right]) => left.localeCompare(right))
346
+ .map(([, node]) => node),
181
347
  });
182
348
  }
183
- const fileInputs = new Set(proof.files);
184
- const deletedSource = status.value
185
- .split('\0')
186
- .some((entry) => (entry[0] === 'D' || entry[1] === 'D') && !outputNodes.has(entry.slice(3)));
187
- const effectiveKind = deletedSource ? 'conservative-provider-source' : proof.kind;
188
- const dirty = dirtyProviderPaths(status.value).filter(
189
- (path) =>
190
- (!outputNodes.has(path) || fileInputs.has(path)) &&
191
- (effectiveKind === 'conservative-provider-source' || fileInputs.has(path)),
192
- );
193
- if (dirty.length > 0) return failed(`${effectiveKind}: provider source differs from ${head}: ${dirty.join(', ')}`);
194
- for (const file of fileInputs) {
195
- const expected = tracked.get(file);
196
- if (expected === undefined) return failed(`Nx file input is not in provider HEAD tree: ${file}`);
197
- const path = join(root, file);
198
- const stats = observe(path);
199
- const mode = stats.isSymbolicLink() ? '120000' : (stats.mode & 0o111n) !== 0n ? '100755' : '100644';
200
- if (mode !== expected.mode) return failed(`Nx file input mode differs from provider HEAD: ${file}`);
201
- if (!stats.isFile()) return failed(`Unproved linked or non-file input source closure: ${file}`);
202
- const oid = fileDigest(path, Number(stats.size), algorithm, true);
203
- if (oid !== expected.oid) return failed(`Nx file input bytes differ from provider HEAD: ${file}`);
204
- }
205
- const finalHead = git(root, ['rev-parse', '--verify', 'HEAD']);
206
- const finalStatus = git(root, ['status', '--porcelain=v1', '-z', '--untracked-files=all']);
207
- if (!finalHead.ok) return failed(finalHead.detail);
208
- if (!finalStatus.ok) return failed(finalStatus.detail);
209
- const newlyDirty = dirtyProviderPaths(finalStatus.value).some(
210
- (path) =>
211
- (!outputNodes.has(path) || fileInputs.has(path)) &&
212
- (effectiveKind === 'conservative-provider-source' || fileInputs.has(path)),
213
- );
214
- if (finalHead.value.trim() !== head || newlyDirty || !nodesUnchanged(observed)) {
215
- return failed('Provider source, revision or public output nodes moved during receipt capture');
216
- }
349
+ const final = producerSourceUnchanged(root, source);
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}`);
363
+ const outputSha256 = createHash('sha256').update(JSON.stringify(receipts)).digest('hex');
217
364
  return {
218
365
  disposition: 'hit',
219
366
  receipt: {
220
- workspaceRoot: root,
221
- head,
222
- headTree: tree.value.trim(),
223
- tasks: receipts,
224
- fileInputProvenance: effectiveKind,
367
+ workspaceRoot: realpathSync(root),
368
+ head: source.head,
369
+ headTree: source.headTree,
370
+ fileInputProvenance: proof.kind,
225
371
  runtimeEnvironmentProvenance: 'nx-task-hash-only',
372
+ producer: { ...producer, outputSha256 },
373
+ tasks: receipts,
226
374
  },
227
375
  };
228
376
  } catch (error) {
229
- if (error instanceof Error) return failed(`Cannot prove provider source/public outputs: ${error.message}`);
377
+ if (error instanceof Error) return failed(`Cannot prove public product census: ${error.message}`);
230
378
  throw error;
231
379
  }
232
380
  }
233
-
234
- /** Admit only recipes emitted by the same owning toolchain generator.
235
- * No shell parsing or keyword approximation confers file-source provenance.
236
- */
381
+ /** Admit only version recipes emitted by the owning generator and declared toolchain pins. */
237
382
  export function boundedReceiptRuntimes(
238
383
  inputs: Readonly<Record<string, HashInputs>>,
239
384
  directories: readonly string[],