Files
adolf/scripts/lib/vitest-local-scheduling.mjs
alvis bedb527145
Some checks failed
ClawSweeper Dispatch / dispatch (push) Has been cancelled
CodeQL / Security High (actions) (push) Has been cancelled
CodeQL / Security High (channel-runtime-boundary) (push) Has been cancelled
CodeQL / Security High (core-auth-secrets) (push) Has been cancelled
CodeQL / Security High (mcp-process-tool-boundary) (push) Has been cancelled
CodeQL / Security High (network-ssrf-boundary) (push) Has been cancelled
CodeQL / Security High (plugin-trust-boundary) (push) Has been cancelled
CodeQL / Security High (process-exec-boundary) (push) Has been cancelled
Docs Sync Publish Repo / sync-publish-repo (push) Has been cancelled
Docs / docs (push) Has been cancelled
OpenClaw Stable Main Closeout / Resolve stable release closeout inputs (push) Has been cancelled
OpenClaw Stable Main Closeout / Verify stable main closeout (push) Has been cancelled
Workflow Sanity / no-tabs (push) Has been cancelled
Workflow Sanity / actionlint (push) Has been cancelled
Workflow Sanity / generated-doc-baselines (push) Has been cancelled
CI / runner-admission (push) Has been cancelled
CI / preflight (push) Has been cancelled
CI / security-fast (push) Has been cancelled
CI / pnpm-store-warmup (push) Has been cancelled
CI / build-artifacts (push) Has been cancelled
CI / native-i18n (push) Has been cancelled
CI / ${{ matrix.check_name }} (push) Has been cancelled
CI / ${{ matrix.checkName }} (push) Has been cancelled
CI / checks-node-compat-node22 (push) Has been cancelled
CI / check-bundled-channel-config-metadata (push) Has been cancelled
CI / check-dependencies (push) Has been cancelled
CI / check-guards (push) Has been cancelled
CI / check-lint (push) Has been cancelled
CI / check-prod-types (push) Has been cancelled
CI / check-shrinkwrap (push) Has been cancelled
CI / check-test-types (push) Has been cancelled
CI / check-additional-boundaries-a (push) Has been cancelled
CI / check-additional-boundaries-bcd (push) Has been cancelled
CI / check-additional-extension-bundled (push) Has been cancelled
CI / check-additional-extension-channels (push) Has been cancelled
CI / check-additional-extension-package-boundary (push) Has been cancelled
CI / check-additional-runtime-topology-architecture (push) Has been cancelled
CI / check-session-accessor-boundary (push) Has been cancelled
CI / check-session-transcript-reader-boundary (push) Has been cancelled
CI / check-docs (push) Has been cancelled
CI / skills-python (push) Has been cancelled
CI / macos-swift (push) Has been cancelled
CI / ios-build (push) Has been cancelled
CI / ci-timings-summary (push) Has been cancelled
Native App Locale Refresh / Refresh native fa (push) Has been cancelled
Native App Locale Refresh / Refresh native fr (push) Has been cancelled
Native App Locale Refresh / Refresh native hi (push) Has been cancelled
Native App Locale Refresh / Refresh native id (push) Has been cancelled
Native App Locale Refresh / Refresh native it (push) Has been cancelled
Native App Locale Refresh / Refresh native ja-JP (push) Has been cancelled
Control UI Locale Refresh / plan (push) Has been cancelled
Control UI Locale Refresh / Refresh ${{ matrix.locale }} (push) Has been cancelled
Control UI Locale Refresh / Commit control UI locale refresh (push) Has been cancelled
Live Media Runner Image / Build live media runner image (push) Has been cancelled
Native App Locale Refresh / Refresh native ar (push) Has been cancelled
Native App Locale Refresh / Refresh native de (push) Has been cancelled
Native App Locale Refresh / Refresh native es (push) Has been cancelled
Native App Locale Refresh / Refresh native ko (push) Has been cancelled
Native App Locale Refresh / Refresh native nl (push) Has been cancelled
Native App Locale Refresh / Refresh native pl (push) Has been cancelled
Native App Locale Refresh / Refresh native pt-BR (push) Has been cancelled
Native App Locale Refresh / Refresh native ru (push) Has been cancelled
Native App Locale Refresh / Refresh native sv (push) Has been cancelled
Native App Locale Refresh / Refresh native th (push) Has been cancelled
Native App Locale Refresh / Refresh native tr (push) Has been cancelled
Native App Locale Refresh / Refresh native uk (push) Has been cancelled
Native App Locale Refresh / Refresh native vi (push) Has been cancelled
Native App Locale Refresh / Refresh native zh-CN (push) Has been cancelled
Native App Locale Refresh / Refresh native zh-TW (push) Has been cancelled
Native App Locale Refresh / Commit native locale refresh (push) Has been cancelled
Plugin Init Scaffold Validation / Validate provider scaffold (push) Has been cancelled
Plugin NPM Release / preview_plugins_npm (push) Has been cancelled
Plugin NPM Release / Validate release publish approval (push) Has been cancelled
Plugin NPM Release / preview_plugin_pack (push) Has been cancelled
Plugin NPM Release / publish_plugins_npm (push) Has been cancelled
Sandbox Common Smoke / sandbox-common-smoke (push) Has been cancelled
Website Installer Sync / static (push) Has been cancelled
Website Installer Sync / linux-docker (push) Has been cancelled
Website Installer Sync / macos-installer (push) Has been cancelled
Website Installer Sync / windows-installer (push) Has been cancelled
Website Installer Sync / sync-website (push) Has been cancelled
Vendor OpenClaw source as Adolf fork baseline
Adolf is a fork/vendored clone of github.com/openclaw/openclaw (v2026.6.11),
free to diverge. Tree copied sans upstream .git; upstream remote added for
future syncs. Node pinned to 24 (.nvmrc); engines already require >=22.19.
Preserves docs/ARCHITECTURE.md.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LeqyaxJF2nbRXJtae2kNB2
2026-07-05 09:36:54 +00:00

231 lines
6.2 KiB
JavaScript

/** @typedef {{ cpuCount?: number, loadAverage1m?: number, totalMemoryBytes?: number, freeMemoryBytes?: number }} VitestHostInfo */
/** @typedef {{ maxWorkers: number, fileParallelism: boolean, throttledBySystem: boolean }} LocalVitestScheduling */
import os from "node:os";
const DEFAULT_LOCAL_FULL_SUITE_PARALLELISM = 4;
const LARGE_LOCAL_FULL_SUITE_PARALLELISM = 10;
const DEFAULT_LOCAL_FULL_SUITE_VITEST_WORKERS = 1;
const LARGE_LOCAL_FULL_SUITE_VITEST_WORKERS = 2;
const clamp = (value, min, max) => Math.max(min, Math.min(max, value));
function parsePositiveInt(value, label) {
const text = value?.trim();
if (!text) {
return null;
}
if (!/^\d+$/u.test(text)) {
throw new Error(`${label} must be a positive integer; got: ${value}`);
}
const parsed = Number(text);
if (!Number.isSafeInteger(parsed) || parsed <= 0) {
throw new Error(`${label} must be a positive integer; got: ${value}`);
}
return parsed;
}
function isSystemThrottleDisabled(env) {
const normalized = env.OPENCLAW_VITEST_DISABLE_SYSTEM_THROTTLE?.trim().toLowerCase();
return normalized === "1" || normalized === "true";
}
export function isCiLikeEnv(env = process.env) {
return env.CI === "true" || env.GITHUB_ACTIONS === "true";
}
export function resolveLocalVitestEnv(env = process.env) {
const normalizedLocalCheck = env.OPENCLAW_LOCAL_CHECK?.trim().toLowerCase();
if (isCiLikeEnv(env) || (normalizedLocalCheck !== "0" && normalizedLocalCheck !== "false")) {
return env;
}
return {
...env,
OPENCLAW_LOCAL_CHECK: "1",
};
}
export function detectVitestHostInfo() {
return {
cpuCount:
typeof os.availableParallelism === "function" ? os.availableParallelism() : os.cpus().length,
loadAverage1m: os.loadavg()[0] ?? 0,
totalMemoryBytes: os.totalmem(),
freeMemoryBytes: os.freemem(),
};
}
function resolveMemoryPressureWorkerLimit(system) {
const freeMemoryGb = (system.freeMemoryBytes ?? 0) / 1024 ** 3;
if (!Number.isFinite(freeMemoryGb) || freeMemoryGb <= 0) {
return null;
}
if (freeMemoryGb <= 4) {
return 1;
}
if (freeMemoryGb <= 8) {
return 2;
}
return null;
}
export function resolveLocalVitestMaxWorkers(
env = process.env,
system = detectVitestHostInfo(),
pool = "threads",
) {
return resolveLocalVitestScheduling(env, system, pool).maxWorkers;
}
/**
* @param {Record<string, string | undefined>} env
* @param {VitestHostInfo} system
* @param {"forks" | "threads"} pool
* @returns {LocalVitestScheduling}
*/
export function resolveLocalVitestScheduling(
env = process.env,
system = detectVitestHostInfo(),
pool = "threads",
) {
const override = parsePositiveInt(
env.OPENCLAW_VITEST_MAX_WORKERS ?? env.OPENCLAW_TEST_WORKERS,
env.OPENCLAW_VITEST_MAX_WORKERS === undefined
? "OPENCLAW_TEST_WORKERS"
: "OPENCLAW_VITEST_MAX_WORKERS",
);
if (override !== null) {
const maxWorkers = clamp(override, 1, 16);
return {
maxWorkers,
fileParallelism: maxWorkers > 1,
throttledBySystem: false,
};
}
const cpuCount = Math.max(1, system.cpuCount ?? 1);
const loadAverage1m = Math.max(0, system.loadAverage1m ?? 0);
const totalMemoryGb = (system.totalMemoryBytes ?? 0) / 1024 ** 3;
let inferred =
cpuCount <= 2
? 1
: cpuCount <= 4
? 2
: cpuCount <= 8
? 4
: Math.max(1, Math.floor(cpuCount * 0.75));
if (totalMemoryGb <= 16) {
inferred = Math.min(inferred, 2);
} else if (totalMemoryGb <= 32) {
inferred = Math.min(inferred, 4);
} else if (totalMemoryGb <= 64) {
inferred = Math.min(inferred, 6);
} else if (totalMemoryGb <= 128) {
inferred = Math.min(inferred, 8);
} else if (totalMemoryGb <= 256) {
inferred = Math.min(inferred, 12);
} else {
inferred = Math.min(inferred, 16);
}
const loadRatio = loadAverage1m > 0 ? loadAverage1m / cpuCount : 0;
if (loadRatio >= 1) {
inferred = Math.max(1, Math.floor(inferred / 2));
} else if (loadRatio >= 0.75) {
inferred = Math.max(1, inferred - 2);
} else if (loadRatio >= 0.5) {
inferred = Math.max(1, inferred - 1);
}
if (pool === "forks") {
inferred = Math.min(inferred, 8);
}
inferred = clamp(inferred, 1, 16);
if (isSystemThrottleDisabled(env)) {
return {
maxWorkers: inferred,
fileParallelism: true,
throttledBySystem: false,
};
}
const memoryPressureLimit = resolveMemoryPressureWorkerLimit(system);
if (memoryPressureLimit !== null && inferred > memoryPressureLimit) {
const maxWorkers = memoryPressureLimit;
return {
maxWorkers,
fileParallelism: maxWorkers > 1,
throttledBySystem: true,
};
}
if (loadRatio >= 1) {
const maxWorkers = Math.max(1, Math.floor(inferred / 2));
return {
maxWorkers,
fileParallelism: maxWorkers > 1,
throttledBySystem: maxWorkers < inferred,
};
}
if (loadRatio >= 0.75) {
const maxWorkers = Math.max(2, Math.ceil(inferred * 0.75));
return {
maxWorkers,
fileParallelism: true,
throttledBySystem: maxWorkers < inferred,
};
}
return {
maxWorkers: inferred,
fileParallelism: true,
throttledBySystem: false,
};
}
export function shouldUseLargeLocalFullSuiteProfile(
env = process.env,
system = detectVitestHostInfo(),
) {
if (isCiLikeEnv(env)) {
return false;
}
const scheduling = resolveLocalVitestScheduling(env, system, "threads");
return scheduling.maxWorkers >= 5 && !scheduling.throttledBySystem;
}
export function resolveLocalFullSuiteProfile(env = process.env, system = detectVitestHostInfo()) {
if (!isSystemThrottleDisabled(env)) {
const memoryPressureLimit = resolveMemoryPressureWorkerLimit(system);
if (memoryPressureLimit === 1) {
return {
shardParallelism: 1,
vitestMaxWorkers: 1,
};
}
if (memoryPressureLimit === 2) {
return {
shardParallelism: 2,
vitestMaxWorkers: 1,
};
}
}
if (shouldUseLargeLocalFullSuiteProfile(env, system)) {
return {
shardParallelism: LARGE_LOCAL_FULL_SUITE_PARALLELISM,
vitestMaxWorkers: LARGE_LOCAL_FULL_SUITE_VITEST_WORKERS,
};
}
return {
shardParallelism: DEFAULT_LOCAL_FULL_SUITE_PARALLELISM,
vitestMaxWorkers: DEFAULT_LOCAL_FULL_SUITE_VITEST_WORKERS,
};
}