Skip to content

Commit f91453c

Browse files
committed
Merge remote-tracking branch 'origin/main' into claude/review-issues-1431-1429-xpehmi
2 parents a1a5f61 + b747069 commit f91453c

8 files changed

Lines changed: 87 additions & 12 deletions

File tree

‎scripts/check-affected/run.test.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import os from 'node:os';
99
import path from 'node:path';
1010
import { test } from 'node:test';
1111
import { runCmdSync } from '../../src/utils/exec.ts';
12+
import { DEFAULT_VITEST_MAX_WORKERS } from '../lib/vitest-concurrency.ts';
1213
import { selectChecks } from './model.ts';
1314
import { type CommandExecutor, readChangedFiles, runChecks } from './run.ts';
1415

@@ -184,7 +185,7 @@ test('runChecks combines related tests with lightweight changed-line coverage',
184185
assert.equal(related.length, 1);
185186
assert.ok(related[0]?.includes('--coverage'));
186187
assert.ok(related[0]?.includes('--coverage.reporter=lcov'));
187-
assert.ok(related[0]?.includes('--maxWorkers=4'));
188+
assert.ok(related[0]?.includes(`--maxWorkers=${DEFAULT_VITEST_MAX_WORKERS}`));
188189
assert.ok(
189190
executed.findIndex((command) => command.includes('test:integration:node')) <
190191
executed.findIndex((command) => command.includes('related')),

‎scripts/check-affected/run.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import path from 'node:path';
1010
import { pathToFileURL } from 'node:url';
1111
import { runCmdStreaming, runCmdSync } from '../../src/utils/exec.ts';
1212
import { parseScriptArgs } from '../lib/cli-args.ts';
13+
import { DEFAULT_VITEST_MAX_WORKERS } from '../lib/vitest-concurrency.ts';
1314
import {
1415
assertCatalogComplete,
1516
CHECK_CATALOG,
@@ -243,7 +244,7 @@ function resolveAffectedCoverageCommands(
243244
// starve otherwise-green subprocess/provider tests past their exact
244245
// timeout budgets. Bound this aggregate feedback lane without changing
245246
// the suites' own timeout or serialization contracts.
246-
'--maxWorkers=4',
247+
`--maxWorkers=${DEFAULT_VITEST_MAX_WORKERS}`,
247248
'--coverage',
248249
'--coverage.reporter=lcov',
249250
'--coverage.thresholds.statements=0',

‎scripts/coverage-changed/model.test.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,14 @@ test('source/test classification matches the coverage include glob', () => {
8989
assert.equal(isIncludableSource('src/core/__tests__/x.ts'), false);
9090
assert.equal(isIncludableSource('scripts/y.ts'), false);
9191
assert.equal(isIncludableSource('src/core/x.tsx'), false);
92+
// Workspace packages are the second coverage root.
93+
assert.equal(isIncludableSource('packages/contracts/src/x.ts'), true);
94+
assert.equal(isIncludableSource('packages/contracts/src/deep/x.ts'), true);
95+
assert.equal(isIncludableSource('packages/contracts/src/x.test.ts'), false);
96+
assert.equal(isIncludableSource('packages/contracts/src/__tests__/x.ts'), false);
97+
assert.equal(isIncludableSource('packages/contracts/src/x.tsx'), false);
98+
assert.equal(isIncludableSource('packages/contracts/test/x.ts'), false);
99+
assert.equal(isIncludableSource('packages/contracts/x.ts'), false);
92100
assert.equal(isTestFile('src/a.test.ts'), true);
93101
assert.equal(isTestFile('src/__tests__/a.ts'), true);
94102
assert.equal(isTestFile('src/a.ts'), false);
@@ -147,6 +155,23 @@ test('gate fails when changed-line coverage is below threshold and lists offende
147155
assert.equal(result.branch.covered, 1);
148156
});
149157

158+
test('workspace package source counts toward the gate like root source', () => {
159+
const packageCoverage = parseLcov(
160+
['SF:packages/contracts/src/feature.ts', 'DA:1,4', 'DA:2,0', 'end_of_record'].join('\n'),
161+
);
162+
const result = computeChangedCoverage({
163+
diffs: [diff('packages/contracts/src/feature.ts', [1, 2])],
164+
coverage: packageCoverage,
165+
fileLines: () => null,
166+
});
167+
// 1/2 = 50% < 70: a package change can now fail the gate on its own.
168+
assert.equal(result.totalLines, 2);
169+
assert.equal(result.coveredLines, 1);
170+
assert.equal(result.passed, false);
171+
assert.equal(result.offenders[0]?.path, 'packages/contracts/src/feature.ts');
172+
assert.deepEqual(result.offenders[0]?.uncoveredLines, [2]);
173+
});
174+
150175
test('non-executable added lines (absent from DA) never enter the denominator', () => {
151176
const result = computeChangedCoverage({
152177
diffs: [diff('src/covered.ts', [1, 3, 99])],

‎scripts/coverage-changed/model.ts‎

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
// the gate is deterministic against renames and deletes. run.ts owns all I/O.
55
//
66
// The coverable universe is the lcov report itself, not a second copy of
7-
// vitest's exclude globs: coverage runs with `all` on, so every includable
8-
// `src/**/*.ts` file appears in lcov even when untested. A changed includable
9-
// file ABSENT from lcov was dropped by an exclude glob — reported (non-gating)
10-
// so exclusions cannot silently absorb new logic.
7+
// vitest's exclude globs: coverage runs with `all` on, so every includable file
8+
// under either coverage root (`src/**/*.ts` and `packages/*/src/**/*.ts`)
9+
// appears in lcov even when untested. A changed includable file ABSENT from
10+
// lcov was dropped by an exclude glob — reported (non-gating) so exclusions
11+
// cannot silently absorb new logic.
1112

1213
// Single source of truth for the gate. Changed-line coverage below this
1314
// percentage fails the Coverage CI job (unless waived).
@@ -149,15 +150,20 @@ export function parseUnifiedDiff(diff: string): ChangedFileDiff[] {
149150
return files;
150151
}
151152

152-
// Vitest coverage `include` is `src/**/*.ts`. Test files are excluded there and
153-
// carry no product logic, so they never count toward the gate or the excluded
154-
// tally (which exists to surface hidden logic, not test code).
153+
// Vitest coverage `include` spans two roots: `src/**/*.ts` and
154+
// `packages/*/src/**/*.ts`. Test files are excluded there and carry no product
155+
// logic, so they never count toward the gate or the excluded tally (which
156+
// exists to surface hidden logic, not test code).
155157
export function isTestFile(path: string): boolean {
156158
return /\.test\.ts$/.test(path) || /(^|\/)__tests__\//.test(path);
157159
}
158160

161+
// Both coverage roots, package-generic so a new workspace needs no edit here.
162+
// Anything outside them — scripts, package-level `test/`, `.tsx` — stays out.
163+
const SOURCE_ROOT = /^(?:packages\/[^/]+\/)?src\/.*\.ts$/;
164+
159165
export function isIncludableSource(path: string): boolean {
160-
return /^src\/.*\.ts$/.test(path) && !isTestFile(path);
166+
return SOURCE_ROOT.test(path) && !isTestFile(path);
161167
}
162168

163169
// A changed line looks like product code (not blank, not a comment-only line).

‎scripts/coverage-changed/run.test.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,20 @@ test('fails when a changed source line is uncovered and names that line', () =>
8383
assert.match(out, /`src\/feature\.ts`: 2/);
8484
});
8585

86+
test('fails when a changed workspace package line is uncovered and names that line', () => {
87+
write(
88+
'packages/contracts/src/feature.ts',
89+
'export const covered = 1;\nexport const uncovered = 2;\n',
90+
);
91+
git('add', '-A');
92+
git('commit', '-q', '-m', 'package feature');
93+
writeLcov('SF:packages/contracts/src/feature.ts\nDA:1,3\nDA:2,0\nend_of_record\n');
94+
const { code, out } = capture(() => run(['--base', 'main'], repo));
95+
assert.equal(code, 1);
96+
assert.match(out, /Changed-line coverage gate: FAIL/);
97+
assert.match(out, /`packages\/contracts\/src\/feature\.ts`: 2/);
98+
});
99+
86100
test('waiver env keeps the job green, still reporting numbers to the job summary', () => {
87101
write('src/feature.ts', 'export const covered = 1;\nexport const uncovered = 2;\n');
88102
git('add', '-A');

‎scripts/lib/vitest-concurrency.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* Keep one Vitest invocation modest enough to coexist with two other Codex
3+
* worktrees on a 12-core development host: 3 agents + (3 suites * 2 workers)
4+
* leaves roughly 3 cores for runners, subprocesses, simulators, and the OS.
5+
*/
6+
export const DEFAULT_VITEST_MAX_WORKERS = 2;
7+
8+
export function resolveVitestMaxWorkers(env: NodeJS.ProcessEnv = process.env): number | undefined {
9+
return env.CI === 'true' ? undefined : DEFAULT_VITEST_MAX_WORKERS;
10+
}

‎src/__tests__/hermetic-env-setup.test.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ import os from 'node:os';
22
import path from 'node:path';
33
import { afterEach, test, vi } from 'vitest';
44
import assert from 'node:assert/strict';
5+
import {
6+
DEFAULT_VITEST_MAX_WORKERS,
7+
resolveVitestMaxWorkers,
8+
} from '../../scripts/lib/vitest-concurrency.ts';
59
import vitestConfig from '../../vitest.config.ts';
610

711
const HERMETIC_ENV_SETUP = 'src/__tests__/hermetic-env-setup.ts';
@@ -13,6 +17,12 @@ const VITEST_CLAIMS_DIR = path.join(os.tmpdir(), `agent-device-vitest-claims-${p
1317

1418
type ProjectShape = { test?: { name?: string; setupFiles?: readonly string[] } };
1519

20+
test('vitest caps aggregate worker concurrency for parallel worktrees', () => {
21+
assert.equal(vitestConfig.test?.maxWorkers, resolveVitestMaxWorkers());
22+
assert.equal(resolveVitestMaxWorkers({}), DEFAULT_VITEST_MAX_WORKERS);
23+
assert.equal(resolveVitestMaxWorkers({ CI: 'true' }), undefined);
24+
});
25+
1626
// Wiring: the scrub only helps if every project loads it as a setup file. CI runs with the
1727
// vars unset, so a dropped wiring is otherwise invisible — assert it structurally instead.
1828
test('every vitest project wires the hermetic-env setup', () => {

‎vitest.config.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ import contentionRetryReporter, {
44
FAILURE_FILE_ENV,
55
} from './scripts/lib/contention-retry-reporter.ts';
66
import { SUBPROCESS_STUB_TESTS } from './scripts/lib/contention-retry.ts';
7+
import { resolveVitestMaxWorkers } from './scripts/lib/vitest-concurrency.ts';
78
import slowTestGateReporter from './scripts/vitest-slow-test-reporter.ts';
89

910
// Tests that stub a real binary (adb/xcrun/npx) by mutating process.env.PATH and
10-
// then spawn it, so each case waits real subprocess/retry/poll time. Run at the
11-
// unit suite's default ~7x file parallelism they contend for CPU and their stub
11+
// then spawn it, so each case waits real subprocess/retry/poll time. Run with
12+
// broad file parallelism they contend for CPU and their stub
1213
// spawns get starved past an internal budget, so production takes a generic
1314
// failure path and returns a different error than the assertion expects — a
1415
// contention flake whose failing subset shifts between runs (see
@@ -48,6 +49,13 @@ export default defineConfig({
4849
// --no-isolate = 205s wall vs 48s (module state thrashes across files),
4950
// threads = no change.
5051
slowTestThreshold: 500,
52+
// Vitest otherwise derives 11 workers from this 12-core host. Three
53+
// concurrent Codex worktrees can then request 33 workers and starve the
54+
// subprocess/test-server paths behind exact timeout budgets. Two workers
55+
// per local invocation preserves useful parallelism while leaving host
56+
// headroom. CI stays uncapped so Vitest derives the runner-appropriate
57+
// worker count from the isolated machine's available CPU pool.
58+
maxWorkers: resolveVitestMaxWorkers(),
5159
reporters: reporters(),
5260
projects: [
5361
{

0 commit comments

Comments
 (0)