Beautiful, AI-native markdown IDE and LLM wiki
TypeScript 2nd-brainagent-skillsclaudecodexcompany-braindocsknowledge-knowledge-baseknowledge-managementllmllm-wikillm-wiki-karpathymarkdownmarkdown-editormdmdxnotespkmsecond-brainskills
fix(ok): derive off-CI Playwright worker count from per-worker cost (#3294)
Nick Gomeze6c2699a909e
diff --git a/packages/app/playwright.a11y.config.ts b/packages/app/playwright.a11y.config.tsindex 5073f1713..fa93aaf67 100644--- a/packages/app/playwright.a11y.config.ts+++ b/packages/app/playwright.a11y.config.ts@@ -1,4 +1,6 @@+import { availableParallelism } from 'node:os';import { defineConfig } from '@playwright/test';+import { resolveWorkerCount } from './playwright.config.ts';/*** A11y Playwright config — per-worker fixture isolation (same shape as@@ -25,6 +27,15 @@ import { defineConfig } from '@playwright/test';* because it exposes what a11y needs (per-worker server + API helpers).*/+/**+ * Off-CI worker count is derived from the same per-worker density as the main+ * suite rather than left to Playwright's default, because this config consumes+ * the identical per-worker fixture: each worker owns a full Vite + Hocuspocus ++ * Chromium stack, not just a browser. `check:full:parallel` runs this tier+ * concurrently with the e2e tier at `--concurrency=100%`, so an unbounded+ * default here would re-import the oversubscription the main config now avoids.+ */+const isCI = !!process.env.CI;export default defineConfig({@@ -35,7 +46,7 @@ export default defineConfig({failOnFlakyTests: false,forbidOnly: isCI,fullyParallel: true,- workers: isCI ? 4 : undefined,+ workers: isCI ? 4 : resolveWorkerCount(availableParallelism()),reporter: [['html', { open: 'never' }], ['list'], ...(isCI ? [['github'] as const] : [])],use: {// `baseURL` is populated by the worker-scoped fixture indiff --git a/packages/app/playwright.config.ts b/packages/app/playwright.config.tsindex 3386df49c..de7e41e10 100644--- a/packages/app/playwright.config.ts+++ b/packages/app/playwright.config.ts@@ -1,9 +1,10 @@+import { availableParallelism } from 'node:os';import { defineConfig } from '@playwright/test';/*** Per-worker server isolation: there is no `webServer` block — instead a* worker-scoped fixture at `tests/stress/_helpers/fixtures.ts`. Each- * Playwright worker spawns its own `bun run dev` process on a+ * Playwright worker spawns its own `pnpm run dev` process on a* kernel-allocated port + unique tmpdir, eliminating the cross-worker CPU* contention that created a structural flake class under shared webServer.*@@ -30,6 +31,39 @@ import { defineConfig } from '@playwright/test';*/const isCI = !!process.env.CI;+/**+ * Logical CPUs one worker occupies. Since the per-worker-server migration a+ * worker is not just a browser: it owns a Vite dev server, a Hocuspocus CRDT+ * server, a parse-worker pool, two filesystem watchers, AND its Chromium.+ * Playwright's built-in default (`max(1, floor(os.cpus().length / 2))`) is+ * calibrated for the stock topology it ships with — many browser-only workers+ * sharing one `webServer` —+ * so inheriting it here provisions ~2x the application stacks the host can+ * actually serve. The resulting saturation does not fail any single test+ * deterministically; it makes whichever worker loses the scheduler lottery+ * miss its budget, which is why the failures were always timeouts on an+ * arbitrary test and never assertion mismatches.+ *+ * 4 is the density CI was calibrated at (`workers: 4` on a 16+-vCPU runner,+ * see the `workers` comment below). Deriving the off-CI count from the same+ * ratio keeps every host running the topology the budgets were measured+ * against, instead of a machine-dependent one.+ */+export const LOGICAL_CPUS_PER_WORKER = 4;++/**+ * Worker count for a host with `logicalCpus` logical CPUs, at the density+ * above. Never returns 0 — any host with fewer than+ * 2 × LOGICAL_CPUS_PER_WORKER (8) logical CPUs runs the suite on a single+ * worker, serially.+ * Exported (with `LOGICAL_CPUS_PER_WORKER`) so the density contract is+ * pinned by `tests/meta/playwright-worker-density.test.ts` rather than+ * re-derived by a reader.+ */+export function resolveWorkerCount(logicalCpus: number): number {+ return Math.max(1, Math.floor(logicalCpus / LOGICAL_CPUS_PER_WORKER));+}+export default defineConfig({testDir: './tests/stress',testMatch: /.*\.e2e\.ts$/,@@ -72,8 +106,11 @@ export default defineConfig({// per-worker server isolation together make fullyParallel fully safe.// If the CI runner tier changes back to 2 vCPU (e.g., ubuntu-64gb quota// exhausted), re-downgrade to workers=1.+ //+ // Off-CI the count is derived from the same per-worker density rather than+ // left to Playwright's shared-server default — see LOGICAL_CPUS_PER_WORKER.fullyParallel: true,- workers: isCI ? 4 : undefined,+ workers: isCI ? 4 : resolveWorkerCount(availableParallelism()),reporter: [['html', { open: 'never' }], ['list'], ...(isCI ? [['github'] as const] : [])],use: {// `baseURL` is populated by the worker-scoped fixture indiff --git a/packages/app/playwright.visual.config.ts b/packages/app/playwright.visual.config.tsindex 15c071f67..90ca01fad 100644--- a/packages/app/playwright.visual.config.ts+++ b/packages/app/playwright.visual.config.ts@@ -1,4 +1,6 @@+import { availableParallelism } from 'node:os';import { defineConfig } from '@playwright/test';+import { resolveWorkerCount } from './playwright.config.ts';/*** Visual Playwright config — per-worker fixture isolation (same shape as@@ -21,6 +23,15 @@ import { defineConfig } from '@playwright/test';* + API helpers + per-worker content dir).*/+/**+ * Off-CI worker count is derived from the same per-worker density as the main+ * suite rather than left to Playwright's default, because this config consumes+ * the identical per-worker fixture: each worker owns a full Vite + Hocuspocus ++ * Chromium stack, not just a browser. `check:full:parallel` runs this tier+ * concurrently with the e2e tier at `--concurrency=100%`, so an unbounded+ * default here would re-import the oversubscription the main config now avoids.+ */+const isCI = !!process.env.CI;export default defineConfig({@@ -35,7 +46,7 @@ export default defineConfig({// changeset from becoming the golden.updateSnapshots: 'none',fullyParallel: true,- workers: isCI ? 4 : undefined,+ workers: isCI ? 4 : resolveWorkerCount(availableParallelism()),use: {// `baseURL` is populated by the worker-scoped fixture in// `tests/stress/_helpers/fixtures.ts`. Leave unset so the fixture'sdiff --git a/packages/app/tests/meta/playwright-worker-density.test.ts b/packages/app/tests/meta/playwright-worker-density.test.tsnew file mode 100644index 000000000..0bebaf73a--- /dev/null+++ b/packages/app/tests/meta/playwright-worker-density.test.ts@@ -0,0 +1,133 @@+/**+ * Playwright worker-density meta-guard.+ *+ * A worker in this suite is not a browser — it is a whole application stack:+ * a Vite dev server, a Hocuspocus CRDT server, a parse-worker pool, two+ * filesystem watchers, and a Chromium (see the worker-scoped fixture in+ * `tests/stress/_helpers/fixtures.ts`). Playwright's built-in default worker+ * count assumes its own stock topology, where workers own only a browser and+ * share one `webServer`, so leaving `workers` unset provisions roughly twice+ * the stacks a host can serve.+ *+ * Oversubscription does not fail any one test deterministically: it starves+ * whichever worker loses the scheduler lottery, so the suite loses a small,+ * arbitrary set of tests to timeouts on every run while the code under test is+ * correct. That is invisible to a per-test reviewer, which is why the density+ * is pinned here instead of trusted to the config's prose.+ *+ * The resolver is exercised against planted CPU counts — a guard that only+ * asserted the current host's value would go vacuously green on any machine+ * that happens to satisfy it. The config itself is exercised by re-importing+ * it with `process.env.CI` planted to each state, because it reads `CI` at+ * import time: an assertion on the ambient import sees only one arm. Under CI+ * `workers` is the runner-tier constant, so an off-CI regression back to+ * `undefined` (Playwright's shared-server default) would keep every+ * ambient-import assertion green in the one environment that gates merges.+ */++import { availableParallelism } from 'node:os';+import { describe, expect, test, vi } from 'vitest';+import { LOGICAL_CPUS_PER_WORKER, resolveWorkerCount } from '../../playwright.config';++/**+ * Every config whose tests consume the per-worker fixture in+ * `tests/stress/_helpers/fixtures.ts`. All three pay the same per-worker cost,+ * and `check:full:parallel` runs them concurrently at `--concurrency=100%`, so+ * the density contract has to hold across the set — bounding one tier while a+ * sibling keeps Playwright's default just relocates the oversubscription.+ *+ * Static importers rather than a computed specifier: the bundler has to see+ * each path literally to resolve it.+ */+/**+ * Logical CPUs on the CI runner tier the `workers: 4` literal was calibrated+ * against (`blacksmith-16vcpu-ubuntu-2404`, see the `workers` comment in+ * `playwright.config.ts`). Only used to prove the CI literal and the off-CI+ * resolver describe the same density.+ */+const CI_RUNNER_LOGICAL_CPUS = 16;++const FIXTURE_SHARING_CONFIGS = {+ e2e: () => import('../../playwright.config'),+ a11y: () => import('../../playwright.a11y.config'),+ visual: () => import('../../playwright.visual.config'),+} as const;++/**+ * Re-evaluate one config with `process.env.CI` planted to the given state and+ * return the resulting config object. The registry reset is what makes the+ * planted state observable — without it the module cache hands back the+ * ambient-environment evaluation.+ */+async function loadConfigWithCI(+ ci: string | undefined,+ which: keyof typeof FIXTURE_SHARING_CONFIGS = 'e2e',+) {+ const saved = process.env.CI;+ if (ci === undefined) delete process.env.CI;+ else process.env.CI = ci;+ vi.resetModules();+ try {+ return (await FIXTURE_SHARING_CONFIGS[which]()).default;+ } finally {+ if (saved === undefined) delete process.env.CI;+ else process.env.CI = saved;+ vi.resetModules();+ }+}++const CONFIG_NAMES = Object.keys(+ FIXTURE_SHARING_CONFIGS,+) as (keyof typeof FIXTURE_SHARING_CONFIGS)[];++describe('playwright worker density', () => {+ test('one worker is budgeted a full application stack, not a bare browser', () => {+ // Pinned exactly: 4 is the ratio the CI calibration established (workers=4+ // on a 16+-vCPU runner), and every planted expectation below is derived+ // from it. A deliberate recalibration updates this and the table together.+ expect(LOGICAL_CPUS_PER_WORKER).toBe(4);+ });++ test.each([+ { logicalCpus: 0, expected: 1 },+ { logicalCpus: 1, expected: 1 },+ { logicalCpus: 2, expected: 1 },+ { logicalCpus: 3, expected: 1 },+ { logicalCpus: 4, expected: 1 },+ { logicalCpus: 7, expected: 1 },+ { logicalCpus: 8, expected: 2 },+ { logicalCpus: 16, expected: 4 },+ { logicalCpus: 18, expected: 4 },+ { logicalCpus: 64, expected: 16 },+ ])('$logicalCpus logical CPUs resolves to $expected worker(s)', ({ logicalCpus, expected }) => {+ expect(resolveWorkerCount(logicalCpus)).toBe(expected);+ });++ test('never exceeds the budgeted density on a host that can afford one worker', () => {+ for (let logicalCpus = LOGICAL_CPUS_PER_WORKER; logicalCpus <= 256; logicalCpus++) {+ expect(resolveWorkerCount(logicalCpus) * LOGICAL_CPUS_PER_WORKER).toBeLessThanOrEqual(+ logicalCpus,+ );+ }+ });++ test.each(CONFIG_NAMES)('under CI the %s config pins the runner-tier constant', async (which) => {+ const config = await loadConfigWithCI('true', which);+ expect(config.workers).toBe(4);+ // The CI count is a separate literal from the off-CI resolver, and the+ // whole justification for LOGICAL_CPUS_PER_WORKER is that off-CI runs at+ // the density CI was calibrated at. Assert that equivalence mechanically:+ // recalibrating the constant without revisiting the CI literal would+ // otherwise leave the two arms at different per-worker budgets, green.+ expect(config.workers).toBe(resolveWorkerCount(CI_RUNNER_LOGICAL_CPUS));+ });++ test.each(+ CONFIG_NAMES,+ )('off-CI the %s config derives workers from the density resolver', async (which) => {+ const config = await loadConfigWithCI(undefined, which);+ // `undefined` here is the regression: it hands the decision to Playwright's+ // shared-server default and reintroduces the oversubscription flake class.+ expect(config.workers).toBe(resolveWorkerCount(availableParallelism()));+ });+});diff --git a/packages/app/tests/stress/_helpers/fixtures.ts b/packages/app/tests/stress/_helpers/fixtures.tsindex 732e6d8c8..4501d9b23 100644--- a/packages/app/tests/stress/_helpers/fixtures.ts+++ b/packages/app/tests/stress/_helpers/fixtures.ts@@ -3,7 +3,7 @@** Replaces the previous single shared `webServer` block in* `playwright.config.ts` with a `{ scope: 'worker' }` fixture that spawns its- * own `bun run dev` process on a kernel-allocated port + unique tmpdir per+ * own `pnpm run dev` process on a kernel-allocated port + unique tmpdir per* worker. Eliminates cross-worker CPU contention on one shared Vite+Hocuspocus* instance (the residual flake class an earlier mitigation could not fully* eliminate).@@ -384,7 +384,7 @@ export const test = base.extend<TestFixtures, WorkerFixtures>({const contentDir = mkdtempSync(join(tmpdir(), `ok-w${workerInfo.workerIndex}-`));// Per-worker Vite optimized-dependency cacheDir — the third per-process// shared resource alongside the port and the content dir. Without it,- // every spawned `bun run dev` falls back to Vite's default+ // every spawned `pnpm run dev` falls back to Vite's default// `<root>/node_modules/.vite`, which the dependency optimizer is// single-writer over; cross-worker re-optimization deletes chunk files// that peer browsers are mid-import on, 404'ing `/src/main.tsx`'s ESM@@ -420,7 +420,8 @@ export const test = base.extend<TestFixtures, WorkerFixtures>({// Vite's load-bearing boot diagnostics (dep-scan failures, restart// notices, "new dependencies optimized") go to stdout. Capture it to// a kernel-level file fd — no pipe backpressure (a `pipe` without a- // consumer fills its 64KB buffer and hangs bun under verbose output),+ // consumer fills its 64KB buffer and stalls the writer under verbose+ // output),// and boot failures get the log tail attached below instead of the// old 'ignore' black hole.const serverLog = openServerLog(`w${workerInfo.workerIndex}`);diff --git a/turbo.json b/turbo.jsonindex 0e4078d8c..34994ad46 100644--- a/turbo.json+++ b/turbo.json@@ -35,7 +35,18 @@"test": {"dependsOn": ["^build"],"cache": true,- "inputs": ["src/**/*.ts", "src/**/*.tsx", "src/**/*.json", "tests/contract/**/*.ts"]+ "inputs": [+ "src/**/*.ts",+ "src/**/*.tsx",+ "src/**/*.json",+ "tests/contract/**/*.ts",+ "vitest.config.ts",+ "vite.dedupe.ts",+ "tests/lingui-macro-shim.tsx",+ "tests/integration/idb-preload.ts",+ "../../test-support/*.ts",+ "../test-support/*.ts"+ ]},"@inkeep/md-conformance#test": {"dependsOn": ["^build"],@@ -46,6 +57,8 @@"src/**/*.json","tests/contract/**/*.ts","corpora/**/*.json",+ "vitest.config.ts",+ "../../test-support/*.ts","../server/src/**/*.ts"]},@@ -59,6 +72,10 @@"tests/dom/**/*.tsx","vitest.dom.config.ts","vitest.config.ts",+ "vite.dedupe.ts",+ "tests/lingui-macro-shim.tsx",+ "tests/integration/idb-preload.ts",+ "../../test-support/*.ts","../core/src/**/*.ts","../server/src/**/*.ts","../md-conformance/src/**/*.ts",@@ -74,6 +91,11 @@"src/**/*.tsx","tests/**/*.ts","tests/**/*.tsx",+ "playwright.config.ts",+ "vitest.integration.config.ts",+ "vitest.config.ts",+ "vite.dedupe.ts",+ "../../test-support/*.ts","../server/src/**/*.ts","../core/src/**/*.ts","../../biome.jsonc",@@ -89,6 +111,12 @@"tests/integration/**/*.ts","tests/meta/**/*.ts","tests/lint-plugins/**/*.ts",+ "playwright.config.ts",+ "vitest.integration.config.ts",+ "vitest.config.ts",+ "vite.dedupe.ts",+ "tests/lingui-macro-shim.tsx",+ "../../test-support/*.ts","../server/src/**/*.ts","../core/src/**/*.ts"]@@ -102,6 +130,12 @@"tests/integration/**/*.ts","tests/meta/**/*.ts","tests/lint-plugins/**/*.ts",+ "playwright.config.ts",+ "vitest.integration.config.ts",+ "vitest.config.ts",+ "vite.dedupe.ts",+ "tests/lingui-macro-shim.tsx",+ "../../test-support/*.ts","../server/src/**/*.ts","../core/src/**/*.ts"]@@ -114,6 +148,10 @@"src/editor/**/*.tsx","tests/conversion/**/*.ts","tests/integration/**/*.ts",+ "vitest.config.ts",+ "vite.dedupe.ts",+ "tests/lingui-macro-shim.tsx",+ "../../test-support/*.ts","../core/src/**/*.ts","../server/src/**/*.ts"]@@ -133,6 +171,11 @@"src/editor/**/*.ts","src/editor/**/*.tsx","tests/stress/component-blocks.perf.test.ts",+ "vitest.config.ts",+ "vite.dedupe.ts",+ "tests/lingui-macro-shim.tsx",+ "tests/integration/idb-preload.ts",+ "../../test-support/*.ts","../core/src/**/*.ts"]},@@ -153,7 +196,12 @@"test:perf:regression:unit": {"dependsOn": [],"cache": true,- "inputs": ["tests/perf/regression-gate.ts", "tests/perf/regression-gate.test.ts"]+ "inputs": [+ "tests/perf/regression-gate.ts",+ "tests/perf/regression-gate.test.ts",+ "vitest.config.ts",+ "../../test-support/*.ts"+ ]},"test:perf:regression": {"dependsOn": [],@@ -175,7 +223,9 @@"src/metrics/parse-health.ts","src/markdown/**/*.ts","tests/health/parse-health-gate.ts",- "tests/health/parse-health-gate.test.ts"+ "tests/health/parse-health-gate.test.ts",+ "vitest.config.ts",+ "../../test-support/*.ts"]},"test:health": {