Skip to content

Commit 9aa82b1

Browse files
MoerAIobviyus
andauthored
fix(plugins): recover orphan installs without weakening ownership
Closes #134321 ## What Problem This Solves Operators could not run `openclaw plugins update --all` or uninstall a path-source plugin after both tracked plugin paths disappeared. Registry refresh succeeded but left the durable install record behind, so both commands failed on the same ownership check. ## Why This Change Was Made Plugin lifecycle ownership now distinguishes an active package from an exact orphan record. Update and uninstall may act on an orphan only after duplicate-path and conflicting-plugin checks pass. Inspection, capability consent, active packages, and restored packages keep strict package ownership. Post-update reconciliation accepts an unchanged safe orphan while another package updates. If an orphan install record changes, the replacement must restore a strictly valid package before any update can commit. Uninstall removes owner-keyed plugin and channel policy plus the durable install record. Child policy remains based only on authoritative discovered children. ## User Impact Operators can update their remaining plugins and remove stale path-source install records without editing OpenClaw state by hand. Ambiguous, conflicting, and invalid replacement packages still fail closed. ## Evidence Current `main` at `77fee957919f06dbaff09dd5cc7f9664d9655651` failed through the real command-line flow after a local plugin was installed and both tracked paths were moved away: ```text $ openclaw plugins update --all --dry-run Plugin "orphan-proof" package owner "orphan-proof" has no authoritative runtime child list. Exit 1 $ openclaw plugins uninstall orphan-proof --force Plugin "orphan-proof" package owner "orphan-proof" has no authoritative runtime child list. Exit 1 $ openclaw plugins registry --refresh Plugin registry refreshed: 0/0 enabled plugins indexed. Exit 0 $ openclaw plugins update --all --dry-run Plugin "orphan-proof" package owner "orphan-proof" has no authoritative runtime child list. Exit 1 ``` Exact head `06de2f66734fd338e457f50a3060e2d625288274` passed the same flow: ```text $ openclaw plugins update --all --dry-run Skipping "orphan-proof" (source: path). Exit 0 $ openclaw plugins uninstall orphan-proof --force Uninstalled plugin "orphan-proof". Removed: config entry, install record. Exit 0 $ openclaw plugins update --all --dry-run No tracked plugins or hook packs to update. Exit 0 ``` The contributor head initially accepted a changed orphan install record with no authoritative children. The new owner-boundary regression failed with `{ ok: true }` before the maintainer repair and passes after it. Validation: - `node scripts/run-vitest.mjs src/plugins/manifest-registry-installed.test.ts src/plugins/management-service.uninstall-ownership.test.ts src/plugins/plugin-package-update.test.ts src/cli/plugins-cli.update.test.ts src/cli/plugins-cli.uninstall.test.ts` — 129 passed. - Targeted Oxfmt and Oxlint — passed. - Max-lines and assertion-safety ratchets — passed. - Coercion, deprecated-API, dead-export, native-schema, and import-cycle guards — passed. - `git diff --check` — passed. - Local type checking was not run because host policy requires CI for OpenClaw type checks. The proof fixture, isolated states, removed plugin copies, and proof manifest remain in the durable maintainer worktree. Co-authored-by: Ayaan Zaidi <[email protected]>
1 parent b1b010d commit 9aa82b1

10 files changed

Lines changed: 323 additions & 49 deletions

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

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -756,19 +756,34 @@ describe("plugins cli uninstall", () => {
756756
plugins: [],
757757
diagnostics: [],
758758
});
759-
primeUninstallPlan(nextConfig, { actions: { channelConfig: true } });
760-
761-
await runPluginsCommand(["plugins", "uninstall", "alpha", "--force", "--keep-files"]);
759+
const actualUninstall =
760+
await vi.importActual<typeof import("../plugins/uninstall.js")>("../plugins/uninstall.js");
761+
planPluginUninstallMock.mockImplementation((params) =>
762+
actualUninstall.planPluginUninstall(
763+
params as Parameters<typeof actualUninstall.planPluginUninstall>[0],
764+
),
765+
);
766+
const installedIndexModule = await import("../plugins/installed-plugin-index.js");
767+
const indexSpy = vi
768+
.spyOn(installedIndexModule, "loadInstalledPluginIndex")
769+
.mockReturnValue(
770+
createTestInstalledPluginIndex({ policyHash: "orphan-channel", installRecords }),
771+
);
772+
try {
773+
await runPluginsCommand(["plugins", "uninstall", "alpha", "--force", "--keep-files"]);
762774

763-
expectLatestUninstallPlanParams({
764-
pluginId: "alpha",
765-
channelIds: undefined,
766-
deleteFiles: false,
767-
});
768-
expectInstallRecordsWrittenWithLease({}, nextConfig);
769-
expect(configWriteMock).toHaveBeenCalledWith(nextConfig);
770-
expectRuntimeLogIncludes("channel config (channels.alpha)");
771-
expect(pluginsCliRuntimeLogs.at(-2)).toContain('Uninstalled plugin "alpha"');
775+
expectLatestUninstallPlanParams({
776+
pluginId: "alpha",
777+
channelIds: undefined,
778+
deleteFiles: false,
779+
});
780+
expectInstallRecordsWrittenWithLease({}, nextConfig);
781+
expect(configWriteMock).toHaveBeenCalledWith(nextConfig);
782+
expectRuntimeLogIncludes("channel config (channels.alpha)");
783+
expect(pluginsCliRuntimeLogs.at(-2)).toContain('Uninstalled plugin "alpha"');
784+
} finally {
785+
indexSpy.mockRestore();
786+
}
772787
});
773788

774789
it("exits when uninstall target is not managed by plugin install records", async () => {

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

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1266,7 +1266,7 @@ describe("plugins cli update", () => {
12661266
expect(configWriteMock).not.toHaveBeenCalled();
12671267
});
12681268

1269-
it("preserves skip behavior for plugin records whose source cannot be updated", async () => {
1269+
it("skips an exact orphan path record during bulk update", async () => {
12701270
const cfg = {
12711271
plugins: {
12721272
installs: {
@@ -1283,12 +1283,23 @@ describe("plugins cli update", () => {
12831283
primePluginUpdate(cfg, [
12841284
{ pluginId: "linked", status: "skipped", message: "Skipping linked." },
12851285
]);
1286+
const installedIndexModule = await import("../plugins/installed-plugin-index.js");
1287+
const indexSpy = vi.spyOn(installedIndexModule, "loadInstalledPluginIndex").mockReturnValue(
1288+
createTestInstalledPluginIndex({
1289+
policyHash: "orphan-path-update",
1290+
installRecords: cfg.plugins?.installs ?? {},
1291+
}),
1292+
);
1293+
try {
1294+
await runPluginsCommand(["plugins", "update", "--all"]);
12861295

1287-
await runPluginsCommand(["plugins", "update", "--all"]);
1288-
1289-
expect(updateNpmInstalledPluginsMock).toHaveBeenCalledOnce();
1290-
expect(updateNpmInstalledHookPacksMock).not.toHaveBeenCalled();
1291-
expect(configWriteMock).not.toHaveBeenCalled();
1296+
expect(runtimeErrors).toEqual([]);
1297+
expect(updateNpmInstalledPluginsMock).toHaveBeenCalledOnce();
1298+
expect(updateNpmInstalledHookPacksMock).not.toHaveBeenCalled();
1299+
expect(configWriteMock).not.toHaveBeenCalled();
1300+
} finally {
1301+
indexSpy.mockRestore();
1302+
}
12921303
});
12931304

12941305
it("preserves skip behavior for ClawHub records missing package metadata", async () => {

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

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ async function runPluginUninstallCommandUnlocked(
6565
}
6666

6767
const { loadInstalledPluginIndex } = await import("../plugins/installed-plugin-index.js");
68-
const { resolveInstalledPluginPackageOwnership } =
68+
const { resolveInstalledPluginLifecycleOwnership } =
6969
await import("../plugins/installed-plugin-package-ownership.js");
7070
const {
7171
loadInstalledPluginIndexInstallRecords,
@@ -131,23 +131,26 @@ async function runPluginUninstallCommandUnlocked(
131131
}
132132
const { plugin } = selection.value;
133133
const requestedPluginId = selection.value.pluginId;
134-
const ownership = resolveInstalledPluginPackageOwnership(installedIndex, requestedPluginId);
134+
const ownership = resolveInstalledPluginLifecycleOwnership(installedIndex, requestedPluginId);
135135
if (!ownership.ok) {
136136
runtime.error(ownership.error);
137137
runtime.exit(1);
138138
return;
139139
}
140140
const { installOwner: pluginId, pluginIds: ownedPluginIds } = ownership.value;
141-
const channelIds =
142-
ownedPluginIds.length === 1 && ownedPluginIds[0] === requestedPluginId
143-
? plugin?.channelIds
144-
: [
145-
...new Set(
146-
ownedPluginIds.flatMap(
147-
(entryId) => report.plugins.find((entry) => entry.id === entryId)?.channelIds ?? [],
148-
),
149-
),
150-
];
141+
const policyPluginIds = ownedPluginIds.length > 0 ? ownedPluginIds : [pluginId];
142+
let channelIds: string[] | undefined;
143+
if (ownedPluginIds.length === 1 && ownedPluginIds[0] === requestedPluginId) {
144+
channelIds = plugin?.channelIds;
145+
} else if (ownedPluginIds.length > 1) {
146+
channelIds = [
147+
...new Set(
148+
ownedPluginIds.flatMap(
149+
(entryId) => report.plugins.find((entry) => entry.id === entryId)?.channelIds ?? [],
150+
),
151+
),
152+
];
153+
}
151154
const initialPlan = planPluginUninstall(
152155
recordPluginPackageUninstallPlan(
153156
{
@@ -158,7 +161,7 @@ async function runPluginUninstallCommandUnlocked(
158161
extensionsDir,
159162
},
160163
{
161-
runtimePluginIds: ownedPluginIds,
164+
runtimePluginIds: policyPluginIds,
162165
runtimeLoadPaths: ownedPluginIds.flatMap(
163166
(entryId) => report.plugins.find((entry) => entry.id === entryId)?.source ?? [],
164167
),
@@ -278,7 +281,7 @@ async function runPluginUninstallCommandUnlocked(
278281
if (plan.directoryRemoval) {
279282
const disabledConfig = prepareConfigForPendingPluginDirectoryRemovalSet(
280283
sourceConfig,
281-
ownedPluginIds,
284+
policyPluginIds,
282285
);
283286
const disabledCommit = await tracePluginLifecyclePhaseAsync(
284287
"config disable",
@@ -321,7 +324,7 @@ async function runPluginUninstallCommandUnlocked(
321324
extensionsDir,
322325
},
323326
{
324-
runtimePluginIds: ownedPluginIds,
327+
runtimePluginIds: policyPluginIds,
325328
runtimeLoadPaths: ownedPluginIds.flatMap(
326329
(entryId) => report.plugins.find((entry) => entry.id === entryId)?.source ?? [],
327330
),

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ import {
4141
withPluginInstallRecords,
4242
} from "../plugins/installed-plugin-index-records.js";
4343
import { loadInstalledPluginIndex } from "../plugins/installed-plugin-index.js";
44-
import { resolveInstalledPluginPackageOwnership } from "../plugins/installed-plugin-package-ownership.js";
44+
import { resolveInstalledPluginLifecycleOwnership } from "../plugins/installed-plugin-package-ownership.js";
4545
import { configReferencesNpmInstallPath } from "../plugins/installs.js";
4646
import { withPluginLifecycleLease } from "../plugins/plugin-lifecycle-lease.js";
4747
import {
@@ -241,7 +241,7 @@ async function runPluginUpdateCommandUnlocked(params: RunPluginUpdateCommandPara
241241
...installedPluginIndex.plugins.map((plugin) => plugin.pluginId),
242242
...Object.keys(pluginInstallRecords),
243243
])) {
244-
const ownership = resolveInstalledPluginPackageOwnership(installedPluginIndex, pluginId);
244+
const ownership = resolveInstalledPluginLifecycleOwnership(installedPluginIndex, pluginId);
245245
if (!ownership.ok) {
246246
rejectedPluginIds.set(pluginId, ownership.error);
247247
continue;
@@ -283,7 +283,7 @@ async function runPluginUpdateCommandUnlocked(params: RunPluginUpdateCommandPara
283283
const packageUpdateSnapshot = packageUpdateSnapshotResult.value;
284284
const packagePluginIds = Object.fromEntries(
285285
pluginSelection.pluginIds.flatMap((pluginId) => {
286-
const ownership = resolveInstalledPluginPackageOwnership(installedPluginIndex, pluginId);
286+
const ownership = resolveInstalledPluginLifecycleOwnership(installedPluginIndex, pluginId);
287287
return ownership.ok ? [[ownership.value.installOwner, ownership.value.pluginIds]] : [];
288288
}),
289289
);

‎src/plugins/installed-plugin-package-ownership.ts‎

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,26 @@ function collectDuplicateInstallRecordOwners(
3838
export type InstalledPluginPackageOwnership = {
3939
installOwner: string;
4040
installRecord: InstalledPluginInstallRecordInfo;
41-
pluginIds: string[];
41+
pluginIds: [string, ...string[]];
4242
};
4343

44+
export type InstalledPluginLifecycleOwnership =
45+
| ({ kind: "package" } & InstalledPluginPackageOwnership)
46+
| {
47+
kind: "orphan";
48+
installOwner: string;
49+
installRecord: InstalledPluginInstallRecordInfo;
50+
pluginIds: [];
51+
};
52+
4453
type InstalledPluginPackageOwnershipResult =
4554
| { ok: true; value: InstalledPluginPackageOwnership }
4655
| { ok: false; error: string };
4756

57+
type InstalledPluginLifecycleOwnershipResult =
58+
| { ok: true; value: InstalledPluginLifecycleOwnership }
59+
| { ok: false; error: string };
60+
4861
function ownershipError(pluginId: string, detail: string): InstalledPluginPackageOwnershipResult {
4962
return {
5063
ok: false,
@@ -84,20 +97,21 @@ export function resolveInstalledPluginPackageOwnership(
8497
return ownershipError(pluginId, `shares package path ownership with "${installOwner}"`);
8598
}
8699

87-
const pluginIds = index.plugins
100+
const [firstPluginId, ...remainingPluginIds] = index.plugins
88101
.filter(
89102
(entry) =>
90103
resolveInstalledPluginIndexInstallOwner(entry) === installOwner &&
91104
!isInstalledPluginIndexInstallOwnerAmbiguous(entry),
92105
)
93106
.map((entry) => entry.pluginId)
94107
.toSorted();
95-
if (pluginIds.length === 0) {
108+
if (!firstPluginId) {
96109
return ownershipError(
97110
pluginId,
98111
`package owner "${installOwner}" has no authoritative runtime child list`,
99112
);
100113
}
114+
const pluginIds: [string, ...string[]] = [firstPluginId, ...remainingPluginIds];
101115
if (target && !pluginIds.includes(target.pluginId)) {
102116
return ownershipError(pluginId, `does not belong to package owner "${installOwner}"`);
103117
}
@@ -111,7 +125,43 @@ export function resolveInstalledPluginPackageOwnership(
111125
if (hasUnsafePackageEntry) {
112126
return ownershipError(pluginId, `package owner "${installOwner}" has conflicting child rows`);
113127
}
114-
return { ok: true, value: { installOwner, installRecord, pluginIds } };
128+
return {
129+
ok: true,
130+
value: { installOwner, installRecord, pluginIds },
131+
};
132+
}
133+
134+
export function resolveInstalledPluginLifecycleOwnership(
135+
index: InstalledPluginIndex,
136+
pluginId: string,
137+
env: NodeJS.ProcessEnv = process.env,
138+
): InstalledPluginLifecycleOwnershipResult {
139+
const ownership = resolveInstalledPluginPackageOwnership(index, pluginId, env);
140+
if (ownership.ok) {
141+
return { ok: true, value: { kind: "package", ...ownership.value } };
142+
}
143+
const installRecord = index.installRecords[pluginId];
144+
if (
145+
!Object.hasOwn(index.installRecords, pluginId) ||
146+
!installRecord ||
147+
collectDuplicateInstallRecordOwners(index, env).has(pluginId)
148+
) {
149+
return ownership;
150+
}
151+
const hasConflictingEntry = index.plugins.some(
152+
(entry) =>
153+
entry.pluginId === pluginId ||
154+
installRecordPathMatchesPluginRoot(installRecord, entry.rootDir, env),
155+
);
156+
if (hasConflictingEntry) {
157+
return ownership;
158+
}
159+
// Cleanup and pre-update planning may act on an exact durable tombstone.
160+
// Replacement reconciliation keeps using the strict package resolver.
161+
return {
162+
ok: true,
163+
value: { kind: "orphan", installOwner: pluginId, installRecord, pluginIds: [] },
164+
};
115165
}
116166

117167
function installRecordPathMatchesPluginRoot(

‎src/plugins/management-service.ts‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,10 @@ import {
9393
withoutPluginInstallRecords,
9494
} from "./installed-plugin-index-records.js";
9595
import { isInstalledPluginEnabled } from "./installed-plugin-index.js";
96-
import { resolveInstalledPluginPackageOwnership } from "./installed-plugin-package-ownership.js";
96+
import {
97+
resolveInstalledPluginLifecycleOwnership,
98+
resolveInstalledPluginPackageOwnership,
99+
} from "./installed-plugin-package-ownership.js";
97100
import { ManagedPluginLifecycleError } from "./management-lifecycle-error.js";
98101
import type { PluginManifestRecord } from "./manifest-registry.js";
99102
import type { PluginDiagnostic } from "./manifest-types.js";
@@ -2053,11 +2056,12 @@ export async function uninstallManagedPlugin(params: {
20532056
if (!record && !Object.hasOwn(installRecords, pluginId)) {
20542057
throw new ManagedPluginLifecycleError(`Plugin not found: ${pluginId}`);
20552058
}
2056-
const ownership = resolveInstalledPluginPackageOwnership(metadata.index, pluginId, env);
2059+
const ownership = resolveInstalledPluginLifecycleOwnership(metadata.index, pluginId, env);
20572060
if (!ownership.ok) {
20582061
throw new ManagedPluginLifecycleError(ownership.error);
20592062
}
20602063
const { installOwner, pluginIds: ownedPluginIds } = ownership.value;
2064+
const policyPluginIds = ownedPluginIds.length > 0 ? ownedPluginIds : [installOwner];
20612065
const ownedManifests = ownedPluginIds.flatMap((entryId) => {
20622066
const manifest = metadata.byPluginId.get(entryId);
20632067
return manifest ? [manifest] : [];
@@ -2077,7 +2081,7 @@ export async function uninstallManagedPlugin(params: {
20772081
extensionsDir,
20782082
},
20792083
{
2080-
runtimePluginIds: ownedPluginIds,
2084+
runtimePluginIds: policyPluginIds,
20812085
runtimeLoadPaths: ownedPluginIds.flatMap(
20822086
(entryId) => metadata.byPluginId.get(entryId)?.source ?? [],
20832087
),
@@ -2093,7 +2097,7 @@ export async function uninstallManagedPlugin(params: {
20932097
if (plan.directoryRemoval) {
20942098
const disabledConfig = prepareConfigForPendingPluginDirectoryRemovalSet(
20952099
snapshot.config,
2096-
ownedPluginIds,
2100+
policyPluginIds,
20972101
);
20982102
await replaceConfigFile({
20992103
nextConfig: disabledConfig,
@@ -2125,7 +2129,7 @@ export async function uninstallManagedPlugin(params: {
21252129
extensionsDir,
21262130
},
21272131
{
2128-
runtimePluginIds: ownedPluginIds,
2132+
runtimePluginIds: policyPluginIds,
21292133
runtimeLoadPaths: ownedPluginIds.flatMap(
21302134
(entryId) => metadata.byPluginId.get(entryId)?.source ?? [],
21312135
),

0 commit comments

Comments
 (0)