fix(cli): handle closed plugin uninstall prompt

This commit is contained in:
ai-hpc
2026-04-28 12:52:25 +00:00
committed by Mason Huang
parent e6f5f5693d
commit beb56424f3
4 changed files with 145 additions and 7 deletions

View File

@@ -148,6 +148,60 @@ describe("plugins cli uninstall", () => {
});
});
it("exits cleanly when confirmation input closes before an answer", async () => {
const baseConfig = {
plugins: {
entries: {
alpha: { enabled: true },
},
installs: {
alpha: {
source: "path",
sourcePath: ALPHA_INSTALL_PATH,
installPath: ALPHA_INSTALL_PATH,
},
},
},
} as OpenClawConfig;
const promptClosedError = new Error("prompt closed");
promptClosedError.name = "PromptInputClosedError";
loadConfig.mockReturnValue(baseConfig);
setInstalledPluginIndexInstallRecords(baseConfig.plugins?.installs ?? {});
buildPluginDiagnosticsReport.mockReturnValue({
plugins: [{ id: "alpha", name: "alpha" }],
diagnostics: [],
});
planPluginUninstall.mockReturnValue({
ok: true,
config: { plugins: { entries: {}, installs: {} } } as OpenClawConfig,
actions: {
entry: true,
install: true,
allowlist: false,
denylist: false,
loadPath: false,
memorySlot: false,
contextEngineSlot: false,
directory: false,
},
directoryRemoval: null,
});
promptYesNo.mockRejectedValueOnce(promptClosedError);
await expect(runPluginsCommand(["plugins", "uninstall", "alpha"])).rejects.toThrow(
"__exit__:1",
);
expect(runtimeErrors).toContain(
"Error: plugins uninstall requires confirmation input. Re-run in an interactive TTY or pass --force.",
);
expect(writePersistedInstalledPluginIndexInstallRecords).not.toHaveBeenCalled();
expect(writeConfigFile).not.toHaveBeenCalled();
expect(refreshPluginRegistry).not.toHaveBeenCalled();
expect(applyPluginUninstallDirectoryRemoval).not.toHaveBeenCalled();
});
it("restores install records when the config write rejects during uninstall", async () => {
const installRecords = {
alpha: {

View File

@@ -19,6 +19,13 @@ export type PluginUninstallOptions = {
dryRun?: boolean;
};
function isPromptInputClosedError(
error: unknown,
PromptInputClosedError: typeof import("./prompt.js").PromptInputClosedError,
): error is InstanceType<typeof PromptInputClosedError> {
return error instanceof PromptInputClosedError;
}
export async function runPluginUninstallCommand(
id: string,
opts: PluginUninstallOptions = {},
@@ -44,7 +51,7 @@ export async function runPluginUninstallCommand(
const { refreshPluginRegistryAfterConfigMutation } =
await import("./plugins-registry-refresh.js");
const { resolvePluginUninstallId } = await import("./plugins-uninstall-selection.js");
const { promptYesNo } = await import("./prompt.js");
const { PromptInputClosedError, promptYesNo } = await import("./prompt.js");
const snapshot = await tracePluginLifecyclePhaseAsync(
"config read",
() => readConfigFileSnapshot(),
@@ -143,7 +150,19 @@ export async function runPluginUninstallCommand(
}
if (!opts.force) {
const confirmed = await promptYesNo(`Uninstall plugin "${pluginId}"?`);
let confirmed: boolean;
try {
confirmed = await promptYesNo(`Uninstall plugin "${pluginId}"?`);
} catch (error) {
if (isPromptInputClosedError(error, PromptInputClosedError)) {
runtime.error(
"Error: plugins uninstall requires confirmation input. Re-run in an interactive TTY or pass --force.",
);
runtime.exit(1);
return;
}
throw error;
}
if (!confirmed) {
runtime.log("Cancelled.");
return;

View File

@@ -1,13 +1,32 @@
import readline from "node:readline/promises";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { isYes, setVerbose, setYes } from "../globals.js";
import { promptYesNo } from "./prompt.js";
import { PromptInputClosedError, promptYesNo } from "./prompt.js";
const readlineState = vi.hoisted(() => {
const question = vi.fn(async () => "");
const close = vi.fn();
const createInterface = vi.fn(() => ({ question, close }));
return { question, close, createInterface };
const listeners = new Map<string, Set<() => void>>();
const once = vi.fn((event: string, listener: () => void) => {
const current = listeners.get(event) ?? new Set<() => void>();
current.add(listener);
listeners.set(event, current);
});
const off = vi.fn((event: string, listener: () => void) => {
listeners.get(event)?.delete(listener);
});
const emit = (event: string) => {
const current = [...(listeners.get(event) ?? [])];
listeners.delete(event);
for (const listener of current) {
listener();
}
};
const resetListeners = () => {
listeners.clear();
};
const createInterface = vi.fn(() => ({ question, close, once, off }));
return { question, close, createInterface, emit, off, once, resetListeners };
});
vi.mock("node:readline/promises", () => ({
@@ -21,6 +40,9 @@ beforeEach(() => {
readlineState.question.mockResolvedValue("");
readlineState.close.mockClear();
readlineState.createInterface.mockClear();
readlineState.off.mockClear();
readlineState.once.mockClear();
readlineState.resetListeners();
});
describe("promptYesNo", () => {
@@ -48,4 +70,14 @@ describe("promptYesNo", () => {
const resultYes = await promptYesNo("Continue?", false);
expect(resultYes).toBe(true);
});
it("rejects when input closes before an answer is received", async () => {
readlineState.question.mockReturnValueOnce(new Promise<string>(() => undefined));
const result = promptYesNo("Continue?");
readlineState.emit("close");
await expect(result).rejects.toThrow(PromptInputClosedError);
expect(readlineState.close).toHaveBeenCalled();
});
});

View File

@@ -3,6 +3,36 @@ import readline from "node:readline/promises";
import { isVerbose, isYes } from "../globals.js";
import { normalizeLowercaseStringOrEmpty } from "../shared/string-coerce.js";
export class PromptInputClosedError extends Error {
constructor() {
super("Prompt input closed before an answer was received.");
this.name = "PromptInputClosedError";
}
}
type ReadlineInterface = ReturnType<typeof readline.createInterface>;
function questionUntilClose(rl: ReadlineInterface, question: string): Promise<string> {
return new Promise((resolve, reject) => {
let settled = false;
const finish = (complete: () => void) => {
if (settled) {
return;
}
settled = true;
rl.off("close", onClose);
complete();
};
const onClose = () => finish(() => reject(new PromptInputClosedError()));
rl.once("close", onClose);
void rl.question(question).then(
(answer) => finish(() => resolve(answer)),
(error: unknown) => finish(() => reject(error)),
);
});
}
export async function promptYesNo(question: string, defaultYes = false): Promise<boolean> {
// Simple Y/N prompt honoring global --yes and verbosity flags.
if (isVerbose() && isYes()) {
@@ -13,8 +43,11 @@ export async function promptYesNo(question: string, defaultYes = false): Promise
}
const rl = readline.createInterface({ input, output });
const suffix = defaultYes ? " [Y/n] " : " [y/N] ";
const answer = normalizeLowercaseStringOrEmpty(await rl.question(`${question}${suffix}`));
rl.close();
const answer = normalizeLowercaseStringOrEmpty(
await questionUntilClose(rl, `${question}${suffix}`).finally(() => {
rl.close();
}),
);
if (!answer) {
return defaultYes;
}