Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

feat: derive local test workers from CoS settings and keep Spotlight off agent worktrees

[._.]/ Adam Eivy50908340ef18
diff --git a/AGENTS.md b/AGENTS.md
index eac481a3f1..af05d0e965 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -49,7 +49,7 @@ npm test --prefix client # Vitest (happy-dom) — component/unit tests
# Supported FULL local validation (same worker cap as CI). A focused file run
# is not a full-suite result; unbounded npm test can time out heavy cases on a
# loaded machine. See docs/CONTRIBUTING.md.
-# PORTOS_PREGATE_MAX_WORKERS=4 npm test
+# npm test (worker cap derives from the CoS max-concurrent-agents setting)
# No NODE_ENV prefix needed: server/vitest.config.js FORCES NODE_ENV=test (#4554),
# because PortOS runs under PM2 with NODE_ENV=development and a suite that
# inherits it aims at the real Postgres.
diff --git a/docs/CONTRIBUTING.md b/docs/CONTRIBUTING.md
index 8f32031162..a752a2054c 100644
--- a/docs/CONTRIBUTING.md
+++ b/docs/CONTRIBUTING.md
@@ -286,8 +286,9 @@ and nested-worker cases then hit their existing budgets. A green focused
file run is evidence for that file only, not a full-suite result.
```bash
-# Server, then client. The value only lowers each workspace cap (client stays at 2).
-PORTOS_PREGATE_MAX_WORKERS=4 npm test
+# Server, then client. Workers derive from the CoS max-concurrent-agents setting
+# (cores / agents, capped at 4; client stays at 2). Lower it in the app to ease host load.
+npm test
# Focused evidence for one file:
npm test --prefix server -- services/creativeDirector/videoAssembly.test.js
diff --git a/docs/GITHUB_ACTIONS.md b/docs/GITHUB_ACTIONS.md
index 610df6ef71..ddcd1f37f9 100644
--- a/docs/GITHUB_ACTIONS.md
+++ b/docs/GITHUB_ACTIONS.md
@@ -293,14 +293,12 @@ On GitHub Actions, `CI=true` caps the server Vitest runner at `maxWorkers: 4`
(`scripts/vitestCiPool.js`, spread into `server/vitest.config.js` and
`client/vitest.config.js`). Standard Linux runners for public repositories are
[4 vCPU / 16GB](https://docs.github.com/en/actions/reference/runners/github-hosted-runners);
-uncapped forks oversubscribe those cores during transform. Local `npm test`
-is unbounded so a developer machine can use every core. The supported full
-local validation is `PORTOS_PREGATE_MAX_WORKERS=4 npm test` (see
-`docs/CONTRIBUTING.md`); a focused file run is not that result. `npm run pregate` exports `PORTOS_PREGATE_MAX_WORKERS` (default 4,
-overridable) so concurrent worktree gates on one host bound their Vitest
-workers; it can only lower a workspace's cap (client stays at 2), and direct
-test runs ignore it unless you set it. Divide the host between simultaneous
-gates, e.g. `PORTOS_PREGATE_MAX_WORKERS=2 npm run pregate`. The DOM-heavy client retains its proven two-worker override:
+uncapped forks oversubscribe those cores during transform. Locally, Vitest workers
+are `cores / maxConcurrentAgents` from the CoS settings (`scripts/lib/cosWorkerBudget.js`),
+capped at 4, so concurrent worktree gates share one host budget; lower
+**max concurrent agents** in the app to ease load — no env var. A focused file
+run is not the full-suite result (`docs/CONTRIBUTING.md`). `npm run pregate`
+passes the derived value to its stages. The DOM-heavy client retains its proven two-worker override:
four workers made its async rendering assertions timing-dependent under CI
contention. File-level parallelism stays on; the DB suite already serializes
files because those tests share one Postgres.
diff --git a/package.json b/package.json
index 3c101ed9d8..401ace11db 100644
--- a/package.json
+++ b/package.json
@@ -30,6 +30,7 @@
"migrations": "node scripts/run-migrations.js",
"setup": "node scripts/checkNodeVersion.js && node scripts/checkNpmVersion.js && git submodule update --init --recursive && npm install --no-save && npm install --no-save --prefix client && npm install --no-save --prefix server && npm install --no-save --prefix autofixer && node scripts/trusted-rebuilds.js server && node scripts/setup-data.js && node scripts/setup-db.js && node scripts/setup-llm.js && node scripts/setup-browser.js && node scripts/setup-cert.js && node scripts/setup-guide.js",
"setup:data": "node scripts/setup-data.js",
+ "setup:spotlight": "node scripts/setup-spotlight.js",
"setup:reactor": "node scripts/setup-reactor.js",
"setup:db": "node scripts/setup-db.js",
"setup:db:test": "npm run setup:db:test --prefix server",
diff --git a/scripts/lib/cosWorkerBudget.js b/scripts/lib/cosWorkerBudget.js
new file mode 100644
index 0000000000..aa0eb1e44f
--- /dev/null
+++ b/scripts/lib/cosWorkerBudget.js
@@ -0,0 +1,41 @@
+/**
+ * Local Vitest worker budget derived from the CoS settings, so the app's
+ * "max concurrent agents" is the one knob for host load — no env var to set.
+ * Every concurrent agent may run its own gate, so the host's cores are divided
+ * by that cap. The result only ever LOWERS the proven per-workspace cap.
+ * Builtins only: vitest configs and pre-install scripts import this.
+ */
+import { execFileSync } from 'node:child_process';
+import { readFileSync } from 'node:fs';
+import { availableParallelism } from 'node:os';
+import { dirname, join } from 'node:path';
+
+/** Mirrors DEFAULT_CONFIG.maxConcurrentAgents in server/services/cosState.js. */
+export const DEFAULT_MAX_CONCURRENT_AGENTS = 3;
+
+/**
+ * Root of the primary checkout. A CoS worktree keeps its own near-empty
+ * `data/`; the live CoS config belongs to the checkout the worktree hangs off.
+ */
+export function primaryCheckoutRoot(cwd = process.cwd()) {
+ try {
+ const gitDir = execFileSync('git', ['rev-parse', '--path-format=absolute', '--git-common-dir'],
+ { cwd, encoding: 'utf8', timeout: 5000, stdio: ['ignore', 'pipe', 'ignore'] }).trim();
+ return gitDir ? dirname(gitDir) : cwd;
+ } catch {
+ return cwd;
+ }
+}
+
+/** `maxConcurrentAgents` from data/cos/config.json; the shipped default when unreadable or invalid. */
+export function readMaxConcurrentAgents(root = primaryCheckoutRoot()) {
+ try {
+ const value = JSON.parse(readFileSync(join(root, 'data', 'cos', 'config.json'), 'utf8'))?.maxConcurrentAgents;
+ if (Number.isInteger(value) && value >= 1) return value;
+ } catch { /* absent or corrupt config: fall through to the default */ }
+ return DEFAULT_MAX_CONCURRENT_AGENTS;
+}
+
+export function cosWorkerBudget({ cpus = availableParallelism(), agents = readMaxConcurrentAgents(), maxWorkers = 4 } = {}) {
+ return Math.max(1, Math.min(maxWorkers, Math.floor(cpus / agents)));
+}
diff --git a/scripts/lib/cosWorkerBudget.test.js b/scripts/lib/cosWorkerBudget.test.js
new file mode 100644
index 0000000000..4e1e078e4b
--- /dev/null
+++ b/scripts/lib/cosWorkerBudget.test.js
@@ -0,0 +1,33 @@
+import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+import { afterEach, describe, expect, it } from 'vitest';
+import { cosWorkerBudget, DEFAULT_MAX_CONCURRENT_AGENTS, readMaxConcurrentAgents } from './cosWorkerBudget.js';
+
+describe('cosWorkerBudget', () => {
+ it('divides the cores between concurrent agents and never exceeds the proven cap', () => {
+ expect(cosWorkerBudget({ cpus: 18, agents: 3 })).toBe(4);
+ expect(cosWorkerBudget({ cpus: 18, agents: 6 })).toBe(3);
+ expect(cosWorkerBudget({ cpus: 8, agents: 8, maxWorkers: 2 })).toBe(1);
+ expect(cosWorkerBudget({ cpus: 2, agents: 10 })).toBe(1);
+ });
+});
+
+describe('readMaxConcurrentAgents', () => {
+ const roots = [];
+ const rootWith = (config) => {
+ const root = mkdtempSync(join(tmpdir(), 'cos-budget-'));
+ roots.push(root);
+ mkdirSync(join(root, 'data', 'cos'), { recursive: true });
+ if (config !== undefined) writeFileSync(join(root, 'data', 'cos', 'config.json'), config);
+ return root;
+ };
+ afterEach(() => roots.splice(0).forEach(root => rmSync(root, { recursive: true, force: true })));
+
+ it('reads the CoS setting and falls back to the default when absent, corrupt or invalid', () => {
+ expect(readMaxConcurrentAgents(rootWith('{"maxConcurrentAgents":5}'))).toBe(5);
+ expect(readMaxConcurrentAgents(rootWith())).toBe(DEFAULT_MAX_CONCURRENT_AGENTS);
+ expect(readMaxConcurrentAgents(rootWith('{not json'))).toBe(DEFAULT_MAX_CONCURRENT_AGENTS);
+ expect(readMaxConcurrentAgents(rootWith('{"maxConcurrentAgents":0}'))).toBe(DEFAULT_MAX_CONCURRENT_AGENTS);
+ });
+});
diff --git a/scripts/lib/spotlightExclude.js b/scripts/lib/spotlightExclude.js
new file mode 100644
index 0000000000..3df1cc9072
--- /dev/null
+++ b/scripts/lib/spotlightExclude.js
@@ -0,0 +1,39 @@
+/**
+ * Keep Spotlight (mds_stores) and fseventsd from indexing the machinery under
+ * `data/`: agent worktrees, checkout archives, browser profiles and the like
+ * churn thousands of files per run and pin both daemons. macOS honors a
+ * `.metadata_never_index` marker for a whole subtree, so we mark only these
+ * directories — user media (videos, images, ...) stays searchable. Builtins only.
+ */
+import { existsSync, mkdirSync, writeFileSync } from 'node:fs';
+import { join } from 'node:path';
+
+export const SPOTLIGHT_MARKER = '.metadata_never_index';
+
+/** `data/`-relative directories that hold checkouts, archives, profiles or runtimes, never user media. */
+export const SPOTLIGHT_EXCLUDED_DIRS = [
+ 'browser-profile',
+ 'cos/branch-retirement-archives',
+ 'cos/held-checkout-archives',
+ 'cos/initialized-checkout-fixtures',
+ 'cos/initialized-checkout-retirements',
+ 'cos/worktrees',
+ 'python',
+ 'repos',
+ 'training-runs',
+];
+
+/** @returns {'created'|'present'|'unsupported'} 'created' when any marker was newly written. */
+export function excludeFromSpotlight(dataDir, { platform = process.platform, dirs = SPOTLIGHT_EXCLUDED_DIRS } = {}) {
+ if (platform !== 'darwin') return 'unsupported';
+ let created = false;
+ for (const rel of dirs) {
+ const dir = join(dataDir, rel);
+ const marker = join(dir, SPOTLIGHT_MARKER);
+ if (existsSync(marker)) continue;
+ mkdirSync(dir, { recursive: true });
+ writeFileSync(marker, '');
+ created = true;
+ }
+ return created ? 'created' : 'present';
+}
diff --git a/scripts/pregate.js b/scripts/pregate.js
index 5fbbc40f30..36ed513a76 100644
--- a/scripts/pregate.js
+++ b/scripts/pregate.js
@@ -60,6 +60,7 @@ import { ALWAYS_RUN_TESTS, structuralTestsFor } from './ci-test-plan.js';
import { assertNodeVersion } from './checkNodeVersion.js';
import { isDirectlyInvoked } from './lib/directInvocation.js';
import { needsBrowserFidelity } from './lib/browserFidelity.js';
+import { cosWorkerBudget } from './lib/cosWorkerBudget.js';
import { buildPregateReport, PREGATE_REPORT_PATH, validatedTreeHash, writePregateReport } from './lib/pregateReport.js';
import { dependencyConsistencyProblems } from './lib/workspaceDependencies.js';
@@ -148,14 +149,14 @@ export function describeBrowserSelection(files) {
}
/** Local pregate worker budget per Vitest workspace; see scripts/vitestCiPool.js. */
-export const PREGATE_DEFAULT_MAX_WORKERS = '4';
-
/**
- * Env for a stage child: bound Vitest workers unless the operator chose a
- * budget. Direct `npm test` never passes through here, so it stays unbounded.
+ * Env for a stage child: one worker budget, from the CoS max-concurrent-agents
+ * setting (scripts/lib/cosWorkerBudget.js), shared by every Vitest workspace.
*/
+let derivedWorkerBudget;
export function pregateWorkerEnv(env) {
- return { ...env, PORTOS_PREGATE_MAX_WORKERS: env.PORTOS_PREGATE_MAX_WORKERS || PREGATE_DEFAULT_MAX_WORKERS };
+ derivedWorkerBudget ??= String(cosWorkerBudget());
+ return { ...env, PORTOS_PREGATE_MAX_WORKERS: env.PORTOS_PREGATE_MAX_WORKERS || derivedWorkerBudget };
}
/**
diff --git a/scripts/pregate.test.js b/scripts/pregate.test.js
index a6837a097d..31dbcf9a24 100644
--- a/scripts/pregate.test.js
+++ b/scripts/pregate.test.js
@@ -7,6 +7,7 @@
* list, naming a downgraded test file that is not tracked, or reporting
* "CI will also run: db" from a plan field that has since been renamed.
*/
+import { cosWorkerBudget } from './lib/cosWorkerBudget.js';
import { afterAll, afterEach, beforeAll, beforeEach, describe, it, expect } from 'vitest';
import { execFileSync, spawn, spawnSync } from 'child_process';
import { copyFileSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync, symlinkSync, existsSync, lstatSync, readdirSync } from 'fs';
@@ -155,7 +156,7 @@ describe('pregate hidden-content invocation', () => {
'scripts/pregate.js', 'scripts/checkNodeVersion.js', 'scripts/scan-diff-hidden-content.js',
'scripts/ci-test-plan.js', 'scripts/ci-base-sha.js', 'server/lib/staticImportGraph.js',
'scripts/lib/directInvocation.js', 'scripts/lib/importReach.js', 'scripts/lib/workspaceTests.js', 'scripts/lib/browserFidelity.js', 'scripts/lib/browserTestProfiles.js', 'scripts/lib/githubOutput.js', 'scripts/lib/dbTestFiles.js',
- 'scripts/lib/browserSuites.js', 'scripts/lib/workspaceDependencies.js', 'scripts/lib/pregateReport.js', 'scripts/lib/pregateAttestation.js',
+ 'scripts/lib/browserSuites.js', 'scripts/lib/workspaceDependencies.js', 'scripts/lib/pregateReport.js', 'scripts/lib/cosWorkerBudget.js', 'scripts/lib/pregateAttestation.js',
'scripts/migrations/shippedPromptHistory.js', 'server/lib/aiToolkit/internal/windowsSafeSpawn.js',
'server/lib/diffHiddenContentScan.js', 'server/lib/modelAbuseGuard.js',
'server/lib/textUtils.js',
@@ -569,8 +570,8 @@ describe('pregate hidden-content invocation', () => {
});
describe('pregateWorkerEnv', () => {
- it('bounds workers by default but honors an operator budget', () => {
- expect(pregateWorkerEnv({}).PORTOS_PREGATE_MAX_WORKERS).toBe('4');
+ it('derives the budget from the CoS settings but honors an explicit handoff', () => {
+ expect(pregateWorkerEnv({}).PORTOS_PREGATE_MAX_WORKERS).toBe(String(cosWorkerBudget()));
expect(pregateWorkerEnv({ PORTOS_PREGATE_MAX_WORKERS: '2' }).PORTOS_PREGATE_MAX_WORKERS).toBe('2');
});
});
@@ -808,7 +809,7 @@ describe('the gate end to end', () => {
'scripts/pregate.js', 'scripts/checkNodeVersion.js', 'scripts/scan-diff-hidden-content.js',
'scripts/ci-test-plan.js', 'scripts/ci-base-sha.js', 'server/lib/staticImportGraph.js',
'scripts/lib/directInvocation.js', 'scripts/lib/importReach.js', 'scripts/lib/workspaceTests.js', 'scripts/lib/browserFidelity.js', 'scripts/lib/browserTestProfiles.js', 'scripts/lib/githubOutput.js', 'scripts/lib/dbTestFiles.js',
- 'scripts/lib/browserSuites.js', 'scripts/lib/workspaceDependencies.js', 'scripts/lib/pregateReport.js', 'scripts/lib/pregateAttestation.js',
+ 'scripts/lib/browserSuites.js', 'scripts/lib/workspaceDependencies.js', 'scripts/lib/pregateReport.js', 'scripts/lib/cosWorkerBudget.js', 'scripts/lib/pregateAttestation.js',
'scripts/migrations/shippedPromptHistory.js', 'server/lib/aiToolkit/internal/windowsSafeSpawn.js',
'server/lib/diffHiddenContentScan.js', 'server/lib/modelAbuseGuard.js',
'server/lib/textUtils.js',
@@ -1015,7 +1016,7 @@ describe('uncoveredSuites', () => {
describe('stageSpawnEnv', () => {
it('applies stage values and removes the variables a stage sets to undefined', () => {
const env = stageSpawnEnv({ TEST_DB_OK: '1', PGDATABASE: 'portos', KEEP: 'x' }, { TEST_DB_OK: undefined, PGDATABASE: 'portos_test' }, { ownsGroup: false });
- expect(env).toEqual({ KEEP: 'x', PGDATABASE: 'portos_test', PORTOS_PREGATE_MAX_WORKERS: '4' });
+ expect(env).toEqual({ KEEP: 'x', PGDATABASE: 'portos_test', PORTOS_PREGATE_MAX_WORKERS: String(cosWorkerBudget()) });
expect('TEST_DB_OK' in env).toBe(false);
});
diff --git a/scripts/setup-data.js b/scripts/setup-data.js
index 7fe205fda6..c287a9d22d 100644
--- a/scripts/setup-data.js
+++ b/scripts/setup-data.js
@@ -8,6 +8,7 @@ import { seedDataDir } from './lib/seedDataDir.js';
import { mergeJsonStarterTargets } from './lib/mergeJsonStarter.js';
import { rewriteAppsPortosRoot } from './lib/rewriteAppsPortosRoot.js';
import { adoptLegacyAppliedList } from './lib/migrationLedger.js';
+import { setupSpotlight } from './setup-spotlight.js';
const __dirname = dirname(fileURLToPath(import.meta.url));
const rootDir = join(__dirname, '..');
@@ -46,6 +47,7 @@ const rewritePortosRootPlaceholder = () => {
const { created } = seedDataDir({ referenceDir, dataDir, log: console.log });
rewritePortosRootPlaceholder();
+setupSpotlight(dataDir);
console.log(created
? '✅ Data directory created'
: '✅ Data directory already exists, ensured subdirectories and files');
diff --git a/scripts/setup-spotlight.js b/scripts/setup-spotlight.js
new file mode 100644
index 0000000000..d3fa065525
--- /dev/null
+++ b/scripts/setup-spotlight.js
@@ -0,0 +1,22 @@
+#!/usr/bin/env node
+/**
+ * Exclude the churny data/ subdirectories (worktrees, archives, profiles) from
+ * Spotlight indexing (macOS); user media stays indexed. Idempotent; also runs from
+ * setup-data.js so fresh installs and updates get it. Opt out with
+ * PORTOS_SKIP_SPOTLIGHT_EXCLUDE=1 (the marker is then left as-is).
+ */
+import { dirname, join } from 'node:path';
+import { fileURLToPath } from 'node:url';
+import { isDirectlyInvoked } from './lib/directInvocation.js';
+import { excludeFromSpotlight } from './lib/spotlightExclude.js';
+
+const dataDir = join(dirname(fileURLToPath(import.meta.url)), '..', 'data');
+
+export function setupSpotlight(dir = dataDir, { log = console.log, env = process.env } = {}) {
+ if (env.PORTOS_SKIP_SPOTLIGHT_EXCLUDE === '1') return 'skipped';
+ const result = excludeFromSpotlight(dir);
+ if (result === 'created') log('🔦 Excluded agent worktrees/archives under data/ from Spotlight (.metadata_never_index)');
+ return result;
+}
+
+if (isDirectlyInvoked(import.meta.url)) setupSpotlight();
diff --git a/scripts/setup-spotlight.test.js b/scripts/setup-spotlight.test.js
new file mode 100644
index 0000000000..94b913bad9
--- /dev/null
+++ b/scripts/setup-spotlight.test.js
@@ -0,0 +1,29 @@
+import { existsSync, mkdtempSync, rmSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+import { afterEach, describe, expect, it } from 'vitest';
+import { excludeFromSpotlight, SPOTLIGHT_MARKER } from './lib/spotlightExclude.js';
+import { setupSpotlight } from './setup-spotlight.js';
+
+describe('Spotlight exclusion of data/', () => {
+ const dirs = [];
+ const tmp = () => { const d = mkdtempSync(join(tmpdir(), 'spotlight-')); dirs.push(d); return d; };
+ afterEach(() => dirs.splice(0).forEach(d => rmSync(d, { recursive: true, force: true })));
+
+ it('marks only the machinery dirs once on macOS, keeps media indexed, and skips other platforms', () => {
+ const dir = tmp();
+ expect(excludeFromSpotlight(dir, { platform: 'linux' })).toBe('unsupported');
+ expect(existsSync(join(dir, 'cos/worktrees', SPOTLIGHT_MARKER))).toBe(false);
+ expect(excludeFromSpotlight(dir, { platform: 'darwin' })).toBe('created');
+ expect(existsSync(join(dir, 'cos/worktrees', SPOTLIGHT_MARKER))).toBe(true);
+ expect(existsSync(join(dir, SPOTLIGHT_MARKER))).toBe(false);
+ expect(existsSync(join(dir, 'videos', SPOTLIGHT_MARKER))).toBe(false);
+ expect(excludeFromSpotlight(dir, { platform: 'darwin' })).toBe('present');
+ });
+
+ it('honors the opt-out without touching the directory', () => {
+ const dir = join(tmp(), 'data');
+ expect(setupSpotlight(dir, { log: () => {}, env: { PORTOS_SKIP_SPOTLIGHT_EXCLUDE: '1' } })).toBe('skipped');
+ expect(existsSync(dir)).toBe(false);
+ });
+});
diff --git a/scripts/vitestCiPool.js b/scripts/vitestCiPool.js
index 97cc8219fd..214147eb0b 100644
--- a/scripts/vitestCiPool.js
+++ b/scripts/vitestCiPool.js
@@ -2,15 +2,13 @@
* Vitest worker caps for GitHub Actions. Standard Linux runners for public
* repositories are 4 vCPU / 16GB; uncapped forks oversubscribe those cores
* during transform and swap.
- * Local `npm test` stays unbounded so a developer machine can use every core.
- * That is not the supported full-suite result: an unbounded run oversubscribes
- * a loaded machine and resource-heavy cases hit their existing budgets.
- * `PORTOS_PREGATE_MAX_WORKERS=4 npm test` is the supported full local
- * validation (same cap as CI). A focused file run is evidence for that file
- * only. See docs/CONTRIBUTING.md.
- * `npm run pregate` exports PORTOS_PREGATE_MAX_WORKERS so several concurrent
- * worktree gates share one host budget (#9773). The value can only LOWER a
- * workspace's cap, never raise it past the proven one.
+ * Local runs are bounded by the CoS "max concurrent agents" setting
+ * (scripts/lib/cosWorkerBudget.js): cores / agents, never above the proven cap,
+ * so several concurrent worktree gates share one host budget (#9773). Lower the
+ * setting in the app to lighten host load; no env var is needed. A focused file
+ * run is evidence for that file only. See docs/CONTRIBUTING.md.
+ * `PORTOS_PREGATE_MAX_WORKERS` remains an internal handoff (pregate stage env,
+ * browser-fidelity's single worker); it can only LOWER a workspace's cap.
*
* fileParallelism stays at Vitest's default (true): four workers stay busy on
* independent files. The DB suite already serializes files because those
@@ -21,6 +19,7 @@
* Vitest 5 exposes `maxWorkers` only — there is no `minWorkers` / `minThreads`.
*/
import { readFileSync } from 'node:fs';
+import { cosWorkerBudget } from './lib/cosWorkerBudget.js';
export function vitestCiPool({ maxWorkers = 4 } = {}) {
if (process.env.CI) {
@@ -32,8 +31,10 @@ export function vitestCiPool({ maxWorkers = 4 } = {}) {
return { maxWorkers: ci === null ? maxWorkers : Math.min(maxWorkers, ci) };
}
const raw = process.env.PORTOS_PREGATE_MAX_WORKERS;
- const pregate = /^[1-9]\d*$/.test(raw ?? '') ? Number(raw) : null;
- return pregate === null ? {} : { maxWorkers: Math.min(maxWorkers, pregate) };
+ // An explicit handoff (pregate stage env, fidelity's single worker) wins;
+ // otherwise the CoS max-concurrent-agents setting sets the host budget.
+ const pregate = /^[1-9]\d*$/.test(raw ?? '') ? Number(raw) : cosWorkerBudget({ maxWorkers });
+ return { maxWorkers: Math.min(maxWorkers, pregate) };
}
// Keep these public Chrome/ffmpeg contracts in the full/CI runner, but give
diff --git a/scripts/vitestCiPool.test.js b/scripts/vitestCiPool.test.js
index d6a1415541..9221e6fe14 100644
--- a/scripts/vitestCiPool.test.js
+++ b/scripts/vitestCiPool.test.js
@@ -5,6 +5,7 @@ import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { fileURLToPath } from 'node:url';
+import { cosWorkerBudget } from './lib/cosWorkerBudget.js';
import { EXCLUSIVE_CAPTURE_TESTS, applyRunnerSelection, vitestCaptureProjects, vitestCiPool, vitestRunnerSelection } from './vitestCiPool.js';
describe('vitestCiPool', () => {
@@ -52,17 +53,17 @@ describe('vitestCiPool', () => {
delete process.env.CI;
for (const bad of ['0', '-1', 'abc', '2.5', '']) {
process.env.PORTOS_PREGATE_MAX_WORKERS = bad;
- expect(vitestCiPool()).toEqual({});
+ expect(vitestCiPool()).toEqual({ maxWorkers: Math.min(4, cosWorkerBudget()) });
}
process.env.CI = 'true';
process.env.PORTOS_PREGATE_MAX_WORKERS = '1';
expect(vitestCiPool()).toEqual({ maxWorkers: 4 });
});
- it('leaves local runs unbounded', () => {
+ it('bounds local runs by the CoS-derived budget when no handoff is set', () => {
delete process.env.CI;
delete process.env.PORTOS_PREGATE_MAX_WORKERS;
- expect(vitestCiPool()).toEqual({});
+ expect(vitestCiPool()).toEqual({ maxWorkers: Math.min(4, cosWorkerBudget()) });
});
it('matches the four CPUs on public GitHub-hosted runners', () => {