Skip to content

Commit 34735a6

Browse files
committed
dx(test): opt-in worker-count override for solo local vitest runs
resolveVitestMaxWorkers() caps local runs at 2 workers so parallel worktrees and spawn-heavy tests keep headroom, but a solo run that owns the machine pays 6x on a 12-core host for no benefit. Add AGENT_DEVICE_VITEST_MAX_WORKERS to opt in to a higher cap. It is clamped to os.cpus().length so a runaway value can't oversubscribe the host, and it is a no-op in CI (CI already derives its own worker count). A missing, blank, non-numeric, non-integer, or non-positive value falls through to the existing default cap rather than throwing. Default (unset) behavior is unchanged. Closes #1962
1 parent 7aaa559 commit 34735a6

4 files changed

Lines changed: 89 additions & 1 deletion

File tree

‎docs/agents/testing.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,3 +180,6 @@ There is no unit-test retry layer—fix or remove flakes.
180180
- Keep Vitest isolation enabled and the pool on forks. Both alternatives were measured and did not
181181
improve the suite; importing the module under test rather than a platform barrel is the useful
182182
optimization.
183+
- A solo local run that owns the machine can raise the default two-worker cap with
184+
`AGENT_DEVICE_VITEST_MAX_WORKERS=<n>`; it is clamped to the host's CPU count and ignored in CI,
185+
which derives its own count.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import os from 'node:os';
2+
import assert from 'node:assert/strict';
3+
import { test } from 'vitest';
4+
import {
5+
DEFAULT_VITEST_MAX_WORKERS,
6+
resolveVitestMaxWorkers,
7+
VITEST_MAX_WORKERS_OVERRIDE_ENV,
8+
} from './vitest-concurrency.ts';
9+
10+
test('an unset override preserves the existing default local cap', () => {
11+
assert.equal(resolveVitestMaxWorkers({}), DEFAULT_VITEST_MAX_WORKERS);
12+
});
13+
14+
test('CI ignores the override even when both signals are present', () => {
15+
assert.equal(
16+
resolveVitestMaxWorkers({ CI: 'true', [VITEST_MAX_WORKERS_OVERRIDE_ENV]: '8' }),
17+
undefined,
18+
);
19+
});
20+
21+
test('a valid override below the core count is honored as-is', () => {
22+
// Below any real host's core count, so the resolver takes the override
23+
// branch rather than clamping it back down.
24+
assert.equal(resolveVitestMaxWorkers({ [VITEST_MAX_WORKERS_OVERRIDE_ENV]: '1' }), 1);
25+
});
26+
27+
test('an override above the available core count is clamped down to it', () => {
28+
assert.equal(
29+
resolveVitestMaxWorkers({ [VITEST_MAX_WORKERS_OVERRIDE_ENV]: '999' }),
30+
os.cpus().length,
31+
);
32+
});
33+
34+
test('a non-numeric override falls through to the default cap', () => {
35+
assert.equal(
36+
resolveVitestMaxWorkers({ [VITEST_MAX_WORKERS_OVERRIDE_ENV]: 'not-a-number' }),
37+
DEFAULT_VITEST_MAX_WORKERS,
38+
);
39+
});
40+
41+
test('a zero or negative override falls through to the default cap', () => {
42+
assert.equal(
43+
resolveVitestMaxWorkers({ [VITEST_MAX_WORKERS_OVERRIDE_ENV]: '0' }),
44+
DEFAULT_VITEST_MAX_WORKERS,
45+
);
46+
assert.equal(
47+
resolveVitestMaxWorkers({ [VITEST_MAX_WORKERS_OVERRIDE_ENV]: '-4' }),
48+
DEFAULT_VITEST_MAX_WORKERS,
49+
);
50+
});
51+
52+
test('a non-integer override falls through to the default cap', () => {
53+
assert.equal(
54+
resolveVitestMaxWorkers({ [VITEST_MAX_WORKERS_OVERRIDE_ENV]: '2.5' }),
55+
DEFAULT_VITEST_MAX_WORKERS,
56+
);
57+
});

‎scripts/lib/vitest-concurrency.ts‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,35 @@
1+
import os from 'node:os';
2+
13
/**
24
* Keep one Vitest invocation modest enough to coexist with two other Codex
35
* worktrees on a 12-core development host: 3 agents + (3 suites * 2 workers)
46
* leaves roughly 3 cores for runners, subprocesses, simulators, and the OS.
57
*/
68
export const DEFAULT_VITEST_MAX_WORKERS = 2;
79

10+
/**
11+
* Opt-in escape hatch for a solo local run that owns the whole machine (see
12+
* docs/agents/testing.md). Ignored in CI, which already derives its own
13+
* worker count from the isolated runner's CPU pool.
14+
*/
15+
export const VITEST_MAX_WORKERS_OVERRIDE_ENV = 'AGENT_DEVICE_VITEST_MAX_WORKERS';
16+
817
export function resolveVitestMaxWorkers(env: NodeJS.ProcessEnv = process.env): number | undefined {
9-
return env.CI === 'true' ? undefined : DEFAULT_VITEST_MAX_WORKERS;
18+
if (env.CI === 'true') return undefined;
19+
20+
const override = parsePositiveInt(env[VITEST_MAX_WORKERS_OVERRIDE_ENV]);
21+
// Clamp rather than trust the override literally: a typo like `999` must not
22+
// oversubscribe the host the way the default cap above exists to prevent.
23+
if (override !== undefined) return Math.min(override, os.cpus().length);
24+
25+
return DEFAULT_VITEST_MAX_WORKERS;
26+
}
27+
28+
// A missing, blank, non-numeric, non-integer, or non-positive value falls
29+
// through to the default cap instead of throwing or coercing to something
30+
// surprising (e.g. `Number('')` is 0, not NaN).
31+
function parsePositiveInt(value: string | undefined): number | undefined {
32+
if (value === undefined || value.trim() === '') return undefined;
33+
const parsed = Number(value);
34+
return Number.isInteger(parsed) && parsed > 0 ? parsed : undefined;
1035
}

‎vitest.config.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ export default defineConfig({
6161
include: [
6262
'src/**/*.test.ts',
6363
'packages/*/src/**/*.test.ts',
64+
// Pure resolver logic (no subprocess, no device), imported by this
65+
// config itself and by scripts/check-affected — belongs in the fast lane.
66+
'scripts/lib/vitest-concurrency.test.ts',
6467
// The validation fuzz generators' expectation gates (#1781 B2): in-process, no
6568
// subprocess or worker, so they ride the fast lane unlike their serialized siblings.
6669
'scripts/fuzz/validation-arbitraries.test.ts',

0 commit comments

Comments
 (0)