@smoothbricks/nx-plugin 0.4.25 → 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.
- package/README.md +1078 -98
- package/dist/bin/smoo-nx-bound-failures.d.ts +3 -0
- package/dist/bin/smoo-nx-bound-failures.d.ts.map +1 -0
- package/dist/bin/smoo-nx-bound-failures.js +31 -0
- package/dist/bin/smoo-nx-cargo-hash.js +28 -3
- package/dist/bin/smoo-nx-exec.js +16 -85
- package/dist/bin/smoo-nx-failures.d.ts +3 -0
- package/dist/bin/smoo-nx-failures.d.ts.map +1 -0
- package/dist/bin/smoo-nx-failures.js +3 -0
- package/dist/bin/smoo-nx-nextest-extract.js +3 -2
- package/dist/bin/smoo-nx-test-walks.d.ts +3 -0
- package/dist/bin/smoo-nx-test-walks.d.ts.map +1 -0
- package/dist/bin/smoo-nx-test-walks.js +34 -0
- package/dist/bounded-test-policy.d.ts +7 -1
- package/dist/bounded-test-policy.d.ts.map +1 -1
- package/dist/bounded-test-policy.js +101 -40
- package/dist/build-state-output-policy.d.ts +25 -0
- package/dist/build-state-output-policy.d.ts.map +1 -0
- package/dist/build-state-output-policy.js +64 -0
- package/dist/cargo-closure-input.d.ts +16 -17
- package/dist/cargo-closure-input.d.ts.map +1 -1
- package/dist/cargo-closure-input.js +37 -44
- package/dist/cargo-dev-profile.d.ts +37 -0
- package/dist/cargo-dev-profile.d.ts.map +1 -0
- package/dist/cargo-dev-profile.js +108 -0
- package/dist/cargo-source-hash.d.ts +50 -8
- package/dist/cargo-source-hash.d.ts.map +1 -1
- package/dist/cargo-source-hash.js +691 -71
- package/dist/cargo-toolchain-policy.d.ts +4 -0
- package/dist/cargo-toolchain-policy.d.ts.map +1 -1
- package/dist/cargo-toolchain-policy.js +5 -0
- package/dist/cargo-workspace.d.ts +108 -12
- package/dist/cargo-workspace.d.ts.map +1 -1
- package/dist/cargo-workspace.js +299 -18
- package/dist/cross-check-policy.d.ts +21 -3
- package/dist/cross-check-policy.d.ts.map +1 -1
- package/dist/cross-check-policy.js +24 -6
- package/dist/disk-command-policy.d.ts +7 -0
- package/dist/disk-command-policy.d.ts.map +1 -0
- package/dist/disk-command-policy.js +98 -0
- package/dist/ensure-built-receipt.d.ts +25 -0
- package/dist/ensure-built-receipt.d.ts.map +1 -0
- package/dist/ensure-built-receipt.js +261 -0
- package/dist/ensure-built-witness.d.ts +57 -0
- package/dist/ensure-built-witness.d.ts.map +1 -0
- package/dist/ensure-built-witness.js +412 -0
- package/dist/ensure-built.d.ts +213 -12
- package/dist/ensure-built.d.ts.map +1 -1
- package/dist/ensure-built.js +624 -177
- package/dist/eslint-file-set.d.ts +7 -0
- package/dist/eslint-file-set.d.ts.map +1 -0
- package/dist/eslint-file-set.js +12 -0
- package/dist/executors/bounded-exec/cpu-pool.d.ts +63 -0
- package/dist/executors/bounded-exec/cpu-pool.d.ts.map +1 -0
- package/dist/executors/bounded-exec/cpu-pool.js +379 -0
- package/dist/executors/bounded-exec/cpu-tokens.d.ts +82 -0
- package/dist/executors/bounded-exec/cpu-tokens.d.ts.map +1 -0
- package/dist/executors/bounded-exec/cpu-tokens.js +114 -0
- package/dist/executors/bounded-exec/disk-lease.d.ts +61 -0
- package/dist/executors/bounded-exec/disk-lease.d.ts.map +1 -0
- package/dist/executors/bounded-exec/disk-lease.js +91 -0
- package/dist/executors/bounded-exec/executor.d.ts +45 -2
- package/dist/executors/bounded-exec/executor.d.ts.map +1 -1
- package/dist/executors/bounded-exec/executor.js +447 -52
- package/dist/executors/bounded-exec/gateway-lease.d.ts +52 -0
- package/dist/executors/bounded-exec/gateway-lease.d.ts.map +1 -0
- package/dist/executors/bounded-exec/gateway-lease.js +143 -0
- package/dist/executors/bounded-exec/hang-diagnostics.d.ts +35 -0
- package/dist/executors/bounded-exec/hang-diagnostics.d.ts.map +1 -0
- package/dist/executors/bounded-exec/hang-diagnostics.js +231 -0
- package/dist/executors/bounded-exec/progress.d.ts +35 -0
- package/dist/executors/bounded-exec/progress.d.ts.map +1 -0
- package/dist/executors/bounded-exec/progress.js +79 -0
- package/dist/executors/bounded-exec/ram-temp-fixtures.d.ts +3 -0
- package/dist/executors/bounded-exec/ram-temp-fixtures.d.ts.map +1 -0
- package/dist/executors/bounded-exec/ram-temp-fixtures.js +12 -0
- package/dist/executors/bounded-exec/ram-temp-held.d.ts +91 -0
- package/dist/executors/bounded-exec/ram-temp-held.d.ts.map +1 -0
- package/dist/executors/bounded-exec/ram-temp-held.js +152 -0
- package/dist/executors/bounded-exec/ram-temp.d.ts +223 -0
- package/dist/executors/bounded-exec/ram-temp.d.ts.map +1 -0
- package/dist/executors/bounded-exec/ram-temp.js +1528 -0
- package/dist/executors/bounded-exec/schema.d.ts +29 -11
- package/dist/executors/bounded-exec/schema.d.ts.map +1 -1
- package/dist/executors/bounded-exec/verdict.d.ts +162 -0
- package/dist/executors/bounded-exec/verdict.d.ts.map +1 -0
- package/dist/executors/bounded-exec/verdict.js +318 -0
- package/dist/executors/shard-timings/executor.d.ts +14 -0
- package/dist/executors/shard-timings/executor.d.ts.map +1 -0
- package/dist/executors/shard-timings/executor.js +25 -0
- package/dist/executors/shard-timings/schema.d.ts +4 -0
- package/dist/executors/shard-timings/schema.d.ts.map +1 -0
- package/dist/executors/shard-timings/schema.js +1 -0
- package/dist/failure-summary.d.ts +5 -0
- package/dist/failure-summary.d.ts.map +1 -0
- package/dist/failure-summary.js +118 -0
- package/dist/generators/create-package/generator.d.ts.map +1 -1
- package/dist/generators/create-package/generator.js +6 -2
- package/dist/index.d.ts +14 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +435 -230
- package/dist/managed-assets.d.ts +6 -0
- package/dist/managed-assets.d.ts.map +1 -1
- package/dist/managed-files/ci-workflow.d.ts +13 -0
- package/dist/managed-files/ci-workflow.d.ts.map +1 -1
- package/dist/managed-files/ci-workflow.js +44 -13
- package/dist/managed-files/files.d.ts +7 -1
- package/dist/managed-files/files.d.ts.map +1 -1
- package/dist/managed-files/files.js +62 -8
- package/dist/managed-files/json-edit.d.ts +21 -0
- package/dist/managed-files/json-edit.d.ts.map +1 -0
- package/dist/managed-files/json-edit.js +295 -0
- package/dist/managed-files/managed-format.d.ts +33 -1
- package/dist/managed-files/managed-format.d.ts.map +1 -1
- package/dist/managed-files/managed-format.js +82 -2
- package/dist/managed-files/pr-preview-cleanup-workflow.d.ts +7 -0
- package/dist/managed-files/pr-preview-cleanup-workflow.d.ts.map +1 -1
- package/dist/managed-files/pr-preview-cleanup-workflow.js +68 -4
- package/dist/managed-files/publish-workflow.d.ts.map +1 -1
- package/dist/managed-files/publish-workflow.js +231 -48
- package/dist/managed-files/tree.d.ts +5 -0
- package/dist/managed-files/tree.d.ts.map +1 -1
- package/dist/managed-files/tree.js +5 -3
- package/dist/manifest-hash.js +3 -2
- package/dist/nextest-code-bound.d.ts +62 -0
- package/dist/nextest-code-bound.d.ts.map +1 -0
- package/dist/nextest-code-bound.js +154 -0
- package/dist/nextest-extraction.d.ts +24 -8
- package/dist/nextest-extraction.d.ts.map +1 -1
- package/dist/nextest-extraction.js +42 -7
- package/dist/package-target-policy.d.ts.map +1 -1
- package/dist/package-target-policy.js +3 -5
- package/dist/release-config-policy.d.ts.map +1 -1
- package/dist/release-config-policy.js +3 -5
- package/dist/runtime-input.d.ts +14 -0
- package/dist/runtime-input.d.ts.map +1 -0
- package/dist/runtime-input.js +15 -0
- package/dist/rust-tokens.d.ts +11 -0
- package/dist/rust-tokens.d.ts.map +1 -0
- package/dist/rust-tokens.js +81 -0
- package/dist/task-results.d.ts +6 -0
- package/dist/task-results.d.ts.map +1 -0
- package/dist/task-results.js +61 -0
- package/dist/test-program.d.ts +54 -0
- package/dist/test-program.d.ts.map +1 -0
- package/dist/test-program.js +292 -0
- package/dist/test-timings.d.ts +71 -0
- package/dist/test-timings.d.ts.map +1 -0
- package/dist/test-timings.js +393 -0
- package/dist/test-walk-policy.d.ts +103 -0
- package/dist/test-walk-policy.d.ts.map +1 -0
- package/dist/test-walk-policy.js +657 -0
- package/dist/testing.d.ts +177 -0
- package/dist/testing.d.ts.map +1 -0
- package/dist/testing.js +824 -0
- package/dist/type-guards.d.ts +3 -0
- package/dist/type-guards.d.ts.map +1 -0
- package/dist/type-guards.js +4 -0
- package/dist/typecheck-test-policy.d.ts +8 -5
- package/dist/typecheck-test-policy.d.ts.map +1 -1
- package/dist/typecheck-test-policy.js +113 -100
- package/dist/workspace-config-policy.d.ts +8 -0
- package/dist/workspace-config-policy.d.ts.map +1 -1
- package/dist/workspace-config-policy.js +175 -63
- package/dist/workspace-index.d.ts +12 -0
- package/dist/workspace-index.d.ts.map +1 -0
- package/dist/workspace-index.js +17 -0
- package/dist/workspace-manifest.d.ts +37 -1
- package/dist/workspace-manifest.d.ts.map +1 -1
- package/executors.json +5 -0
- package/loader.js +13 -11
- package/managed/raw/envrc +33 -62
- package/managed/raw/git-format-staged.yml +30 -35
- package/managed/raw/patches/nx@23.2.1.patch +3484 -0
- package/managed/raw/tooling/direnv/apple-developer.sh +83 -0
- package/managed/raw/tooling/direnv/developer-links.ts +607 -17
- package/managed/raw/tooling/direnv/devenv.smoo.nix +314 -92
- package/managed/raw/tooling/direnv/github-actions-bootstrap.sh +20 -11
- package/managed/raw/tooling/direnv/linked-snapshots.ts +845 -0
- package/managed/raw/tooling/direnv/nx-socket-dir.sh +82 -0
- package/managed/raw/tooling/direnv/repo-path +14 -2
- package/managed/raw/tooling/direnv/setup-environment.ts +1009 -118
- package/managed/raw/tooling/direnv/toolchain-stamp.ts +45 -3
- package/managed/raw/tooling/direnv/ttsc-env.sh +39 -0
- package/managed/raw/tooling/git-hooks/commit-msg.sh +5 -6
- package/managed/raw/tooling/git-hooks/pre-commit.sh +7 -6
- package/managed/raw/tooling/git-hooks/pre-push.sh +20 -15
- package/managed/templates/github/actions/cache-nix-devenv/action.yml +6 -6
- package/managed/templates/github/actions/cache-node-modules/action.yml +6 -4
- package/managed/templates/github/actions/cache-nx/action.yml +1 -1
- package/managed/templates/github/actions/cache-ttsc-plugins/action.yml +1 -1
- package/managed/templates/github/actions/forgejo-artifact.cjs +99 -0
- package/managed/templates/github/actions/forgejo-download-artifact/action.yml +49 -0
- package/managed/templates/github/actions/forgejo-download-artifact/index.cjs +7 -0
- package/managed/templates/github/actions/forgejo-upload-artifact/action.yml +65 -0
- package/managed/templates/github/actions/forgejo-upload-artifact/index.cjs +7 -0
- package/managed/templates/github/actions/setup-devenv/action.yml +21 -3
- package/managed/templates/github/workflows/managed-files.yml +1 -2
- package/nextest.toml +8 -75
- package/package.json +75 -13
- package/runtime-input.sh +81 -0
- package/src/__tests__/counted-cargo-process.ts +286 -0
- package/src/__tests__/counted-cargo.ts +730 -0
- package/src/__tests__/fixture-cargo-home.ts +36 -0
- package/src/__tests__/fixture-nx-env.ts +157 -2
- package/src/__tests__/progress-clock.ts +43 -0
- package/src/__tests__/runtime-input.ts +23 -0
- package/src/bin/smoo-nx-bound-failures.test.ts +127 -0
- package/src/bin/smoo-nx-bound-failures.ts +33 -0
- package/src/bin/smoo-nx-cargo-hash.ts +28 -3
- package/src/bin/smoo-nx-exec.ts +17 -86
- package/src/bin/smoo-nx-failures.ts +4 -0
- package/src/bin/smoo-nx-nextest-extract.ts +3 -2
- package/src/bin/smoo-nx-test-walks.ts +42 -0
- package/src/bounded-test-policy.test.ts +268 -22
- package/src/bounded-test-policy.ts +114 -42
- package/src/build-state-output-policy.test.ts +115 -0
- package/src/build-state-output-policy.ts +90 -0
- package/src/bun-bootstrap.test.ts +57 -3
- package/src/bun-file-packages.test.ts +47 -0
- package/src/cargo-closure-input.test.ts +432 -62
- package/src/cargo-closure-input.ts +51 -57
- package/src/cargo-dev-profile.ts +137 -0
- package/src/cargo-resolve-cache.test.ts +1044 -0
- package/src/cargo-source-hash.test.ts +203 -93
- package/src/cargo-source-hash.ts +762 -75
- package/src/cargo-toolchain-policy.ts +6 -0
- package/src/cargo-workspace.test.ts +320 -0
- package/src/cargo-workspace.ts +344 -18
- package/src/cross-check-policy.test.ts +59 -1
- package/src/cross-check-policy.ts +25 -6
- package/src/disk-command-policy.test.ts +61 -0
- package/src/disk-command-policy.ts +107 -0
- package/src/ensure-built-receipt.test.ts +184 -0
- package/src/ensure-built-receipt.ts +263 -0
- package/src/ensure-built-witness.test.ts +344 -0
- package/src/ensure-built-witness.ts +252 -0
- package/src/ensure-built.test.ts +1043 -483
- package/src/ensure-built.ts +870 -197
- package/src/eslint-file-set.ts +9 -0
- package/src/executors/bounded-exec/cpu-pool.test.ts +454 -0
- package/src/executors/bounded-exec/cpu-pool.ts +417 -0
- package/src/executors/bounded-exec/cpu-tokens.test.ts +269 -0
- package/src/executors/bounded-exec/cpu-tokens.ts +177 -0
- package/src/executors/bounded-exec/disk-lease.test.ts +185 -0
- package/src/executors/bounded-exec/disk-lease.ts +139 -0
- package/src/executors/bounded-exec/executor-host.test.ts +778 -0
- package/src/executors/bounded-exec/executor.test.ts +1116 -63
- package/src/executors/bounded-exec/executor.ts +565 -67
- package/src/executors/bounded-exec/gateway-lease.ts +176 -0
- package/src/executors/bounded-exec/hang-diagnostics.test.ts +285 -0
- package/src/executors/bounded-exec/hang-diagnostics.ts +283 -0
- package/src/executors/bounded-exec/progress.test.ts +79 -0
- package/src/executors/bounded-exec/progress.ts +118 -0
- package/src/executors/bounded-exec/ram-temp-fixtures.ts +12 -0
- package/src/executors/bounded-exec/ram-temp-held.test.ts +264 -0
- package/src/executors/bounded-exec/ram-temp-held.ts +242 -0
- package/src/executors/bounded-exec/ram-temp-host.test.ts +605 -0
- package/src/executors/bounded-exec/ram-temp.test.ts +674 -0
- package/src/executors/bounded-exec/ram-temp.ts +1918 -0
- package/src/executors/bounded-exec/schema.json +11 -5
- package/src/executors/bounded-exec/schema.ts +27 -11
- package/src/executors/bounded-exec/verdict.test.ts +440 -0
- package/src/executors/bounded-exec/verdict.ts +433 -0
- package/src/executors/shard-timings/executor.ts +36 -0
- package/src/executors/shard-timings/schema.json +21 -0
- package/src/executors/shard-timings/schema.ts +4 -0
- package/src/failure-summary.ts +119 -0
- package/src/generators/bounded-test-targets/generator.test.ts +4 -4
- package/src/generators/bun-test-tracing/generator.test.ts +2 -2
- package/src/generators/create-package/generator.test.ts +6 -0
- package/src/generators/create-package/generator.ts +6 -2
- package/src/host-controller-wiring.test.ts +31 -0
- package/src/index.test.ts +1070 -360
- package/src/index.ts +524 -255
- package/src/linked-snapshots.nx.test.ts +172 -0
- package/src/managed-assets.ts +12 -0
- package/src/managed-files/ci-workflow.ts +54 -13
- package/src/managed-files/files.ts +65 -9
- package/src/managed-files/forgejo-artifact.test.ts +395 -0
- package/src/managed-files/generator.test.ts +60 -1
- package/src/managed-files/json-edit.test.ts +216 -0
- package/src/managed-files/json-edit.ts +290 -0
- package/src/managed-files/managed-format-json.test.ts +308 -0
- package/src/managed-files/managed-format.ts +90 -2
- package/src/managed-files/pr-preview-cleanup-workflow.ts +69 -3
- package/src/managed-files/publish-workflow.ts +285 -67
- package/src/managed-files/repo-path.test.ts +129 -0
- package/src/managed-files/trace-db-artifact.test.ts +7 -0
- package/src/managed-files/tree.ts +11 -3
- package/src/manifest-hash.ts +3 -2
- package/src/nextest-code-bound.test.ts +155 -0
- package/src/nextest-code-bound.ts +195 -0
- package/src/nextest-extraction.test.ts +37 -5
- package/src/nextest-extraction.ts +51 -9
- package/src/nextest-partitions.test.ts +495 -0
- package/src/nx-cache-bound.test.ts +109 -0
- package/src/nx-cache-restore-mtime.test.ts +170 -0
- package/src/nx-daemon-claim.test.ts +277 -0
- package/src/nx-daemon-idle-cpu.test.ts +81 -0
- package/src/nx-daemon-idle-policy.test.ts +51 -0
- package/src/nx-daemon-ignore-files.test.ts +210 -0
- package/src/nx-daemon-root-normalization.test.ts +105 -0
- package/src/nx-daemon-validity.test.ts +925 -0
- package/src/nx-dot-input-policy.test.ts +353 -0
- package/src/nx-failure-life-cycles.test.ts +207 -0
- package/src/nx-files-archive-racy.test.ts +97 -0
- package/src/nx-foreign-workspace-env.test.ts +329 -0
- package/src/nx-incomplete-run-summary.test.ts +70 -0
- package/src/nx-plugin-load-errors.test.ts +120 -0
- package/src/nx-project-resolution-cache.test.ts +176 -0
- package/src/nx-raw-inputs.test.ts +166 -0
- package/src/nx-resident-graph-workers.test.ts +112 -0
- package/src/nx-run-information.test.ts +118 -0
- package/src/nx-skipped-task-exit.test.ts +180 -0
- package/src/nx-state-location.test.ts +63 -0
- package/src/nx-store-resolution.test.ts +99 -0
- package/src/nx-task-graph-cycles.test.ts +148 -0
- package/src/nx-task-output-lease.test.ts +414 -0
- package/src/nx-use-local.test.ts +103 -0
- package/src/package-target-policy.ts +3 -6
- package/src/release-config-policy.ts +3 -6
- package/src/runtime-input.test.ts +215 -0
- package/src/runtime-input.ts +17 -0
- package/src/rust-tokens.ts +78 -0
- package/src/serial-group-wiring.test.ts +326 -0
- package/src/task-history-namespace.test.ts +181 -0
- package/src/task-results.test.ts +226 -0
- package/src/task-results.ts +75 -0
- package/src/test-program.test.ts +303 -0
- package/src/test-program.ts +341 -0
- package/src/test-timings.test.ts +240 -0
- package/src/test-timings.ts +311 -0
- package/src/test-walk-policy.test.ts +585 -0
- package/src/test-walk-policy.ts +731 -0
- package/src/testing.test.ts +509 -0
- package/src/testing.ts +922 -0
- package/src/type-guards.ts +4 -0
- package/src/typecheck-test-policy.test.ts +496 -11
- package/src/typecheck-test-policy.ts +137 -103
- package/src/workspace-config-policy.test.ts +212 -13
- package/src/workspace-config-policy.ts +101 -73
- package/src/workspace-index.ts +18 -0
- package/src/workspace-manifest.ts +41 -1
- package/test-timings.json +85 -28
- package/managed/raw/tooling/direnv/inherited-devenv.ts +0 -615
- package/managed/raw/tooling/direnv/shared-caches.sh +0 -53
- package/src/__tests__/in-process-nx-index.ts +0 -9
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/** The exact command and sources source-aware ESLint inference retains for cache inspectors. */
|
|
2
|
+
export interface EslintFileSet {
|
|
3
|
+
readonly command: string;
|
|
4
|
+
readonly files: readonly string[];
|
|
5
|
+
}
|
|
6
|
+
export declare const isEslintFileSet: (input: unknown) => input is EslintFileSet;
|
|
7
|
+
//# sourceMappingURL=eslint-file-set.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"eslint-file-set.d.ts","sourceRoot":"","sources":["../src/eslint-file-set.ts"],"names":[],"mappings":"AAEA,gGAAgG;AAChG,MAAM,WAAW,aAAa;IAC5B,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,KAAK,EAAE,SAAS,MAAM,EAAE,CAAC;CACnC;AAED,eAAO,MAAM,eAAe,4CAAsC,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import typia from 'typia';
|
|
2
|
+
export const isEslintFileSet = (() => {
|
|
3
|
+
const _io0 = (input, _exceptionable = true) => "string" === typeof input.command && (Array.isArray(input.files) && input.files.every((elem, _index1) => "string" === typeof elem)) && (2 === Object.keys(input).length || Object.keys(input).every(key => {
|
|
4
|
+
if (["command", "files"].some(prop => key === prop))
|
|
5
|
+
return true;
|
|
6
|
+
const value = input[key];
|
|
7
|
+
if (undefined === value)
|
|
8
|
+
return true;
|
|
9
|
+
return false;
|
|
10
|
+
}));
|
|
11
|
+
return (input, _exceptionable = true) => "object" === typeof input && null !== input && _io0(input, true);
|
|
12
|
+
})();
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { type TokenGrant } from './cpu-tokens.js';
|
|
2
|
+
/**
|
|
3
|
+
* A parent's CPU pool: the tokens a granted process lends its own children, held live rather than
|
|
4
|
+
* inherited as a bare count (specs/cowshed/05_gateway.md, "Host CPU budget", "Nested runners use a
|
|
5
|
+
* parent-held sub-ledger").
|
|
6
|
+
*
|
|
7
|
+
* The pool is a private Unix socket in a directory the process owns: the directory 0700, the socket
|
|
8
|
+
* 0600. A child asks it for a count with one request line, exactly as it asks the gateway
|
|
9
|
+
* (`holdLease`). The pool answers `queued` at once and `granted` when the child reaches the head of
|
|
10
|
+
* its FIFO queue. A grant lasts while its connection does, so a child that dies returns its tokens
|
|
11
|
+
* with its socket. A pool grants only from its own count, and a pool whose upstream grant has gone
|
|
12
|
+
* grants nothing.
|
|
13
|
+
*/
|
|
14
|
+
/** The environment name a child reads its parent's pool endpoint from; its count is `CPU_TOKENS_ENV`. */
|
|
15
|
+
export declare const CPU_POOL_ENV = "BOUNDED_EXEC_CPU_POOL";
|
|
16
|
+
/** An inherited pool descriptor; only a grant from its endpoint proves a live allocation. */
|
|
17
|
+
export interface InheritedCpuPool {
|
|
18
|
+
readonly socket: string;
|
|
19
|
+
readonly tokens: number;
|
|
20
|
+
}
|
|
21
|
+
/** A running pool: the environment a child inherits to reach it, and how to close it. */
|
|
22
|
+
export interface CpuPool {
|
|
23
|
+
/** The endpoint and the count the pool actually holds. */
|
|
24
|
+
readonly environment: Record<string, string>;
|
|
25
|
+
/**
|
|
26
|
+
* Refuses new grants, ends every child connection and removes the pool's directory. The outcome is
|
|
27
|
+
* cached: every call, and an upstream-loss close, returns the first close's outcome. A failed
|
|
28
|
+
* cleanup is an `ok: false` value, never a rejection.
|
|
29
|
+
*/
|
|
30
|
+
close(): Promise<CloseOutcome>;
|
|
31
|
+
}
|
|
32
|
+
type CloseOutcome = {
|
|
33
|
+
ok: true;
|
|
34
|
+
} | {
|
|
35
|
+
ok: false;
|
|
36
|
+
error: string;
|
|
37
|
+
};
|
|
38
|
+
/**
|
|
39
|
+
* The inherited descriptor, or null when malformed or absent. A missing or bare count, or an empty
|
|
40
|
+
* or NUL-bearing endpoint, proves no allocation. A valid descriptor still needs a live endpoint grant.
|
|
41
|
+
*/
|
|
42
|
+
export declare function readInheritedCpuPool(environment: Readonly<NodeJS.ProcessEnv>): InheritedCpuPool | null;
|
|
43
|
+
/**
|
|
44
|
+
* Ask the immediate parent pool for `min(want, parent.tokens, ceil(cores / 2))` tokens. A granted
|
|
45
|
+
* lease is the child's local allocation: its connection is the grant, and closing it returns only
|
|
46
|
+
* that allocation. An absent endpoint allows normal host acquisition; a connected endpoint's refusal
|
|
47
|
+
* or protocol failure must fail admission rather than taking a second grant above its parent.
|
|
48
|
+
*/
|
|
49
|
+
export declare function takeParentCpuTokens(parent: InheritedCpuPool, want: number, cores: number): Promise<TokenGrant>;
|
|
50
|
+
/**
|
|
51
|
+
* Make a pool of `tokens` that lives while `closed` does: when `closed` resolves the pool refuses
|
|
52
|
+
* grants, ends its child connections and removes its directory. Setup failures are values; a
|
|
53
|
+
* non-positive or unsafe count is a programmer bug and throws.
|
|
54
|
+
*/
|
|
55
|
+
export declare function createCpuPool(tokens: number, closed: Promise<void>): Promise<{
|
|
56
|
+
ok: true;
|
|
57
|
+
pool: CpuPool;
|
|
58
|
+
} | {
|
|
59
|
+
ok: false;
|
|
60
|
+
error: string;
|
|
61
|
+
}>;
|
|
62
|
+
export {};
|
|
63
|
+
//# sourceMappingURL=cpu-pool.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cpu-pool.d.ts","sourceRoot":"","sources":["../../../src/executors/bounded-exec/cpu-pool.ts"],"names":[],"mappings":"AAKA,OAAO,EAAwC,KAAK,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAGxF;;;;;;;;;;;GAWG;AAEH,yGAAyG;AACzG,eAAO,MAAM,YAAY,0BAA0B,CAAC;AAEpD,6FAA6F;AAC7F,MAAM,WAAW,gBAAgB;IAC/B,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;CACzB;AAED,yFAAyF;AACzF,MAAM,WAAW,OAAO;IACtB,0DAA0D;IAC1D,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAC7C;;;;OAIG;IACH,KAAK,IAAI,OAAO,CAAC,YAAY,CAAC,CAAC;CAChC;AAED,KAAK,YAAY,GAAG;IAAE,EAAE,EAAE,IAAI,CAAA;CAAE,GAAG;IAAE,EAAE,EAAE,KAAK,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,CAAC;AAoDhE;;;GAGG;AACH,wBAAgB,oBAAoB,CAAC,WAAW,EAAE,QAAQ,CAAC,MAAM,CAAC,UAAU,CAAC,GAAG,gBAAgB,GAAG,IAAI,CAOtG;AAED;;;;;GAKG;AACH,wBAAsB,mBAAmB,CAAC,MAAM,EAAE,gBAAgB,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,CAAC,CAgCpH;AAED;;;;GAIG;AACH,wBAAsB,aAAa,CACjC,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,OAAO,CAAC,IAAI,CAAC,GACpB,OAAO,CAAC;IAAE,EAAE,EAAE,IAAI,CAAC;IAAC,IAAI,EAAE,OAAO,CAAA;CAAE,GAAG;IAAE,EAAE,EAAE,KAAK,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,CAAC,CAqQrE"}
|
|
@@ -0,0 +1,379 @@
|
|
|
1
|
+
import * as _isTypeUint32_1 from "typia/lib/internal/_isTypeUint32";
|
|
2
|
+
import * as _jsonStringifyNumber_1 from "typia/lib/internal/_jsonStringifyNumber";
|
|
3
|
+
import * as _jsonStringifyString_1 from "typia/lib/internal/_jsonStringifyString";
|
|
4
|
+
import * as _jsonStringifyTail_1 from "typia/lib/internal/_jsonStringifyTail";
|
|
5
|
+
import * as _throwTypeGuardError_1 from "typia/lib/internal/_throwTypeGuardError";
|
|
6
|
+
import { chmod, rm } from 'node:fs/promises';
|
|
7
|
+
import { createServer } from 'node:net';
|
|
8
|
+
import { join } from 'node:path';
|
|
9
|
+
import typia from 'typia';
|
|
10
|
+
import { ownedSocketDirectory } from '../../testing.js';
|
|
11
|
+
import { CPU_TOKENS_ENV, parseInheritedTokens } from './cpu-tokens.js';
|
|
12
|
+
import { holdLease } from './gateway-lease.js';
|
|
13
|
+
/**
|
|
14
|
+
* A parent's CPU pool: the tokens a granted process lends its own children, held live rather than
|
|
15
|
+
* inherited as a bare count (specs/cowshed/05_gateway.md, "Host CPU budget", "Nested runners use a
|
|
16
|
+
* parent-held sub-ledger").
|
|
17
|
+
*
|
|
18
|
+
* The pool is a private Unix socket in a directory the process owns: the directory 0700, the socket
|
|
19
|
+
* 0600. A child asks it for a count with one request line, exactly as it asks the gateway
|
|
20
|
+
* (`holdLease`). The pool answers `queued` at once and `granted` when the child reaches the head of
|
|
21
|
+
* its FIFO queue. A grant lasts while its connection does, so a child that dies returns its tokens
|
|
22
|
+
* with its socket. A pool grants only from its own count, and a pool whose upstream grant has gone
|
|
23
|
+
* grants nothing.
|
|
24
|
+
*/
|
|
25
|
+
/** The environment name a child reads its parent's pool endpoint from; its count is `CPU_TOKENS_ENV`. */
|
|
26
|
+
export const CPU_POOL_ENV = 'BOUNDED_EXEC_CPU_POOL';
|
|
27
|
+
const parseRequest = (() => {
|
|
28
|
+
const _io0 = input => "cpu-pool" === input.op && ("number" === typeof input.want && (_isTypeUint32_1._isTypeUint32(input.want) && 1 <= input.want)) && (undefined === input.children || "boolean" === typeof input.children);
|
|
29
|
+
const __is = input => "object" === typeof input && null !== input && _io0(input);
|
|
30
|
+
return input => { input = JSON.parse(input); return __is(input) ? input : null; };
|
|
31
|
+
})();
|
|
32
|
+
const stringifyReply = (() => {
|
|
33
|
+
const _ip0 = input => true === input["ok"];
|
|
34
|
+
const _so0 = input => `{"ok":${String(input.ok)},"lease":${"\"" + input.lease + "\""}}`;
|
|
35
|
+
const _so1 = input => `{${_jsonStringifyTail_1._jsonStringifyTail(`"ok":${String(input.ok)},"lease":${"\"" + input.lease + "\""},"tokens":${String(_jsonStringifyNumber_1._jsonStringifyNumber(input.tokens))},${undefined === input.pool ? "" : `"pool":${undefined !== input.pool ? _jsonStringifyString_1._jsonStringifyString(input.pool) : undefined}`}`)}}`;
|
|
36
|
+
const _so2 = input => `{"ok":${String(input.ok)},"code":${"\"" + input.code + "\""},"error":${_jsonStringifyString_1._jsonStringifyString(input.error)}}`;
|
|
37
|
+
const _su0 = input => (() => {
|
|
38
|
+
if ("queued" === input.lease)
|
|
39
|
+
return _so0(input);
|
|
40
|
+
else if ("granted" === input.lease)
|
|
41
|
+
return _so1(input);
|
|
42
|
+
else if (false === input.ok)
|
|
43
|
+
return _so2(input);
|
|
44
|
+
else
|
|
45
|
+
_throwTypeGuardError_1._throwTypeGuardError({
|
|
46
|
+
method: "typia.json.createStringify",
|
|
47
|
+
expected: "({ ok: true; lease: \"queued\"; } | { ok: true; lease: \"granted\"; tokens: number; pool?: string | undefined; } | { ok: false; code: \"invalid-request\"; error: string; })",
|
|
48
|
+
value: input
|
|
49
|
+
});
|
|
50
|
+
})();
|
|
51
|
+
const _io0 = input => _ip0(input) && "queued" === input.lease;
|
|
52
|
+
const _io1 = input => _ip0(input) && "granted" === input.lease && "number" === typeof input.tokens && (undefined === input.pool || "string" === typeof input.pool);
|
|
53
|
+
const _io2 = input => false === input.ok && "invalid-request" === input.code && "string" === typeof input.error;
|
|
54
|
+
return input => _su0(input);
|
|
55
|
+
})();
|
|
56
|
+
/** A request over this many bytes is refused as `invalid-request` (specs/cowshed/05_gateway.md, "Wire"). */
|
|
57
|
+
const MAX_REQUEST_BYTES = 4096;
|
|
58
|
+
/** The refusal for a request too long to be one line, or a chunk that cannot be one. */
|
|
59
|
+
const TOO_LONG = `a request is one line of at most ${MAX_REQUEST_BYTES} bytes`;
|
|
60
|
+
/** The one empty buffer every connection starts from, so a request never copies a buffer it does not need. */
|
|
61
|
+
const EMPTY = Buffer.alloc(0);
|
|
62
|
+
const POOL_DIRECTORY = 'cpu-pool-';
|
|
63
|
+
const POOL_SOCKET = 'p.sock';
|
|
64
|
+
const CLOSED_WHILE_MADE = 'the grant that lends this CPU pool closed while the pool was being made';
|
|
65
|
+
/** One framed reply line. The newline is the framing; every reply goes through here. */
|
|
66
|
+
const frame = (reply) => `${stringifyReply(reply)}\n`;
|
|
67
|
+
/**
|
|
68
|
+
* The inherited descriptor, or null when malformed or absent. A missing or bare count, or an empty
|
|
69
|
+
* or NUL-bearing endpoint, proves no allocation. A valid descriptor still needs a live endpoint grant.
|
|
70
|
+
*/
|
|
71
|
+
export function readInheritedCpuPool(environment) {
|
|
72
|
+
const socket = environment[CPU_POOL_ENV];
|
|
73
|
+
const tokens = parseInheritedTokens(environment[CPU_TOKENS_ENV]);
|
|
74
|
+
if (socket === undefined || socket.length === 0 || socket.includes('\0') || tokens === null) {
|
|
75
|
+
return null;
|
|
76
|
+
}
|
|
77
|
+
return { socket, tokens };
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Ask the immediate parent pool for `min(want, parent.tokens, ceil(cores / 2))` tokens. A granted
|
|
81
|
+
* lease is the child's local allocation: its connection is the grant, and closing it returns only
|
|
82
|
+
* that allocation. An absent endpoint allows normal host acquisition; a connected endpoint's refusal
|
|
83
|
+
* or protocol failure must fail admission rather than taking a second grant above its parent.
|
|
84
|
+
*/
|
|
85
|
+
export async function takeParentCpuTokens(parent, want, cores) {
|
|
86
|
+
const ask = Math.min(want, parent.tokens, Math.ceil(cores / 2));
|
|
87
|
+
if (!Number.isSafeInteger(ask) || ask < 1) {
|
|
88
|
+
throw new RangeError(`a child asks its parent pool for a positive count, not ${ask} (want ${want}, pool ${parent.tokens}, cores ${cores})`);
|
|
89
|
+
}
|
|
90
|
+
if (parent.socket.length === 0 || parent.socket.includes('\0')) {
|
|
91
|
+
return {
|
|
92
|
+
granted: false,
|
|
93
|
+
cause: 'other',
|
|
94
|
+
reason: `the parent CPU pool's endpoint ${JSON.stringify(parent.socket)} is not a socket path`,
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
const lease = await holdLease(parent.socket, { op: 'cpu-pool', want: ask }, 'the parent CPU pool', {
|
|
98
|
+
ackMs: 2_000,
|
|
99
|
+
afterCloseMs: 5_000,
|
|
100
|
+
grantMs: null,
|
|
101
|
+
});
|
|
102
|
+
if (!lease.granted) {
|
|
103
|
+
return lease;
|
|
104
|
+
}
|
|
105
|
+
const tokens = lease.answer.tokens;
|
|
106
|
+
if (typeof tokens !== 'number' || !Number.isInteger(tokens) || tokens < 1 || tokens > ask) {
|
|
107
|
+
lease.release();
|
|
108
|
+
return {
|
|
109
|
+
granted: false,
|
|
110
|
+
cause: 'other',
|
|
111
|
+
reason: `the parent CPU pool granted ${JSON.stringify(tokens)} tokens for ${ask}`,
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
return { granted: true, tokens, release: lease.release, closed: lease.closed, inherited: true };
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Make a pool of `tokens` that lives while `closed` does: when `closed` resolves the pool refuses
|
|
118
|
+
* grants, ends its child connections and removes its directory. Setup failures are values; a
|
|
119
|
+
* non-positive or unsafe count is a programmer bug and throws.
|
|
120
|
+
*/
|
|
121
|
+
export async function createCpuPool(tokens, closed) {
|
|
122
|
+
if (!Number.isSafeInteger(tokens) || tokens < 1) {
|
|
123
|
+
throw new RangeError(`a CPU pool holds a positive safe integer of tokens, not ${tokens}`);
|
|
124
|
+
}
|
|
125
|
+
let directory;
|
|
126
|
+
try {
|
|
127
|
+
directory = await ownedSocketDirectory(POOL_DIRECTORY, POOL_SOCKET);
|
|
128
|
+
}
|
|
129
|
+
catch (error) {
|
|
130
|
+
return {
|
|
131
|
+
ok: false,
|
|
132
|
+
error: `the CPU pool's directory could not be made: ${error instanceof Error ? error.message : String(error)}`,
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
const socketPath = join(directory, POOL_SOCKET);
|
|
136
|
+
let free = tokens;
|
|
137
|
+
let closing = false;
|
|
138
|
+
let listening = false;
|
|
139
|
+
let shut;
|
|
140
|
+
// Every failure the pool records, including one its listener reports while it runs. The outcome reads them.
|
|
141
|
+
const failures = [];
|
|
142
|
+
const queue = [];
|
|
143
|
+
// Allocated only for children that need the owner to serve their local sub-ledger.
|
|
144
|
+
let subpools;
|
|
145
|
+
let creating;
|
|
146
|
+
const children = new Set();
|
|
147
|
+
let resolveStarted;
|
|
148
|
+
const started = new Promise((done) => {
|
|
149
|
+
resolveStarted = done;
|
|
150
|
+
});
|
|
151
|
+
// FIFO: the head takes `min(want, free)` as one grant once any token is free. A waiting child holds
|
|
152
|
+
// nothing, so no child waits while it holds tokens another child needs.
|
|
153
|
+
const drain = () => {
|
|
154
|
+
while (!closing && free > 0) {
|
|
155
|
+
const waiter = queue.shift();
|
|
156
|
+
if (waiter === undefined) {
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
if (!waiter.child.socket.writable) {
|
|
160
|
+
// A child that half-closed or failed while queued. Its `close` finds it gone from the queue.
|
|
161
|
+
continue;
|
|
162
|
+
}
|
|
163
|
+
const granted = Math.min(waiter.want, free);
|
|
164
|
+
free -= granted;
|
|
165
|
+
waiter.child.phase = { kind: 'holding', tokens: granted };
|
|
166
|
+
if (!waiter.children) {
|
|
167
|
+
waiter.child.socket.write(frame({ ok: true, lease: 'granted', tokens: granted }));
|
|
168
|
+
continue;
|
|
169
|
+
}
|
|
170
|
+
const ended = new Promise((done) => {
|
|
171
|
+
if (waiter.child.socket.destroyed)
|
|
172
|
+
done();
|
|
173
|
+
else
|
|
174
|
+
waiter.child.socket.once('close', done);
|
|
175
|
+
});
|
|
176
|
+
const creation = createCpuPool(granted, ended).then(async (made) => {
|
|
177
|
+
if (!made.ok) {
|
|
178
|
+
failures.push(made.error);
|
|
179
|
+
// Keep holding until close credits exactly this allocation once.
|
|
180
|
+
waiter.child.socket.end(frame({ ok: false, code: 'invalid-request', error: made.error }));
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
subpools ??= new Set();
|
|
184
|
+
subpools.add(made.pool);
|
|
185
|
+
if (waiter.child.phase.kind !== 'holding' || closing) {
|
|
186
|
+
const outcome = await made.pool.close();
|
|
187
|
+
if (!outcome.ok)
|
|
188
|
+
failures.push(outcome.error);
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
waiter.child.socket.write(frame({ ok: true, lease: 'granted', tokens: granted, pool: made.pool.environment[CPU_POOL_ENV] }));
|
|
192
|
+
});
|
|
193
|
+
creating ??= [];
|
|
194
|
+
creating.push(creation);
|
|
195
|
+
}
|
|
196
|
+
};
|
|
197
|
+
const refuse = (child, error) => {
|
|
198
|
+
child.phase = { kind: 'done' };
|
|
199
|
+
child.socket.end(frame({ ok: false, code: 'invalid-request', error }));
|
|
200
|
+
};
|
|
201
|
+
const serve = (socket) => {
|
|
202
|
+
if (closing) {
|
|
203
|
+
socket.destroy();
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
const child = { socket, phase: { kind: 'reading' } };
|
|
207
|
+
children.add(socket);
|
|
208
|
+
// The child's own process reports a socket error. Accounting comes from `close`, the one event that
|
|
209
|
+
// reports every way a socket ends.
|
|
210
|
+
socket.on('error', () => socket.destroy());
|
|
211
|
+
socket.on('close', () => {
|
|
212
|
+
children.delete(socket);
|
|
213
|
+
if (child.phase.kind === 'queued') {
|
|
214
|
+
const at = queue.findIndex((waiter) => waiter.child === child);
|
|
215
|
+
if (at !== -1) {
|
|
216
|
+
queue.splice(at, 1);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
else if (child.phase.kind === 'holding') {
|
|
220
|
+
free += child.phase.tokens;
|
|
221
|
+
}
|
|
222
|
+
child.phase = { kind: 'done' };
|
|
223
|
+
drain();
|
|
224
|
+
});
|
|
225
|
+
let buffered = EMPTY;
|
|
226
|
+
// Refusing releases what the connection buffered: a refused request holds no storage.
|
|
227
|
+
const rejectRequest = (error) => {
|
|
228
|
+
buffered = EMPTY;
|
|
229
|
+
refuse(child, error);
|
|
230
|
+
};
|
|
231
|
+
socket.on('data', (chunk) => {
|
|
232
|
+
// A refusal already ended the writable side. Discard remaining request bytes without resetting
|
|
233
|
+
// the socket: an RST here can erase the typed refusal before the peer reads it.
|
|
234
|
+
if (child.phase.kind === 'done')
|
|
235
|
+
return;
|
|
236
|
+
if (child.phase.kind !== 'reading') {
|
|
237
|
+
// A child that writes past its one request has broken the protocol. Its grant or place in line
|
|
238
|
+
// ends with the connection, and `close` returns whatever it held.
|
|
239
|
+
socket.destroy();
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
// Bound the total before copying: one request is at most MAX_REQUEST_BYTES and its newline, so a
|
|
243
|
+
// larger total is refused without copying any of it, however much one chunk carries.
|
|
244
|
+
if (buffered.length + chunk.length > MAX_REQUEST_BYTES + 1) {
|
|
245
|
+
rejectRequest(TOO_LONG);
|
|
246
|
+
return;
|
|
247
|
+
}
|
|
248
|
+
// The first chunk is the buffer itself: a request that arrives whole is never copied.
|
|
249
|
+
buffered = buffered.length === 0 ? chunk : Buffer.concat([buffered, chunk]);
|
|
250
|
+
const newline = buffered.indexOf(0x0a);
|
|
251
|
+
const lineBytes = newline === -1 ? buffered.length : newline;
|
|
252
|
+
if (lineBytes > MAX_REQUEST_BYTES) {
|
|
253
|
+
rejectRequest(TOO_LONG);
|
|
254
|
+
return;
|
|
255
|
+
}
|
|
256
|
+
if (newline === -1) {
|
|
257
|
+
return;
|
|
258
|
+
}
|
|
259
|
+
if (newline !== buffered.length - 1) {
|
|
260
|
+
rejectRequest('a connection makes one request; this one sent more');
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
263
|
+
const line = buffered.subarray(0, newline).toString('utf8');
|
|
264
|
+
buffered = EMPTY;
|
|
265
|
+
let request;
|
|
266
|
+
try {
|
|
267
|
+
request = parseRequest(line);
|
|
268
|
+
}
|
|
269
|
+
catch (error) {
|
|
270
|
+
// isParse returns null for a wrong shape, but JSON syntax errors still throw.
|
|
271
|
+
if (!(error instanceof SyntaxError))
|
|
272
|
+
throw error;
|
|
273
|
+
request = null;
|
|
274
|
+
}
|
|
275
|
+
if (request === null) {
|
|
276
|
+
refuse(child, 'a request is {"op":"cpu-pool","want":<a whole number of tokens, at least 1>}');
|
|
277
|
+
return;
|
|
278
|
+
}
|
|
279
|
+
child.phase = { kind: 'queued' };
|
|
280
|
+
queue.push({ child, want: request.want, children: request.children === true });
|
|
281
|
+
socket.write(frame({ ok: true, lease: 'queued' }));
|
|
282
|
+
drain();
|
|
283
|
+
});
|
|
284
|
+
};
|
|
285
|
+
const server = createServer(serve);
|
|
286
|
+
// Memoized: the first call does the teardown, every later call awaits its outcome. The outcome
|
|
287
|
+
// collects every cleanup failure as text, so the teardown itself never rejects.
|
|
288
|
+
const shutdown = () => {
|
|
289
|
+
shut ??= (async () => {
|
|
290
|
+
closing = true;
|
|
291
|
+
await started;
|
|
292
|
+
for (const socket of children) {
|
|
293
|
+
socket.destroy();
|
|
294
|
+
}
|
|
295
|
+
if (listening) {
|
|
296
|
+
await new Promise((done) => {
|
|
297
|
+
server.close((error) => {
|
|
298
|
+
if (error) {
|
|
299
|
+
failures.push(`its socket did not close: ${error.message}`);
|
|
300
|
+
}
|
|
301
|
+
done();
|
|
302
|
+
});
|
|
303
|
+
});
|
|
304
|
+
}
|
|
305
|
+
if (creating !== undefined)
|
|
306
|
+
await Promise.all(creating);
|
|
307
|
+
if (subpools !== undefined) {
|
|
308
|
+
for (const pool of subpools) {
|
|
309
|
+
const outcome = await pool.close();
|
|
310
|
+
if (!outcome.ok)
|
|
311
|
+
failures.push(outcome.error);
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
try {
|
|
315
|
+
await rm(directory, { recursive: true, force: true });
|
|
316
|
+
}
|
|
317
|
+
catch (error) {
|
|
318
|
+
failures.push(`its directory was not removed: ${error instanceof Error ? error.message : String(error)}`);
|
|
319
|
+
}
|
|
320
|
+
return failures.length === 0
|
|
321
|
+
? { ok: true }
|
|
322
|
+
: { ok: false, error: `the CPU pool at ${socketPath} did not close cleanly: ${failures.join('; ')}` };
|
|
323
|
+
})();
|
|
324
|
+
return shut;
|
|
325
|
+
};
|
|
326
|
+
// Upstream loss and a server failure close the pool with no caller to return to. A failed close is
|
|
327
|
+
// written to stderr and stays cached for the owner's own `close()`; the callback never rejects.
|
|
328
|
+
const closeInBackground = () => {
|
|
329
|
+
void shutdown().then((outcome) => {
|
|
330
|
+
if (!outcome.ok) {
|
|
331
|
+
process.stderr.write(`bounded-exec: ${outcome.error}\n`);
|
|
332
|
+
}
|
|
333
|
+
});
|
|
334
|
+
};
|
|
335
|
+
void closed.then(closeInBackground);
|
|
336
|
+
server.on('error', (error) => {
|
|
337
|
+
if (!listening) {
|
|
338
|
+
resolveStarted(error);
|
|
339
|
+
return;
|
|
340
|
+
}
|
|
341
|
+
// The failure joins the pool's outcome, so the owner's close reports it as an ordinary failure.
|
|
342
|
+
failures.push(`it failed while listening: ${error.message}`);
|
|
343
|
+
process.stderr.write(`bounded-exec: the CPU pool at ${socketPath} failed: ${error.message}\n`);
|
|
344
|
+
closeInBackground();
|
|
345
|
+
});
|
|
346
|
+
server.listen(socketPath, () => {
|
|
347
|
+
listening = true;
|
|
348
|
+
resolveStarted(null);
|
|
349
|
+
});
|
|
350
|
+
const abort = async (reason) => {
|
|
351
|
+
const outcome = await shutdown();
|
|
352
|
+
return { ok: false, error: outcome.ok ? reason : `${reason}; ${outcome.error}` };
|
|
353
|
+
};
|
|
354
|
+
const failure = await started;
|
|
355
|
+
if (failure !== null) {
|
|
356
|
+
return abort(`the CPU pool could not listen at ${socketPath}: ${failure.message}`);
|
|
357
|
+
}
|
|
358
|
+
if (closing) {
|
|
359
|
+
return abort(CLOSED_WHILE_MADE);
|
|
360
|
+
}
|
|
361
|
+
try {
|
|
362
|
+
// Until this runs the socket carries the umask's mode, which is safe: the 0700 directory above it
|
|
363
|
+
// denies every other user the path.
|
|
364
|
+
await chmod(socketPath, 0o600);
|
|
365
|
+
}
|
|
366
|
+
catch (error) {
|
|
367
|
+
return abort(`the CPU pool's socket could not be made private: ${error instanceof Error ? error.message : String(error)}`);
|
|
368
|
+
}
|
|
369
|
+
if (closing) {
|
|
370
|
+
return abort(CLOSED_WHILE_MADE);
|
|
371
|
+
}
|
|
372
|
+
return {
|
|
373
|
+
ok: true,
|
|
374
|
+
pool: {
|
|
375
|
+
environment: { [CPU_POOL_ENV]: socketPath, [CPU_TOKENS_ENV]: String(tokens) },
|
|
376
|
+
close: shutdown,
|
|
377
|
+
},
|
|
378
|
+
};
|
|
379
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { type UnleasedCause } from './gateway-lease.js';
|
|
2
|
+
/**
|
|
3
|
+
* The client half of cowshed's host CPU budget (specs/cowshed/05_gateway.md, "Host CPU budget").
|
|
4
|
+
*
|
|
5
|
+
* Every gate on a host sizes its runners to the whole machine: Nx runs a task per core and each
|
|
6
|
+
* nextest run under it runs a test thread per core again, so a few concurrent gates keep the load
|
|
7
|
+
* average several times the core count and tests that do no disk work time out waiting for a CPU.
|
|
8
|
+
* The cowshed gateway holds one budget of CPU tokens for the host, about one per core. Before it
|
|
9
|
+
* starts a runner, bounded-exec asks for as many tokens as the runner would run threads or
|
|
10
|
+
* processes at once, and sizes the runner to the grant: every gate keeps running in parallel, and
|
|
11
|
+
* the total stays near the core count.
|
|
12
|
+
*/
|
|
13
|
+
/** How a runner's parallelism is set, which decides what it asks for and how a grant sizes it. */
|
|
14
|
+
export type RunnerKind =
|
|
15
|
+
/** `nextest run`: its test threads, `--test-threads` or `NEXTEST_TEST_THREADS`. */
|
|
16
|
+
'nextest'
|
|
17
|
+
/** `bun test --parallel`: its worker processes. */
|
|
18
|
+
| 'bun-parallel'
|
|
19
|
+
/** A cargo build (`cargo build`/`test`/`clippy`/…, `nextest archive`, `napi build`): its jobs. */
|
|
20
|
+
| 'cargo'
|
|
21
|
+
/** Anything else, one `bun test` process included: one process unless it declares more. */
|
|
22
|
+
| 'process';
|
|
23
|
+
/** What a runner asks the budget for; Cargo also carries its independent test-thread ceiling. */
|
|
24
|
+
export type Demand = {
|
|
25
|
+
kind: 'cargo';
|
|
26
|
+
want: number;
|
|
27
|
+
testThreads: number | null;
|
|
28
|
+
} | {
|
|
29
|
+
kind: Exclude<RunnerKind, 'cargo'>;
|
|
30
|
+
want: number;
|
|
31
|
+
};
|
|
32
|
+
/** Every budgeted command learns its grant here, so a script that starts a runner can size it. */
|
|
33
|
+
export declare const CPU_TOKENS_ENV = "BOUNDED_EXEC_CPU_TOKENS";
|
|
34
|
+
/**
|
|
35
|
+
* What `command` asks for: its kind by invocation, and `declared` (the target's `parallelism`)
|
|
36
|
+
* when it says, else the count the command names (`--test-threads=N`, `--parallel=N`), else the
|
|
37
|
+
* runner's own default — one thread per core for nextest, a bare `--parallel` and cargo, one
|
|
38
|
+
* process for anything else. Positive inherited runner counts are ceilings; configured counts may only lower them.
|
|
39
|
+
*/
|
|
40
|
+
export declare function runnerDemand(command: string, declared: number | undefined, cores: number, inherited: Readonly<NodeJS.ProcessEnv>, configured?: Readonly<NodeJS.ProcessEnv>): Demand;
|
|
41
|
+
/**
|
|
42
|
+
* Size the runner to the lesser of its bounded demand and grant, retaining Cargo's test-thread ceiling.
|
|
43
|
+
* Rewrite named flags so they cannot outrank that ceiling. `CPU_TOKENS_ENV` still reports the actual held grant.
|
|
44
|
+
*/
|
|
45
|
+
export declare function sizeRunner(demand: Demand, command: string, tokens: number): {
|
|
46
|
+
command: string;
|
|
47
|
+
env: Record<string, string>;
|
|
48
|
+
};
|
|
49
|
+
/**
|
|
50
|
+
* The count `CPU_TOKENS_ENV` names, as a positive safe integer, or `null` when absent or malformed. It is
|
|
51
|
+
* descriptor input only: a count proves nothing about a live pool and grants nothing. A missing or malformed
|
|
52
|
+
* descriptor follows normal host admission; a valid one still needs a grant from its endpoint. Connection
|
|
53
|
+
* absence follows the host path, while a connected parent's refusal fails admission to avoid hold-and-wait.
|
|
54
|
+
*/
|
|
55
|
+
export declare function parseInheritedTokens(value: string | undefined): number | null;
|
|
56
|
+
/**
|
|
57
|
+
* Tokens held until released, or why the runner starts without the budget. `closed` resolves when
|
|
58
|
+
* the grant's connection closes, however it closes, so a sub-pool built on the grant ends with it.
|
|
59
|
+
* `inherited` marks tokens a parent's CPU pool lent, not tokens the host gateway granted.
|
|
60
|
+
*/
|
|
61
|
+
export type TokenGrant = {
|
|
62
|
+
granted: true;
|
|
63
|
+
tokens: number;
|
|
64
|
+
release: () => void;
|
|
65
|
+
closed: Promise<void>;
|
|
66
|
+
inherited?: true;
|
|
67
|
+
} | {
|
|
68
|
+
granted: false;
|
|
69
|
+
cause: UnleasedCause;
|
|
70
|
+
reason: string;
|
|
71
|
+
};
|
|
72
|
+
/** Where bounded-exec takes its tokens; the gateway in production, a fake in tests. */
|
|
73
|
+
export interface CpuBudget {
|
|
74
|
+
take(want: number, checkout: string, command: string): Promise<TokenGrant>;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* The gateway at `socket`'s budget. A grant has no deadline: the wait is how the budget works, and
|
|
78
|
+
* the gateway answers `queued` at once, so a client knows it is alive. `checkout` is the Nx
|
|
79
|
+
* workspace root: the gateway shares tokens fairly between checkouts.
|
|
80
|
+
*/
|
|
81
|
+
export declare function gatewayCpuBudget(socket?: string): CpuBudget;
|
|
82
|
+
//# sourceMappingURL=cpu-tokens.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cpu-tokens.d.ts","sourceRoot":"","sources":["../../../src/executors/bounded-exec/cpu-tokens.ts"],"names":[],"mappings":"AAAA,OAAO,EAA6B,KAAK,aAAa,EAAE,MAAM,oBAAoB,CAAC;AAGnF;;;;;;;;;;GAUG;AAEH,kGAAkG;AAClG,MAAM,MAAM,UAAU;AACpB,mFAAmF;AACjF,SAAS;AACX,mDAAmD;GACjD,cAAc;AAChB,kGAAkG;GAChG,OAAO;AACT,2FAA2F;GACzF,SAAS,CAAC;AAEd,iGAAiG;AACjG,MAAM,MAAM,MAAM,GACd;IAAE,IAAI,EAAE,OAAO,CAAC;IAAC,IAAI,EAAE,MAAM,CAAC;IAAC,WAAW,EAAE,MAAM,GAAG,IAAI,CAAA;CAAE,GAC3D;IAAE,IAAI,EAAE,OAAO,CAAC,UAAU,EAAE,OAAO,CAAC,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,CAAC;AAEzD,kGAAkG;AAClG,eAAO,MAAM,cAAc,4BAA4B,CAAC;AAQxD;;;;;GAKG;AACH,wBAAgB,YAAY,CAC1B,OAAO,EAAE,MAAM,EACf,QAAQ,EAAE,MAAM,GAAG,SAAS,EAC5B,KAAK,EAAE,MAAM,EACb,SAAS,EAAE,QAAQ,CAAC,MAAM,CAAC,UAAU,CAAC,EACtC,UAAU,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,UAAU,CAAC,GACvC,MAAM,CAyBR;AAED;;;GAGG;AACH,wBAAgB,UAAU,CACxB,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,MAAM,EACf,MAAM,EAAE,MAAM,GACb;IAAE,OAAO,EAAE,MAAM,CAAC;IAAC,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;CAAE,CAwBlD;AASD;;;;;GAKG;AACH,wBAAgB,oBAAoB,CAAC,KAAK,EAAE,MAAM,GAAG,SAAS,GAAG,MAAM,GAAG,IAAI,CAM7E;AAED;;;;GAIG;AACH,MAAM,MAAM,UAAU,GAClB;IAAE,OAAO,EAAE,IAAI,CAAC;IAAC,MAAM,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,IAAI,CAAC;IAAC,MAAM,EAAE,OAAO,CAAC,IAAI,CAAC,CAAC;IAAC,SAAS,CAAC,EAAE,IAAI,CAAA;CAAE,GAC/F;IAAE,OAAO,EAAE,KAAK,CAAC;IAAC,KAAK,EAAE,aAAa,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAAC;AAE7D,uFAAuF;AACvF,MAAM,WAAW,SAAS;IACxB,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,CAAC,CAAC;CAC5E;AAED;;;;GAIG;AACH,wBAAgB,gBAAgB,CAAC,MAAM,GAAE,MAAuB,GAAG,SAAS,CAuB3E"}
|