Files
clean-architecture-frontend…/scripts/lib/ungated-scripts.ts
donghyeon-kaandClaude Opus 5.5 15a946fc46 ci(gates): require every verification package script to run in a gate or name why not (12-04)
check-ci-contract only caught test files outside the gates; check:* scripts
such as check:request-fanout and check:remediation-ledger ran nowhere. The
new check reads config/ci/ungated-scripts.json; three checks are now wired
into FE-GATE-010 and FE-GATE-012 and the rest carry a reason.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-27 14:14:42 +09:00

77 lines
3.1 KiB
TypeScript

import { z } from "zod";
import { reachablePackageScripts } from "./package-script-graph.ts";
/**
* 12-04. A verification package script protects nothing unless a CI gate runs
* it. `ungated-tests.ts` covers test files; this covers the scripts. A script
* of a verification family is gated when a gate command reaches it through
* the package script graph, or when it only re-runs Vitest paths a gate
* already runs. Anything else needs a reasoned entry in
* config/ci/ungated-scripts.json.
*/
export const UNGATED_SCRIPTS_PATH = "config/ci/ungated-scripts.json";
const VERIFICATION_SCRIPT = /^(?:check|verify|test|drill|review):/u;
const ungatedScriptsSchema = z
.object({
schemaVersion: z.literal(1),
entries: z.array(
z
.object({
script: z.string().regex(VERIFICATION_SCRIPT),
reason: z.string().trim().min(1, "exclusion reason is required"),
})
.strict(),
),
})
.strict();
export type UngatedScriptExclusion = Readonly<{ script: string; reason: string }>;
export function parseUngatedScriptExclusions(value: unknown): readonly UngatedScriptExclusion[] {
return ungatedScriptsSchema.parse(value).entries;
}
/** Every segment is a Vitest run whose paths all lie inside gated paths. */
function rerunsGatedTests(command: string, gatedTestPaths: readonly string[]): boolean {
const gated = (target: string) =>
gatedTestPaths.some((gatedPath) => target === gatedPath || target.startsWith(`${gatedPath}/`));
return command.split("&&").every((segment) => {
const tokens = segment.trim().split(/\s+/u);
const run = tokens.findIndex(
(token, index) => token === "run" && /(?:run-vitest\.ts|vitest)$/u.test(tokens[index - 1] ?? ""),
);
if (run < 0) return false;
const paths = tokens.slice(run + 1).filter((token) => !token.startsWith("-"));
return paths.length > 0 && paths.every((target) => gated(target.replace(/\/$/u, "")));
});
}
export function ungatedScriptFailures(input: Readonly<{
scripts: Readonly<Record<string, string>>;
entryScripts: readonly string[];
gatedTestPaths: readonly string[];
exclusions: readonly UngatedScriptExclusion[];
}>): string[] {
const reached = new Set(reachablePackageScripts(input.scripts, input.entryScripts));
const gated = (script: string) =>
reached.has(script) || rerunsGatedTests(input.scripts[script] ?? "", input.gatedTestPaths);
const excluded = new Set(input.exclusions.map(({ script }) => script));
const failures = Object.keys(input.scripts)
.filter((script) => VERIFICATION_SCRIPT.test(script) && !gated(script) && !excluded.has(script))
.sort()
.map((script) =>
`verification script is not reached by any CI gate command and is not in ${UNGATED_SCRIPTS_PATH}: ${script}`
);
// An entry for a script a removal deleted is harmless; one for a script a
// gate now reaches would hide the next regression, so it has to go.
for (const { script } of input.exclusions) {
if (input.scripts[script] !== undefined && gated(script)) {
failures.push(`ungated script exclusion is stale; a CI gate reaches it: ${script}`);
}
}
return failures;
}