Skip to content

Commit fc87337

Browse files
authored
fix(ci): keep Telegram shards alive after the first isolated file (#123576)
Five-file Telegram jobs finished the first file, then isolate re-imported the next graph in silence until the 300s watchdog killed the worker. Recycle the Vitest process after each file and keep five files per CI job.
1 parent 8865c25 commit fc87337

7 files changed

Lines changed: 213 additions & 25 deletions

‎scripts/lib/ci-changed-node-test-plan.mts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ import {
1515
resolvePolicyTestTargets,
1616
} from "./ci-node-test-plan.mts";
1717
import {
18-
createExtensionTestProcessTargetChunks,
18+
listExtensionTestFilesForRoots,
1919
resolveExtensionTestConfig,
20+
shouldSplitExtensionTestProcesses,
21+
splitExtensionTestJobTargets,
2022
} from "./extension-test-plan.mts";
2123
import { buildPluginSdkEntrySources, publicPluginSdkEntrypoints } from "./plugin-sdk-entries.mts";
2224

@@ -329,7 +331,10 @@ function createChangedExtensionConfigShards(extensionRoots: string[]) {
329331
const plans: Array<{ config: string; includePatterns?: string[]; roots: string[] }> = [
330332
...rootsByConfig,
331333
].flatMap(([config, roots]) => {
332-
const chunks = createExtensionTestProcessTargetChunks(config, roots);
334+
const testFiles = shouldSplitExtensionTestProcesses(config)
335+
? listExtensionTestFilesForRoots(roots)
336+
: [];
337+
const chunks = testFiles.length > 0 ? splitExtensionTestJobTargets(config, testFiles) : [roots];
333338
return chunks.length > 1
334339
? chunks.map((includePatterns) => ({ config, includePatterns, roots }))
335340
: [{ config, roots }];

‎scripts/lib/extension-test-plan.mts‎

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -89,16 +89,26 @@ const EXTENSION_TEST_COST_MULTIPLIERS: Record<string, number> = {
8989
// overstates its real wall-clock cost during CI shard planning.
9090
"test/vitest/vitest.extensions.config.ts": 1.1,
9191
};
92+
export const MATRIX_EXTENSION_TEST_PROCESS_FILE_LIMIT = 40;
93+
export const TELEGRAM_EXTENSION_TEST_PROCESS_FILE_LIMIT = 1;
94+
export const TELEGRAM_EXTENSION_TEST_JOB_FILE_LIMIT = 5;
9295
const EXTENSION_TEST_PROCESS_FILE_LIMITS = new Map<string, number>([
9396
// The non-isolated Matrix suite intentionally shares module state within a process.
9497
// Bound its lifetime so Vite's transformed module graph cannot grow across the whole suite.
95-
["test/vitest/vitest.extension-matrix.config.ts", 40],
98+
["test/vitest/vitest.extension-matrix.config.ts", MATRIX_EXTENSION_TEST_PROCESS_FILE_LIMIT],
9699
[
97100
"test/vitest/vitest.extension-telegram.config.ts",
98-
// A 10-file serial worker reached 231s and 2.7 GiB RSS (observed 2026-08).
99-
5,
101+
// isolate:true re-evaluates the Telegram graph per file. A second file in
102+
// the same process stayed silent past the 300s CI watchdog (observed 2026-08,
103+
// changed-extensions-config-14/15 on #123528).
104+
TELEGRAM_EXTENSION_TEST_PROCESS_FILE_LIMIT,
100105
],
101106
]);
107+
const EXTENSION_TEST_JOB_FILE_LIMITS = new Map<string, number>([
108+
// Keep Telegram CI jobs at five files so isolate recycling stays inside one
109+
// job instead of minting one runner per test file.
110+
["test/vitest/vitest.extension-telegram.config.ts", TELEGRAM_EXTENSION_TEST_JOB_FILE_LIMIT],
111+
]);
102112
const EXTENSION_TEST_CONFIG_ROUTES: Array<[(root: string) => boolean, string]> = [
103113
[isActiveMemoryExtensionRoot, "test/vitest/vitest.extension-active-memory.config.ts"],
104114
[isAcpxExtensionRoot, "test/vitest/vitest.extension-acpx.config.ts"],
@@ -236,15 +246,17 @@ export function listExtensionTestFilesForRoots(roots: string[]) {
236246
return [...new Set(files)].toSorted((left, right) => left.localeCompare(right));
237247
}
238248

239-
/** Split an extension config's test files across bounded process lifetimes when required. */
240-
export function splitExtensionTestProcessTargets(config: string, targets: string[]) {
241-
const maxFilesPerProcess = EXTENSION_TEST_PROCESS_FILE_LIMITS.get(config);
242-
const orderedTargets = [...new Set(targets)].toSorted((left, right) => left.localeCompare(right));
243-
if (!maxFilesPerProcess || orderedTargets.length <= maxFilesPerProcess) {
249+
function uniqueSortedTargets(targets: string[]) {
250+
return [...new Set(targets)].toSorted((left, right) => left.localeCompare(right));
251+
}
252+
253+
function splitTargetsByFileLimit(targets: string[], maxFilesPerChunk: number) {
254+
const orderedTargets = uniqueSortedTargets(targets);
255+
if (orderedTargets.length <= maxFilesPerChunk) {
244256
return [orderedTargets];
245257
}
246258

247-
const chunkCount = Math.ceil(orderedTargets.length / maxFilesPerProcess);
259+
const chunkCount = Math.ceil(orderedTargets.length / maxFilesPerChunk);
248260
const baseSize = Math.floor(orderedTargets.length / chunkCount);
249261
const remainder = orderedTargets.length % chunkCount;
250262
const chunks = [];
@@ -257,6 +269,28 @@ export function splitExtensionTestProcessTargets(config: string, targets: string
257269
return chunks;
258270
}
259271

272+
function resolveExtensionTestJobFileLimit(config: string) {
273+
return (
274+
EXTENSION_TEST_JOB_FILE_LIMITS.get(config) ?? EXTENSION_TEST_PROCESS_FILE_LIMITS.get(config)
275+
);
276+
}
277+
278+
/** Split an extension config's test files across bounded process lifetimes when required. */
279+
export function splitExtensionTestProcessTargets(config: string, targets: string[]) {
280+
const maxFilesPerProcess = EXTENSION_TEST_PROCESS_FILE_LIMITS.get(config);
281+
return maxFilesPerProcess
282+
? splitTargetsByFileLimit(targets, maxFilesPerProcess)
283+
: [uniqueSortedTargets(targets)];
284+
}
285+
286+
/** Split an extension config's test files across CI jobs without changing process lifetime. */
287+
export function splitExtensionTestJobTargets(config: string, targets: string[]) {
288+
const maxFilesPerJob = resolveExtensionTestJobFileLimit(config);
289+
return maxFilesPerJob
290+
? splitTargetsByFileLimit(targets, maxFilesPerJob)
291+
: [uniqueSortedTargets(targets)];
292+
}
293+
260294
/** Whether a Vitest invocation can safely be split into independent one-shot processes. */
261295
export function shouldSplitExtensionTestProcesses(config: string, vitestArgs: string[] = []) {
262296
// Passthrough options can carry suite-wide semantics such as bail thresholds,

‎scripts/test-projects.test-support.mts‎

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,10 @@ import {
8989
} from "./changed-lanes.mts";
9090
import { parsePermissiveBooleanToken } from "./lib/arg-utils.mts";
9191
import { getChangedPathFacts } from "./lib/changed-path-facts.mjs";
92-
import { createExtensionTestProcessTargetChunks } from "./lib/extension-test-plan.mts";
92+
import {
93+
createExtensionTestProcessTargetChunks,
94+
splitExtensionTestProcessTargets,
95+
} from "./lib/extension-test-plan.mts";
9396
import {
9497
GATEWAY_SERVER_TEST_PROCESS_COUNT,
9598
listGatewayServerTestTargets,
@@ -975,10 +978,25 @@ function createBoundedExtensionPlans(plan: VitestRunPlan, env?: NodeJS.ProcessEn
975978
if (watchMode || !roots) {
976979
return [plan];
977980
}
978-
// A CI include file already owns the test scope. Expanding its config here
979-
// or emitting local patterns would replace that shard in the run spec.
980-
if (env?.[INCLUDE_FILE_ENV_KEY]?.trim()) {
981-
return [{ ...plan, includePatterns: null }];
981+
// A CI include file already owns the test scope. Keep that file set, but
982+
// still honor process lifetime so isolate:true configs cannot re-import
983+
// a second heavy file in the same Vitest process.
984+
const includeFilePath = env?.[INCLUDE_FILE_ENV_KEY]?.trim();
985+
if (includeFilePath) {
986+
if (!fs.existsSync(includeFilePath)) {
987+
return [{ ...plan, includePatterns: null }];
988+
}
989+
const scopedTargets = loadIncludePatternsForSpecFilter(env ?? {}) ?? [];
990+
const chunks = splitExtensionTestProcessTargets(config, scopedTargets);
991+
if (chunks.length <= 1) {
992+
return [{ ...plan, includePatterns: null }];
993+
}
994+
return chunks.map((includePatterns) => ({
995+
config,
996+
forwardedArgs,
997+
includePatterns,
998+
watchMode,
999+
}));
9821000
}
9831001
const chunks = createExtensionTestProcessTargetChunks(config, roots, forwardedArgs);
9841002
if (chunks.length <= 1) {
@@ -4052,6 +4070,12 @@ export function applyDefaultMultiSpecVitestCachePaths<T extends WatchableVitestS
40524070
if (specs.length <= 1 || specs.some((spec) => spec.watchMode)) {
40534071
return specs;
40544072
}
4073+
// Same-config process lifetimes run one after another and must keep the
4074+
// restored CI seed. Isolating them would make every Telegram file pay a
4075+
// silent cold import.
4076+
if (specs.every((spec) => spec.config === specs[0]?.config)) {
4077+
return specs;
4078+
}
40554079
return applyParallelVitestCachePaths(specs, params);
40564080
}
40574081

‎test/scripts/ci-changed-node-test-plan.test.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
hasQaSmokeAffectingChange,
1111
hasSqliteSessionLifecycleAffectingChange,
1212
} from "../../scripts/lib/ci-changed-node-test-plan.mts";
13+
import { TELEGRAM_EXTENSION_TEST_JOB_FILE_LIMIT } from "../../scripts/lib/extension-test-plan.mts";
1314
import { hasImportGraphImpactOnTargets } from "../../scripts/test-projects.test-support.mts";
1415
import { listGitTrackedFiles } from "../../src/test-utils/repo-files.js";
1516
import { isGatewayServerTestFile } from "../vitest/vitest.gateway-server-paths.mjs";
@@ -299,6 +300,24 @@ describe("CI changed Node test plan", () => {
299300
expect(shards?.flatMap((shard) => shard.configs)).toContain(config);
300301
});
301302

303+
it("packs Telegram process lifetimes into bounded changed-extension jobs", () => {
304+
const shards = createChangedExtensionFallbackShards(["extensions/telegram/src/channel.ts"]);
305+
const targets = shards.flatMap((shard) => shard.includePatterns ?? []);
306+
307+
expect(shards.length).toBeGreaterThan(1);
308+
expect(
309+
shards.every(
310+
(shard) =>
311+
shard.configs[0] === "test/vitest/vitest.extension-telegram.config.ts" &&
312+
(shard.includePatterns?.length ?? 0) > 0 &&
313+
(shard.includePatterns?.length ?? 0) <= TELEGRAM_EXTENSION_TEST_JOB_FILE_LIMIT,
314+
),
315+
).toBe(true);
316+
expect(targets.length).toBeGreaterThan(TELEGRAM_EXTENSION_TEST_JOB_FILE_LIMIT);
317+
expect(new Set(targets).size).toBe(targets.length);
318+
expect(shards).toHaveLength(Math.ceil(targets.length / TELEGRAM_EXTENSION_TEST_JOB_FILE_LIMIT));
319+
});
320+
302321
it("preserves Matrix process bounds in mixed package fallbacks", () => {
303322
const shards = createChangedExtensionFallbackShards([
304323
"packages/gateway-protocol/src/frame-guards.ts",

‎test/scripts/test-extension.test.ts‎

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import {
1414
} from "../../scripts/lib/changed-extensions.mts";
1515
import {
1616
DEFAULT_EXTENSION_TEST_SHARD_COUNT,
17+
MATRIX_EXTENSION_TEST_PROCESS_FILE_LIMIT,
18+
TELEGRAM_EXTENSION_TEST_PROCESS_FILE_LIMIT,
1719
createExtensionTestProcessTargetChunks,
1820
createExtensionTestShards,
1921
listExtensionTestFilesForRoots,
@@ -37,7 +39,7 @@ import { extensionCatchAllExcludedTestRoots } from "../vitest/vitest.extensions.
3739

3840
const scriptPath = path.join(process.cwd(), "scripts", "test-extension.mts");
3941
const posixIt = process.platform === "win32" ? it.skip : it;
40-
const MATRIX_TEST_PROCESS_FILE_LIMIT = 40;
42+
const MATRIX_TEST_PROCESS_FILE_LIMIT = MATRIX_EXTENSION_TEST_PROCESS_FILE_LIMIT;
4143

4244
type RunGroupParams = VitestBatchRunParams;
4345

@@ -170,14 +172,26 @@ describe("scripts/test-extension.mts", () => {
170172
expect(plan.hasTests).toBe(true);
171173
});
172174

173-
it("bounds Matrix test files across balanced process lifetimes", () => {
174-
const config = "test/vitest/vitest.extension-matrix.config.ts";
175-
const roots = [bundledPluginRoot("matrix")];
175+
it.each([
176+
{
177+
name: "Matrix",
178+
config: "test/vitest/vitest.extension-matrix.config.ts",
179+
root: "matrix",
180+
limit: MATRIX_EXTENSION_TEST_PROCESS_FILE_LIMIT,
181+
},
182+
{
183+
name: "Telegram",
184+
config: "test/vitest/vitest.extension-telegram.config.ts",
185+
root: "telegram",
186+
limit: TELEGRAM_EXTENSION_TEST_PROCESS_FILE_LIMIT,
187+
},
188+
])("bounds $name test files across balanced process lifetimes", ({ config, root, limit }) => {
189+
const roots = [bundledPluginRoot(root)];
176190
const expectedFiles = listExtensionTestFilesForRoots(roots);
177191
const chunks = createExtensionTestProcessTargetChunks(config, roots);
178192

179-
expect(chunks).toHaveLength(expectedMatrixTestProcessCount());
180-
expect(chunks.every((chunk) => chunk.length <= MATRIX_TEST_PROCESS_FILE_LIMIT)).toBe(true);
193+
expect(chunks).toHaveLength(Math.max(1, Math.ceil(expectedFiles.length / limit)));
194+
expect(chunks.every((chunk) => chunk.length <= limit)).toBe(true);
181195
expect(Math.max(...chunks.map((chunk) => chunk.length))).toBeLessThanOrEqual(
182196
Math.min(...chunks.map((chunk) => chunk.length)) + 1,
183197
);

‎test/scripts/test-projects.test.ts‎

Lines changed: 93 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ import fs from "node:fs";
44
import os from "node:os";
55
import path from "node:path";
66
import { beforeAll, describe, expect, it, vi } from "vitest";
7-
import { listExtensionTestFilesForRoots } from "../../scripts/lib/extension-test-plan.mts";
7+
import {
8+
listExtensionTestFilesForRoots,
9+
MATRIX_EXTENSION_TEST_PROCESS_FILE_LIMIT,
10+
TELEGRAM_EXTENSION_TEST_PROCESS_FILE_LIMIT,
11+
} from "../../scripts/lib/extension-test-plan.mts";
812
import {
913
CHANNEL_CONTRACT_CONFIG_PATTERNS,
1014
DEFAULT_TEST_PROJECTS_VITEST_NO_OUTPUT_HEARTBEAT_MS,
@@ -41,8 +45,8 @@ import {
4145
} from "../vitest/vitest.contracts-shared.ts";
4246

4347
const normalizeRepoPath = toRepoPath;
44-
const MATRIX_TEST_PROCESS_FILE_LIMIT = 40;
45-
const TELEGRAM_TEST_PROCESS_FILE_LIMIT = 5;
48+
const MATRIX_TEST_PROCESS_FILE_LIMIT = MATRIX_EXTENSION_TEST_PROCESS_FILE_LIMIT;
49+
const TELEGRAM_TEST_PROCESS_FILE_LIMIT = TELEGRAM_EXTENSION_TEST_PROCESS_FILE_LIMIT;
4650

4751
function expectedMatrixTestProcessCount() {
4852
const testFileCount = listExtensionTestFilesForRoots(["extensions/matrix"]).length;
@@ -2146,6 +2150,64 @@ describe("scripts/test-projects changed-target routing", () => {
21462150
);
21472151
});
21482152

2153+
it("splits an externally scoped Telegram include file across process lifetimes", () => {
2154+
const config = "test/vitest/vitest.extension-telegram.config.ts";
2155+
const files = listExtensionTestFilesForRoots(["extensions/telegram"]).slice(
2156+
0,
2157+
TELEGRAM_TEST_PROCESS_FILE_LIMIT + 4,
2158+
);
2159+
expect(files.length).toBeGreaterThan(TELEGRAM_TEST_PROCESS_FILE_LIMIT);
2160+
2161+
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-telegram-include-scope-"));
2162+
try {
2163+
const includeFile = path.join(tempDir, "ci-shard.json");
2164+
fs.writeFileSync(includeFile, JSON.stringify(files));
2165+
const plans = buildVitestRunPlans([config], process.cwd(), () => [], {
2166+
env: { OPENCLAW_VITEST_INCLUDE_FILE: includeFile },
2167+
});
2168+
2169+
expect(plans).toHaveLength(files.length);
2170+
expect(
2171+
plans.every(
2172+
(plan) =>
2173+
plan.config === config &&
2174+
(plan.includePatterns?.length ?? 0) === TELEGRAM_TEST_PROCESS_FILE_LIMIT,
2175+
),
2176+
).toBe(true);
2177+
expect(plans.flatMap((plan) => plan.includePatterns ?? [])).toEqual(files);
2178+
} finally {
2179+
fs.rmSync(tempDir, { force: true, recursive: true });
2180+
}
2181+
});
2182+
2183+
it("turns a five-file Telegram include file into five one-file run specs", () => {
2184+
const config = "test/vitest/vitest.extension-telegram.config.ts";
2185+
const files = listExtensionTestFilesForRoots(["extensions/telegram"]).slice(0, 5);
2186+
expect(files).toHaveLength(5);
2187+
2188+
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-telegram-include-specs-"));
2189+
try {
2190+
const includeFile = path.join(tempDir, "ci-shard.json");
2191+
fs.writeFileSync(includeFile, JSON.stringify(files));
2192+
const specs = createVitestRunSpecs([config], {
2193+
baseEnv: { OPENCLAW_VITEST_INCLUDE_FILE: includeFile },
2194+
tempDir,
2195+
});
2196+
2197+
expect(specs).toHaveLength(5);
2198+
expect(
2199+
specs.every((spec) => spec.config === config && (spec.includePatterns?.length ?? 0) === 1),
2200+
).toBe(true);
2201+
expect(specs.map((spec) => spec.includePatterns?.[0])).toEqual(files);
2202+
expect(new Set(specs.map((spec) => spec.env.OPENCLAW_VITEST_INCLUDE_FILE)).size).toBe(5);
2203+
expect(specs.every((spec) => spec.env.OPENCLAW_VITEST_INCLUDE_FILE !== includeFile)).toBe(
2204+
true,
2205+
);
2206+
} finally {
2207+
fs.rmSync(tempDir, { force: true, recursive: true });
2208+
}
2209+
});
2210+
21492211
it.each([
21502212
{
21512213
channel: "Telegram",
@@ -3696,6 +3758,34 @@ describe("scripts/test-projects Vitest stall watchdog", () => {
36963758
});
36973759

36983760
describe("scripts/test-projects Vitest cache isolation", () => {
3761+
it("keeps same-config process lifetimes on one restored cache", () => {
3762+
const specs = [
3763+
{
3764+
config: "test/vitest/vitest.extension-telegram.config.ts",
3765+
env: { OPENCLAW_VITEST_FS_MODULE_CACHE_PATH: "/tmp/cache" },
3766+
includeFilePath: null,
3767+
includePatterns: ["extensions/telegram/src/a.test.ts"],
3768+
pnpmArgs: [],
3769+
watchMode: false,
3770+
},
3771+
{
3772+
config: "test/vitest/vitest.extension-telegram.config.ts",
3773+
env: { OPENCLAW_VITEST_FS_MODULE_CACHE_PATH: "/tmp/cache" },
3774+
includeFilePath: null,
3775+
includePatterns: ["extensions/telegram/src/b.test.ts"],
3776+
pnpmArgs: [],
3777+
watchMode: false,
3778+
},
3779+
];
3780+
3781+
expect(
3782+
applyDefaultMultiSpecVitestCachePaths(specs, {
3783+
cwd: "/repo",
3784+
env: { OPENCLAW_VITEST_FS_MODULE_CACHE_PATH: "/tmp/cache" },
3785+
}),
3786+
).toBe(specs);
3787+
});
3788+
36993789
it("assigns isolated fs-module caches to multi-spec non-watch runs", () => {
37003790
const specs = applyDefaultMultiSpecVitestCachePaths(
37013791
[

‎test/vitest/vitest.extension-telegram.config.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ export function createExtensionTelegramVitestConfig(
77
) {
88
return createExtensionVitestConfig("telegram", telegramExtensionTestRoots, env, {
99
fileParallelism: false,
10+
// Conflicting module mocks need a fresh graph per file. Pair with the
11+
// one-file process limit so isolate never re-imports inside one worker.
1012
isolate: true,
1113
});
1214
}

0 commit comments

Comments
 (0)