@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,177 @@
1
+ import { GATEWAY_SOCKET, holdLease, type UnleasedCause } from './gateway-lease.js';
2
+ import { BUN_TEST, NEXTEST_RUN } from './verdict.js';
3
+
4
+ /**
5
+ * The client half of cowshed's host CPU budget (specs/cowshed/05_gateway.md, "Host CPU budget").
6
+ *
7
+ * Every gate on a host sizes its runners to the whole machine: Nx runs a task per core and each
8
+ * nextest run under it runs a test thread per core again, so a few concurrent gates keep the load
9
+ * average several times the core count and tests that do no disk work time out waiting for a CPU.
10
+ * The cowshed gateway holds one budget of CPU tokens for the host, about one per core. Before it
11
+ * starts a runner, bounded-exec asks for as many tokens as the runner would run threads or
12
+ * processes at once, and sizes the runner to the grant: every gate keeps running in parallel, and
13
+ * the total stays near the core count.
14
+ */
15
+
16
+ /** How a runner's parallelism is set, which decides what it asks for and how a grant sizes it. */
17
+ export type RunnerKind =
18
+ /** `nextest run`: its test threads, `--test-threads` or `NEXTEST_TEST_THREADS`. */
19
+ | 'nextest'
20
+ /** `bun test --parallel`: its worker processes. */
21
+ | 'bun-parallel'
22
+ /** A cargo build (`cargo build`/`test`/`clippy`/…, `nextest archive`, `napi build`): its jobs. */
23
+ | 'cargo'
24
+ /** Anything else, one `bun test` process included: one process unless it declares more. */
25
+ | 'process';
26
+
27
+ /** What a runner asks the budget for; Cargo also carries its independent test-thread ceiling. */
28
+ export type Demand =
29
+ | { kind: 'cargo'; want: number; testThreads: number | null }
30
+ | { kind: Exclude<RunnerKind, 'cargo'>; want: number };
31
+
32
+ /** Every budgeted command learns its grant here, so a script that starts a runner can size it. */
33
+ export const CPU_TOKENS_ENV = 'BOUNDED_EXEC_CPU_TOKENS';
34
+
35
+ const CARGO_BUILD =
36
+ /(?<=^|[\s;&|()])(?:cargo(?:\s+-\S+)*\s+(?:build|test|check|clippy|rustc|doc|run|nextest\s+archive)|napi\s+build)(?=\s|$)/;
37
+ const TEST_THREADS = /--test-threads(?:=|\s+)(\d+)/g;
38
+ const BUN_PARALLEL = /--parallel(?:=(\d+))?(?=\s|$)/g;
39
+ const INHERITED_COUNT = /^[1-9]\d*$/;
40
+
41
+ /**
42
+ * What `command` asks for: its kind by invocation, and `declared` (the target's `parallelism`)
43
+ * when it says, else the count the command names (`--test-threads=N`, `--parallel=N`), else the
44
+ * runner's own default — one thread per core for nextest, a bare `--parallel` and cargo, one
45
+ * process for anything else. Positive inherited runner counts are ceilings; configured counts may only lower them.
46
+ */
47
+ export function runnerDemand(
48
+ command: string,
49
+ declared: number | undefined,
50
+ cores: number,
51
+ inherited: Readonly<NodeJS.ProcessEnv>,
52
+ configured?: Readonly<NodeJS.ProcessEnv>,
53
+ ): Demand {
54
+ const kind: RunnerKind =
55
+ command.search(NEXTEST_RUN) !== -1
56
+ ? 'nextest'
57
+ : command.search(BUN_TEST) !== -1 && command.search(BUN_PARALLEL) !== -1
58
+ ? 'bun-parallel'
59
+ : command.search(CARGO_BUILD) !== -1
60
+ ? 'cargo'
61
+ : 'process';
62
+ const counted = kind === 'nextest' ? TEST_THREADS : kind === 'bun-parallel' ? BUN_PARALLEL : null;
63
+ const named =
64
+ counted === null
65
+ ? []
66
+ : [...command.matchAll(counted)].flatMap((match) => (match[1] === undefined ? [] : [Number(match[1])]));
67
+ const requested = declared ?? (named.length > 0 ? Math.max(...named) : kind === 'process' ? 1 : cores);
68
+ const ceiling =
69
+ kind === 'nextest'
70
+ ? inheritedCeiling(inherited.NEXTEST_TEST_THREADS, configured?.NEXTEST_TEST_THREADS)
71
+ : kind === 'cargo'
72
+ ? inheritedCeiling(inherited.CARGO_BUILD_JOBS, configured?.CARGO_BUILD_JOBS)
73
+ : null;
74
+ const want = Math.max(1, Math.min(requested, ceiling ?? requested));
75
+ return kind === 'cargo'
76
+ ? { kind, want, testThreads: inheritedCeiling(inherited.RUST_TEST_THREADS, configured?.RUST_TEST_THREADS) }
77
+ : { kind, want };
78
+ }
79
+
80
+ /**
81
+ * Size the runner to the lesser of its bounded demand and grant, retaining Cargo's test-thread ceiling.
82
+ * Rewrite named flags so they cannot outrank that ceiling. `CPU_TOKENS_ENV` still reports the actual held grant.
83
+ */
84
+ export function sizeRunner(
85
+ demand: Demand,
86
+ command: string,
87
+ tokens: number,
88
+ ): { command: string; env: Record<string, string> } {
89
+ const runnerCount = Math.min(tokens, demand.want);
90
+ const count = String(runnerCount);
91
+ const held = String(tokens);
92
+ switch (demand.kind) {
93
+ case 'nextest':
94
+ return {
95
+ command: command.replace(TEST_THREADS, `--test-threads=${count}`),
96
+ env: { [CPU_TOKENS_ENV]: held, NEXTEST_TEST_THREADS: count },
97
+ };
98
+ case 'bun-parallel':
99
+ return { command: command.replace(BUN_PARALLEL, `--parallel=${count}`), env: { [CPU_TOKENS_ENV]: held } };
100
+ case 'cargo':
101
+ return {
102
+ command,
103
+ env: {
104
+ [CPU_TOKENS_ENV]: held,
105
+ CARGO_BUILD_JOBS: count,
106
+ RUST_TEST_THREADS: String(Math.min(runnerCount, demand.testThreads ?? runnerCount)),
107
+ },
108
+ };
109
+ case 'process':
110
+ return { command, env: { [CPU_TOKENS_ENV]: held } };
111
+ }
112
+ }
113
+
114
+ /** A configured value may lower an inherited ceiling, never raise it. */
115
+ function inheritedCeiling(inherited: string | undefined, configured: string | undefined): number | null {
116
+ const left = parseInheritedTokens(inherited);
117
+ const right = parseInheritedTokens(configured);
118
+ return left === null ? right : right === null ? left : Math.min(left, right);
119
+ }
120
+
121
+ /**
122
+ * The count `CPU_TOKENS_ENV` names, as a positive safe integer, or `null` when absent or malformed. It is
123
+ * descriptor input only: a count proves nothing about a live pool and grants nothing. A missing or malformed
124
+ * descriptor follows normal host admission; a valid one still needs a grant from its endpoint. Connection
125
+ * absence follows the host path, while a connected parent's refusal fails admission to avoid hold-and-wait.
126
+ */
127
+ export function parseInheritedTokens(value: string | undefined): number | null {
128
+ if (value === undefined || !INHERITED_COUNT.test(value)) {
129
+ return null;
130
+ }
131
+ const tokens = Number(value);
132
+ return Number.isSafeInteger(tokens) ? tokens : null;
133
+ }
134
+
135
+ /**
136
+ * Tokens held until released, or why the runner starts without the budget. `closed` resolves when
137
+ * the grant's connection closes, however it closes, so a sub-pool built on the grant ends with it.
138
+ * `inherited` marks tokens a parent's CPU pool lent, not tokens the host gateway granted.
139
+ */
140
+ export type TokenGrant =
141
+ | { granted: true; tokens: number; release: () => void; closed: Promise<void>; inherited?: true }
142
+ | { granted: false; cause: UnleasedCause; reason: string };
143
+
144
+ /** Where bounded-exec takes its tokens; the gateway in production, a fake in tests. */
145
+ export interface CpuBudget {
146
+ take(want: number, checkout: string, command: string): Promise<TokenGrant>;
147
+ }
148
+
149
+ /**
150
+ * The gateway at `socket`'s budget. A grant has no deadline: the wait is how the budget works, and
151
+ * the gateway answers `queued` at once, so a client knows it is alive. `checkout` is the Nx
152
+ * workspace root: the gateway shares tokens fairly between checkouts.
153
+ */
154
+ export function gatewayCpuBudget(socket: string = GATEWAY_SOCKET): CpuBudget {
155
+ return {
156
+ async take(want, checkout, command) {
157
+ const lease = await holdLease(socket, { op: 'cpu-tokens', want, checkout, command }, 'the CPU budget', {
158
+ ackMs: 2_000,
159
+ afterCloseMs: 5_000,
160
+ grantMs: null,
161
+ });
162
+ if (!lease.granted) {
163
+ return lease;
164
+ }
165
+ const tokens = lease.answer.tokens;
166
+ if (typeof tokens !== 'number' || !Number.isInteger(tokens) || tokens < 1 || tokens > want) {
167
+ lease.release();
168
+ return {
169
+ granted: false,
170
+ cause: 'other',
171
+ reason: `the gateway granted ${JSON.stringify(tokens)} tokens for ${want}`,
172
+ };
173
+ }
174
+ return { granted: true, tokens, release: lease.release, closed: lease.closed };
175
+ },
176
+ };
177
+ }
@@ -0,0 +1,185 @@
1
+ import { afterEach, describe, expect, it } from 'bun:test';
2
+ import { rmSync } from 'node:fs';
3
+ import { mkdtemp, readFile } from 'node:fs/promises';
4
+ import { createServer, type Server, type Socket } from 'node:net';
5
+ import { tmpdir } from 'node:os';
6
+ import { join } from 'node:path';
7
+ import { ownedSocketDirectory } from '../../testing.js';
8
+ import { diskClassOf, takeDiskLease, unixMicros, waitRecord } from './disk-lease.js';
9
+
10
+ /**
11
+ * Real, short bounds: the client's half-close and grant expiry are socket behavior against the
12
+ * platform clock, which fake timers would not drive through a real Unix socket.
13
+ */
14
+ const QUICK = { ackMs: 200, afterCloseMs: 5_000, grantMs: 500 };
15
+
16
+ const servers: Server[] = [];
17
+ const directories: string[] = [];
18
+
19
+ afterEach(() => {
20
+ for (const server of servers.splice(0)) {
21
+ server.close();
22
+ }
23
+ for (const directory of directories.splice(0)) {
24
+ rmSync(directory, { recursive: true, force: true });
25
+ }
26
+ });
27
+
28
+ /**
29
+ * A socket path in a directory of its own, made in the socket namespace the sandbox admits for Unix
30
+ * sockets: a literal `/tmp` path binds, but no sandboxed process may connect to it, so a client of it
31
+ * would see a refusal in place of the absent socket or the gateway the test means.
32
+ */
33
+ async function socketPath(name: string): Promise<string> {
34
+ const directory = await ownedSocketDirectory('dl-', name);
35
+ directories.push(directory);
36
+ return join(directory, name);
37
+ }
38
+
39
+ /** A gateway stand-in on a socket in a directory of its own that runs `serve` per client. */
40
+ async function gateway(serve: (client: Socket) => void): Promise<string> {
41
+ const socket = await socketPath('gw.sock');
42
+ const server = createServer({ allowHalfOpen: true }, serve);
43
+ const { promise, resolve, reject } = Promise.withResolvers<void>();
44
+ server.once('error', reject);
45
+ server.listen(socket, resolve);
46
+ await promise;
47
+ server.off('error', reject);
48
+ servers.push(server);
49
+ return socket;
50
+ }
51
+
52
+ /** A fresh directory under the OS temp dir, removed after the test. */
53
+ async function scratchDirectory(): Promise<string> {
54
+ const directory = await mkdtemp(join(tmpdir(), 'disk-lease-record-'));
55
+ directories.push(directory);
56
+ return directory;
57
+ }
58
+
59
+ describe('cowshed disk-lifecycle lease client', () => {
60
+ it('classes programs by what they change', () => {
61
+ expect(diskClassOf('/usr/sbin/diskutil')).toBe('storage');
62
+ expect(diskClassOf('/usr/bin/hdiutil')).toBe('storage');
63
+ expect(diskClassOf('/sbin/mount_apfs')).toBe('namespace');
64
+ expect(diskClassOf('/sbin/umount')).toBe('namespace');
65
+ expect(diskClassOf('/sbin/mount')).toBeNull();
66
+ expect(diskClassOf('/usr/sbin/ioreg')).toBeNull();
67
+ });
68
+
69
+ it('asks with one line, waits for queued then granted, and holds until released', async () => {
70
+ const { promise: request, resolve: requested } = Promise.withResolvers<string>();
71
+ const { promise: closed, resolve: close } = Promise.withResolvers<void>();
72
+ const socket = await gateway((client) => {
73
+ client.once('data', (chunk) => {
74
+ requested(chunk.toString('utf8'));
75
+ client.write('{"ok":true,"lease":"queued"}\n{"ok":true,"lease":"granted"}\n');
76
+ });
77
+ // The gateway takes a lease back when it reads EOF. With allowHalfOpen the socket stays open
78
+ // after the peer closes until the server ends it, so 'close' is not that signal on Linux.
79
+ client.on('end', () => close());
80
+ });
81
+ const lease = await takeDiskLease(socket, 'storage', '/usr/bin/hdiutil detach /dev/disk9', QUICK, 'shared');
82
+ expect(await request).toBe(
83
+ '{"op":"disk-lease","class":"storage","command":"/usr/bin/hdiutil detach /dev/disk9"}\n',
84
+ );
85
+ expect(lease.granted).toBe(true);
86
+ if (lease.granted) {
87
+ lease.release();
88
+ }
89
+ await closed;
90
+ });
91
+
92
+ it('recognizes a gateway that predates leases by its answer to the half-close', async () => {
93
+ const socket = await gateway((client) => {
94
+ client.resume();
95
+ client.on('end', () =>
96
+ client.end('{"ok":false,"code":"invalid-request","error":"unknown gateway control operation"}\n'),
97
+ );
98
+ });
99
+ const lease = await takeDiskLease(socket, 'namespace', '/sbin/umount /x', QUICK, 'shared');
100
+ expect(lease).toEqual({
101
+ granted: false,
102
+ cause: 'predates',
103
+ reason:
104
+ 'the gateway predates disk leases (invalid-request: unknown gateway control operation); restart it with `cowshed setup`',
105
+ });
106
+ });
107
+
108
+ it('asks for the whole phase by naming its share, and only then', async () => {
109
+ const { promise: request, resolve: requested } = Promise.withResolvers<string>();
110
+ const socket = await gateway((client) => {
111
+ client.once('data', (chunk) => {
112
+ requested(chunk.toString('utf8'));
113
+ client.write('{"ok":true,"lease":"queued"}\n{"ok":true,"lease":"granted"}\n');
114
+ });
115
+ });
116
+ const lease = await takeDiskLease(socket, 'storage', '/usr/bin/hdiutil detach /dev/disk9', QUICK, 'alone');
117
+ expect(await request).toBe(
118
+ '{"op":"disk-lease","class":"storage","share":"alone","command":"/usr/bin/hdiutil detach /dev/disk9"}\n',
119
+ );
120
+ expect(lease.granted).toBe(true);
121
+ if (lease.granted) {
122
+ lease.release();
123
+ }
124
+ });
125
+
126
+ it('recognizes a gateway that predates the share field by its refusal at once', async () => {
127
+ const socket = await gateway((client) => {
128
+ client.once('data', () =>
129
+ client.end(
130
+ '{"ok":false,"code":"invalid-request","error":"gateway control encoding failed: gateway control request contains an unknown field"}\n',
131
+ ),
132
+ );
133
+ });
134
+ const lease = await takeDiskLease(socket, 'storage', 'x', QUICK, 'alone');
135
+ expect(lease.granted === false && lease.cause).toBe('predates-field');
136
+ });
137
+
138
+ it('runs unleased, said as absent, when no gateway listens', async () => {
139
+ const lease = await takeDiskLease(await socketPath('none.sock'), 'storage', 'x', QUICK, 'shared');
140
+ expect(lease.granted === false && lease.cause).toBe('absent');
141
+ });
142
+
143
+ it('runs unleased when the grant never comes', async () => {
144
+ const socket = await gateway((client) => {
145
+ client.once('data', () => client.write('{"ok":true,"lease":"queued"}\n'));
146
+ });
147
+ const lease = await takeDiskLease(socket, 'storage', 'x', QUICK, 'shared');
148
+ expect(lease).toEqual({ granted: false, cause: 'other', reason: 'the gateway granted nothing within 500 ms' });
149
+ });
150
+
151
+ it('appends a start and an end line per wait, numbered per record and stamped in microseconds', async () => {
152
+ const file = join(await scratchDirectory(), 'waits');
153
+ const record = waitRecord(file);
154
+
155
+ const before = unixMicros();
156
+ record.begin('storage')();
157
+ record.begin('namespace')();
158
+ const after = unixMicros();
159
+
160
+ const text = await readFile(file, 'utf8');
161
+ expect(text.endsWith('\n')).toBe(true);
162
+ const lines = text.slice(0, -1).split('\n');
163
+ // The stamp follows the seq on every line; blanking it leaves each line's shape exact.
164
+ const shapes = lines.map((line) => line.replace(/^(\S+ \S+ \S+) \d+/, '$1 <stamp>'));
165
+ expect(shapes).toEqual([
166
+ `start ${process.pid} 0 <stamp> storage`,
167
+ `end ${process.pid} 0 <stamp>`,
168
+ `start ${process.pid} 1 <stamp> namespace`,
169
+ `end ${process.pid} 1 <stamp>`,
170
+ ]);
171
+ for (const line of lines) {
172
+ const stamp = line.split(' ')[3];
173
+ expect(stamp).toMatch(/^\d+$/);
174
+ expect(Number(stamp)).toBeGreaterThanOrEqual(before);
175
+ expect(Number(stamp)).toBeLessThanOrEqual(after);
176
+ }
177
+ });
178
+
179
+ it('never fails a wait for a record it cannot write', async () => {
180
+ // A directory cannot be appended to, so every write to this record fails.
181
+ const record = waitRecord(await scratchDirectory());
182
+ expect(() => record.begin('storage')()).not.toThrow();
183
+ expect(() => record.begin('namespace')()).not.toThrow();
184
+ });
185
+ });
@@ -0,0 +1,139 @@
1
+ import { appendFileSync } from 'node:fs';
2
+ import { holdLease, type LeaseBounds, type UnleasedCause } from './gateway-lease.js';
3
+
4
+ /**
5
+ * The client half of cowshed's host disk-lifecycle lease (specs/cowshed/05_gateway.md,
6
+ * "Disk-lifecycle lease"), for the RAM temp volume's disk commands.
7
+ *
8
+ * An attach spends most of its time in StorageKit's `syncAllDisks`, which does not finish while
9
+ * the mount table keeps changing, so the cowshed gateway schedules every disk tool on the host in
10
+ * two classes that never overlap. This module's `hdiutil` and `diskutil` calls take the same
11
+ * lease cowshed's own do, when the gateway is there to ask. The lease spaces commands out; it
12
+ * never decides whether one runs: without a grant, the command runs unleased.
13
+ */
14
+
15
+ export type DiskClass = 'storage' | 'namespace';
16
+
17
+ /**
18
+ * The class each disk tool draws on, by absolute path. `hdiutil`, `newfs_apfs`, and `diskutil`
19
+ * change the set of disks through `storagekitd`. `mount_apfs` and `umount` change the mount
20
+ * table. Every other program takes no lease (specs/cowshed/05_gateway.md, "Disk-lifecycle lease").
21
+ */
22
+ const CLASS_BY_PROGRAM: Record<string, DiskClass> = {
23
+ '/usr/sbin/diskutil': 'storage',
24
+ '/usr/bin/hdiutil': 'storage',
25
+ '/System/Library/Filesystems/apfs.fs/Contents/Resources/newfs_apfs': 'storage',
26
+ '/sbin/mount_apfs': 'namespace',
27
+ '/sbin/umount': 'namespace',
28
+ };
29
+
30
+ export function diskClassOf(file: string): DiskClass | null {
31
+ return Object.hasOwn(CLASS_BY_PROGRAM, file) ? CLASS_BY_PROGRAM[file] : null;
32
+ }
33
+
34
+ /**
35
+ * How much of its class's running phase a lease takes: one place beside the phase's other members, or the whole
36
+ * phase, so its command queues in `storagekitd` behind nobody (specs/cowshed/05_gateway.md, "Disk-lifecycle lease").
37
+ */
38
+ export type PhaseShare = 'shared' | 'alone';
39
+
40
+ /** A granted lease, held until released, or why the command runs without one. */
41
+ export type LeaseOutcome =
42
+ | { granted: true; release: () => void }
43
+ | { granted: false; cause: UnleasedCause; reason: string };
44
+
45
+ /**
46
+ * Names the file every lease wait of a test's process tree is appended to; unset outside the real-APFS lane. The
47
+ * record exists so the real-APFS lane's wrapper can judge a test's code time without its lease waits
48
+ * (specs/cowshed/08_testing.md, "Integration tests (real substrate)").
49
+ */
50
+ export const LEASE_WAIT_RECORD_ENV = 'COWSHED_LEASE_WAIT_RECORD';
51
+
52
+ /**
53
+ * A process's lease-wait record. `begin` appends the wait's `start` line and returns the function that appends its
54
+ * `end` line once the wait is over.
55
+ */
56
+ export interface WaitRecord {
57
+ begin(diskClass: DiskClass): () => void;
58
+ }
59
+
60
+ /** Wall-clock microseconds since the Unix epoch: the clock every lease-wait stamp is read from. */
61
+ export function unixMicros(): number {
62
+ return Math.round((performance.timeOrigin + performance.now()) * 1000);
63
+ }
64
+
65
+ /**
66
+ * The lease-wait record at `path`. A wait is two lines: `start <pid> <seq> <unix-µs> <class>` before the client asks,
67
+ * and `end <pid> <seq> <unix-µs>` once it is over, granted or not. `seq` counts this record's waits from 0. Each line is
68
+ * one append, so concurrent processes' lines never interleave. A record that cannot be written is said once per process
69
+ * and never fails a wait: its waits then count as the test's code time, which can only fail the bound.
70
+ */
71
+ export function waitRecord(path: string): WaitRecord {
72
+ let next = 0;
73
+ let said = false;
74
+ const append = (line: string): void => {
75
+ try {
76
+ appendFileSync(path, line, { mode: 0o600 });
77
+ } catch (error) {
78
+ if (!said) {
79
+ said = true;
80
+ const message = error instanceof Error ? error.message : String(error);
81
+ process.stderr.write(
82
+ `cowshed: disk-lease wait record ${path} is unwritable (${message}); this process's lease waits count as its test's code time\n`,
83
+ );
84
+ }
85
+ }
86
+ };
87
+ return {
88
+ begin(diskClass) {
89
+ const seq = next;
90
+ next += 1;
91
+ append(`start ${process.pid} ${seq} ${unixMicros()} ${diskClass}\n`);
92
+ return () => append(`end ${process.pid} ${seq} ${unixMicros()}\n`);
93
+ },
94
+ };
95
+ }
96
+
97
+ /** This process's record, read from the environment on first use: `undefined` until then, `null` when unset. */
98
+ let environmentRecord: WaitRecord | null | undefined;
99
+
100
+ function leaseWaitRecord(): WaitRecord | null {
101
+ if (environmentRecord === undefined) {
102
+ const path = process.env[LEASE_WAIT_RECORD_ENV];
103
+ environmentRecord = path === undefined ? null : waitRecord(path);
104
+ }
105
+ return environmentRecord;
106
+ }
107
+
108
+ /**
109
+ * The share this process asks for: `alone` when its waits are recorded, so its test's code time holds no other
110
+ * process's disk work, and `shared` otherwise (specs/cowshed/08_testing.md, "Integration tests (real substrate)").
111
+ */
112
+ export function recordedShare(): PhaseShare {
113
+ return leaseWaitRecord() === null ? 'shared' : 'alone';
114
+ }
115
+
116
+ /**
117
+ * Ask the gateway at `socket` for `share` of a `diskClass` phase for `command` (`holdLease`). A shared request names no
118
+ * `share`, so every lease-scheduling gateway reads it. The wait is appended to this process's lease-wait record when
119
+ * the environment names one, and its end is appended however the ask ends.
120
+ */
121
+ export async function takeDiskLease(
122
+ socket: string,
123
+ diskClass: DiskClass,
124
+ command: string,
125
+ bounds: LeaseBounds,
126
+ share: PhaseShare,
127
+ ): Promise<LeaseOutcome> {
128
+ const endWait = leaseWaitRecord()?.begin(diskClass);
129
+ const request =
130
+ share === 'alone'
131
+ ? { op: 'disk-lease', class: diskClass, share, command }
132
+ : { op: 'disk-lease', class: diskClass, command };
133
+ try {
134
+ const lease = await holdLease(socket, request, 'disk leases', bounds);
135
+ return lease.granted ? { granted: true, release: lease.release } : lease;
136
+ } finally {
137
+ endWait?.();
138
+ }
139
+ }