Skip to content

Commit ff4560e

Browse files
committed
fix(cli): report plugin policy refusals as expected conditions
1 parent 793669c commit ff4560e

8 files changed

Lines changed: 203 additions & 64 deletions

File tree

‎src/cli/failure-output.test.ts‎

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Failure output tests cover CLI error formatting and failure summaries.
22
import { describe, expect, it } from "vitest";
3-
import { CliParseError, formatCliFailureLines, formatCliJsonFailure } from "./failure-output.js";
3+
import { ExpectedCliError, formatCliFailureLines, formatCliJsonFailure } from "./failure-output.js";
44

55
describe("formatCliJsonFailure", () => {
66
it("uses the canonical typed envelope and redacts the message", () => {
@@ -34,7 +34,7 @@ describe("formatCliJsonFailure", () => {
3434
{ label: "debug output", env: { OPENCLAW_DEBUG: "1" } },
3535
])("keeps the full parse guidance unchanged in $label", ({ env }) => {
3636
const error = Object.assign(
37-
new CliParseError({
37+
new ExpectedCliError({
3838
message: 'OpenClaw sessions has no command "lst".',
3939
humanOutput:
4040
'\u001B[31mOpenClaw sessions has no command "lst".\u001B[39m\nDid you mean this?\n openclaw sessions list\nTry: openclaw sessions --help\nDocs: \u001B]8;;https://docs.openclaw.ai/cli\u0007docs.openclaw.ai/cli\u001B]8;;\u0007\n',
@@ -55,19 +55,35 @@ describe("formatCliJsonFailure", () => {
5555
});
5656
expect(payload.error.message).not.toContain("internal parse cause");
5757
});
58+
59+
it("keeps plugin policy messages in the canonical JSON envelope", () => {
60+
const message =
61+
'The `openclaw workboard` command is provided by the "workboard" plugin, but that bundled plugin is disabled by default. Run `openclaw plugins enable workboard` to enable that CLI surface.';
62+
63+
const error = new ExpectedCliError({
64+
message,
65+
humanOutput: message,
66+
machineOutput: message,
67+
});
68+
69+
expect(formatCliJsonFailure(error)).toEqual({
70+
ok: false,
71+
error: { type: "cli_error", message },
72+
});
73+
});
5874
});
5975

6076
describe("formatCliFailureLines", () => {
6177
it.each([
6278
{ label: "default output", env: {} },
6379
{ label: "debug output", env: { OPENCLAW_DEBUG: "1" } },
64-
])("emits parse guidance only when not already written in $label", ({ env }) => {
65-
const pending = new CliParseError({
80+
])("emits expected guidance only when not already written in $label", ({ env }) => {
81+
const pending = new ExpectedCliError({
6682
message: "bad input",
6783
humanOutput: "\u001B[31mfirst\u001B[39m\nsecond\n",
6884
machineOutput: "first\nsecond\n",
6985
});
70-
const written = new CliParseError({
86+
const written = new ExpectedCliError({
7187
message: "bad input",
7288
humanOutput: "\u001B[31mfirst\u001B[39m\nsecond\n",
7389
humanOutputWritten: true,

‎src/cli/failure-output.ts‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export type CliJsonFailure = {
2121
};
2222
};
2323

24-
export class CliParseError extends Error {
24+
export class ExpectedCliError extends Error {
2525
readonly humanOutput: string;
2626
readonly humanOutputWritten: boolean;
2727
readonly machineOutput: string;
@@ -33,22 +33,25 @@ export class CliParseError extends Error {
3333
machineOutput: string;
3434
}) {
3535
super(params.message);
36-
this.name = "CliParseError";
36+
this.name = "ExpectedCliError";
3737
this.humanOutput = params.humanOutput;
3838
this.humanOutputWritten = params.humanOutputWritten ?? false;
3939
this.machineOutput = params.machineOutput;
4040
}
4141
}
4242

43+
export function isExpectedCliError(error: unknown): error is ExpectedCliError {
44+
return error instanceof ExpectedCliError;
45+
}
46+
4347
/** Canonical machine-readable failure envelope for CLI-owned errors. */
4448
export function formatCliJsonFailure(
4549
error: unknown,
4650
options: CliFailureDebugOptions = {},
4751
): CliJsonFailure {
48-
const message =
49-
error instanceof CliParseError
50-
? formatErrorMessage(error.machineOutput.trimEnd())
51-
: formatCliOperatorError(error, options);
52+
const message = isExpectedCliError(error)
53+
? formatErrorMessage(error.machineOutput.trimEnd())
54+
: formatCliOperatorError(error, options);
5255
return {
5356
ok: false,
5457
error: {
@@ -97,7 +100,7 @@ function pushPrefixed(out: string[], value: string): void {
97100
}
98101

99102
export function formatCliFailureLines(options: FormatCliFailureOptions): string[] {
100-
if (options.error instanceof CliParseError) {
103+
if (isExpectedCliError(options.error)) {
101104
return options.error.humanOutputWritten ? [] : options.error.humanOutput.trimEnd().split("\n");
102105
}
103106

‎src/cli/program/error-output.ts‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { formatDocsLink } from "../../../packages/terminal-core/src/links.js";
44
import { theme } from "../../../packages/terminal-core/src/theme.js";
55
import { getCommandPathWithRootOptions } from "../argv.js";
66
import { formatCliCommand } from "../command-format.js";
7-
import { CliParseError } from "../failure-output.js";
7+
import { ExpectedCliError } from "../failure-output.js";
88
import { formatCliCommandSuggestions } from "./command-suggestions.js";
99

1010
type FormatCliParseErrorOptions = {
@@ -80,22 +80,22 @@ export function createCliParseError(
8080
raw: string,
8181
options: FormatCliParseErrorOptions = {},
8282
errorOptions: { humanOutputWritten?: boolean } = {},
83-
): CliParseError {
83+
): ExpectedCliError {
8484
const message = stripCommanderErrorPrefix(raw);
8585
const unknownCommand = message.match(/^unknown command ['"`](.+?)['"`]/i);
8686
if (unknownCommand) {
8787
const command = unknownCommand[1] ?? "";
8888
const commandPath = options.commandPath ?? [];
8989
const humanOutput = formatCliUnknownCommandOutput(command, options);
90-
return new CliParseError({
90+
return new ExpectedCliError({
9191
message: formatUnknownCommandMessage(command, commandPath),
9292
humanOutput,
9393
humanOutputWritten: errorOptions.humanOutputWritten,
9494
machineOutput: formatCliMachineOutput(humanOutput),
9595
});
9696
}
9797
const humanOutput = formatCliParseErrorOutput(raw, options);
98-
return new CliParseError({
98+
return new ExpectedCliError({
9999
message,
100100
humanOutput,
101101
humanOutputWritten: errorOptions.humanOutputWritten,
@@ -106,10 +106,10 @@ export function createCliParseError(
106106
export function createCliUnknownCommandError(
107107
command: string,
108108
options: FormatCliParseErrorOptions = {},
109-
): CliParseError {
109+
): ExpectedCliError {
110110
const commandPath = options.commandPath ?? [];
111111
const humanOutput = formatCliUnknownCommandOutput(command, options);
112-
return new CliParseError({
112+
return new ExpectedCliError({
113113
message: formatUnknownCommandMessage(command, commandPath),
114114
humanOutput,
115115
machineOutput: formatCliMachineOutput(humanOutput),

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

Lines changed: 103 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { createSubsystemLogger } from "../logging/subsystem.js";
1212
import { withSecureTestNodeExecPath } from "../secrets/test-node-command.test-support.js";
1313
import type { LocalOnboardingState } from "../state/local-onboarding-state.js";
1414
import { captureEnv, withEnvAsync } from "../test-utils/env.js";
15-
import { CliParseError } from "./failure-output.js";
15+
import { ExpectedCliError } from "./failure-output.js";
1616
import { getGatewayRunRuntimeHooks } from "./gateway-cli/runtime-hooks.js";
1717
import type { RootHelpRenderOptions } from "./program/root-help.js";
1818
import { registerSignalExitBarrier } from "./signal-exit-barrier.js";
@@ -2935,8 +2935,8 @@ describe("runCli exit behavior", () => {
29352935
it("suggests close known commands for unowned command roots before proxy startup", async () => {
29362936
const error = await runCli(["node", "openclaw", "upate"]).catch((cause: unknown) => cause);
29372937

2938-
expect(error).toBeInstanceOf(CliParseError);
2939-
expect((error as CliParseError).humanOutput).toContain(
2938+
expect(error).toBeInstanceOf(ExpectedCliError);
2939+
expect((error as ExpectedCliError).humanOutput).toContain(
29402940
"Did you mean this?\n openclaw update\n",
29412941
);
29422942

@@ -2982,28 +2982,109 @@ describe("runCli exit behavior", () => {
29822982
expect(registerPluginCliCommandsFromValidatedConfigMock).not.toHaveBeenCalled();
29832983
});
29842984

2985-
it("keeps suggestions out of plugin-policy diagnostics", async () => {
2986-
resolveManifestCommandAliasOwnerMock.mockReturnValueOnce({
2987-
pluginId: "codex",
2988-
kind: "runtime-slash",
2989-
cliCommand: "plugins",
2985+
it.each([
2986+
{
2987+
label: "plugins.allow exclusion",
2988+
command: "workboard",
2989+
config: { plugins: { allow: ["browser"] } },
2990+
commandAlias: { pluginId: "workboard" },
2991+
expectedText: '`plugins.allow` excludes "workboard"',
2992+
},
2993+
{
2994+
label: "parent plugin allowlist guidance",
2995+
command: "voicecall",
2996+
config: { plugins: { allow: ["voicecall"] } },
2997+
commandAlias: { pluginId: "voice-call" },
2998+
expectedText: 'Add "voice-call" to `plugins.allow` instead of "voicecall"',
2999+
},
3000+
{
3001+
label: "explicit plugin disablement",
3002+
command: "browser",
3003+
config: { plugins: { entries: { browser: { enabled: false } } } },
3004+
commandAlias: { pluginId: "browser", enabledByDefault: true },
3005+
expectedText: "plugins.entries.browser.enabled=false",
3006+
},
3007+
{
3008+
label: "runtime slash command",
3009+
command: "dreaming",
3010+
config: {},
3011+
commandAlias: {
3012+
pluginId: "memory-core",
3013+
kind: "runtime-slash",
3014+
cliCommand: "memory",
3015+
},
3016+
expectedText: "runtime slash command (/dreaming)",
3017+
},
3018+
{
3019+
label: "loaded agent tool",
3020+
command: "lcm_recent",
3021+
config: {},
3022+
toolOwner: {
3023+
toolName: "lcm_recent",
3024+
pluginId: "lossless-claw",
3025+
availability: "loaded",
3026+
},
3027+
expectedText: "is an agent tool available",
3028+
},
3029+
{
3030+
label: "manifest-only agent tool",
3031+
command: "feishu_chat",
3032+
config: {},
3033+
toolOwner: {
3034+
toolName: "feishu_chat",
3035+
pluginId: "feishu",
3036+
availability: "manifest-only",
3037+
},
3038+
expectedText: "may be provided",
3039+
},
3040+
])(
3041+
"reports $label as an expected condition before proxy startup",
3042+
async ({ command, config, commandAlias, toolOwner, expectedText }) => {
3043+
loadConfigMock.mockReturnValue(config);
3044+
resolveManifestCommandAliasOwnerMock.mockReturnValue(commandAlias);
3045+
resolveManifestToolOwnerMock.mockReturnValue(toolOwner);
3046+
3047+
const error = await runCli(["node", "openclaw", command]).catch((cause: unknown) => cause);
3048+
3049+
expect(error).toBeInstanceOf(ExpectedCliError);
3050+
expect((error as ExpectedCliError).message).toContain(expectedText);
3051+
expect((error as ExpectedCliError).humanOutput).toBe((error as Error).message);
3052+
expect((error as ExpectedCliError).machineOutput).toBe((error as Error).message);
3053+
expect((error as Error).message).not.toContain("Did you mean this?");
3054+
expect(startProxyMock).not.toHaveBeenCalled();
3055+
expect(tryRouteCliMock).not.toHaveBeenCalled();
3056+
expect(buildProgramMock).not.toHaveBeenCalled();
3057+
expect(registerPluginCliCommandsFromValidatedConfigMock).not.toHaveBeenCalled();
3058+
},
3059+
);
3060+
3061+
it("reports disabled-by-default plugin commands as expected after lazy registration", async () => {
3062+
const program = { commands: [], parseAsync: vi.fn() };
3063+
buildProgramMock.mockReturnValueOnce(program);
3064+
tryRouteCliMock.mockResolvedValueOnce(false);
3065+
resolvePluginCliRootOwnerIdsMock.mockReturnValue(["workboard"]);
3066+
resolveManifestCommandAliasOwnerMock.mockReturnValue({
3067+
pluginId: "workboard",
3068+
enabledByDefault: false,
29903069
});
29913070

2992-
let error: unknown;
2993-
try {
2994-
await runCli(["node", "openclaw", "codex"]);
2995-
} catch (caught) {
2996-
error = caught;
2997-
}
3071+
const error = await runCli(["node", "openclaw", "workboard", "list"]).catch(
3072+
(cause: unknown) => cause,
3073+
);
29983074

2999-
expect(error).toBeInstanceOf(Error);
3000-
expect((error as Error).message).toContain("runtime slash command");
3001-
expect((error as Error).message).toContain("/codex");
3002-
expect((error as Error).message).not.toContain("Did you mean this?");
3003-
expect(startProxyMock).not.toHaveBeenCalled();
3004-
expect(tryRouteCliMock).not.toHaveBeenCalled();
3005-
expect(buildProgramMock).not.toHaveBeenCalled();
3006-
expect(registerPluginCliCommandsFromValidatedConfigMock).not.toHaveBeenCalled();
3075+
expect(error).toBeInstanceOf(ExpectedCliError);
3076+
expect((error as ExpectedCliError).message).toContain(
3077+
'the "workboard" plugin, but that bundled plugin is disabled by default',
3078+
);
3079+
expect((error as ExpectedCliError).humanOutput).toBe((error as Error).message);
3080+
expect((error as ExpectedCliError).machineOutput).toBe((error as Error).message);
3081+
expect(registerPluginCliCommandsFromValidatedConfigMock).toHaveBeenCalledWith(
3082+
program,
3083+
undefined,
3084+
undefined,
3085+
{ mode: "lazy", primary: "workboard", skipPluginValidation: false },
3086+
);
3087+
expect(program.parseAsync).not.toHaveBeenCalled();
30073088
});
30083089

30093090
it("rejects unowned command roots even when --help is appended (regression for #81077)", async () => {

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

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,9 @@ describe("resolveMissingPluginCommandMessage", () => {
348348
},
349349
{ registry: browserCommandAliasRegistry },
350350
),
351-
).toContain('`plugins.allow` excludes "browser"');
351+
).toBe(
352+
'The `openclaw browser` command is unavailable because `plugins.allow` excludes "browser". Add "browser" to `plugins.allow` if you want that bundled plugin CLI surface.',
353+
);
352354
});
353355

354356
it("explains explicit bundled plugin disablement", () => {
@@ -362,7 +364,9 @@ describe("resolveMissingPluginCommandMessage", () => {
362364
},
363365
},
364366
}),
365-
).toContain("plugins.entries.browser.enabled=false");
367+
).toBe(
368+
"The `openclaw browser` command is unavailable because `plugins.entries.browser.enabled=false`. Re-enable that entry if you want the bundled plugin CLI surface.",
369+
);
366370
});
367371

368372
it("returns null when the bundled plugin command is already allowed", () => {
@@ -394,10 +398,9 @@ describe("resolveMissingPluginCommandMessage", () => {
394398
registry: memoryCoreCommandAliasRegistry,
395399
},
396400
);
397-
expect(message).toContain("runtime slash command");
398-
expect(message).toContain("/dreaming");
399-
expect(message).toContain("memory-core");
400-
expect(message).toContain("openclaw memory");
401+
expect(message).toBe(
402+
'"dreaming" is a runtime slash command (/dreaming), not a CLI command. It is provided by the "memory-core" plugin. Use `openclaw memory` for related CLI operations, or `/dreaming` in a chat session.',
403+
);
401404
});
402405

403406
it("returns the runtime command message even when plugins.allow is set", () => {
@@ -428,9 +431,9 @@ describe("resolveMissingPluginCommandMessage", () => {
428431
registry: memoryCoreCommandAliasRegistry,
429432
},
430433
);
431-
expect(message).toContain('"dreaming" is not a plugin');
432-
expect(message).toContain('"memory-core"');
433-
expect(message).toContain("plugins.allow");
434+
expect(message).toBe(
435+
'"dreaming" is not a plugin; it is a command provided by the "memory-core" plugin. Add "memory-core" to `plugins.allow` instead of "dreaming".',
436+
);
434437
});
435438

436439
it("explains disabled-by-default parent plugins for CLI command aliases", () => {
@@ -461,10 +464,9 @@ describe("resolveMissingPluginCommandMessage", () => {
461464
{ registry: workboardCommandAliasRegistry },
462465
);
463466

464-
expect(message).toContain('"workboard" plugin');
465-
expect(message).toContain("disabled by default");
466-
expect(message).toContain("openclaw plugins enable workboard");
467-
expect(message).not.toContain("runtime slash command");
467+
expect(message).toBe(
468+
'The `openclaw workboard` command is provided by the "workboard" plugin, but that bundled plugin is disabled by default. Run `openclaw plugins enable workboard` to enable that CLI surface.',
469+
);
468470
});
469471

470472
it("returns null for CLI command aliases when disabled-by-default parent plugins are enabled", () => {
@@ -554,10 +556,9 @@ describe("resolveMissingPluginCommandMessage", () => {
554556
if (message === null) {
555557
throw new Error("expected missing plugin command message");
556558
}
557-
expect(message).toContain('"lcm_recent"');
558-
expect(message).toContain('"lossless-claw"');
559-
expect(message).toContain("agent tool");
560-
expect(message).not.toContain("plugins.allow");
559+
expect(message).toBe(
560+
'"lcm_recent" is an agent tool available from the "lossless-claw" plugin, not a CLI subcommand. Use it from an agent turn (model tool-use), not the CLI. Run `openclaw --help` to see available CLI subcommands.',
561+
);
561562
});
562563

563564
it("matches agent tool names case-insensitively", () => {
@@ -653,8 +654,8 @@ describe("resolveMissingPluginCommandMessage", () => {
653654
if (message === null) {
654655
throw new Error("expected missing plugin command message");
655656
}
656-
expect(message).toContain("may be provided by");
657-
expect(message).toContain('"feishu"');
658-
expect(message).not.toContain("registered by");
657+
expect(message).toBe(
658+
'"feishu_chat" may be provided by the "feishu" plugin as an agent tool, not a CLI subcommand. Run `openclaw --help` to see available CLI subcommands.',
659+
);
659660
});
660661
});

0 commit comments

Comments
 (0)