mirror of
https://github.com/openclaw/openclaw.git
synced 2026-03-27 18:01:46 +00:00
test: introduce planner-backed test runner, stabilize local builds (#54650)
* test: stabilize ci and local vitest workers * test: introduce planner-backed test runner * test: address planner review follow-ups * test: derive planner budgets from host capabilities * test: restore planner filter helper import * test: align planner explain output with execution * test: keep low profile as serial alias * test: restrict explicit planner file targets * test: clean planner exits and pnpm launch * test: tighten wrapper flag validation * ci: gate heavy fanout on check * test: key shard assignments by unit identity * ci(bun): shard vitest lanes further * test: restore ci overlap and stabilize planner tests * test: relax planner output worker assertions * test: reset plugin runtime state in optional tools suite * ci: split macos node and swift jobs * test: honor no-isolate top-level concurrency budgets * ci: fix macos swift format lint * test: cap max-profile top-level concurrency * ci: shard macos node checks * ci: use four macos node shards * test: normalize explain targets before classification
This commit is contained in:
File diff suppressed because it is too large
Load Diff
187
scripts/test-planner/catalog.mjs
Normal file
187
scripts/test-planner/catalog.mjs
Normal file
@@ -0,0 +1,187 @@
|
||||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
import { channelTestPrefixes } from "../../vitest.channel-paths.mjs";
|
||||
import { isUnitConfigTestFile } from "../../vitest.unit-paths.mjs";
|
||||
import { dedupeFilesPreserveOrder, loadTestRunnerBehavior } from "../test-runner-manifest.mjs";
|
||||
|
||||
const baseConfigPrefixes = ["src/agents/", "src/auto-reply/", "src/commands/", "test/", "ui/"];
|
||||
|
||||
export const normalizeRepoPath = (value) => value.split(path.sep).join("/");
|
||||
|
||||
const toRepoRelativePath = (value) => {
|
||||
const relativePath = normalizeRepoPath(path.relative(process.cwd(), path.resolve(value)));
|
||||
return relativePath.startsWith("../") || relativePath === ".." ? null : relativePath;
|
||||
};
|
||||
|
||||
const walkTestFiles = (rootDir) => {
|
||||
if (!fs.existsSync(rootDir)) {
|
||||
return [];
|
||||
}
|
||||
const entries = fs.readdirSync(rootDir, { withFileTypes: true });
|
||||
const files = [];
|
||||
for (const entry of entries) {
|
||||
const fullPath = path.join(rootDir, entry.name);
|
||||
if (entry.isDirectory()) {
|
||||
files.push(...walkTestFiles(fullPath));
|
||||
continue;
|
||||
}
|
||||
if (!entry.isFile()) {
|
||||
continue;
|
||||
}
|
||||
if (
|
||||
fullPath.endsWith(".test.ts") ||
|
||||
fullPath.endsWith(".live.test.ts") ||
|
||||
fullPath.endsWith(".e2e.test.ts")
|
||||
) {
|
||||
files.push(normalizeRepoPath(fullPath));
|
||||
}
|
||||
}
|
||||
return files;
|
||||
};
|
||||
|
||||
export function loadTestCatalog() {
|
||||
const behaviorManifest = loadTestRunnerBehavior();
|
||||
const existingFiles = (entries) =>
|
||||
entries.map((entry) => entry.file).filter((file) => fs.existsSync(file));
|
||||
const existingUnitConfigFiles = (entries) => existingFiles(entries).filter(isUnitConfigTestFile);
|
||||
const baseThreadPinnedFiles = existingFiles(behaviorManifest.base?.threadPinned ?? []);
|
||||
const channelIsolatedManifestFiles = existingFiles(behaviorManifest.channels?.isolated ?? []);
|
||||
const channelIsolatedPrefixes = behaviorManifest.channels?.isolatedPrefixes ?? [];
|
||||
const extensionForkIsolatedFiles = existingFiles(behaviorManifest.extensions?.isolated ?? []);
|
||||
const unitForkIsolatedFiles = existingUnitConfigFiles(behaviorManifest.unit.isolated);
|
||||
const unitThreadPinnedFiles = existingUnitConfigFiles(behaviorManifest.unit.threadPinned);
|
||||
const unitBehaviorOverrideSet = new Set([...unitForkIsolatedFiles, ...unitThreadPinnedFiles]);
|
||||
const allKnownTestFiles = [
|
||||
...new Set([
|
||||
...walkTestFiles("src"),
|
||||
...walkTestFiles("extensions"),
|
||||
...walkTestFiles("test"),
|
||||
...walkTestFiles(path.join("ui", "src", "ui")),
|
||||
]),
|
||||
];
|
||||
const channelIsolatedFiles = dedupeFilesPreserveOrder([
|
||||
...channelIsolatedManifestFiles,
|
||||
...allKnownTestFiles.filter((file) =>
|
||||
channelIsolatedPrefixes.some((prefix) => file.startsWith(prefix)),
|
||||
),
|
||||
]);
|
||||
const channelIsolatedFileSet = new Set(channelIsolatedFiles);
|
||||
const extensionForkIsolatedFileSet = new Set(extensionForkIsolatedFiles);
|
||||
const baseThreadPinnedFileSet = new Set(baseThreadPinnedFiles);
|
||||
const unitThreadPinnedFileSet = new Set(unitThreadPinnedFiles);
|
||||
const unitForkIsolatedFileSet = new Set(unitForkIsolatedFiles);
|
||||
|
||||
const classifyTestFile = (fileFilter, options = {}) => {
|
||||
const normalizedFile = normalizeRepoPath(fileFilter);
|
||||
const reasons = [];
|
||||
const isolated =
|
||||
options.unitMemoryIsolatedFiles?.includes(normalizedFile) ||
|
||||
unitForkIsolatedFileSet.has(normalizedFile) ||
|
||||
extensionForkIsolatedFileSet.has(normalizedFile) ||
|
||||
channelIsolatedFileSet.has(normalizedFile);
|
||||
if (options.unitMemoryIsolatedFiles?.includes(normalizedFile)) {
|
||||
reasons.push("unit-memory-isolated");
|
||||
}
|
||||
if (unitForkIsolatedFileSet.has(normalizedFile)) {
|
||||
reasons.push("unit-isolated-manifest");
|
||||
}
|
||||
if (extensionForkIsolatedFileSet.has(normalizedFile)) {
|
||||
reasons.push("extensions-isolated-manifest");
|
||||
}
|
||||
if (channelIsolatedFileSet.has(normalizedFile)) {
|
||||
reasons.push("channels-isolated-rule");
|
||||
}
|
||||
|
||||
let surface = "base";
|
||||
if (isUnitConfigTestFile(normalizedFile)) {
|
||||
surface = "unit";
|
||||
} else if (normalizedFile.endsWith(".live.test.ts")) {
|
||||
surface = "live";
|
||||
} else if (normalizedFile.endsWith(".e2e.test.ts")) {
|
||||
surface = "e2e";
|
||||
} else if (channelTestPrefixes.some((prefix) => normalizedFile.startsWith(prefix))) {
|
||||
surface = "channels";
|
||||
} else if (normalizedFile.startsWith("extensions/")) {
|
||||
surface = "extensions";
|
||||
} else if (normalizedFile.startsWith("src/gateway/")) {
|
||||
surface = "gateway";
|
||||
} else if (baseConfigPrefixes.some((prefix) => normalizedFile.startsWith(prefix))) {
|
||||
surface = "base";
|
||||
} else if (normalizedFile.startsWith("src/")) {
|
||||
surface = "unit";
|
||||
}
|
||||
if (surface === "unit") {
|
||||
reasons.push("unit-surface");
|
||||
} else if (surface !== "base") {
|
||||
reasons.push(`${surface}-surface`);
|
||||
} else {
|
||||
reasons.push("base-surface");
|
||||
}
|
||||
|
||||
const legacyBasePinned = baseThreadPinnedFileSet.has(normalizedFile);
|
||||
if (legacyBasePinned) {
|
||||
reasons.push("base-pinned-manifest");
|
||||
}
|
||||
if (unitThreadPinnedFileSet.has(normalizedFile)) {
|
||||
reasons.push("unit-pinned-manifest");
|
||||
}
|
||||
|
||||
return {
|
||||
file: normalizedFile,
|
||||
surface,
|
||||
isolated,
|
||||
legacyBasePinned,
|
||||
reasons,
|
||||
};
|
||||
};
|
||||
|
||||
const resolveFilterMatches = (fileFilter) => {
|
||||
const normalizedFilter = normalizeRepoPath(fileFilter);
|
||||
const repoRelativeFilter = toRepoRelativePath(fileFilter);
|
||||
if (fs.existsSync(fileFilter)) {
|
||||
const stats = fs.statSync(fileFilter);
|
||||
if (stats.isFile()) {
|
||||
if (repoRelativeFilter && allKnownTestFiles.includes(repoRelativeFilter)) {
|
||||
return [repoRelativeFilter];
|
||||
}
|
||||
throw new Error(`Explicit path ${fileFilter} is not a known test file.`);
|
||||
}
|
||||
if (stats.isDirectory()) {
|
||||
if (!repoRelativeFilter) {
|
||||
throw new Error(`Explicit path ${fileFilter} is outside the repo test roots.`);
|
||||
}
|
||||
const prefix = repoRelativeFilter.endsWith("/")
|
||||
? repoRelativeFilter
|
||||
: `${repoRelativeFilter}/`;
|
||||
const matches = allKnownTestFiles.filter((file) => file.startsWith(prefix));
|
||||
if (matches.length === 0) {
|
||||
throw new Error(`Explicit path ${fileFilter} does not contain known test files.`);
|
||||
}
|
||||
return matches;
|
||||
}
|
||||
}
|
||||
if (/[*?[\]{}]/.test(normalizedFilter)) {
|
||||
return allKnownTestFiles.filter((file) => path.matchesGlob(file, normalizedFilter));
|
||||
}
|
||||
return allKnownTestFiles.filter((file) => file.includes(normalizedFilter));
|
||||
};
|
||||
|
||||
return {
|
||||
allKnownTestFiles,
|
||||
allKnownUnitFiles: allKnownTestFiles.filter((file) => isUnitConfigTestFile(file)),
|
||||
baseThreadPinnedFiles,
|
||||
channelIsolatedFiles,
|
||||
channelIsolatedFileSet,
|
||||
channelTestPrefixes,
|
||||
extensionForkIsolatedFiles,
|
||||
extensionForkIsolatedFileSet,
|
||||
unitBehaviorOverrideSet,
|
||||
unitForkIsolatedFiles,
|
||||
unitThreadPinnedFiles,
|
||||
baseThreadPinnedFileSet,
|
||||
classifyTestFile,
|
||||
resolveFilterMatches,
|
||||
};
|
||||
}
|
||||
|
||||
export const testSurfaces = ["unit", "extensions", "channels", "gateway", "live", "e2e", "base"];
|
||||
668
scripts/test-planner/executor.mjs
Normal file
668
scripts/test-planner/executor.mjs
Normal file
@@ -0,0 +1,668 @@
|
||||
import { spawn } from "node:child_process";
|
||||
import fs from "node:fs";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import {
|
||||
getProcessTreeRecords,
|
||||
parseCompletedTestFileLines,
|
||||
sampleProcessTreeRssKb,
|
||||
} from "../test-parallel-memory.mjs";
|
||||
import {
|
||||
appendCapturedOutput,
|
||||
formatCapturedOutputTail,
|
||||
hasFatalTestRunOutput,
|
||||
resolveTestRunExitCode,
|
||||
} from "../test-parallel-utils.mjs";
|
||||
import { countExplicitEntryFilters, getExplicitEntryFilters } from "./vitest-args.mjs";
|
||||
|
||||
export function resolvePnpmCommandInvocation(options = {}) {
|
||||
const npmExecPath = typeof options.npmExecPath === "string" ? options.npmExecPath.trim() : "";
|
||||
if (npmExecPath && path.isAbsolute(npmExecPath)) {
|
||||
const npmExecBase = path.basename(npmExecPath).toLowerCase();
|
||||
if (npmExecBase.startsWith("pnpm")) {
|
||||
return {
|
||||
command: options.nodeExecPath || process.execPath,
|
||||
args: [npmExecPath],
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (options.platform === "win32") {
|
||||
return {
|
||||
command: options.comSpec || "cmd.exe",
|
||||
args: ["/d", "/s", "/c", "pnpm.cmd"],
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
command: "pnpm",
|
||||
args: [],
|
||||
};
|
||||
}
|
||||
|
||||
const sanitizeArtifactName = (value) => {
|
||||
const normalized = value
|
||||
.trim()
|
||||
.replace(/[^a-z0-9._-]+/giu, "-")
|
||||
.replace(/^-+|-+$/gu, "");
|
||||
return normalized || "artifact";
|
||||
};
|
||||
|
||||
const DEFAULT_CI_MAX_OLD_SPACE_SIZE_MB = 4096;
|
||||
const WARNING_SUPPRESSION_FLAGS = [
|
||||
"--disable-warning=ExperimentalWarning",
|
||||
"--disable-warning=DEP0040",
|
||||
"--disable-warning=DEP0060",
|
||||
"--disable-warning=MaxListenersExceededWarning",
|
||||
];
|
||||
|
||||
const formatElapsedMs = (elapsedMs) =>
|
||||
elapsedMs >= 1000 ? `${(elapsedMs / 1000).toFixed(1)}s` : `${Math.round(elapsedMs)}ms`;
|
||||
const formatMemoryKb = (rssKb) =>
|
||||
rssKb >= 1024 ** 2
|
||||
? `${(rssKb / 1024 ** 2).toFixed(2)}GiB`
|
||||
: rssKb >= 1024
|
||||
? `${(rssKb / 1024).toFixed(1)}MiB`
|
||||
: `${rssKb}KiB`;
|
||||
const formatMemoryDeltaKb = (rssKb) =>
|
||||
`${rssKb >= 0 ? "+" : "-"}${formatMemoryKb(Math.abs(rssKb))}`;
|
||||
|
||||
export function createExecutionArtifacts(env = process.env) {
|
||||
let tempArtifactDir = null;
|
||||
const ensureTempArtifactDir = () => {
|
||||
if (tempArtifactDir === null) {
|
||||
tempArtifactDir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-test-parallel-"));
|
||||
}
|
||||
return tempArtifactDir;
|
||||
};
|
||||
const writeTempJsonArtifact = (name, value) => {
|
||||
const filePath = path.join(ensureTempArtifactDir(), `${sanitizeArtifactName(name)}.json`);
|
||||
fs.writeFileSync(filePath, `${JSON.stringify(value)}\n`, "utf8");
|
||||
return filePath;
|
||||
};
|
||||
const cleanupTempArtifacts = () => {
|
||||
if (tempArtifactDir === null) {
|
||||
return;
|
||||
}
|
||||
if (env.OPENCLAW_TEST_KEEP_TEMP_ARTIFACTS === "1") {
|
||||
console.error(`[test-parallel] keeping temp artifacts at ${tempArtifactDir}`);
|
||||
return;
|
||||
}
|
||||
fs.rmSync(tempArtifactDir, { recursive: true, force: true });
|
||||
tempArtifactDir = null;
|
||||
};
|
||||
return { ensureTempArtifactDir, writeTempJsonArtifact, cleanupTempArtifacts };
|
||||
}
|
||||
|
||||
const ensureNodeOptionFlag = (nodeOptions, flagPrefix, nextValue) =>
|
||||
nodeOptions.includes(flagPrefix) ? nodeOptions : `${nodeOptions} ${nextValue}`.trim();
|
||||
|
||||
const isNodeLikeProcess = (command) => /(?:^|\/)node(?:$|\.exe$)/iu.test(command);
|
||||
|
||||
const getShardLabel = (args) => {
|
||||
const shardIndex = args.findIndex((arg) => arg === "--shard");
|
||||
if (shardIndex < 0) {
|
||||
return "";
|
||||
}
|
||||
return typeof args[shardIndex + 1] === "string" ? args[shardIndex + 1] : "";
|
||||
};
|
||||
|
||||
export function formatPlanOutput(plan) {
|
||||
return [
|
||||
`runtime=${plan.runtimeCapabilities.runtimeProfileName} mode=${plan.runtimeCapabilities.mode} intent=${plan.runtimeCapabilities.intentProfile} memoryBand=${plan.runtimeCapabilities.memoryBand} loadBand=${plan.runtimeCapabilities.loadBand} vitestMaxWorkers=${String(plan.executionBudget.vitestMaxWorkers ?? "default")} topLevelParallel=${plan.topLevelParallelEnabled ? String(plan.topLevelParallelLimit) : "off"}`,
|
||||
...plan.selectedUnits.map(
|
||||
(unit) =>
|
||||
`${unit.id} filters=${String(countExplicitEntryFilters(unit.args) ?? "all")} maxWorkers=${String(
|
||||
unit.maxWorkers ?? "default",
|
||||
)} surface=${unit.surface} isolate=${unit.isolate ? "yes" : "no"} pool=${unit.pool}`,
|
||||
),
|
||||
].join("\n");
|
||||
}
|
||||
|
||||
export function formatExplanation(explanation) {
|
||||
return [
|
||||
`file=${explanation.file}`,
|
||||
`runtime=${explanation.runtimeProfile} intent=${explanation.intentProfile} memoryBand=${explanation.memoryBand} loadBand=${explanation.loadBand}`,
|
||||
`surface=${explanation.surface}`,
|
||||
`isolate=${explanation.isolate ? "yes" : "no"}`,
|
||||
`pool=${explanation.pool}`,
|
||||
`maxWorkers=${String(explanation.maxWorkers ?? "default")}`,
|
||||
`reasons=${explanation.reasons.join(",")}`,
|
||||
`command=${explanation.args.join(" ")}`,
|
||||
].join("\n");
|
||||
}
|
||||
|
||||
export async function executePlan(plan, options = {}) {
|
||||
const env = options.env ?? process.env;
|
||||
const artifacts = options.artifacts ?? createExecutionArtifacts(env);
|
||||
const pnpmInvocation = resolvePnpmCommandInvocation({
|
||||
npmExecPath: env.npm_execpath,
|
||||
nodeExecPath: process.execPath,
|
||||
platform: process.platform,
|
||||
comSpec: env.ComSpec,
|
||||
});
|
||||
const children = new Set();
|
||||
const windowsCiArgs = plan.runtimeCapabilities.isWindowsCi
|
||||
? ["--dangerouslyIgnoreUnhandledErrors"]
|
||||
: [];
|
||||
const silentArgs = env.OPENCLAW_TEST_SHOW_PASSED_LOGS === "1" ? [] : ["--silent=passed-only"];
|
||||
const rawMemoryTrace = env.OPENCLAW_TEST_MEMORY_TRACE?.trim().toLowerCase();
|
||||
const memoryTraceEnabled =
|
||||
process.platform !== "win32" &&
|
||||
(rawMemoryTrace === "1" ||
|
||||
rawMemoryTrace === "true" ||
|
||||
(rawMemoryTrace !== "0" && rawMemoryTrace !== "false" && plan.runtimeCapabilities.isCI));
|
||||
const parseEnvNumber = (name, fallback) => {
|
||||
const parsed = Number.parseInt(env[name] ?? "", 10);
|
||||
return Number.isFinite(parsed) && parsed >= 0 ? parsed : fallback;
|
||||
};
|
||||
const memoryTracePollMs = Math.max(
|
||||
250,
|
||||
parseEnvNumber("OPENCLAW_TEST_MEMORY_TRACE_POLL_MS", 1000),
|
||||
);
|
||||
const memoryTraceTopCount = Math.max(
|
||||
1,
|
||||
parseEnvNumber("OPENCLAW_TEST_MEMORY_TRACE_TOP_COUNT", 6),
|
||||
);
|
||||
const requestedHeapSnapshotIntervalMs = Math.max(
|
||||
0,
|
||||
parseEnvNumber("OPENCLAW_TEST_HEAPSNAPSHOT_INTERVAL_MS", 0),
|
||||
);
|
||||
const heapSnapshotMinIntervalMs = 1000;
|
||||
const heapSnapshotIntervalMs =
|
||||
requestedHeapSnapshotIntervalMs > 0
|
||||
? Math.max(heapSnapshotMinIntervalMs, requestedHeapSnapshotIntervalMs)
|
||||
: 0;
|
||||
const heapSnapshotEnabled =
|
||||
process.platform !== "win32" && heapSnapshotIntervalMs >= heapSnapshotMinIntervalMs;
|
||||
const heapSnapshotSignal = env.OPENCLAW_TEST_HEAPSNAPSHOT_SIGNAL?.trim() || "SIGUSR2";
|
||||
const heapSnapshotBaseDir = heapSnapshotEnabled
|
||||
? path.resolve(
|
||||
env.OPENCLAW_TEST_HEAPSNAPSHOT_DIR?.trim() ||
|
||||
path.join(os.tmpdir(), `openclaw-heapsnapshots-${Date.now()}`),
|
||||
)
|
||||
: null;
|
||||
const maxOldSpaceSizeMb = (() => {
|
||||
const raw = env.OPENCLAW_TEST_MAX_OLD_SPACE_SIZE_MB ?? "";
|
||||
const parsed = Number.parseInt(raw, 10);
|
||||
if (Number.isFinite(parsed) && parsed > 0) {
|
||||
return parsed;
|
||||
}
|
||||
if (plan.runtimeCapabilities.isCI && !plan.runtimeCapabilities.isWindows) {
|
||||
return DEFAULT_CI_MAX_OLD_SPACE_SIZE_MB;
|
||||
}
|
||||
return null;
|
||||
})();
|
||||
|
||||
const shutdown = (signal) => {
|
||||
for (const child of children) {
|
||||
child.kill(signal);
|
||||
}
|
||||
};
|
||||
process.on("SIGINT", () => shutdown("SIGINT"));
|
||||
process.on("SIGTERM", () => shutdown("SIGTERM"));
|
||||
process.on("exit", artifacts.cleanupTempArtifacts);
|
||||
|
||||
const runOnce = (unit, extraArgs = []) =>
|
||||
new Promise((resolve) => {
|
||||
const startedAt = Date.now();
|
||||
const entryArgs = unit.args;
|
||||
const explicitEntryFilters = getExplicitEntryFilters(entryArgs);
|
||||
const args = unit.maxWorkers
|
||||
? [
|
||||
...entryArgs,
|
||||
"--maxWorkers",
|
||||
String(unit.maxWorkers),
|
||||
...silentArgs,
|
||||
...windowsCiArgs,
|
||||
...extraArgs,
|
||||
]
|
||||
: [...entryArgs, ...silentArgs, ...windowsCiArgs, ...extraArgs];
|
||||
const spawnArgs = [...pnpmInvocation.args, ...args];
|
||||
const shardLabel = getShardLabel(extraArgs);
|
||||
const artifactStem = [
|
||||
sanitizeArtifactName(unit.id),
|
||||
shardLabel ? `shard-${sanitizeArtifactName(shardLabel)}` : "",
|
||||
String(startedAt),
|
||||
]
|
||||
.filter(Boolean)
|
||||
.join("-");
|
||||
const laneLogPath = path.join(artifacts.ensureTempArtifactDir(), `${artifactStem}.log`);
|
||||
const laneLogStream = fs.createWriteStream(laneLogPath, { flags: "w" });
|
||||
laneLogStream.write(`[test-parallel] entry=${unit.id}\n`);
|
||||
laneLogStream.write(`[test-parallel] cwd=${process.cwd()}\n`);
|
||||
laneLogStream.write(
|
||||
`[test-parallel] command=${[pnpmInvocation.command, ...spawnArgs].join(" ")}\n\n`,
|
||||
);
|
||||
console.log(
|
||||
`[test-parallel] start ${unit.id} workers=${unit.maxWorkers ?? "default"} filters=${String(
|
||||
countExplicitEntryFilters(entryArgs) ?? "all",
|
||||
)}`,
|
||||
);
|
||||
const nodeOptions = env.NODE_OPTIONS ?? "";
|
||||
const nextNodeOptions = WARNING_SUPPRESSION_FLAGS.reduce(
|
||||
(acc, flag) => (acc.includes(flag) ? acc : `${acc} ${flag}`.trim()),
|
||||
nodeOptions,
|
||||
);
|
||||
const heapSnapshotDir =
|
||||
heapSnapshotBaseDir === null ? null : path.join(heapSnapshotBaseDir, unit.id);
|
||||
let resolvedNodeOptions =
|
||||
maxOldSpaceSizeMb && !nextNodeOptions.includes("--max-old-space-size=")
|
||||
? `${nextNodeOptions} --max-old-space-size=${maxOldSpaceSizeMb}`.trim()
|
||||
: nextNodeOptions;
|
||||
if (heapSnapshotEnabled && heapSnapshotDir) {
|
||||
try {
|
||||
fs.mkdirSync(heapSnapshotDir, { recursive: true });
|
||||
} catch (err) {
|
||||
console.error(
|
||||
`[test-parallel] failed to create heap snapshot dir ${heapSnapshotDir}: ${String(err)}`,
|
||||
);
|
||||
resolve(1);
|
||||
return;
|
||||
}
|
||||
resolvedNodeOptions = ensureNodeOptionFlag(
|
||||
resolvedNodeOptions,
|
||||
"--diagnostic-dir=",
|
||||
`--diagnostic-dir=${heapSnapshotDir}`,
|
||||
);
|
||||
resolvedNodeOptions = ensureNodeOptionFlag(
|
||||
resolvedNodeOptions,
|
||||
"--heapsnapshot-signal=",
|
||||
`--heapsnapshot-signal=${heapSnapshotSignal}`,
|
||||
);
|
||||
}
|
||||
let output = "";
|
||||
let fatalSeen = false;
|
||||
let childError = null;
|
||||
let child;
|
||||
let pendingLine = "";
|
||||
let memoryPollTimer = null;
|
||||
let heapSnapshotTimer = null;
|
||||
const memoryFileRecords = [];
|
||||
let initialTreeSample = null;
|
||||
let latestTreeSample = null;
|
||||
let peakTreeSample = null;
|
||||
let heapSnapshotSequence = 0;
|
||||
const updatePeakTreeSample = (sample, reason) => {
|
||||
if (!sample) {
|
||||
return;
|
||||
}
|
||||
if (!peakTreeSample || sample.rssKb > peakTreeSample.rssKb) {
|
||||
peakTreeSample = { ...sample, reason };
|
||||
}
|
||||
};
|
||||
const triggerHeapSnapshot = (reason) => {
|
||||
if (!heapSnapshotEnabled || !child?.pid || !heapSnapshotDir) {
|
||||
return;
|
||||
}
|
||||
const records = getProcessTreeRecords(child.pid) ?? [];
|
||||
const targetPids = records
|
||||
.filter((record) => record.pid !== process.pid && isNodeLikeProcess(record.command))
|
||||
.map((record) => record.pid);
|
||||
if (targetPids.length === 0) {
|
||||
return;
|
||||
}
|
||||
heapSnapshotSequence += 1;
|
||||
let signaledCount = 0;
|
||||
for (const pid of targetPids) {
|
||||
try {
|
||||
process.kill(pid, heapSnapshotSignal);
|
||||
signaledCount += 1;
|
||||
} catch {}
|
||||
}
|
||||
if (signaledCount > 0) {
|
||||
console.log(
|
||||
`[test-parallel][heap] ${unit.id} seq=${String(heapSnapshotSequence)} reason=${reason} signaled=${String(
|
||||
signaledCount,
|
||||
)}/${String(targetPids.length)} dir=${heapSnapshotDir}`,
|
||||
);
|
||||
}
|
||||
};
|
||||
const captureTreeSample = (reason) => {
|
||||
if (!memoryTraceEnabled || !child?.pid) {
|
||||
return null;
|
||||
}
|
||||
const sample = sampleProcessTreeRssKb(child.pid);
|
||||
if (!sample) {
|
||||
return null;
|
||||
}
|
||||
latestTreeSample = sample;
|
||||
if (!initialTreeSample) {
|
||||
initialTreeSample = sample;
|
||||
}
|
||||
updatePeakTreeSample(sample, reason);
|
||||
return sample;
|
||||
};
|
||||
const logMemoryTraceForText = (text) => {
|
||||
if (!memoryTraceEnabled) {
|
||||
return;
|
||||
}
|
||||
const combined = `${pendingLine}${text}`;
|
||||
const lines = combined.split(/\r?\n/u);
|
||||
pendingLine = lines.pop() ?? "";
|
||||
const completedFiles = parseCompletedTestFileLines(lines.join("\n"));
|
||||
for (const completedFile of completedFiles) {
|
||||
const sample = captureTreeSample(completedFile.file);
|
||||
if (!sample) {
|
||||
continue;
|
||||
}
|
||||
const previousRssKb =
|
||||
memoryFileRecords.length > 0
|
||||
? (memoryFileRecords.at(-1)?.rssKb ?? initialTreeSample?.rssKb ?? sample.rssKb)
|
||||
: (initialTreeSample?.rssKb ?? sample.rssKb);
|
||||
const deltaKb = sample.rssKb - previousRssKb;
|
||||
const record = {
|
||||
...completedFile,
|
||||
rssKb: sample.rssKb,
|
||||
processCount: sample.processCount,
|
||||
deltaKb,
|
||||
};
|
||||
memoryFileRecords.push(record);
|
||||
console.log(
|
||||
`[test-parallel][mem] ${unit.id} file=${record.file} rss=${formatMemoryKb(
|
||||
record.rssKb,
|
||||
)} delta=${formatMemoryDeltaKb(record.deltaKb)} peak=${formatMemoryKb(
|
||||
peakTreeSample?.rssKb ?? record.rssKb,
|
||||
)} procs=${record.processCount}${record.durationMs ? ` duration=${formatElapsedMs(record.durationMs)}` : ""}`,
|
||||
);
|
||||
}
|
||||
};
|
||||
const logMemoryTraceSummary = () => {
|
||||
if (!memoryTraceEnabled) {
|
||||
return;
|
||||
}
|
||||
captureTreeSample("close");
|
||||
const fallbackRecord =
|
||||
memoryFileRecords.length === 0 &&
|
||||
explicitEntryFilters.length === 1 &&
|
||||
latestTreeSample &&
|
||||
initialTreeSample
|
||||
? [
|
||||
{
|
||||
file: explicitEntryFilters[0],
|
||||
deltaKb: latestTreeSample.rssKb - initialTreeSample.rssKb,
|
||||
},
|
||||
]
|
||||
: [];
|
||||
const totalDeltaKb =
|
||||
initialTreeSample && latestTreeSample
|
||||
? latestTreeSample.rssKb - initialTreeSample.rssKb
|
||||
: 0;
|
||||
const topGrowthFiles = [...memoryFileRecords, ...fallbackRecord]
|
||||
.filter((record) => record.deltaKb > 0 && typeof record.file === "string")
|
||||
.toSorted((left, right) => right.deltaKb - left.deltaKb)
|
||||
.slice(0, memoryTraceTopCount)
|
||||
.map((record) => `${record.file}:${formatMemoryDeltaKb(record.deltaKb)}`);
|
||||
console.log(
|
||||
`[test-parallel][mem] summary ${unit.id} files=${memoryFileRecords.length} peak=${formatMemoryKb(
|
||||
peakTreeSample?.rssKb ?? 0,
|
||||
)} totalDelta=${formatMemoryDeltaKb(totalDeltaKb)} peakAt=${
|
||||
peakTreeSample?.reason ?? "n/a"
|
||||
} top=${topGrowthFiles.length > 0 ? topGrowthFiles.join(", ") : "none"}`,
|
||||
);
|
||||
};
|
||||
try {
|
||||
child = spawn(pnpmInvocation.command, spawnArgs, {
|
||||
stdio: ["inherit", "pipe", "pipe"],
|
||||
env: {
|
||||
...env,
|
||||
...unit.env,
|
||||
VITEST_GROUP: unit.id,
|
||||
NODE_OPTIONS: resolvedNodeOptions,
|
||||
},
|
||||
shell: false,
|
||||
});
|
||||
captureTreeSample("spawn");
|
||||
if (memoryTraceEnabled) {
|
||||
memoryPollTimer = setInterval(() => {
|
||||
captureTreeSample("poll");
|
||||
}, memoryTracePollMs);
|
||||
}
|
||||
if (heapSnapshotEnabled) {
|
||||
heapSnapshotTimer = setInterval(() => {
|
||||
triggerHeapSnapshot("interval");
|
||||
}, heapSnapshotIntervalMs);
|
||||
}
|
||||
} catch (err) {
|
||||
laneLogStream.end();
|
||||
console.error(`[test-parallel] spawn failed: ${String(err)}`);
|
||||
resolve(1);
|
||||
return;
|
||||
}
|
||||
children.add(child);
|
||||
child.stdout?.on("data", (chunk) => {
|
||||
const text = chunk.toString();
|
||||
fatalSeen ||= hasFatalTestRunOutput(`${output}${text}`);
|
||||
output = appendCapturedOutput(output, text);
|
||||
laneLogStream.write(text);
|
||||
logMemoryTraceForText(text);
|
||||
process.stdout.write(chunk);
|
||||
});
|
||||
child.stderr?.on("data", (chunk) => {
|
||||
const text = chunk.toString();
|
||||
fatalSeen ||= hasFatalTestRunOutput(`${output}${text}`);
|
||||
output = appendCapturedOutput(output, text);
|
||||
laneLogStream.write(text);
|
||||
logMemoryTraceForText(text);
|
||||
process.stderr.write(chunk);
|
||||
});
|
||||
child.on("error", (err) => {
|
||||
childError = err;
|
||||
laneLogStream.write(`\n[test-parallel] child error: ${String(err)}\n`);
|
||||
console.error(`[test-parallel] child error: ${String(err)}`);
|
||||
});
|
||||
child.on("close", (code, signal) => {
|
||||
if (memoryPollTimer) {
|
||||
clearInterval(memoryPollTimer);
|
||||
}
|
||||
if (heapSnapshotTimer) {
|
||||
clearInterval(heapSnapshotTimer);
|
||||
}
|
||||
children.delete(child);
|
||||
const resolvedCode = resolveTestRunExitCode({
|
||||
code,
|
||||
signal,
|
||||
output,
|
||||
fatalSeen,
|
||||
childError,
|
||||
});
|
||||
const elapsedMs = Date.now() - startedAt;
|
||||
logMemoryTraceSummary();
|
||||
if (resolvedCode !== 0) {
|
||||
const failureTail = formatCapturedOutputTail(output);
|
||||
const failureArtifactPath = artifacts.writeTempJsonArtifact(`${artifactStem}-failure`, {
|
||||
entry: unit.id,
|
||||
command: [pnpmInvocation.command, ...spawnArgs],
|
||||
elapsedMs,
|
||||
error: childError ? String(childError) : null,
|
||||
exitCode: resolvedCode,
|
||||
fatalSeen,
|
||||
logPath: laneLogPath,
|
||||
outputTail: failureTail,
|
||||
signal: signal ?? null,
|
||||
});
|
||||
if (failureTail) {
|
||||
console.error(`[test-parallel] failure tail ${unit.id}\n${failureTail}`);
|
||||
}
|
||||
console.error(
|
||||
`[test-parallel] failure artifacts ${unit.id} log=${laneLogPath} meta=${failureArtifactPath}`,
|
||||
);
|
||||
}
|
||||
laneLogStream.write(
|
||||
`\n[test-parallel] done ${unit.id} code=${String(resolvedCode)} signal=${
|
||||
signal ?? "none"
|
||||
} elapsed=${formatElapsedMs(elapsedMs)}\n`,
|
||||
);
|
||||
laneLogStream.end();
|
||||
console.log(
|
||||
`[test-parallel] done ${unit.id} code=${String(resolvedCode)} elapsed=${formatElapsedMs(elapsedMs)}`,
|
||||
);
|
||||
resolve(resolvedCode);
|
||||
});
|
||||
});
|
||||
|
||||
const runUnit = async (unit, extraArgs = []) => {
|
||||
if (unit.fixedShardIndex !== undefined) {
|
||||
if (plan.shardIndexOverride !== null && plan.shardIndexOverride !== unit.fixedShardIndex) {
|
||||
return 0;
|
||||
}
|
||||
return runOnce(unit, extraArgs);
|
||||
}
|
||||
const explicitFilterCount = countExplicitEntryFilters(unit.args);
|
||||
const topLevelAssignedShard = plan.topLevelSingleShardAssignments.get(unit);
|
||||
if (topLevelAssignedShard !== undefined) {
|
||||
if (plan.shardIndexOverride !== null && plan.shardIndexOverride !== topLevelAssignedShard) {
|
||||
return 0;
|
||||
}
|
||||
return runOnce(unit, extraArgs);
|
||||
}
|
||||
const effectiveShardCount =
|
||||
explicitFilterCount === null
|
||||
? plan.shardCount
|
||||
: Math.min(plan.shardCount, Math.max(1, explicitFilterCount - 1));
|
||||
if (effectiveShardCount <= 1) {
|
||||
if (plan.shardIndexOverride !== null && plan.shardIndexOverride > effectiveShardCount) {
|
||||
return 0;
|
||||
}
|
||||
return runOnce(unit, extraArgs);
|
||||
}
|
||||
if (plan.shardIndexOverride !== null) {
|
||||
if (plan.shardIndexOverride > effectiveShardCount) {
|
||||
return 0;
|
||||
}
|
||||
return runOnce(unit, [
|
||||
"--shard",
|
||||
`${plan.shardIndexOverride}/${effectiveShardCount}`,
|
||||
...extraArgs,
|
||||
]);
|
||||
}
|
||||
for (let shardIndex = 1; shardIndex <= effectiveShardCount; shardIndex += 1) {
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
const code = await runOnce(unit, [
|
||||
"--shard",
|
||||
`${shardIndex}/${effectiveShardCount}`,
|
||||
...extraArgs,
|
||||
]);
|
||||
if (code !== 0) {
|
||||
return code;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
};
|
||||
|
||||
const runUnitsWithLimit = async (units, extraArgs = [], concurrency = 1) => {
|
||||
if (units.length === 0) {
|
||||
return undefined;
|
||||
}
|
||||
const normalizedConcurrency = Math.max(1, Math.floor(concurrency));
|
||||
if (normalizedConcurrency <= 1) {
|
||||
for (const unit of units) {
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
const code = await runUnit(unit, extraArgs);
|
||||
if (code !== 0) {
|
||||
return code;
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
let nextIndex = 0;
|
||||
let firstFailure;
|
||||
const worker = async () => {
|
||||
while (firstFailure === undefined) {
|
||||
const unitIndex = nextIndex;
|
||||
nextIndex += 1;
|
||||
if (unitIndex >= units.length) {
|
||||
return;
|
||||
}
|
||||
const code = await runUnit(units[unitIndex], extraArgs);
|
||||
if (code !== 0 && firstFailure === undefined) {
|
||||
firstFailure = code;
|
||||
}
|
||||
}
|
||||
};
|
||||
const workerCount = Math.min(normalizedConcurrency, units.length);
|
||||
await Promise.all(Array.from({ length: workerCount }, () => worker()));
|
||||
return firstFailure;
|
||||
};
|
||||
|
||||
const runUnits = async (units, extraArgs = []) => {
|
||||
if (plan.topLevelParallelEnabled) {
|
||||
return runUnitsWithLimit(units, extraArgs, plan.topLevelParallelLimit);
|
||||
}
|
||||
return runUnitsWithLimit(units, extraArgs);
|
||||
};
|
||||
|
||||
if (plan.passthroughMetadataOnly) {
|
||||
return runOnce(
|
||||
{
|
||||
id: "vitest-meta",
|
||||
args: ["vitest", "run"],
|
||||
maxWorkers: null,
|
||||
},
|
||||
plan.passthroughOptionArgs,
|
||||
);
|
||||
}
|
||||
|
||||
if (plan.targetedUnits.length > 0) {
|
||||
if (plan.passthroughRequiresSingleRun && plan.targetedUnits.length > 1) {
|
||||
console.error(
|
||||
"[test-parallel] The provided Vitest args require a single run, but the selected test filters span multiple wrapper configs. Run one target/config at a time.",
|
||||
);
|
||||
return 2;
|
||||
}
|
||||
const failedTargetedParallel = await runUnits(plan.parallelUnits, plan.passthroughOptionArgs);
|
||||
if (failedTargetedParallel !== undefined) {
|
||||
return failedTargetedParallel;
|
||||
}
|
||||
for (const unit of plan.serialUnits) {
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
const code = await runUnit(unit, plan.passthroughOptionArgs);
|
||||
if (code !== 0) {
|
||||
return code;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (plan.passthroughRequiresSingleRun && plan.passthroughOptionArgs.length > 0) {
|
||||
console.error(
|
||||
"[test-parallel] The provided Vitest args require a single run. Use the dedicated npm script for that workflow (for example `pnpm test:coverage`) or target a single test file/filter.",
|
||||
);
|
||||
return 2;
|
||||
}
|
||||
|
||||
if (plan.serialPrefixUnits.length > 0) {
|
||||
const failedSerialPrefix = await runUnitsWithLimit(
|
||||
plan.serialPrefixUnits,
|
||||
plan.passthroughOptionArgs,
|
||||
1,
|
||||
);
|
||||
if (failedSerialPrefix !== undefined) {
|
||||
return failedSerialPrefix;
|
||||
}
|
||||
const failedDeferredParallel = plan.deferredRunConcurrency
|
||||
? await runUnitsWithLimit(
|
||||
plan.deferredParallelUnits,
|
||||
plan.passthroughOptionArgs,
|
||||
plan.deferredRunConcurrency,
|
||||
)
|
||||
: await runUnits(plan.deferredParallelUnits, plan.passthroughOptionArgs);
|
||||
if (failedDeferredParallel !== undefined) {
|
||||
return failedDeferredParallel;
|
||||
}
|
||||
} else {
|
||||
const failedParallel = await runUnits(plan.parallelUnits, plan.passthroughOptionArgs);
|
||||
if (failedParallel !== undefined) {
|
||||
return failedParallel;
|
||||
}
|
||||
}
|
||||
|
||||
for (const unit of plan.serialUnits) {
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
const code = await runUnit(unit, plan.passthroughOptionArgs);
|
||||
if (code !== 0) {
|
||||
return code;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
1023
scripts/test-planner/planner.mjs
Normal file
1023
scripts/test-planner/planner.mjs
Normal file
File diff suppressed because it is too large
Load Diff
348
scripts/test-planner/runtime-profile.mjs
Normal file
348
scripts/test-planner/runtime-profile.mjs
Normal file
@@ -0,0 +1,348 @@
|
||||
import os from "node:os";
|
||||
|
||||
export const TEST_PROFILES = new Set(["normal", "serial", "max"]);
|
||||
|
||||
export const parsePositiveInt = (value) => {
|
||||
const parsed = Number.parseInt(value ?? "", 10);
|
||||
return Number.isFinite(parsed) && parsed > 0 ? parsed : null;
|
||||
};
|
||||
|
||||
export const resolveVitestMode = (env = process.env, explicitMode = null) => {
|
||||
if (explicitMode === "ci" || explicitMode === "local") {
|
||||
return explicitMode;
|
||||
}
|
||||
return env.CI === "true" || env.GITHUB_ACTIONS === "true" ? "ci" : "local";
|
||||
};
|
||||
|
||||
const clamp = (value, min, max) => Math.max(min, Math.min(max, value));
|
||||
|
||||
const parseProfile = (rawProfile) => {
|
||||
if (!rawProfile) {
|
||||
return "normal";
|
||||
}
|
||||
const normalized = rawProfile.trim().toLowerCase();
|
||||
if (normalized === "low") {
|
||||
return "serial";
|
||||
}
|
||||
if (!TEST_PROFILES.has(normalized)) {
|
||||
throw new Error(
|
||||
`Unsupported test profile "${normalized}". Supported profiles: normal, serial, max.`,
|
||||
);
|
||||
}
|
||||
return normalized;
|
||||
};
|
||||
|
||||
const resolveLoadRatio = (env, cpuCount, platform, loadAverage) => {
|
||||
const loadAwareDisabledRaw = env.OPENCLAW_TEST_LOAD_AWARE?.trim().toLowerCase();
|
||||
const loadAwareDisabled = loadAwareDisabledRaw === "0" || loadAwareDisabledRaw === "false";
|
||||
if (loadAwareDisabled || platform === "win32" || cpuCount <= 0) {
|
||||
return 0;
|
||||
}
|
||||
const source = Array.isArray(loadAverage) ? loadAverage : os.loadavg();
|
||||
return source.length > 0 ? source[0] / cpuCount : 0;
|
||||
};
|
||||
|
||||
const resolveMemoryBand = (memoryGiB) => {
|
||||
if (memoryGiB < 24) {
|
||||
return "constrained";
|
||||
}
|
||||
if (memoryGiB < 48) {
|
||||
return "moderate";
|
||||
}
|
||||
if (memoryGiB < 96) {
|
||||
return "mid";
|
||||
}
|
||||
return "high";
|
||||
};
|
||||
|
||||
const resolveLoadBand = (isLoadAware, loadRatio) => {
|
||||
if (!isLoadAware) {
|
||||
return "normal";
|
||||
}
|
||||
if (loadRatio < 0.5) {
|
||||
return "idle";
|
||||
}
|
||||
if (loadRatio < 0.9) {
|
||||
return "normal";
|
||||
}
|
||||
if (loadRatio < 1.1) {
|
||||
return "busy";
|
||||
}
|
||||
return "saturated";
|
||||
};
|
||||
|
||||
const scaleForLoad = (value, loadBand) => {
|
||||
if (value === null || value === undefined) {
|
||||
return value;
|
||||
}
|
||||
const scale = loadBand === "busy" ? 0.75 : loadBand === "saturated" ? 0.5 : 1;
|
||||
return Math.max(1, Math.floor(value * scale));
|
||||
};
|
||||
|
||||
const scaleConcurrencyForLoad = (value, loadBand) => {
|
||||
if (value === null || value === undefined) {
|
||||
return value;
|
||||
}
|
||||
const scale = loadBand === "busy" ? 0.8 : loadBand === "saturated" ? 0.5 : 1;
|
||||
return Math.max(1, Math.floor(value * scale));
|
||||
};
|
||||
|
||||
const LOCAL_MEMORY_BUDGETS = {
|
||||
constrained: {
|
||||
vitestCap: 2,
|
||||
unitShared: 2,
|
||||
unitIsolated: 1,
|
||||
unitHeavy: 1,
|
||||
extensions: 1,
|
||||
gateway: 1,
|
||||
topLevelNoIsolate: 4,
|
||||
topLevelIsolated: 2,
|
||||
deferred: 1,
|
||||
heavyFileLimit: 36,
|
||||
heavyLaneCount: 3,
|
||||
memoryHeavyFileLimit: 8,
|
||||
unitFastBatchTargetMs: 10_000,
|
||||
},
|
||||
moderate: {
|
||||
vitestCap: 3,
|
||||
unitShared: 3,
|
||||
unitIsolated: 1,
|
||||
unitHeavy: 1,
|
||||
extensions: 2,
|
||||
gateway: 1,
|
||||
topLevelNoIsolate: 6,
|
||||
topLevelIsolated: 2,
|
||||
deferred: 1,
|
||||
heavyFileLimit: 48,
|
||||
heavyLaneCount: 4,
|
||||
memoryHeavyFileLimit: 12,
|
||||
unitFastBatchTargetMs: 15_000,
|
||||
},
|
||||
mid: {
|
||||
vitestCap: 4,
|
||||
unitShared: 4,
|
||||
unitIsolated: 1,
|
||||
unitHeavy: 1,
|
||||
extensions: 3,
|
||||
gateway: 1,
|
||||
topLevelNoIsolate: 8,
|
||||
topLevelIsolated: 3,
|
||||
deferred: 2,
|
||||
heavyFileLimit: 60,
|
||||
heavyLaneCount: 4,
|
||||
memoryHeavyFileLimit: 16,
|
||||
unitFastBatchTargetMs: 0,
|
||||
},
|
||||
high: {
|
||||
vitestCap: 6,
|
||||
unitShared: 6,
|
||||
unitIsolated: 2,
|
||||
unitHeavy: 2,
|
||||
extensions: 4,
|
||||
gateway: 3,
|
||||
topLevelNoIsolate: 12,
|
||||
topLevelIsolated: 4,
|
||||
deferred: 3,
|
||||
heavyFileLimit: 80,
|
||||
heavyLaneCount: 5,
|
||||
memoryHeavyFileLimit: 16,
|
||||
unitFastBatchTargetMs: 45_000,
|
||||
},
|
||||
};
|
||||
|
||||
const withIntentBudgetAdjustments = (budget, intentProfile, cpuCount) => {
|
||||
if (intentProfile === "serial") {
|
||||
return {
|
||||
...budget,
|
||||
vitestMaxWorkers: 1,
|
||||
unitSharedWorkers: 1,
|
||||
unitIsolatedWorkers: 1,
|
||||
unitHeavyWorkers: 1,
|
||||
extensionWorkers: 1,
|
||||
gatewayWorkers: 1,
|
||||
topLevelParallelEnabled: false,
|
||||
topLevelParallelLimit: 1,
|
||||
topLevelParallelLimitNoIsolate: 1,
|
||||
topLevelParallelLimitIsolated: 1,
|
||||
deferredRunConcurrency: 1,
|
||||
};
|
||||
}
|
||||
|
||||
if (intentProfile === "max") {
|
||||
const maxTopLevelParallelLimit = clamp(
|
||||
Math.max(budget.topLevelParallelLimitNoIsolate ?? budget.topLevelParallelLimit ?? 1, 5),
|
||||
1,
|
||||
8,
|
||||
);
|
||||
return {
|
||||
...budget,
|
||||
vitestMaxWorkers: clamp(Math.max(budget.vitestMaxWorkers, Math.min(8, cpuCount)), 1, 16),
|
||||
unitSharedWorkers: clamp(Math.max(budget.unitSharedWorkers, Math.min(8, cpuCount)), 1, 16),
|
||||
unitIsolatedWorkers: clamp(Math.max(budget.unitIsolatedWorkers, Math.min(4, cpuCount)), 1, 4),
|
||||
unitHeavyWorkers: clamp(Math.max(budget.unitHeavyWorkers, Math.min(4, cpuCount)), 1, 4),
|
||||
extensionWorkers: clamp(Math.max(budget.extensionWorkers, Math.min(6, cpuCount)), 1, 6),
|
||||
gatewayWorkers: clamp(Math.max(budget.gatewayWorkers, Math.min(2, cpuCount)), 1, 6),
|
||||
topLevelParallelEnabled: true,
|
||||
topLevelParallelLimit: maxTopLevelParallelLimit,
|
||||
topLevelParallelLimitNoIsolate: maxTopLevelParallelLimit,
|
||||
topLevelParallelLimitIsolated: clamp(
|
||||
Math.max(budget.topLevelParallelLimitIsolated ?? budget.topLevelParallelLimit ?? 1, 4),
|
||||
1,
|
||||
8,
|
||||
),
|
||||
deferredRunConcurrency: Math.max(budget.deferredRunConcurrency ?? 1, 3),
|
||||
};
|
||||
}
|
||||
|
||||
return budget;
|
||||
};
|
||||
|
||||
export function resolveRuntimeCapabilities(env = process.env, options = {}) {
|
||||
const mode = resolveVitestMode(env, options.mode ?? null);
|
||||
const isCI = mode === "ci";
|
||||
const platform = options.platform ?? process.platform;
|
||||
const runnerOs = env.RUNNER_OS ?? "";
|
||||
const isMacOS = platform === "darwin" || runnerOs === "macOS";
|
||||
const isWindows = platform === "win32" || runnerOs === "Windows";
|
||||
const isWindowsCi = isCI && isWindows;
|
||||
const hostCpuCount =
|
||||
parsePositiveInt(env.OPENCLAW_TEST_HOST_CPU_COUNT) ?? options.cpuCount ?? os.cpus().length;
|
||||
const totalMemoryBytes = options.totalMemoryBytes ?? os.totalmem();
|
||||
const hostMemoryGiB =
|
||||
parsePositiveInt(env.OPENCLAW_TEST_HOST_MEMORY_GIB) ?? Math.floor(totalMemoryBytes / 1024 ** 3);
|
||||
const nodeMajor = Number.parseInt(
|
||||
(options.nodeVersion ?? process.versions.node).split(".")[0] ?? "",
|
||||
10,
|
||||
);
|
||||
const intentProfile = parseProfile(options.profile ?? env.OPENCLAW_TEST_PROFILE ?? "normal");
|
||||
const loadRatio = !isCI ? resolveLoadRatio(env, hostCpuCount, platform, options.loadAverage) : 0;
|
||||
const loadAware = !isCI && platform !== "win32";
|
||||
const memoryBand = resolveMemoryBand(hostMemoryGiB);
|
||||
const loadBand = resolveLoadBand(loadAware, loadRatio);
|
||||
const runtimeProfileName = isCI
|
||||
? isWindows
|
||||
? "ci-windows"
|
||||
: isMacOS
|
||||
? "ci-macos"
|
||||
: "ci-linux"
|
||||
: isWindows
|
||||
? "local-windows"
|
||||
: isMacOS
|
||||
? "local-darwin"
|
||||
: "local-linux";
|
||||
|
||||
return {
|
||||
mode,
|
||||
runtimeProfileName,
|
||||
isCI,
|
||||
isMacOS,
|
||||
isWindows,
|
||||
isWindowsCi,
|
||||
platform,
|
||||
hostCpuCount,
|
||||
hostMemoryGiB,
|
||||
nodeMajor,
|
||||
intentProfile,
|
||||
memoryBand,
|
||||
loadAware,
|
||||
loadRatio,
|
||||
loadBand,
|
||||
};
|
||||
}
|
||||
|
||||
export function resolveExecutionBudget(runtimeCapabilities) {
|
||||
const runtime = runtimeCapabilities;
|
||||
const cpuCount = clamp(runtime.hostCpuCount, 1, 16);
|
||||
|
||||
if (runtime.isCI) {
|
||||
const macCiWorkers = runtime.isMacOS ? 1 : null;
|
||||
return {
|
||||
vitestMaxWorkers: runtime.isWindows ? 2 : runtime.isMacOS ? 1 : 3,
|
||||
unitSharedWorkers: macCiWorkers,
|
||||
unitIsolatedWorkers: macCiWorkers,
|
||||
unitHeavyWorkers: macCiWorkers,
|
||||
extensionWorkers: macCiWorkers,
|
||||
gatewayWorkers: macCiWorkers,
|
||||
topLevelParallelEnabled: runtime.intentProfile !== "serial" && !runtime.isWindows,
|
||||
topLevelParallelLimit: runtime.isWindows ? 2 : 4,
|
||||
topLevelParallelLimitNoIsolate: runtime.isWindows ? 2 : 4,
|
||||
topLevelParallelLimitIsolated: runtime.isWindows ? 2 : 4,
|
||||
deferredRunConcurrency: null,
|
||||
heavyUnitFileLimit: 64,
|
||||
heavyUnitLaneCount: 4,
|
||||
memoryHeavyUnitFileLimit: 64,
|
||||
unitFastLaneCount: runtime.isWindows ? 1 : 3,
|
||||
unitFastBatchTargetMs: runtime.isWindows ? 0 : 45_000,
|
||||
channelsBatchTargetMs: runtime.isWindows ? 0 : 30_000,
|
||||
extensionsBatchTargetMs: runtime.isWindows ? 0 : 30_000,
|
||||
};
|
||||
}
|
||||
|
||||
const bandBudget = LOCAL_MEMORY_BUDGETS[runtime.memoryBand];
|
||||
const baseBudget = {
|
||||
vitestMaxWorkers: Math.min(cpuCount, bandBudget.vitestCap),
|
||||
unitSharedWorkers: Math.min(cpuCount, bandBudget.unitShared),
|
||||
unitIsolatedWorkers: Math.min(cpuCount, bandBudget.unitIsolated),
|
||||
unitHeavyWorkers: Math.min(cpuCount, bandBudget.unitHeavy),
|
||||
extensionWorkers: Math.min(cpuCount, bandBudget.extensions),
|
||||
gatewayWorkers: Math.min(cpuCount, bandBudget.gateway),
|
||||
topLevelParallelEnabled: runtime.nodeMajor < 25,
|
||||
topLevelParallelLimit: Math.min(cpuCount, bandBudget.topLevelIsolated),
|
||||
topLevelParallelLimitNoIsolate: Math.min(cpuCount, bandBudget.topLevelNoIsolate),
|
||||
topLevelParallelLimitIsolated: Math.min(cpuCount, bandBudget.topLevelIsolated),
|
||||
deferredRunConcurrency: bandBudget.deferred,
|
||||
heavyUnitFileLimit: bandBudget.heavyFileLimit,
|
||||
heavyUnitLaneCount: bandBudget.heavyLaneCount,
|
||||
memoryHeavyUnitFileLimit: bandBudget.memoryHeavyFileLimit,
|
||||
unitFastLaneCount: 1,
|
||||
unitFastBatchTargetMs: bandBudget.unitFastBatchTargetMs,
|
||||
channelsBatchTargetMs: 0,
|
||||
extensionsBatchTargetMs: 0,
|
||||
};
|
||||
|
||||
const loadAdjustedBudget = {
|
||||
...baseBudget,
|
||||
vitestMaxWorkers: scaleForLoad(baseBudget.vitestMaxWorkers, runtime.loadBand),
|
||||
unitSharedWorkers: scaleForLoad(baseBudget.unitSharedWorkers, runtime.loadBand),
|
||||
unitHeavyWorkers: scaleForLoad(baseBudget.unitHeavyWorkers, runtime.loadBand),
|
||||
extensionWorkers: scaleForLoad(baseBudget.extensionWorkers, runtime.loadBand),
|
||||
gatewayWorkers: scaleForLoad(baseBudget.gatewayWorkers, runtime.loadBand),
|
||||
topLevelParallelLimit: scaleConcurrencyForLoad(
|
||||
baseBudget.topLevelParallelLimit,
|
||||
runtime.loadBand,
|
||||
),
|
||||
topLevelParallelLimitNoIsolate: scaleConcurrencyForLoad(
|
||||
baseBudget.topLevelParallelLimitNoIsolate,
|
||||
runtime.loadBand,
|
||||
),
|
||||
topLevelParallelLimitIsolated: scaleConcurrencyForLoad(
|
||||
baseBudget.topLevelParallelLimitIsolated,
|
||||
runtime.loadBand,
|
||||
),
|
||||
deferredRunConcurrency:
|
||||
runtime.loadBand === "busy"
|
||||
? Math.max(1, (baseBudget.deferredRunConcurrency ?? 1) - 1)
|
||||
: runtime.loadBand === "saturated"
|
||||
? 1
|
||||
: baseBudget.deferredRunConcurrency,
|
||||
};
|
||||
|
||||
return withIntentBudgetAdjustments(loadAdjustedBudget, runtime.intentProfile, cpuCount);
|
||||
}
|
||||
|
||||
export function resolveLocalVitestMaxWorkers(env = process.env, options = {}) {
|
||||
const explicit = parsePositiveInt(env.OPENCLAW_VITEST_MAX_WORKERS);
|
||||
if (explicit !== null) {
|
||||
return explicit;
|
||||
}
|
||||
|
||||
const runtimeCapabilities = resolveRuntimeCapabilities(env, {
|
||||
cpuCount: options.cpuCount,
|
||||
totalMemoryBytes: options.totalMemoryBytes,
|
||||
platform: options.platform,
|
||||
mode: "local",
|
||||
loadAverage: options.loadAverage,
|
||||
profile: options.profile,
|
||||
});
|
||||
return resolveExecutionBudget(runtimeCapabilities).vitestMaxWorkers;
|
||||
}
|
||||
74
scripts/test-planner/vitest-args.mjs
Normal file
74
scripts/test-planner/vitest-args.mjs
Normal file
@@ -0,0 +1,74 @@
|
||||
export const OPTION_TAKES_VALUE = new Set([
|
||||
"-t",
|
||||
"-c",
|
||||
"-r",
|
||||
"--testNamePattern",
|
||||
"--config",
|
||||
"--root",
|
||||
"--dir",
|
||||
"--reporter",
|
||||
"--outputFile",
|
||||
"--pool",
|
||||
"--execArgv",
|
||||
"--vmMemoryLimit",
|
||||
"--maxWorkers",
|
||||
"--environment",
|
||||
"--shard",
|
||||
"--changed",
|
||||
"--sequence",
|
||||
"--inspect",
|
||||
"--inspectBrk",
|
||||
"--testTimeout",
|
||||
"--hookTimeout",
|
||||
"--bail",
|
||||
"--retry",
|
||||
"--diff",
|
||||
"--exclude",
|
||||
"--project",
|
||||
"--slowTestThreshold",
|
||||
"--teardownTimeout",
|
||||
"--attachmentsDir",
|
||||
"--mode",
|
||||
"--api",
|
||||
"--browser",
|
||||
"--maxConcurrency",
|
||||
"--mergeReports",
|
||||
"--configLoader",
|
||||
"--experimental",
|
||||
]);
|
||||
|
||||
export const SINGLE_RUN_ONLY_FLAGS = new Set(["--coverage", "--outputFile", "--mergeReports"]);
|
||||
|
||||
export const parsePassthroughArgs = (args = []) => {
|
||||
const fileFilters = [];
|
||||
const optionArgs = [];
|
||||
let consumeNextAsOptionValue = false;
|
||||
|
||||
for (const arg of args) {
|
||||
if (consumeNextAsOptionValue) {
|
||||
optionArgs.push(arg);
|
||||
consumeNextAsOptionValue = false;
|
||||
continue;
|
||||
}
|
||||
if (arg === "--") {
|
||||
optionArgs.push(arg);
|
||||
continue;
|
||||
}
|
||||
if (typeof arg === "string" && arg.startsWith("-")) {
|
||||
optionArgs.push(arg);
|
||||
consumeNextAsOptionValue = !arg.includes("=") && OPTION_TAKES_VALUE.has(arg);
|
||||
continue;
|
||||
}
|
||||
fileFilters.push(arg);
|
||||
}
|
||||
|
||||
return { fileFilters, optionArgs };
|
||||
};
|
||||
|
||||
export const countExplicitEntryFilters = (entryArgs) => {
|
||||
const { fileFilters } = parsePassthroughArgs(entryArgs.slice(2));
|
||||
return fileFilters.length > 0 ? fileFilters.length : null;
|
||||
};
|
||||
|
||||
export const getExplicitEntryFilters = (entryArgs) =>
|
||||
parsePassthroughArgs(entryArgs.slice(2)).fileFilters;
|
||||
Reference in New Issue
Block a user