Skip to content

Commit 8fe2c1b

Browse files
authored
perf(ui): consolidate Control UI boot chunk graph for HTTP/1.1 gateways (#128514)
* perf(ui): consolidate Control UI boot chunk graph for HTTP/1.1 gateways The Control UI boot flow (app shell + sidebar + chat route) lazily loaded ~124 automatic chunks in one burst after the gateway handshake, which the gateway's HTTP/1.1 transport serializes into ~24 six-connection round-trips on high-latency links (Tailscale, remote gateways). Add a measured boot-module manifest (ui/config/control-ui-boot-modules.json, regenerated via pnpm ui:boot-manifest:gen) and a control-ui-boot codeSplitting group that merges exactly that module set into a handful of chunks with recursive dependency inclusion. Lazy islands (locales, ghostty-web, novnc, non-default routes) keep their own chunks; stale manifest entries degrade gracefully back to automatic chunking. Measured on the built dist with the mocked gateway (chat route, 3 runs): unique boot JS requests 140 -> 45, raw boot JS 3751 -> 3717 KiB, chat composer interactive at simulated 50 ms RTT ~1600 ms -> ~575 ms. Largest-CSS budget rises 45 -> 47 KiB for the merged boot CSS; startup JS gzip baseline ratchets down (345049 -> 339214 B) as consolidation shrinks the startup graph. * chore(ui): refresh boot module manifest after rebase onto current main * fix(ui): stop the pending lazy shell action replay loop starving boot When a pending lazy shell action (command palette open, panel toggle) replayed while the shell was still splash-gated, the dispatched event had no rendered element to consume it and re-entered requestLazyElement in a microtask cycle: request -> load -> replay -> dispatch -> request. The cycle starved tasks (Gateway WebSocket messages included), so the boot never finished and the recovery e2e froze on the splash screen. Gate replay on the element actually being rendered: the controller skips the action after load until the host's render root contains the tag, and restorePendingLazyAction skips dispatch while a defined element is still render-gated. The host retries after every completed update, so the replay fires on the update that first renders the element. Regression test fails on the pre-fix controller. * fix(ui): re-anchor the scope-upgrade details popover before opening wa-popover resolves its `for` target once per property change and never re-resolves a missing or replaced anchor. The trigger with the shared id can render after the popover's first update (the header trigger ships with the lazy chat chunk), leaving the opened popover permanently invisible: active popup with a native [popover] part stuck at UA display:none because showPopover() never ran without an anchor. Re-arm the watcher when opening while the anchor is missing or disconnected. * test(ui): compare settled layouts in device-scope stability assertions The 0.5px no-move assertions sampled geometry that later reflowed when the details surface's first render fetched glyph subsets, reporting sub-pixel drift the open never caused. Burn in the one-time open per context and sample the baseline adjacent to the click. * fix(ui): map the keyboard shortcuts dialog in lazy replay gating Current main added the keyboard-shortcuts lazy shell event; the replay gate's exhaustive event-to-element record needs its entry. * chore(ui): refresh startup budget baseline after rebase onto current main
1 parent d9165f9 commit 8fe2c1b

18 files changed

Lines changed: 2357 additions & 21 deletions
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"startupJsGzipBytes": 345049,
3-
"reason": "streamed-markdown highlighting changes (#127749, #127754) consumed the ratchet",
4-
"updatedAt": "2026-08-22"
2+
"startupJsGzipBytes": 340388,
3+
"reason": "rebase onto current main; startup growth landed on main since the boot-group ratchet",
4+
"updatedAt": "2026-08-24"
55
}

‎package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2004,6 +2004,7 @@
20042004
"tui:pty:test:watch:all": "node --import tsx scripts/dev/tui-pty-test-watch.ts --mode all",
20052005
"tui:pty:test:watch:fake": "node --import tsx scripts/dev/tui-pty-test-watch.ts --mode fake",
20062006
"tui:pty:test:watch:local": "node --import tsx scripts/dev/tui-pty-test-watch.ts --mode local",
2007+
"ui:boot-manifest:gen": "node --import tsx scripts/control-ui-boot-manifest.mts",
20072008
"ui:build": "node scripts/ui.js build",
20082009
"ui:dev": "node scripts/ui.js dev",
20092010
"ui:i18n:baseline": "node --import tsx scripts/control-ui-i18n-verify.ts baseline",

‎scripts/check-control-ui-performance.mts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@ const controlUiPerformanceBudgets = {
3333
// sidebar zone styling; headroom over the ~36.5 KiB post-diet baseline.
3434
startupCssGzipBytes: 45 * KIB,
3535
largestJsGzipBytes: 215 * KIB,
36-
largestCssGzipBytes: 45 * KIB,
36+
// Startup CSS stays at 45 KiB; the boot-group consolidation (2026-08,
37+
// control-ui-boot chunking) merges boot-path component CSS into one file
38+
// that lands just above it, trading ~1 KiB of ceiling for ~95 fewer boot
39+
// requests on HTTP/1.1 gateways.
40+
largestCssGzipBytes: 47 * KIB,
3741
} satisfies Record<string, number>;
3842
export const CONTROL_UI_PERFORMANCE_BUDGETS = Object.freeze(controlUiPerformanceBudgets);
3943

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
#!/usr/bin/env -S node --import tsx
2+
// 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.
7+
import fs from "node:fs";
8+
import http from "node:http";
9+
import path from "node:path";
10+
import { fileURLToPath } from "node:url";
11+
import { chromium } from "playwright";
12+
import { controlUiBootManifestKey } from "../ui/config/control-ui-chunking.ts";
13+
import { installMockGateway } from "../ui/src/test-helpers/control-ui-e2e.ts";
14+
15+
const repoRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
16+
const distDir = path.join(repoRoot, "dist", "control-ui");
17+
const manifestPath = path.join(repoRoot, "ui", "config", "control-ui-boot-modules.json");
18+
const SETTLE_MS = 3_000;
19+
const READY_TIMEOUT_MS = 60_000;
20+
21+
const mime: Record<string, string> = {
22+
".html": "text/html",
23+
".js": "text/javascript",
24+
".css": "text/css",
25+
".json": "application/json",
26+
".svg": "image/svg+xml",
27+
".map": "application/json",
28+
".webmanifest": "application/manifest+json",
29+
};
30+
31+
function serveDist(): Promise<{ baseUrl: string; close: () => void }> {
32+
const server = http.createServer((req, res) => {
33+
const urlPath = new URL(req.url ?? "/", "http://localhost").pathname;
34+
if (urlPath === "/control-ui-config.json") {
35+
res.setHeader("Content-Type", "application/json");
36+
res.end(JSON.stringify({ basePath: "/", assistantName: "", assistantAvatar: "" }));
37+
return;
38+
}
39+
let filePath = path.join(distDir, urlPath === "/" ? "index.html" : urlPath.slice(1));
40+
if (!fs.existsSync(filePath) || fs.statSync(filePath).isDirectory()) {
41+
filePath = path.join(distDir, "index.html");
42+
}
43+
res.setHeader("Content-Type", mime[path.extname(filePath)] ?? "application/octet-stream");
44+
res.end(fs.readFileSync(filePath));
45+
});
46+
return new Promise((resolve) => {
47+
server.listen(0, "127.0.0.1", () => {
48+
const address = server.address();
49+
if (address === null || typeof address !== "object") {
50+
throw new Error("Control UI boot manifest server has no port");
51+
}
52+
resolve({
53+
baseUrl: `http://127.0.0.1:${address.port}`,
54+
close: () => server.close(),
55+
});
56+
});
57+
});
58+
}
59+
60+
function readDistBuildId(): string {
61+
const swSource = fs.readFileSync(path.join(distDir, "sw.js"), "utf8");
62+
const buildId = /EMBEDDED_CACHE_VERSION = "([^"]+)"/.exec(swSource)?.[1];
63+
if (!buildId) {
64+
throw new Error("Control UI boot manifest cannot read the dist build id from sw.js");
65+
}
66+
return buildId;
67+
}
68+
69+
async function collectBootChunkPaths(baseUrl: string): Promise<Set<string>> {
70+
const browser = await chromium.launch();
71+
try {
72+
const page = await browser.newPage();
73+
const chunkPaths = new Set<string>();
74+
page.on("request", (request) => {
75+
const { pathname } = new URL(request.url());
76+
if (pathname.startsWith("/assets/") && pathname.endsWith(".js")) {
77+
chunkPaths.add(pathname);
78+
}
79+
});
80+
await installMockGateway(page, { serverBuildId: readDistBuildId() });
81+
await page.goto(`${baseUrl}/chat`, { waitUntil: "commit" });
82+
// Chat readiness proves the boot flow completed instead of stalling on an
83+
// error surface; a manifest captured from a broken boot would be garbage.
84+
await page
85+
.locator(".agent-chat__composer-combobox textarea")
86+
.waitFor({ timeout: READY_TIMEOUT_MS });
87+
await page.waitForTimeout(SETTLE_MS);
88+
return chunkPaths;
89+
} finally {
90+
await browser.close();
91+
}
92+
}
93+
94+
function manifestKeysForChunks(chunkPaths: Iterable<string>): string[] {
95+
const keys = new Set<string>();
96+
for (const chunkPath of chunkPaths) {
97+
const mapPath = path.join(distDir, `${chunkPath}.map`);
98+
if (!fs.existsSync(mapPath)) {
99+
// Facade chunks for dynamic entries can omit maps; their modules are
100+
// covered by the chunks that carry the actual code.
101+
continue;
102+
}
103+
const map = JSON.parse(fs.readFileSync(mapPath, "utf8")) as { sources?: string[] };
104+
for (const source of map.sources ?? []) {
105+
keys.add(controlUiBootManifestKey(path.resolve(path.join(distDir, "assets"), source)));
106+
}
107+
}
108+
return [...keys].toSorted();
109+
}
110+
111+
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();
116+
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`);
121+
}
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+
);
126+
} finally {
127+
server.close();
128+
}
129+
}
130+
131+
main().catch((error: unknown) => {
132+
console.error(error);
133+
console.error("[control-ui-boot-manifest] FAILED (exit 1)");
134+
process.exit(1);
135+
});

‎ui/AGENTS.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ This directory owns Control UI-specific guidance that should not live in the rep
4242
- Method-advertisement checks (`isGatewayMethodAdvertised`) remain only as feature gates for config/plugin-dependent surfaces, never as version compat.
4343
- The handshake rejects gateway-served same-origin skew. The admission-exempt paths (`pnpm ui:dev`, custom `gateway.controlUi.root`, cross-origin/connection-settings dialing) are unsupported for version mismatch without enforcement: they carry no compat code and fail visibly at the first missing method, by design. Tightening admission to reject them at connect is a server-side product change owned separately.
4444

45+
## Build Chunking
46+
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.
48+
4549
## Live Verification
4650

4751
- The Gateway serves the prebuilt bundle from `dist/control-ui`; editing `ui/src` changes nothing live until `pnpm ui:build`. Confirm the served `/assets/index-*.js` hash changed before trusting a live result.

0 commit comments

Comments
 (0)