Skip to content

Commit 355295b

Browse files
authored
fix(plugins): relink host runtime dependencies (#117408)
* fix(plugins): relink host runtime dependencies * fix(plugins): remove stale peer helper * chore(release): leave changelog ownership unchanged * fix(plugins): clarify host link recovery
1 parent bba7b93 commit 355295b

11 files changed

Lines changed: 200 additions & 37 deletions

‎src/infra/package-update-utils.ts‎

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,22 +44,21 @@ export async function readInstalledPackageVersion(dir: string): Promise<string |
4444
return typeof manifest?.version === "string" ? manifest.version : undefined;
4545
}
4646

47-
/** Read string-valued peer dependencies from an installed package. */
48-
export function readInstalledPackagePeerDependencies(dir: string): Record<string, string> {
47+
/** Read the installed package declaration that requires the OpenClaw host link. */
48+
export function readInstalledPackageOpenClawLinkDependencies(dir: string): Record<string, string> {
4949
const manifest = readInstalledPackageManifest(dir);
5050
const peerDependencies = isRecord(manifest?.peerDependencies) ? manifest.peerDependencies : {};
51-
return Object.fromEntries(
52-
Object.entries(peerDependencies).filter((entry): entry is [string, string] => {
53-
const [, value] = entry;
54-
return typeof value === "string";
55-
}),
56-
);
51+
const dependencies = isRecord(manifest?.dependencies) ? manifest.dependencies : {};
52+
const peerSpec = peerDependencies.openclaw;
53+
const dependencySpec = dependencies.openclaw;
54+
const spec = typeof peerSpec === "string" ? peerSpec : dependencySpec;
55+
return typeof spec === "string" ? { openclaw: spec } : {};
5756
}
5857

5958
/** Return true when an installed package needs an openclaw peer link repair. */
6059
export function installedPackageNeedsOpenClawPeerLinkRepair(dir: string): boolean {
61-
const peerDependencies = readInstalledPackagePeerDependencies(dir);
62-
if (!Object.hasOwn(peerDependencies, "openclaw")) {
60+
const linkDependencies = readInstalledPackageOpenClawLinkDependencies(dir);
61+
if (!Object.hasOwn(linkDependencies, "openclaw")) {
6362
return false;
6463
}
6564

‎src/plugins/install-security-scan.runtime.test.ts‎

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { beforeEach, describe, expect, it, vi } from "vitest";
1+
import fs from "node:fs/promises";
2+
import os from "node:os";
3+
import path from "node:path";
4+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
25

36
const runInstallPolicyMock = vi.fn();
47
const findBlockedManifestDependenciesMock = vi.fn();
@@ -42,8 +45,15 @@ const {
4245
preflightPluginNpmInstallPolicyRuntime,
4346
scanBundleInstallSourceRuntime,
4447
scanFileInstallSourceRuntime,
48+
scanInstalledPackageDependencyTreeRuntime,
4549
} = await import("./install-security-scan.runtime.js");
4650

51+
const tempDirs: string[] = [];
52+
53+
afterEach(async () => {
54+
await Promise.all(tempDirs.splice(0).map((dir) => fs.rm(dir, { force: true, recursive: true })));
55+
});
56+
4757
function expectOnlyOperatorPolicyRan() {
4858
expect(runInstallPolicyMock).toHaveBeenCalledTimes(1);
4959
expect(findBlockedManifestDependenciesMock).not.toHaveBeenCalled();
@@ -176,6 +186,67 @@ describe("install security scan official bypass", () => {
176186
});
177187
});
178188

189+
describe("installed dependency tree scan", () => {
190+
it("accepts a managed host link declared as a runtime dependency", async () => {
191+
findBlockedManifestDependenciesMock.mockReturnValue([]);
192+
const npmRoot = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-install-scan-"));
193+
tempDirs.push(npmRoot);
194+
const packageDir = path.join(npmRoot, "node_modules", "runtime-plugin");
195+
const hostLink = path.join(packageDir, "node_modules", "openclaw");
196+
await fs.mkdir(path.dirname(hostLink), { recursive: true });
197+
await fs.writeFile(
198+
path.join(packageDir, "package.json"),
199+
JSON.stringify({
200+
name: "runtime-plugin",
201+
dependencies: { openclaw: "2026.7.1" },
202+
}),
203+
"utf8",
204+
);
205+
await fs.symlink(process.cwd(), hostLink, "junction");
206+
207+
const result = await scanInstalledPackageDependencyTreeRuntime({
208+
allowManagedNpmRootPackagePeerSymlinks: true,
209+
dependencyScanRootDir: npmRoot,
210+
logger: {},
211+
packageDir,
212+
pluginId: "runtime-plugin",
213+
});
214+
215+
expect(result).toBeUndefined();
216+
expect(runInstallPolicyMock).toHaveBeenCalledTimes(1);
217+
});
218+
219+
it("rejects an openclaw dependency symlink that does not target the trusted host", async () => {
220+
findBlockedManifestDependenciesMock.mockReturnValue([]);
221+
const npmRoot = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-install-scan-"));
222+
const outsideRoot = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-install-outside-"));
223+
tempDirs.push(npmRoot, outsideRoot);
224+
const packageDir = path.join(npmRoot, "node_modules", "runtime-plugin");
225+
const hostLink = path.join(packageDir, "node_modules", "openclaw");
226+
await fs.mkdir(path.dirname(hostLink), { recursive: true });
227+
await fs.writeFile(
228+
path.join(packageDir, "package.json"),
229+
JSON.stringify({
230+
name: "runtime-plugin",
231+
dependencies: { openclaw: "2026.7.1" },
232+
}),
233+
"utf8",
234+
);
235+
await fs.writeFile(path.join(outsideRoot, "package.json"), '{"name":"openclaw"}', "utf8");
236+
await fs.symlink(outsideRoot, hostLink, "junction");
237+
238+
await expect(
239+
scanInstalledPackageDependencyTreeRuntime({
240+
allowManagedNpmRootPackagePeerSymlinks: true,
241+
dependencyScanRootDir: npmRoot,
242+
logger: {},
243+
packageDir,
244+
pluginId: "runtime-plugin",
245+
}),
246+
).rejects.toThrow("installed dependency scan found package outside install root");
247+
});
248+
});
249+
179250
describe("legacy file install scan compatibility", () => {
180251
it("preserves policy and hook metadata for published lazy install chunks", async () => {
181252
const warnings: string[] = [];

‎src/plugins/install-security-scan.runtime.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,9 +362,11 @@ function collectManifestRuntimeDependencyNames(manifest: PackageManifest): strin
362362
}
363363

364364
async function resolveInstalledPackageScanRoot(params: {
365+
allowManagedNpmRootPackagePeerSymlinks?: boolean;
365366
boundaryRealPath: string;
366367
dependencyName: string;
367368
packageDir: string;
369+
trustedHostOpenClawRootRealPath: string | null;
368370
}): Promise<InstalledPackageScanRoot | undefined> {
369371
const packageDir = path.join(params.packageDir, "node_modules", params.dependencyName);
370372
let stats: Awaited<ReturnType<typeof fs.stat>>;
@@ -382,6 +384,16 @@ async function resolveInstalledPackageScanRoot(params: {
382384

383385
const realPath = await fs.realpath(packageDir).catch(() => path.resolve(packageDir));
384386
if (!isSamePathOrInside(params.boundaryRealPath, realPath)) {
387+
if (
388+
params.allowManagedNpmRootPackagePeerSymlinks === true &&
389+
params.dependencyName === "openclaw" &&
390+
isTrustedHostOpenClawPath({
391+
resolvedTargetPath: realPath,
392+
trustedHostOpenClawRootRealPath: params.trustedHostOpenClawRootRealPath,
393+
})
394+
) {
395+
return undefined;
396+
}
385397
throw new Error(
386398
`installed dependency scan found package outside install root at ${packageDir}`,
387399
);
@@ -391,12 +403,14 @@ async function resolveInstalledPackageScanRoot(params: {
391403

392404
async function collectInstalledPackageScanRoots(params: {
393405
additionalPackageDirs?: string[];
406+
allowManagedNpmRootPackagePeerSymlinks?: boolean;
394407
dependencyScanRootDir?: string;
395408
packageDir: string;
396409
}): Promise<string[]> {
397410
const limits = resolvePackageManifestTraversalLimits();
398411
const boundaryDir = params.dependencyScanRootDir ?? params.packageDir;
399412
const boundaryRealPath = await fs.realpath(boundaryDir).catch(() => path.resolve(boundaryDir));
413+
const trustedHostOpenClawRootRealPath = await resolveTrustedHostOpenClawRootRealPath();
400414
const packageRealPath = await fs
401415
.realpath(params.packageDir)
402416
.catch(() => path.resolve(params.packageDir));
@@ -444,17 +458,21 @@ async function collectInstalledPackageScanRoots(params: {
444458
}
445459
for (const dependencyName of collectManifestRuntimeDependencyNames(manifest)) {
446460
const nestedCandidate = await resolveInstalledPackageScanRoot({
461+
allowManagedNpmRootPackagePeerSymlinks: params.allowManagedNpmRootPackagePeerSymlinks,
447462
boundaryRealPath,
448463
dependencyName,
449464
packageDir: current.packageDir,
465+
trustedHostOpenClawRootRealPath,
450466
});
451467
const candidate =
452468
nestedCandidate ??
453469
(params.dependencyScanRootDir
454470
? await resolveInstalledPackageScanRoot({
471+
allowManagedNpmRootPackagePeerSymlinks: params.allowManagedNpmRootPackagePeerSymlinks,
455472
boundaryRealPath,
456473
dependencyName,
457474
packageDir: params.dependencyScanRootDir,
475+
trustedHostOpenClawRootRealPath,
458476
})
459477
: undefined);
460478
if (candidate && !visitedRealPaths.has(candidate.realPath)) {
@@ -1114,6 +1132,7 @@ export async function scanInstalledPackageDependencyTreeRuntime(params: {
11141132
? { additionalPackageDirs: params.additionalPackageDirs }
11151133
: {}),
11161134
dependencyScanRootDir: params.dependencyScanRootDir,
1135+
allowManagedNpmRootPackagePeerSymlinks: params.allowManagedNpmRootPackagePeerSymlinks,
11171136
packageDir: params.packageDir,
11181137
});
11191138
const manifestScanRoots = await collectNonOverlappingPackageScanRoots(scanRoots);

‎src/plugins/install-shared.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export type PluginInstallRuntime = Awaited<ReturnType<typeof loadPluginInstallRu
3434
export const defaultLogger: PluginInstallLogger = {};
3535

3636
export function formatUnresolvedOpenClawPeerLinkError(packageName: string): string {
37-
return `Installed plugin ${packageName} declares openclaw as a peer dependency, but OpenClaw could not create a plugin-local node_modules/openclaw link. Run from a packaged OpenClaw install or reinstall OpenClaw, then retry.`;
37+
return `Installed plugin ${packageName} declares an openclaw dependency, but OpenClaw could not create a plugin-local node_modules/openclaw link. Run from a packaged OpenClaw install or reinstall OpenClaw, then retry.`;
3838
}
3939

4040
const MISSING_EXTENSIONS_ERROR =

‎src/plugins/plugin-peer-link.test.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,33 @@ describe("plugin peer links", () => {
5252
expect(messages.join("\n")).toContain('Linked peerDependency "openclaw"');
5353
});
5454

55+
it("relinks openclaw runtime dependencies in the managed npm root", async () => {
56+
const npmRoot = makeTempDir();
57+
const packageDir = path.join(npmRoot, "node_modules", "runtime-plugin");
58+
fs.mkdirSync(packageDir, { recursive: true });
59+
fs.writeFileSync(
60+
path.join(packageDir, "package.json"),
61+
JSON.stringify({
62+
name: "runtime-plugin",
63+
version: "1.0.0",
64+
dependencies: {
65+
openclaw: "2026.7.1",
66+
},
67+
}),
68+
"utf8",
69+
);
70+
71+
const result = await relinkOpenClawPeerDependenciesInManagedNpmRoot({
72+
npmRoot,
73+
logger: {},
74+
});
75+
76+
const linkPath = path.join(packageDir, "node_modules", "openclaw");
77+
expect(result).toEqual({ checked: 1, attempted: 1, repaired: 1, skipped: 0 });
78+
expect(fs.lstatSync(linkPath).isSymbolicLink()).toBe(true);
79+
expect(fs.realpathSync(linkPath)).toBe(fs.realpathSync(process.cwd()));
80+
});
81+
5582
it("reports one unreadable package and continues repairing its sibling", async () => {
5683
const npmRoot = makeTempDir();
5784
const unreadableDir = path.join(npmRoot, "node_modules", "bad-plugin");

‎src/plugins/plugin-peer-link.ts‎

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,16 @@ function readStringRecord(value: unknown): Record<string, string> {
4343
return record;
4444
}
4545

46-
async function readPackagePeerDependencies(packageDir: string): Promise<Record<string, string>> {
46+
async function readPackageOpenClawLinkDependencies(
47+
packageDir: string,
48+
): Promise<Record<string, string>> {
4749
try {
4850
const raw = await fs.readFile(path.join(packageDir, "package.json"), "utf8");
49-
const parsed = JSON.parse(raw) as { peerDependencies?: unknown };
50-
return readStringRecord(parsed.peerDependencies);
51+
const parsed = JSON.parse(raw) as { dependencies?: unknown; peerDependencies?: unknown };
52+
const peerDependencies = readStringRecord(parsed.peerDependencies);
53+
const dependencies = readStringRecord(parsed.dependencies);
54+
const openclaw = peerDependencies.openclaw ?? dependencies.openclaw;
55+
return openclaw ? { openclaw } : {};
5156
} catch (error) {
5257
if ((error as NodeJS.ErrnoException).code === "ENOENT") {
5358
return {};
@@ -293,7 +298,7 @@ async function readPackageName(packageDir: string): Promise<string | undefined>
293298
}
294299

295300
/**
296-
* Symlink the host openclaw package for plugins that declare it as a peer.
301+
* Symlink the host openclaw package for plugins that declare it as a dependency.
297302
* Plugin package managers still own third-party dependencies; this only wires
298303
* the host SDK package into the plugin-local Node graph.
299304
*/
@@ -347,9 +352,9 @@ export async function relinkOpenClawPeerDependenciesInManagedNpmRoot(params: {
347352
let repaired = 0;
348353
let skipped = 0;
349354
for (const packageDir of await listManagedNpmRootPackageDirs(params.npmRoot)) {
350-
let peerDependencies: Record<string, string>;
355+
let openClawLinkDependencies: Record<string, string>;
351356
try {
352-
peerDependencies = await readPackagePeerDependencies(packageDir);
357+
openClawLinkDependencies = await readPackageOpenClawLinkDependencies(packageDir);
353358
} catch (error) {
354359
if (!params.onPackageReadError) {
355360
throw error;
@@ -358,13 +363,13 @@ export async function relinkOpenClawPeerDependenciesInManagedNpmRoot(params: {
358363
skipped += 1;
359364
continue;
360365
}
361-
if (!Object.hasOwn(peerDependencies, "openclaw")) {
366+
if (!Object.hasOwn(openClawLinkDependencies, "openclaw")) {
362367
continue;
363368
}
364369
checked += 1;
365370
const result = await linkOpenClawPeerDependencies({
366371
installedDir: packageDir,
367-
peerDependencies,
372+
peerDependencies: openClawLinkDependencies,
368373
logger: params.logger,
369374
});
370375
attempted += 1;
@@ -390,17 +395,17 @@ export async function auditOpenClawPeerDependenciesInManagedNpmRoot(params: {
390395
let checked = 0;
391396
const issues: OpenClawPeerLinkAuditIssue[] = [];
392397
for (const packageDir of await listManagedNpmRootPackageDirs(params.npmRoot)) {
393-
let peerDependencies: Record<string, string>;
398+
let openClawLinkDependencies: Record<string, string>;
394399
try {
395-
peerDependencies = await readPackagePeerDependencies(packageDir);
400+
openClawLinkDependencies = await readPackageOpenClawLinkDependencies(packageDir);
396401
} catch (error) {
397402
if (!params.onPackageReadError) {
398403
throw error;
399404
}
400405
params.onPackageReadError(error, packageDir);
401406
continue;
402407
}
403-
if (!Object.hasOwn(peerDependencies, "openclaw")) {
408+
if (!Object.hasOwn(openClawLinkDependencies, "openclaw")) {
404409
continue;
405410
}
406411
checked += 1;

‎src/plugins/status-snapshot.ts‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,14 @@ function buildPluginRecordFromInstalledIndex(
123123
hookCount: 0,
124124
configSchema: Boolean(manifest?.configSchema),
125125
contracts: manifest?.contracts,
126-
dependencyStatus: buildPluginDependencyStatus({
127-
rootDir: plugin.rootDir,
128-
dependencies: manifest?.packageDependencies,
129-
optionalDependencies: manifest?.packageOptionalDependencies,
130-
}),
126+
dependencyStatus:
127+
plugin.origin === "bundled"
128+
? undefined
129+
: buildPluginDependencyStatus({
130+
rootDir: plugin.rootDir,
131+
dependencies: manifest?.packageDependencies,
132+
optionalDependencies: manifest?.packageOptionalDependencies,
133+
}),
131134
};
132135
}
133136

‎src/plugins/status.dependency-health.test.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,20 @@ describe("plugin dependency health projection", () => {
121121
expect(report.plugins[0]?.status).toBe("error");
122122
});
123123

124+
it("does not project package-local dependency health onto bundled plugins", () => {
125+
const { fixture, reportParams } = createDependencyHealthFixture();
126+
loaderState.registry = createDependencyHealthRegistry(fixture.pluginId, {
127+
dependencyStatus: undefined,
128+
origin: "bundled",
129+
});
130+
131+
const report = buildPluginDiagnosticsReport(reportParams);
132+
133+
expect(report.plugins[0]?.dependencyStatus).toBeUndefined();
134+
expect(report.plugins[0]?.status).toBe("loaded");
135+
expect(report.diagnostics).toEqual([]);
136+
});
137+
124138
it("preserves an existing error diagnostic when dependency health also fails", () => {
125139
const registry = createDependencyHealthRegistry("existing-plugin-error", {
126140
status: "error",

‎src/plugins/status.registry-snapshot.test.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,28 @@ function expectFields(actual: Record<string, unknown>, expected: Record<string,
8383
}
8484

8585
describe("buildPluginRegistrySnapshotReport", () => {
86+
it("does not project package-local dependency health onto bundled plugins", () => {
87+
const tempRoot = makeTempDir();
88+
const bundledRoot = path.join(tempRoot, "bundled");
89+
const pluginRoot = path.join(bundledRoot, "bundled-demo");
90+
fs.mkdirSync(pluginRoot, { recursive: true });
91+
createColdPluginFixture({
92+
rootDir: pluginRoot,
93+
pluginId: "bundled-demo",
94+
packageJson: { dependencies: { "missing-build-time-dependency": "1.0.0" } },
95+
});
96+
97+
const report = buildPluginRegistrySnapshotReport({
98+
config: { plugins: { entries: { "bundled-demo": { enabled: true } } } },
99+
env: createColdPluginHermeticEnv(tempRoot, { bundledPluginsDir: bundledRoot }),
100+
});
101+
const plugin = requirePlugin(report.plugins, "bundled-demo");
102+
103+
expectFields(plugin, { origin: "bundled", status: "loaded" });
104+
expect(plugin.dependencyStatus).toBeUndefined();
105+
expect(report.diagnostics).toEqual([]);
106+
});
107+
86108
it("keeps recovered managed npm plugins visible when the persisted registry is stale", () => {
87109
const tempRoot = makeTempDir();
88110
const stateDir = path.join(tempRoot, "state");

0 commit comments

Comments
 (0)