Skip to content

Commit 06d283e

Browse files
committed
fix(ui): measure boot manifest without stale grouping
1 parent 7d48fe2 commit 06d283e

4 files changed

Lines changed: 107 additions & 57 deletions

File tree

‎scripts/control-ui-boot-manifest.mts‎

Lines changed: 58 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
#!/usr/bin/env -S node --import tsx
22
// Regenerates ui/config/control-ui-boot-modules.json: the measured module set
3-
// the default Control UI boot flow loads lazily. Boots the built dist bundle
4-
// against the mocked Gateway, records every JS chunk fetched through chat
5-
// readiness, and unions their sourcemap sources into canonical manifest keys.
6-
// Requires a current `pnpm ui:build` output in dist/control-ui.
3+
// the default Control UI boot flow loads lazily. Builds without the previous
4+
// boot group, then captures Chat-ready chunk sources against the mocked Gateway.
75
import fs from "node:fs";
86
import http from "node:http";
7+
import os from "node:os";
98
import path from "node:path";
109
import { fileURLToPath } from "node:url";
1110
import { chromium } from "playwright";
12-
import { controlUiBootManifestKey } from "../ui/config/control-ui-chunking.ts";
11+
import { build } from "vite";
12+
import {
13+
controlUiBootManifestKey,
14+
controlUiCodeSplitting,
15+
} from "../ui/config/control-ui-chunking.ts";
1316
import { installMockGateway } from "../ui/src/test-helpers/control-ui-e2e.ts";
17+
import controlUiViteConfig from "../ui/vite.config.ts";
1418

1519
const repoRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
16-
const distDir = path.join(repoRoot, "dist", "control-ui");
1720
const manifestPath = path.join(repoRoot, "ui", "config", "control-ui-boot-modules.json");
1821
const SETTLE_MS = 3_000;
1922
const READY_TIMEOUT_MS = 60_000;
@@ -28,7 +31,7 @@ const mime: Record<string, string> = {
2831
".webmanifest": "application/manifest+json",
2932
};
3033

31-
function serveDist(): Promise<{ baseUrl: string; close: () => void }> {
34+
function serveDist(distDir: string): Promise<{ baseUrl: string; close: () => Promise<void> }> {
3235
const server = http.createServer((req, res) => {
3336
const urlPath = new URL(req.url ?? "/", "http://localhost").pathname;
3437
if (urlPath === "/control-ui-config.json") {
@@ -51,13 +54,16 @@ function serveDist(): Promise<{ baseUrl: string; close: () => void }> {
5154
}
5255
resolve({
5356
baseUrl: `http://127.0.0.1:${address.port}`,
54-
close: () => server.close(),
57+
close: () =>
58+
new Promise((resolveClose, rejectClose) => {
59+
server.close((error) => (error ? rejectClose(error) : resolveClose()));
60+
}),
5561
});
5662
});
5763
});
5864
}
5965

60-
function readDistBuildId(): string {
66+
function readDistBuildId(distDir: string): string {
6167
const swSource = fs.readFileSync(path.join(distDir, "sw.js"), "utf8");
6268
const buildId = /EMBEDDED_CACHE_VERSION = "([^"]+)"/.exec(swSource)?.[1];
6369
if (!buildId) {
@@ -66,7 +72,7 @@ function readDistBuildId(): string {
6672
return buildId;
6773
}
6874

69-
async function collectBootChunkPaths(baseUrl: string): Promise<Set<string>> {
75+
async function collectBootChunkPaths(baseUrl: string, distDir: string): Promise<Set<string>> {
7076
const browser = await chromium.launch();
7177
try {
7278
const page = await browser.newPage();
@@ -77,7 +83,7 @@ async function collectBootChunkPaths(baseUrl: string): Promise<Set<string>> {
7783
chunkPaths.add(pathname);
7884
}
7985
});
80-
await installMockGateway(page, { serverBuildId: readDistBuildId() });
86+
await installMockGateway(page, { serverBuildId: readDistBuildId(distDir) });
8187
await page.goto(`${baseUrl}/chat`, { waitUntil: "commit" });
8288
// Chat readiness proves the boot flow completed instead of stalling on an
8389
// error surface; a manifest captured from a broken boot would be garbage.
@@ -91,7 +97,7 @@ async function collectBootChunkPaths(baseUrl: string): Promise<Set<string>> {
9197
}
9298
}
9399

94-
function manifestKeysForChunks(chunkPaths: Iterable<string>): string[] {
100+
function manifestKeysForChunks(chunkPaths: Iterable<string>, distDir: string): string[] {
95101
const keys = new Set<string>();
96102
for (const chunkPath of chunkPaths) {
97103
const mapPath = path.join(distDir, `${chunkPath}.map`);
@@ -109,22 +115,49 @@ function manifestKeysForChunks(chunkPaths: Iterable<string>): string[] {
109115
}
110116

111117
async function main(): Promise<void> {
112-
if (!fs.existsSync(path.join(distDir, "index.html"))) {
113-
throw new Error(`No Control UI build at ${distDir}; run \`pnpm ui:build\` first`);
114-
}
115-
const server = await serveDist();
118+
const distDir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-control-ui-boot-"));
116119
try {
117-
const chunkPaths = await collectBootChunkPaths(server.baseUrl);
118-
const keys = manifestKeysForChunks(chunkPaths);
119-
if (keys.length < 100) {
120-
throw new Error(`Boot capture looks truncated: only ${keys.length} modules recorded`);
120+
const config = controlUiViteConfig({ outDir: distDir });
121+
await build({
122+
...config,
123+
configFile: false,
124+
root: path.join(repoRoot, "ui"),
125+
plugins: [
126+
config.plugins,
127+
{
128+
name: "control-ui-measure-boot-dependencies",
129+
outputOptions(options) {
130+
// The old boot group would keep stale modules in fetched chunks,
131+
// feeding them back into every regenerated manifest.
132+
return {
133+
...options,
134+
codeSplitting: {
135+
...controlUiCodeSplitting,
136+
groups: controlUiCodeSplitting.groups.filter(
137+
(group) => group.name !== "control-ui-boot",
138+
),
139+
},
140+
};
141+
},
142+
},
143+
],
144+
});
145+
const server = await serveDist(distDir);
146+
try {
147+
const chunkPaths = await collectBootChunkPaths(server.baseUrl, distDir);
148+
const keys = manifestKeysForChunks(chunkPaths, distDir);
149+
if (keys.length < 100) {
150+
throw new Error(`Boot capture looks truncated: only ${keys.length} modules recorded`);
151+
}
152+
fs.writeFileSync(manifestPath, `${JSON.stringify(keys, null, 1)}\n`);
153+
console.log(
154+
`control-ui-boot-manifest: ${chunkPaths.size} boot chunks -> ${keys.length} modules -> ${path.relative(repoRoot, manifestPath)}`,
155+
);
156+
} finally {
157+
await server.close();
121158
}
122-
fs.writeFileSync(manifestPath, `${JSON.stringify(keys, null, 1)}\n`);
123-
console.log(
124-
`control-ui-boot-manifest: ${chunkPaths.size} boot chunks -> ${keys.length} modules -> ${path.relative(repoRoot, manifestPath)}`,
125-
);
126159
} finally {
127-
server.close();
160+
fs.rmSync(distDir, { recursive: true, force: true });
128161
}
129162
}
130163

‎ui/AGENTS.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ This directory owns Control UI-specific guidance that should not live in the rep
4444

4545
## Build Chunking
4646

47-
- `ui/config/control-ui-boot-modules.json` is a generated manifest of the modules the default boot flow loads lazily; the `control-ui-boot` group in `ui/config/control-ui-chunking.ts` merges them into a few chunks so boot avoids ~140 HTTP/1.1 requests. Regenerate with `pnpm ui:boot-manifest:gen` after `pnpm ui:build` when boot-path surfaces change materially; stale entries degrade to extra chunks, never breakage. Do not hand-edit the manifest.
47+
- `ui/config/control-ui-boot-modules.json` is a generated manifest of the modules the default boot flow loads lazily; the `control-ui-boot` group in `ui/config/control-ui-chunking.ts` merges them into a few chunks so boot avoids ~140 HTTP/1.1 requests. Regenerate with `pnpm ui:boot-manifest:gen` when boot-path surfaces change materially; it builds into a temporary directory with only the old boot group disabled, preventing stale entries from feeding back into the capture. Rebuild with `pnpm ui:build` afterward to verify normal grouped output. Do not hand-edit the manifest.
4848

4949
## Live Verification
5050

0 commit comments

Comments
 (0)