Skip to content

Commit 68e3777

Browse files
authored
fix(cli): bound unknown command diagnostics (#120380)
* fix(cli): bound unknown command diagnostics * fix(cli): include truncation marker in display cap
1 parent b855934 commit 68e3777

2 files changed

Lines changed: 47 additions & 2 deletions

File tree

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2687,6 +2687,42 @@ describe("runCli exit behavior", () => {
26872687
expect(registerPluginCliCommandsFromValidatedConfigMock).not.toHaveBeenCalled();
26882688
});
26892689

2690+
it("sanitizes control characters in unowned command diagnostics", async () => {
2691+
const primary = "bad\u001b[31m-red\u001b[0m\nforged\tline";
2692+
2693+
await expect(runCli(["node", "openclaw", primary])).rejects.toThrow(
2694+
'Unknown command: openclaw bad-red\\nforged\\tline. No built-in command or plugin CLI metadata owns "bad-red\\nforged\\tline".',
2695+
);
2696+
2697+
expect(startProxyMock).not.toHaveBeenCalled();
2698+
expect(tryRouteCliMock).not.toHaveBeenCalled();
2699+
expect(buildProgramMock).not.toHaveBeenCalled();
2700+
expect(registerPluginCliCommandsFromValidatedConfigMock).not.toHaveBeenCalled();
2701+
});
2702+
2703+
it("bounds long unowned command diagnostics without splitting Unicode", async () => {
2704+
const primary = "🦞".repeat(1_000);
2705+
2706+
let error: unknown;
2707+
try {
2708+
await runCli(["node", "openclaw", primary]);
2709+
} catch (caught) {
2710+
error = caught;
2711+
}
2712+
2713+
expect(error).toBeInstanceOf(Error);
2714+
const message = (error as Error).message;
2715+
const displayPrimary = `${"🦞".repeat(63)}…`;
2716+
expect(displayPrimary.length).toBeLessThanOrEqual(128);
2717+
expect(message).toContain(`Unknown command: openclaw ${displayPrimary}`);
2718+
expect(message).not.toContain("�");
2719+
expect(message.length).toBeLessThan(500);
2720+
expect(startProxyMock).not.toHaveBeenCalled();
2721+
expect(tryRouteCliMock).not.toHaveBeenCalled();
2722+
expect(buildProgramMock).not.toHaveBeenCalled();
2723+
expect(registerPluginCliCommandsFromValidatedConfigMock).not.toHaveBeenCalled();
2724+
});
2725+
26902726
it("keeps suggestions out of plugin-policy diagnostics", async () => {
26912727
resolveManifestCommandAliasOwnerMock.mockReturnValueOnce({
26922728
pluginId: "codex",

‎src/cli/run-main.ts‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import { existsSync } from "node:fs";
33
import path from "node:path";
44
import process from "node:process";
55
import { normalizeOptionalString } from "@openclaw/normalization-core/string-coerce";
6+
import { truncateUtf16Safe } from "@openclaw/normalization-core/utf16-slice";
67
import type { Command as CommanderCommand, Option as CommanderOption } from "commander";
8+
import { sanitizeTerminalText } from "../../packages/terminal-core/src/safe-text.js";
79
import { resolveStateDir } from "../config/paths.js";
810
import type { ConfigFileSnapshot, OpenClawConfig } from "../config/types.openclaw.js";
911
import { isLoopbackAddress, isSecureWebSocketUrl } from "../gateway/net.js";
@@ -87,6 +89,7 @@ const CLI_PROXY_ENV_KEYS = [
8789
"https_proxy",
8890
"all_proxy",
8991
] as const;
92+
const UNKNOWN_COMMAND_DISPLAY_LIMIT = 128;
9093

9194
const loadRootHelpLiveConfigModule = async () => await import("./root-help-live-config.js");
9295
const loadRootHelpMetadataModule = async () => await import("./root-help-metadata.js");
@@ -1029,9 +1032,15 @@ async function resolveUnownedCliPrimaryMessage(params: {
10291032
if (pluginPolicyMessage) {
10301033
return pluginPolicyMessage;
10311034
}
1032-
const suggestion = formatCliCommandSuggestions(params.primary);
1035+
const sanitizedPrimary = sanitizeTerminalText(params.primary);
1036+
const displayPrimary =
1037+
sanitizedPrimary.length <= UNKNOWN_COMMAND_DISPLAY_LIMIT
1038+
? sanitizedPrimary
1039+
: `${truncateUtf16Safe(sanitizedPrimary, UNKNOWN_COMMAND_DISPLAY_LIMIT - 1)}…`;
1040+
const suggestion =
1041+
displayPrimary === params.primary ? formatCliCommandSuggestions(params.primary) : "";
10331042
return [
1034-
`Unknown command: openclaw ${params.primary}. No built-in command or plugin CLI metadata owns "${params.primary}".`,
1043+
`Unknown command: openclaw ${displayPrimary}. No built-in command or plugin CLI metadata owns "${displayPrimary}".`,
10351044
suggestion,
10361045
]
10371046
.filter(Boolean)

0 commit comments

Comments
 (0)