Skip to content

Commit 0fe49c5

Browse files
authored
fix(plugins): prevent uninstalled npm plugins from coming back (#113902)
* fix(plugins): preserve retained uninstall files * test(plugins): track retention temp dirs * fix(plugins): retain rollback marker snapshots
1 parent 75e2595 commit 0fe49c5

9 files changed

Lines changed: 458 additions & 21 deletions

‎scripts/e2e/lib/plugins/assertions.mjs‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,23 @@ function assertNpmPluginRemoved() {
749749
}
750750
}
751751

752+
function assertNpmPluginRetained() {
753+
const installPath = fs.readFileSync(scratchFile("plugins-npm-install-path.txt"), "utf8").trim();
754+
const dependencyPackagePath = fs
755+
.readFileSync(scratchFile("plugins-npm-dependency-path.txt"), "utf8")
756+
.trim();
757+
assertPluginRemoved({
758+
pluginId: "demo-plugin-npm",
759+
listFile: scratchFile("plugins-npm-retained.json"),
760+
});
761+
if (!fs.existsSync(installPath)) {
762+
throw new Error(`npm managed package was deleted by --keep-files: ${installPath}`);
763+
}
764+
if (!fs.existsSync(dependencyPackagePath)) {
765+
throw new Error(`npm managed dependency was deleted by --keep-files: ${dependencyPackagePath}`);
766+
}
767+
}
768+
752769
function assertInvalidOpenClawExtensionsRejected() {
753770
const pluginId = "demo-plugin-invalid-metadata";
754771
for (const expected of ["openclaw.extensions[1]", "non-empty string"]) {
@@ -1025,6 +1042,7 @@ const commands = {
10251042
"plugin-file-removed": assertPluginFileRemoved,
10261043
"plugin-npm": assertNpmPlugin,
10271044
"plugin-npm-update": assertNpmPluginUpdateUnchanged,
1045+
"plugin-npm-retained": assertNpmPluginRetained,
10281046
"plugin-npm-removed": assertNpmPluginRemoved,
10291047
"invalid-openclaw-extensions": assertInvalidOpenClawExtensionsRejected,
10301048
"bundle-disabled": assertClaudeBundleDisabled,

‎scripts/e2e/lib/plugins/sweep.sh‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,16 @@ node scripts/e2e/lib/plugins/assertions.mjs plugin-npm
223223
openclaw_e2e_maybe_timeout "$OPENCLAW_PLUGINS_CLI_TIMEOUT" node "$OPENCLAW_ENTRY" plugins update demo-plugin-npm >"$OPENCLAW_PLUGINS_TMP_DIR/plugins-npm-update.log" 2>&1
224224
node scripts/e2e/lib/plugins/assertions.mjs plugin-npm-update
225225

226+
run_plugins_openclaw_logged uninstall-npm-retained plugins uninstall demo-plugin-npm --force --keep-files
227+
run_plugins_openclaw_capture "$OPENCLAW_PLUGINS_TMP_DIR/plugins-npm-retained.json" plugins list --json
228+
node scripts/e2e/lib/plugins/assertions.mjs plugin-npm-retained
229+
230+
run_plugins_openclaw_logged reinstall-npm plugins install "npm:@openclaw/[email protected]" --force
231+
run_plugins_openclaw_capture "$OPENCLAW_PLUGINS_TMP_DIR/plugins-npm.json" plugins list --json
232+
run_plugins_openclaw_capture "$OPENCLAW_PLUGINS_TMP_DIR/plugins-npm-inspect.json" plugins inspect demo-plugin-npm --runtime --json
233+
run_plugins_shell_logged exec-reinstalled-npm-plugin-cli 'node "$OPENCLAW_ENTRY" demo-npm ping >"$OPENCLAW_PLUGINS_TMP_DIR/plugins-npm-cli.txt"'
234+
node scripts/e2e/lib/plugins/assertions.mjs plugin-npm
235+
226236
run_plugins_openclaw_logged uninstall-npm plugins uninstall demo-plugin-npm --force
227237
run_plugins_openclaw_capture "$OPENCLAW_PLUGINS_TMP_DIR/plugins-npm-uninstalled.json" plugins list --json
228238
node scripts/e2e/lib/plugins/assertions.mjs plugin-npm-removed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import fs from "node:fs";
2+
import { expect, it, vi } from "vitest";
3+
import { RETAINED_MANAGED_NPM_KEEP_FILES_REASON } from "../plugins/managed-npm-retention-contract.js";
4+
import {
5+
hasRetainedManagedNpmInstallMarker,
6+
markRetainedManagedNpmInstall,
7+
} from "../plugins/managed-npm-retention.js";
8+
import { writeManagedNpmPlugin } from "../plugins/test-helpers/managed-npm-plugin.js";
9+
import { withOpenClawTestState } from "../test-utils/openclaw-test-state.js";
10+
import { cleanupRetainedPluginInstallGenerations } from "./server-retained-plugin-cleanup.js";
11+
12+
it("preserves package files retained by plugin uninstall", async () => {
13+
await withOpenClawTestState({ label: "gateway-retained-plugin-cleanup" }, async (state) => {
14+
const packageDir = writeManagedNpmPlugin({
15+
stateDir: state.stateDir,
16+
packageName: "@openclaw/kept-plugin",
17+
pluginId: "kept-plugin",
18+
version: "1.0.0",
19+
});
20+
await markRetainedManagedNpmInstall({
21+
packageDir,
22+
pluginId: "kept-plugin",
23+
reason: RETAINED_MANAGED_NPM_KEEP_FILES_REASON,
24+
});
25+
const log = { info: vi.fn(), warn: vi.fn() };
26+
27+
await cleanupRetainedPluginInstallGenerations({ log });
28+
29+
expect(fs.existsSync(packageDir)).toBe(true);
30+
expect(hasRetainedManagedNpmInstallMarker(packageDir)).toBe(true);
31+
expect(log.info).not.toHaveBeenCalled();
32+
expect(log.warn).not.toHaveBeenCalled();
33+
});
34+
});
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
import fs from "node:fs";
2+
import path from "node:path";
3+
import { describe, expect, it } from "vitest";
4+
import type { PluginInstallRecord } from "../config/types.plugins.js";
5+
import { withOpenClawTestState } from "../test-utils/openclaw-test-state.js";
6+
import { commitPluginInstallRecordsWithConfig } from "./install-record-commit.js";
7+
import { listRecoveredManagedNpmInstallCandidates } from "./installed-plugin-index-record-reader.js";
8+
import {
9+
cleanupRetainedManagedNpmInstallGenerations,
10+
hasRetainedManagedNpmInstallMarker,
11+
} from "./managed-npm-retention.js";
12+
import { writeManagedNpmPlugin } from "./test-helpers/managed-npm-plugin.js";
13+
14+
function npmRecord(packageName: string, installPath: string): PluginInstallRecord {
15+
return { source: "npm", spec: `${packageName}@1.0.0`, installPath };
16+
}
17+
18+
describe("retained managed npm record commits", () => {
19+
it("suppresses recovery when a retained install record is removed", async () => {
20+
await withOpenClawTestState({ label: "retained-record-removal" }, async (state) => {
21+
const packageName = "@openclaw/retained-demo";
22+
const installPath = writeManagedNpmPlugin({
23+
stateDir: state.stateDir,
24+
packageName,
25+
pluginId: "retained-demo",
26+
version: "1.0.0",
27+
});
28+
expect(
29+
listRecoveredManagedNpmInstallCandidates({ stateDir: state.stateDir }).map(
30+
(candidate) => candidate.pluginId,
31+
),
32+
).toContain("retained-demo");
33+
34+
await commitPluginInstallRecordsWithConfig({
35+
previousInstallRecords: { "retained-demo": npmRecord(packageName, installPath) },
36+
nextInstallRecords: {},
37+
nextConfig: {},
38+
});
39+
40+
expect(hasRetainedManagedNpmInstallMarker(installPath)).toBe(true);
41+
expect(
42+
listRecoveredManagedNpmInstallCandidates({ stateDir: state.stateDir }).map(
43+
(candidate) => candidate.pluginId,
44+
),
45+
).not.toContain("retained-demo");
46+
});
47+
});
48+
49+
it.each(["direct", "symlink"] as const)(
50+
"does not retire a package still used by a %s active install path",
51+
async (activePathKind) => {
52+
await withOpenClawTestState({ label: `retained-active-${activePathKind}` }, async (state) => {
53+
const packageName = "@openclaw/retained-active";
54+
const installPath = writeManagedNpmPlugin({
55+
stateDir: state.stateDir,
56+
packageName,
57+
pluginId: "retained-active",
58+
version: "1.0.0",
59+
});
60+
let activePath = installPath;
61+
if (activePathKind === "symlink") {
62+
activePath = state.statePath("active", "retained-active");
63+
fs.mkdirSync(path.dirname(activePath), { recursive: true });
64+
fs.symlinkSync(installPath, activePath, "dir");
65+
}
66+
67+
await commitPluginInstallRecordsWithConfig({
68+
previousInstallRecords: { "retained-active": npmRecord(packageName, installPath) },
69+
nextInstallRecords: {
70+
"active-alias": {
71+
source: "path",
72+
sourcePath: activePath,
73+
installPath: activePath,
74+
},
75+
},
76+
nextConfig: {},
77+
});
78+
79+
expect(hasRetainedManagedNpmInstallMarker(installPath)).toBe(false);
80+
});
81+
},
82+
);
83+
84+
it("does not retire a removed npm record outside the managed npm root", async () => {
85+
await withOpenClawTestState({ label: "retained-outside-root" }, async (state) => {
86+
const outsideRoot = state.path("outside-root");
87+
try {
88+
const packageName = "@openclaw/outside-retained";
89+
const installPath = writeManagedNpmPlugin({
90+
stateDir: outsideRoot,
91+
packageName,
92+
pluginId: "outside-retained",
93+
version: "1.0.0",
94+
});
95+
await commitPluginInstallRecordsWithConfig({
96+
previousInstallRecords: { "outside-retained": npmRecord(packageName, installPath) },
97+
nextInstallRecords: {},
98+
nextConfig: {},
99+
});
100+
expect(hasRetainedManagedNpmInstallMarker(installPath)).toBe(false);
101+
} finally {
102+
fs.rmSync(outsideRoot, { recursive: true, force: true });
103+
}
104+
});
105+
});
106+
107+
it("keeps npm-to-local source changes cleanup-eligible", async () => {
108+
await withOpenClawTestState({ label: "retained-source-change" }, async (state) => {
109+
const packageName = "@openclaw/moved-local";
110+
const installPath = writeManagedNpmPlugin({
111+
stateDir: state.stateDir,
112+
packageName,
113+
pluginId: "moved-local",
114+
version: "1.0.0",
115+
});
116+
const localInstallPath = state.statePath("extensions", "moved-local");
117+
fs.mkdirSync(localInstallPath, { recursive: true });
118+
119+
await commitPluginInstallRecordsWithConfig({
120+
previousInstallRecords: { "moved-local": npmRecord(packageName, installPath) },
121+
nextInstallRecords: {
122+
"moved-local": {
123+
source: "path",
124+
sourcePath: localInstallPath,
125+
installPath: localInstallPath,
126+
},
127+
},
128+
nextConfig: {},
129+
});
130+
131+
expect(hasRetainedManagedNpmInstallMarker(installPath)).toBe(true);
132+
await expect(
133+
cleanupRetainedManagedNpmInstallGenerations({
134+
activeInstallPaths: [localInstallPath],
135+
}),
136+
).resolves.toBe(1);
137+
expect(fs.existsSync(installPath)).toBe(false);
138+
expect(fs.existsSync(localInstallPath)).toBe(true);
139+
});
140+
});
141+
});

‎src/plugins/install-record-commit.test.ts‎

Lines changed: 111 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
import fs from "node:fs";
33
import os from "node:os";
44
import path from "node:path";
5-
import { beforeEach, describe, expect, it, vi } from "vitest";
5+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
6+
import { useAutoCleanupTempDirTracker } from "../../test/helpers/temp-dir.js";
67
import {
78
createPluginInstallRecordMap,
89
getPluginInstallRecordMapEntry,
@@ -11,11 +12,16 @@ import {
1112
import type { OpenClawConfig } from "../config/types.openclaw.js";
1213
import type { PluginInstallRecord } from "../config/types.plugins.js";
1314
import { withEnvAsync } from "../test-utils/env.js";
15+
import { listRecoveredManagedNpmInstallCandidates } from "./installed-plugin-index-record-reader.js";
1416
import type { InstalledPluginIndex } from "./installed-plugin-index.js";
1517
import {
1618
hasRetainedManagedNpmInstallMarker,
1719
markRetainedManagedNpmInstall,
20+
resolveRetainedManagedNpmInstallMarkerPath,
1821
} from "./managed-npm-retention.js";
22+
import { writeManagedNpmPlugin } from "./test-helpers/managed-npm-plugin.js";
23+
24+
const retentionTempDirs = useAutoCleanupTempDirTracker(afterEach);
1925

2026
const mocks = vi.hoisted(() => {
2127
const lease = {
@@ -480,6 +486,49 @@ describe("commitConfigWithPendingPluginInstalls", () => {
480486
}
481487
});
482488

489+
it("removes a new retirement marker when the leased config commit rolls back", async () => {
490+
const stateDir = retentionTempDirs.make("openclaw-record-commit-");
491+
const installPath = writeManagedNpmPlugin({
492+
stateDir,
493+
packageName: "@openclaw/retained-rollback",
494+
pluginId: "retained-rollback",
495+
version: "1.0.0",
496+
});
497+
const previousInstallRecords: Record<string, PluginInstallRecord> = {
498+
"retained-rollback": {
499+
source: "npm",
500+
spec: "@openclaw/[email protected]",
501+
installPath,
502+
},
503+
};
504+
mocks.replaceConfigFile.mockRejectedValueOnce(new Error("config changed"));
505+
506+
try {
507+
await withEnvAsync({ OPENCLAW_STATE_DIR: stateDir }, async () => {
508+
await expect(
509+
commitPluginInstallRecordsWithConfig({
510+
previousInstallRecords,
511+
nextInstallRecords: {},
512+
nextConfig: {},
513+
}),
514+
).rejects.toThrow("config changed");
515+
516+
expect(hasRetainedManagedNpmInstallMarker(installPath)).toBe(false);
517+
expect(
518+
listRecoveredManagedNpmInstallCandidates({ stateDir }).map(
519+
(candidate) => candidate.pluginId,
520+
),
521+
).toContain("retained-rollback");
522+
expect(mocks.restorePersistedInstalledPluginIndexIfCurrent).toHaveBeenCalledWith(null, 1, {
523+
filePath: mocks.lease.databasePath,
524+
lease: mocks.lease,
525+
});
526+
});
527+
} finally {
528+
fs.rmSync(stateDir, { recursive: true, force: true });
529+
}
530+
});
531+
483532
it("does not mark arbitrary npm paths outside the managed npm root", async () => {
484533
const stateDir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-record-commit-"));
485534
const outsideRoot = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-record-outside-"));
@@ -805,6 +854,67 @@ describe("commitConfigWithPendingPluginInstalls", () => {
805854
}
806855
});
807856

857+
it("restores earlier active markers when clearing a later marker fails", async () => {
858+
const stateDir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-record-commit-"));
859+
const installPaths = ["codex", "voice-call"].map((pluginId) =>
860+
path.join(
861+
stateDir,
862+
"npm",
863+
"projects",
864+
`${pluginId}-v2`,
865+
"node_modules",
866+
"@openclaw",
867+
pluginId,
868+
),
869+
);
870+
for (const [index, installPath] of installPaths.entries()) {
871+
fs.mkdirSync(installPath, { recursive: true });
872+
await markRetainedManagedNpmInstall({
873+
packageDir: installPath,
874+
pluginId: index === 0 ? "codex" : "voice-call",
875+
retainedAt: "2026-04-25T00:00:00.000Z",
876+
reason: "test-retained-generation",
877+
});
878+
}
879+
const laterMarkerPath = resolveRetainedManagedNpmInstallMarkerPath(installPaths[1] ?? "");
880+
const realRm = fs.promises.rm.bind(fs.promises);
881+
const rmSpy = vi.spyOn(fs.promises, "rm").mockImplementation(async (target, options) => {
882+
if (String(target) === laterMarkerPath) {
883+
const error = new Error("marker clear failed") as NodeJS.ErrnoException;
884+
error.code = "EIO";
885+
throw error;
886+
}
887+
return await realRm(target, options);
888+
});
889+
890+
try {
891+
await expect(
892+
commitPluginInstallRecordsWithConfig({
893+
previousInstallRecords: {},
894+
nextInstallRecords: Object.fromEntries(
895+
installPaths.map((installPath, index) => {
896+
const pluginId = index === 0 ? "codex" : "voice-call";
897+
return [
898+
pluginId,
899+
{
900+
source: "npm",
901+
spec: `@openclaw/${pluginId}@2.0.0`,
902+
installPath,
903+
},
904+
];
905+
}),
906+
),
907+
nextConfig: {},
908+
}),
909+
).rejects.toThrow("marker clear failed");
910+
911+
expect(installPaths.every(hasRetainedManagedNpmInstallMarker)).toBe(true);
912+
} finally {
913+
rmSpy.mockRestore();
914+
fs.rmSync(stateDir, { recursive: true, force: true });
915+
}
916+
});
917+
808918
it("rolls back plugin index writes when the config write fails", async () => {
809919
const existingRecords: Record<string, PluginInstallRecord> = {
810920
existing: {

0 commit comments

Comments
 (0)