diff --git a/codev/projects/bugfix-1956-tests-one-run-of-the-packages-/status.yaml b/codev/projects/bugfix-1956-tests-one-run-of-the-packages-/status.yaml new file mode 100644 index 000000000..92ed6051f --- /dev/null +++ b/codev/projects/bugfix-1956-tests-one-run-of-the-packages-/status.yaml @@ -0,0 +1,16 @@ +id: bugfix-1956 +title: tests-one-run-of-the-packages- +protocol: bugfix +phase: pr +plan_phases: [] +current_plan_phase: null +gates: + pr: + status: pending + requested_at: '2026-11-24T01:23:29.390Z' +iteration: 1 +build_complete: false +history: [] +started_at: '2026-11-24T01:06:29.068Z' +updated_at: '2026-11-24T01:23:29.391Z' +pr_ready_for_human: true diff --git a/codev/state/bugfix-1956_thread.md b/codev/state/bugfix-1956_thread.md new file mode 100644 index 000000000..8e3902ecf --- /dev/null +++ b/codev/state/bugfix-1956_thread.md @@ -0,0 +1,68 @@ +# bugfix-1956 — the packages/codev unit suite loads an 18-core Mac to 59 + +Scope is the issue's last architect comment (2026-11-24): replace the dead `poolOptions` block +with Vitest 4 top-level options, choose a cap deliberately, check the other vitest configs, +add a test asserting the resolved cap, and one owner-approved full local run as verification. + +## Investigate (2026-11-23) + +Verified against the installed vitest 4.1.4 (not memory): + +- `test.poolOptions` is ignored. `dist/chunks/coverage.*.js` resolveConfig only logs the + deprecation for it; nothing in `dist/` reads `singleFork`. +- With no `maxWorkers`, `resolveMaxWorkers()` in `dist/chunks/cli-api.*.js` returns + `availableParallelism() - 1` in run mode and `floor(n/2)` in watch mode. So 17 files at + once on the 18-core Mac. GitHub's ubuntu-latest has 4 vCPUs (the repo is public), so 3 there. +- The v4 migration guide (v4.vitest.dev/guide/migration, "Pool Rework"): `maxThreads`/ + `maxForks` → `maxWorkers`; `singleFork` → `maxWorkers: 1, isolate: false`; + `VITEST_MAX_FORKS`/`VITEST_MAX_THREADS` → `VITEST_MAX_WORKERS`. The installed resolveConfig + reads `VITEST_MAX_WORKERS` natively, so an env override needs no code of ours. +- Types: `maxWorkers?: number | string` is top-level on `test`. + +Other configs: no other `poolOptions` anywhere. `vitest.cli.config.ts` and +`vitest.e2e.config.ts` (same package, spawn-heavy: built binary / real Tower servers) have no +cap either. `vitest.e2e.config.ts` sets `maxConcurrency: 1` with the comment "Run +sequentially", but `maxConcurrency` only limits `test.concurrent` tests inside one file. It +does not stop files running in parallel. + +Reproduced without a full run: + +- `resolveConfig()` from `vitest/node` on the current `vitest.config.ts` prints the + deprecation and gives `maxWorkers: undefined`, `isolate: true`, effective 17 on this Mac. + Same for the cli and e2e configs (no deprecation, also uncapped). +- 8 small pure test files under the current config: peak 9 live node children of the vitest + process (8 forks + esbuild). With a single fork it would be 2. + +Also learned: under `isolate: true` (the default, kept) every file gets a fresh fork that ends +after the file. Porch's tests check has a 5-minute default timeout and no internal retry. One +`porch done` is one suite run. GitHub runs the suite in 2m13s at 3 workers with coverage on. + +Cap decision: vitest's default on GitHub's 4-vCPU runners (ubuntu-latest and windows-latest, +public repo) is `cpus - 1 = 3`, not 4. A cap of 3 means CI keeps its current parallelism; a +cap of 4 would raise it on both. Told the architect I'm going with 3 unless they say 4. + +## Fix (2026-11-23) + +- `packages/codev/vitest-workers.ts`: `MAX_WORKERS = 3`, used by `vitest.config.ts` (replacing + the dead `poolOptions` block), `vitest.cli.config.ts` and `vitest.e2e.config.ts`. The + architect agreed to 3 over 4 because it is CI's existing parallelism. +- Comment fixes: `vitest-setup.ts` (no longer claims singleFork shares one process) and the + e2e config's `maxConcurrency: 1 // Run sequentially` (it never did that). +- Regression test `src/__tests__/bugfix-1956-vitest-worker-cap.test.ts`: `resolveConfig()` on + each of the three configs must give `maxWorkers === 3`. Failed 3/3 before, passes 3/3 after. + +Targeted (before the full run): the 8-file probe peaks at 3 forks (was 8); +`VITEST_MAX_WORKERS=1` gives 1. The issue's 10 spawn-heavy files run together: 239 tests in +36 s, peak load 4.4. + +## The one full verification run (owner-approved), via `porch done` (2026-11-23 17:15 PST) + +- Green: build check 18.5 s, tests check 137.1 s. All 429 files pass, 0 failures. +- One-minute load: 4.55 when vitest started, **peak 8.14** during the tests, 6.29 at the end + (was 30 to 59 uncapped). Live descendants of vitest: peak 14, median 7. +- Not a quiet machine: other worktrees' vitest runs (spir-1940, bugfix-1853, targeted) were + alive in 107 of 118 test-phase samples, up to 2 at once. +- Per-file (vitest results cache): slowest are spec-1280-measurement-instrument 35 s, + session-manager 28 s, pir-1233-session-launch-loop 22.5 s, tunnel-e2e 19.5 s, + claude-cli-lane 18.3 s. The issue's 10 spawn-heavy files total 73 s in the full run against + 89 s alone at the cap, so their times are normal for them, not load-inflated. diff --git a/packages/codev/src/__tests__/bugfix-1956-vitest-worker-cap.test.ts b/packages/codev/src/__tests__/bugfix-1956-vitest-worker-cap.test.ts new file mode 100644 index 000000000..745f4e4e7 --- /dev/null +++ b/packages/codev/src/__tests__/bugfix-1956-vitest-worker-cap.test.ts @@ -0,0 +1,48 @@ +/** + * Issue #1956: every packages/codev vitest config must cap how many test files run at once, + * as vitest itself resolves the config. + * + * Vitest 4 removed `test.poolOptions`, so the old `poolOptions.forks.singleFork: true` was + * ignored and vitest fell back to `os.availableParallelism() - 1` workers: 17 parallel forks + * on an 18-core Mac, enough load that the suite's own process-spawning tests timed out. + * Reading the config file's keys would not have caught it (the key was there; vitest + * ignored it), so this resolves each config through vitest's public `resolveConfig` and + * checks the worker count vitest will actually use. + */ + +import { dirname, resolve } from 'node:path'; +import { fileURLToPath } from 'node:url'; +import { afterEach, beforeEach, describe, expect, it } from 'vitest'; +import { resolveConfig } from 'vitest/node'; + +const PACKAGE_ROOT = resolve(dirname(fileURLToPath(import.meta.url)), '../..'); + +/** vitest's own default on GitHub's 4-vCPU runners (cores - 1); see `vitest-workers.ts`. */ +const EXPECTED_MAX_WORKERS = 3; + +describe('vitest worker cap (#1956)', () => { + // `VITEST_MAX_WORKERS` is vitest's per-run override; a shell that exports it must not + // decide what this test reads. + let savedOverride: string | undefined; + beforeEach(() => { + savedOverride = process.env.VITEST_MAX_WORKERS; + delete process.env.VITEST_MAX_WORKERS; + }); + afterEach(() => { + if (savedOverride === undefined) delete process.env.VITEST_MAX_WORKERS; + else process.env.VITEST_MAX_WORKERS = savedOverride; + }); + + it.each(['vitest.config.ts', 'vitest.cli.config.ts', 'vitest.e2e.config.ts'])( + '%s resolves to a cap of 3 workers', + async (config) => { + const { vitestConfig } = await resolveConfig({ + root: PACKAGE_ROOT, + config, + watch: false, + run: true, + }); + expect(vitestConfig.maxWorkers).toBe(EXPECTED_MAX_WORKERS); + }, + ); +}); diff --git a/packages/codev/vitest-setup.ts b/packages/codev/vitest-setup.ts index 163482753..66dadf5fa 100644 --- a/packages/codev/vitest-setup.ts +++ b/packages/codev/vitest-setup.ts @@ -121,11 +121,11 @@ process.stdin.on('end', () => { `; /** - * setupFiles re-executes for every test file, but with `singleFork` all ~200 unit - * files share one process. Creating a sandbox (and registering an exit hook) per - * file would mean ~200 temp dirs and ~200 listeners on the same emitter — enough - * to trip Node's MaxListenersExceededWarning. Cache on globalThis so the setup is - * genuinely one-shot per process. + * setupFiles re-executes for every test file. Under vitest's default `isolate: true` + * each file gets its own fork, but with `isolate: false` one process runs many files, + * and a sandbox (and exit hook) per file would mean hundreds of temp dirs and + * listeners on the same emitter — enough to trip Node's MaxListenersExceededWarning. + * Cache on globalThis so the setup is genuinely one-shot per process. */ const SANDBOX_KEY = '__codevTestSandbox1323'; const g = globalThis as Record; diff --git a/packages/codev/vitest-workers.ts b/packages/codev/vitest-workers.ts new file mode 100644 index 000000000..7bb30999d --- /dev/null +++ b/packages/codev/vitest-workers.ts @@ -0,0 +1,13 @@ +/** + * How many test files run at once, shared by every packages/codev vitest config (#1956). + * + * Without a cap, vitest runs `os.availableParallelism() - 1` files at once: 17 on an + * 18-core Mac. The suite's process-spawning tests then load the machine past their own + * 5 s timeouts. (Vitest 4 removed `poolOptions`, so the `singleFork` this replaces had been + * ignored since the upgrade.) + * + * 3 is what vitest already picks on GitHub's 4-vCPU runners (ubuntu-latest and + * windows-latest), so CI keeps its parallelism and a developer machine runs the suite the + * way CI does. For one run, vitest's own `VITEST_MAX_WORKERS` overrides it. + */ +export const MAX_WORKERS = 3; diff --git a/packages/codev/vitest.cli.config.ts b/packages/codev/vitest.cli.config.ts index 293f4d423..c5782a233 100644 --- a/packages/codev/vitest.cli.config.ts +++ b/packages/codev/vitest.cli.config.ts @@ -10,9 +10,11 @@ */ import { defineConfig } from 'vitest/config'; +import { MAX_WORKERS } from './vitest-workers'; export default defineConfig({ test: { + maxWorkers: MAX_WORKERS, // #1956; see vitest-workers.ts // Sandboxes the real agy binary and the user-global metrics DB (#1323). // Spawned CLI children inherit the pins via `{ ...process.env }`. setupFiles: ['./vitest-setup.ts'], diff --git a/packages/codev/vitest.config.ts b/packages/codev/vitest.config.ts index 6b33efa86..3e6741a9c 100644 --- a/packages/codev/vitest.config.ts +++ b/packages/codev/vitest.config.ts @@ -6,6 +6,7 @@ */ import { defineConfig } from 'vitest/config'; +import { MAX_WORKERS } from './vitest-workers'; /** * Native Windows (Spec 1658): process creation, Git Bash and the CIM census take @@ -25,11 +26,7 @@ export default defineConfig({ // Sandboxes the real agy binary and the user-global metrics DB (#1323). setupFiles: ['./vitest-setup.ts'], pool: 'forks', - poolOptions: { - forks: { - singleFork: true, // Prevent worker crash on CI during cleanup - }, - }, + maxWorkers: MAX_WORKERS, // #1956; see vitest-workers.ts exclude: [ '**/node_modules/**', '**/dist/**', diff --git a/packages/codev/vitest.e2e.config.ts b/packages/codev/vitest.e2e.config.ts index d89ed0148..35dd064c6 100644 --- a/packages/codev/vitest.e2e.config.ts +++ b/packages/codev/vitest.e2e.config.ts @@ -14,6 +14,7 @@ */ import { defineConfig } from 'vitest/config'; +import { MAX_WORKERS } from './vitest-workers'; export default defineConfig({ test: { @@ -26,7 +27,8 @@ export default defineConfig({ testTimeout: 1200000, // 20 minutes per test hookTimeout: 300000, // 5 minutes for setup/teardown pool: 'forks', // Isolate tests - maxConcurrency: 1, // Run sequentially (expensive) + maxWorkers: MAX_WORKERS, // Files at once (#1956); see vitest-workers.ts + maxConcurrency: 1, // `test.concurrent` tests within one file run one at a time globals: true, }, });