Repository navigation
Description
Summary
readConfig()'s default for SH_WORKERS is defaultWorkers(cpus().length) (packages/supervisor/src/config.ts:90, cpus imported from node:os at :1). os.cpus() enumerates the host's physical CPU list and does not consult cgroup CPU quotas. Under a container or pod with a CPU limit smaller than the host (docker run --cpus=2, a Kubernetes CPU limit, any cgroup-quota-based cap), SH_WORKERS silently defaults to the host's core count, not the deployment's actual allocation.
This is not hypothetical for this repo. P6's whole thesis is measuring density against a fixed worker pool size W (P6 spec §5.1; E8/E9 — rossoctl/moca-experiments#11, rossoctl/moca-experiments#12). If W silently tracks host cores instead of the container's real CPU allocation, any density number gathered in a CPU-limited container describes the host, not the deployment being measured — and an operator who deploys P6 in a CPU-limited container gets an oversized, thrashing worker pool with no error and no warning. That's the same failure shape defaultWorkers's own docstring already worries about for the os.cpus() === [] case (:52-59), just from the opposite direction (too many, not too few).
The env template already documents the current (buggy) behavior as fact: deploy/vm/env/supervisor.env.example says "SH_WORKERS defaults to os.cpus().length" — that comment needs to change with the fix.
Current behavior (verified in this tree)
packages/supervisor/src/config.ts:90:workers: readInt(env, 'SH_WORKERS', defaultWorkers(cpus().length), { min: 1 })packages/supervisor/src/config.ts:61-63:defaultWorkersonly clamps its input to≥ 1— it has no opinion about where the count comes from, so the fix is entirely at the call site, not indefaultWorkersitselfpackages/supervisor/test/config.test.ts:12: the existing default-value test assertsc.workers === defaultWorkers(cpus().length)— it currently encodes the buggy source as the expected behavior and must change with the fix- Confirmed in this environment (Node v25.2.1, pinned in this repo):
node:os'savailableParallelism()exists (added Node 19.4/20) and is backed by libuv'suv_available_parallelism(), which — unlikeuv_cpu_info()(os.cpus()'s backing call) — checkscpu.max/cpu.cfs_quota_usunder cgroup v2/v1 on Linux and clamps to it
Fix
Swap the call site's source from cpus().length to availableParallelism() (same node:os import). No change to defaultWorkers's signature or clamping behavior — it stays a pure function of a count, so its existing 0/1/8 clamp tests (:62-64) are untouched.
Test plan (write these first)
packages/supervisor/test/config.test.ts:vi.mock('node:os', ...)soavailableParallelism()andcpus()return different values, then assertreadConfig({...}).workers === defaultWorkers(<availableParallelism mock value>)and explicitly!== defaultWorkers(<cpus().length mock value>). This fails against current code (which usescpus().length) and passes once the call site switches — the two mocked values must differ, or the test would pass vacuously without the fix.- Update the pre-existing assertion at
:12to useavailableParallelism()instead ofcpus().length. defaultWorkers's own unit tests (:62-64) need no change — its contract (clamp to≥ 1) is unchanged.- Update the comment in
deploy/vm/env/supervisor.env.example("SH_WORKERSdefaults toos.cpus().length") to describe the new source.
Acceptance
-
SH_WORKERS's default is computed from a cgroup-aware source (availableParallelism()), notcpus().length - A test fails on pre-fix code and passes on post-fix code via two mocked sources that differ (not a vacuous pass)
-
defaultWorkers's existing clamp behavior and tests are untouched - No behavior change when
SH_WORKERSis set explicitly by an operator — this only touches the unset-default path -
deploy/vm/env/supervisor.env.example's comment updated to match
Out of scope
- Verifying against a real CPU-limited container (
docker run --cpus=N) — worth doing once a Docker Compose deployment for P6 lands (companion issue, filed alongside this one), but the mocked unit test above is sufficient to fix and pin this bug today. - Any other config knob (
SH_TURNS_PER_WORKER, ports, routing policy, etc.) — untouched.
Related: P6 spec (docs/specs/2026-09-08-p6-vm-process-manager-design.md), ADR-0034, rossoctl/moca-experiments#11 (E8), rossoctl/moca-experiments#12 (E9).
Activity
Merge pull request #343 from rossoctl/fix/341-sh-workers-cgroup-aware
fix(supervisor): default W from availableParallelism(), not the host'…