Skip to content

Commit ce097a1

Browse files
authored
perf(ci): report type failures before broad audits (#134298)
1 parent b99512e commit ce097a1

2 files changed

Lines changed: 156 additions & 39 deletions

File tree

‎scripts/check-changed.mts‎

Lines changed: 48 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -457,28 +457,52 @@ export function createChangedCheckPlan(
457457
options: ChangedCheckPlanOptions = {},
458458
) {
459459
const commands: ChangedCheckCommand[] = [];
460+
const broadAudits = new Set<ChangedCheckCommand>();
461+
const typechecks = new Set<ChangedCheckCommand>();
460462
const baseEnv: NodeJS.ProcessEnv = createChangedCheckChildEnv(options.env ?? process.env);
461463
const generatedExtensionAssetPaths = result.paths.some((changedPath) =>
462464
LINTABLE_EXTENSION_PATH_RE.test(changedPath),
463465
)
464466
? (cachedGeneratedExtensionAssetPaths ??= new Set(listGeneratedExtensionAssetSources()))
465467
: new Set<string>();
466468
const add = (name: string, args: string[], env?: NodeJS.ProcessEnv) => {
467-
if (!commands.some((command) => command.name === name && sameArgs(command.args, args))) {
468-
commands.push({ name, args, ...(env ? { env } : {}) });
469+
const existing = commands.find(
470+
(command) => command.name === name && sameArgs(command.args, args),
471+
);
472+
if (existing) {
473+
return existing;
469474
}
475+
const command = { name, args, ...(env ? { env } : {}) };
476+
commands.push(command);
477+
return command;
470478
};
471479
const addCommand = (name: string, bin: string, args: string[], env?: NodeJS.ProcessEnv) => {
472-
if (
473-
!commands.some(
474-
(command) => command.name === name && command.bin === bin && sameArgs(command.args, args),
475-
)
476-
) {
477-
commands.push({ name, bin, args, ...(env ? { env } : {}) });
480+
const existing = commands.find(
481+
(command) => command.name === name && command.bin === bin && sameArgs(command.args, args),
482+
);
483+
if (existing) {
484+
return existing;
478485
}
486+
const command = { name, bin, args, ...(env ? { env } : {}) };
487+
commands.push(command);
488+
return command;
479489
};
480490
const addTypecheck = (name: string, args: string[]) =>
481-
add(name, args, createSparseTsgoSkipEnv(baseEnv));
491+
typechecks.add(add(name, args, createSparseTsgoSkipEnv(baseEnv)));
492+
const finishPlan = (summary: string) => {
493+
const end = commands.findLastIndex((command) => typechecks.has(command)) + 1;
494+
const prefix = commands.slice(0, end);
495+
// These audits produce diagnostics, not compiler inputs. Defer them without
496+
// moving compiler prerequisites or overlapping their resource-heavy processes.
497+
return {
498+
commands: [
499+
...prefix.filter((command) => !broadAudits.has(command)),
500+
...prefix.filter((command) => broadAudits.has(command)),
501+
...commands.slice(end),
502+
],
503+
summary,
504+
};
505+
};
482506
const addLint = (name: string, args: string[]) => add(name, args, baseEnv);
483507
const addTargetedLint = (
484508
createCommand: (
@@ -583,7 +607,7 @@ export function createChangedCheckPlan(
583607
add("extension test core imports", ["lint:plugins:no-extension-test-core-imports"]);
584608
}
585609
add("duplicate scan target coverage", ["dup:check:coverage"]);
586-
add("coercion helper declaration guard", ["check:coercion-helpers"]);
610+
broadAudits.add(add("coercion helper declaration guard", ["check:coercion-helpers"]));
587611
add("dependency pin guard", ["deps:pins:check"]);
588612
if (result.paths.length > 0) {
589613
add("format changed files", [
@@ -642,7 +666,7 @@ export function createChangedCheckPlan(
642666
add("Plugin SDK surface budget", ["plugin-sdk:surface:check"]);
643667
}
644668
if (result.lanes.all || shouldRunDeprecationHygieneChecks(result.paths)) {
645-
add("deprecated API usage", ["check:deprecated-api-usage"]);
669+
broadAudits.add(add("deprecated API usage", ["check:deprecated-api-usage"]));
646670
// After 2026-07-24, lapsed compatibility windows intentionally fail this gate
647671
// until their scheduled deletion PRs land.
648672
add("plugin boundaries", ["plugins:boundary-report:ci"]);
@@ -662,19 +686,18 @@ export function createChangedCheckPlan(
662686
hasDeadcodeScannedSource(result.paths) &&
663687
!isOpenEndedTruthyValue(baseEnv.OPENCLAW_CHECK_CHANGED_SKIP_DEADCODE)
664688
) {
665-
addCommand(
666-
"dead export scan (skip with OPENCLAW_CHECK_CHANGED_SKIP_DEADCODE=1)",
667-
"node",
668-
["--import", "tsx", "scripts/check-deadcode-exports.mts"],
669-
baseEnv,
689+
broadAudits.add(
690+
addCommand(
691+
"dead export scan (skip with OPENCLAW_CHECK_CHANGED_SKIP_DEADCODE=1)",
692+
"node",
693+
["--import", "tsx", "scripts/check-deadcode-exports.mts"],
694+
baseEnv,
695+
),
670696
);
671697
}
672698

673699
if (result.docsOnly) {
674-
return {
675-
commands,
676-
summary: "docs-only",
677-
};
700+
return finishPlan("docs-only");
678701
}
679702

680703
addTestTempCreationReport();
@@ -718,10 +741,7 @@ export function createChangedCheckPlan(
718741
add("config schema baseline", ["config:schema:check"]);
719742
add("config docs baseline", ["config:docs:check"]);
720743
add("root dependency ownership", ["deps:root-ownership:check"]);
721-
return {
722-
commands,
723-
summary: "release metadata",
724-
};
744+
return finishPlan("release metadata");
725745
}
726746

727747
if (shouldRunAndroidVersionSync) {
@@ -735,10 +755,7 @@ export function createChangedCheckPlan(
735755
addTypecheck("typecheck all", ["tsgo:all"]);
736756
addLint("lint", ["lint"]);
737757
add("runtime import cycles", ["check:import-cycles"]);
738-
return {
739-
commands,
740-
summary: "all",
741-
};
758+
return finishPlan("all");
742759
}
743760

744761
if (shouldRunControlUiI18nVerify(result.paths)) {
@@ -905,13 +922,12 @@ export function createChangedCheckPlan(
905922
});
906923
}
907924

908-
return {
909-
commands,
910-
summary: Object.entries(lanes)
925+
return finishPlan(
926+
Object.entries(lanes)
911927
.filter(([, enabled]) => enabled)
912928
.map(([lane]) => lane)
913929
.join(", "),
914-
};
930+
);
915931
}
916932

917933
export function createTargetedCoreLintCommand(

‎test/scripts/changed-lanes.test.ts‎

Lines changed: 108 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Changed Lanes tests cover changed lanes script behavior.
22
import { execFileSync, spawnSync } from "node:child_process";
3-
import { existsSync, mkdirSync, readFileSync, unlinkSync, writeFileSync } from "node:fs";
3+
import { chmodSync, existsSync, mkdirSync, readFileSync, unlinkSync, writeFileSync } from "node:fs";
44
import { createRequire } from "node:module";
55
import path from "node:path";
66
import { pathToFileURL } from "node:url";
@@ -165,6 +165,62 @@ function runChangedFormatLaneWithRepoOxfmt(cwd: string, changedPaths: string[])
165165
});
166166
}
167167

168+
// Keep the real gate and managed children; only the external check commands are synthetic.
169+
function runChangedCheckWithRecordedCommands(failingCommand: string | null) {
170+
const dir = makeTempRepoRoot(tempDirs, "openclaw-changed-check-order-");
171+
const binDir = path.join(dir, "bin");
172+
const eventsPath = path.join(dir, "events.jsonl");
173+
const childPath = path.join(dir, "command.cjs");
174+
mkdirSync(binDir);
175+
writeFileSync(eventsPath, "");
176+
writeFileSync(
177+
childPath,
178+
`
179+
const fs = require("node:fs");
180+
const bin = process.argv[2];
181+
const args = process.argv.slice(3);
182+
const events = ${JSON.stringify(eventsPath)};
183+
const active = ${JSON.stringify(path.join(dir, "active"))};
184+
const record = (event) => fs.appendFileSync(events, JSON.stringify({event, bin, args}) + "\\n");
185+
fs.mkdirSync(active);
186+
record("start");
187+
process.on("exit", () => { record("finish"); fs.rmdirSync(active); });
188+
if (bin === "pnpm" && args[0] === ${JSON.stringify(failingCommand)}) {
189+
console.error("Synthetic check failure: " + args[0]);
190+
process.exitCode = 23;
191+
}
192+
`,
193+
);
194+
const quote = (value: string) => `'${value.replaceAll("'", "'\\''")}'`;
195+
for (const bin of ["pnpm", "node"]) {
196+
const launcher = path.join(binDir, bin);
197+
writeFileSync(
198+
launcher,
199+
`#!/bin/sh\nexec ${quote(process.execPath)} ${quote(childPath)} ${bin} "$@"\n`,
200+
);
201+
chmodSync(launcher, 0o755);
202+
writeFileSync(
203+
`${launcher}.cmd`,
204+
`@echo off\r\n"${process.execPath}" "${childPath}" ${bin} %*\r\n`,
205+
);
206+
}
207+
const paths = ["src/gateway/server-runtime-state.ts"];
208+
const result = runRepoScript("scripts/check-changed.mjs", ["--", ...paths], {
209+
...createNestedGitEnv(),
210+
CI: "",
211+
GITHUB_ACTIONS: "",
212+
OPENCLAW_CHECK_CHANGED_REMOTE_CHILD: "1",
213+
OPENCLAW_CHECK_CHANGED_SKIP_DEADCODE: "",
214+
PATH: `${binDir}${path.delimiter}${process.env.PATH ?? ""}`,
215+
});
216+
const events: { event: string; bin: string; args: string[] }[] = readFileSync(eventsPath, "utf8")
217+
.trim()
218+
.split("\n")
219+
.filter(Boolean)
220+
.map((line) => JSON.parse(line));
221+
return { events, paths, result };
222+
}
223+
168224
function createSyntheticMergeRepo(prefix: string): { dir: string; staleBase: string } {
169225
const dir = makeTempRepoRoot(tempDirs, prefix);
170226
git(dir, ["init", "-q", "--initial-branch=main"]);
@@ -367,6 +423,51 @@ describe("scripts/changed-lanes", () => {
367423
]);
368424
});
369425

426+
it.each(["tsgo:core:test", "lint:tmp:tsgo-core-boundary", null])(
427+
"runs types before broad audits while retaining serial gate execution (%s)",
428+
(failingCommand) => {
429+
const { events, paths, result } = runChangedCheckWithRecordedCommands(failingCommand);
430+
expect(result.error, result.stderr).toBeUndefined();
431+
expect(result.signal, result.stderr).toBeNull();
432+
expect(result.status, result.stderr).toBe(failingCommand === null ? 0 : 23);
433+
const commands = events.filter((event) => event.event === "start");
434+
const planned = createChangedCheckPlan(detectChangedLanes(paths)).commands.map((command) => ({
435+
bin: command.bin ?? "pnpm",
436+
args: command.args,
437+
}));
438+
const end =
439+
failingCommand === null
440+
? planned.length
441+
: planned.findIndex((command) => command.args[0] === failingCommand) + 1;
442+
expect(commands.map(({ bin, args }) => ({ bin, args }))).toEqual(planned.slice(0, end));
443+
expect(events).toEqual(
444+
commands.flatMap(({ bin, args }) => [
445+
{ event: "start", bin, args },
446+
{ event: "finish", bin, args },
447+
]),
448+
);
449+
const broadAudits = commands.filter(({ args }) =>
450+
args.some((arg) =>
451+
[
452+
"check:coercion-helpers",
453+
"check:deprecated-api-usage",
454+
"scripts/check-deadcode-exports.mts",
455+
].includes(arg),
456+
),
457+
);
458+
if (failingCommand !== null) {
459+
expect(result.stderr).toContain(`Synthetic check failure: ${failingCommand}`);
460+
expect(broadAudits).toEqual([]);
461+
} else {
462+
expect(broadAudits).toHaveLength(3);
463+
const lastTypecheck = commands.findLastIndex(({ args }) => args[0]?.startsWith("tsgo:"));
464+
for (const audit of broadAudits) {
465+
expect(commands.indexOf(audit)).toBeGreaterThan(lastTypecheck);
466+
}
467+
}
468+
},
469+
);
470+
370471
it("prints changed check dry-run commands", () => {
371472
const result = runRepoScript("scripts/check-changed.mjs", [
372473
"--dry-run",
@@ -406,13 +507,13 @@ describe("scripts/changed-lanes", () => {
406507
"pnpm lint:extensions:no-guarded-wildcard-reexports",
407508
"pnpm lint:extensions:no-plugin-sdk-wildcard-reexports",
408509
"pnpm dup:check:coverage",
409-
"pnpm check:coercion-helpers",
410510
"pnpm deps:pins:check",
411511
`pnpm format:check --no-error-on-unmatched-pattern -- ${lanes.paths.join(" ")}`,
412512
"pnpm deps:patches:check",
413513
"node scripts/report-test-temp-creations.mjs --base origin/main --head HEAD",
414514
"pnpm lint:tmp:tsgo-core-boundary",
415515
"pnpm tsgo:test:root",
516+
"pnpm check:coercion-helpers",
416517
"pnpm lint:scripts",
417518
]);
418519
});
@@ -1472,20 +1573,20 @@ describe("scripts/changed-lanes", () => {
14721573
"guarded extension wildcard re-exports",
14731574
"plugin-sdk wildcard re-exports",
14741575
"duplicate scan target coverage",
1475-
"coercion helper declaration guard",
14761576
"dependency pin guard",
14771577
"format changed files",
1478-
"deprecated API usage",
14791578
"plugin boundaries",
14801579
"wrapper shadowing",
14811580
"package patch guard",
1581+
"test temp creation report (warning-only)",
1582+
"core tsgo graph boundary",
1583+
"typecheck core tests",
1584+
"coercion helper declaration guard",
1585+
"deprecated API usage",
14821586
// These live-Docker paths include `src/gateway/*.live.test.ts`, and the
14831587
// full-tree knip scan sees test files, so a deleted last consumer can
14841588
// orphan an export here too.
14851589
"dead export scan (skip with OPENCLAW_CHECK_CHANGED_SKIP_DEADCODE=1)",
1486-
"test temp creation report (warning-only)",
1487-
"core tsgo graph boundary",
1488-
"typecheck core tests",
14891590
"lint core",
14901591
"lint scripts",
14911592
"live Docker shell syntax",

0 commit comments

Comments
 (0)