@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,417 @@
|
|
|
1
|
+
import { chmod, rm } from 'node:fs/promises';
|
|
2
|
+
import { createServer, type Server, type Socket } from 'node:net';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import typia from 'typia';
|
|
5
|
+
import { ownedSocketDirectory } from '../../testing.js';
|
|
6
|
+
import { CPU_TOKENS_ENV, parseInheritedTokens, type TokenGrant } from './cpu-tokens.js';
|
|
7
|
+
import { holdLease } from './gateway-lease.js';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* A parent's CPU pool: the tokens a granted process lends its own children, held live rather than
|
|
11
|
+
* inherited as a bare count (specs/cowshed/05_gateway.md, "Host CPU budget", "Nested runners use a
|
|
12
|
+
* parent-held sub-ledger").
|
|
13
|
+
*
|
|
14
|
+
* The pool is a private Unix socket in a directory the process owns: the directory 0700, the socket
|
|
15
|
+
* 0600. A child asks it for a count with one request line, exactly as it asks the gateway
|
|
16
|
+
* (`holdLease`). The pool answers `queued` at once and `granted` when the child reaches the head of
|
|
17
|
+
* its FIFO queue. A grant lasts while its connection does, so a child that dies returns its tokens
|
|
18
|
+
* with its socket. A pool grants only from its own count, and a pool whose upstream grant has gone
|
|
19
|
+
* grants nothing.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
/** The environment name a child reads its parent's pool endpoint from; its count is `CPU_TOKENS_ENV`. */
|
|
23
|
+
export const CPU_POOL_ENV = 'BOUNDED_EXEC_CPU_POOL';
|
|
24
|
+
|
|
25
|
+
/** An inherited pool descriptor; only a grant from its endpoint proves a live allocation. */
|
|
26
|
+
export interface InheritedCpuPool {
|
|
27
|
+
readonly socket: string;
|
|
28
|
+
readonly tokens: number;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** A running pool: the environment a child inherits to reach it, and how to close it. */
|
|
32
|
+
export interface CpuPool {
|
|
33
|
+
/** The endpoint and the count the pool actually holds. */
|
|
34
|
+
readonly environment: Record<string, string>;
|
|
35
|
+
/**
|
|
36
|
+
* Refuses new grants, ends every child connection and removes the pool's directory. The outcome is
|
|
37
|
+
* cached: every call, and an upstream-loss close, returns the first close's outcome. A failed
|
|
38
|
+
* cleanup is an `ok: false` value, never a rejection.
|
|
39
|
+
*/
|
|
40
|
+
close(): Promise<CloseOutcome>;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
type CloseOutcome = { ok: true } | { ok: false; error: string };
|
|
44
|
+
|
|
45
|
+
/** The one line a child sends: a count of whole tokens, at least one. */
|
|
46
|
+
interface CpuPoolRequest {
|
|
47
|
+
op: 'cpu-pool';
|
|
48
|
+
want: number & typia.tags.Type<'uint32'> & typia.tags.Minimum<1>;
|
|
49
|
+
/** Ask the owning pool to serve this allocation as its own child pool (Cargo's host launcher). */
|
|
50
|
+
children?: boolean;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** The pool's answers, in `holdLease`'s framing. */
|
|
54
|
+
type CpuPoolReply =
|
|
55
|
+
| { ok: true; lease: 'queued' }
|
|
56
|
+
| { ok: true; lease: 'granted'; tokens: number; pool?: string }
|
|
57
|
+
| { ok: false; code: 'invalid-request'; error: string };
|
|
58
|
+
|
|
59
|
+
const parseRequest = typia.json.createIsParse<CpuPoolRequest>();
|
|
60
|
+
const stringifyReply = typia.json.createStringify<CpuPoolReply>();
|
|
61
|
+
|
|
62
|
+
/** A request over this many bytes is refused as `invalid-request` (specs/cowshed/05_gateway.md, "Wire"). */
|
|
63
|
+
const MAX_REQUEST_BYTES = 4096;
|
|
64
|
+
/** The refusal for a request too long to be one line, or a chunk that cannot be one. */
|
|
65
|
+
const TOO_LONG = `a request is one line of at most ${MAX_REQUEST_BYTES} bytes`;
|
|
66
|
+
/** The one empty buffer every connection starts from, so a request never copies a buffer it does not need. */
|
|
67
|
+
const EMPTY: Buffer = Buffer.alloc(0);
|
|
68
|
+
const POOL_DIRECTORY = 'cpu-pool-';
|
|
69
|
+
const POOL_SOCKET = 'p.sock';
|
|
70
|
+
const CLOSED_WHILE_MADE = 'the grant that lends this CPU pool closed while the pool was being made';
|
|
71
|
+
|
|
72
|
+
/** A child's phase. Tokens are owed back only while `holding`; a queued child holds nothing. */
|
|
73
|
+
type Phase =
|
|
74
|
+
| { readonly kind: 'reading' }
|
|
75
|
+
| { readonly kind: 'queued' }
|
|
76
|
+
| { readonly kind: 'holding'; readonly tokens: number }
|
|
77
|
+
| { readonly kind: 'done' };
|
|
78
|
+
|
|
79
|
+
/** A child connection and what the pool owes it. Its grant, or its place in line, ends with the connection. */
|
|
80
|
+
interface Child {
|
|
81
|
+
readonly socket: Socket;
|
|
82
|
+
phase: Phase;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** A queued child and the count it asked for. */
|
|
86
|
+
interface Waiter {
|
|
87
|
+
readonly child: Child;
|
|
88
|
+
readonly want: number;
|
|
89
|
+
readonly children: boolean;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** One framed reply line. The newline is the framing; every reply goes through here. */
|
|
93
|
+
const frame = (reply: CpuPoolReply): string => `${stringifyReply(reply)}\n`;
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* The inherited descriptor, or null when malformed or absent. A missing or bare count, or an empty
|
|
97
|
+
* or NUL-bearing endpoint, proves no allocation. A valid descriptor still needs a live endpoint grant.
|
|
98
|
+
*/
|
|
99
|
+
export function readInheritedCpuPool(environment: Readonly<NodeJS.ProcessEnv>): InheritedCpuPool | null {
|
|
100
|
+
const socket = environment[CPU_POOL_ENV];
|
|
101
|
+
const tokens = parseInheritedTokens(environment[CPU_TOKENS_ENV]);
|
|
102
|
+
if (socket === undefined || socket.length === 0 || socket.includes('\0') || tokens === null) {
|
|
103
|
+
return null;
|
|
104
|
+
}
|
|
105
|
+
return { socket, tokens };
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Ask the immediate parent pool for `min(want, parent.tokens, ceil(cores / 2))` tokens. A granted
|
|
110
|
+
* lease is the child's local allocation: its connection is the grant, and closing it returns only
|
|
111
|
+
* that allocation. An absent endpoint allows normal host acquisition; a connected endpoint's refusal
|
|
112
|
+
* or protocol failure must fail admission rather than taking a second grant above its parent.
|
|
113
|
+
*/
|
|
114
|
+
export async function takeParentCpuTokens(parent: InheritedCpuPool, want: number, cores: number): Promise<TokenGrant> {
|
|
115
|
+
const ask = Math.min(want, parent.tokens, Math.ceil(cores / 2));
|
|
116
|
+
if (!Number.isSafeInteger(ask) || ask < 1) {
|
|
117
|
+
throw new RangeError(
|
|
118
|
+
`a child asks its parent pool for a positive count, not ${ask} (want ${want}, pool ${parent.tokens}, cores ${cores})`,
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
if (parent.socket.length === 0 || parent.socket.includes('\0')) {
|
|
122
|
+
return {
|
|
123
|
+
granted: false,
|
|
124
|
+
cause: 'other',
|
|
125
|
+
reason: `the parent CPU pool's endpoint ${JSON.stringify(parent.socket)} is not a socket path`,
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
const lease = await holdLease(parent.socket, { op: 'cpu-pool', want: ask }, 'the parent CPU pool', {
|
|
129
|
+
ackMs: 2_000,
|
|
130
|
+
afterCloseMs: 5_000,
|
|
131
|
+
grantMs: null,
|
|
132
|
+
});
|
|
133
|
+
if (!lease.granted) {
|
|
134
|
+
return lease;
|
|
135
|
+
}
|
|
136
|
+
const tokens = lease.answer.tokens;
|
|
137
|
+
if (typeof tokens !== 'number' || !Number.isInteger(tokens) || tokens < 1 || tokens > ask) {
|
|
138
|
+
lease.release();
|
|
139
|
+
return {
|
|
140
|
+
granted: false,
|
|
141
|
+
cause: 'other',
|
|
142
|
+
reason: `the parent CPU pool granted ${JSON.stringify(tokens)} tokens for ${ask}`,
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
return { granted: true, tokens, release: lease.release, closed: lease.closed, inherited: true };
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Make a pool of `tokens` that lives while `closed` does: when `closed` resolves the pool refuses
|
|
150
|
+
* grants, ends its child connections and removes its directory. Setup failures are values; a
|
|
151
|
+
* non-positive or unsafe count is a programmer bug and throws.
|
|
152
|
+
*/
|
|
153
|
+
export async function createCpuPool(
|
|
154
|
+
tokens: number,
|
|
155
|
+
closed: Promise<void>,
|
|
156
|
+
): Promise<{ ok: true; pool: CpuPool } | { ok: false; error: string }> {
|
|
157
|
+
if (!Number.isSafeInteger(tokens) || tokens < 1) {
|
|
158
|
+
throw new RangeError(`a CPU pool holds a positive safe integer of tokens, not ${tokens}`);
|
|
159
|
+
}
|
|
160
|
+
let directory: string;
|
|
161
|
+
try {
|
|
162
|
+
directory = await ownedSocketDirectory(POOL_DIRECTORY, POOL_SOCKET);
|
|
163
|
+
} catch (error) {
|
|
164
|
+
return {
|
|
165
|
+
ok: false,
|
|
166
|
+
error: `the CPU pool's directory could not be made: ${error instanceof Error ? error.message : String(error)}`,
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
const socketPath = join(directory, POOL_SOCKET);
|
|
170
|
+
let free = tokens;
|
|
171
|
+
let closing = false;
|
|
172
|
+
let listening = false;
|
|
173
|
+
let shut: Promise<CloseOutcome> | undefined;
|
|
174
|
+
// Every failure the pool records, including one its listener reports while it runs. The outcome reads them.
|
|
175
|
+
const failures: string[] = [];
|
|
176
|
+
const queue: Waiter[] = [];
|
|
177
|
+
// Allocated only for children that need the owner to serve their local sub-ledger.
|
|
178
|
+
let subpools: Set<CpuPool> | undefined;
|
|
179
|
+
let creating: Promise<void>[] | undefined;
|
|
180
|
+
const children = new Set<Socket>();
|
|
181
|
+
let resolveStarted!: (failure: Error | null) => void;
|
|
182
|
+
const started = new Promise<Error | null>((done) => {
|
|
183
|
+
resolveStarted = done;
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
// FIFO: the head takes `min(want, free)` as one grant once any token is free. A waiting child holds
|
|
187
|
+
// nothing, so no child waits while it holds tokens another child needs.
|
|
188
|
+
const drain = (): void => {
|
|
189
|
+
while (!closing && free > 0) {
|
|
190
|
+
const waiter = queue.shift();
|
|
191
|
+
if (waiter === undefined) {
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
if (!waiter.child.socket.writable) {
|
|
195
|
+
// A child that half-closed or failed while queued. Its `close` finds it gone from the queue.
|
|
196
|
+
continue;
|
|
197
|
+
}
|
|
198
|
+
const granted = Math.min(waiter.want, free);
|
|
199
|
+
free -= granted;
|
|
200
|
+
waiter.child.phase = { kind: 'holding', tokens: granted };
|
|
201
|
+
if (!waiter.children) {
|
|
202
|
+
waiter.child.socket.write(frame({ ok: true, lease: 'granted', tokens: granted }));
|
|
203
|
+
continue;
|
|
204
|
+
}
|
|
205
|
+
const ended = new Promise<void>((done) => {
|
|
206
|
+
if (waiter.child.socket.destroyed) done();
|
|
207
|
+
else waiter.child.socket.once('close', done);
|
|
208
|
+
});
|
|
209
|
+
const creation = createCpuPool(granted, ended).then(async (made) => {
|
|
210
|
+
if (!made.ok) {
|
|
211
|
+
failures.push(made.error);
|
|
212
|
+
// Keep holding until close credits exactly this allocation once.
|
|
213
|
+
waiter.child.socket.end(frame({ ok: false, code: 'invalid-request', error: made.error }));
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
subpools ??= new Set();
|
|
217
|
+
subpools.add(made.pool);
|
|
218
|
+
if (waiter.child.phase.kind !== 'holding' || closing) {
|
|
219
|
+
const outcome = await made.pool.close();
|
|
220
|
+
if (!outcome.ok) failures.push(outcome.error);
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
waiter.child.socket.write(
|
|
224
|
+
frame({ ok: true, lease: 'granted', tokens: granted, pool: made.pool.environment[CPU_POOL_ENV] }),
|
|
225
|
+
);
|
|
226
|
+
});
|
|
227
|
+
creating ??= [];
|
|
228
|
+
creating.push(creation);
|
|
229
|
+
}
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
const refuse = (child: Child, error: string): void => {
|
|
233
|
+
child.phase = { kind: 'done' };
|
|
234
|
+
child.socket.end(frame({ ok: false, code: 'invalid-request', error }));
|
|
235
|
+
};
|
|
236
|
+
|
|
237
|
+
const serve = (socket: Socket): void => {
|
|
238
|
+
if (closing) {
|
|
239
|
+
socket.destroy();
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
const child: Child = { socket, phase: { kind: 'reading' } };
|
|
243
|
+
children.add(socket);
|
|
244
|
+
// The child's own process reports a socket error. Accounting comes from `close`, the one event that
|
|
245
|
+
// reports every way a socket ends.
|
|
246
|
+
socket.on('error', () => socket.destroy());
|
|
247
|
+
socket.on('close', () => {
|
|
248
|
+
children.delete(socket);
|
|
249
|
+
if (child.phase.kind === 'queued') {
|
|
250
|
+
const at = queue.findIndex((waiter) => waiter.child === child);
|
|
251
|
+
if (at !== -1) {
|
|
252
|
+
queue.splice(at, 1);
|
|
253
|
+
}
|
|
254
|
+
} else if (child.phase.kind === 'holding') {
|
|
255
|
+
free += child.phase.tokens;
|
|
256
|
+
}
|
|
257
|
+
child.phase = { kind: 'done' };
|
|
258
|
+
drain();
|
|
259
|
+
});
|
|
260
|
+
let buffered: Buffer = EMPTY;
|
|
261
|
+
// Refusing releases what the connection buffered: a refused request holds no storage.
|
|
262
|
+
const rejectRequest = (error: string): void => {
|
|
263
|
+
buffered = EMPTY;
|
|
264
|
+
refuse(child, error);
|
|
265
|
+
};
|
|
266
|
+
socket.on('data', (chunk: Buffer) => {
|
|
267
|
+
// A refusal already ended the writable side. Discard remaining request bytes without resetting
|
|
268
|
+
// the socket: an RST here can erase the typed refusal before the peer reads it.
|
|
269
|
+
if (child.phase.kind === 'done') return;
|
|
270
|
+
if (child.phase.kind !== 'reading') {
|
|
271
|
+
// A child that writes past its one request has broken the protocol. Its grant or place in line
|
|
272
|
+
// ends with the connection, and `close` returns whatever it held.
|
|
273
|
+
socket.destroy();
|
|
274
|
+
return;
|
|
275
|
+
}
|
|
276
|
+
// Bound the total before copying: one request is at most MAX_REQUEST_BYTES and its newline, so a
|
|
277
|
+
// larger total is refused without copying any of it, however much one chunk carries.
|
|
278
|
+
if (buffered.length + chunk.length > MAX_REQUEST_BYTES + 1) {
|
|
279
|
+
rejectRequest(TOO_LONG);
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
// The first chunk is the buffer itself: a request that arrives whole is never copied.
|
|
283
|
+
buffered = buffered.length === 0 ? chunk : Buffer.concat([buffered, chunk]);
|
|
284
|
+
const newline = buffered.indexOf(0x0a);
|
|
285
|
+
const lineBytes = newline === -1 ? buffered.length : newline;
|
|
286
|
+
if (lineBytes > MAX_REQUEST_BYTES) {
|
|
287
|
+
rejectRequest(TOO_LONG);
|
|
288
|
+
return;
|
|
289
|
+
}
|
|
290
|
+
if (newline === -1) {
|
|
291
|
+
return;
|
|
292
|
+
}
|
|
293
|
+
if (newline !== buffered.length - 1) {
|
|
294
|
+
rejectRequest('a connection makes one request; this one sent more');
|
|
295
|
+
return;
|
|
296
|
+
}
|
|
297
|
+
const line = buffered.subarray(0, newline).toString('utf8');
|
|
298
|
+
buffered = EMPTY;
|
|
299
|
+
let request: CpuPoolRequest | null;
|
|
300
|
+
try {
|
|
301
|
+
request = parseRequest(line);
|
|
302
|
+
} catch (error) {
|
|
303
|
+
// isParse returns null for a wrong shape, but JSON syntax errors still throw.
|
|
304
|
+
if (!(error instanceof SyntaxError)) throw error;
|
|
305
|
+
request = null;
|
|
306
|
+
}
|
|
307
|
+
if (request === null) {
|
|
308
|
+
refuse(child, 'a request is {"op":"cpu-pool","want":<a whole number of tokens, at least 1>}');
|
|
309
|
+
return;
|
|
310
|
+
}
|
|
311
|
+
child.phase = { kind: 'queued' };
|
|
312
|
+
queue.push({ child, want: request.want, children: request.children === true });
|
|
313
|
+
socket.write(frame({ ok: true, lease: 'queued' }));
|
|
314
|
+
drain();
|
|
315
|
+
});
|
|
316
|
+
};
|
|
317
|
+
|
|
318
|
+
const server: Server = createServer(serve);
|
|
319
|
+
|
|
320
|
+
// Memoized: the first call does the teardown, every later call awaits its outcome. The outcome
|
|
321
|
+
// collects every cleanup failure as text, so the teardown itself never rejects.
|
|
322
|
+
const shutdown = (): Promise<CloseOutcome> => {
|
|
323
|
+
shut ??= (async (): Promise<CloseOutcome> => {
|
|
324
|
+
closing = true;
|
|
325
|
+
await started;
|
|
326
|
+
for (const socket of children) {
|
|
327
|
+
socket.destroy();
|
|
328
|
+
}
|
|
329
|
+
if (listening) {
|
|
330
|
+
await new Promise<void>((done) => {
|
|
331
|
+
server.close((error) => {
|
|
332
|
+
if (error) {
|
|
333
|
+
failures.push(`its socket did not close: ${error.message}`);
|
|
334
|
+
}
|
|
335
|
+
done();
|
|
336
|
+
});
|
|
337
|
+
});
|
|
338
|
+
}
|
|
339
|
+
if (creating !== undefined) await Promise.all(creating);
|
|
340
|
+
if (subpools !== undefined) {
|
|
341
|
+
for (const pool of subpools) {
|
|
342
|
+
const outcome = await pool.close();
|
|
343
|
+
if (!outcome.ok) failures.push(outcome.error);
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
try {
|
|
347
|
+
await rm(directory, { recursive: true, force: true });
|
|
348
|
+
} catch (error) {
|
|
349
|
+
failures.push(`its directory was not removed: ${error instanceof Error ? error.message : String(error)}`);
|
|
350
|
+
}
|
|
351
|
+
return failures.length === 0
|
|
352
|
+
? { ok: true }
|
|
353
|
+
: { ok: false, error: `the CPU pool at ${socketPath} did not close cleanly: ${failures.join('; ')}` };
|
|
354
|
+
})();
|
|
355
|
+
return shut;
|
|
356
|
+
};
|
|
357
|
+
|
|
358
|
+
// Upstream loss and a server failure close the pool with no caller to return to. A failed close is
|
|
359
|
+
// written to stderr and stays cached for the owner's own `close()`; the callback never rejects.
|
|
360
|
+
const closeInBackground = (): void => {
|
|
361
|
+
void shutdown().then((outcome) => {
|
|
362
|
+
if (!outcome.ok) {
|
|
363
|
+
process.stderr.write(`bounded-exec: ${outcome.error}\n`);
|
|
364
|
+
}
|
|
365
|
+
});
|
|
366
|
+
};
|
|
367
|
+
|
|
368
|
+
void closed.then(closeInBackground);
|
|
369
|
+
|
|
370
|
+
server.on('error', (error) => {
|
|
371
|
+
if (!listening) {
|
|
372
|
+
resolveStarted(error);
|
|
373
|
+
return;
|
|
374
|
+
}
|
|
375
|
+
// The failure joins the pool's outcome, so the owner's close reports it as an ordinary failure.
|
|
376
|
+
failures.push(`it failed while listening: ${error.message}`);
|
|
377
|
+
process.stderr.write(`bounded-exec: the CPU pool at ${socketPath} failed: ${error.message}\n`);
|
|
378
|
+
closeInBackground();
|
|
379
|
+
});
|
|
380
|
+
|
|
381
|
+
server.listen(socketPath, () => {
|
|
382
|
+
listening = true;
|
|
383
|
+
resolveStarted(null);
|
|
384
|
+
});
|
|
385
|
+
|
|
386
|
+
const abort = async (reason: string): Promise<{ ok: false; error: string }> => {
|
|
387
|
+
const outcome = await shutdown();
|
|
388
|
+
return { ok: false, error: outcome.ok ? reason : `${reason}; ${outcome.error}` };
|
|
389
|
+
};
|
|
390
|
+
|
|
391
|
+
const failure = await started;
|
|
392
|
+
if (failure !== null) {
|
|
393
|
+
return abort(`the CPU pool could not listen at ${socketPath}: ${failure.message}`);
|
|
394
|
+
}
|
|
395
|
+
if (closing) {
|
|
396
|
+
return abort(CLOSED_WHILE_MADE);
|
|
397
|
+
}
|
|
398
|
+
try {
|
|
399
|
+
// Until this runs the socket carries the umask's mode, which is safe: the 0700 directory above it
|
|
400
|
+
// denies every other user the path.
|
|
401
|
+
await chmod(socketPath, 0o600);
|
|
402
|
+
} catch (error) {
|
|
403
|
+
return abort(
|
|
404
|
+
`the CPU pool's socket could not be made private: ${error instanceof Error ? error.message : String(error)}`,
|
|
405
|
+
);
|
|
406
|
+
}
|
|
407
|
+
if (closing) {
|
|
408
|
+
return abort(CLOSED_WHILE_MADE);
|
|
409
|
+
}
|
|
410
|
+
return {
|
|
411
|
+
ok: true,
|
|
412
|
+
pool: {
|
|
413
|
+
environment: { [CPU_POOL_ENV]: socketPath, [CPU_TOKENS_ENV]: String(tokens) },
|
|
414
|
+
close: shutdown,
|
|
415
|
+
},
|
|
416
|
+
};
|
|
417
|
+
}
|
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
import { afterEach, describe, expect, it } from 'bun:test';
|
|
2
|
+
import { rmSync } from 'node:fs';
|
|
3
|
+
import { createServer, type Server, type Socket } from 'node:net';
|
|
4
|
+
import { join } from 'node:path';
|
|
5
|
+
import { ownedSocketDirectory } from '../../testing.js';
|
|
6
|
+
import { CPU_TOKENS_ENV, gatewayCpuBudget, parseInheritedTokens, runnerDemand, sizeRunner } from './cpu-tokens.js';
|
|
7
|
+
|
|
8
|
+
const servers: Server[] = [];
|
|
9
|
+
const directories: string[] = [];
|
|
10
|
+
|
|
11
|
+
afterEach(() => {
|
|
12
|
+
for (const server of servers.splice(0)) {
|
|
13
|
+
server.close();
|
|
14
|
+
}
|
|
15
|
+
for (const directory of directories.splice(0)) {
|
|
16
|
+
rmSync(directory, { recursive: true, force: true });
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* A socket path in a directory of its own, made in the socket namespace the sandbox admits for Unix
|
|
22
|
+
* sockets: a literal `/tmp` path binds, but no sandboxed process may connect to it, so a client of it
|
|
23
|
+
* would see a refusal in place of the absent socket or the gateway the test means.
|
|
24
|
+
*/
|
|
25
|
+
async function socketPath(name: string): Promise<string> {
|
|
26
|
+
const directory = await ownedSocketDirectory('cpu-', name);
|
|
27
|
+
directories.push(directory);
|
|
28
|
+
return join(directory, name);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** A gateway stand-in on a socket in a directory of its own that runs `serve` per client. */
|
|
32
|
+
async function gateway(serve: (client: Socket) => void): Promise<string> {
|
|
33
|
+
const socket = await socketPath('gw.sock');
|
|
34
|
+
const server = createServer({ allowHalfOpen: true }, serve);
|
|
35
|
+
const { promise, resolve, reject } = Promise.withResolvers<void>();
|
|
36
|
+
server.once('error', reject);
|
|
37
|
+
server.listen(socket, resolve);
|
|
38
|
+
await promise;
|
|
39
|
+
server.off('error', reject);
|
|
40
|
+
servers.push(server);
|
|
41
|
+
return socket;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** The runner command the plugin infers for a nextest piece, as `index.ts` builds it. */
|
|
45
|
+
const NEXTEST_PIECE =
|
|
46
|
+
`extracted="$(node node_modules/@smoothbricks/nx-plugin/dist/bin/smoo-nx-nextest-extract.js .cache/nextest/archive.tar.zst)" && ` +
|
|
47
|
+
`cargo --frozen nextest run --binaries-metadata "$extracted/m.json" --workspace-remap . -E 'package(core)' --partition hash:1/5 --no-tests=pass`;
|
|
48
|
+
|
|
49
|
+
describe('cowshed CPU budget client', () => {
|
|
50
|
+
it('asks for what each runner would run at once', () => {
|
|
51
|
+
expect(runnerDemand(NEXTEST_PIECE, undefined, 18, {})).toEqual({ kind: 'nextest', want: 18 });
|
|
52
|
+
expect(runnerDemand('cargo-nextest nextest run --archive-file a.tar.zst', undefined, 12, {})).toEqual({
|
|
53
|
+
kind: 'nextest',
|
|
54
|
+
want: 12,
|
|
55
|
+
});
|
|
56
|
+
expect(runnerDemand('cargo nextest run --test-threads=4', undefined, 18, {})).toEqual({ kind: 'nextest', want: 4 });
|
|
57
|
+
expect(runnerDemand('bun test --timeout=30000 --parallel=3', undefined, 18, {})).toEqual({
|
|
58
|
+
kind: 'bun-parallel',
|
|
59
|
+
want: 3,
|
|
60
|
+
});
|
|
61
|
+
expect(runnerDemand('bun test --parallel ./a.test.ts', undefined, 18, {})).toEqual({
|
|
62
|
+
kind: 'bun-parallel',
|
|
63
|
+
want: 18,
|
|
64
|
+
});
|
|
65
|
+
// A runtime pinned by path is the same runner.
|
|
66
|
+
expect(runnerDemand('../bun-runtime/.runtime/bun test --parallel=5', undefined, 18, {})).toEqual({
|
|
67
|
+
kind: 'bun-parallel',
|
|
68
|
+
want: 5,
|
|
69
|
+
});
|
|
70
|
+
// One bun process: `test.concurrent` inside it is not another CPU.
|
|
71
|
+
expect(runnerDemand('bun test --timeout=30000 native.test.ts', undefined, 18, {})).toEqual({
|
|
72
|
+
kind: 'process',
|
|
73
|
+
want: 1,
|
|
74
|
+
});
|
|
75
|
+
expect(runnerDemand('cargo --frozen test --workspace', undefined, 18, {})).toEqual({
|
|
76
|
+
kind: 'cargo',
|
|
77
|
+
want: 18,
|
|
78
|
+
testThreads: null,
|
|
79
|
+
});
|
|
80
|
+
expect(runnerDemand('napi build --platform --release', undefined, 8, {})).toEqual({
|
|
81
|
+
kind: 'cargo',
|
|
82
|
+
want: 8,
|
|
83
|
+
testThreads: null,
|
|
84
|
+
});
|
|
85
|
+
expect(runnerDemand('bun scripts/test-shard.ts 3', undefined, 18, {})).toEqual({ kind: 'process', want: 1 });
|
|
86
|
+
// A declared parallelism outranks what the command names, within inherited ceilings.
|
|
87
|
+
expect(runnerDemand('bun scripts/test-shard.ts 3', 2, 18, {})).toEqual({ kind: 'process', want: 2 });
|
|
88
|
+
expect(runnerDemand('cargo nextest run --test-threads=4', 6, 18, {})).toEqual({ kind: 'nextest', want: 6 });
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it('sizes each runner to its grant', () => {
|
|
92
|
+
expect(sizeRunner({ kind: 'nextest', want: 18 }, NEXTEST_PIECE, 5)).toEqual({
|
|
93
|
+
command: NEXTEST_PIECE,
|
|
94
|
+
env: { [CPU_TOKENS_ENV]: '5', NEXTEST_TEST_THREADS: '5' },
|
|
95
|
+
});
|
|
96
|
+
expect(sizeRunner({ kind: 'nextest', want: 16 }, 'cargo nextest run --test-threads 16 -E all()', 3).command).toBe(
|
|
97
|
+
'cargo nextest run --test-threads=3 -E all()',
|
|
98
|
+
);
|
|
99
|
+
expect(sizeRunner({ kind: 'bun-parallel', want: 3 }, 'bun test --parallel=3 --timeout=1', 2)).toEqual({
|
|
100
|
+
command: 'bun test --parallel=2 --timeout=1',
|
|
101
|
+
env: { [CPU_TOKENS_ENV]: '2' },
|
|
102
|
+
});
|
|
103
|
+
expect(sizeRunner({ kind: 'bun-parallel', want: 18 }, 'bun test --parallel', 7).command).toBe(
|
|
104
|
+
'bun test --parallel=7',
|
|
105
|
+
);
|
|
106
|
+
expect(sizeRunner({ kind: 'cargo', want: 18, testThreads: null }, 'cargo test', 4).env).toEqual({
|
|
107
|
+
[CPU_TOKENS_ENV]: '4',
|
|
108
|
+
CARGO_BUILD_JOBS: '4',
|
|
109
|
+
RUST_TEST_THREADS: '4',
|
|
110
|
+
});
|
|
111
|
+
expect(sizeRunner({ kind: 'process', want: 1 }, 'bun scripts/x.ts', 1)).toEqual({
|
|
112
|
+
command: 'bun scripts/x.ts',
|
|
113
|
+
env: { [CPU_TOKENS_ENV]: '1' },
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it('treats inherited runner bounds as ceilings even under larger grants', () => {
|
|
118
|
+
const inherited = { NEXTEST_TEST_THREADS: '6', CARGO_BUILD_JOBS: '4', RUST_TEST_THREADS: '2' };
|
|
119
|
+
const configured = { NEXTEST_TEST_THREADS: '64', CARGO_BUILD_JOBS: '64', RUST_TEST_THREADS: '64' };
|
|
120
|
+
const nextest = runnerDemand('cargo nextest run --test-threads=18', 18, 18, inherited, configured);
|
|
121
|
+
const cargo = runnerDemand('cargo test', 18, 18, inherited, configured);
|
|
122
|
+
expect(nextest).toEqual({ kind: 'nextest', want: 6 });
|
|
123
|
+
expect(cargo).toEqual({ kind: 'cargo', want: 4, testThreads: 2 });
|
|
124
|
+
for (const tokens of [15, 18]) {
|
|
125
|
+
expect(sizeRunner(nextest, 'cargo nextest run --test-threads=18', tokens)).toEqual({
|
|
126
|
+
command: 'cargo nextest run --test-threads=6',
|
|
127
|
+
env: { [CPU_TOKENS_ENV]: String(tokens), NEXTEST_TEST_THREADS: '6' },
|
|
128
|
+
});
|
|
129
|
+
expect(sizeRunner(cargo, 'cargo test', tokens).env).toEqual({
|
|
130
|
+
[CPU_TOKENS_ENV]: String(tokens),
|
|
131
|
+
CARGO_BUILD_JOBS: '4',
|
|
132
|
+
RUST_TEST_THREADS: '2',
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
expect(sizeRunner(nextest, NEXTEST_PIECE, 3).env.NEXTEST_TEST_THREADS).toBe('3');
|
|
136
|
+
expect(sizeRunner(cargo, 'cargo test', 3).env).toEqual({
|
|
137
|
+
[CPU_TOKENS_ENV]: '3',
|
|
138
|
+
CARGO_BUILD_JOBS: '3',
|
|
139
|
+
RUST_TEST_THREADS: '2',
|
|
140
|
+
});
|
|
141
|
+
expect(runnerDemand(NEXTEST_PIECE, 18, 18, inherited, { NEXTEST_TEST_THREADS: '2' }).want).toBe(2);
|
|
142
|
+
expect(runnerDemand('cargo build', 18, 18, {}, { CARGO_BUILD_JOBS: '4' }).want).toBe(4);
|
|
143
|
+
expect(runnerDemand(NEXTEST_PIECE, 18, 18, { NEXTEST_TEST_THREADS: '0' }).want).toBe(18);
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
it('reads an inherited count only as a positive safe integer', () => {
|
|
147
|
+
expect(parseInheritedTokens(undefined)).toBeNull();
|
|
148
|
+
for (const invalid of ['', '0', '-1', '1.5', '2e3', '0x10', ' 3', '3 ', '03', 'three', '9007199254740993']) {
|
|
149
|
+
expect(parseInheritedTokens(invalid)).toBeNull();
|
|
150
|
+
}
|
|
151
|
+
expect(parseInheritedTokens('1')).toBe(1);
|
|
152
|
+
expect(parseInheritedTokens('16')).toBe(16);
|
|
153
|
+
expect(parseInheritedTokens(String(Number.MAX_SAFE_INTEGER))).toBe(Number.MAX_SAFE_INTEGER);
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
it('reads back the count a runner was sized to', () => {
|
|
157
|
+
expect(
|
|
158
|
+
parseInheritedTokens(sizeRunner({ kind: 'process', want: 5 }, 'bun scripts/x.ts', 5).env[CPU_TOKENS_ENV]),
|
|
159
|
+
).toBe(5);
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
it('asks the gateway with one line, takes the granted count, and holds until released', async () => {
|
|
163
|
+
const { promise: request, resolve: requested } = Promise.withResolvers<string>();
|
|
164
|
+
const { promise: closed, resolve: close } = Promise.withResolvers<void>();
|
|
165
|
+
const socket = await gateway((client) => {
|
|
166
|
+
client.once('data', (chunk) => {
|
|
167
|
+
requested(chunk.toString('utf8'));
|
|
168
|
+
client.write('{"ok":true,"lease":"queued"}\n{"ok":true,"lease":"granted","tokens":6}\n');
|
|
169
|
+
});
|
|
170
|
+
// The gateway takes a lease back when it reads EOF. With allowHalfOpen the socket stays open
|
|
171
|
+
// after the peer closes until the server ends it, so 'close' is not that signal on Linux.
|
|
172
|
+
client.on('end', () => close());
|
|
173
|
+
});
|
|
174
|
+
const grant = await gatewayCpuBudget(socket).take(18, '/w/one', 'cargo nextest run');
|
|
175
|
+
expect(JSON.parse(await request)).toEqual({
|
|
176
|
+
op: 'cpu-tokens',
|
|
177
|
+
want: 18,
|
|
178
|
+
checkout: '/w/one',
|
|
179
|
+
command: 'cargo nextest run',
|
|
180
|
+
});
|
|
181
|
+
expect(grant.granted && grant.tokens).toBe(6);
|
|
182
|
+
if (grant.granted) {
|
|
183
|
+
grant.release();
|
|
184
|
+
// The client's side of the connection really closed, not merely its release call.
|
|
185
|
+
await grant.closed;
|
|
186
|
+
}
|
|
187
|
+
await closed;
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
it('closes a granted lease when the gateway ends the connection', async () => {
|
|
191
|
+
const socket = await gateway((client) => {
|
|
192
|
+
client.once('data', () => {
|
|
193
|
+
client.write('{"ok":true,"lease":"queued"}\n{"ok":true,"lease":"granted","tokens":2}\n');
|
|
194
|
+
client.end();
|
|
195
|
+
});
|
|
196
|
+
});
|
|
197
|
+
const grant = await gatewayCpuBudget(socket).take(2, '/w', 'x');
|
|
198
|
+
if (!grant.granted) {
|
|
199
|
+
throw new Error(grant.reason);
|
|
200
|
+
}
|
|
201
|
+
await grant.closed;
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
it('runs unbudgeted, said as absent, when no gateway listens', async () => {
|
|
205
|
+
const grant = await gatewayCpuBudget(await socketPath('none.sock')).take(4, '/w', 'x');
|
|
206
|
+
expect(grant.granted === false && grant.cause).toBe('absent');
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
it('recognizes a gateway that does not know the operation as predating the budget', async () => {
|
|
210
|
+
const socket = await gateway((client) => {
|
|
211
|
+
client.once('data', () =>
|
|
212
|
+
client.end(
|
|
213
|
+
'{"ok":false,"code":"invalid-request","error":"gateway control encoding failed: unknown gateway control operation"}\n',
|
|
214
|
+
),
|
|
215
|
+
);
|
|
216
|
+
});
|
|
217
|
+
const grant = await gatewayCpuBudget(socket).take(4, '/w', 'x');
|
|
218
|
+
expect(grant).toEqual({
|
|
219
|
+
granted: false,
|
|
220
|
+
cause: 'predates',
|
|
221
|
+
reason:
|
|
222
|
+
'the gateway predates the CPU budget (invalid-request: gateway control encoding failed: unknown gateway control operation); restart it with `cowshed setup`',
|
|
223
|
+
});
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
it('refuses a grant larger than it asked for', async () => {
|
|
227
|
+
const socket = await gateway((client) => {
|
|
228
|
+
client.once('data', () =>
|
|
229
|
+
client.write('{"ok":true,"lease":"queued"}\n{"ok":true,"lease":"granted","tokens":9}\n'),
|
|
230
|
+
);
|
|
231
|
+
});
|
|
232
|
+
const grant = await gatewayCpuBudget(socket).take(4, '/w', 'x');
|
|
233
|
+
expect(grant).toEqual({ granted: false, cause: 'other', reason: 'the gateway granted 9 tokens for 4' });
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
it('returns a grant when the process holding it is killed mid-leg', async () => {
|
|
237
|
+
const { promise: closed, resolve: close } = Promise.withResolvers<void>();
|
|
238
|
+
const { promise: granted, resolve: grant } = Promise.withResolvers<void>();
|
|
239
|
+
const socket = await gateway((client) => {
|
|
240
|
+
client.once('data', () => {
|
|
241
|
+
client.write('{"ok":true,"lease":"queued"}\n{"ok":true,"lease":"granted","tokens":2}\n');
|
|
242
|
+
grant();
|
|
243
|
+
});
|
|
244
|
+
client.on('end', () => close());
|
|
245
|
+
});
|
|
246
|
+
// The holder is its own process so SIGKILL is a real death. Its script imports the module by
|
|
247
|
+
// path because `bun -e` has no module of its own to import from statically; the held lease's
|
|
248
|
+
// open socket is what keeps it alive.
|
|
249
|
+
const holder = Bun.spawn(
|
|
250
|
+
[
|
|
251
|
+
process.execPath,
|
|
252
|
+
'-e',
|
|
253
|
+
`const { gatewayCpuBudget } = await import(${JSON.stringify(new URL('./cpu-tokens.ts', import.meta.url).pathname)});
|
|
254
|
+
const grant = await gatewayCpuBudget(${JSON.stringify(socket)}).take(2, '/w', 'leg');
|
|
255
|
+
if (!grant.granted) process.exit(3);
|
|
256
|
+
console.log('holding');`,
|
|
257
|
+
],
|
|
258
|
+
{ stdout: 'pipe', stderr: 'inherit' },
|
|
259
|
+
);
|
|
260
|
+
await granted;
|
|
261
|
+
const reader = holder.stdout.getReader();
|
|
262
|
+
const { value } = await reader.read();
|
|
263
|
+
expect(new TextDecoder().decode(value)).toContain('holding');
|
|
264
|
+
holder.kill('SIGKILL');
|
|
265
|
+
await holder.exited;
|
|
266
|
+
// The gateway sees the connection close, which is how it takes the tokens back.
|
|
267
|
+
await closed;
|
|
268
|
+
});
|
|
269
|
+
});
|