Skip to content

Commit 3895c93

Browse files
perf(cli): speed up precomputed command help startup
* perf: speed up precomputed command help * perf: precompute sessions and tasks help * Speed up precomputed command help startup * Speed up precomputed command help startup --------- Co-authored-by: Zeheng Huang <[email protected]> Co-authored-by: openclaw-clownfish[bot] <280122609+openclaw-clownfish[bot]@users.noreply.github.com>
1 parent 7626ca3 commit 3895c93

13 files changed

Lines changed: 504 additions & 27 deletions

‎openclaw.mjs‎

Lines changed: 113 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -371,22 +371,120 @@ const buildMissingEntryErrorMessage = async () => {
371371
const isBareRootHelpInvocation = (argv) =>
372372
argv.length === 3 && (argv[2] === "--help" || argv[2] === "-h");
373373

374-
const resolvePrecomputedCommandHelp = (argv) => {
375-
if (argv.length !== 4 || (argv[3] !== "--help" && argv[3] !== "-h")) {
376-
return null;
374+
const LAUNCHER_HELP_FLAGS = new Set(["-h", "--help"]);
375+
const LAUNCHER_ROOT_BOOLEAN_FLAGS = new Set(["--dev", "--no-color"]);
376+
const LAUNCHER_ROOT_VALUE_FLAGS = new Set(["--profile", "--log-level", "--container"]);
377+
const LAUNCHER_PRECOMPUTED_COMMAND_HELP = {
378+
browser: { command: "browser", metadataKey: "browserHelpText" },
379+
secrets: { command: "secrets", metadataKey: "secretsHelpText" },
380+
nodes: { command: "nodes", metadataKey: "nodesHelpText" },
381+
};
382+
const LAUNCHER_PRECOMPUTED_SUBCOMMAND_HELP = new Set([
383+
"doctor",
384+
"gateway",
385+
"models",
386+
"plugins",
387+
"sessions",
388+
"tasks",
389+
]);
390+
391+
const isLauncherRootOptionValueToken = (arg) => {
392+
if (!arg || arg === "--") {
393+
return false;
394+
}
395+
if (!arg.startsWith("-")) {
396+
return true;
377397
}
378-
if (argv[2] === "browser") {
379-
return { command: "browser", metadataKey: "browserHelpText" };
398+
return /^-\d+(?:\.\d+)?$/.test(arg);
399+
};
400+
401+
const consumeLauncherRootOptionToken = (args, index) => {
402+
const arg = args[index];
403+
if (!arg) {
404+
return 0;
405+
}
406+
if (LAUNCHER_ROOT_BOOLEAN_FLAGS.has(arg)) {
407+
return 1;
380408
}
381-
if (argv[2] === "secrets") {
382-
return { command: "secrets", metadataKey: "secretsHelpText" };
409+
if (
410+
arg.startsWith("--profile=") ||
411+
arg.startsWith("--log-level=") ||
412+
arg.startsWith("--container=")
413+
) {
414+
return 1;
383415
}
384-
if (argv[2] === "nodes") {
385-
return { command: "nodes", metadataKey: "nodesHelpText" };
416+
if (LAUNCHER_ROOT_VALUE_FLAGS.has(arg)) {
417+
return isLauncherRootOptionValueToken(args[index + 1]) ? 2 : 1;
418+
}
419+
return 0;
420+
};
421+
422+
const hasLauncherContainerTarget = (argv) => {
423+
if (normalizeLauncherMetadataValue(process.env.OPENCLAW_CONTAINER)) {
424+
return true;
425+
}
426+
const args = argv.slice(2);
427+
for (let index = 0; index < args.length; index += 1) {
428+
const arg = args[index];
429+
if (!arg || arg === "--") {
430+
return false;
431+
}
432+
if (arg === "--container" || arg.startsWith("--container=")) {
433+
return true;
434+
}
435+
}
436+
return false;
437+
};
438+
439+
const resolvePrecomputedCommandHelpByName = (commandName) => {
440+
if (Object.hasOwn(LAUNCHER_PRECOMPUTED_COMMAND_HELP, commandName)) {
441+
return LAUNCHER_PRECOMPUTED_COMMAND_HELP[commandName];
442+
}
443+
if (LAUNCHER_PRECOMPUTED_SUBCOMMAND_HELP.has(commandName)) {
444+
return {
445+
command: commandName,
446+
metadataKey: "subcommandHelpText",
447+
subcommandKey: commandName,
448+
};
386449
}
387450
return null;
388451
};
389452

453+
const resolvePrecomputedCommandHelp = (argv) => {
454+
const args = argv.slice(2);
455+
let commandHelp = null;
456+
let sawHelp = false;
457+
458+
for (let index = 0; index < args.length; index += 1) {
459+
const arg = args[index];
460+
if (!arg || arg === "--") {
461+
return null;
462+
}
463+
if (!commandHelp) {
464+
const consumed = consumeLauncherRootOptionToken(args, index);
465+
if (consumed > 0) {
466+
index += consumed - 1;
467+
continue;
468+
}
469+
if (arg.startsWith("-")) {
470+
return null;
471+
}
472+
commandHelp = resolvePrecomputedCommandHelpByName(arg);
473+
if (!commandHelp) {
474+
return null;
475+
}
476+
continue;
477+
}
478+
if (LAUNCHER_HELP_FLAGS.has(arg)) {
479+
sawHelp = true;
480+
continue;
481+
}
482+
return null;
483+
}
484+
485+
return commandHelp && sawHelp ? commandHelp : null;
486+
};
487+
390488
const isHelpFastPathDisabled = () =>
391489
process.env.OPENCLAW_DISABLE_CLI_STARTUP_HELP_FAST_PATH === "1";
392490

@@ -456,11 +554,11 @@ const shouldDeferRootHelpToRuntimeEntry = () => {
456554
return false;
457555
};
458556

459-
const loadPrecomputedHelpText = (key) => {
557+
const loadPrecomputedHelpText = (key, subkey) => {
460558
try {
461559
const raw = readFileSync(new URL("./dist/cli-startup-metadata.json", import.meta.url), "utf8");
462560
const parsed = JSON.parse(raw);
463-
const value = parsed?.[key];
561+
const value = subkey ? parsed?.[key]?.[subkey] : parsed?.[key];
464562
return typeof value === "string" && value.length > 0 ? value : null;
465563
} catch {
466564
return null;
@@ -650,10 +748,13 @@ const tryOutputPrecomputedCommandHelp = () => {
650748
if (!commandHelp) {
651749
return false;
652750
}
751+
if (hasLauncherContainerTarget(process.argv)) {
752+
return false;
753+
}
653754
if (commandHelp.command === "nodes" && shouldDeferRootHelpToRuntimeEntry()) {
654755
return false;
655756
}
656-
const precomputed = loadPrecomputedHelpText(commandHelp.metadataKey);
757+
const precomputed = loadPrecomputedHelpText(commandHelp.metadataKey, commandHelp.subcommandKey);
657758
if (!precomputed) {
658759
return false;
659760
}

‎scripts/bench-cli-startup.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,10 @@ function parseMaxRssMb(stderr: string): number | null {
663663
return Number(lastMatch[1]) / 1024;
664664
}
665665

666+
function nodeImportSpecifierForPath(filePath: string): string {
667+
return pathToFileURL(filePath).href;
668+
}
669+
666670
function buildCpuOrHeapFlags(options: { cpuProfDir?: string; heapProfDir?: string }): string[] {
667671
const flags: string[] = [];
668672
if (options.cpuProfDir) {
@@ -697,7 +701,7 @@ async function runSample(params: {
697701
}
698702
const nodeArgs = [
699703
"--import",
700-
params.rssHookPath,
704+
nodeImportSpecifierForPath(params.rssHookPath),
701705
...buildCpuOrHeapFlags({
702706
cpuProfDir: params.cpuProfDir,
703707
heapProfDir: params.heapProfDir,
@@ -1268,6 +1272,7 @@ async function main(): Promise<void> {
12681272
export const testing = {
12691273
buildConfigFixture,
12701274
collectFailedSamples,
1275+
nodeImportSpecifierForPath,
12711276
parseGatewayPortEnv,
12721277
parseNonNegativeInt,
12731278
parsePositiveInt,

‎scripts/check-cli-startup-memory.mjs‎

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,10 @@ function formatCaseCommand(testCase) {
173173
return `node ${testCase.args.join(" ")}`;
174174
}
175175

176+
function nodeImportSpecifierForPath(filePath) {
177+
return pathToFileURL(filePath).href;
178+
}
179+
176180
function buildBenchEnv() {
177181
if (!tmpHome) {
178182
throw new Error("temporary home is not initialized");
@@ -218,14 +222,18 @@ function runCase(testCase, params = {}) {
218222
const env = buildBenchEnv();
219223
const spawn = params.spawnSync ?? defaultSpawnSync;
220224
const timeoutMs = params.timeoutMs ?? COMMAND_TIMEOUT_MS;
221-
const result = spawn(process.execPath, ["--import", rssHookPath, ...testCase.args], {
222-
cwd: repoRoot,
223-
env,
224-
encoding: "utf8",
225-
maxBuffer: 20 * 1024 * 1024,
226-
timeout: timeoutMs,
227-
killSignal: "SIGKILL",
228-
});
225+
const result = spawn(
226+
process.execPath,
227+
["--import", nodeImportSpecifierForPath(rssHookPath), ...testCase.args],
228+
{
229+
cwd: repoRoot,
230+
env,
231+
encoding: "utf8",
232+
maxBuffer: 20 * 1024 * 1024,
233+
timeout: timeoutMs,
234+
killSignal: "SIGKILL",
235+
},
236+
);
229237
const stderr = result.stderr ?? "";
230238
const maxRssMb = parseMaxRssMb(stderr);
231239
const matrixBootstrapWarning = /matrix: crypto runtime bootstrap failed/i.test(stderr);
@@ -373,6 +381,7 @@ function runStartupMemoryCheck(argv = process.argv.slice(2), params = {}) {
373381
*/
374382
export const testing = {
375383
cases,
384+
nodeImportSpecifierForPath,
376385
parseArgs,
377386
readPositiveIntEnv,
378387
readPositiveNumberEnv,

‎scripts/write-cli-startup-metadata.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,14 @@ const COMMAND_HELP_RENDER_TIMEOUT_MS = 120_000;
3333
const COMMAND_HELP_RENDER_MAX_OUTPUT_BYTES = 16 * 1024 * 1024;
3434
const COMMAND_HELP_RENDER_KILL_GRACE_MS = 5_000;
3535
const COMMAND_HELP_RENDER_CONCURRENCY = 2;
36-
const PRECOMPUTED_SUBCOMMAND_HELP_COMMANDS = ["doctor", "gateway", "models", "plugins"] as const;
36+
const PRECOMPUTED_SUBCOMMAND_HELP_COMMANDS = [
37+
"doctor",
38+
"gateway",
39+
"models",
40+
"plugins",
41+
"sessions",
42+
"tasks",
43+
] as const;
3744
const CORE_CHANNEL_ORDER = [
3845
"telegram",
3946
"whatsapp",
@@ -249,6 +256,7 @@ function resolveSubcommandHelpSourceSignature(sourceRootDir: string = rootDir):
249256
path.join(sourceRootDir, "src/cli/help-format.ts"),
250257
path.join(sourceRootDir, "src/cli/daemon-cli/register-service-commands.ts"),
251258
path.join(sourceRootDir, "src/cli/program/register.maintenance.ts"),
259+
path.join(sourceRootDir, "src/cli/program/register.status-health-sessions.ts"),
252260
path.join(sourceRootDir, "src/cli/gateway-cli.ts"),
253261
path.join(sourceRootDir, "src/cli/gateway-cli/register.ts"),
254262
path.join(sourceRootDir, "src/cli/gateway-cli/run-command.ts"),

‎src/cli/root-help-metadata.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
// Cached startup metadata readers for precomputed root and subcommand help text.
22
import { readCliStartupMetadata } from "./startup-metadata.js";
33

4-
export type PrecomputedSubcommandHelpName = "doctor" | "gateway" | "models" | "plugins";
4+
export type PrecomputedSubcommandHelpName =
5+
| "doctor"
6+
| "gateway"
7+
| "models"
8+
| "plugins"
9+
| "sessions"
10+
| "tasks";
511

612
let precomputedRootHelpText: string | null | undefined;
713
let precomputedBrowserHelpText: string | null | undefined;
@@ -139,7 +145,9 @@ function isPrecomputedSubcommandHelpName(
139145
commandName === "doctor" ||
140146
commandName === "gateway" ||
141147
commandName === "models" ||
142-
commandName === "plugins"
148+
commandName === "plugins" ||
149+
commandName === "sessions" ||
150+
commandName === "tasks"
143151
);
144152
}
145153

‎src/cli/run-main-policy.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,14 @@ import { getSubCliParentDefaultHelpCommands } from "./program/subcli-descriptors
2424

2525
const ROOT_HELP_ALIASES = new Set(["tools"]);
2626
const SETUP_ONBOARD_CONFIGURE_HELP_COMMANDS = new Set(["setup", "onboard", "configure"]);
27-
const PRECOMPUTED_SUBCOMMAND_HELP_COMMANDS = new Set(["doctor", "gateway", "models", "plugins"]);
27+
const PRECOMPUTED_SUBCOMMAND_HELP_COMMANDS = new Set([
28+
"doctor",
29+
"gateway",
30+
"models",
31+
"plugins",
32+
"sessions",
33+
"tasks",
34+
]);
2835
const HELP_FLAGS = new Set(["-h", "--help"]);
2936
const VERSION_FLAGS = new Set(["-V", "--version"]);
3037
const BARE_PARENT_DEFAULT_HELP_COMMANDS = new Set([

‎src/cli/run-main.test.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,12 @@ describe("resolvePrecomputedSubcommandHelpFastPath", () => {
319319
expect(
320320
resolvePrecomputedSubcommandHelpFastPath(["node", "openclaw", "plugins", "--help"]),
321321
).toBe("plugins");
322+
expect(
323+
resolvePrecomputedSubcommandHelpFastPath(["node", "openclaw", "sessions", "--help"]),
324+
).toBe("sessions");
325+
expect(resolvePrecomputedSubcommandHelpFastPath(["node", "openclaw", "tasks", "-h"])).toBe(
326+
"tasks",
327+
);
322328
expect(
323329
resolvePrecomputedSubcommandHelpFastPath(["node", "openclaw", "doctor", "--version"]),
324330
).toBeNull();

0 commit comments

Comments
 (0)