Skip to content

Commit 0dd08aa

Browse files
committed
refactor(plugins): make discovery invocation-owned
Remove the cache-era split result assembly, unreachable recursive scans, and repeated scan-state plumbing. Derive preferred install paths and managed classification together while keeping their distinct exclusion semantics.
1 parent 86e2083 commit 0dd08aa

2 files changed

Lines changed: 807 additions & 846 deletions

File tree

‎src/plugins/discovery.test.ts‎

Lines changed: 234 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ import {
99
isPluginCandidateInstallOwnerAmbiguous,
1010
resolvePluginCandidateInstallOwner,
1111
} from "./candidate-install-owner.js";
12-
import { discoverOpenClawPlugins } from "./discovery.js";
12+
import { discoverConfiguredPluginLoadPaths, discoverOpenClawPlugins } from "./discovery.js";
1313
import * as pluginHardlinkPolicy from "./hardlink-policy.js";
14+
import { resolvePluginManifestInstallOwner } from "./manifest-install-owner.js";
1415
import { loadPluginManifestRegistryCore } from "./manifest-registry.js";
1516
import type { PackageManifest } from "./manifest.js";
1617
import { resolvePackageSetupSource } from "./package-entry-resolution.js";
@@ -1139,6 +1140,167 @@ describe("discoverOpenClawPlugins", () => {
11391140
},
11401141
);
11411142

1143+
it.runIf(process.platform !== "win32" && canCreateDirectorySymlinks)(
1144+
"still scans sibling package entries beside a duplicate installed file alias",
1145+
() => {
1146+
const stateDir = makeTempDir();
1147+
const firstDir = path.join(stateDir, "extensions", "first");
1148+
const secondDir = path.join(stateDir, "extensions", "second");
1149+
const installedFile = path.join(firstDir, "index.js");
1150+
const aliasedFile = path.join(secondDir, "alias.js");
1151+
mkdirSafe(firstDir);
1152+
writePluginEntry(installedFile);
1153+
createPackagePluginWithEntry({
1154+
packageDir: secondDir,
1155+
packageName: "second",
1156+
pluginId: "second",
1157+
entryPath: "other.js",
1158+
});
1159+
fs.symlinkSync(installedFile, aliasedFile);
1160+
const result = discoverOpenClawPlugins({
1161+
env: buildDiscoveryEnv(stateDir),
1162+
installRecords: {
1163+
first: { source: "npm", installPath: installedFile },
1164+
alias: { source: "npm", installPath: aliasedFile },
1165+
},
1166+
});
1167+
expect(result.candidates.map((candidate) => candidate.idHint)).toEqual(["index", "second"]);
1168+
expect(isPluginCandidateInstallOwnerAmbiguous(result.candidates[0]!)).toBe(true);
1169+
expectDiagnostic({
1170+
diagnostics: result.diagnostics,
1171+
messageIncludes: "multiple plugin install records claim the same package path",
1172+
});
1173+
},
1174+
);
1175+
1176+
it.runIf(canCreateDirectorySymlinks).each([
1177+
{ name: "official registry", overrides: {}, ambiguous: false, trusted: true },
1178+
{
1179+
name: "local archive",
1180+
overrides: { sourcePath: "/tmp/diffs.tgz", artifactKind: "npm-pack", artifactFormat: "tgz" },
1181+
ambiguous: false,
1182+
trusted: undefined,
1183+
},
1184+
{ name: "conflicting owners", overrides: {}, ambiguous: true, trusted: undefined },
1185+
] satisfies Array<{
1186+
name: string;
1187+
overrides: Partial<PluginInstallRecord>;
1188+
ambiguous: boolean;
1189+
trusted: true | undefined;
1190+
}>)("preserves $name trust through configured aliases and bundled ownership", (scenario) => {
1191+
const stateDir = makeTempDir();
1192+
const bundledDir = path.join(stateDir, "bundled");
1193+
const pluginDir = path.join(bundledDir, "diffs");
1194+
const aliasDir = path.join(stateDir, "diffs-link");
1195+
mkdirSafe(pluginDir);
1196+
writePluginPackageManifest({
1197+
packageDir: pluginDir,
1198+
packageName: "@openclaw/diffs",
1199+
extensions: ["./two.js", "./one.js"],
1200+
});
1201+
writePluginManifest({ pluginDir, id: "diffs" });
1202+
for (const entry of ["two.js", "one.js"]) {
1203+
fs.writeFileSync(
1204+
path.join(pluginDir, entry),
1205+
'throw new Error("must not evaluate metadata")',
1206+
);
1207+
}
1208+
symlinkDirectory(pluginDir, aliasDir);
1209+
const env = buildDiscoveryEnvWithOverrides(stateDir, {
1210+
OPENCLAW_BUNDLED_PLUGINS_DIR: bundledDir,
1211+
});
1212+
const record: PluginInstallRecord = {
1213+
source: "npm",
1214+
spec: "@openclaw/diffs",
1215+
resolvedName: "@openclaw/diffs",
1216+
resolvedSpec: "@openclaw/[email protected]",
1217+
installPath: pluginDir,
1218+
...scenario.overrides,
1219+
};
1220+
const installRecords = {
1221+
diffs: record,
1222+
...(scenario.ambiguous ? { other: { ...record, installPath: aliasDir } } : {}),
1223+
};
1224+
const extraPaths = [aliasDir, pluginDir];
1225+
const result = discoverOpenClawPlugins({ env, extraPaths, installRecords });
1226+
expect(
1227+
result.candidates.map((candidate) => ({
1228+
id: candidate.idHint,
1229+
source: candidate.source,
1230+
origin: candidate.origin,
1231+
owner: resolvePluginCandidateInstallOwner(candidate),
1232+
ambiguous: isPluginCandidateInstallOwnerAmbiguous(candidate),
1233+
})),
1234+
).toEqual(
1235+
["two", "one"].map((entry) => ({
1236+
id: `diffs/${entry}`,
1237+
source: fs.realpathSync(path.join(pluginDir, `${entry}.js`)),
1238+
origin: "config",
1239+
owner: scenario.ambiguous ? undefined : "diffs",
1240+
ambiguous: scenario.ambiguous,
1241+
})),
1242+
);
1243+
const registry = loadPluginManifestRegistryCore({ discovery: result, installRecords, env });
1244+
expect(
1245+
registry.plugins.map((plugin) => ({
1246+
id: plugin.id,
1247+
owner: resolvePluginManifestInstallOwner(plugin),
1248+
trusted: plugin.trustedOfficialInstall,
1249+
})),
1250+
).toEqual(
1251+
["two", "one"].map((entry) => ({
1252+
id: `diffs/${entry}`,
1253+
owner: scenario.ambiguous ? undefined : "diffs",
1254+
trusted: scenario.trusted,
1255+
})),
1256+
);
1257+
expect(result.diagnostics).toEqual(
1258+
scenario.ambiguous
1259+
? [
1260+
{
1261+
level: "error",
1262+
source: aliasDir,
1263+
message:
1264+
"multiple plugin install records claim the same package path; refresh or reinstall the package before using managed lifecycle actions",
1265+
},
1266+
]
1267+
: [],
1268+
);
1269+
});
1270+
1271+
it.runIf(canCreateDirectorySymlinks)(
1272+
"leaves explicit-only file aliases and diagnostics unfinalized",
1273+
() => {
1274+
const stateDir = makeTempDir();
1275+
const pluginDir = path.join(stateDir, "plugin");
1276+
const aliasDir = path.join(stateDir, "alias");
1277+
mkdirSafe(pluginDir);
1278+
writePluginManifest({ pluginDir, id: "plugin", requiresPlugins: ["missing"] });
1279+
writePluginEntry(path.join(pluginDir, "index.js"));
1280+
symlinkDirectory(pluginDir, aliasDir);
1281+
const missing = path.join(stateDir, "missing.js");
1282+
const loadPaths = [
1283+
path.join(aliasDir, "index.js"),
1284+
path.join(pluginDir, "index.js"),
1285+
missing,
1286+
missing,
1287+
];
1288+
const env = buildDiscoveryEnv(stateDir);
1289+
const raw = discoverConfiguredPluginLoadPaths({ env, loadPaths });
1290+
expect(raw.candidates.map((candidate) => candidate.source)).toEqual(loadPaths.slice(0, 2));
1291+
expect(raw.diagnostics).toEqual(
1292+
[missing, missing].map((source) => ({
1293+
level: "error",
1294+
source,
1295+
message: `plugin path not found: ${source}`,
1296+
})),
1297+
);
1298+
const registry = loadPluginManifestRegistryCore({ discovery: raw, installRecords: {}, env });
1299+
expect(registry.plugins).toHaveLength(1);
1300+
expect(discoverOpenClawPlugins({ env, extraPaths: loadPaths }).candidates).toHaveLength(1);
1301+
},
1302+
);
1303+
11421304
it("still requires compiled runtime output for tracked installed package plugins", async () => {
11431305
const stateDir = makeTempDir();
11441306
const pluginDir = path.join(stateDir, "extensions", "source-only-pack");
@@ -1180,39 +1342,44 @@ describe("discoverOpenClawPlugins", () => {
11801342
expect(result.diagnostics).toHaveLength(1);
11811343
});
11821344

1183-
it("treats install record sourcePath dirs as managed during global scans", async () => {
1184-
const stateDir = makeTempDir();
1185-
const sourceDir = path.join(stateDir, "extensions", "source-path-pack");
1186-
const installDir = path.join(stateDir, "installed", "source-path-pack");
1187-
mkdirSafe(path.join(sourceDir, "src"));
1188-
mkdirSafe(installDir);
1345+
it.each([true, false])(
1346+
"treats sourcePath as managed with preferred install present=%s",
1347+
async (installed) => {
1348+
const stateDir = makeTempDir();
1349+
const sourceDir = path.join(stateDir, "extensions", "source-path-pack");
1350+
const installDir = path.join(stateDir, "installed", "source-path-pack");
1351+
mkdirSafe(path.join(sourceDir, "src"));
1352+
if (installed) {
1353+
mkdirSafe(installDir);
1354+
}
11891355

1190-
writePluginPackageManifest({
1191-
packageDir: sourceDir,
1192-
packageName: "@openclaw/source-path-pack",
1193-
extensions: ["./src/index.ts"],
1194-
});
1195-
writePluginEntry(path.join(sourceDir, "src", "index.ts"));
1356+
writePluginPackageManifest({
1357+
packageDir: sourceDir,
1358+
packageName: "@openclaw/source-path-pack",
1359+
extensions: ["./src/index.ts"],
1360+
});
1361+
writePluginEntry(path.join(sourceDir, "src", "index.ts"));
11961362

1197-
const installRecords = {
1198-
"source-path-pack": {
1199-
source: "path",
1200-
installPath: installDir,
1201-
sourcePath: sourceDir,
1202-
},
1203-
} satisfies Record<string, PluginInstallRecord>;
1204-
const result = await discoverWithStateDir(stateDir, { installRecords });
1363+
const installRecords = {
1364+
"source-path-pack": {
1365+
source: "path",
1366+
installPath: installDir,
1367+
sourcePath: sourceDir,
1368+
},
1369+
} satisfies Record<string, PluginInstallRecord>;
1370+
const result = await discoverWithStateDir(stateDir, { installRecords });
12051371

1206-
expectCandidateIds(result.candidates, { excludes: ["source-path-pack"] });
1207-
expectDiagnostic({
1208-
diagnostics: result.diagnostics,
1209-
level: "warn",
1210-
pluginId: "source-path-pack",
1211-
messageIncludes: "requires compiled runtime output",
1212-
source: sourceDir,
1213-
});
1214-
expect(result.diagnostics).toHaveLength(1);
1215-
});
1372+
expectCandidateIds(result.candidates, { excludes: ["source-path-pack"] });
1373+
expectDiagnostic({
1374+
diagnostics: result.diagnostics,
1375+
level: "warn",
1376+
pluginId: "source-path-pack",
1377+
messageIncludes: "requires compiled runtime output",
1378+
source: sourceDir,
1379+
});
1380+
expect(result.diagnostics).toHaveLength(1);
1381+
},
1382+
);
12161383

12171384
it.skipIf(!canCreateDirectorySymlinks)(
12181385
"treats symlinked install record sourcePath dirs as managed during global scans",
@@ -2867,6 +3034,30 @@ describe("discoverOpenClawPlugins", () => {
28673034
expect(candidates.map((candidate) => candidate.idHint)).not.toContain("pack");
28683035
});
28693036

3037+
it.runIf(process.platform !== "win32")(
3038+
"rechecks a rejected configured path under bundled policy after deduplicating scoped attempts",
3039+
() => {
3040+
const stateDir = makeTempDir();
3041+
const bundledDir = path.join(stateDir, "bundled");
3042+
const pluginDir = path.join(bundledDir, "repairable");
3043+
createPackagePluginWithEntry({
3044+
packageDir: pluginDir,
3045+
packageName: "repairable",
3046+
entryPath: "index.js",
3047+
});
3048+
fs.chmodSync(pluginDir, 0o777);
3049+
const result = discoverOpenClawPlugins({
3050+
env: buildDiscoveryEnvWithOverrides(stateDir, { OPENCLAW_BUNDLED_PLUGINS_DIR: bundledDir }),
3051+
extraPaths: [pluginDir, pluginDir],
3052+
});
3053+
expect(result.candidates).toHaveLength(1);
3054+
expectCandidateFields(result.candidates[0]!, { idHint: "repairable", origin: "bundled" });
3055+
expect(result.diagnostics).toHaveLength(1);
3056+
expectDiagnostic({ diagnostics: result.diagnostics, messageIncludes: "world-writable path" });
3057+
expect(fs.statSync(pluginDir).mode & 0o777).toBe(0o755);
3058+
},
3059+
);
3060+
28703061
it.runIf(process.platform !== "win32")("blocks world-writable plugin paths", async () => {
28713062
const stateDir = makeTempDir();
28723063
const pluginDir = path.join(stateDir, "extensions", "world-open");
@@ -3207,6 +3398,18 @@ describe("discoverOpenClawPlugins", () => {
32073398
present: ["bundled-plugin", "global-plugin", "workspace-b-plugin"],
32083399
absent: ["workspace-a-plugin"],
32093400
});
3401+
3402+
const bundledOnly = withOpenClawPackageArgv(packageRoot, () =>
3403+
discoverWithEnv({
3404+
workspaceDir: workspaceA,
3405+
extraPaths: [path.join(stateDir, "missing-configured-plugin")],
3406+
installRecords: { missing: { source: "npm", installPath: stateDir } },
3407+
rootScope: "bundled",
3408+
env,
3409+
}),
3410+
);
3411+
expect(bundledOnly.candidates.map((candidate) => candidate.idHint)).toEqual(["bundled-plugin"]);
3412+
expect(bundledOnly.diagnostics).toEqual([]);
32103413
});
32113414

32123415
it.each([

0 commit comments

Comments
 (0)