Skip to content

Commit 5a643e3

Browse files
authored
fix(plugins): keep packed entries consistent through package lifecycle (#121174)
* fix(plugins): own packed entry lifecycles by package Persist package ownership per runtime child, route lifecycle actions through one closed resolver, reconcile removed child policy during updates, and retain rollback generations until durable config/index commit. * fix(plugins): break uninstall policy import cycle * test(plugins): model package ownership in lifecycle fixtures
1 parent 016c5aa commit 5a643e3

60 files changed

Lines changed: 4060 additions & 1093 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎docs/cli/plugins.md‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -421,9 +421,11 @@ openclaw plugins uninstall <id> --keep-files
421421
openclaw plugins uninstall <id> --force
422422
```
423423

424-
`uninstall` removes plugin records from `plugins.entries`, the persisted plugin index, plugin allow/deny list entries, and any `plugins.load.paths` entry that exactly resolves to the recorded install path. Linked path installs also remove an exact entry for their recorded source path. Parent directories, child paths, prefix matches, and unrelated load paths are preserved. Unless `--keep-files` is set, uninstall also removes the tracked managed install directory, but only when it resolves inside OpenClaw's plugin extensions root. If the plugin currently owns the `memory` or `contextEngine` slot, that slot resets to its default (`memory-core` for memory, `legacy` for context engine).
424+
`uninstall` removes plugin records from `plugins.entries`, the persisted plugin index, plugin allow/deny list entries, and any `plugins.load.paths` entry that exactly resolves to the recorded install path. For a package with multiple child entries, any child id resolves to the package owner; uninstall removes every sibling's policy and slot/channel references, the one package install record, and the managed directory once. Linked path installs also remove an exact entry for their recorded source path. Parent directories, child paths, prefix matches, and unrelated load paths are preserved. Unless `--keep-files` is set, uninstall also removes the tracked managed install directory, but only when it resolves inside OpenClaw's plugin extensions root. If the plugin currently owns the `memory` or `contextEngine` slot, that slot resets to its default (`memory-core` for memory, `legacy` for context engine).
425425

426-
`uninstall` prints a preview of what will be removed, then prompts `Uninstall plugin "<id>"?` before making changes. Pass `--force` to skip the confirmation prompt (useful for scripts and non-interactive runs); without it, uninstall requires an interactive TTY. `--dry-run` prints the same preview and exits without prompting or changing anything.
426+
`uninstall` prints a preview of what will be removed. Multi-entry packages name the package owner and every affected child before prompting. Pass `--force` to skip the confirmation prompt (useful for scripts and non-interactive runs); without it, uninstall requires an interactive TTY. `--dry-run` prints the same preview and exits without prompting or changing anything.
427+
428+
If OpenClaw cannot prove exactly one package owner and a complete child list, lifecycle mutations fail closed without changing package files, config, or the installed index. Run `openclaw plugins registry --refresh`, inspect `openclaw plugins doctor`, and use `openclaw doctor --fix` for repairable legacy index state. If ownership is still ambiguous, reinstall the package before retrying update or uninstall.
427429

428430
<Note>
429431
`--keep-config` is supported as a deprecated alias for `--keep-files`.
@@ -445,7 +447,7 @@ Updates apply to tracked plugin installs in the managed plugin index and tracked
445447

446448
<AccordionGroup>
447449
<Accordion title="Resolving plugin id vs npm spec">
448-
When you pass a plugin id, OpenClaw reuses the recorded install spec for that plugin. That means previously stored dist-tags such as `@beta` and exact pinned versions continue to be used on later `update <id>` runs.
450+
When you pass a plugin id, OpenClaw reuses the recorded install spec for that plugin. For a multi-entry package, a child id resolves to its package owner and updates every sibling together. If the new package version removes or renames children, OpenClaw removes the retired children's entries, allow/deny policy, exact child load paths, channel config, and memory/context slot selections while preserving retained/new children and unrelated plugins. Previously stored dist-tags such as `@beta` and exact pinned versions continue to be used on later `update <id>` runs.
449451

450452
The narrow exception is a trusted official package completing a catalog-declared plugin id replacement. That update starts from the catalog package selector so the renamed manifest can replace the legacy id.
451453

‎docs/plugins/manage-plugins.md‎

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,12 @@ OpenClaw records the install but leaves the plugin disabled. Configure
143143
`plugins.entries.<id>.config`, then run `openclaw plugins enable <id>`. If an
144144
existing config entry is present but invalid, install fails without rewriting it.
145145

146+
A plugin package can expose multiple child entries. Installation tracks that
147+
package once, enables each ready child entry, and preserves any child that you
148+
explicitly disabled. Runtime policy remains child-addressable through
149+
`plugins.entries.<child-id>`, allow/deny lists, channel config, exact child load
150+
paths, and the `memory` and `contextEngine` slots.
151+
146152
## Restart and inspect
147153

148154
A running managed Gateway with config reload enabled restarts automatically
@@ -171,7 +177,17 @@ openclaw plugins update <plugin-id> --dry-run
171177

172178
Passing a plugin id reuses its tracked install spec: stored dist-tags
173179
(`@beta`) and exact pinned versions carry over to later `update <plugin-id>`
174-
runs.
180+
runs. For a multi-entry package, any child id resolves to the one tracked
181+
package install, so all siblings update together. Removed or renamed children
182+
have their stale entries, allow/deny policy, exact load paths, channel config,
183+
and memory/context slot selections reconciled before the new package/index
184+
state commits; retained/new children and unrelated plugins are preserved.
185+
186+
If OpenClaw cannot prove exactly one package owner and a complete child list,
187+
update and uninstall fail closed without changing package files, config, or the
188+
installed index. Run `openclaw plugins registry --refresh`, inspect
189+
`openclaw plugins doctor`, and use `openclaw doctor --fix` for repairable legacy
190+
index state. If the ambiguity remains, reinstall the package before retrying.
175191

176192
`openclaw plugins update --all` is the bulk maintenance path. It still
177193
respects ordinary tracked install specs, but trusted official OpenClaw
@@ -204,10 +220,12 @@ openclaw plugins uninstall <plugin-id>
204220
openclaw plugins uninstall <plugin-id> --keep-files
205221
```
206222

207-
Uninstall removes the plugin's config entry, persisted plugin index record,
208-
allow/deny list entries, and linked `plugins.load.paths` entries when
209-
applicable. The managed install directory is removed unless you pass
210-
`--keep-files`. A running managed Gateway restarts automatically when the
223+
Uninstall removes the package's persisted install record and every owned child
224+
entry from plugin config, allow/deny lists, memory/context slots, exact linked
225+
`plugins.load.paths`, and channel config entries when applicable. You may address a multi-entry
226+
package by any child id; the preview names the package owner and all siblings
227+
that will be removed. The managed install directory is removed once unless you
228+
pass `--keep-files`. A running managed Gateway restarts automatically when the
211229
uninstall changes plugin source.
212230

213231
In Nix mode (`OPENCLAW_NIX_MODE=1`), plugin install, update, uninstall,

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,38 @@ function writePlugin([dir, id, version, method, name]) {
4747
writePluginManifest(path.join(dir, "openclaw.plugin.json"), id);
4848
}
4949

50+
function writePluginPack([dir, id, version, entryList]) {
51+
for (const [value, label] of [
52+
[dir, "dir"],
53+
[id, "id"],
54+
[version, "version"],
55+
]) {
56+
requireArg(value, label);
57+
}
58+
const entries = entryList
59+
? entryList
60+
.split(",")
61+
.map((entry) => entry.trim())
62+
.filter(Boolean)
63+
: ["one", "two"];
64+
if (entries.length === 0) {
65+
throw new Error("plugin-pack entries must not be empty");
66+
}
67+
writeJson(path.join(dir, "package.json"), {
68+
name: `@openclaw/${id}`,
69+
version,
70+
openclaw: { extensions: entries.map((entry) => `./${entry}.js`) },
71+
});
72+
for (const entry of entries) {
73+
const childId = `${id}/${entry}`;
74+
write(
75+
path.join(dir, `${entry}.js`),
76+
`module.exports = { id: ${JSON.stringify(childId)}, name: ${JSON.stringify(childId)}, register(api) { api.registerGatewayMethod(${JSON.stringify(`${id}.${entry}`)}, async () => ({ version: ${JSON.stringify(version)} })); }, };\n`,
77+
);
78+
}
79+
writePluginManifest(path.join(dir, "openclaw.plugin.json"), id);
80+
}
81+
5082
function writePluginWithVendoredDependency([dir, id, version, method, name]) {
5183
writePlugin([dir, id, version, method, name]);
5284
const packageJsonPath = path.join(dir, "package.json");
@@ -162,6 +194,7 @@ function writePluginMarketplace(args) {
162194
export const pluginCommands = {
163195
"plugin-demo": writePluginDemo,
164196
plugin: writePlugin,
197+
"plugin-pack": writePluginPack,
165198
"plugin-vendored-dep": writePluginWithVendoredDependency,
166199
"plugin-cli": writePluginWithCli,
167200
"plugin-cli-registry-dep": writePluginWithCliRegistryDependency,

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

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type { HookInstallRecord } from "../config/types.hooks.js";
77
import type { OpenClawConfig } from "../config/types.openclaw.js";
88
import type { PluginInstallRecord } from "../config/types.plugins.js";
99
import type { InstalledPluginIndex } from "../plugins/installed-plugin-index.js";
10+
import { recordPluginManifestInstallOwner } from "../plugins/manifest-install-owner.js";
1011
import type { CliMockOutputRuntime } from "./test-runtime-capture.js";
1112

1213
type UnknownMock = Mock<(...args: unknown[]) => unknown>;
@@ -69,6 +70,7 @@ function clonePluginInstallRecords(records: PluginInstallRecordMap): PluginInsta
6970
export function createTestInstalledPluginIndex(params: {
7071
policyHash: string;
7172
installRecords: PluginInstallRecordMap;
73+
plugins?: InstalledPluginIndex["plugins"];
7274
}): InstalledPluginIndex {
7375
return {
7476
version: 1,
@@ -79,7 +81,7 @@ export function createTestInstalledPluginIndex(params: {
7981
generatedAtMs: 0,
8082
refreshReason: "source-changed",
8183
installRecords: clonePluginInstallRecords(params.installRecords),
82-
plugins: [],
84+
plugins: params.plugins ?? [],
8385
diagnostics: [],
8486
};
8587
}
@@ -955,9 +957,30 @@ export function resetPluginsCliTestState() {
955957
return true;
956958
},
957959
);
958-
loadPluginManifestRegistryMock.mockReturnValue({
959-
plugins: [],
960-
diagnostics: [],
960+
loadPluginManifestRegistryMock.mockImplementation((input: unknown) => {
961+
const installRecords =
962+
(input as { installRecords?: PluginInstallRecordMap } | undefined)?.installRecords ?? {};
963+
return {
964+
plugins: Object.entries(installRecords).map(([pluginId, record]) => {
965+
const rootDir = record.installPath ?? record.sourcePath ?? `/tmp/${pluginId}`;
966+
return recordPluginManifestInstallOwner(
967+
{
968+
id: pluginId,
969+
channels: [],
970+
providers: [],
971+
cliBackends: [],
972+
skills: [],
973+
hooks: [],
974+
origin: "global",
975+
rootDir,
976+
source: `${rootDir}/index.js`,
977+
manifestPath: `${rootDir}/openclaw.plugin.json`,
978+
},
979+
pluginId,
980+
);
981+
}),
982+
diagnostics: [],
983+
};
961984
});
962985
const defaultPluginReport = {
963986
plugins: [],

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

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { installedPluginRoot } from "openclaw/plugin-sdk/test-fixtures";
66
import { afterEach, beforeEach, describe, expect, it } from "vitest";
77
import type { OpenClawConfig } from "../config/config.js";
88
import { hashConfigIncludeRaw } from "../config/includes.js";
9+
import { recordPluginManifestInstallOwner } from "../plugins/manifest-install-owner.js";
910
import {
1011
listOfficialExternalPluginCatalogEntries,
1112
resolveOfficialExternalPluginId,
@@ -1268,27 +1269,30 @@ describe("plugins cli install", () => {
12681269
marketplaceSource: "local/repo",
12691270
marketplacePlugin: "alpha",
12701271
});
1271-
enablePluginInConfigMock.mockReturnValue({ config: enabledCfg });
1272+
enablePluginInConfigMock.mockReturnValue({ config: enabledCfg, enabled: true });
12721273
buildPluginSnapshotReportMock.mockReturnValue({
12731274
plugins: [{ id: "alpha", kind: "provider" }],
12741275
diagnostics: [],
12751276
});
12761277
const alphaRoot = cliInstallPath("alpha");
12771278
loadPluginManifestRegistryMock.mockReturnValue({
12781279
plugins: [
1279-
{
1280-
id: "alpha",
1281-
kind: "memory",
1282-
origin: "global",
1283-
channels: [],
1284-
providers: [],
1285-
cliBackends: [],
1286-
skills: [],
1287-
hooks: [],
1288-
rootDir: alphaRoot,
1289-
source: `${alphaRoot}/index.js`,
1290-
manifestPath: `${alphaRoot}/openclaw.plugin.json`,
1291-
},
1280+
recordPluginManifestInstallOwner(
1281+
{
1282+
id: "alpha",
1283+
kind: "memory",
1284+
origin: "global",
1285+
channels: [],
1286+
providers: [],
1287+
cliBackends: [],
1288+
skills: [],
1289+
hooks: [],
1290+
rootDir: alphaRoot,
1291+
source: `${alphaRoot}/index.js`,
1292+
manifestPath: `${alphaRoot}/openclaw.plugin.json`,
1293+
},
1294+
"alpha",
1295+
),
12921296
],
12931297
diagnostics: [],
12941298
});

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

Lines changed: 49 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,9 @@ describe("plugins cli uninstall", () => {
150150
plugins: [{ id: "alpha", name: "alpha" }],
151151
diagnostics: [],
152152
});
153+
setInstalledPluginIndexInstallRecords({
154+
alpha: { source: "path", sourcePath: ALPHA_INSTALL_PATH, installPath: ALPHA_INSTALL_PATH },
155+
});
153156
primeUninstallPlan({} as OpenClawConfig, { actions: { contextEngineSlot: true } });
154157

155158
await runPluginsCommand(["plugins", "uninstall", "alpha", "--dry-run"]);
@@ -564,71 +567,77 @@ describe("plugins cli uninstall", () => {
564567
expect(refreshPluginRegistryMock).not.toHaveBeenCalled();
565568
});
566569

567-
it("cleans stale policy refs even when plugin is absent from the current registry", async () => {
570+
it("rejects stale child-keyed records that claim one package path", async () => {
571+
const sharedPath = "/tmp/openclaw-ambiguous-uninstall-pack";
572+
const installRecords = {
573+
"pack/one": {
574+
source: "npm" as const,
575+
spec: "@acme/pack",
576+
installPath: sharedPath,
577+
},
578+
"pack/two": {
579+
source: "npm" as const,
580+
spec: "@acme/pack",
581+
installPath: sharedPath,
582+
},
583+
};
584+
const config = {} as OpenClawConfig;
585+
pluginCliConfigMock.mockReturnValue(config);
586+
setInstalledPluginIndexInstallRecords(installRecords);
587+
buildPluginSnapshotReportMock.mockReturnValue({
588+
plugins: [{ id: "pack/one", name: "pack/one" }],
589+
diagnostics: [],
590+
});
591+
592+
await expect(
593+
runPluginsCommand(["plugins", "uninstall", "pack/one", "--force"]),
594+
).rejects.toThrow("__exit__:1");
595+
596+
expect(runtimeErrors.at(-1)).toContain('Plugin "pack/one"');
597+
expect(planPluginUninstallMock).not.toHaveBeenCalled();
598+
expect(configWriteMock).not.toHaveBeenCalled();
599+
});
600+
601+
it("fails closed for stale policy refs without authoritative installed children", async () => {
568602
const baseConfig = {
569603
plugins: {
570604
allow: ["alpha", "beta"],
571605
deny: ["alpha"],
572606
},
573607
} as OpenClawConfig;
574-
const nextConfig = {
575-
plugins: {
576-
allow: ["beta"],
577-
},
578-
} as OpenClawConfig;
579-
580608
pluginCliConfigMock.mockReturnValue(baseConfig);
581609
buildPluginSnapshotReportMock.mockReturnValue({
582610
plugins: [],
583611
diagnostics: [],
584612
});
585-
primeUninstallPlan(nextConfig, {
586-
actions: {
587-
entry: false,
588-
install: false,
589-
allowlist: true,
590-
denylist: true,
591-
channelConfig: false,
592-
},
593-
});
594-
595-
await runPluginsCommand(["plugins", "uninstall", "alpha", "--force"]);
596613

597-
expectLatestUninstallPlanParams({ pluginId: "alpha", deleteFiles: true });
598-
expect(configWriteMock).toHaveBeenCalledWith(nextConfig);
599-
expect(pluginsCliRuntimeLogs.at(-2)).toContain('Uninstalled plugin "alpha"');
614+
await expect(runPluginsCommand(["plugins", "uninstall", "alpha", "--force"])).rejects.toThrow(
615+
"__exit__:1",
616+
);
617+
expect(planPluginUninstallMock).not.toHaveBeenCalled();
618+
expect(configWriteMock).not.toHaveBeenCalled();
600619
});
601620

602-
it("uninstalls stale enabled entries when plugin is absent from the current registry", async () => {
621+
it("fails closed for stale enabled entries without authoritative installed children", async () => {
603622
const baseConfig = {
604623
plugins: {
605624
entries: {
606625
alpha: { enabled: true },
607626
},
608627
},
609628
} as OpenClawConfig;
610-
const nextConfig = {} as OpenClawConfig;
611-
612629
pluginCliConfigMock.mockReturnValue(baseConfig);
613630
buildPluginSnapshotReportMock.mockReturnValue({
614631
plugins: [],
615632
diagnostics: [],
616633
});
617-
primeUninstallPlan(nextConfig, {
618-
actions: { install: false, channelConfig: false },
619-
});
620-
621-
await runPluginsCommand(["plugins", "uninstall", "alpha", "--force"]);
622634

623-
expectLatestUninstallPlanParams({ pluginId: "alpha", deleteFiles: true });
624-
expect(configWriteMock).toHaveBeenCalledWith(nextConfig);
625-
expect(refreshPluginRegistryMock).toHaveBeenCalledWith({
626-
config: nextConfig,
627-
installRecords: {},
628-
reason: "source-changed",
629-
});
630-
expect(runtimeErrors).not.toContain("Plugin not found: alpha");
631-
expect(pluginsCliRuntimeLogs.at(-2)).toContain('Uninstalled plugin "alpha"');
635+
await expect(runPluginsCommand(["plugins", "uninstall", "alpha", "--force"])).rejects.toThrow(
636+
"__exit__:1",
637+
);
638+
expect(planPluginUninstallMock).not.toHaveBeenCalled();
639+
expect(configWriteMock).not.toHaveBeenCalled();
640+
expect(refreshPluginRegistryMock).not.toHaveBeenCalled();
632641
});
633642

634643
it.each([
@@ -782,7 +791,7 @@ describe("plugins cli uninstall", () => {
782791
"__exit__:1",
783792
);
784793

785-
expect(runtimeErrors.at(-1)).toContain("is not managed by plugins config/install records");
786-
expect(planPluginUninstallMock).toHaveBeenCalledTimes(1);
794+
expect(runtimeErrors.at(-1)).toContain("is not associated with a tracked package install");
795+
expect(planPluginUninstallMock).not.toHaveBeenCalled();
787796
});
788797
});

0 commit comments

Comments
 (0)