Skip to content

Commit 90ae3c2

Browse files
steipetePeter Steinberger
andauthored
fix(plugins): report installed dependency health authoritatively (#117029)
* fix(plugins): project missing dependency health consistently * fix(plugins): preserve leaf dependency-health boundaries * fix(plugins): retain runtime dependency failure guidance * test(plugins): reproduce failed dependency runtime imports * fix(plugins): narrow existing dependency diagnostics safely --------- Co-authored-by: Peter Steinberger <[email protected]>
1 parent 8939497 commit 90ae3c2

5 files changed

Lines changed: 340 additions & 12 deletions

File tree

‎src/plugins/status-dependencies-core.ts‎

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,23 @@ export type PluginDependencyStatus = {
2626
optionalDependencies: PluginDependencyEntry[];
2727
};
2828

29+
type PluginDependencyHealthRegistry = {
30+
plugins: Array<{
31+
id: string;
32+
source: string;
33+
enabled: boolean;
34+
status: "loaded" | "disabled" | "error";
35+
error?: string;
36+
dependencyStatus?: PluginDependencyStatus;
37+
}>;
38+
diagnostics: Array<{
39+
level: "warn" | "error";
40+
message: string;
41+
pluginId?: string;
42+
source?: string;
43+
}>;
44+
};
45+
2946
function normalizeDependencyMap(raw: unknown): PluginDependencySpecMap {
3047
if (!raw || typeof raw !== "object" || Array.isArray(raw)) {
3148
return {};
@@ -49,9 +66,14 @@ export function normalizePluginDependencySpecs(params: {
4966
dependencies: PluginDependencySpecMap;
5067
optionalDependencies: PluginDependencySpecMap;
5168
} {
69+
const dependencies = normalizeDependencyMap(params.dependencies);
70+
const optionalDependencies = normalizeDependencyMap(params.optionalDependencies);
71+
for (const name of Object.keys(optionalDependencies)) {
72+
delete dependencies[name];
73+
}
5274
return {
53-
dependencies: normalizeDependencyMap(params.dependencies),
54-
optionalDependencies: normalizeDependencyMap(params.optionalDependencies),
75+
dependencies,
76+
optionalDependencies,
5577
};
5678
}
5779

@@ -142,3 +164,46 @@ export function buildPluginDependencyStatus(params: {
142164
optionalDependencies,
143165
};
144166
}
167+
168+
/** Projects missing required dependencies consistently across cold plugin status surfaces. */
169+
export function projectPluginDependencyHealth<T extends PluginDependencyHealthRegistry>(
170+
registry: T,
171+
): T {
172+
const diagnostics = [...registry.diagnostics];
173+
const plugins = registry.plugins.map((plugin) => {
174+
const status = plugin.dependencyStatus;
175+
if (!plugin.enabled || status?.requiredInstalled !== false) {
176+
return plugin;
177+
}
178+
const message =
179+
`Plugin "${plugin.id}" cannot load because required dependencies are missing: ` +
180+
`${status.missing.join(", ")}. Install the plugin dependencies or reinstall/update the ` +
181+
"plugin, then restart the Gateway.";
182+
const existingDiagnosticIndex = diagnostics.findIndex(
183+
(entry) => entry.level === "error" && entry.pluginId === plugin.id,
184+
);
185+
if (existingDiagnosticIndex === -1) {
186+
diagnostics.push({ level: "error", pluginId: plugin.id, source: plugin.source, message });
187+
} else {
188+
const existingDiagnostic = diagnostics[existingDiagnosticIndex];
189+
if (existingDiagnostic && !existingDiagnostic.message.includes(message)) {
190+
diagnostics[existingDiagnosticIndex] = {
191+
...existingDiagnostic,
192+
message: `${existingDiagnostic.message}\n${message}`,
193+
};
194+
}
195+
}
196+
if (plugin.status === "error") {
197+
const existingError = plugin.error;
198+
return {
199+
...plugin,
200+
error:
201+
existingError && !existingError.includes(message)
202+
? `${existingError}\n${message}`
203+
: (existingError ?? message),
204+
};
205+
}
206+
return { ...plugin, status: "error" as const, error: message };
207+
});
208+
return { ...registry, plugins, diagnostics };
209+
}

‎src/plugins/status-snapshot.ts‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ import {
99
} from "./plugin-registry.js";
1010
import { createEmptyPluginRegistry } from "./registry-empty.js";
1111
import type { PluginRecord, PluginRegistry } from "./registry-types.js";
12-
import { buildPluginDependencyStatus } from "./status-dependencies-core.js";
12+
import {
13+
buildPluginDependencyStatus,
14+
projectPluginDependencyHealth,
15+
} from "./status-dependencies-core.js";
1316
import type { PluginLogger } from "./types.js";
1417

1518
/** Control-plane plugin status shape used by `openclaw plugins status` style surfaces. */
@@ -150,7 +153,7 @@ export function buildPluginRegistrySnapshotReport(
150153
workspaceDir: params?.workspaceDir,
151154
});
152155
const manifestByPluginId = metadataSnapshot.byPluginId;
153-
return {
156+
return projectPluginDependencyHealth({
154157
workspaceDir: params?.workspaceDir,
155158
...createEmptyPluginRegistry(),
156159
plugins: result.snapshot.plugins.map((plugin) =>
@@ -159,5 +162,5 @@ export function buildPluginRegistrySnapshotReport(
159162
diagnostics: [...result.snapshot.diagnostics],
160163
registrySource: result.source,
161164
registryDiagnostics: result.diagnostics,
162-
};
165+
});
163166
}
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
import fs from "node:fs";
2+
import path from "node:path";
3+
import { afterEach, describe, expect, it, vi } from "vitest";
4+
import {
5+
buildPluginDependencyStatus,
6+
projectPluginDependencyHealth,
7+
} from "./status-dependencies-core.js";
8+
import { buildPluginDiagnosticsReport, buildPluginSnapshotReport } from "./status.js";
9+
import { createPluginLoadResult, createPluginRecord } from "./status.test-fixtures.js";
10+
import {
11+
createColdPluginConfig,
12+
createColdPluginFixture,
13+
createColdPluginHermeticEnv,
14+
} from "./test-helpers/cold-plugin-fixtures.js";
15+
import { cleanupTrackedTempDirs, makeTrackedTempDir } from "./test-helpers/fs-fixtures.js";
16+
17+
const loaderState = vi.hoisted(() => ({
18+
registry: undefined as
19+
| ReturnType<typeof import("./status.test-fixtures.js").createPluginLoadResult>
20+
| undefined,
21+
}));
22+
23+
vi.mock("./loader.js", async (importOriginal) => ({
24+
...(await importOriginal<typeof import("./loader.js")>()),
25+
loadOpenClawPlugins: () => loaderState.registry,
26+
}));
27+
28+
vi.mock("./runtime/metadata-registry-loader.js", async (importOriginal) => ({
29+
...(await importOriginal<typeof import("./runtime/metadata-registry-loader.js")>()),
30+
loadPluginMetadataRegistrySnapshot: () => loaderState.registry,
31+
}));
32+
33+
const tempDirs: string[] = [];
34+
35+
afterEach(() => {
36+
loaderState.registry = undefined;
37+
cleanupTrackedTempDirs(tempDirs);
38+
});
39+
40+
function createDependencyHealthRegistry(
41+
pluginId: string,
42+
overrides: Omit<Parameters<typeof createPluginRecord>[0], "id"> = {},
43+
) {
44+
return createPluginLoadResult({
45+
plugins: [
46+
createPluginRecord({
47+
id: pluginId,
48+
dependencyStatus: buildPluginDependencyStatus({
49+
dependencies: { "missing-runtime": "1.0.0" },
50+
}),
51+
...overrides,
52+
}),
53+
],
54+
});
55+
}
56+
57+
function createDependencyHealthFixture() {
58+
const rootDir = makeTrackedTempDir("openclaw-plugin-dependency-health", tempDirs);
59+
const pluginRoot = path.join(rootDir, "plugin");
60+
const bundledRoot = path.join(rootDir, "bundled");
61+
fs.mkdirSync(pluginRoot);
62+
fs.mkdirSync(bundledRoot);
63+
const fixture = createColdPluginFixture({
64+
rootDir: pluginRoot,
65+
pluginId: "missing-dependency-plugin",
66+
packageJson: {
67+
dependencies: { "missing-runtime": "1.0.0", "optional-runtime": "1.0.0" },
68+
optionalDependencies: { "optional-runtime": "2.0.0" },
69+
},
70+
});
71+
return {
72+
fixture,
73+
reportParams: {
74+
config: createColdPluginConfig(pluginRoot, fixture.pluginId),
75+
env: createColdPluginHermeticEnv(rootDir, { bundledPluginsDir: bundledRoot }),
76+
workspaceDir: rootDir,
77+
},
78+
};
79+
}
80+
81+
describe("plugin dependency health projection", () => {
82+
it.each([
83+
{ mode: "snapshot", load: buildPluginSnapshotReport },
84+
{ mode: "runtime", load: buildPluginDiagnosticsReport },
85+
])("surfaces missing required plugin dependencies in $mode inspections", ({ load }) => {
86+
const { fixture, reportParams } = createDependencyHealthFixture();
87+
loaderState.registry = createDependencyHealthRegistry(fixture.pluginId);
88+
89+
const report = load(reportParams);
90+
91+
expect(report.plugins[0]).toEqual(
92+
expect.objectContaining({
93+
status: "error",
94+
error: expect.stringContaining("missing-runtime"),
95+
}),
96+
);
97+
expect(report.diagnostics).toContainEqual(
98+
expect.objectContaining({
99+
level: "error",
100+
pluginId: "missing-dependency-plugin",
101+
message: expect.stringContaining("reinstall/update the plugin"),
102+
}),
103+
);
104+
});
105+
106+
it("uses prepared manifest facts when runtime records omit dependency metadata", () => {
107+
const { fixture, reportParams } = createDependencyHealthFixture();
108+
loaderState.registry = createDependencyHealthRegistry(fixture.pluginId, {
109+
dependencyStatus: undefined,
110+
});
111+
112+
const report = buildPluginDiagnosticsReport(reportParams);
113+
114+
expect(report.plugins[0]?.dependencyStatus).toEqual(
115+
expect.objectContaining({
116+
requiredInstalled: false,
117+
missing: ["missing-runtime"],
118+
missingOptional: ["optional-runtime"],
119+
}),
120+
);
121+
expect(report.plugins[0]?.status).toBe("error");
122+
});
123+
124+
it("preserves an existing error diagnostic when dependency health also fails", () => {
125+
const registry = createDependencyHealthRegistry("existing-plugin-error", {
126+
status: "error",
127+
error: "Cannot find module 'missing-runtime'",
128+
});
129+
registry.diagnostics.push({
130+
level: "error",
131+
pluginId: "existing-plugin-error",
132+
message: "already recorded",
133+
});
134+
135+
const report = projectPluginDependencyHealth(registry);
136+
137+
expect(report.plugins[0]?.status).toBe("error");
138+
expect(report.plugins[0]?.error).toContain("Cannot find module 'missing-runtime'");
139+
expect(report.plugins[0]?.error).toContain("Install the plugin dependencies");
140+
expect(report.diagnostics).toHaveLength(1);
141+
expect(report.diagnostics[0]?.message).toContain("already recorded");
142+
expect(report.diagnostics[0]?.message).toContain("Install the plugin dependencies");
143+
});
144+
});

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

Lines changed: 112 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ import { afterEach, describe, expect, it } from "vitest";
55
import { writePersistedInstalledPluginIndexSync } from "./installed-plugin-index-store.js";
66
import { loadInstalledPluginIndex } from "./installed-plugin-index.js";
77
import { refreshPluginRegistry } from "./plugin-registry.js";
8-
import { buildPluginRegistrySnapshotReport, buildPluginSnapshotReport } from "./status.js";
8+
import {
9+
buildPluginDiagnosticsReport,
10+
buildPluginRegistrySnapshotReport,
11+
buildPluginSnapshotReport,
12+
} from "./status.js";
913
import {
1014
createColdPluginConfig,
1115
createColdPluginFixture,
@@ -207,6 +211,18 @@ describe("buildPluginRegistrySnapshotReport", () => {
207211
});
208212

209213
const plugin = requirePlugin(report.plugins, "dependency-demo");
214+
expectFields(plugin, {
215+
status: "error",
216+
error:
217+
'Plugin "dependency-demo" cannot load because required dependencies are missing: missing-required. Install the plugin dependencies or reinstall/update the plugin, then restart the Gateway.',
218+
});
219+
expect(report.diagnostics).toContainEqual({
220+
level: "error",
221+
pluginId: "dependency-demo",
222+
source: fs.realpathSync(fixture.runtimeSource),
223+
message:
224+
'Plugin "dependency-demo" cannot load because required dependencies are missing: missing-required. Install the plugin dependencies or reinstall/update the plugin, then restart the Gateway.',
225+
});
210226
const dependencyStatus = requireRecord(plugin.dependencyStatus);
211227
expectFields(dependencyStatus, {
212228
hasDependencies: true,
@@ -241,6 +257,101 @@ describe("buildPluginRegistrySnapshotReport", () => {
241257
expect(isColdPluginRuntimeLoaded(fixture)).toBe(false);
242258
});
243259

260+
it("honors npm optional dependency precedence without reporting a false required failure", () => {
261+
const fixture = createColdPluginFixture({
262+
rootDir: makeTempDir(),
263+
pluginId: "optional-dependency-demo",
264+
packageJson: {
265+
dependencies: { "optional-runtime": "1.0.0" },
266+
optionalDependencies: { "optional-runtime": "2.0.0" },
267+
},
268+
});
269+
270+
const report = buildPluginRegistrySnapshotReport({
271+
config: createColdPluginConfig(fixture.rootDir, fixture.pluginId),
272+
});
273+
const plugin = requirePlugin(report.plugins, fixture.pluginId);
274+
275+
expectFields(plugin, { status: "loaded" });
276+
expectFields(requireRecord(plugin.dependencyStatus), {
277+
requiredInstalled: true,
278+
optionalInstalled: false,
279+
missing: [],
280+
missingOptional: ["optional-runtime"],
281+
dependencies: [],
282+
});
283+
expect(report.diagnostics).not.toContainEqual(
284+
expect.objectContaining({ pluginId: fixture.pluginId, level: "error" }),
285+
);
286+
expect(isColdPluginRuntimeLoaded(fixture)).toBe(false);
287+
});
288+
289+
it("keeps disabled plugins with missing required dependencies diagnostic-free", () => {
290+
const fixture = createColdPluginFixture({
291+
rootDir: makeTempDir(),
292+
pluginId: "disabled-dependency-demo",
293+
packageJson: { dependencies: { "missing-required": "1.0.0" } },
294+
});
295+
296+
const report = buildPluginRegistrySnapshotReport({
297+
config: {
298+
plugins: {
299+
load: { paths: [fixture.rootDir] },
300+
entries: { [fixture.pluginId]: { enabled: false } },
301+
},
302+
},
303+
});
304+
const plugin = requirePlugin(report.plugins, fixture.pluginId);
305+
306+
expectFields(plugin, { enabled: false, status: "disabled" });
307+
expect(plugin.error).toBeUndefined();
308+
expect(requireRecord(plugin.dependencyStatus).missing).toEqual(["missing-required"]);
309+
expect(report.diagnostics).not.toContainEqual(
310+
expect.objectContaining({ pluginId: fixture.pluginId, level: "error" }),
311+
);
312+
expect(isColdPluginRuntimeLoaded(fixture)).toBe(false);
313+
});
314+
315+
it("preserves the real runtime import error while explaining missing dependencies", () => {
316+
const rootDir = makeTempDir();
317+
const bundledRoot = makeTempDir();
318+
const fixture = createColdPluginFixture({
319+
rootDir,
320+
pluginId: "failed-runtime-dependency-demo",
321+
packageJson: {
322+
dependencies: { "missing-runtime": "1.0.0", "optional-runtime": "1.0.0" },
323+
optionalDependencies: { "optional-runtime": "2.0.0" },
324+
},
325+
});
326+
fs.writeFileSync(
327+
fixture.runtimeSource,
328+
`require("node:fs").writeFileSync(${JSON.stringify(fixture.runtimeMarker)}, "loaded");\n` +
329+
'require("missing-runtime");\n',
330+
"utf8",
331+
);
332+
333+
const report = buildPluginDiagnosticsReport({
334+
config: createColdPluginConfig(rootDir, fixture.pluginId),
335+
workspaceDir: rootDir,
336+
env: createColdPluginHermeticEnv(rootDir, { bundledPluginsDir: bundledRoot }),
337+
logger: { info() {}, warn() {}, error() {}, debug() {} },
338+
});
339+
const plugin = requirePlugin(report.plugins, fixture.pluginId);
340+
const diagnostics = report.diagnostics.filter((entry) => entry.pluginId === fixture.pluginId);
341+
342+
expectFields(plugin, { status: "error" });
343+
expect(String(plugin.error)).toContain("Cannot find module");
344+
expect(String(plugin.error)).toContain("Install the plugin dependencies");
345+
expectFields(requireRecord(plugin.dependencyStatus), {
346+
missing: ["missing-runtime"],
347+
missingOptional: ["optional-runtime"],
348+
});
349+
expect(diagnostics).toHaveLength(1);
350+
expect(diagnostics[0]?.message).toContain("Cannot find module");
351+
expect(diagnostics[0]?.message).toContain("Install the plugin dependencies");
352+
expect(isColdPluginRuntimeLoaded(fixture)).toBe(true);
353+
});
354+
244355
it("replays persisted list metadata without importing plugin runtime", async () => {
245356
const fixture = createColdPluginFixture({
246357
rootDir: makeTempDir(),

0 commit comments

Comments
 (0)