@smoothbricks/nx-plugin 0.4.27 → 0.4.29

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