Carries 359 template commits, tag v0.2.0 plus one documentation commit: the generator-built feature index with its app, bounded-context and feature declarations, the src/platform kernel and scale tooling, the 2026-09-24 src review fixes, and the 2026-09-25 maturity review round with its CHANGELOG [보안]/[계약] rows. 691 of the 824 template paths are applied, 85 of them merged with product changes; 127 are not carried. Product identity is unchanged: name, version 0.1.0, routes, design and the TechLog domain. Three decisions worth naming. TechLog stays composed directly, as the last two syncs recorded; its directory carries a NOT_INSTALLED marker that says so, and every installed-* composer takes the template's projection with TechLog added on top. The reference sample is no longer installed: the template now installs a feature only through feature-module.ts and context-routes.ts, which load the screens this product deleted, so its three operations, six schemas and one topic leave the registries, recorded as registry change evidence. And AppRouter mounts the platform and TechLog routes only; the template's bounded-context route gateway waits for a generator-installed feature. Required fixes: readSubject on the BFF session owner (without it every HTTP Studio session would close as integration-failed), zod through the facade in nine TechLog files, noUnusedLocals, and public/config.json written out to match the local profile. Derived baselines were recomputed after reproducing both sides' values: CI contract 101 commands / 106 references / 141 artifacts, gate shape c9adcf85; registry approval 2dc6c3a3; the runtime config schema baseline equals the schema at8210d48. Verified here: types, lint, architecture, adapter inventory, CI contract, i18n, design system, diagnostics, browser security, dev release manifest, TechLog contract and build pass; 220 component, 155 integration, 15 contract, 418 TechLog, 43 reference-feature, 18 recipe and 46 release-coherence tests pass, and 2381 of 2383 unit tests. Reproduced at8210d48first and unchanged: check:registries (byte-identical), test:runtime-schema, verify:documentation. Brought by the template and escalated rather than suppressed: check:request-fanout on TechLog's cursor pagination, check:remediation-ledger (fails upstream too), the shared-chunk hash test, and the registry-dependent sample removal and second-feature tests. Playwright suites were not run. Full record, the five open questions and the follow-ups in docs/operations/template-sync-2026-09-29.md. (cherry picked from commit b2170cf265dea45dc9cc05d3c61e627e97f31cdb)
305 lines
11 KiB
TypeScript
305 lines
11 KiB
TypeScript
import { spawn } from "node:child_process";
|
|
import { mkdir, readFile } from "node:fs/promises";
|
|
import { cpus, loadavg } from "node:os";
|
|
import process from "node:process";
|
|
|
|
import { chromium, type Browser } from "@playwright/test";
|
|
|
|
import {
|
|
assertPortUnused,
|
|
assertServedRelease,
|
|
DEFAULT_LAB_PORT,
|
|
DEFAULT_LAB_RUNS,
|
|
previewArguments,
|
|
summarizeSamples,
|
|
} from "./lib/lab-performance.ts";
|
|
import { evaluateLabBudget } from "./lib/performance-budgets.ts";
|
|
import { ROUTE_REGISTRY } from "../src/features/installed-feature-contracts.ts";
|
|
import { labPerformanceArtifactSchema } from "./contracts/release-artifacts.ts";
|
|
import { writeValidatedJsonArtifact } from "./lib/validated-json-artifact.ts";
|
|
|
|
type ContractPerformanceEvidence = {
|
|
lcpMs: number;
|
|
cls: number;
|
|
clickAt: number | null;
|
|
headingAt: number | null;
|
|
eventDurationMs: number | null;
|
|
};
|
|
|
|
type ContractPerformanceWindow = Window & {
|
|
__contractPerformance?: ContractPerformanceEvidence;
|
|
};
|
|
|
|
type LayoutShiftEntry = PerformanceEntry & {
|
|
hadRecentInput: boolean;
|
|
value: number;
|
|
};
|
|
|
|
const argument = (name: string) =>
|
|
process.argv.find((entry) => entry.startsWith(`--${name}=`))?.slice(name.length + 3);
|
|
const port = Number(process.env.LAB_PERFORMANCE_PORT ?? DEFAULT_LAB_PORT);
|
|
const runs = Number(argument("runs") ?? DEFAULT_LAB_RUNS);
|
|
if (!Number.isSafeInteger(port) || port <= 0 || !Number.isSafeInteger(runs) || runs < 1) {
|
|
throw new TypeError("LAB_PERFORMANCE_PORT must be a port and --runs a positive integer.");
|
|
}
|
|
const baseUrl = `http://127.0.0.1:${port}`;
|
|
// Template sync (af3d8e6). The named interaction is this product's first
|
|
// primary navigation entry: from the landing page to Explore. The previous
|
|
// sync measured the landing heading alone, which the template's page-clock
|
|
// interaction (click to target heading) cannot express.
|
|
const target = ROUTE_REGISTRY.TECH_LOG_EXPLORE;
|
|
const navigationLabel = target.navigationLabel;
|
|
if (!navigationLabel) {
|
|
throw new Error("Performance route must be present in navigation.");
|
|
}
|
|
// TechLog's route labels are typed `string | null`; the check above is what
|
|
// makes this one a string inside the hoisted measurement function too.
|
|
const targetLabel: string = navigationLabel;
|
|
|
|
// 11-07. A server already on the port used to be measured in place of this
|
|
// build: vite moved to another port and the gate never noticed.
|
|
await assertPortUnused(baseUrl);
|
|
// Its own process group: `corepack` starts pnpm, which starts vite, and a
|
|
// signal to `corepack` alone left vite serving the port after the gate ended.
|
|
const server = spawn("corepack", [...previewArguments(port)], {
|
|
stdio: "ignore",
|
|
detached: true,
|
|
});
|
|
let serverExit: string | null = null;
|
|
server.on("exit", (code, signal) => {
|
|
serverExit = `preview server exited (${signal ?? code})`;
|
|
});
|
|
|
|
async function waitForServer() {
|
|
for (let attempt = 0; attempt < 100; attempt += 1) {
|
|
if (serverExit) throw new Error(serverExit);
|
|
try {
|
|
const response = await fetch(baseUrl);
|
|
if (response.ok) return;
|
|
} catch {
|
|
// The bounded retry loop handles startup races.
|
|
}
|
|
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
}
|
|
throw new Error("Preview server did not become ready.");
|
|
}
|
|
|
|
type RunMetrics = { lcpMs: number; cls: number; namedInteractionMs: number; eventDurationMs: number | null };
|
|
|
|
async function measureOnce(browser: Browser): Promise<RunMetrics> {
|
|
// Every run is a cold, isolated context.
|
|
const context = await browser.newContext({
|
|
viewport: { width: 1280, height: 720 },
|
|
});
|
|
try {
|
|
const page = await context.newPage();
|
|
const cdp = await context.newCDPSession(page);
|
|
await cdp.send("Network.enable");
|
|
await cdp.send("Network.emulateNetworkConditions", {
|
|
offline: false,
|
|
latency: 40,
|
|
downloadThroughput: 200_000,
|
|
uploadThroughput: 93_750,
|
|
connectionType: "cellular4g",
|
|
});
|
|
await cdp.send("Emulation.setCPUThrottlingRate", { rate: 4 });
|
|
await page.addInitScript((title: string) => {
|
|
const evidence: ContractPerformanceEvidence = {
|
|
lcpMs: 0,
|
|
cls: 0,
|
|
clickAt: null,
|
|
headingAt: null,
|
|
eventDurationMs: null,
|
|
};
|
|
(window as ContractPerformanceWindow).__contractPerformance = evidence;
|
|
new PerformanceObserver((list) => {
|
|
for (const entry of list.getEntries()) evidence.lcpMs = entry.startTime;
|
|
}).observe({ type: "largest-contentful-paint", buffered: true });
|
|
new PerformanceObserver((list) => {
|
|
for (const entry of list.getEntries()) {
|
|
const layoutShift = entry as LayoutShiftEntry;
|
|
if (!layoutShift.hadRecentInput) {
|
|
evidence.cls += layoutShift.value;
|
|
}
|
|
}
|
|
}).observe({ type: "layout-shift", buffered: true });
|
|
// 11-07. The interaction is timed by the page's own clock, from the
|
|
// click event to the target heading entering the DOM, so Playwright's
|
|
// round trips are not in it. The Event Timing entry of the click (the
|
|
// INP input) is recorded next to it.
|
|
new PerformanceObserver((list) => {
|
|
for (const entry of list.getEntries()) {
|
|
if (entry.name === "click") {
|
|
evidence.eventDurationMs = Math.max(evidence.eventDurationMs ?? 0, entry.duration);
|
|
}
|
|
}
|
|
}).observe({ type: "event", buffered: true, durationThreshold: 16 } as PerformanceObserverInit);
|
|
document.addEventListener(
|
|
"click",
|
|
(event) => {
|
|
evidence.clickAt ??= event.timeStamp;
|
|
},
|
|
{ capture: true },
|
|
);
|
|
new MutationObserver((_records, observer) => {
|
|
if (evidence.clickAt === null) return;
|
|
const found = [...document.querySelectorAll("h1, h2")].some(
|
|
(heading) => heading.textContent?.trim() === title,
|
|
);
|
|
if (found) {
|
|
evidence.headingAt = performance.now();
|
|
observer.disconnect();
|
|
}
|
|
}).observe(document, { childList: true, subtree: true, characterData: true });
|
|
}, target.title);
|
|
await page.goto(baseUrl, { waitUntil: "networkidle" });
|
|
// Playwright matches accessible names by substring, so a navigation entry
|
|
// can also match a call to action that contains it; `exact` keeps the
|
|
// locator on the navigation link.
|
|
await page.getByRole("link", { name: targetLabel, exact: true }).click();
|
|
await page.getByRole("heading", { name: target.title }).waitFor();
|
|
await page.waitForFunction(
|
|
() => (window as ContractPerformanceWindow).__contractPerformance?.headingAt !== null,
|
|
);
|
|
const paint = await page.evaluate(
|
|
() => (window as ContractPerformanceWindow).__contractPerformance,
|
|
);
|
|
if (!paint || paint.clickAt === null || paint.headingAt === null) {
|
|
throw new Error("Browser performance evidence was not initialized.");
|
|
}
|
|
return {
|
|
lcpMs: Math.round(paint.lcpMs),
|
|
cls: Number(paint.cls.toFixed(4)),
|
|
namedInteractionMs: Math.round(paint.headingAt - paint.clickAt),
|
|
eventDurationMs:
|
|
paint.eventDurationMs === null ? null : Math.round(paint.eventDurationMs),
|
|
};
|
|
} finally {
|
|
await context.close();
|
|
}
|
|
}
|
|
|
|
const load = () => loadavg().map((value) => Math.round(value * 100) / 100);
|
|
|
|
try {
|
|
await waitForServer();
|
|
const release = JSON.parse(
|
|
await readFile("dist/release-manifest.json", "utf8"),
|
|
);
|
|
await assertServedRelease(baseUrl, release);
|
|
const thresholds = JSON.parse(
|
|
await readFile("config/performance/budgets.json", "utf8"),
|
|
).lab;
|
|
const browser = await chromium.launch();
|
|
try {
|
|
const loadBefore = load();
|
|
const samples: RunMetrics[] = [];
|
|
for (let run = 0; run < runs; run += 1) samples.push(await measureOnce(browser));
|
|
const loadAfter = load();
|
|
// The served build must not have changed under the runs either.
|
|
await assertServedRelease(baseUrl, release);
|
|
const lcp = summarizeSamples(samples.map((sample) => sample.lcpMs));
|
|
const cls = summarizeSamples(samples.map((sample) => sample.cls));
|
|
const interaction = summarizeSamples(
|
|
samples.map((sample) => sample.namedInteractionMs),
|
|
);
|
|
const eventDurations = samples.flatMap((sample) =>
|
|
sample.eventDurationMs === null ? [] : [sample.eventDurationMs],
|
|
);
|
|
const contextMetadata = {
|
|
runner: {
|
|
platform: process.platform,
|
|
architecture: process.arch,
|
|
nodeVersion: process.version,
|
|
cpuModel: cpus()[0]?.model.trim() ?? "unknown",
|
|
logicalCpus: cpus().length,
|
|
loadAverage: { before: loadBefore, after: loadAfter },
|
|
},
|
|
browser: { name: "chromium", version: await browser.version() },
|
|
viewport: { width: 1280, height: 720 },
|
|
network: {
|
|
profile: "contract-fast-4g",
|
|
latencyMs: 40,
|
|
downloadBytesPerSecond: 200_000,
|
|
uploadBytesPerSecond: 93_750,
|
|
},
|
|
cpu: { throttlingRate: 4 },
|
|
cache: { state: "cold", isolation: "new-browser-context" },
|
|
build: {
|
|
buildId: release.buildId,
|
|
releaseId: release.releaseId,
|
|
served: `${baseUrl}/release-manifest.json`,
|
|
},
|
|
// 11-07. The budget is judged on the median of cold runs; every run and
|
|
// the spread stay in the evidence.
|
|
sampling: {
|
|
runs,
|
|
statistic: "median",
|
|
interactionClock: "page: click event to target heading in DOM",
|
|
lcpMs: lcp,
|
|
cls,
|
|
namedInteractionMs: interaction,
|
|
clickEventDurationMs: eventDurations.length
|
|
? summarizeSamples(eventDurations)
|
|
: null,
|
|
},
|
|
};
|
|
const metrics = {
|
|
lcpMs: lcp.median,
|
|
cls: cls.median,
|
|
namedInteractionMs: interaction.median,
|
|
};
|
|
const result = evaluateLabBudget(
|
|
{ context: contextMetadata, metrics },
|
|
thresholds,
|
|
);
|
|
const fixtures = [
|
|
{
|
|
name: "missing-context",
|
|
passed: !evaluateLabBudget({ metrics }, thresholds).passed,
|
|
},
|
|
{
|
|
name: "lcp-over-threshold",
|
|
passed: !evaluateLabBudget(
|
|
{
|
|
context: contextMetadata,
|
|
metrics: { ...metrics, lcpMs: thresholds.lcpMs + 1 },
|
|
},
|
|
thresholds,
|
|
).passed,
|
|
},
|
|
];
|
|
const passed = result.passed && fixtures.every((fixture) => fixture.passed);
|
|
await mkdir("artifacts/performance", { recursive: true });
|
|
await writeValidatedJsonArtifact({
|
|
path: "artifacts/performance/lab.json",
|
|
schema: labPerformanceArtifactSchema,
|
|
value: {
|
|
schemaVersion: 1,
|
|
generatedAt: new Date().toISOString(),
|
|
context: contextMetadata,
|
|
metrics,
|
|
thresholds,
|
|
fixtures,
|
|
passed,
|
|
},
|
|
});
|
|
if (!passed) {
|
|
throw new Error(
|
|
`Lab performance failed (median of ${runs}): ${JSON.stringify(metrics)}; ` +
|
|
`interaction samples ${interaction.samples.join(",")}`,
|
|
);
|
|
}
|
|
process.stdout.write(
|
|
`Lab performance: PASS, median of ${runs} (LCP ${metrics.lcpMs}ms, CLS ${metrics.cls}, interaction ${metrics.namedInteractionMs}ms ` +
|
|
`[${interaction.min}-${interaction.max}])\n`,
|
|
);
|
|
} finally {
|
|
await browser.close();
|
|
}
|
|
} finally {
|
|
if (server.pid !== undefined && serverExit === null) {
|
|
process.kill(-server.pid, "SIGTERM");
|
|
}
|
|
}
|