Skip to content

Commit e0fcb85

Browse files
author
Peter Steinberger
committed
fix(plugins): fail closed on ambiguous uninstall targets
1 parent 2d545c1 commit e0fcb85

4 files changed

Lines changed: 252 additions & 14 deletions

File tree

‎src/cli/plugins-cli.uninstall.test.ts‎

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,98 @@ describe("plugins cli uninstall", () => {
217217
});
218218
});
219219

220+
it("uninstalls the exact plugin id when an earlier plugin uses it as a display name", async () => {
221+
const baseConfig = {
222+
plugins: {
223+
entries: {
224+
"unrelated-plugin": { enabled: true },
225+
calendar: { enabled: true },
226+
},
227+
installs: {
228+
"unrelated-plugin": { source: "npm", spec: "[email protected]" },
229+
calendar: { source: "npm", spec: "[email protected]" },
230+
},
231+
},
232+
} as OpenClawConfig;
233+
const nextConfig = {
234+
plugins: {
235+
entries: { "unrelated-plugin": { enabled: true } },
236+
installs: {
237+
"unrelated-plugin": { source: "npm", spec: "[email protected]" },
238+
},
239+
},
240+
} as OpenClawConfig;
241+
242+
loadConfig.mockReturnValue(baseConfig);
243+
setInstalledPluginIndexInstallRecords(baseConfig.plugins?.installs ?? {});
244+
buildPluginSnapshotReport.mockReturnValue({
245+
plugins: [
246+
{ id: "unrelated-plugin", name: "calendar" },
247+
{ id: "calendar", name: "Real Calendar" },
248+
],
249+
diagnostics: [],
250+
});
251+
planPluginUninstall.mockReturnValue({
252+
ok: true,
253+
config: nextConfig,
254+
actions: {
255+
entry: true,
256+
install: true,
257+
allowlist: false,
258+
denylist: false,
259+
loadPath: false,
260+
memorySlot: false,
261+
contextEngineSlot: false,
262+
directory: false,
263+
},
264+
directoryRemoval: null,
265+
});
266+
267+
await runPluginsCommand(["plugins", "uninstall", "calendar", "--force", "--keep-files"]);
268+
269+
expectLatestUninstallPlanParams({ pluginId: "calendar", deleteFiles: false });
270+
expect(writePersistedInstalledPluginIndexInstallRecords).toHaveBeenCalledWith({
271+
"unrelated-plugin": { source: "npm", spec: "[email protected]" },
272+
});
273+
});
274+
275+
it("rejects an ambiguous display name before planning or mutating installed plugins", async () => {
276+
const baseConfig = {
277+
plugins: {
278+
entries: {
279+
"calendar-one": { enabled: true },
280+
"calendar-two": { enabled: true },
281+
},
282+
installs: {
283+
"calendar-one": { source: "npm", spec: "[email protected]" },
284+
"calendar-two": { source: "npm", spec: "[email protected]" },
285+
},
286+
},
287+
} as OpenClawConfig;
288+
289+
loadConfig.mockReturnValue(baseConfig);
290+
setInstalledPluginIndexInstallRecords(baseConfig.plugins?.installs ?? {});
291+
buildPluginSnapshotReport.mockReturnValue({
292+
plugins: [
293+
{ id: "calendar-one", name: "calendar" },
294+
{ id: "calendar-two", name: "calendar" },
295+
],
296+
diagnostics: [],
297+
});
298+
299+
await expect(
300+
runPluginsCommand(["plugins", "uninstall", "calendar", "--force"]),
301+
).rejects.toThrow("__exit__:1");
302+
303+
expect(runtimeErrors.at(-1)).toContain('Plugin uninstall target "calendar" is ambiguous');
304+
expect(planPluginUninstall).not.toHaveBeenCalled();
305+
expect(promptYesNo).not.toHaveBeenCalled();
306+
expect(applyPluginUninstallDirectoryRemoval).not.toHaveBeenCalled();
307+
expect(writePersistedInstalledPluginIndexInstallRecords).not.toHaveBeenCalled();
308+
expect(writeConfigFile).not.toHaveBeenCalled();
309+
expect(refreshPluginRegistry).not.toHaveBeenCalled();
310+
});
311+
220312
it("warns but proceeds when a shared plugin has an uncertain Claw reference", async () => {
221313
const previousStateDir = process.env.OPENCLAW_STATE_DIR;
222314
process.env.OPENCLAW_STATE_DIR = tempDirs.make("openclaw-claw-plugin-ref-");

‎src/cli/plugins-uninstall-command.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,17 @@ async function runPluginUninstallCommandUnlocked(
114114
runtime.log(theme.warn("`--keep-config` is deprecated, use `--keep-files`."));
115115
}
116116

117-
const { plugin, pluginId } = resolvePluginUninstallId({
117+
const selection = resolvePluginUninstallId({
118118
rawId: id,
119119
config: cfg,
120120
plugins: report.plugins,
121121
});
122+
if (!selection.ok) {
123+
runtime.error(selection.error);
124+
runtime.exit(1);
125+
return;
126+
}
127+
const { plugin, pluginId } = selection.value;
122128
const channelIds = plugin?.status === "loaded" ? plugin.channelIds : undefined;
123129
const initialPlan = planPluginUninstall({
124130
config: cfg,

‎src/cli/plugins-uninstall-selection.test.ts‎

Lines changed: 120 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,118 @@ import type { OpenClawConfig } from "../config/config.js";
44
import { resolvePluginUninstallId } from "./plugins-uninstall-selection.js";
55

66
describe("resolvePluginUninstallId", () => {
7+
it("prefers an exact plugin id over an earlier plugin display name", () => {
8+
const alias = { id: "unrelated-plugin", name: "calendar" };
9+
const target = { id: "calendar", name: "Real Calendar" };
10+
11+
const result = resolvePluginUninstallId({
12+
rawId: "calendar",
13+
config: {},
14+
plugins: [alias, target],
15+
});
16+
17+
expect(result).toEqual({ ok: true, value: { pluginId: "calendar", plugin: target } });
18+
});
19+
20+
it.each([
21+
{ label: "an installed plugin", plugins: { installs: { calendar: { source: "npm" } } } },
22+
{ label: "a stale plugin entry", plugins: { entries: { calendar: { enabled: true } } } },
23+
{ label: "an allowlist entry", plugins: { allow: ["calendar"] } },
24+
{ label: "a denylist entry", plugins: { deny: ["calendar"] } },
25+
{ label: "the memory slot", plugins: { slots: { memory: "calendar" } } },
26+
{ label: "the context-engine slot", plugins: { slots: { contextEngine: "calendar" } } },
27+
])("prefers the exact id recorded by $label over a display-name alias", ({ plugins }) => {
28+
const alias = { id: "unrelated-plugin", name: "calendar" };
29+
30+
const result = resolvePluginUninstallId({
31+
rawId: "calendar",
32+
config: { plugins } as OpenClawConfig,
33+
plugins: [alias],
34+
});
35+
36+
expect(result).toEqual({ ok: true, value: { pluginId: "calendar" } });
37+
});
38+
39+
it("rejects an ambiguous plugin display name", () => {
40+
const result = resolvePluginUninstallId({
41+
rawId: "calendar",
42+
config: {},
43+
plugins: [
44+
{ id: "calendar-one", name: "calendar" },
45+
{ id: "calendar-two", name: "calendar" },
46+
],
47+
});
48+
49+
expect(result).toEqual({
50+
ok: false,
51+
error:
52+
'Plugin uninstall target "calendar" is ambiguous; matches: calendar-one, calendar-two. Use an exact plugin id.',
53+
});
54+
});
55+
56+
it("rejects a package alias shared by multiple installed plugins", () => {
57+
const result = resolvePluginUninstallId({
58+
rawId: "@scope/calendar",
59+
config: {
60+
plugins: {
61+
installs: {
62+
"calendar-one": { source: "npm", resolvedName: "@scope/calendar" },
63+
"calendar-two": { source: "npm", resolvedName: "@scope/calendar" },
64+
},
65+
},
66+
} as OpenClawConfig,
67+
plugins: [],
68+
});
69+
70+
expect(result).toEqual({
71+
ok: false,
72+
error:
73+
'Plugin uninstall target "@scope/calendar" is ambiguous; matches: calendar-one, calendar-two. Use an exact plugin id.',
74+
});
75+
});
76+
77+
it("rejects a ClawHub alias shared by multiple installed plugins", () => {
78+
const result = resolvePluginUninstallId({
79+
rawId: "clawhub:calendar",
80+
config: {
81+
plugins: {
82+
installs: {
83+
"calendar-one": { source: "npm", spec: "clawhub:[email protected]" },
84+
"calendar-two": { source: "npm", clawhubPackage: "calendar" },
85+
},
86+
},
87+
} as OpenClawConfig,
88+
plugins: [],
89+
});
90+
91+
expect(result).toEqual({
92+
ok: false,
93+
error:
94+
'Plugin uninstall target "clawhub:calendar" is ambiguous; matches: calendar-one, calendar-two. Use an exact plugin id.',
95+
});
96+
});
97+
98+
it("deduplicates display and package aliases owned by the same plugin", () => {
99+
const plugin = { id: "calendar-owner", name: "calendar" };
100+
101+
const result = resolvePluginUninstallId({
102+
rawId: "calendar",
103+
config: {
104+
plugins: {
105+
installs: {
106+
"calendar-owner": { source: "npm", resolvedName: "calendar" },
107+
},
108+
},
109+
} as OpenClawConfig,
110+
plugins: [plugin],
111+
});
112+
113+
expect(result).toEqual({
114+
ok: true,
115+
value: { pluginId: "calendar-owner", plugin },
116+
});
117+
});
118+
7119
it("accepts the recorded ClawHub spec as an uninstall target", () => {
8120
const plugin = {
9121
id: "linkmind-context",
@@ -29,8 +141,10 @@ describe("resolvePluginUninstallId", () => {
29141
plugins: [plugin],
30142
});
31143

32-
expect(result.pluginId).toBe("linkmind-context");
33-
expect(result.plugin).toBe(plugin);
144+
expect(result).toEqual({
145+
ok: true,
146+
value: { pluginId: "linkmind-context", plugin },
147+
});
34148
});
35149

36150
it("accepts a versionless ClawHub spec when the install was pinned", () => {
@@ -57,7 +171,9 @@ describe("resolvePluginUninstallId", () => {
57171
plugins: [plugin],
58172
});
59173

60-
expect(result.pluginId).toBe("linkmind-context");
61-
expect(result.plugin).toBe(plugin);
174+
expect(result).toEqual({
175+
ok: true,
176+
value: { pluginId: "linkmind-context", plugin },
177+
});
62178
});
63179
});
Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// Plugin uninstall id resolver for registry ids, display names, npm specs, and ClawHub specs.
2+
import { err as resultError, ok, type Result } from "@openclaw/normalization-core/result";
23
import type { OpenClawConfig } from "../config/types.openclaw.js";
34
import { parseClawHubPluginSpec } from "../infra/clawhub-spec.js";
45
import type { PluginRecord } from "../plugins/registry.js";
@@ -10,40 +11,63 @@ export function resolvePluginUninstallId<
1011
rawId: string;
1112
config: OpenClawConfig;
1213
plugins: TPlugin[];
13-
}): { pluginId: string; plugin?: TPlugin } {
14+
}): Result<{ pluginId: string; plugin?: TPlugin }, string> {
1415
const rawId = params.rawId.trim();
16+
const pluginConfig = params.config.plugins;
17+
const installs = pluginConfig?.installs ?? {};
1518
const resolveInstalledPlugin = (pluginId: string) => {
1619
const plugin = params.plugins.find((entry) => entry.id === pluginId);
1720
return plugin ? { pluginId, plugin } : { pluginId };
1821
};
19-
const plugin = params.plugins.find((entry) => entry.id === rawId || entry.name === rawId);
20-
if (plugin) {
21-
return { pluginId: plugin.id, plugin };
22+
const exactPlugin = params.plugins.find((entry) => entry.id === rawId);
23+
if (exactPlugin) {
24+
return ok({ pluginId: exactPlugin.id, plugin: exactPlugin });
25+
}
26+
// Config records and stale policy references are canonical ids, never display-name aliases.
27+
if (
28+
Object.hasOwn(installs, rawId) ||
29+
Object.hasOwn(pluginConfig?.entries ?? {}, rawId) ||
30+
pluginConfig?.allow?.includes(rawId) ||
31+
pluginConfig?.deny?.includes(rawId) ||
32+
pluginConfig?.slots?.memory === rawId ||
33+
pluginConfig?.slots?.contextEngine === rawId
34+
) {
35+
return ok(resolveInstalledPlugin(rawId));
2236
}
2337

24-
for (const [pluginId, install] of Object.entries(params.config.plugins?.installs ?? {})) {
38+
const matchingPluginIds = new Set(
39+
params.plugins.filter((plugin) => plugin.name === rawId).map((plugin) => plugin.id),
40+
);
41+
for (const [pluginId, install] of Object.entries(installs)) {
2542
if (
2643
install.spec === rawId ||
2744
install.resolvedSpec === rawId ||
2845
install.resolvedName === rawId ||
2946
install.marketplacePlugin === rawId
3047
) {
31-
return resolveInstalledPlugin(pluginId);
48+
matchingPluginIds.add(pluginId);
3249
}
3350
}
3451

3552
const requestedClawHub = parseClawHubPluginSpec(rawId);
3653
if (requestedClawHub) {
37-
for (const [pluginId, install] of Object.entries(params.config.plugins?.installs ?? {})) {
54+
for (const [pluginId, install] of Object.entries(installs)) {
3855
const installedClawHubName =
3956
install.clawhubPackage ??
4057
parseClawHubPluginSpec(install.spec ?? "")?.name ??
4158
parseClawHubPluginSpec(install.resolvedSpec ?? "")?.name;
4259
if (installedClawHubName === requestedClawHub.name) {
43-
return resolveInstalledPlugin(pluginId);
60+
matchingPluginIds.add(pluginId);
4461
}
4562
}
4663
}
4764

48-
return { pluginId: rawId };
65+
if (matchingPluginIds.size > 1) {
66+
const matches = [...matchingPluginIds].toSorted().join(", ");
67+
return resultError(
68+
`Plugin uninstall target "${rawId}" is ambiguous; matches: ${matches}. Use an exact plugin id.`,
69+
);
70+
}
71+
const [matchedPluginId] = matchingPluginIds;
72+
return ok(resolveInstalledPlugin(matchedPluginId ?? rawId));
4973
}

0 commit comments

Comments
 (0)