Skip to content

Commit b5d41f9

Browse files
authored
fix(test): bound direct gateway server runs (#119184)
1 parent 121c6c1 commit b5d41f9

12 files changed

Lines changed: 339 additions & 169 deletions

‎scripts/lib/ci-node-test-plan.mjs‎

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ import {
55
embeddedAgentVitestProjectOwners,
66
} from "../../test/vitest/vitest.agents-paths.mjs";
77
import { commandsLightTestFiles } from "../../test/vitest/vitest.commands-light-paths.mjs";
8+
import {
9+
isGatewayServerBackedHttpTestFile,
10+
isGatewayServerTestFile,
11+
} from "../../test/vitest/vitest.gateway-server-paths.mjs";
812
import { fullSuiteVitestShards } from "../../test/vitest/vitest.test-shards.mjs";
913
import { toolingIsolatedTestFiles } from "../../test/vitest/vitest.tooling-isolated-paths.mjs";
1014
import {
@@ -612,29 +616,6 @@ function createAgentCoreSplitShards() {
612616
];
613617
}
614618

615-
const GATEWAY_SERVER_BACKED_HTTP_TESTS = new Set([
616-
"src/gateway/embeddings-http.test.ts",
617-
"src/gateway/models-http.test.ts",
618-
"src/gateway/openai-http.test.ts",
619-
"src/gateway/openresponses-http.test.ts",
620-
"src/gateway/probe.auth.integration.test.ts",
621-
]);
622-
623-
const GATEWAY_SERVER_EXCLUDED_TESTS = new Set([
624-
"src/gateway/gateway.test.ts",
625-
"src/gateway/server.startup-matrix-migration.integration.test.ts",
626-
"src/gateway/sessions-history-http.test.ts",
627-
]);
628-
629-
function isGatewayServerTestFile(file) {
630-
return (
631-
file.startsWith("src/gateway/") &&
632-
!file.startsWith("src/gateway/server-methods/") &&
633-
!GATEWAY_SERVER_EXCLUDED_TESTS.has(file) &&
634-
(file.includes("server") || GATEWAY_SERVER_BACKED_HTTP_TESTS.has(file))
635-
);
636-
}
637-
638619
function resolveGatewayStartupShardName(file) {
639620
const name = relative("src/gateway", file).replaceAll("\\", "/");
640621
if (name.startsWith("server-startup-config") || name.startsWith("server-startup-early")) {
@@ -643,9 +624,7 @@ function resolveGatewayStartupShardName(file) {
643624
if (
644625
name.startsWith("server-runtime") ||
645626
name.startsWith("server.health") ||
646-
name.startsWith("server.lazy") ||
647-
name.startsWith("server/health-state") ||
648-
name.startsWith("server/readiness")
627+
name.startsWith("server.lazy")
649628
) {
650629
return "agentic-control-plane-startup-health-runtime";
651630
}
@@ -658,7 +637,7 @@ function resolveGatewayStartupShardName(file) {
658637
function resolveGatewayServerShardName(file) {
659638
const name = relative("src/gateway", file).replaceAll("\\", "/");
660639
if (
661-
GATEWAY_SERVER_BACKED_HTTP_TESTS.has(file) ||
640+
isGatewayServerBackedHttpTestFile(file) ||
662641
name.startsWith("server.models") ||
663642
name.startsWith("server.talk")
664643
) {
@@ -688,8 +667,6 @@ function resolveGatewayServerShardName(file) {
688667
name.startsWith("server-runtime") ||
689668
name.startsWith("server.lazy") ||
690669
name.startsWith("server.health") ||
691-
name.startsWith("server/health-state") ||
692-
name.startsWith("server/readiness") ||
693670
name === "server-close.test.ts"
694671
) {
695672
return resolveGatewayStartupShardName(file);
@@ -725,10 +702,7 @@ function resolveGatewayServerShardName(file) {
725702
) {
726703
return "agentic-control-plane-runtime-ui-tools";
727704
}
728-
if (name.startsWith("server/")) {
729-
return "agentic-control-plane-runtime-events";
730-
}
731-
if (name.startsWith("server.") || name.startsWith("server/")) {
705+
if (name.startsWith("server.")) {
732706
return "agentic-control-plane-runtime-state";
733707
}
734708
return "agentic-control-plane-runtime";
@@ -748,7 +722,6 @@ function createGatewayServerSplitShards() {
748722
"agentic-control-plane-runtime",
749723
"agentic-control-plane-runtime-config",
750724
"agentic-control-plane-runtime-cron",
751-
"agentic-control-plane-runtime-events",
752725
"agentic-control-plane-runtime-network",
753726
"agentic-control-plane-runtime-server",
754727
"agentic-control-plane-runtime-shared-token",
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export const GATEWAY_SERVER_TEST_PROCESS_COUNT: 4;
2+
export function listGatewayServerTestTargets(cwd?: string): string[];
3+
export function splitTestTargetChunks(targets: string[], chunkCount: number): string[][];
4+
export function createGatewayServerTestTargetChunks(cwd?: string): string[][];
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Bounds the non-isolated Gateway server project before its shared module graph
2+
// reaches the V8 worker heap limit.
3+
import { spawnSync } from "node:child_process";
4+
import fs from "node:fs";
5+
import path from "node:path";
6+
import { isGatewayServerTestFile } from "../../test/vitest/vitest.gateway-server-paths.mjs";
7+
8+
export const GATEWAY_SERVER_TEST_PROCESS_COUNT = 4;
9+
10+
function normalizePath(value) {
11+
return value.replaceAll("\\", "/");
12+
}
13+
14+
function listRepoFilesRecursive(root, cwd) {
15+
const entries = fs.readdirSync(root, { withFileTypes: true });
16+
return entries.flatMap((entry) => {
17+
const absolute = path.join(root, entry.name);
18+
if (entry.isDirectory()) {
19+
return listRepoFilesRecursive(absolute, cwd);
20+
}
21+
return entry.isFile() ? [normalizePath(path.relative(cwd, absolute))] : [];
22+
});
23+
}
24+
25+
function listGatewayFilesFromGit(cwd) {
26+
const result = spawnSync("git", ["ls-files", "--", "src/gateway"], {
27+
cwd,
28+
encoding: "utf8",
29+
stdio: ["ignore", "pipe", "ignore"],
30+
});
31+
if (result.status !== 0) {
32+
return null;
33+
}
34+
return result.stdout
35+
.split("\n")
36+
.map((line) => normalizePath(line.trim()))
37+
.filter((line) => line.length > 0);
38+
}
39+
40+
export function listGatewayServerTestTargets(cwd = process.cwd()) {
41+
const gatewayDir = path.join(cwd, "src/gateway");
42+
if (!fs.existsSync(gatewayDir)) {
43+
return [];
44+
}
45+
return (listGatewayFilesFromGit(cwd) ?? listRepoFilesRecursive(gatewayDir, cwd))
46+
.filter(isGatewayServerTestFile)
47+
.toSorted((a, b) => a.localeCompare(b));
48+
}
49+
50+
export function splitTestTargetChunks(targets, chunkCount) {
51+
if (targets.length === 0) {
52+
return [];
53+
}
54+
const normalizedChunkCount = Math.min(chunkCount, targets.length);
55+
const baseSize = Math.floor(targets.length / normalizedChunkCount);
56+
const remainder = targets.length % normalizedChunkCount;
57+
const chunks = [];
58+
let offset = 0;
59+
for (let index = 0; index < normalizedChunkCount; index += 1) {
60+
const chunkSize = baseSize + (index < remainder ? 1 : 0);
61+
chunks.push(targets.slice(offset, offset + chunkSize));
62+
offset += chunkSize;
63+
}
64+
return chunks;
65+
}
66+
67+
export function createGatewayServerTestTargetChunks(cwd = process.cwd()) {
68+
return splitTestTargetChunks(
69+
listGatewayServerTestTargets(cwd),
70+
GATEWAY_SERVER_TEST_PROCESS_COUNT,
71+
);
72+
}

‎scripts/run-vitest.d.mts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ export function resolveMissingExplicitTestFiles(
4141
fsImpl?: VitestFs,
4242
): string[];
4343
export function resolveImplicitVitestArgs(argv: string[], cwd?: string): string[];
44+
export function resolveBoundedVitestInvocations(
45+
argv: string[],
46+
options?: {
47+
cwd?: string;
48+
env?: NodeJS.ProcessEnv;
49+
gatewayServerTargetChunks?: string[][];
50+
},
51+
): string[][];
4452
export function installVitestNoOutputWatchdog(params: {
4553
streams?: Array<{ on(event: string, listener: (...args: unknown[]) => void): unknown } | null>;
4654
timeoutMs: number | null;

‎scripts/run-vitest.mjs‎

Lines changed: 93 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { toolingIsolatedTestFiles } from "../test/vitest/vitest.tooling-isolated
99
import { isUiTestTarget } from "../test/vitest/vitest.ui-paths.mjs";
1010
import { boundaryTestFiles } from "../test/vitest/vitest.unit-paths.mjs";
1111
import { runWithFailedTrailer, writeFailedTrailer } from "./lib/failed-trailer.mjs";
12+
import { createGatewayServerTestTargetChunks } from "./lib/gateway-server-test-plan.mjs";
1213
import { signalExitCode } from "./lib/managed-child-process.mjs";
1314
import { resolveRepoRoot } from "./lib/repo-root.mjs";
1415
import { resolveLocalVitestEnv } from "./lib/vitest-local-scheduling.mjs";
@@ -126,6 +127,16 @@ const VITEST_DOTTED_OPTIONS_WITH_VALUE_PREFIXES = [
126127
"--sequence.",
127128
"--typecheck.",
128129
];
130+
const UNBOUNDED_CONFIG_ONLY_OPTIONS = [
131+
"--changed",
132+
"--coverage",
133+
"--dir",
134+
"--mergeReports",
135+
"--outputFile",
136+
"--project",
137+
"--root",
138+
"--shard",
139+
];
129140
const require = createRequire(import.meta.url);
130141
const repoRoot = resolveRepoRoot(import.meta.url);
131142
const testProjectsRunnerPath = path.join(repoRoot, "scripts", "test-projects.mjs");
@@ -427,15 +438,66 @@ function resolveVitestConfigArg(argv) {
427438
}
428439

429440
function resolveVitestConfigNoOutputTimeoutMs(config) {
430-
const normalized = path.normalize(config).replaceAll(path.sep, "/").replace(/^\.\//u, "");
441+
const normalized = normalizeVitestConfigPath(config);
431442
for (const [candidate, timeoutMs] of VITEST_CONFIG_NO_OUTPUT_TIMEOUT_MS) {
432-
if (normalized === candidate || normalized.endsWith(`/${candidate}`)) {
443+
if (matchesVitestConfigPath(normalized, candidate)) {
433444
return timeoutMs;
434445
}
435446
}
436447
return null;
437448
}
438449

450+
function normalizeVitestConfigPath(config) {
451+
return path.normalize(config).replaceAll(path.sep, "/").replace(/^\.\//u, "");
452+
}
453+
454+
function matchesVitestConfigPath(normalized, candidate) {
455+
return normalized === candidate || normalized.endsWith("/" + candidate);
456+
}
457+
458+
function hasVitestOption(argv, option) {
459+
for (const arg of argv) {
460+
if (arg === "--") {
461+
return false;
462+
}
463+
if (arg === option || arg.startsWith(option + "=") || arg.startsWith(option + ".")) {
464+
return true;
465+
}
466+
}
467+
return false;
468+
}
469+
470+
function insertVitestTargets(argv, targets) {
471+
const separatorIndex = argv.indexOf("--");
472+
const insertionIndex = separatorIndex < 0 ? argv.length : separatorIndex;
473+
return [...argv.slice(0, insertionIndex), ...targets, ...argv.slice(insertionIndex)];
474+
}
475+
476+
/**
477+
* Splits config-only Gateway server runs into fresh processes before the
478+
* non-isolated module graph reaches the worker heap limit.
479+
*/
480+
export function resolveBoundedVitestInvocations(argv, options = {}) {
481+
const config = resolveVitestConfigArg(argv);
482+
const normalizedConfig = config === null ? "" : normalizeVitestConfigPath(config);
483+
const env = options.env ?? process.env;
484+
const cwd = options.cwd ?? process.cwd();
485+
const mode = resolveExplicitVitestMode(argv);
486+
if (
487+
!matchesVitestConfigPath(normalizedConfig, GATEWAY_SERVER_VITEST_CONFIG) ||
488+
mode === "watch" ||
489+
(mode !== "run" && !isTruthyEnvValue(env.CI)) ||
490+
hasNonRunVitestSubcommand(argv) ||
491+
hasAlternateVitestRootArg(argv) ||
492+
collectExplicitProjectRouterTargetArgs(argv, cwd).length > 0 ||
493+
UNBOUNDED_CONFIG_ONLY_OPTIONS.some((option) => hasVitestOption(argv, option))
494+
) {
495+
return [argv];
496+
}
497+
const chunks = options.gatewayServerTargetChunks ?? createGatewayServerTestTargetChunks(cwd);
498+
return chunks.length > 1 ? chunks.map((targets) => insertVitestTargets(argv, targets)) : [argv];
499+
}
500+
439501
/**
440502
* Builds spawn options for the primary Vitest child process.
441503
*/
@@ -1115,9 +1177,11 @@ async function finishVitestProcess({ completion, getForwardedSignal }) {
11151177
if (exitSignal) {
11161178
writeFailedTrailer("vitest", signalExitCode(exitSignal));
11171179
process.kill(process.pid, exitSignal);
1118-
return;
1180+
return signalExitCode(exitSignal);
11191181
}
1120-
process.exitCode = code ?? 1;
1182+
const exitCode = code ?? 1;
1183+
process.exitCode = exitCode;
1184+
return exitCode;
11211185
}
11221186

11231187
async function main(argv = process.argv.slice(2), env = process.env) {
@@ -1146,8 +1210,7 @@ async function main(argv = process.argv.slice(2), env = process.env) {
11461210
}
11471211

11481212
const vitestArgs = resolveImplicitVitestArgs(argv);
1149-
const guardedVitestArgs = resolveExplicitTestFileNoPassArgs(vitestArgs);
1150-
const spawnEnv = resolveRunVitestSpawnEnv(env, guardedVitestArgs);
1213+
const invocations = resolveBoundedVitestInvocations(vitestArgs, { env });
11511214
let vitestCliEntry;
11521215
try {
11531216
vitestCliEntry = resolveVitestCliEntry();
@@ -1160,20 +1223,30 @@ async function main(argv = process.argv.slice(2), env = process.env) {
11601223
throw error;
11611224
}
11621225

1163-
await finishVitestProcess(
1164-
spawnWatchedVitestProcess({
1165-
pnpmArgs: [
1166-
"exec",
1167-
"node",
1168-
...resolveVitestNodeArgs(env),
1169-
vitestCliEntry,
1170-
...guardedVitestArgs,
1171-
],
1172-
spawnParams: resolveVitestSpawnParams(spawnEnv),
1173-
env: spawnEnv,
1174-
label: guardedVitestArgs.join(" "),
1175-
}),
1176-
);
1226+
for (let index = 0; index < invocations.length; index += 1) {
1227+
const guardedVitestArgs = resolveExplicitTestFileNoPassArgs(invocations[index]);
1228+
const spawnEnv = resolveRunVitestSpawnEnv(env, guardedVitestArgs);
1229+
if (invocations.length > 1) {
1230+
console.error("[vitest] Gateway server process " + (index + 1) + "/" + invocations.length);
1231+
}
1232+
const exitCode = await finishVitestProcess(
1233+
spawnWatchedVitestProcess({
1234+
pnpmArgs: [
1235+
"exec",
1236+
"node",
1237+
...resolveVitestNodeArgs(env),
1238+
vitestCliEntry,
1239+
...guardedVitestArgs,
1240+
],
1241+
spawnParams: resolveVitestSpawnParams(spawnEnv),
1242+
env: spawnEnv,
1243+
label: guardedVitestArgs.join(" "),
1244+
}),
1245+
);
1246+
if (exitCode !== 0) {
1247+
return;
1248+
}
1249+
}
11771250
}
11781251

11791252
if (import.meta.main) {

0 commit comments

Comments
 (0)