Skip to content

Commit 60f6575

Browse files
committed
perf(ui): defer Settings English until needed
Keep optional Settings copy out of Control UI startup while preserving cold settings search, the Updates introduction, translation fallbacks, and shared startup/update guidance. Move 263 strings unchanged through the existing lazy catalog registration boundary. Unify complete source-catalog composition and source-hash dependencies for Vite, verification, and locale generation. Preserve source ordering and translation-memory aliases; no budget or baseline changes. Related: #121497 Separate startup-headroom follow-up to #134490.
1 parent 7fd30e6 commit 60f6575

18 files changed

Lines changed: 621 additions & 470 deletions

‎scripts/control-ui-i18n-verify.ts‎

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@ import path from "node:path";
55
import { fileURLToPath, pathToFileURL } from "node:url";
66
import * as ts from "typescript";
77
import {
8-
loadControlUiTranslationMemory,
9-
loadControlUiLocaleCatalog,
108
loadControlUiSourceCatalog,
9+
loadControlUiTranslationMemory,
1110
materializeControlUiLocaleCatalog,
1211
readControlUiSourceCatalog,
1312
} from "./lib/control-ui-i18n-catalog.ts";
1413
import { CONTROL_UI_LOCALE_ENTRIES } from "./lib/control-ui-i18n-config.ts";
1514
import { syncControlUiRawCopyBaseline } from "./lib/control-ui-i18n-raw-copy.ts";
16-
import type { TranslationMap } from "./lib/control-ui-i18n-sync-plan.ts";
1715
import { collectSourceFileContents } from "./lib/source-file-scan-cache.mts";
1816

1917
export type CatalogFallbackBaseline = {
@@ -23,12 +21,7 @@ export type CatalogFallbackBaseline = {
2321
};
2422

2523
const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
26-
const LOCALES_DIR = path.join(ROOT, "ui", "src", "i18n", "locales");
2724
const I18N_ASSETS_DIR = path.join(ROOT, "ui", "src", "i18n", ".i18n");
28-
const SOURCE_LOCALE_PATH = path.join(LOCALES_DIR, "en.ts");
29-
const ACTIVITY_SOURCE_LOCALE_PATH = path.join(LOCALES_DIR, "en-activity.ts");
30-
const SESSION_PLACEMENT_SOURCE_LOCALE_PATH = path.join(LOCALES_DIR, "en-session-placement.ts");
31-
const PLUGIN_CONSENT_SOURCE_LOCALE_PATH = path.join(LOCALES_DIR, "en-plugin-consent.ts");
3225
const FALLBACK_BASELINE_PATH = path.join(I18N_ASSETS_DIR, "catalog-fallbacks.json");
3326
const FALLBACK_BASELINE_VERSION = 1;
3427
const CONTROL_UI_TEST_FILE_PATTERN = /\.(?:test|browser\.test|node\.test)\.tsx?$/u;
@@ -48,24 +41,6 @@ export function formatControlUiCatalogFallbackDriftError(): string {
4841
].join("\n");
4942
}
5043

51-
async function loadSourceLocaleMap(): Promise<TranslationMap> {
52-
return await loadControlUiSourceCatalog(
53-
SOURCE_LOCALE_PATH,
54-
ACTIVITY_SOURCE_LOCALE_PATH,
55-
SESSION_PLACEMENT_SOURCE_LOCALE_PATH,
56-
PLUGIN_CONSENT_SOURCE_LOCALE_PATH,
57-
);
58-
}
59-
60-
async function readSourceLocaleRaw(): Promise<string> {
61-
return await readControlUiSourceCatalog(
62-
SOURCE_LOCALE_PATH,
63-
ACTIVITY_SOURCE_LOCALE_PATH,
64-
SESSION_PLACEMENT_SOURCE_LOCALE_PATH,
65-
PLUGIN_CONSENT_SOURCE_LOCALE_PATH,
66-
);
67-
}
68-
6944
function extractPlaceholders(text: string): string[] {
7045
return [...new Set([...text.matchAll(/\{(\w+)\}/g)].map((match) => match[1] ?? ""))]
7146
.filter(Boolean)
@@ -215,8 +190,8 @@ async function buildCatalogFallbackBaseline(
215190
allowCatalogDrift?: boolean;
216191
} = {},
217192
): Promise<CatalogFallbackBaseline> {
218-
const sourceRaw = await readSourceLocaleRaw();
219-
const sourceMap = await loadSourceLocaleMap();
193+
const sourceRaw = await readControlUiSourceCatalog();
194+
const sourceMap = loadControlUiSourceCatalog();
220195
const sourceFlat = flattenControlUiCatalog(sourceMap, "en");
221196
const localeFlats = new Map<string, Map<string, string>>();
222197
for (const entry of CONTROL_UI_LOCALE_ENTRIES) {
@@ -267,7 +242,7 @@ function printCatalogFallbackSummary(baseline: CatalogFallbackBaseline) {
267242
}
268243

269244
async function verifyControlUiSourceCatalogShape() {
270-
const sourceMap = await loadSourceLocaleMap();
245+
const sourceMap = loadControlUiSourceCatalog();
271246
const sourceFlat = flattenControlUiCatalog(sourceMap, "en");
272247
const sourceFiles = (
273248
await collectSourceFileContents({
@@ -317,7 +292,7 @@ export async function verifyRuntimeLocaleConfig() {
317292
);
318293
}
319294

320-
const enMap = (await loadControlUiLocaleCatalog(SOURCE_LOCALE_PATH, "en")) ?? {};
295+
const enMap = loadControlUiSourceCatalog();
321296
const languageMap = enMap.languages;
322297
const languageKeys =
323298
languageMap && typeof languageMap === "object"

‎scripts/control-ui-i18n.ts‎

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ import {
3434
type LocaleEntry,
3535
type LocaleMeta,
3636
type TranslationBatchItem,
37-
type TranslationMap,
3837
} from "./lib/control-ui-i18n-sync-plan.ts";
3938
import { toErrorObject as toLintErrorObject } from "./lib/error-format.mts";
4039
import { sleep } from "./lib/sleep.mjs";
@@ -51,12 +50,7 @@ const DEFAULT_ANTHROPIC_MODEL = "claude-opus-4-6";
5150
const DEFAULT_PROVIDER = "openai";
5251
const HERE = path.dirname(fileURLToPath(import.meta.url));
5352
const ROOT = path.resolve(HERE, "..");
54-
const LOCALES_DIR = path.join(ROOT, "ui", "src", "i18n", "locales");
5553
const I18N_ASSETS_DIR = path.join(ROOT, "ui", "src", "i18n", ".i18n");
56-
const SOURCE_LOCALE_PATH = path.join(LOCALES_DIR, "en.ts");
57-
const ACTIVITY_SOURCE_LOCALE_PATH = path.join(LOCALES_DIR, "en-activity.ts");
58-
const SESSION_PLACEMENT_SOURCE_LOCALE_PATH = path.join(LOCALES_DIR, "en-session-placement.ts");
59-
const PLUGIN_CONSENT_SOURCE_LOCALE_PATH = path.join(LOCALES_DIR, "en-plugin-consent.ts");
6054
const SOURCE_LOCALE = "en";
6155
const MAX_BATCH_ITEMS = 20;
6256
const DEFAULT_BATCH_CHAR_BUDGET = 2_000;
@@ -282,24 +276,6 @@ function tmPath(entry: LocaleEntry): string {
282276
return path.join(I18N_ASSETS_DIR, `${entry.locale}.tm.jsonl`);
283277
}
284278

285-
async function loadSourceLocaleMap(): Promise<TranslationMap> {
286-
return await loadControlUiSourceCatalog(
287-
SOURCE_LOCALE_PATH,
288-
ACTIVITY_SOURCE_LOCALE_PATH,
289-
SESSION_PLACEMENT_SOURCE_LOCALE_PATH,
290-
PLUGIN_CONSENT_SOURCE_LOCALE_PATH,
291-
);
292-
}
293-
294-
async function readSourceLocaleRaw(): Promise<string> {
295-
return await readControlUiSourceCatalog(
296-
SOURCE_LOCALE_PATH,
297-
ACTIVITY_SOURCE_LOCALE_PATH,
298-
SESSION_PLACEMENT_SOURCE_LOCALE_PATH,
299-
PLUGIN_CONSENT_SOURCE_LOCALE_PATH,
300-
);
301-
}
302-
303279
type PlaceholderMismatch = {
304280
key: string;
305281
locale: string;
@@ -1100,9 +1076,9 @@ async function syncLocale(
11001076
) {
11011077
const localeLabel = formatLocaleLabel(entry.locale, context);
11021078
const localeStartedAt = Date.now();
1103-
const sourceRaw = await readSourceLocaleRaw();
1079+
const sourceRaw = await readControlUiSourceCatalog();
11041080
const sourceHash = sha256(sourceRaw);
1105-
const sourceMap = await loadSourceLocaleMap();
1081+
const sourceMap = loadControlUiSourceCatalog();
11061082
const sourceFlat = flattenTranslations(sourceMap);
11071083
const tm = loadControlUiTranslationMemory(tmPath(entry));
11081084
const existingMap = materializeControlUiLocaleCatalog(sourceFlat, tm);

‎scripts/lib/control-ui-i18n-catalog.ts‎

Lines changed: 32 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,45 @@
11
import { createHash } from "node:crypto";
22
import { existsSync, readFileSync } from "node:fs";
3-
import { readFile, stat } from "node:fs/promises";
4-
import { pathToFileURL } from "node:url";
3+
import { readFile } from "node:fs/promises";
4+
import path from "node:path";
5+
import { fileURLToPath } from "node:url";
6+
import { registerActivityEnglish } from "../../ui/src/i18n/locales/en-activity.ts";
7+
import { registerPluginConsentEnglish } from "../../ui/src/i18n/locales/en-plugin-consent.ts";
8+
import { registerSessionPlacementEnglish } from "../../ui/src/i18n/locales/en-session-placement.ts";
9+
import { registerSettingsEnglish } from "../../ui/src/i18n/locales/en-settings.ts";
10+
import { en } from "../../ui/src/i18n/locales/en.ts";
511
import type { TranslationMap, TranslationMemoryEntry } from "./control-ui-i18n-sync-plan.ts";
612

7-
async function importControlUiLocaleModule<T>(filePath: string): Promise<T> {
8-
const stats = await stat(filePath);
9-
return (await import(`${pathToFileURL(filePath).href}?ts=${stats.mtimeMs}`)) as T;
10-
}
13+
// Host-only owner for generation, verification and Vite. Static imports let Vite
14+
// track every English dependency (including en.ts's en-agents.ts import).
15+
const localesDir = path.resolve(
16+
path.dirname(fileURLToPath(import.meta.url)),
17+
"../../ui/src/i18n/locales",
18+
);
19+
const sourceFiles = [
20+
"en.ts",
21+
"en-agents.ts",
22+
"en-activity.ts",
23+
"en-session-placement.ts",
24+
"en-plugin-consent.ts",
25+
"en-settings.ts",
26+
];
1127

12-
export async function loadControlUiLocaleCatalog(
13-
filePath: string,
14-
exportName: string,
15-
): Promise<TranslationMap | null> {
16-
if (!existsSync(filePath)) {
17-
return null;
18-
}
19-
const module = await importControlUiLocaleModule<Record<string, TranslationMap>>(filePath);
20-
return module[exportName] ?? null;
21-
}
22-
23-
export async function loadControlUiSourceCatalog(
24-
sourceLocalePath: string,
25-
activitySourceLocalePath: string,
26-
sessionPlacementSourceLocalePath: string,
27-
pluginConsentSourceLocalePath: string,
28-
): Promise<TranslationMap> {
29-
const source = await loadControlUiLocaleCatalog(sourceLocalePath, "en");
30-
const activitySource = (
31-
await importControlUiLocaleModule<{
32-
registerActivityEnglish: { catalog: TranslationMap };
33-
}>(activitySourceLocalePath)
34-
).registerActivityEnglish.catalog;
35-
const sessionPlacementSource = (
36-
await importControlUiLocaleModule<{
37-
registerSessionPlacementEnglish: { catalog: TranslationMap };
38-
}>(sessionPlacementSourceLocalePath)
39-
).registerSessionPlacementEnglish.catalog;
40-
const pluginConsentSource = (
41-
await importControlUiLocaleModule<{
42-
registerPluginConsentEnglish: { catalog: TranslationMap };
43-
}>(pluginConsentSourceLocalePath)
44-
).registerPluginConsentEnglish.catalog;
45-
if (!source || !activitySource || !sessionPlacementSource || !pluginConsentSource) {
46-
throw new Error("Control UI English source catalogs are incomplete");
47-
}
28+
export function loadControlUiSourceCatalog(): TranslationMap {
29+
// Read fragment data without registering it into the shared runtime catalog.
30+
// en.ts's empty anchors retain source order for extracted whole subtrees.
4831
return mergeControlUiTranslationMaps(
49-
source,
50-
activitySource,
51-
sessionPlacementSource,
52-
pluginConsentSource,
32+
en,
33+
registerActivityEnglish.catalog,
34+
registerSessionPlacementEnglish.catalog,
35+
registerPluginConsentEnglish.catalog,
36+
registerSettingsEnglish.catalog,
5337
);
5438
}
5539

56-
export async function readControlUiSourceCatalog(
57-
sourceLocalePath: string,
58-
activitySourceLocalePath: string,
59-
sessionPlacementSourceLocalePath: string,
60-
pluginConsentSourceLocalePath: string,
61-
): Promise<string> {
40+
export async function readControlUiSourceCatalog(): Promise<string> {
6241
const sources = await Promise.all(
63-
[
64-
sourceLocalePath,
65-
activitySourceLocalePath,
66-
sessionPlacementSourceLocalePath,
67-
pluginConsentSourceLocalePath,
68-
].map((filePath) => readFile(filePath, "utf8")),
42+
sourceFiles.map((fileName) => readFile(path.join(localesDir, fileName), "utf8")),
6943
);
7044
return sources.join("\n");
7145
}

‎test/scripts/control-ui-i18n-sync-plan.test.ts‎

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,26 @@ function localeMeta(overrides: Partial<LocaleMeta> = {}): LocaleMeta {
5656
}
5757

5858
describe("createControlUiLocaleSyncPlan", () => {
59-
it("merges lazy English source catalogs without losing sibling keys", () => {
60-
expect(
61-
mergeControlUiTranslationMaps(
62-
{ activity: { title: "Activity" }, common: { ok: "OK" } },
63-
{ activity: { runInspector: { title: "Run inspector" } } },
64-
),
65-
).toEqual({
66-
activity: { title: "Activity", runInspector: { title: "Run inspector" } },
59+
it("fills lazy anchors in source order without mutating source or losing siblings", () => {
60+
const startup = {
61+
updates: { before: "Before", page: {}, after: "After" },
62+
settings: {},
6763
common: { ok: "OK" },
68-
});
64+
};
65+
const fragment = { settings: { title: "Settings" }, updates: { page: { title: "Updates" } } };
66+
const merged = mergeControlUiTranslationMaps(startup, fragment);
67+
68+
expect([...flattenTranslations(merged)]).toEqual([
69+
["updates.before", "Before"],
70+
["updates.page.title", "Updates"],
71+
["updates.after", "After"],
72+
["settings.title", "Settings"],
73+
["common.ok", "OK"],
74+
]);
75+
expect(startup.settings).toEqual({});
76+
expect(startup.updates.page).toEqual({});
77+
expect(merged.updates).not.toBe(startup.updates);
78+
expect(merged.settings).not.toBe(fragment.settings);
6979
});
7080

7181
it("preserves provenance when a configured provider performs no translation", () => {

‎ui/AGENTS.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@ This directory owns Control UI-specific guidance that should not live in the rep
66

77
- Foreign-language files in `ui/src/i18n/locales/*.ts` are stable, source-owned lazy-module adapters; their translations are generated from canonical grouped memory in `ui/src/i18n/.i18n/*.tm.jsonl`.
88
- Do not hand-edit translation memory, locale metadata, or fallback metadata unless a targeted generated-output fix is explicitly requested.
9-
- The source of truth is `ui/src/i18n/locales/en.ts` and `en-agents.ts` plus the generator/runtime wiring in:
9+
- English source lives in `ui/src/i18n/locales/en.ts`, its static `en-agents.ts` dependency, and lazy `en-*.ts` registrar catalogs. `scripts/lib/control-ui-i18n-catalog.ts` owns complete ordered composition and raw source-hash dependencies for generation, verification, and Vite; it reads `.catalog` data without runtime registration. Related wiring:
1010
- `scripts/control-ui-i18n.ts`
1111
- `scripts/lib/control-ui-i18n-catalog.ts`
1212
- `scripts/lib/control-ui-i18n-sync-plan.ts`
1313
- `ui/config/control-ui-locales.ts`
1414
- `ui/src/i18n/lib/types.ts`
1515
- `ui/src/i18n/lib/registry.ts`
16+
- Register lazy English synchronously at each lazy consumer, including Settings search before a destination page loads. Keep startup/shared copy eager. Preserve the shared `en` object and sibling namespaces; leave empty whole-subtree anchors in `en.ts` when extraction would change flattened source order and grouped translation-memory aliases. Never import the host-only catalog owner into the runtime.
1617
- Contributor flow: update English strings and locale adapters/wiring, run keyless `pnpm ui:i18n:baseline`, and commit source files plus any changed raw-copy baseline. Do not include catalog fallback metadata, locale metadata, or translation memory in a source PR; CI rejects mixed source/generated diffs outside canonical `release/YYYY.M.PATCH` branches or an explicitly detected complete canonical-memory ownership migration.
1718
- `pnpm ui:i18n:verify` is deterministic and keyless. `pnpm lint` and the changed-check UI lane run it. It validates English catalog shape, runtime locale wiring, and raw-copy baseline drift; foreign catalog parity belongs to the post-merge bot and strict generated-output gate.
1819
- Translation flow: the serialized `control-ui-locale-refresh` workflow translates after merge, opens an isolated generated PR, and enables auto-merge for its exact head. `pnpm ui:i18n:sync` remains the authenticated maintainer/release repair path; do not run it without provider auth when new keys exist.

‎ui/config/control-ui-locales.ts‎

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,12 @@ import path from "node:path";
33
import { fileURLToPath } from "node:url";
44
import type { Plugin } from "vite";
55
import {
6+
loadControlUiSourceCatalog,
67
loadControlUiTranslationMemory,
78
materializeControlUiLocaleCatalog,
8-
mergeControlUiTranslationMaps,
99
} from "../../scripts/lib/control-ui-i18n-catalog.ts";
1010
import { CONTROL_UI_LOCALE_ENTRIES } from "../../scripts/lib/control-ui-i18n-config.ts";
1111
import { flattenTranslations } from "../../scripts/lib/control-ui-i18n-sync-plan.ts";
12-
import { registerActivityEnglish } from "../src/i18n/locales/en-activity.ts";
13-
import { registerPluginConsentEnglish } from "../src/i18n/locales/en-plugin-consent.ts";
14-
import { registerSessionPlacementEnglish } from "../src/i18n/locales/en-session-placement.ts";
15-
import { en } from "../src/i18n/locales/en.ts";
1612

1713
const localeModulePrefix = "virtual:openclaw-control-ui-locale/";
1814
const resolvedLocaleModulePrefix = `\0${localeModulePrefix}`;
@@ -22,12 +18,7 @@ const i18nAssetsDir = path.resolve(
2218
"../src/i18n/.i18n",
2319
);
2420
const locales = new Set(CONTROL_UI_LOCALE_ENTRIES.map(({ locale }) => locale));
25-
const sourceCatalog = mergeControlUiTranslationMaps(
26-
en,
27-
registerActivityEnglish.catalog,
28-
registerSessionPlacementEnglish.catalog,
29-
registerPluginConsentEnglish.catalog,
30-
);
21+
const sourceCatalog = loadControlUiSourceCatalog();
3122

3223
export function controlUiLocaleModulesPlugin(): Plugin {
3324
return {

0 commit comments

Comments
 (0)