문제: `RUN_OUTPUT_ROOT`가 다른 폴더를 가리키면 빌드는 그 아래에 쓰는데, `verify-reproducible-build.ts`, `check-optional-recipes.ts`, `check-app-build-input.ts --require-bundle`, `create-release-candidate.ts`는 checkout의 `dist/`·`artifacts/`를 읽고 스스로 거절하지 않았다. 게이트 안에서는 앞 단계가 거절해 거짓 PASS가 없었지만, 단독 실행은 `verify:reproducible-build`가 이전 빌드를 두 번 재서 `passed: true`를 쓸 수 있었다. 고친 것: 넷이 시작할 때 `checkoutBuildReadFailure`(check-bundle.ts와 같다)를 부르고 변수 이름·값을 적으며 exit 1. `check-optional-recipes.ts`는 `--dist-root`를 받았으면 그 폴더를 읽으므로 묻지 않는다. 검증: desktop `node scripts/run-vitest.ts run tests/unit/run-isolation.test.ts` 통과. 넷을 `RUN_OUTPUT_ROOT=.tmp/run-output-refusal`로 띄워 rc=1과 메시지를 본다(가장 긴 실행 499 ms). 기준 커밋에서는 넷 다 실패한다(RED). Refs: 1106/12-02
152 lines
6.1 KiB
TypeScript
152 lines
6.1 KiB
TypeScript
import { readFile } from "node:fs/promises";
|
|
import path from "node:path";
|
|
|
|
import {
|
|
createFileSystemDeclarationSource,
|
|
type TopologyDeclarationSource,
|
|
} from "./generate-product-feature-index.ts";
|
|
import { checkoutBuildReadFailure } from "./lib/run-isolation.ts";
|
|
|
|
/**
|
|
* scale-architecture.md §3 / §37. Build-input regression gate for deployable-app isolation.
|
|
*
|
|
* The app being built is the one recorded in `src/.generated/product-app.ts`.
|
|
* Its allowed features are exactly those owned (via their context) by that
|
|
* app's declarations. The gate fails when
|
|
* - a generated index imports a feature or declares a context outside that
|
|
* ownership set (e.g. a regression back to "import every src/features"),
|
|
* - the production bundle's module inventory contains a module of a feature
|
|
* the app does not own.
|
|
*/
|
|
export type AppBuildInputReport = Readonly<{
|
|
appId: string;
|
|
ownedContexts: readonly string[];
|
|
ownedFeatureDirectories: readonly string[];
|
|
generatedFeatureImports: readonly string[];
|
|
generatedContexts: readonly string[];
|
|
bundle: Readonly<{ checked: boolean; featureDirectories: readonly string[] }>;
|
|
violations: readonly string[];
|
|
}>;
|
|
|
|
const GENERATED = [
|
|
"src/.generated/product-feature-index.ts",
|
|
"src/.generated/product-context-index.ts",
|
|
"src/.generated/product-app.ts",
|
|
] as const;
|
|
|
|
function unique(values: Iterable<string>): string[] {
|
|
return [...new Set(values)].sort();
|
|
}
|
|
|
|
export async function inspectAppBuildInput(
|
|
root: string,
|
|
options: Readonly<{
|
|
source?: TopologyDeclarationSource;
|
|
bundleInventory?: string | null;
|
|
}> = {},
|
|
): Promise<AppBuildInputReport> {
|
|
const source = options.source ?? (await createFileSystemDeclarationSource(root));
|
|
const [featureIndex, contextIndex, appIndex] = await Promise.all(
|
|
GENERATED.map((file) => readFile(path.join(root, file), "utf8")),
|
|
);
|
|
const appId = /id:\s*"([^"]+)"/u.exec(appIndex ?? "")?.[1];
|
|
const violations: string[] = [];
|
|
if (!appId || !source.app(appId)) {
|
|
violations.push(`generated app ${appId ?? "<missing>"} is not a declared deployable app`);
|
|
}
|
|
const ownedContexts = appId ? source.contextsOf(appId).map((context) => context.id) : [];
|
|
const ownedFeatureDirectories = unique(
|
|
ownedContexts.flatMap((contextId) => source.featuresOf(contextId).map((feature) => feature.directory)),
|
|
);
|
|
const owned = new Set(ownedFeatureDirectories);
|
|
|
|
const featureDirectoriesIn = (text: string) =>
|
|
unique([...text.matchAll(/\.\.\/features\/([^/"']+)\//gu)].map((match) => match[1] as string));
|
|
const generatedFeatureImports = unique([
|
|
...featureDirectoriesIn(featureIndex ?? ""),
|
|
...featureDirectoriesIn(contextIndex ?? ""),
|
|
]);
|
|
for (const directory of generatedFeatureImports) {
|
|
if (!owned.has(directory)) {
|
|
violations.push(`generated index imports feature "${directory}" not owned by app ${appId}`);
|
|
}
|
|
}
|
|
const generatedContexts = unique(
|
|
[...(contextIndex ?? "").matchAll(/defineBoundedContextModule\(\{\s*id:\s*"([^"]+)"/gu)].map(
|
|
(match) => match[1] as string,
|
|
),
|
|
);
|
|
const ownedContextSet = new Set(ownedContexts);
|
|
for (const contextId of generatedContexts) {
|
|
if (!ownedContextSet.has(contextId)) {
|
|
violations.push(`generated index declares context "${contextId}" not owned by app ${appId}`);
|
|
}
|
|
}
|
|
|
|
let bundle: AppBuildInputReport["bundle"] = { checked: false, featureDirectories: [] };
|
|
const inventoryPath =
|
|
options.bundleInventory === undefined
|
|
? path.join(root, "artifacts/quality/vite-module-inventory.json")
|
|
: options.bundleInventory;
|
|
if (inventoryPath) {
|
|
const raw = await readFile(inventoryPath, "utf8").catch(() => null);
|
|
if (raw !== null) {
|
|
const inventory = JSON.parse(raw) as { chunks?: { modules?: string[] }[] };
|
|
const modules = (inventory.chunks ?? []).flatMap((chunk) => chunk.modules ?? []);
|
|
const featureDirectories = unique(
|
|
modules.flatMap((module) => {
|
|
const match = /^src\/features\/([^/]+)\//u.exec(module);
|
|
return match ? [match[1] as string] : [];
|
|
}),
|
|
);
|
|
bundle = { checked: true, featureDirectories };
|
|
for (const directory of featureDirectories) {
|
|
if (!owned.has(directory)) {
|
|
violations.push(`production bundle contains feature "${directory}" not owned by app ${appId}`);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return Object.freeze({
|
|
appId: appId ?? "",
|
|
ownedContexts: unique(ownedContexts),
|
|
ownedFeatureDirectories,
|
|
generatedFeatureImports,
|
|
generatedContexts,
|
|
bundle,
|
|
violations,
|
|
});
|
|
}
|
|
|
|
async function main(): Promise<void> {
|
|
// The bundle inventory is only meaningful right after a build of this tree;
|
|
// without `--require-bundle` the gate checks the generated indexes only, so a
|
|
// stale inventory from another build can never pass or fail it.
|
|
const requireBundle = process.argv.includes("--require-bundle");
|
|
// 1106/12-02. The inventory is read where the checkout keeps it; under
|
|
// RUN_OUTPUT_ROOT the build wrote elsewhere and this would judge an earlier one.
|
|
const outputRootFailure = requireBundle ? checkoutBuildReadFailure("scripts/check-app-build-input.ts") : undefined;
|
|
if (outputRootFailure !== undefined) {
|
|
process.stderr.write(`${outputRootFailure}\n`);
|
|
process.exit(1);
|
|
}
|
|
const report = await inspectAppBuildInput(process.cwd(), requireBundle ? {} : { bundleInventory: null });
|
|
if (requireBundle && !report.bundle.checked) {
|
|
process.stderr.write("app build input: no production module inventory; run the build first\n");
|
|
process.exit(1);
|
|
}
|
|
for (const violation of report.violations) process.stderr.write(`FAIL ${violation}\n`);
|
|
process.stdout.write(
|
|
`app build input: app=${report.appId} contexts=${report.ownedContexts.length} ` +
|
|
`features=${report.ownedFeatureDirectories.length} generatedImports=${report.generatedFeatureImports.length} ` +
|
|
`bundle=${report.bundle.checked ? `${report.bundle.featureDirectories.length} feature dirs` : "not checked"} ` +
|
|
`${report.violations.length === 0 ? "PASS" : "FAIL"}\n`,
|
|
);
|
|
if (report.violations.length > 0) process.exitCode = 1;
|
|
}
|
|
|
|
if (import.meta.url === `file://${process.argv[1]}`) {
|
|
await main();
|
|
}
|