openclaw/src/cli/run-main.ts

129 lines
4.2 KiB
TypeScript
Raw Normal View History

2026-01-05 01:25:37 +01:00
import process from "node:process";
import { fileURLToPath } from "node:url";
import { loadDotEnv } from "../infra/dotenv.js";
import { normalizeEnv } from "../infra/env.js";
import { formatUncaughtError } from "../infra/errors.js";
2026-01-05 01:25:37 +01:00
import { isMainModule } from "../infra/is-main.js";
2026-01-30 03:15:10 +01:00
import { ensureOpenClawCliOnPath } from "../infra/path-env.js";
2026-01-05 01:25:37 +01:00
import { assertSupportedRuntime } from "../infra/runtime-guard.js";
import { installUnhandledRejectionHandler } from "../infra/unhandled-rejections.js";
2026-01-05 01:25:37 +01:00
import { enableConsoleCapture } from "../logging.js";
import { getCommandPath, getPrimaryCommand, hasHelpOrVersion } from "./argv.js";
import { tryRouteCli } from "./route.js";
import { normalizeWindowsArgv } from "./windows-argv.js";
2026-01-10 20:32:15 +01:00
export function rewriteUpdateFlagArgv(argv: string[]): string[] {
const index = argv.indexOf("--update");
if (index === -1) {
return argv;
}
2026-01-10 20:32:15 +01:00
const next = [...argv];
next.splice(index, 1, "update");
return next;
}
2026-01-05 01:25:37 +01:00
export function shouldRegisterPrimarySubcommand(argv: string[]): boolean {
return !hasHelpOrVersion(argv);
}
export function shouldSkipPluginCommandRegistration(params: {
argv: string[];
primary: string | null;
hasBuiltinPrimary: boolean;
}): boolean {
if (params.hasBuiltinPrimary) {
return true;
}
if (!params.primary) {
return hasHelpOrVersion(params.argv);
}
return false;
}
export function shouldEnsureCliPath(argv: string[]): boolean {
if (hasHelpOrVersion(argv)) {
return false;
}
const [primary, secondary] = getCommandPath(argv, 2);
if (!primary) {
return true;
}
if (primary === "status" || primary === "health" || primary === "sessions") {
return false;
}
if (primary === "config" && (secondary === "get" || secondary === "unset")) {
return false;
}
if (primary === "models" && (secondary === "list" || secondary === "status")) {
return false;
}
return true;
}
2026-01-05 01:25:37 +01:00
export async function runCli(argv: string[] = process.argv) {
const normalizedArgv = normalizeWindowsArgv(argv);
2026-01-05 01:25:37 +01:00
loadDotEnv({ quiet: true });
normalizeEnv();
if (shouldEnsureCliPath(normalizedArgv)) {
ensureOpenClawCliOnPath();
}
2026-01-05 01:25:37 +01:00
// Enforce the minimum supported runtime before doing any work.
assertSupportedRuntime();
if (await tryRouteCli(normalizedArgv)) {
return;
}
// Capture all console output into structured logs while keeping stdout/stderr behavior.
enableConsoleCapture();
2026-01-05 01:25:37 +01:00
const { buildProgram } = await import("./program.js");
const program = buildProgram();
// Global error handlers to prevent silent crashes from unhandled rejections/exceptions.
// These log the error and exit gracefully instead of crashing without trace.
installUnhandledRejectionHandler();
2026-01-05 01:25:37 +01:00
process.on("uncaughtException", (error) => {
2026-01-30 03:15:10 +01:00
console.error("[openclaw] Uncaught exception:", formatUncaughtError(error));
2026-01-05 01:25:37 +01:00
process.exit(1);
});
2026-01-21 04:01:23 +00:00
const parseArgv = rewriteUpdateFlagArgv(normalizedArgv);
2026-02-14 12:16:16 +00:00
// Register the primary command (builtin or subcli) so help and command parsing
// are correct even with lazy command registration.
CLI: fix subcommand registration to work without --help/--version flags (#1683) ## Problem The clawdbot-gateway systemd service was crash-looping on Linux (Fedora 42, aarch64) with the error: error: unknown command '/usr/bin/node-22' After ~20 seconds of runtime, the gateway would exit with status 1/FAILURE and systemd would restart it, repeating the cycle indefinitely (80+ restarts observed). ## Root Cause Analysis ### Investigation Steps 1. Examined systemd service logs via `journalctl --user -u clawdbot-gateway.service` 2. Found the error appeared consistently after the service had been running for 20-30 seconds 3. Added debug logging to trace argv at parseAsync() call 4. Discovered that argv was being passed to Commander.js with the node binary and script paths still present: `["/usr/bin/node-22", "/path/to/entry.js", "gateway", "--port", "18789"]` 5. Traced the issue to the lazy subcommand registration logic in runCli() ### The Bug The lazy-loading logic for subcommands was gated behind `hasHelpOrVersion(parseArgv)`: ```typescript if (hasHelpOrVersion(parseArgv)) { const primary = getPrimaryCommand(parseArgv); if (primary) { const { registerSubCliByName } = await import("./program/register.subclis.js"); await registerSubCliByName(program, primary); } } ``` This meant that when running `clawdbot gateway --port 18789` (without --help or --version), the `gateway` subcommand was never registered before `program.parseAsync(parseArgv)` was called. Commander.js would then try to parse the arguments without knowing about the gateway command, leading to parse errors. The error message "unknown command '/usr/bin/node-22'" appeared because Commander was treating the first positional argument as a command name due to argv not being properly stripped on non-Windows platforms in some code paths. ## The Fix Remove the `hasHelpOrVersion()` gate and always register the primary subcommand when one is detected: ```typescript // Register the primary subcommand if one exists (for lazy-loading) const primary = getPrimaryCommand(parseArgv); if (primary) { const { registerSubCliByName } = await import("./program/register.subclis.js"); await registerSubCliByName(program, primary); } ``` This ensures that subcommands like `gateway` are properly registered before parsing begins, regardless of what flags are present. ## Environment - OS: Fedora 42 (Linux 6.15.9-201.fc42.aarch64) - Arch: aarch64 - Node: /usr/bin/node-22 (symlink to node-22) - Deployment: systemd user service - Runtime: Gateway started via `clawdbot gateway --port 18789` ## Why This Should Be Merged 1. **Critical Bug**: The gateway service cannot run reliably on Linux without this fix, making it a blocking issue for production deployments via systemd. 2. **Affects All Non-Help Invocations**: Any direct subcommand invocation (gateway, channels, etc.) without --help/--version is broken. 3. **Simple & Safe Fix**: The change removes an unnecessary condition that was preventing lazy-loading from working correctly. Subcommands should always be registered when detected, not just for help/version requests. 4. **No Regression Risk**: The fix maintains the lazy-loading behavior (only loads the requested subcommand), just ensures it works in all cases instead of only help/version scenarios. 5. **Tested**: Verified that the gateway service now runs stably for extended periods (45+ seconds continuous runtime with no crashes) after applying this fix. Co-authored-by: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-01-25 14:17:02 +11:00
const primary = getPrimaryCommand(parseArgv);
2026-02-14 12:16:16 +00:00
if (primary) {
const { getProgramContext } = await import("./program/program-context.js");
const ctx = getProgramContext(program);
if (ctx) {
const { registerCoreCliByName } = await import("./program/command-registry.js");
await registerCoreCliByName(program, ctx, primary, parseArgv);
}
CLI: fix subcommand registration to work without --help/--version flags (#1683) ## Problem The clawdbot-gateway systemd service was crash-looping on Linux (Fedora 42, aarch64) with the error: error: unknown command '/usr/bin/node-22' After ~20 seconds of runtime, the gateway would exit with status 1/FAILURE and systemd would restart it, repeating the cycle indefinitely (80+ restarts observed). ## Root Cause Analysis ### Investigation Steps 1. Examined systemd service logs via `journalctl --user -u clawdbot-gateway.service` 2. Found the error appeared consistently after the service had been running for 20-30 seconds 3. Added debug logging to trace argv at parseAsync() call 4. Discovered that argv was being passed to Commander.js with the node binary and script paths still present: `["/usr/bin/node-22", "/path/to/entry.js", "gateway", "--port", "18789"]` 5. Traced the issue to the lazy subcommand registration logic in runCli() ### The Bug The lazy-loading logic for subcommands was gated behind `hasHelpOrVersion(parseArgv)`: ```typescript if (hasHelpOrVersion(parseArgv)) { const primary = getPrimaryCommand(parseArgv); if (primary) { const { registerSubCliByName } = await import("./program/register.subclis.js"); await registerSubCliByName(program, primary); } } ``` This meant that when running `clawdbot gateway --port 18789` (without --help or --version), the `gateway` subcommand was never registered before `program.parseAsync(parseArgv)` was called. Commander.js would then try to parse the arguments without knowing about the gateway command, leading to parse errors. The error message "unknown command '/usr/bin/node-22'" appeared because Commander was treating the first positional argument as a command name due to argv not being properly stripped on non-Windows platforms in some code paths. ## The Fix Remove the `hasHelpOrVersion()` gate and always register the primary subcommand when one is detected: ```typescript // Register the primary subcommand if one exists (for lazy-loading) const primary = getPrimaryCommand(parseArgv); if (primary) { const { registerSubCliByName } = await import("./program/register.subclis.js"); await registerSubCliByName(program, primary); } ``` This ensures that subcommands like `gateway` are properly registered before parsing begins, regardless of what flags are present. ## Environment - OS: Fedora 42 (Linux 6.15.9-201.fc42.aarch64) - Arch: aarch64 - Node: /usr/bin/node-22 (symlink to node-22) - Deployment: systemd user service - Runtime: Gateway started via `clawdbot gateway --port 18789` ## Why This Should Be Merged 1. **Critical Bug**: The gateway service cannot run reliably on Linux without this fix, making it a blocking issue for production deployments via systemd. 2. **Affects All Non-Help Invocations**: Any direct subcommand invocation (gateway, channels, etc.) without --help/--version is broken. 3. **Simple & Safe Fix**: The change removes an unnecessary condition that was preventing lazy-loading from working correctly. Subcommands should always be registered when detected, not just for help/version requests. 4. **No Regression Risk**: The fix maintains the lazy-loading behavior (only loads the requested subcommand), just ensures it works in all cases instead of only help/version scenarios. 5. **Tested**: Verified that the gateway service now runs stably for extended periods (45+ seconds continuous runtime with no crashes) after applying this fix. Co-authored-by: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-01-25 14:17:02 +11:00
const { registerSubCliByName } = await import("./program/register.subclis.js");
await registerSubCliByName(program, primary);
2026-01-21 04:01:23 +00:00
}
const hasBuiltinPrimary =
primary !== null && program.commands.some((command) => command.name() === primary);
const shouldSkipPluginRegistration = shouldSkipPluginCommandRegistration({
argv: parseArgv,
primary,
hasBuiltinPrimary,
});
if (!shouldSkipPluginRegistration) {
// Register plugin CLI commands before parsing
const { registerPluginCliCommands } = await import("../plugins/cli.js");
const { loadConfig } = await import("../config/config.js");
registerPluginCliCommands(program, loadConfig());
}
2026-01-21 04:01:23 +00:00
await program.parseAsync(parseArgv);
2026-01-19 13:08:21 +00:00
}
2026-01-05 01:25:37 +01:00
export function isCliMainModule(): boolean {
return isMainModule({ currentFile: fileURLToPath(import.meta.url) });
}