Skip to content

Commit 4c60b3b

Browse files
authored
fix(plugins): preserve bundled install ownership (#123063)
Punchcard-Session: ember-meadow-river-68
1 parent 3b6e540 commit 4c60b3b

2 files changed

Lines changed: 90 additions & 12 deletions

File tree

‎src/plugins/discovery.test.ts‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1380,6 +1380,65 @@ describe("discoverOpenClawPlugins", () => {
13801380
).toBe(true);
13811381
});
13821382

1383+
it("adds managed ownership to bundled candidates deduplicated in the shared scan", () => {
1384+
const stateDir = makeTempDir();
1385+
const bundledDir = path.join(stateDir, "bundled");
1386+
const plainDir = path.join(bundledDir, "plain");
1387+
const packageDir = path.join(bundledDir, "package");
1388+
mkdirSafe(plainDir);
1389+
mkdirSafe(packageDir);
1390+
writePluginManifest({ pluginDir: plainDir, id: "plain" });
1391+
writePluginEntry(path.join(plainDir, "index.js"));
1392+
writePluginPackageManifest({
1393+
packageDir,
1394+
packageName: "@openclaw/package",
1395+
extensions: ["./index.js"],
1396+
});
1397+
writePluginManifest({ pluginDir: packageDir, id: "package" });
1398+
writePluginEntry(path.join(packageDir, "index.js"));
1399+
const env = buildDiscoveryEnvWithOverrides(stateDir, {
1400+
OPENCLAW_BUNDLED_PLUGINS_DIR: bundledDir,
1401+
});
1402+
const installRecords = {
1403+
"plain-owner": { source: "path", installPath: plainDir },
1404+
"package-owner": { source: "path", installPath: packageDir },
1405+
} satisfies Record<string, PluginInstallRecord>;
1406+
1407+
const result = discoverOpenClawPlugins({ env, installRecords });
1408+
1409+
expectCandidateSource(result.candidates, "plain", path.join(plainDir, "index.js"));
1410+
expectCandidateFields(requireCandidateById(result.candidates, "plain"), {
1411+
origin: "bundled",
1412+
packageName: undefined,
1413+
installOwner: "plain-owner",
1414+
});
1415+
expectCandidateSource(result.candidates, "package", path.join(packageDir, "index.js"));
1416+
expectCandidateFields(requireCandidateById(result.candidates, "package"), {
1417+
origin: "bundled",
1418+
packageName: "@openclaw/package",
1419+
installOwner: "package-owner",
1420+
});
1421+
1422+
const ambiguous = discoverOpenClawPlugins({
1423+
env,
1424+
installRecords: {
1425+
...installRecords,
1426+
"other-owner": installRecords["plain-owner"],
1427+
},
1428+
});
1429+
1430+
expectCandidateFields(requireCandidateById(ambiguous.candidates, "plain"), {
1431+
origin: "bundled",
1432+
installOwner: undefined,
1433+
installOwnerAmbiguous: true,
1434+
});
1435+
expectDiagnostic({
1436+
diagnostics: ambiguous.diagnostics,
1437+
level: "error",
1438+
messageIncludes: "multiple plugin install records claim the same package path",
1439+
});
1440+
});
1441+
13831442
it("reuses one filesystem realpath lookup per package root within a discovery run", () => {
13841443
const stateDir = makeTempDir();
13851444
const packageDir = path.join(stateDir, "extensions", "pack");

‎src/plugins/discovery.ts‎

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,24 @@ function createDiscoveryResult(): PluginDiscoveryResult {
391391
};
392392
}
393393

394+
function mergeCandidateInstallOwner(
395+
existing: PluginCandidate,
396+
candidateOwner: string | undefined,
397+
candidateOwnerAmbiguous: boolean,
398+
): void {
399+
const existingOwner = resolvePluginCandidateInstallOwner(existing);
400+
const ownerConflict = existingOwner && candidateOwner && existingOwner !== candidateOwner;
401+
if (
402+
isPluginCandidateInstallOwnerAmbiguous(existing) ||
403+
candidateOwnerAmbiguous ||
404+
ownerConflict
405+
) {
406+
recordPluginCandidateInstallOwner(existing, undefined, true);
407+
} else if (candidateOwner) {
408+
recordPluginCandidateInstallOwner(existing, candidateOwner);
409+
}
410+
}
411+
394412
function mergeDiscoveryResult(
395413
target: PluginDiscoveryResult,
396414
source: PluginDiscoveryResult,
@@ -404,18 +422,11 @@ function mergeDiscoveryResult(
404422
const key = safeRealpathSync(candidate.source, realpathCache) ?? path.resolve(candidate.source);
405423
const existing = candidatesBySource.get(key);
406424
if (existing) {
407-
const existingOwner = resolvePluginCandidateInstallOwner(existing);
408-
const candidateOwner = resolvePluginCandidateInstallOwner(candidate);
409-
const ownerConflict = existingOwner && candidateOwner && existingOwner !== candidateOwner;
410-
if (
411-
isPluginCandidateInstallOwnerAmbiguous(existing) ||
412-
isPluginCandidateInstallOwnerAmbiguous(candidate) ||
413-
ownerConflict
414-
) {
415-
recordPluginCandidateInstallOwner(existing, undefined, true);
416-
} else if (candidateOwner) {
417-
recordPluginCandidateInstallOwner(existing, candidateOwner);
418-
}
425+
mergeCandidateInstallOwner(
426+
existing,
427+
resolvePluginCandidateInstallOwner(candidate),
428+
isPluginCandidateInstallOwnerAmbiguous(candidate),
429+
);
419430
continue;
420431
}
421432
candidatesBySource.set(key, candidate);
@@ -802,6 +813,14 @@ function addCandidate(params: {
802813
}) {
803814
const resolved = path.resolve(params.source);
804815
if (params.seen.has(resolved)) {
816+
const existing = params.candidates.find((candidate) => candidate.source === resolved);
817+
if (existing) {
818+
mergeCandidateInstallOwner(
819+
existing,
820+
params.installOwner,
821+
params.installOwnerAmbiguous === true,
822+
);
823+
}
805824
return;
806825
}
807826
const resolvedRoot =

0 commit comments

Comments
 (0)