@smoothbricks/nx-plugin 0.4.26 → 0.4.27

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 +962 -148
  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
@@ -1,5 +1,13 @@
1
1
  import { spawn } from 'node:child_process';
2
- import { isAbsolute, join } from 'node:path';
2
+ import { availableParallelism, userInfo } from 'node:os';
3
+ import { isAbsolute, join, resolve as resolvePath } from 'node:path';
4
+ import { workspaceDataDirectoryForWorkspace } from 'nx/src/utils/cache-directory.js';
5
+ import { CPU_POOL_ENV, createCpuPool, readInheritedCpuPool, takeParentCpuTokens, } from './cpu-pool.js';
6
+ import { CPU_TOKENS_ENV, gatewayCpuBudget, runnerDemand, sizeRunner, } from './cpu-tokens.js';
7
+ import { captureHangDiagnostics, HANG_CAPTURE_MS } from './hang-diagnostics.js';
8
+ import { createRunProgress, withProgressReport } from './progress.js';
9
+ import { COWSHED_EXECUTABLE_ENV, createHostCommands, describeRamTempError, RAM_TEMP_CAPACITY_BYTES, RamTempVolume, ramTempPaths, } from './ram-temp.js';
10
+ import { clearTaskDirectory, JUNIT_FILE, JUNIT_REPORT_ENV, judgeOperationalFailure, judgeRun, readJunitReport, taskDirectory, withJunitReport, writeRecord, } from './verdict.js';
3
11
  const DEFAULT_KILL_AFTER_MS = 10_000;
4
12
  const REAP_AFTER_FORCE_KILL_MS = 2_000;
5
13
  const EXIT_CODE_BY_SIGNAL = {
@@ -7,53 +15,330 @@ const EXIT_CODE_BY_SIGNAL = {
7
15
  SIGINT: 130,
8
16
  SIGTERM: 143,
9
17
  };
18
+ /** The variables the CPU budget sets for the command it grants: no target's environment may set them. */
19
+ const BUDGET_ENV = [CPU_TOKENS_ENV, CPU_POOL_ENV];
10
20
  export default function boundedExecExecutor(options, context) {
11
- return runBoundedExec(options, context, createProcessTreeKiller());
21
+ return runDefaultBoundedExec(options, recordContext(context));
12
22
  }
13
- export async function runBoundedExec(options, context, killer) {
23
+ /** Runs with the normal process, RAM lease and CPU-budget owners, with an optional stdout sink. */
24
+ export function runDefaultBoundedExec(options, context) {
25
+ // A target that names the cowshed its disk commands run through names it for this run's own RAM volume work and
26
+ // for its command alike. Nx resolves `{workspaceRoot}/…` in an option to a workspace-relative path, which the
27
+ // command, starting in another directory, could not find: it gets the absolute one.
28
+ const named = options.env?.[COWSHED_EXECUTABLE_ENV];
29
+ const cowshed = named === undefined ? undefined : resolvePath(context.root, named);
30
+ const run = cowshed === undefined ? options : { ...options, env: { ...options.env, [COWSHED_EXECUTABLE_ENV]: cowshed } };
31
+ // Only macOS has `hdiutil ram://`; elsewhere the command keeps the inherited TMPDIR.
32
+ const tempVolume = process.platform === 'darwin'
33
+ ? new RamTempVolume(ramTempPaths(userInfo()), RAM_TEMP_CAPACITY_BYTES, createHostCommands(undefined, undefined, undefined, cowshed))
34
+ : null;
35
+ // A command a parent's CPU pool started draws its tokens from that pool. The pool is named by the environment the
36
+ // parent set for this command; a run no parent started has none.
37
+ const inherited = readInheritedCpuPool(process.env);
38
+ return runBoundedExec(run, context, createProcessTreeKiller(), tempVolume, gatewayCpuBudget(), inherited);
39
+ }
40
+ /**
41
+ * Nx gives an executor the task's project, target and configuration, not its id; the id is the
42
+ * key of the task graph entry they name. Outside a task graph the run records nothing.
43
+ */
44
+ export function recordContext(context) {
45
+ const { root, projectName, targetName, configurationName, taskGraph } = context;
46
+ if (!projectName || !targetName) {
47
+ return { root };
48
+ }
49
+ const id = `${projectName}:${targetName}${configurationName ? `:${configurationName}` : ''}`;
50
+ const task = taskGraph?.tasks[id];
51
+ if (task === undefined) {
52
+ return { root };
53
+ }
54
+ return {
55
+ root,
56
+ record: {
57
+ task: { id, hash: task.hash ?? null },
58
+ directory: taskDirectory(workspaceDataDirectoryForWorkspace(root), id),
59
+ },
60
+ };
61
+ }
62
+ /**
63
+ * Run the command once its runner holds CPU tokens from `budget` (`cpu-tokens.ts`), sized to the
64
+ * grant; `null`, or a budget nobody serves, runs it as asked. Both bounds start once it is
65
+ * granted: a wait for the host is not the command's time.
66
+ *
67
+ * `inherited` is the parent's CPU pool (`cpu-pool.ts`) that lends this run its tokens. The run asks that pool first
68
+ * and the host budget only when the pool refuses. Whatever it is granted, the run serves its own sub-pool to its
69
+ * descendants, closed before the grant returns. A borrowed grant that closes while the command runs stops the command
70
+ * as a lost grant, which is a failure.
71
+ */
72
+ export async function runBoundedExec(options, context, killer, tempVolume, budget, inherited = null, captureDiagnostics = captureHangDiagnostics) {
14
73
  const cwd = resolveCwd(options.cwd, context.root);
15
- const command = buildCommand(options);
74
+ const directory = context.record?.directory ?? null;
75
+ // A previous run's verdict and report go first, so neither can be read as this run's.
76
+ if (directory !== null) {
77
+ await clearTaskDirectory(directory);
78
+ }
79
+ const requested = buildCommand(options);
16
80
  const timeoutMs = options.timeoutMs;
17
81
  const idleTimeoutMs = options.idleTimeoutMs;
18
82
  const killAfterMs = options.killAfterMs ?? DEFAULT_KILL_AFTER_MS;
19
- const startedAt = Date.now();
20
83
  const outputChunks = [];
21
- const state = { settled: false, expiry: null, forceKillNeeded: false };
22
- const child = spawn(command, [], {
23
- cwd,
24
- env: mergeEnv(options.env),
25
- shell: true,
26
- detached: process.platform !== 'win32',
27
- windowsHide: true,
28
- });
84
+ const state = {
85
+ settled: false,
86
+ expiry: null,
87
+ forceKillNeeded: false,
88
+ grantLost: false,
89
+ poolCloseError: null,
90
+ };
29
91
  const appendStdout = (chunk) => {
30
92
  const text = chunk.toString();
31
- outputChunks.push(text);
32
- process.stdout.write(text);
93
+ if (context.stdout !== undefined) {
94
+ context.stdout(text);
95
+ }
96
+ else {
97
+ outputChunks.push(text);
98
+ process.stdout.write(text);
99
+ }
33
100
  };
34
101
  const appendStderr = (chunk) => {
35
102
  const text = chunk.toString();
36
103
  outputChunks.push(text);
37
104
  process.stderr.write(text);
38
105
  };
106
+ let lease = null;
107
+ if (tempVolume !== null) {
108
+ const acquired = await tempVolume.acquire();
109
+ if (!acquired.ok) {
110
+ const message = `${describeRamTempError(acquired.error)}\n`;
111
+ process.stderr.write(message);
112
+ return { success: false, terminalOutput: message };
113
+ }
114
+ if (acquired.value.kind === 'leased') {
115
+ lease = acquired.value.lease;
116
+ for (const held of acquired.value.held) {
117
+ appendStderr(`RAM temp volume: dead lease ${held}\n`);
118
+ }
119
+ for (const reaped of acquired.value.reaped) {
120
+ appendStderr(`RAM temp volume: stopped ${reaped}, which a dead task left working in its lease\n`);
121
+ }
122
+ }
123
+ else {
124
+ appendStderr(`RAM temp volume unavailable in this sandbox (${acquired.value.detail}); TMPDIR stays ${process.env.TMPDIR ?? '(unset)'}\n`);
125
+ }
126
+ }
127
+ const releaseLease = async () => {
128
+ if (lease === null) {
129
+ return;
130
+ }
131
+ const held = lease;
132
+ lease = null;
133
+ // What the command left working in its lease is a leak, whichever way the command ended; it
134
+ // is stopped before the lease goes, so the volume can detach, and said, so its owner fixes it.
135
+ const reaped = await tempVolume?.reap(held);
136
+ if (reaped?.ok) {
137
+ for (const leftover of reaped.value) {
138
+ appendStderr(`RAM temp volume: stopped ${leftover}, which the command left working in its lease\n`);
139
+ }
140
+ }
141
+ else if (reaped) {
142
+ appendStderr(`${describeRamTempError(reaped.error)}\n`);
143
+ }
144
+ const released = await tempVolume?.release(held);
145
+ if (released && !released.ok) {
146
+ appendStderr(`${describeRamTempError(released.error)}\n`);
147
+ }
148
+ };
149
+ // The wait is one span line, as cowshed's own spans read. With no gateway listening, as on any
150
+ // host without cowshed, the command runs exactly as asked and nothing is said.
151
+ const cores = availableParallelism();
152
+ const demand = runnerDemand(requested, options.parallelism, cores, process.env, options.env);
153
+ const asked = Date.now();
154
+ // A run a parent's CPU pool lent tokens to takes them there. Only an absent pool leaves the host budget to ask.
155
+ // A pool that answered and refused still holds tokens of its own: asking the host then would hold them while waiting
156
+ // on another budget, so the run fails instead.
157
+ let grant = null;
158
+ let parentRefusal = null;
159
+ if (inherited !== null) {
160
+ const fromParent = await takeParentCpuTokens(inherited, demand.want, cores);
161
+ if (fromParent.granted) {
162
+ grant = fromParent;
163
+ }
164
+ else if (fromParent.cause === 'absent') {
165
+ appendStderr(`cowshed: cpu-tokens parent pool refused: ${fromParent.reason}; asking the host budget\n`);
166
+ }
167
+ else {
168
+ parentRefusal = fromParent.reason;
169
+ }
170
+ }
171
+ let unbudgeted = null;
172
+ if (grant === null && parentRefusal === null && budget !== null) {
173
+ const fromHost = await budget.take(demand.want, context.root, requested);
174
+ if (fromHost.granted) {
175
+ grant = fromHost;
176
+ }
177
+ else if (fromHost.cause !== 'absent') {
178
+ unbudgeted = fromHost.reason;
179
+ }
180
+ }
181
+ const waitedMs = Date.now() - asked;
182
+ let cpu;
183
+ let sized = { command: requested, env: {} };
184
+ if (grant !== null) {
185
+ sized = sizeRunner(demand, requested, grant.tokens);
186
+ cpu = { tokens: grant.tokens, want: demand.want, waitedMs, ...(grant.inherited ? { inherited: true } : {}) };
187
+ appendStderr(`cowshed: cpu-tokens wait done elapsed=${waitedMs}ms status=ok ${grant.inherited ? 'inherited=true ' : ''}tokens=${grant.tokens}/${demand.want} runner=${demand.kind}\n`);
188
+ }
189
+ else if (unbudgeted !== null) {
190
+ appendStderr(`cowshed: cpu-tokens wait done elapsed=${waitedMs}ms status=err runner=${demand.kind} runs unbudgeted: ${unbudgeted}\n`);
191
+ }
192
+ // The run's sub-pool and grant are given back exactly once, however the run ends. The sub-pool closes first, so no
193
+ // descendant still draws on tokens the grant has returned.
194
+ let pool = null;
195
+ let cpuReleased = null;
196
+ const releaseCpu = () => {
197
+ cpuReleased ??= (async () => {
198
+ try {
199
+ const outcome = await pool?.close();
200
+ if (outcome !== undefined && !outcome.ok) {
201
+ state.poolCloseError = outcome.error;
202
+ appendStderr(`cowshed: cpu pool did not close cleanly: ${outcome.error}\n`);
203
+ }
204
+ }
205
+ finally {
206
+ grant?.release();
207
+ }
208
+ })();
209
+ return cpuReleased;
210
+ };
211
+ // Records the run's verdict when it has a task to record as. An `expiry` is set only when a bound ended the run.
212
+ // An `operational` run failed on its CPU budget (a lost grant, or a sub-pool that did not close): an ordinary
213
+ // failure, whatever bound also fired, so it is never judged as a bound.
214
+ const recordRun = async (exitCode, expiry, elapsedMs, operational) => {
215
+ if (context.record === undefined) {
216
+ return;
217
+ }
218
+ const { task, directory: recordDirectory } = context.record;
219
+ const report = await readJunitReport(recordDirectory);
220
+ const verdict = operational
221
+ ? judgeOperationalFailure(exitCode, report)
222
+ : judgeRun({ exitCode, elapsedMs, expiry, report });
223
+ if (verdict.outcome === 'bound' && verdict.bound === 'test') {
224
+ appendStderr(`Every failing test failed only on its per-test timeout: ${verdict.tests.join(', ')}\n`);
225
+ }
226
+ await writeRecord(recordDirectory, { task: task.id, hash: task.hash, verdict, ...(cpu ? { cpu } : {}) });
227
+ };
228
+ // A run refused before its command starts says why, gives back its grant and RAM, and records the failure.
229
+ const refuse = async (message) => {
230
+ appendStderr(message);
231
+ await releaseCpu();
232
+ await releaseLease();
233
+ await recordRun(1, null, Date.now() - asked, false);
234
+ return { success: false, terminalOutput: outputChunks.join('') };
235
+ };
236
+ if (parentRefusal !== null) {
237
+ return refuse(`cowshed: cpu-tokens parent pool refused: ${parentRefusal}; refusing to run the command without asking the host budget\n`);
238
+ }
239
+ // A granted run serves a sub-pool of its own grant, so its descendants draw from that and never from above it.
240
+ // A pool that cannot be set up refuses the run: no command runs unbudgeted.
241
+ if (grant !== null) {
242
+ const created = await createCpuPool(grant.tokens, grant.closed);
243
+ if (!created.ok) {
244
+ return refuse(`cowshed: cpu pool unavailable: ${created.error}; refusing to run the command\n`);
245
+ }
246
+ pool = created.pool;
247
+ }
248
+ const command = directory === null
249
+ ? withProgressReport(sized.command)
250
+ : await withJunitReport(withProgressReport(sized.command), directory);
251
+ // A runner bounded-exec cannot see in the command (a script that spawns `bun test`) writes its JUnit report to this
252
+ // path when it is set. The grant's sizing outranks the target's own environment: the runner may use what it was
253
+ // granted, not what it was configured with. The sub-pool's environment comes last: a descendant reads only the pool
254
+ // this run serves.
255
+ const env = {
256
+ ...(directory === null ? {} : { [JUNIT_REPORT_ENV]: join(directory, JUNIT_FILE) }),
257
+ ...targetEnv(options.env, appendStderr),
258
+ ...sized.env,
259
+ ...(pool?.environment ?? {}),
260
+ };
261
+ const startedAt = Date.now();
262
+ let child;
263
+ try {
264
+ child = spawn(command, [], {
265
+ cwd,
266
+ env: mergeEnv(lease, env),
267
+ shell: true,
268
+ detached: process.platform !== 'win32',
269
+ windowsHide: true,
270
+ });
271
+ }
272
+ catch (error) {
273
+ // Nothing started, so nothing runs under the grant: it goes back here, with the RAM lease.
274
+ return refuse(`cowshed: could not start the command: ${error instanceof Error ? error.message : String(error)}\n`);
275
+ }
276
+ // Progress is a finished test, not output: a runner's SLOW warnings and a hung test's heartbeat keep printing.
277
+ const progress = createRunProgress();
39
278
  let idleTimer;
279
+ let captureTimer;
280
+ let idleCapture;
281
+ const cancelCapture = () => {
282
+ clearTimeout(captureTimer);
283
+ idleCapture?.controller.abort();
284
+ };
40
285
  const armIdleTimer = () => {
41
286
  if (idleTimeoutMs === undefined || state.settled || state.expiry !== null) {
42
287
  return;
43
288
  }
44
289
  clearTimeout(idleTimer);
45
- idleTimer = setTimeout(() => expire({ kind: 'idle', limitMs: idleTimeoutMs }), idleTimeoutMs);
290
+ cancelCapture();
291
+ const capture = {
292
+ expiresAtMs: Date.now() + idleTimeoutMs,
293
+ controller: new AbortController(),
294
+ result: { kind: 'pending', startedAtMs: null },
295
+ };
296
+ idleCapture = capture;
297
+ captureTimer = setTimeout(() => {
298
+ if (state.settled || state.expiry !== null || idleCapture !== capture)
299
+ return;
300
+ const pid = child.pid;
301
+ if (pid === undefined) {
302
+ idleCapture.result = { kind: 'failed', detail: 'runner PID unavailable; no holder conclusion' };
303
+ return;
304
+ }
305
+ idleCapture.result = { kind: 'pending', startedAtMs: Date.now() };
306
+ void Promise.resolve()
307
+ .then(() => captureDiagnostics({
308
+ pid,
309
+ pgid: process.platform === 'win32' ? null : pid,
310
+ expiresAtMs: capture.expiresAtMs,
311
+ deadlineMs: Math.max(0, Math.min(HANG_CAPTURE_MS, capture.expiresAtMs - Date.now())),
312
+ signal: capture.controller.signal,
313
+ }))
314
+ .then((snapshot) => {
315
+ if (idleCapture === capture)
316
+ idleCapture.result = { kind: 'captured', snapshot };
317
+ }, (error) => {
318
+ if (idleCapture === capture) {
319
+ idleCapture.result = {
320
+ kind: 'failed',
321
+ detail: error instanceof Error ? error.message : String(error),
322
+ };
323
+ }
324
+ });
325
+ }, Math.max(0, idleTimeoutMs - HANG_CAPTURE_MS));
326
+ idleTimer = setTimeout(() => expire({ kind: 'idle', limitMs: idleTimeoutMs, tests: progress.inFlight() }), idleTimeoutMs);
46
327
  };
47
- // Only the CHILD's own output counts as progress. The diagnostics below go
328
+ // Only the CHILD's own output is read for progress. The diagnostics below go
48
329
  // through appendStderr as well, and re-arming on those would let a teardown
49
330
  // message extend the very bound that just fired.
50
331
  child.stdout?.on('data', (chunk) => {
51
- armIdleTimer();
52
- appendStdout(chunk);
332
+ const text = chunk.toString();
333
+ if (progress.scan('stdout', text))
334
+ armIdleTimer();
335
+ appendStdout(text);
53
336
  });
54
337
  child.stderr?.on('data', (chunk) => {
55
- armIdleTimer();
56
- appendStderr(chunk);
338
+ const text = chunk.toString();
339
+ if (progress.scan('stderr', text))
340
+ armIdleTimer();
341
+ appendStderr(text);
57
342
  });
58
343
  const killChildTree = async (force) => {
59
344
  const pid = child.pid;
@@ -67,7 +352,11 @@ export async function runBoundedExec(options, context, killer) {
67
352
  };
68
353
  const onTerminationSignal = (signal) => {
69
354
  removeSignalHandlers();
70
- void killChildTree(false).finally(() => process.kill(process.pid, signal));
355
+ cancelCapture();
356
+ void killChildTree(false)
357
+ .finally(releaseCpu)
358
+ .finally(releaseLease)
359
+ .finally(() => process.kill(process.pid, signal));
71
360
  };
72
361
  const onSigint = () => onTerminationSignal('SIGINT');
73
362
  const onSigterm = () => onTerminationSignal('SIGTERM');
@@ -101,8 +390,9 @@ export async function runBoundedExec(options, context, killer) {
101
390
  // the report always names which bound did it. A bare "timed out" would leave
102
391
  // the reader unable to tell a wedged toolchain from a loaded machine — the
103
392
  // two have opposite fixes.
393
+ let termination = Promise.resolve();
104
394
  const expire = (bound) => {
105
- if (state.settled || state.expiry !== null) {
395
+ if (state.settled || state.expiry !== null || state.grantLost) {
106
396
  return;
107
397
  }
108
398
  state.expiry = bound;
@@ -110,30 +400,81 @@ export async function runBoundedExec(options, context, killer) {
110
400
  clearTimeout(idleTimer);
111
401
  const elapsedMs = Date.now() - startedAt;
112
402
  appendStderr(bound.kind === 'idle'
113
- ? `\nCommand made no progress: no output for ${bound.limitMs}ms (idleTimeoutMs=${bound.limitMs}) after ${elapsedMs}ms of runtime (cwd=${cwd}): ${command}\n`
403
+ ? `\nCommand made no progress: no test finished for ${bound.limitMs}ms (idleTimeoutMs=${bound.limitMs}) after ${elapsedMs}ms of runtime; ${bound.tests.length > 0
404
+ ? `still running: ${bound.tests.join(', ')}`
405
+ : 'the runner named no test still running'} (cwd=${cwd}): ${command}\n`
114
406
  : `\nCommand timed out after ${elapsedMs}ms (timeoutMs=${bound.limitMs}, cwd=${cwd}): ${command}\n`);
115
- void (async () => {
116
- await ignoreKillError(killChildTree(false));
117
- if (!state.settled && killAfterMs > 0) {
118
- await delay(killAfterMs);
407
+ if (bound.kind === 'idle') {
408
+ const observed = idleCapture?.result;
409
+ appendStderr(`Hang diagnostics: runner pid=${child.pid ?? 'unavailable'} pgid=${process.platform === 'win32' ? 'unavailable' : (child.pid ?? 'unavailable')} expiryAtMs=${Date.now()} scheduledExpiryAtMs=${idleCapture?.expiresAtMs ?? 'unavailable'}\n`);
410
+ if (observed?.kind === 'captured') {
411
+ appendStderr(`Hang diagnostics: outcome=${observed.snapshot.outcome} capturedAtMs=${observed.snapshot.capturedAtMs} actualExpiryOffsetMs=${Date.now() - observed.snapshot.capturedAtMs}\n${observed.snapshot.text}`);
412
+ }
413
+ else {
414
+ appendStderr(`Hang diagnostics: outcome=incomplete; ${observed?.kind === 'failed'
415
+ ? `capture failed: ${observed.detail}`
416
+ : `capture incomplete at expiry (startedAtMs=${observed?.startedAtMs ?? 'not started'})`}; no empty-holder conclusion\n`);
119
417
  }
120
- if (!state.settled) {
418
+ }
419
+ cancelCapture();
420
+ termination = terminate();
421
+ };
422
+ // A timed-out run ends with its whole process group, not with its leader.
423
+ // The graceful signal reaches every member that exists when it is sent, but
424
+ // a member that ignores SIGTERM, or one forked while the signal goes out
425
+ // (macOS does not hold a fork back for a group signal in flight), outlives
426
+ // a leader that exits on it. The grace ends when the leader exits or
427
+ // killAfterMs passes, whichever comes first; whatever is left then is killed.
428
+ const terminate = async () => {
429
+ await ignoreKillError(killChildTree(false));
430
+ if (!state.settled && killAfterMs > 0) {
431
+ await settledWithin(exitPromise, killAfterMs);
432
+ }
433
+ if (state.settled) {
434
+ if (child.pid !== undefined && processGroupHasMembers(child.pid)) {
121
435
  state.forceKillNeeded = true;
122
- appendStderr(`Force-killing timed out command after killAfterMs=${killAfterMs}: ${command}\n`);
436
+ appendStderr(`Force-killing what the ${state.expiry === null ? 'command' : 'timed-out command'} left in its process group: ${command}\n`);
123
437
  await ignoreKillError(killChildTree(true));
124
- await delay(REAP_AFTER_FORCE_KILL_MS);
125
438
  }
126
- if (!state.settled) {
127
- resolveExit({ code: 1, signal: 'SIGKILL' });
128
- }
129
- })();
439
+ return;
440
+ }
441
+ state.forceKillNeeded = true;
442
+ appendStderr(`Force-killing ${state.expiry === null ? 'command' : 'timed out command'} after killAfterMs=${killAfterMs}: ${command}\n`);
443
+ await ignoreKillError(killChildTree(true));
444
+ await settledWithin(exitPromise, REAP_AFTER_FORCE_KILL_MS);
445
+ if (!state.settled) {
446
+ resolveExit({ code: 1, signal: 'SIGKILL' });
447
+ }
130
448
  };
131
- const totalTimer = setTimeout(() => expire({ kind: 'total', limitMs: timeoutMs }), timeoutMs);
449
+ const totalTimer = timeoutMs === undefined ? undefined : setTimeout(() => expire({ kind: 'total', limitMs: timeoutMs }), timeoutMs);
132
450
  armIdleTimer();
451
+ // A borrowed grant ends with the ancestor that lent it, and the command runs on tokens that ancestor takes back
452
+ // then. Stopping the command is a lost grant: a failure, not a bound. Our own release after the command settled
453
+ // is not a loss; `cpuReleased` marks it.
454
+ const onGrantLost = () => {
455
+ if (state.settled || state.grantLost || cpuReleased !== null) {
456
+ return;
457
+ }
458
+ state.grantLost = true;
459
+ cancelCapture();
460
+ appendStderr(`cowshed: cpu-tokens grant lost: its parent pool closed while the command ran; stopping it: ${command}\n`);
461
+ // A bound may already be terminating the still-running command. The loss remains an ordinary
462
+ // operational failure, but that existing termination owns the signals and grace.
463
+ if (state.expiry === null)
464
+ termination = terminate();
465
+ };
466
+ if (grant?.inherited) {
467
+ void grant.closed.then(onGrantLost);
468
+ }
133
469
  const { code: exitCode, signal: exitSignal } = await exitPromise;
470
+ await termination;
134
471
  clearTimeout(totalTimer);
135
472
  clearTimeout(idleTimer);
473
+ cancelCapture();
136
474
  removeSignalHandlers();
475
+ // The runner is gone. Its sub-pool closes first, so no descendant still draws on tokens the grant returns;
476
+ // then the grant goes back to its parent or the host.
477
+ await releaseCpu();
137
478
  const code = exitCode ?? signalToExitCode(exitSignal);
138
479
  if (code !== 0 && state.expiry === null) {
139
480
  appendStderr(`Command exited with status ${code}: ${command}\n`);
@@ -141,8 +482,18 @@ export async function runBoundedExec(options, context, killer) {
141
482
  if (state.expiry !== null && !state.forceKillNeeded) {
142
483
  appendStderr(`Timed out command exited after graceful termination: ${command}\n`);
143
484
  }
485
+ // A test that dies of ENOSPC rarely says where it was writing; the volume can.
486
+ const full = code !== 0 && lease !== null ? await tempVolume?.fullness(lease) : null;
487
+ if (full) {
488
+ appendStderr(`${describeRamTempError(full)}\n`);
489
+ }
490
+ await releaseLease();
491
+ // A run whose grant was lost, or whose sub-pool did not close cleanly, fails even when the command exited 0, and
492
+ // whatever bound also fired: its verdict is an ordinary failure, never a bound.
493
+ const operationalFailure = state.grantLost || state.poolCloseError !== null;
494
+ await recordRun(code, state.expiry, Date.now() - startedAt, operationalFailure);
144
495
  return {
145
- success: state.expiry === null && code === 0,
496
+ success: !operationalFailure && state.expiry === null && code === 0,
146
497
  terminalOutput: outputChunks.join(''),
147
498
  };
148
499
  }
@@ -155,23 +506,36 @@ export function createProcessTreeKiller() {
155
506
  }
156
507
  function killProcessGroup(pid, signal) {
157
508
  try {
158
- // Send signal to the entire process group (negative PID).
159
- // The executor spawns with detached: true on POSIX, which creates a
160
- // dedicated process group. Signaling -pid is atomic and catches all
161
- // descendants, including grandchildren that tree-walk libraries miss
162
- // when parents die and children get reparented to init.
163
- process.kill(-pid, signal);
509
+ // On POSIX the executor spawns with detached: true, so the child leads a
510
+ // process group of its own and -pid signals all of it at once, including
511
+ // grandchildren that tree-walk libraries miss when parents die and
512
+ // children get reparented to init. Only the group is named: the leader may
513
+ // already be reaped, and its pid reused by an unrelated process, while the
514
+ // group lives on in its other members. Windows has no process groups.
515
+ process.kill(process.platform === 'win32' ? pid : -pid, signal);
164
516
  }
165
517
  catch {
166
- // ESRCH: process group already exited, or this pid is not a group leader.
518
+ // ESRCH: the group (or the Windows process) has already exited.
519
+ }
520
+ return Promise.resolve();
521
+ }
522
+ /**
523
+ * Whether any process is left in the group `pgid` leads. A group outlives its
524
+ * reaped leader for as long as one member does, and no process can take its id
525
+ * meanwhile. Windows has no process groups, so there is nothing left to name.
526
+ */
527
+ function processGroupHasMembers(pgid) {
528
+ if (process.platform === 'win32') {
529
+ return false;
167
530
  }
168
531
  try {
169
- process.kill(pid, signal);
532
+ process.kill(-pgid, 0);
533
+ return true;
170
534
  }
171
- catch {
172
- // Already reaped.
535
+ catch (error) {
536
+ // EPERM: a member exists but runs as another user.
537
+ return error instanceof Error && 'code' in error && error.code === 'EPERM';
173
538
  }
174
- return Promise.resolve();
175
539
  }
176
540
  function resolveCwd(cwd, root) {
177
541
  if (!cwd) {
@@ -192,8 +556,32 @@ function buildCommand(options) {
192
556
  }
193
557
  return parts.join(' ');
194
558
  }
195
- function mergeEnv(env) {
196
- return env ? { ...process.env, ...env } : process.env;
559
+ /** The lease's directory is the command's TMPDIR unless the target names one itself. */
560
+ function mergeEnv(lease, env) {
561
+ const inherited = withoutBudgetEnv(process.env);
562
+ return lease === null ? { ...inherited, ...env } : { ...inherited, TMPDIR: lease.directory, ...env };
563
+ }
564
+ /** The process's environment without the CPU budget's variables: a pool and its count only a grant may give. */
565
+ function withoutBudgetEnv(env) {
566
+ const kept = { ...env };
567
+ for (const name of BUDGET_ENV) {
568
+ delete kept[name];
569
+ }
570
+ return kept;
571
+ }
572
+ /**
573
+ * The target's environment without the CPU budget's variables: the budget sets those for the command it grants,
574
+ * so a value the target names is ignored, and `say` tells the reader so.
575
+ */
576
+ function targetEnv(env, say) {
577
+ const kept = { ...env };
578
+ for (const name of BUDGET_ENV) {
579
+ if (name in kept) {
580
+ say(`cowshed: ${name} in the target's env is ignored: the CPU budget sets it\n`);
581
+ delete kept[name];
582
+ }
583
+ }
584
+ return kept;
197
585
  }
198
586
  function signalToExitCode(signal) {
199
587
  if (!signal) {
@@ -209,6 +597,13 @@ async function ignoreKillError(promise) {
209
597
  // The process tree may have already exited between timeout and kill.
210
598
  }
211
599
  }
212
- function delay(ms) {
213
- return new Promise((resolve) => setTimeout(resolve, ms));
600
+ /** Resolves when `promise` settles or `ms` passes, whichever is first, leaving no timer behind. */
601
+ async function settledWithin(promise, ms) {
602
+ let timer;
603
+ try {
604
+ await Promise.race([promise, new Promise((resolve) => (timer = setTimeout(resolve, ms)))]);
605
+ }
606
+ finally {
607
+ clearTimeout(timer);
608
+ }
214
609
  }
@@ -0,0 +1,52 @@
1
+ /**
2
+ * The client half of a lease held on the cowshed gateway's control socket: the host
3
+ * disk-lifecycle lease (`disk-lease.ts`) and the host CPU budget (`cpu-tokens.ts`) speak the same
4
+ * protocol (specs/cowshed/05_gateway.md, "Disk-lifecycle lease" and "Host CPU budget").
5
+ *
6
+ * The client writes one request line and keeps the connection open without half-closing it. The
7
+ * gateway answers `queued` at once and `granted` when the lease is the client's, and the lease
8
+ * lasts until the client closes the connection, so a client that dies holding one releases it with
9
+ * its socket. A lease never decides whether a command runs: without a grant, it runs without one
10
+ * and the caller says why.
11
+ */
12
+ /** The cowshed gateway's control socket, at the store root every peer agrees on. */
13
+ export declare const GATEWAY_SOCKET = "/private/cowshed/store/gateway.sock";
14
+ export interface LeaseBounds {
15
+ /** For the `queued` answer a lease-scheduling gateway sends at once. */
16
+ ackMs: number;
17
+ /** For any answer once the request is half-closed. */
18
+ afterCloseMs: number;
19
+ /** For the grant, once queued; `null` waits for as long as the gateway keeps it queued. */
20
+ grantMs: number | null;
21
+ }
22
+ /**
23
+ * Why a command runs without a lease: no gateway listens, the gateway predates this kind of lease,
24
+ * the gateway knows the lease but predates a field the request names (it refuses the field at
25
+ * once), or any other refusal, silence or breakage.
26
+ */
27
+ export type UnleasedCause = 'absent' | 'predates' | 'predates-field' | 'other';
28
+ /**
29
+ * A granted lease with its `granted` answer, held until released, or why there is none. `closed`
30
+ * resolves when the connection really closes: after `release`, when the gateway ends it, or when
31
+ * the client process dies.
32
+ */
33
+ export type HeldLease = {
34
+ granted: true;
35
+ answer: Readonly<Record<string, unknown>>;
36
+ release: () => void;
37
+ closed: Promise<void>;
38
+ } | {
39
+ granted: false;
40
+ cause: UnleasedCause;
41
+ reason: string;
42
+ code?: string;
43
+ };
44
+ /**
45
+ * Ask the gateway at `socket` for the lease `request` names, `what` in its messages ("disk
46
+ * leases", "the CPU budget"). A gateway from before one-line requests says nothing until it reads
47
+ * EOF, so a request not queued within `ackMs` is half-closed and that gateway's refusal read; a
48
+ * gateway that reads the line but does not know the operation refuses it at once. Both predate the
49
+ * lease.
50
+ */
51
+ export declare function holdLease(socket: string, request: Readonly<Record<string, unknown>>, what: string, bounds: LeaseBounds, onQueued?: () => void): Promise<HeldLease>;
52
+ //# sourceMappingURL=gateway-lease.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"gateway-lease.d.ts","sourceRoot":"","sources":["../../../src/executors/bounded-exec/gateway-lease.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;GAUG;AAEH,oFAAoF;AACpF,eAAO,MAAM,cAAc,wCAAwC,CAAC;AAEpE,MAAM,WAAW,WAAW;IAC1B,wEAAwE;IACxE,KAAK,EAAE,MAAM,CAAC;IACd,sDAAsD;IACtD,YAAY,EAAE,MAAM,CAAC;IACrB,2FAA2F;IAC3F,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;CACxB;AAED;;;;GAIG;AACH,MAAM,MAAM,aAAa,GAAG,QAAQ,GAAG,UAAU,GAAG,gBAAgB,GAAG,OAAO,CAAC;AAE/E;;;;GAIG;AACH,MAAM,MAAM,SAAS,GACjB;IAAE,OAAO,EAAE,IAAI,CAAC;IAAC,MAAM,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;IAAC,OAAO,EAAE,MAAM,IAAI,CAAC;IAAC,MAAM,EAAE,OAAO,CAAC,IAAI,CAAC,CAAA;CAAE,GACxG;IAAE,OAAO,EAAE,KAAK,CAAC;IAAC,KAAK,EAAE,aAAa,CAAC;IAAC,MAAM,EAAE,MAAM,CAAC;IAAC,IAAI,CAAC,EAAE,MAAM,CAAA;CAAE,CAAC;AAE5E;;;;;;GAMG;AACH,wBAAgB,SAAS,CACvB,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,EAC1C,IAAI,EAAE,MAAM,EACZ,MAAM,EAAE,WAAW,EACnB,QAAQ,CAAC,EAAE,MAAM,IAAI,GACpB,OAAO,CAAC,SAAS,CAAC,CAwHpB"}