Skip to content

Commit 0f091ea

Browse files
committed
fix(plugins): keep bundled plugins bundled when selected by plugins.load.paths
Pointing `plugins.load.paths` at a plugin that already lives in the host's own bundled tree demoted it from `origin: "bundled"` to `origin: "config"` whenever compiled output existed beside the source. That silently revoked the host-owned runtime privileges bundled plugins get: host agent event streams, bundled channel and capability runtime registration, and active-runtime registry trust. Origin was decided by how the operator referenced a plugin rather than where it physically lives. Configured paths were labelled `config`, and config/global entry resolution prefers `dist/index.js` while bundled resolution does not, so one physical plugin produced two candidates with different `source` values. `discovery.finish()` intends "a configured alias must not downgrade that same physical entry" but dedupes on `source`, so the guard stopped applying the moment the two entry resolutions diverged. Both candidates then reached the manifest registry's same-`rootDir` branch, which applies the opposite policy and ranks `config` above `bundled`. Classify at the producer instead: a configured path whose resolved root lies inside the trees the bundled scan itself walks stays `bundled`, and the operator's selection is recorded separately as `configSelected`. Containment is realpath-based, so a directory symlinked into the bundled root from outside still stays `config`. Incidentally repairs `openclaw doctor`, whose pristine-startup check compares registry and candidate counts and read a configured bundled path as non-pristine purely because the checkout was built.
1 parent 7504a3b commit 0f091ea

4 files changed

Lines changed: 114 additions & 11 deletions

File tree

‎docs/plugins/sdk-entrypoints.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ built entries:
4747
`plugins.load.paths` or global roots looks for matching JavaScript peers under
4848
`dist/` first, then beside the TypeScript source entry, trying `.js`, `.mjs`,
4949
and `.cjs` in that order at each location.
50+
- A `plugins.load.paths` entry that resolves inside the host's own bundled
51+
plugin tree is discovered as that bundled plugin, so it keeps the bundled
52+
entry point and bundled provenance whether or not compiled output exists
53+
beside the source. Selecting a bundled plugin's own path never reclassifies it.
5054
- Package installation and managed installed-package discovery require compiled
5155
output for TypeScript extension and setup entries. Missing compiled output is
5256
a packaging error, not a reason to fall back to TypeScript.

‎src/plugins/discovery-checkout.test.ts‎

Lines changed: 75 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,17 +123,86 @@ describe("running checkout discovery", () => {
123123
candidates: discovery.candidates,
124124
installRecords: {},
125125
});
126+
// Never keyed on whether the checkout happens to be built: the alias must
127+
// resolve exactly what the bundled scan resolves in either state.
126128
expect(registry.plugins.find((plugin) => plugin.id === "codex")).toMatchObject({
127129
origin: "bundled",
128130
rootDir: pluginRoot,
129-
source: path.join(
130-
pluginRoot,
131-
alias.endsWith("directory") && fs.existsSync(path.join(pluginRoot, "dist", "index.js"))
132-
? "dist/index.js"
133-
: "index.ts",
134-
),
131+
source: path.join(pluginRoot, "index.ts"),
135132
});
136133
});
137134
},
138135
);
139136
});
137+
138+
describe("host provenance across bundled build states", () => {
139+
// The checkout may or may not be built, so the invariant guard cannot depend on
140+
// it: a configured alias of a host-owned plugin must classify identically
141+
// whether the entry resolves to the TypeScript source or to compiled output.
142+
const aliasShapes = ["direct directory", "symlink directory", "direct file"] as const;
143+
const buildStates = [
144+
{ label: "built", built: true },
145+
{ label: "unbuilt", built: false },
146+
] as const;
147+
148+
it.each(
149+
buildStates.flatMap(({ label, built }) =>
150+
aliasShapes.map((alias) => ({ label, built, alias })),
151+
),
152+
)("keeps a $alias alias bundled in a $label bundled tree", ({ built, alias }) => {
153+
const stateDir = fs.realpathSync(makeTrackedTempDir("openclaw-host-provenance", tempDirs));
154+
const bundledDir = fs.realpathSync(makeTrackedTempDir("openclaw-bundled-tree", tempDirs));
155+
const pluginRoot = path.join(bundledDir, "hosted");
156+
fs.mkdirSync(pluginRoot, { recursive: true });
157+
fs.writeFileSync(
158+
path.join(pluginRoot, "package.json"),
159+
JSON.stringify({ name: "@openclaw/hosted", openclaw: { extensions: ["./index.ts"] } }),
160+
);
161+
fs.writeFileSync(
162+
path.join(pluginRoot, "openclaw.plugin.json"),
163+
JSON.stringify({ id: "hosted", configSchema: { type: "object" } }),
164+
);
165+
fs.writeFileSync(path.join(pluginRoot, "index.ts"), "export default {};\n");
166+
if (built) {
167+
fs.mkdirSync(path.join(pluginRoot, "dist"), { recursive: true });
168+
fs.writeFileSync(path.join(pluginRoot, "dist", "index.js"), "export default {};\n");
169+
}
170+
let aliasRoot = pluginRoot;
171+
if (alias.startsWith("symlink")) {
172+
aliasRoot = path.join(stateDir, "linked-hosted");
173+
fs.symlinkSync(pluginRoot, aliasRoot, process.platform === "win32" ? "junction" : "dir");
174+
}
175+
const env = {
176+
OPENCLAW_STATE_DIR: stateDir,
177+
OPENCLAW_BUNDLED_PLUGINS_DIR: bundledDir,
178+
OPENCLAW_TEST_TRUST_BUNDLED_PLUGINS_DIR: "1",
179+
};
180+
withPluginCache(createPluginCache(), () => {
181+
const discovery = discoverOpenClawPlugins({
182+
env,
183+
extraPaths: [alias.endsWith("directory") ? aliasRoot : path.join(aliasRoot, "index.ts")],
184+
installRecords: {},
185+
});
186+
// One physical plugin must yield one candidate; a second candidate means the
187+
// alias resolved a different entry point than the bundled scan did.
188+
expect(discovery.candidates.filter((candidate) => candidate.idHint === "hosted")).toEqual([
189+
expect.objectContaining({
190+
origin: "bundled",
191+
rootDir: pluginRoot,
192+
source: path.join(pluginRoot, "index.ts"),
193+
configSelected: true,
194+
}),
195+
]);
196+
const registry = loadPluginManifestRegistryCore({
197+
env,
198+
candidates: discovery.candidates,
199+
installRecords: {},
200+
});
201+
expect(registry.plugins.find((plugin) => plugin.id === "hosted")).toMatchObject({
202+
origin: "bundled",
203+
rootDir: pluginRoot,
204+
source: path.join(pluginRoot, "index.ts"),
205+
});
206+
});
207+
});
208+
});

‎src/plugins/discovery.test.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3045,7 +3045,7 @@ describe("discoverOpenClawPlugins", () => {
30453045
});
30463046

30473047
it.runIf(process.platform !== "win32")(
3048-
"rechecks a rejected configured path under bundled policy after deduplicating scoped attempts",
3048+
"repairs a world-writable bundled plugin selected by config without warning that it was blocked",
30493049
() => {
30503050
const stateDir = makeTempDir();
30513051
const bundledDir = path.join(stateDir, "bundled");
@@ -3060,10 +3060,12 @@ describe("discoverOpenClawPlugins", () => {
30603060
env: buildDiscoveryEnvWithOverrides(stateDir, { OPENCLAW_BUNDLED_PLUGINS_DIR: bundledDir }),
30613061
extraPaths: [pluginDir, pluginDir],
30623062
});
3063+
// A host-owned path gets bundled policy on the first attempt, so the repair
3064+
// runs once and the operator never sees a "blocked" warning for a plugin that
3065+
// actually loads. Repeating the same path still yields a single candidate.
30633066
expect(result.candidates).toHaveLength(1);
30643067
expectCandidateFields(result.candidates[0]!, { idHint: "repairable", origin: "bundled" });
3065-
expect(result.diagnostics).toHaveLength(1);
3066-
expectDiagnostic({ diagnostics: result.diagnostics, messageIncludes: "world-writable path" });
3068+
expect(result.diagnostics).toEqual([]);
30673069
expect(fs.statSync(pluginDir).mode & 0o777).toBe(0o755);
30683070
},
30693071
);

‎src/plugins/discovery.ts‎

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { resolveCompatibilityHostVersion } from "../version.js";
1414
import { detectBundleManifestFormat, loadBundleManifest } from "./bundle-manifest.js";
1515
import {
1616
hasUsableBundledPluginTree,
17+
resolveBundledPluginsDir,
1718
resolveSourceCheckoutDependencyDiagnostic,
1819
} from "./bundled-dir.js";
1920
import { buildLegacyBundledRootPath } from "./bundled-load-path-aliases.js";
@@ -677,6 +678,17 @@ function resolveBundledSourceCheckoutExtensionsDir(bundledRoot?: string): string
677678
return legacyRoot;
678679
}
679680

681+
// Host provenance is a fact about where a plugin lives, not how it was referenced.
682+
// These are exactly the trees the bundled scan walks, so a configured alias of
683+
// anything inside them is that same host-owned plugin and must classify identically.
684+
function isHostBundledPluginRoot(dir: string, env: NodeJS.ProcessEnv): boolean {
685+
const bundledRoot = resolveBundledPluginsDir(env);
686+
const realDir = pluginCacheRealpathSync(dir) ?? path.resolve(dir);
687+
return [bundledRoot, resolveBundledSourceCheckoutExtensionsDir(bundledRoot)].some(
688+
(root) => root && isPathInside(pluginCacheRealpathSync(root) ?? path.resolve(root), realDir),
689+
);
690+
}
691+
680692
function readChildDirectoryNames(dir: string | undefined): Set<string> {
681693
if (!dir || !pluginCacheExistsSync(dir)) {
682694
return new Set();
@@ -1144,6 +1156,13 @@ function createPluginScanner(env: NodeJS.ProcessEnv, ownershipUid?: number | nul
11441156
if (!stat) {
11451157
return;
11461158
}
1159+
// Origin gates entry resolution and bundled runtime privileges, so pointing
1160+
// plugins.load.paths at a host-owned plugin must not reclassify it.
1161+
const origin =
1162+
params.origin === "config" &&
1163+
isHostBundledPluginRoot(stat.isFile() ? path.dirname(resolved) : resolved, env)
1164+
? "bundled"
1165+
: params.origin;
11471166
if (stat.isFile()) {
11481167
if (!isExtensionFile(resolved)) {
11491168
diagnostics.push({
@@ -1157,7 +1176,7 @@ function createPluginScanner(env: NodeJS.ProcessEnv, ownershipUid?: number | nul
11571176
idHint: path.basename(resolved, path.extname(resolved)),
11581177
source: resolved,
11591178
rootDir: path.dirname(resolved),
1160-
origin: params.origin,
1179+
origin,
11611180
workspaceDir: params.workspaceDir,
11621181
...(params.installOwner ? { installOwner: params.installOwner } : {}),
11631182
...(params.installOwnerAmbiguous ? { installOwnerAmbiguous: true } : {}),
@@ -1169,6 +1188,7 @@ function createPluginScanner(env: NodeJS.ProcessEnv, ownershipUid?: number | nul
11691188
if (
11701189
discoverPluginDirectory({
11711190
...params,
1191+
origin,
11721192
dir: resolved,
11731193
rootRealPath: pluginCacheRealpathSync(resolved) ?? undefined,
11741194
})
@@ -1178,7 +1198,7 @@ function createPluginScanner(env: NodeJS.ProcessEnv, ownershipUid?: number | nul
11781198

11791199
discoverInDirectory({
11801200
dir: resolved,
1181-
origin: params.origin,
1201+
origin,
11821202
workspaceDir: params.workspaceDir,
11831203
...(params.scanFiles !== undefined || params.origin === "config"
11841204
? { scanFiles: params.scanFiles ?? true }
@@ -1193,11 +1213,17 @@ function createPluginScanner(env: NodeJS.ProcessEnv, ownershipUid?: number | nul
11931213
}
11941214

11951215
function discoverConfiguredPaths(loadPaths: readonly string[], workspaceDir?: string): void {
1216+
const firstConfigured = candidates.length;
11961217
for (const loadPath of loadPaths) {
11971218
if (typeof loadPath === "string" && loadPath.trim()) {
11981219
discoverFromPath({ rawPath: loadPath.trim(), origin: "config", workspaceDir });
11991220
}
12001221
}
1222+
// Explicit operator selection is its own fact: host-owned aliases keep bundled
1223+
// origin, so duplicate precedence must not read selection off `origin` alone.
1224+
for (const candidate of candidates.slice(firstConfigured)) {
1225+
candidate.configSelected = true;
1226+
}
12011227
}
12021228
function finish(): PluginDiscoveryResult {
12031229
const candidatesBySource = new Map<string, PluginCandidate>();
@@ -1256,6 +1282,8 @@ function discoveryPolicy(env: NodeJS.ProcessEnv, ownershipUid: number | null | u
12561282
return {
12571283
ownershipUid: currentUid(ownershipUid),
12581284
compatibilityHostVersion: resolveCompatibilityHostVersion(env),
1285+
// Configured-path classification depends on the host's bundled tree.
1286+
bundledRoot: resolveBundledPluginsDir(env) ?? "",
12591287
nix: resolveIsNixMode(env),
12601288
sourceOverlaysDisabled: env.OPENCLAW_DISABLE_BUNDLED_SOURCE_OVERLAYS ?? "",
12611289
home: env.OPENCLAW_HOME ?? "",

0 commit comments

Comments
 (0)