Files
clean-architecture-frontend…/scripts/check-optional-recipes.ts
DongHyeonka 7679ce6592 revert: #55 되돌림(411c9338)을 되돌려 그룹 C 검사기 변경을 다시 넣는다
This reverts commit 411c9338c6.

문제: #55(b4895894)가 main run 376의 FE-GATE-030(browser file/storage
제거 드릴)을 깨서 411c9338(#64)로 통째로 되돌렸다. #55의 검사기 진단과
시험 빈틈 고침 12커밋이 main에 없다.

고른 방법: 되돌림을 되돌려 #55를 그대로 다시 넣고, 드릴을 깬 원인은
다음 커밋에서 따로 고친다. 리뷰어가 이 커밋이 #55와 같은지 트리로 볼 수
있다. 이 커밋의 트리는 b4895894 트리에 그 뒤 main에 들어온 #63·#65만
더한 것이다(git diff b4895894 <이 커밋>: docs/ledger 두 파일,
cross-context-invalidation adapter 한 파일과 시험 두 파일).
버린 대안: #55 커밋 12개 cherry-pick. 71606ff1 merge의 충돌 해소분을
다시 풀어야 하고 결과 트리를 b4895894와 대조하기 어렵다.

위험·한계: 이 커밋만으로는 main 단계의 FE-GATE-030이 run 376처럼
실패한다(같은 트리 내용). PR 단계 게이트는 돈다.

검증: 다음 커밋 본문에 적는다. 그 커밋의 새 단언을 이 커밋의 검사기에
돌리면 실패한다(RED).

Refs: #55, #64
2026-11-19 16:32:00 +09:00

282 lines
9.0 KiB
TypeScript

import { mkdir, readFile, stat } from "node:fs/promises";
import { optionalRecipesArtifactSchema } from "./contracts/release-artifacts.ts";
import {
measureOptionalRecipeBundle,
type OptionalRecipeBundleMeasurement,
} from "./lib/optional-recipe-bundle.ts";
import {
scanOptionalRecipeSources,
scanProductionBundle,
validateRecipeCatalog,
} from "./lib/optional-recipes.ts";
import { assertMatchesJsonSchema } from "./lib/json-schema.ts";
import { checkoutBuildReadFailure } from "./lib/run-isolation.ts";
import { writeValidatedJsonArtifact } from "./lib/validated-json-artifact.ts";
type GateViolation = Readonly<{
ruleId: string;
path: string;
detail?: string;
}>;
type ReferenceRuntimeBundleInput = Readonly<{
recipeId: string;
sourceRoots: readonly string[];
bundleBudgetGzipBytes: number;
}>;
const argument = (name: string, fallback: string): string => {
const index = process.argv.indexOf(name);
return index === -1 ? fallback : (process.argv[index + 1] ?? fallback);
};
const catalogPath = argument(
"--catalog",
"config/recipes/frontend-capability-recipes.json",
);
const sourceRoot = argument("--source-root", "src");
const distRoot = argument("--dist-root", "dist");
// 1106/12-02. Without --dist-root this scans the checkout's dist; under
// RUN_OUTPUT_ROOT the build wrote elsewhere, so it would judge an earlier build.
const outputRootFailure = process.argv.includes("--dist-root")
? undefined
: checkoutBuildReadFailure("scripts/check-optional-recipes.ts");
if (outputRootFailure !== undefined) {
process.stderr.write(`${outputRootFailure}\n`);
process.exit(1);
}
const artifactPath = argument(
"--artifact",
"artifacts/quality/optional-recipes.json",
);
const requireDist = process.argv.includes("--require-dist");
const catalog = JSON.parse(
await readFile(catalogPath, "utf8"),
) as Record<string, unknown>;
const recipes = Array.isArray(catalog.recipes) ? catalog.recipes : [];
const packageDocument = JSON.parse(
await readFile("package.json", "utf8"),
) as Record<string, unknown>;
const catalogSchemaViolations: string[] = [];
try {
assertMatchesJsonSchema(
JSON.parse(
await readFile(
"config/schemas/recipes/frontend-capability-recipes.schema.json",
"utf8",
),
),
catalog,
"optional recipe catalog",
);
} catch {
catalogSchemaViolations.push("CATALOG_JSON_SCHEMA_INVALID");
}
const catalogViolations = [
...catalogSchemaViolations,
...validateRecipeCatalog(catalog, packageDocument),
];
const runtimeSourceViolations = (
await Promise.all(
recipes.flatMap((recipe: unknown) => {
if (!recipe || typeof recipe !== "object") return [];
const row = recipe as Record<string, unknown>;
const runtime = row.referenceRuntime;
if (!runtime || typeof runtime !== "object") return [];
const sourceRoots = (runtime as Record<string, unknown>).sourceRoots;
if (!Array.isArray(sourceRoots)) return [];
return sourceRoots
.filter(
(sourceRoot): sourceRoot is string =>
typeof sourceRoot === "string",
)
.map(async (sourceRoot) => ({
sourceRoot,
exists: await stat(sourceRoot)
.then(() => true)
.catch(() => false),
}));
}),
)
).filter(({ exists }) => !exists);
const {
inputs: referenceRuntimeBundleInputs,
violations: referenceRuntimeBundleConfigurationViolations,
} = referenceRuntimeBundleInputsFrom(recipes);
const referenceRuntimeBundles: OptionalRecipeBundleMeasurement[] = [];
const referenceRuntimeBundleMeasurementViolations: GateViolation[] = [];
for (const bundleInput of referenceRuntimeBundleInputs) {
try {
const measurement = await measureOptionalRecipeBundle(bundleInput);
referenceRuntimeBundles.push(measurement);
if (!measurement.passed) {
referenceRuntimeBundleMeasurementViolations.push({
ruleId: "REFERENCE_RUNTIME_BUNDLE_BUDGET_EXCEEDED",
path: measurement.recipeId,
detail:
`${measurement.gzipBytes} > ` +
`${measurement.bundleBudgetGzipBytes} gzip bytes`,
});
}
} catch (error) {
referenceRuntimeBundleMeasurementViolations.push({
ruleId: "REFERENCE_RUNTIME_BUNDLE_MEASUREMENT_FAILED",
path: bundleInput.recipeId,
detail: safeErrorSummary(error),
});
}
}
referenceRuntimeBundles.sort((left, right) =>
compareText(left.recipeId, right.recipeId),
);
const sourceViolations = await scanOptionalRecipeSources(sourceRoot);
const bundlePresent = await stat(`${distRoot}/.vite/manifest.json`)
.then(() => true)
.catch(() => false);
const bundleViolations = await scanProductionBundle(distRoot);
const violations: GateViolation[] = [
...catalogViolations.map((ruleId) => ({ ruleId, path: catalogPath })),
...runtimeSourceViolations.map(({ sourceRoot }) => ({
ruleId: "REFERENCE_RUNTIME_SOURCE_MISSING",
path: sourceRoot,
})),
...sourceViolations,
...bundleViolations.map((path) => ({
ruleId: "UNSELECTED_RECIPE_IN_PRODUCTION_BUNDLE",
path,
})),
...(requireDist && !bundlePresent
? [{ ruleId: "PRODUCTION_BUNDLE_MISSING", path: distRoot }]
: []),
...referenceRuntimeBundleConfigurationViolations,
...referenceRuntimeBundleMeasurementViolations,
];
const report = optionalRecipesArtifactSchema.parse({
schemaVersion: 1,
decisionId: "VD-10",
selectedCapabilities: [],
referenceRuntimes: recipes
.filter(
(recipe: unknown): recipe is Record<string, unknown> =>
Boolean(
recipe &&
typeof recipe === "object" &&
(recipe as Record<string, unknown>).referenceRuntime,
),
)
.map((recipe: Record<string, unknown>) => ({
id: recipe.id,
referenceRuntime: recipe.referenceRuntime,
})),
recipeCount: recipes.length,
productionRuntimeDependencies:
catalog.productionRuntimeDependencies ?? null,
referenceRuntimeBundleBudgets: referenceRuntimeBundles,
bundleStatus: bundlePresent
? bundleViolations.length === 0
? "PASS"
: "FAIL"
: "NOT_BUILT",
violations,
passed: violations.length === 0,
});
await mkdir("artifacts/quality", { recursive: true });
await writeValidatedJsonArtifact({
path: artifactPath,
schema: optionalRecipesArtifactSchema,
value: report,
});
if (violations.length > 0) {
process.stderr.write(
`Optional recipe contract failed:\n${violations
.map(
(violation) =>
`${violation.ruleId}: ${violation.path}` +
(violation.detail ? ` (${violation.detail})` : ""),
)
.join("\n")}\n`,
);
process.exit(1);
}
const referenceRuntimeBudgetStatus =
referenceRuntimeBundles.length > 0
? `, budgets=${referenceRuntimeBundles
.map(
(measurement) =>
`${measurement.recipeId}:${measurement.gzipBytes}/` +
measurement.bundleBudgetGzipBytes,
)
.join(",")} gzip bytes`
: "";
process.stdout.write(
`Optional recipes: PASS (${report.recipeCount} optional capabilities, ${report.referenceRuntimes.length} uncomposed reference runtimes, bundle=${report.bundleStatus}${referenceRuntimeBudgetStatus})\n`,
);
function referenceRuntimeBundleInputsFrom(
recipeRows: readonly unknown[],
): Readonly<{
inputs: readonly ReferenceRuntimeBundleInput[];
violations: readonly GateViolation[];
}> {
const inputs: ReferenceRuntimeBundleInput[] = [];
const violations: GateViolation[] = [];
for (const recipe of recipeRows) {
if (!recipe || typeof recipe !== "object") continue;
const row = recipe as Record<string, unknown>;
if (row.referenceRuntime === undefined) continue;
const recipeId =
typeof row.id === "string" ? row.id : "unknown-reference-runtime";
const runtime = row.referenceRuntime;
const sourceRoots =
runtime && typeof runtime === "object"
? (runtime as Record<string, unknown>).sourceRoots
: null;
if (
!Array.isArray(sourceRoots) ||
!sourceRoots.every(
(sourceRoot): sourceRoot is string =>
typeof sourceRoot === "string",
) ||
!Number.isSafeInteger(row.bundleBudgetGzipBytes) ||
(row.bundleBudgetGzipBytes as number) < 1
) {
violations.push({
ruleId: "REFERENCE_RUNTIME_BUNDLE_CONFIGURATION_INVALID",
path: recipeId,
});
continue;
}
inputs.push(
Object.freeze({
recipeId,
sourceRoots: Object.freeze([...sourceRoots]),
bundleBudgetGzipBytes: row.bundleBudgetGzipBytes as number,
}),
);
}
return Object.freeze({
inputs: Object.freeze(
inputs.sort((left, right) =>
compareText(left.recipeId, right.recipeId),
),
),
violations: Object.freeze(violations),
});
}
function safeErrorSummary(error: unknown): string {
const message =
error instanceof Error ? error.message : "unknown measurement failure";
return message
.split(/\r?\n/, 1)[0]
?.replaceAll(process.cwd(), ".")
.slice(0, 512) ?? "unknown measurement failure";
}
function compareText(left: string, right: string): number {
return left < right ? -1 : left > right ? 1 : 0;
}