@smoothbricks/nx-plugin 0.4.26 → 0.4.28

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 (343) hide show
  1. package/README.md +994 -117
  2. package/dist/bin/smoo-nx-bound-failures.d.ts +3 -0
  3. package/dist/bin/smoo-nx-bound-failures.d.ts.map +1 -0
  4. package/dist/bin/smoo-nx-bound-failures.js +31 -0
  5. package/dist/bin/smoo-nx-cargo-hash.js +28 -3
  6. package/dist/bin/smoo-nx-exec.js +15 -73
  7. package/dist/bin/smoo-nx-failures.d.ts +3 -0
  8. package/dist/bin/smoo-nx-failures.d.ts.map +1 -0
  9. package/dist/bin/smoo-nx-failures.js +3 -0
  10. package/dist/bin/smoo-nx-nextest-extract.js +3 -2
  11. package/dist/bin/smoo-nx-test-walks.d.ts +3 -0
  12. package/dist/bin/smoo-nx-test-walks.d.ts.map +1 -0
  13. package/dist/bin/smoo-nx-test-walks.js +34 -0
  14. package/dist/bounded-test-policy.d.ts +7 -1
  15. package/dist/bounded-test-policy.d.ts.map +1 -1
  16. package/dist/bounded-test-policy.js +101 -40
  17. package/dist/build-state-output-policy.d.ts +25 -0
  18. package/dist/build-state-output-policy.d.ts.map +1 -0
  19. package/dist/build-state-output-policy.js +64 -0
  20. package/dist/cargo-closure-input.d.ts +3 -2
  21. package/dist/cargo-closure-input.d.ts.map +1 -1
  22. package/dist/cargo-closure-input.js +16 -15
  23. package/dist/cargo-dev-profile.d.ts +37 -0
  24. package/dist/cargo-dev-profile.d.ts.map +1 -0
  25. package/dist/cargo-dev-profile.js +108 -0
  26. package/dist/cargo-source-hash.d.ts +43 -6
  27. package/dist/cargo-source-hash.d.ts.map +1 -1
  28. package/dist/cargo-source-hash.js +309 -73
  29. package/dist/cargo-toolchain-policy.d.ts +4 -0
  30. package/dist/cargo-toolchain-policy.d.ts.map +1 -1
  31. package/dist/cargo-toolchain-policy.js +5 -0
  32. package/dist/cargo-workspace.d.ts +101 -5
  33. package/dist/cargo-workspace.d.ts.map +1 -1
  34. package/dist/cargo-workspace.js +291 -10
  35. package/dist/cross-check-policy.d.ts +21 -3
  36. package/dist/cross-check-policy.d.ts.map +1 -1
  37. package/dist/cross-check-policy.js +24 -6
  38. package/dist/disk-command-policy.d.ts +7 -0
  39. package/dist/disk-command-policy.d.ts.map +1 -0
  40. package/dist/disk-command-policy.js +98 -0
  41. package/dist/ensure-built-receipt.d.ts +25 -0
  42. package/dist/ensure-built-receipt.d.ts.map +1 -0
  43. package/dist/ensure-built-receipt.js +261 -0
  44. package/dist/ensure-built-witness.d.ts +57 -0
  45. package/dist/ensure-built-witness.d.ts.map +1 -0
  46. package/dist/ensure-built-witness.js +412 -0
  47. package/dist/ensure-built.d.ts +207 -12
  48. package/dist/ensure-built.d.ts.map +1 -1
  49. package/dist/ensure-built.js +576 -202
  50. package/dist/eslint-file-set.d.ts +7 -0
  51. package/dist/eslint-file-set.d.ts.map +1 -0
  52. package/dist/eslint-file-set.js +12 -0
  53. package/dist/executors/bounded-exec/cpu-pool.d.ts +63 -0
  54. package/dist/executors/bounded-exec/cpu-pool.d.ts.map +1 -0
  55. package/dist/executors/bounded-exec/cpu-pool.js +379 -0
  56. package/dist/executors/bounded-exec/cpu-tokens.d.ts +82 -0
  57. package/dist/executors/bounded-exec/cpu-tokens.d.ts.map +1 -0
  58. package/dist/executors/bounded-exec/cpu-tokens.js +114 -0
  59. package/dist/executors/bounded-exec/disk-lease.d.ts +61 -0
  60. package/dist/executors/bounded-exec/disk-lease.d.ts.map +1 -0
  61. package/dist/executors/bounded-exec/disk-lease.js +91 -0
  62. package/dist/executors/bounded-exec/executor.d.ts +45 -2
  63. package/dist/executors/bounded-exec/executor.d.ts.map +1 -1
  64. package/dist/executors/bounded-exec/executor.js +447 -52
  65. package/dist/executors/bounded-exec/gateway-lease.d.ts +52 -0
  66. package/dist/executors/bounded-exec/gateway-lease.d.ts.map +1 -0
  67. package/dist/executors/bounded-exec/gateway-lease.js +143 -0
  68. package/dist/executors/bounded-exec/hang-diagnostics.d.ts +35 -0
  69. package/dist/executors/bounded-exec/hang-diagnostics.d.ts.map +1 -0
  70. package/dist/executors/bounded-exec/hang-diagnostics.js +231 -0
  71. package/dist/executors/bounded-exec/progress.d.ts +35 -0
  72. package/dist/executors/bounded-exec/progress.d.ts.map +1 -0
  73. package/dist/executors/bounded-exec/progress.js +79 -0
  74. package/dist/executors/bounded-exec/ram-temp-fixtures.d.ts +3 -0
  75. package/dist/executors/bounded-exec/ram-temp-fixtures.d.ts.map +1 -0
  76. package/dist/executors/bounded-exec/ram-temp-fixtures.js +12 -0
  77. package/dist/executors/bounded-exec/ram-temp-held.d.ts +91 -0
  78. package/dist/executors/bounded-exec/ram-temp-held.d.ts.map +1 -0
  79. package/dist/executors/bounded-exec/ram-temp-held.js +152 -0
  80. package/dist/executors/bounded-exec/ram-temp.d.ts +223 -0
  81. package/dist/executors/bounded-exec/ram-temp.d.ts.map +1 -0
  82. package/dist/executors/bounded-exec/ram-temp.js +1528 -0
  83. package/dist/executors/bounded-exec/schema.d.ts +29 -11
  84. package/dist/executors/bounded-exec/schema.d.ts.map +1 -1
  85. package/dist/executors/bounded-exec/verdict.d.ts +162 -0
  86. package/dist/executors/bounded-exec/verdict.d.ts.map +1 -0
  87. package/dist/executors/bounded-exec/verdict.js +318 -0
  88. package/dist/executors/shard-timings/executor.d.ts +14 -0
  89. package/dist/executors/shard-timings/executor.d.ts.map +1 -0
  90. package/dist/executors/shard-timings/executor.js +25 -0
  91. package/dist/executors/shard-timings/schema.d.ts +4 -0
  92. package/dist/executors/shard-timings/schema.d.ts.map +1 -0
  93. package/dist/executors/shard-timings/schema.js +1 -0
  94. package/dist/failure-summary.d.ts +5 -0
  95. package/dist/failure-summary.d.ts.map +1 -0
  96. package/dist/failure-summary.js +118 -0
  97. package/dist/generators/create-package/generator.d.ts.map +1 -1
  98. package/dist/generators/create-package/generator.js +6 -2
  99. package/dist/index.d.ts +14 -2
  100. package/dist/index.d.ts.map +1 -1
  101. package/dist/index.js +370 -138
  102. package/dist/managed-files/ci-workflow.d.ts +13 -0
  103. package/dist/managed-files/ci-workflow.d.ts.map +1 -1
  104. package/dist/managed-files/ci-workflow.js +44 -13
  105. package/dist/managed-files/files.d.ts +7 -1
  106. package/dist/managed-files/files.d.ts.map +1 -1
  107. package/dist/managed-files/files.js +62 -8
  108. package/dist/managed-files/json-edit.d.ts +21 -0
  109. package/dist/managed-files/json-edit.d.ts.map +1 -0
  110. package/dist/managed-files/json-edit.js +295 -0
  111. package/dist/managed-files/managed-format.d.ts +33 -1
  112. package/dist/managed-files/managed-format.d.ts.map +1 -1
  113. package/dist/managed-files/managed-format.js +82 -2
  114. package/dist/managed-files/pr-preview-cleanup-workflow.d.ts +7 -0
  115. package/dist/managed-files/pr-preview-cleanup-workflow.d.ts.map +1 -1
  116. package/dist/managed-files/pr-preview-cleanup-workflow.js +68 -4
  117. package/dist/managed-files/publish-workflow.d.ts.map +1 -1
  118. package/dist/managed-files/publish-workflow.js +231 -48
  119. package/dist/managed-files/tree.d.ts +5 -0
  120. package/dist/managed-files/tree.d.ts.map +1 -1
  121. package/dist/managed-files/tree.js +5 -3
  122. package/dist/manifest-hash.d.ts.map +1 -1
  123. package/dist/manifest-hash.js +2 -5
  124. package/dist/nextest-code-bound.d.ts +62 -0
  125. package/dist/nextest-code-bound.d.ts.map +1 -0
  126. package/dist/nextest-code-bound.js +154 -0
  127. package/dist/nextest-extraction.d.ts +24 -8
  128. package/dist/nextest-extraction.d.ts.map +1 -1
  129. package/dist/nextest-extraction.js +42 -7
  130. package/dist/package-target-policy.d.ts.map +1 -1
  131. package/dist/package-target-policy.js +3 -5
  132. package/dist/release-config-policy.d.ts.map +1 -1
  133. package/dist/release-config-policy.js +3 -5
  134. package/dist/runtime-input.d.ts +14 -0
  135. package/dist/runtime-input.d.ts.map +1 -0
  136. package/dist/runtime-input.js +15 -0
  137. package/dist/rust-tokens.d.ts +11 -0
  138. package/dist/rust-tokens.d.ts.map +1 -0
  139. package/dist/rust-tokens.js +81 -0
  140. package/dist/task-results.d.ts +6 -0
  141. package/dist/task-results.d.ts.map +1 -0
  142. package/dist/task-results.js +61 -0
  143. package/dist/test-program.d.ts +54 -0
  144. package/dist/test-program.d.ts.map +1 -0
  145. package/dist/test-program.js +292 -0
  146. package/dist/test-timings.d.ts +71 -0
  147. package/dist/test-timings.d.ts.map +1 -0
  148. package/dist/test-timings.js +393 -0
  149. package/dist/test-walk-policy.d.ts +103 -0
  150. package/dist/test-walk-policy.d.ts.map +1 -0
  151. package/dist/test-walk-policy.js +657 -0
  152. package/dist/testing.d.ts +177 -0
  153. package/dist/testing.d.ts.map +1 -0
  154. package/dist/testing.js +824 -0
  155. package/dist/type-guards.d.ts +3 -0
  156. package/dist/type-guards.d.ts.map +1 -0
  157. package/dist/type-guards.js +4 -0
  158. package/dist/typecheck-test-policy.d.ts +8 -5
  159. package/dist/typecheck-test-policy.d.ts.map +1 -1
  160. package/dist/typecheck-test-policy.js +113 -100
  161. package/dist/workspace-config-policy.d.ts +8 -0
  162. package/dist/workspace-config-policy.d.ts.map +1 -1
  163. package/dist/workspace-config-policy.js +175 -63
  164. package/dist/workspace-index.d.ts +12 -0
  165. package/dist/workspace-index.d.ts.map +1 -0
  166. package/dist/workspace-index.js +17 -0
  167. package/dist/workspace-manifest.d.ts +37 -0
  168. package/dist/workspace-manifest.d.ts.map +1 -1
  169. package/executors.json +5 -0
  170. package/loader.js +13 -11
  171. package/managed/raw/envrc +33 -67
  172. package/managed/raw/git-format-staged.yml +30 -35
  173. package/managed/raw/patches/nx@23.2.1.patch +3484 -0
  174. package/managed/raw/tooling/direnv/apple-developer.sh +83 -0
  175. package/managed/raw/tooling/direnv/developer-links.ts +569 -11
  176. package/managed/raw/tooling/direnv/devenv.smoo.nix +314 -93
  177. package/managed/raw/tooling/direnv/github-actions-bootstrap.sh +20 -11
  178. package/managed/raw/tooling/direnv/linked-snapshots.ts +845 -0
  179. package/managed/raw/tooling/direnv/nx-socket-dir.sh +82 -0
  180. package/managed/raw/tooling/direnv/repo-path +14 -2
  181. package/managed/raw/tooling/direnv/setup-environment.ts +1146 -159
  182. package/managed/raw/tooling/direnv/toolchain-stamp.ts +45 -3
  183. package/managed/raw/tooling/direnv/ttsc-env.sh +39 -0
  184. package/managed/raw/tooling/git-hooks/commit-msg.sh +5 -6
  185. package/managed/raw/tooling/git-hooks/pre-commit.sh +7 -6
  186. package/managed/raw/tooling/git-hooks/pre-push.sh +20 -15
  187. package/managed/templates/github/actions/cache-nix-devenv/action.yml +6 -6
  188. package/managed/templates/github/actions/cache-node-modules/action.yml +6 -4
  189. package/managed/templates/github/actions/cache-nx/action.yml +1 -1
  190. package/managed/templates/github/actions/cache-ttsc-plugins/action.yml +1 -1
  191. package/managed/templates/github/actions/forgejo-artifact.cjs +99 -0
  192. package/managed/templates/github/actions/forgejo-download-artifact/action.yml +49 -0
  193. package/managed/templates/github/actions/forgejo-download-artifact/index.cjs +7 -0
  194. package/managed/templates/github/actions/forgejo-upload-artifact/action.yml +65 -0
  195. package/managed/templates/github/actions/forgejo-upload-artifact/index.cjs +7 -0
  196. package/managed/templates/github/actions/setup-devenv/action.yml +21 -3
  197. package/managed/templates/github/workflows/managed-files.yml +1 -2
  198. package/nextest.toml +8 -41
  199. package/package.json +74 -12
  200. package/runtime-input.sh +81 -0
  201. package/src/__tests__/counted-cargo.ts +33 -14
  202. package/src/__tests__/fixture-nx-env.ts +111 -46
  203. package/src/__tests__/progress-clock.ts +43 -0
  204. package/src/__tests__/runtime-input.ts +23 -0
  205. package/src/bin/smoo-nx-bound-failures.test.ts +127 -0
  206. package/src/bin/smoo-nx-bound-failures.ts +33 -0
  207. package/src/bin/smoo-nx-cargo-hash.ts +28 -3
  208. package/src/bin/smoo-nx-exec.ts +16 -74
  209. package/src/bin/smoo-nx-failures.ts +4 -0
  210. package/src/bin/smoo-nx-nextest-extract.ts +3 -2
  211. package/src/bin/smoo-nx-test-walks.ts +42 -0
  212. package/src/bounded-test-policy.test.ts +268 -21
  213. package/src/bounded-test-policy.ts +114 -42
  214. package/src/build-state-output-policy.test.ts +115 -0
  215. package/src/build-state-output-policy.ts +90 -0
  216. package/src/bun-bootstrap.test.ts +57 -3
  217. package/src/bun-file-packages.test.ts +47 -0
  218. package/src/cargo-closure-input.test.ts +138 -66
  219. package/src/cargo-closure-input.ts +17 -15
  220. package/src/cargo-dev-profile.ts +137 -0
  221. package/src/cargo-resolve-cache.test.ts +70 -1
  222. package/src/cargo-source-hash.test.ts +193 -97
  223. package/src/cargo-source-hash.ts +345 -68
  224. package/src/cargo-toolchain-policy.ts +6 -0
  225. package/src/cargo-workspace.test.ts +320 -0
  226. package/src/cargo-workspace.ts +336 -10
  227. package/src/cross-check-policy.test.ts +59 -1
  228. package/src/cross-check-policy.ts +25 -6
  229. package/src/disk-command-policy.test.ts +61 -0
  230. package/src/disk-command-policy.ts +107 -0
  231. package/src/ensure-built-receipt.test.ts +184 -0
  232. package/src/ensure-built-receipt.ts +263 -0
  233. package/src/ensure-built-witness.test.ts +344 -0
  234. package/src/ensure-built-witness.ts +252 -0
  235. package/src/ensure-built.test.ts +1036 -578
  236. package/src/ensure-built.ts +820 -224
  237. package/src/eslint-file-set.ts +9 -0
  238. package/src/executors/bounded-exec/cpu-pool.test.ts +454 -0
  239. package/src/executors/bounded-exec/cpu-pool.ts +417 -0
  240. package/src/executors/bounded-exec/cpu-tokens.test.ts +269 -0
  241. package/src/executors/bounded-exec/cpu-tokens.ts +177 -0
  242. package/src/executors/bounded-exec/disk-lease.test.ts +185 -0
  243. package/src/executors/bounded-exec/disk-lease.ts +139 -0
  244. package/src/executors/bounded-exec/executor-host.test.ts +778 -0
  245. package/src/executors/bounded-exec/executor.test.ts +1116 -63
  246. package/src/executors/bounded-exec/executor.ts +565 -67
  247. package/src/executors/bounded-exec/gateway-lease.ts +176 -0
  248. package/src/executors/bounded-exec/hang-diagnostics.test.ts +285 -0
  249. package/src/executors/bounded-exec/hang-diagnostics.ts +283 -0
  250. package/src/executors/bounded-exec/progress.test.ts +79 -0
  251. package/src/executors/bounded-exec/progress.ts +118 -0
  252. package/src/executors/bounded-exec/ram-temp-fixtures.ts +12 -0
  253. package/src/executors/bounded-exec/ram-temp-held.test.ts +264 -0
  254. package/src/executors/bounded-exec/ram-temp-held.ts +242 -0
  255. package/src/executors/bounded-exec/ram-temp-host.test.ts +605 -0
  256. package/src/executors/bounded-exec/ram-temp.test.ts +674 -0
  257. package/src/executors/bounded-exec/ram-temp.ts +1918 -0
  258. package/src/executors/bounded-exec/schema.json +11 -5
  259. package/src/executors/bounded-exec/schema.ts +27 -11
  260. package/src/executors/bounded-exec/verdict.test.ts +440 -0
  261. package/src/executors/bounded-exec/verdict.ts +433 -0
  262. package/src/executors/shard-timings/executor.ts +36 -0
  263. package/src/executors/shard-timings/schema.json +21 -0
  264. package/src/executors/shard-timings/schema.ts +4 -0
  265. package/src/failure-summary.ts +119 -0
  266. package/src/generators/bounded-test-targets/generator.test.ts +4 -4
  267. package/src/generators/bun-test-tracing/generator.test.ts +2 -2
  268. package/src/generators/create-package/generator.test.ts +6 -0
  269. package/src/generators/create-package/generator.ts +6 -2
  270. package/src/host-controller-wiring.test.ts +31 -0
  271. package/src/index.test.ts +909 -295
  272. package/src/index.ts +441 -143
  273. package/src/linked-snapshots.nx.test.ts +172 -0
  274. package/src/managed-files/ci-workflow.ts +54 -13
  275. package/src/managed-files/files.ts +65 -9
  276. package/src/managed-files/forgejo-artifact.test.ts +395 -0
  277. package/src/managed-files/generator.test.ts +60 -1
  278. package/src/managed-files/json-edit.test.ts +216 -0
  279. package/src/managed-files/json-edit.ts +290 -0
  280. package/src/managed-files/managed-format-json.test.ts +308 -0
  281. package/src/managed-files/managed-format.ts +90 -2
  282. package/src/managed-files/pr-preview-cleanup-workflow.ts +69 -3
  283. package/src/managed-files/publish-workflow.ts +285 -67
  284. package/src/managed-files/repo-path.test.ts +129 -0
  285. package/src/managed-files/trace-db-artifact.test.ts +7 -0
  286. package/src/managed-files/tree.ts +11 -3
  287. package/src/manifest-hash.ts +2 -6
  288. package/src/nextest-code-bound.test.ts +155 -0
  289. package/src/nextest-code-bound.ts +195 -0
  290. package/src/nextest-extraction.test.ts +37 -5
  291. package/src/nextest-extraction.ts +51 -9
  292. package/src/nextest-partitions.test.ts +427 -82
  293. package/src/nx-cache-bound.test.ts +5 -10
  294. package/src/nx-cache-restore-mtime.test.ts +170 -0
  295. package/src/nx-daemon-claim.test.ts +277 -0
  296. package/src/nx-daemon-idle-cpu.test.ts +81 -0
  297. package/src/nx-daemon-idle-policy.test.ts +51 -0
  298. package/src/nx-daemon-ignore-files.test.ts +210 -0
  299. package/src/nx-daemon-root-normalization.test.ts +105 -0
  300. package/src/nx-daemon-validity.test.ts +925 -0
  301. package/src/nx-dot-input-policy.test.ts +353 -0
  302. package/src/nx-failure-life-cycles.test.ts +207 -0
  303. package/src/nx-files-archive-racy.test.ts +97 -0
  304. package/src/nx-foreign-workspace-env.test.ts +329 -0
  305. package/src/nx-incomplete-run-summary.test.ts +70 -0
  306. package/src/nx-plugin-load-errors.test.ts +120 -0
  307. package/src/nx-project-resolution-cache.test.ts +176 -0
  308. package/src/nx-raw-inputs.test.ts +166 -0
  309. package/src/nx-resident-graph-workers.test.ts +7 -16
  310. package/src/nx-run-information.test.ts +118 -0
  311. package/src/nx-skipped-task-exit.test.ts +180 -0
  312. package/src/nx-state-location.test.ts +63 -0
  313. package/src/nx-store-resolution.test.ts +26 -25
  314. package/src/nx-task-graph-cycles.test.ts +148 -0
  315. package/src/nx-task-output-lease.test.ts +414 -0
  316. package/src/nx-use-local.test.ts +103 -0
  317. package/src/package-target-policy.ts +3 -6
  318. package/src/release-config-policy.ts +3 -6
  319. package/src/runtime-input.test.ts +215 -0
  320. package/src/runtime-input.ts +17 -0
  321. package/src/rust-tokens.ts +78 -0
  322. package/src/serial-group-wiring.test.ts +326 -0
  323. package/src/task-history-namespace.test.ts +87 -89
  324. package/src/task-results.test.ts +226 -0
  325. package/src/task-results.ts +75 -0
  326. package/src/test-program.test.ts +303 -0
  327. package/src/test-program.ts +341 -0
  328. package/src/test-timings.test.ts +240 -0
  329. package/src/test-timings.ts +311 -0
  330. package/src/test-walk-policy.test.ts +585 -0
  331. package/src/test-walk-policy.ts +731 -0
  332. package/src/testing.test.ts +509 -0
  333. package/src/testing.ts +922 -0
  334. package/src/type-guards.ts +4 -0
  335. package/src/typecheck-test-policy.test.ts +496 -11
  336. package/src/typecheck-test-policy.ts +137 -103
  337. package/src/workspace-config-policy.test.ts +212 -13
  338. package/src/workspace-config-policy.ts +101 -73
  339. package/src/workspace-index.ts +18 -0
  340. package/src/workspace-manifest.ts +41 -0
  341. package/test-timings.json +85 -30
  342. package/managed/raw/tooling/direnv/inherited-devenv.ts +0 -665
  343. package/managed/raw/tooling/direnv/shared-caches.sh +0 -53
@@ -0,0 +1,264 @@
1
+ import { describe, expect, it } from 'bun:test';
2
+ import { lstatSync, mkdirSync, mkdtempSync, realpathSync, rmSync, symlinkSync, writeFileSync } from 'node:fs';
3
+ import { tmpdir } from 'node:os';
4
+ import { join } from 'node:path';
5
+ import {
6
+ type CacheResult,
7
+ captureHeldVerdict,
8
+ type HeldCandidate,
9
+ type HeldContext,
10
+ type HeldVerdict,
11
+ type HolderStats,
12
+ heldProverOn,
13
+ parseHeldVerdict,
14
+ validateHeldVerdict,
15
+ } from './ram-temp-held.js';
16
+
17
+ const RAM_DEVICE = 77n;
18
+ const context: HeldContext = { volumeKey: 'root 77:2:100 marker 77:9:100:200', leaseKey: '77:40:1', ramDevice: '77' };
19
+
20
+ const workspace: Extract<HeldCandidate, { kind: 'mount' }> = {
21
+ kind: 'mount',
22
+ mountpoint: '/Volumes/ram/1-abcdef/workspace',
23
+ description: '/Volumes/ram/1-abcdef/workspace (mounted)',
24
+ };
25
+ const cache: Extract<HeldCandidate, { kind: 'mount' }> = {
26
+ kind: 'mount',
27
+ mountpoint: '/Volumes/ram/1-abcdef/cache',
28
+ description: '/Volumes/ram/1-abcdef/cache (mounted)',
29
+ };
30
+ const image: HeldCandidate = {
31
+ kind: 'image',
32
+ imagePath: '/Volumes/ram/1-abcdef/fixture.sparseimage',
33
+ diskPath: '/dev/disk42',
34
+ description: '/Volumes/ram/1-abcdef/fixture.sparseimage (/dev/disk42)',
35
+ };
36
+
37
+ type Entry = { shape: 'file' | 'directory'; dev: bigint; ino: bigint; birthtimeNs: bigint };
38
+ type Table = Map<string, Entry | Error>;
39
+
40
+ /**
41
+ * A host whose paths are a table: a missing row is ENOENT, an `Error` row is thrown as read.
42
+ * `reads` records every path read.
43
+ */
44
+ function tableHost(table: Table, reads: string[] = []) {
45
+ return heldProverOn(async (path): Promise<HolderStats> => {
46
+ reads.push(path);
47
+ const row = table.get(path);
48
+ if (row instanceof Error) {
49
+ throw row;
50
+ }
51
+ if (!row) {
52
+ throw Object.assign(new Error(`ENOENT: no such file or directory, lstat '${path}'`), { code: 'ENOENT' });
53
+ }
54
+ return { dev: row.dev, ino: row.ino, birthtimeNs: row.birthtimeNs, isDirectory: () => row.shape === 'directory' };
55
+ });
56
+ }
57
+
58
+ /** A lease held by two foreign volumes mounted below it. */
59
+ function heldTable(): Table {
60
+ return new Map<string, Entry | Error>([
61
+ [workspace.mountpoint, { shape: 'directory', dev: 88n, ino: 2n, birthtimeNs: 6_000n }],
62
+ [cache.mountpoint, { shape: 'directory', dev: 89n, ino: 2n, birthtimeNs: 6_100n }],
63
+ ]);
64
+ }
65
+
66
+ function changed(table: Table, path: string, change: Partial<Entry>): void {
67
+ const row = table.get(path);
68
+ if (!row || row instanceof Error) {
69
+ throw new Error(`no entry for ${path}`);
70
+ }
71
+ table.set(path, { ...row, ...change });
72
+ }
73
+
74
+ async function capturedOn(table: Table): Promise<HeldVerdict> {
75
+ const captured = await tableHost(table).capture(context, [workspace, cache]);
76
+ if (!captured.ok || !captured.value) {
77
+ throw new Error(`no verdict: ${JSON.stringify(captured)}`);
78
+ }
79
+ return captured.value;
80
+ }
81
+
82
+ describe('RAM lease held verdicts', () => {
83
+ it('keys every mount and stays valid while each is still mounted', async () => {
84
+ const table = heldTable();
85
+ const verdict = await capturedOn(table);
86
+ expect(verdict).toEqual({
87
+ version: 1,
88
+ context,
89
+ holders: [
90
+ { ...workspace, root: { dev: '88', ino: '2', birthtimeNs: '6000' } },
91
+ { ...cache, root: { dev: '89', ino: '2', birthtimeNs: '6100' } },
92
+ ],
93
+ });
94
+ expect(await tableHost(table).validate(verdict, { ...context })).toEqual({ ok: true, value: true });
95
+ });
96
+
97
+ it('caches no lease an image holds, without reading the image or its disk node', async () => {
98
+ const reads: string[] = [];
99
+ const host = tableHost(heldTable(), reads);
100
+ expect(await host.capture(context, [image])).toEqual({ ok: true, value: undefined });
101
+ expect(await host.capture(context, [workspace, image, cache])).toEqual({ ok: true, value: undefined });
102
+ expect(reads).not.toContain(image.imagePath);
103
+ expect(reads).not.toContain(image.diskPath);
104
+ });
105
+
106
+ it('caches nothing when there is no holder or a mount is gone, leaving the inventory result', async () => {
107
+ const table = heldTable();
108
+ const host = tableHost(table);
109
+ expect(await host.capture(context, [])).toEqual({ ok: true, value: undefined });
110
+ table.delete(cache.mountpoint);
111
+ expect(await host.capture(context, [workspace, cache])).toEqual({ ok: true, value: undefined });
112
+ // Unmounted before it was keyed: the RAM directory beneath proves nothing.
113
+ table.set(cache.mountpoint, { shape: 'directory', dev: RAM_DEVICE, ino: 41n, birthtimeNs: 1n });
114
+ expect(await host.capture(context, [workspace, cache])).toEqual({ ok: true, value: undefined });
115
+ });
116
+
117
+ it('asks for a fresh inventory once any mount is gone or changed', async () => {
118
+ const enotdir = Object.assign(new Error('ENOTDIR: not a directory'), { code: 'ENOTDIR' });
119
+ const changes: [string, (table: Table) => void][] = [
120
+ ['unmounted externally: RAM shows beneath', (table) => changed(table, cache.mountpoint, { dev: RAM_DEVICE })],
121
+ ['another volume mounted there', (table) => changed(table, cache.mountpoint, { dev: 90n, birthtimeNs: 6_500n })],
122
+ ['the same device with another root', (table) => changed(table, cache.mountpoint, { ino: 3n })],
123
+ ['the same root reborn', (table) => changed(table, cache.mountpoint, { birthtimeNs: 6_101n })],
124
+ ['no longer a directory', (table) => changed(table, cache.mountpoint, { shape: 'file' })],
125
+ ['mountpoint removed', (table) => table.delete(cache.mountpoint)],
126
+ ['a parent replaced by a file', (table) => table.set(cache.mountpoint, enotdir)],
127
+ ];
128
+ for (const [what, change] of changes) {
129
+ const table = heldTable();
130
+ const verdict = await capturedOn(table);
131
+ change(table);
132
+ expect([what, await tableHost(table).validate(verdict, context)]).toEqual([what, { ok: true, value: false }]);
133
+ }
134
+ });
135
+
136
+ it('is about one volume incarnation and lease: any other context asks for a fresh inventory', async () => {
137
+ const table = heldTable();
138
+ const verdict = await capturedOn(table);
139
+ for (const other of [
140
+ { ...context, volumeKey: 'root 77:2:100 marker 77:9:101:201' },
141
+ { ...context, leaseKey: '77:40:2' },
142
+ { ...context, ramDevice: '78' },
143
+ ]) {
144
+ expect(await tableHost(table).validate(verdict, other)).toEqual({ ok: true, value: false });
145
+ }
146
+ });
147
+
148
+ it('reports an unreadable mount as an error, never as a verdict', async () => {
149
+ const table = heldTable();
150
+ const verdict = await capturedOn(table);
151
+ table.set(
152
+ cache.mountpoint,
153
+ Object.assign(new Error(`EACCES: permission denied, lstat '${cache.mountpoint}'`), { code: 'EACCES' }),
154
+ );
155
+ const expected: CacheResult<never> = {
156
+ ok: false,
157
+ error: `lstat ${cache.mountpoint}: EACCES: permission denied, lstat '${cache.mountpoint}'`,
158
+ };
159
+ expect(await tableHost(table).capture(context, [workspace, cache])).toEqual(expected);
160
+ expect(await tableHost(table).validate(verdict, context)).toEqual(expected);
161
+ });
162
+
163
+ it('reads mounts without following links, and their owners churning entries keeps them valid', async () => {
164
+ const root = mkdtempSync(join(realpathSync(tmpdir()), 'ram-held-'));
165
+ try {
166
+ const mounted = join(root, 'workspace');
167
+ mkdirSync(mounted);
168
+ const link = join(root, 'link');
169
+ symlinkSync(mounted, link);
170
+ const device = String(lstatSync(mounted).dev);
171
+ // Seen from a RAM volume on another device, the directory stands in for a mounted volume.
172
+ const elsewhere: HeldContext = { ...context, ramDevice: device === '1' ? '2' : '1' };
173
+ const candidate: HeldCandidate = { kind: 'mount', mountpoint: mounted, description: `${mounted} (mounted)` };
174
+ const captured = await captureHeldVerdict(elsewhere, [candidate]);
175
+ if (!captured.ok || !captured.value) {
176
+ throw new Error(`no verdict: ${JSON.stringify(captured)}`);
177
+ }
178
+ // Its owner's files coming and going move the root's ctime and mtime, not its identity.
179
+ writeFileSync(join(mounted, 'scratch'), 'x');
180
+ mkdirSync(join(mounted, 'build'));
181
+ rmSync(join(mounted, 'scratch'));
182
+ expect(await validateHeldVerdict(captured.value, elsewhere)).toEqual({ ok: true, value: true });
183
+ // On the RAM volume's own device it is a plain directory, which proves nothing.
184
+ expect(await captureHeldVerdict({ ...context, ramDevice: device }, [candidate])).toEqual({
185
+ ok: true,
186
+ value: undefined,
187
+ });
188
+ // A link to a directory is not a mount: no-follow sees the link.
189
+ expect(await captureHeldVerdict(elsewhere, [{ ...candidate, mountpoint: link }])).toEqual({
190
+ ok: true,
191
+ value: undefined,
192
+ });
193
+ rmSync(mounted, { recursive: true });
194
+ expect(await validateHeldVerdict(captured.value, elsewhere)).toEqual({ ok: true, value: false });
195
+ } finally {
196
+ rmSync(root, { recursive: true, force: true });
197
+ }
198
+ });
199
+ });
200
+
201
+ describe('RAM lease held verdict wire', () => {
202
+ it('round-trips through JSON to an equal verdict that still validates', async () => {
203
+ const table = heldTable();
204
+ const verdict = await capturedOn(table);
205
+ const parsed = parseHeldVerdict(JSON.parse(JSON.stringify(verdict)));
206
+ expect(parsed).toEqual(verdict);
207
+ if (!parsed) {
208
+ throw new Error('round trip lost the verdict');
209
+ }
210
+ expect(await tableHost(table).validate(parsed, context)).toEqual({ ok: true, value: true });
211
+ });
212
+
213
+ it('keeps only the fields it knows', async () => {
214
+ const verdict = await capturedOn(heldTable());
215
+ const [first, ...rest] = verdict.holders;
216
+ const wire = {
217
+ ...verdict,
218
+ extra: true,
219
+ context: { ...verdict.context, extra: true },
220
+ holders: [{ ...first, extra: true, root: { ...first.root, ctimeNs: '1' } }, ...rest],
221
+ };
222
+ expect(parseHeldVerdict(JSON.parse(JSON.stringify(wire)))).toEqual(verdict);
223
+ });
224
+
225
+ it('refuses every malformed wire, so the caller probes the inventory instead of trusting it', async () => {
226
+ const verdict = await capturedOn(heldTable());
227
+ const [first] = verdict.holders;
228
+ // What a store written while image holders were keyed would carry.
229
+ const legacyImage = {
230
+ ...image,
231
+ image: { dev: '77', ino: '501', birthtimeNs: '900' },
232
+ disk: { dev: '3', ino: '655', birthtimeNs: '0', rdev: '16777258', ctimeNs: '5000' },
233
+ };
234
+ const malformed: [string, unknown][] = [
235
+ ['not an object', 'held'],
236
+ ['null', null],
237
+ ['an array', [verdict]],
238
+ ['another version', { ...verdict, version: 2 }],
239
+ ['no context', { ...verdict, context: undefined }],
240
+ ['an empty lease key', { ...verdict, context: { ...verdict.context, leaseKey: '' } }],
241
+ ['a RAM device that is no integer', { ...verdict, context: { ...verdict.context, ramDevice: 'disk3' } }],
242
+ ['no holders', { ...verdict, holders: [] }],
243
+ ['holders that are no list', { ...verdict, holders: first }],
244
+ ['an image holder', { ...verdict, holders: [legacyImage] }],
245
+ ['a mount beside an image holder', { ...verdict, holders: [first, legacyImage] }],
246
+ ['an unknown holder kind', { ...verdict, holders: [{ ...first, kind: 'socket' }] }],
247
+ ['a holder without its description', { ...verdict, holders: [{ ...first, description: '' }] }],
248
+ ['a holder without its mountpoint', { ...verdict, holders: [{ ...first, mountpoint: undefined }] }],
249
+ ['a mount without its root', { ...verdict, holders: [{ ...first, root: undefined }] }],
250
+ ['a root without its birth', { ...verdict, holders: [{ ...first, root: { ...first.root, birthtimeNs: '' } }] }],
251
+ [
252
+ 'an identity as a number, losing precision',
253
+ { ...verdict, holders: [{ ...first, root: { ...first.root, ino: 2 } }] },
254
+ ],
255
+ [
256
+ 'an identity in a spelling no read writes',
257
+ { ...verdict, holders: [{ ...first, root: { ...first.root, dev: '08' } }] },
258
+ ],
259
+ ];
260
+ for (const [what, wire] of malformed) {
261
+ expect([what, parseHeldVerdict(JSON.parse(JSON.stringify(wire)))]).toEqual([what, undefined]);
262
+ }
263
+ });
264
+ });
@@ -0,0 +1,242 @@
1
+ import { lstat } from 'node:fs/promises';
2
+ import { isRecord } from '../../type-guards.js';
3
+
4
+ /**
5
+ * A dead RAM lease kept because something is attached from below it stays kept until its owner
6
+ * detaches it, and every acquisition visits it. Proving it is still held from the host's full
7
+ * attachment inventory (`ioreg` and `mount`) costs seconds a pass; this module proves it again
8
+ * from a no-follow `lstat` of each mount the last inventory named.
9
+ *
10
+ * A verdict is only ever positive: it records holders that were there, never their absence, so
11
+ * nothing read from it can remove a lease. A mount is keyed by its root's dev, inode and birth,
12
+ * on a device other than the RAM volume's: files coming and going in it move only its ctime, and
13
+ * an unmount exposes the RAM directory beneath. An attached image has no such key: a whole-disk
14
+ * node's identity is not measured to change when DiskImages reuses its number for another image,
15
+ * so an image holder leaves nothing to cache and its lease keeps the fresh inventory's result.
16
+ * Any absence or change sends the caller back to a fresh inventory. This module reads only; the
17
+ * volume's owner stores verdicts.
18
+ */
19
+
20
+ /** Which volume incarnation and lease a verdict is about; keyed by the volume's owner. */
21
+ export interface HeldContext {
22
+ /** The mounted RAM root's and marker's stable identity: no root ctime or mtime. */
23
+ readonly volumeKey: string;
24
+ /** The lease directory's identity. */
25
+ readonly leaseKey: string;
26
+ /** The mounted RAM root's `st_dev`, in decimal. */
27
+ readonly ramDevice: string;
28
+ }
29
+
30
+ /** A holder the inventory named below a lease, with the text that names it to the task. */
31
+ export type HeldCandidate =
32
+ | { readonly kind: 'image'; readonly imagePath: string; readonly diskPath: string; readonly description: string }
33
+ | { readonly kind: 'mount'; readonly mountpoint: string; readonly description: string };
34
+
35
+ /**
36
+ * A mount as it was keyed: the identity of the mounted root, which stays the same for as long as
37
+ * the same volume is mounted there. Decimal strings: exact on the wire.
38
+ */
39
+ export interface HeldProof {
40
+ readonly kind: 'mount';
41
+ readonly mountpoint: string;
42
+ readonly description: string;
43
+ readonly root: { readonly dev: string; readonly ino: string; readonly birthtimeNs: string };
44
+ }
45
+
46
+ /** A lease proven held by every one of its holders, as JSON carries it. */
47
+ export interface HeldVerdict {
48
+ readonly version: 1;
49
+ readonly context: HeldContext;
50
+ readonly holders: readonly [HeldProof, ...HeldProof[]];
51
+ }
52
+
53
+ export type CacheResult<T> = { ok: true; value: T } | { ok: false; error: string };
54
+
55
+ /** The no-follow metadata a proof reads: `lstat(path, { bigint: true })` on the host. */
56
+ export interface HolderStats {
57
+ readonly dev: bigint;
58
+ readonly ino: bigint;
59
+ readonly birthtimeNs: bigint;
60
+ isDirectory(): boolean;
61
+ }
62
+
63
+ /** Proving held leases over `read`; a seam so mounts are testable on any host. */
64
+ export interface HeldProver {
65
+ capture(context: HeldContext, candidates: readonly HeldCandidate[]): Promise<CacheResult<HeldVerdict | undefined>>;
66
+ validate(verdict: HeldVerdict, context: HeldContext): Promise<CacheResult<boolean>>;
67
+ }
68
+
69
+ /** A holder read once: keyed, without a key (gone, unmounted, or an image), or unreadable. */
70
+ type Keyed = { kind: 'keyed'; proof: HeldProof } | { kind: 'unkeyed' } | { kind: 'failed'; error: string };
71
+
72
+ export function heldProverOn(read: (path: string) => Promise<HolderStats>): HeldProver {
73
+ const key = async (candidate: HeldCandidate, ramDevice: string): Promise<Keyed> => {
74
+ if (candidate.kind === 'image') {
75
+ return { kind: 'unkeyed' };
76
+ }
77
+ const { kind, mountpoint, description } = candidate;
78
+ let root: HolderStats;
79
+ try {
80
+ root = await read(mountpoint);
81
+ } catch (error) {
82
+ const code = typeof error === 'object' && error !== null && 'code' in error ? error.code : undefined;
83
+ return code === 'ENOENT' || code === 'ENOTDIR'
84
+ ? { kind: 'unkeyed' }
85
+ : { kind: 'failed', error: `lstat ${mountpoint}: ${error instanceof Error ? error.message : String(error)}` };
86
+ }
87
+ if (!root.isDirectory() || root.dev.toString() === ramDevice) {
88
+ return { kind: 'unkeyed' };
89
+ }
90
+ return {
91
+ kind: 'keyed',
92
+ proof: {
93
+ kind,
94
+ mountpoint,
95
+ description,
96
+ root: { dev: root.dev.toString(), ino: root.ino.toString(), birthtimeNs: root.birthtimeNs.toString() },
97
+ },
98
+ };
99
+ };
100
+
101
+ /** Every candidate keyed, or why not: the first unreadable one, else `undefined` for no proof. */
102
+ const keyAll = async (
103
+ candidates: readonly HeldCandidate[],
104
+ ramDevice: string,
105
+ ): Promise<CacheResult<readonly [HeldProof, ...HeldProof[]] | undefined>> => {
106
+ const keyed = await Promise.all(candidates.map((candidate) => key(candidate, ramDevice)));
107
+ const proofs: HeldProof[] = [];
108
+ for (const holder of keyed) {
109
+ if (holder.kind === 'failed') {
110
+ return { ok: false, error: holder.error };
111
+ }
112
+ if (holder.kind === 'keyed') {
113
+ proofs.push(holder.proof);
114
+ }
115
+ }
116
+ return { ok: true, value: proofs.length === keyed.length && nonEmpty(proofs) ? proofs : undefined };
117
+ };
118
+
119
+ return {
120
+ async capture(context, candidates) {
121
+ const proofs = await keyAll(candidates, context.ramDevice);
122
+ if (!proofs.ok) {
123
+ return proofs;
124
+ }
125
+ const { volumeKey, leaseKey, ramDevice } = context;
126
+ return {
127
+ ok: true,
128
+ value: proofs.value && { version: 1, context: { volumeKey, leaseKey, ramDevice }, holders: proofs.value },
129
+ };
130
+ },
131
+
132
+ async validate(verdict, context) {
133
+ const was = verdict.context;
134
+ const sameLease =
135
+ was.volumeKey === context.volumeKey && was.leaseKey === context.leaseKey && was.ramDevice === context.ramDevice;
136
+ if (!sameLease) {
137
+ return { ok: true, value: false };
138
+ }
139
+ const proofs = await keyAll(verdict.holders, context.ramDevice);
140
+ if (!proofs.ok) {
141
+ return proofs;
142
+ }
143
+ const now = proofs.value;
144
+ return {
145
+ ok: true,
146
+ value:
147
+ now !== undefined &&
148
+ verdict.holders.every(({ mountpoint, root }, index) => {
149
+ const again = now[index];
150
+ return (
151
+ again !== undefined &&
152
+ again.mountpoint === mountpoint &&
153
+ again.root.dev === root.dev &&
154
+ again.root.ino === root.ino &&
155
+ again.root.birthtimeNs === root.birthtimeNs
156
+ );
157
+ }),
158
+ };
159
+ },
160
+ };
161
+ }
162
+
163
+ const hostProver = heldProverOn((path) => lstat(path, { bigint: true }));
164
+
165
+ /**
166
+ * The verdict that `candidates`, the holders a fresh inventory found below the lease, still hold
167
+ * it; `undefined` when there are none or one cannot be keyed, an image always among them, which
168
+ * leaves the inventory's held result standing uncached. An unreadable holder is an error, not a
169
+ * verdict.
170
+ */
171
+ export function captureHeldVerdict(
172
+ context: HeldContext,
173
+ candidates: readonly HeldCandidate[],
174
+ ): Promise<CacheResult<HeldVerdict | undefined>> {
175
+ return hostProver.capture(context, candidates);
176
+ }
177
+
178
+ /**
179
+ * Whether `verdict` still proves its lease held in `context`: the same volume incarnation and
180
+ * lease, and every holder still mounted with the identity it was keyed by. `false` asks for a
181
+ * fresh inventory; it never says the lease is free.
182
+ */
183
+ export function validateHeldVerdict(verdict: HeldVerdict, context: HeldContext): Promise<CacheResult<boolean>> {
184
+ return hostProver.validate(verdict, context);
185
+ }
186
+
187
+ /** A verdict from its JSON value; `undefined` for anything malformed, which asks for a fresh inventory. */
188
+ export function parseHeldVerdict(value: unknown): HeldVerdict | undefined {
189
+ if (!isRecord(value) || value.version !== 1 || !Array.isArray(value.holders)) {
190
+ return undefined;
191
+ }
192
+ const context = parseContext(value.context);
193
+ const proofs: HeldProof[] = [];
194
+ for (const holder of value.holders) {
195
+ const proof = parseProof(holder);
196
+ if (!proof) {
197
+ return undefined;
198
+ }
199
+ proofs.push(proof);
200
+ }
201
+ const holders = nonEmpty(proofs) ? proofs : undefined;
202
+ return context && holders && { version: 1, context, holders };
203
+ }
204
+
205
+ function parseContext(value: unknown): HeldContext | undefined {
206
+ if (!isRecord(value)) {
207
+ return undefined;
208
+ }
209
+ const volumeKey = text(value.volumeKey);
210
+ const leaseKey = text(value.leaseKey);
211
+ const ramDevice = decimal(value.ramDevice);
212
+ return volumeKey && leaseKey && ramDevice ? { volumeKey, leaseKey, ramDevice } : undefined;
213
+ }
214
+
215
+ /** A mount proof; any other kind, an image's among them, is malformed. */
216
+ function parseProof(value: unknown): HeldProof | undefined {
217
+ if (!isRecord(value) || value.kind !== 'mount' || !isRecord(value.root)) {
218
+ return undefined;
219
+ }
220
+ const mountpoint = text(value.mountpoint);
221
+ const description = text(value.description);
222
+ const dev = decimal(value.root.dev);
223
+ const ino = decimal(value.root.ino);
224
+ const birthtimeNs = decimal(value.root.birthtimeNs);
225
+ return mountpoint && description && dev && ino && birthtimeNs
226
+ ? { kind: 'mount', mountpoint, description, root: { dev, ino, birthtimeNs } }
227
+ : undefined;
228
+ }
229
+
230
+ /** A nonempty string. */
231
+ function text(value: unknown): string | undefined {
232
+ return typeof value === 'string' && value.length > 0 ? value : undefined;
233
+ }
234
+
235
+ /** An integer as `bigint.toString()` writes it: the only spelling that compares equal to a fresh read. */
236
+ function decimal(value: unknown): string | undefined {
237
+ return typeof value === 'string' && /^(?:0|-?[1-9]\d*)$/.test(value) ? value : undefined;
238
+ }
239
+
240
+ function nonEmpty(items: readonly HeldProof[]): items is readonly [HeldProof, ...HeldProof[]] {
241
+ return items.length > 0;
242
+ }