openclaw/scripts/lib/cli-startup-root-help-bundle.ts
Peter Steinberger 8b37368a4e
perf: reduce packaged runtime module-loading overhead (#139754)
* build: emit internal runtime chunks as explicit ESM

Avoid repeated Node package-format metadata parsing for internal chunks while preserving named JavaScript entrypoints, public SDK paths, stable runtime aliases, and historical upgrade aliases.

Validation: 242 focused tests, normal build, built status-runtime loading, actual package inventory/import-closure checks, and independent Codex P0 review passed. All typechecks passed. Local aggregate lint was blocked by an ancestor-checkout punycode manifest lookup; neutral Linux validation and original retention stress acceptance remain pending. Related: #136639.

* build: preserve private QA exclusions across chunk formats

Match the existing inventory-owned private QA namespace when packing reused build outputs. Add real npm-pack regression coverage and remove ignored promise-executor return values from startup metadata tests without changing their timing or cleanup behavior.

The new package regression failed before the exclusion repair and passed afterward. Package E2E: 63 passed, 3 Windows-only skipped. Startup metadata: 22 passed. Focused lint and independent Codex P0 review passed. Related: #136639.

* test: preserve user loader proof for ESM chunks

* test: recognize ESM chunks in remaining runtime fixtures

* test(docker): complete package lifecycle before read-only proof

* test(docker): consolidate the packaged-image lifecycle contract
2026-09-06 01:31:24 -07:00

47 lines
1.7 KiB
TypeScript

// Resolves the generated root-help bundle identity for CLI startup metadata caching.
import { createHash } from "node:crypto";
import { readdirSync, readFileSync } from "node:fs";
import path from "node:path";
const OUTPUT_ROOT_HELP_EXPORT_RE =
/\bexport\s+(?:(?:async\s+)?function|class|const|let|var)\s+outputRootHelp\b|\bexport\s*\{[^}]*\boutputRootHelp\s*(?=[,}])/u;
export function resolveCliStartupRootHelpBundleIdentity(
distDir: string,
): { bundleName: string; signature: string } | null {
for (const bundleName of readdirSync(distDir).toSorted()) {
if (!bundleName.startsWith("root-help-") || !/\.m?js$/u.test(bundleName)) {
continue;
}
const bundleContents = readFileSync(path.join(distDir, bundleName), "utf8");
// The build emits multiple root-help chunks; only the renderer owns this cache identity.
// Selecting a helper chunk forces metadata generation onto its expensive source fallback.
if (!OUTPUT_ROOT_HELP_EXPORT_RE.test(bundleContents)) {
continue;
}
const buildInfo = readBuildIdentity(distDir);
return {
bundleName,
signature: createHash("sha1")
.update(bundleContents)
.update(JSON.stringify(buildInfo))
.digest("hex"),
};
}
return null;
}
function readBuildIdentity(distDir: string): { version: string | null; commit: string | null } {
try {
const parsed = JSON.parse(readFileSync(path.join(distDir, "build-info.json"), "utf8")) as {
commit?: unknown;
version?: unknown;
};
return {
version: typeof parsed.version === "string" ? parsed.version : null,
commit: typeof parsed.commit === "string" ? parsed.commit : null,
};
} catch {
return { version: null, commit: null };
}
}