Skip to content

Commit 5cc5c98

Browse files
committed
fix(plugins): surface swallowed cleanup, setup, and CLI-load failures
Three silent-failure sinks in the plugin lifecycle, all violating 'every action ends in a visible outcome or a recorded reason': - cleanupReplacedPluginHostRegistry collects per-hook cleanup failures instead of throwing (it must finish every plugin), but both callers discarded the returned failures array entirely — broken session- extension/scheduler teardown vanished. The shared funnel now warns per failure with plugin and hook ids. - setup-registry dropped broken setup entries with catch{return null} and threw-away registration errors with catch{return false}, silently removing a plugin's providers/CLI backends/migrations from onboarding. Both now push typed diagnostics (setup-entry-load-failed, setup-registration-failed) — and since the diagnostics array had no operator surface at all, the registry build now warns once per uncached build for every diagnostic (including the previously invisible descriptor-drift ones). - loadPluginCliDescriptors swallowed total load failures behind a muted per-plugin logger; a failure removes every plugin command from help/dispatch and must not vanish with it. One warn on the catch.
1 parent 66db701 commit 5cc5c98

4 files changed

Lines changed: 84 additions & 11 deletions

File tree

‎src/plugins/cli-registry-loader.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { normalizeLowercaseStringOrEmpty } from "@openclaw/normalization-core/st
33
import { uniqueStrings } from "@openclaw/normalization-core/string-normalization";
44
import { collectUniqueCommandDescriptors } from "../cli/program/command-descriptor-utils.js";
55
import type { OpenClawConfig } from "../config/types.openclaw.js";
6+
import { createSubsystemLogger } from "../logging/subsystem.js";
67
import { resolveManifestActivationPluginIds } from "./activation-planner.js";
78
import { createPluginCliGatewayNodesRuntime } from "./cli-gateway-nodes-runtime.js";
89
import type { PluginLoadOptions } from "./loader.js";
@@ -46,6 +47,8 @@ export type PluginCliCommandGroupEntry = {
4647
register: (program: OpenClawPluginCliContext["program"]) => Promise<void>;
4748
};
4849

50+
const log = createSubsystemLogger("plugins/cli-registry-loader");
51+
4952
/** Creates the default plugin CLI logger shared with runtime loading. */
5053
export function createPluginCliLogger(): PluginLogger {
5154
return createPluginRuntimeLoaderLogger();
@@ -236,7 +239,11 @@ export async function loadPluginCliDescriptors(
236239
.filter((entry) => (entry.parentPath ?? []).length === 0)
237240
.map((entry) => entry.descriptors),
238241
);
239-
} catch {
242+
} catch (error) {
243+
// Callers pass a muted per-plugin logger for descriptor scans; a total
244+
// load failure still removes every plugin command from help/dispatch and
245+
// must not vanish with it.
246+
log.warn(`plugin CLI descriptor load failed: ${String(error)}`);
240247
return [];
241248
}
242249
}

‎src/plugins/runtime.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,20 @@ async function cleanupPreviousPluginHostRegistry(params: {
7777
// Async cleanup must not clear state for a registry that has been restored
7878
// active, but later swaps should not strand cleanup for the retiring registry.
7979
const shouldCleanup = () => state.activeRegistry !== params.previousRegistry;
80-
await cleanupReplacedPluginHostRegistry({
80+
const { failures } = await cleanupReplacedPluginHostRegistry({
8181
cfg: getRuntimeConfig(),
8282
previousRegistry: params.previousRegistry,
8383
nextRegistry,
8484
shouldCleanup,
8585
});
86+
// Per-hook cleanup errors are collected instead of thrown (host-hook-cleanup
87+
// must finish every plugin); dropping them here would hide broken
88+
// session-extension/scheduler teardown from operators entirely.
89+
for (const failure of failures) {
90+
log.warn(
91+
`plugin host cleanup failed for ${failure.pluginId} hook ${failure.hookId}: ${String(failure.error)}`,
92+
);
93+
}
8694
}
8795

8896
function cleanupRetiredPluginHostRegistry(previousRegistry: PluginRegistry): void {

‎src/plugins/setup-registry.test.ts‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -835,6 +835,24 @@ describe("setup-registry module loader", () => {
835835
});
836836
});
837837

838+
it("records a diagnostic when the setup entry fails to load", () => {
839+
const brokenRoot = makeTempDir();
840+
writeSetupApiStub(brokenRoot);
841+
mockSinglePlugin({ id: "broken-entry", rootDir: brokenRoot });
842+
mocks.createJiti.mockImplementation(() => () => {
843+
throw new Error("module parse failed");
844+
});
845+
846+
const registry = resolvePluginSetupRegistry({ env: {} });
847+
848+
// A broken setup entry removes the plugin from onboarding; the reason must
849+
// be recorded instead of vanishing.
850+
expect(registry.providers).toStrictEqual([]);
851+
expect(registry.diagnostics).toMatchObject([
852+
{ pluginId: "broken-entry", code: "setup-entry-load-failed" },
853+
]);
854+
});
855+
838856
it("publishes each plugin setup registration atomically on synchronous success", () => {
839857
const throwingRoot = makeTempDir();
840858
const healthyRoot = makeTempDir();
@@ -905,7 +923,10 @@ describe("setup-registry module loader", () => {
905923
expect(registry.configMigrations[0]?.migrate({} as never)?.changes).toEqual(["healthy"]);
906924
expect(registry.autoEnableProbes).toHaveLength(1);
907925
expect(registry.autoEnableProbes[0]?.probe({ config: {}, env: {} } as never)).toBe("healthy");
908-
expect(registry.diagnostics).toStrictEqual([]);
926+
// The throwing registration is recorded, not silently dropped.
927+
expect(registry.diagnostics).toMatchObject([
928+
{ pluginId: "shared-plugin", code: "setup-registration-failed" },
929+
]);
909930
}
910931
expect(second).not.toBe(first);
911932
expect(mocks.loadPluginManifestRegistry).toHaveBeenCalledTimes(1);

‎src/plugins/setup-registry.ts‎

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
normalizeUniqueStringEntries,
99
} from "@openclaw/normalization-core/string-normalization";
1010
import type { OpenClawConfig } from "../config/types.openclaw.js";
11+
import { createSubsystemLogger } from "../logging/subsystem.js";
1112
import { buildPluginApi } from "./api-builder.js";
1213
import { collectPluginConfigContractMatches } from "./config-contracts.js";
1314
import { getCurrentPluginMetadataSnapshotState } from "./current-plugin-metadata-state.js";
@@ -30,6 +31,8 @@ import type {
3031
ProviderPlugin,
3132
} from "./types.js";
3233

34+
const log = createSubsystemLogger("plugins/setup-registry");
35+
3336
const SETUP_API_EXTENSIONS = [".js", ".mjs", ".cjs", ".ts", ".mts", ".cts"] as const;
3437
const CURRENT_MODULE_PATH = fileURLToPath(import.meta.url);
3538
const RUNNING_FROM_BUILT_ARTIFACT =
@@ -61,7 +64,9 @@ type PluginSetupRegistryDiagnosticCode =
6164
| "setup-descriptor-provider-missing-runtime"
6265
| "setup-descriptor-provider-runtime-undeclared"
6366
| "setup-descriptor-cli-backend-missing-runtime"
64-
| "setup-descriptor-cli-backend-runtime-undeclared";
67+
| "setup-descriptor-cli-backend-runtime-undeclared"
68+
| "setup-entry-load-failed"
69+
| "setup-registration-failed";
6570

6671
type PluginSetupRegistryDiagnostic = {
6772
pluginId: string;
@@ -271,7 +276,10 @@ function resolveDeclaredSetupRuntimeSource(record: PluginManifestRecord): string
271276
);
272277
}
273278

274-
function resolveSetupRegistration(record: PluginManifestRecord): {
279+
function resolveSetupRegistration(
280+
record: PluginManifestRecord,
281+
diagnostics?: PluginSetupRegistryDiagnostic[],
282+
): {
275283
setupSource: string;
276284
register: (api: ReturnType<typeof buildPluginApi>) => void | Promise<void>;
277285
} | null {
@@ -286,7 +294,14 @@ function resolveSetupRegistration(record: PluginManifestRecord): {
286294
let mod: OpenClawPluginModule;
287295
try {
288296
mod = getModuleLoader(setupSource)(setupSource) as OpenClawPluginModule;
289-
} catch {
297+
} catch (error) {
298+
// A broken setup entry silently removes the plugin's providers/CLI
299+
// backends/migrations from onboarding; record why instead of vanishing.
300+
diagnostics?.push({
301+
pluginId: record.id,
302+
code: "setup-entry-load-failed",
303+
message: `setup entry failed to load from ${setupSource}: ${String(error)}`,
304+
});
290305
return null;
291306
}
292307

@@ -336,11 +351,13 @@ function ignoreAsyncSetupRegisterResult(result: void | Promise<void>): void {
336351
function runSetupRegistration(
337352
register: (api: ReturnType<typeof buildPluginApi>) => void | Promise<void>,
338353
api: ReturnType<typeof buildPluginApi>,
354+
onError: (error: unknown) => void,
339355
): boolean {
340356
try {
341357
ignoreAsyncSetupRegisterResult(register(api));
342358
return true;
343-
} catch {
359+
} catch (error) {
360+
onError(error);
344361
return false;
345362
}
346363
}
@@ -644,7 +661,7 @@ export function resolvePluginSetupRegistry(params?: {
644661
});
645662
continue;
646663
}
647-
const setupRegistration = resolveSetupRegistration(record);
664+
const setupRegistration = resolveSetupRegistration(record, diagnostics);
648665
if (!setupRegistration) {
649666
continue;
650667
}
@@ -703,7 +720,13 @@ export function resolvePluginSetupRegistry(params?: {
703720
},
704721
});
705722

706-
const registered = runSetupRegistration(setupRegistration.register, api);
723+
const registered = runSetupRegistration(setupRegistration.register, api, (error) => {
724+
diagnostics.push({
725+
pluginId: record.id,
726+
code: "setup-registration-failed",
727+
message: `setup registration threw: ${String(error)}`,
728+
});
729+
});
707730
acceptingRegistrations = false;
708731
if (!registered) {
709732
continue;
@@ -729,6 +752,12 @@ export function resolvePluginSetupRegistry(params?: {
729752
autoEnableProbes,
730753
diagnostics,
731754
} satisfies PluginSetupRegistry;
755+
// The diagnostics array has no other operator surface; warn once per
756+
// (uncached) build so broken setup entries and descriptor drift are
757+
// visible instead of silently narrowing onboarding.
758+
for (const diagnostic of diagnostics) {
759+
log.warn(`plugin setup [${diagnostic.pluginId}] ${diagnostic.code}: ${diagnostic.message}`);
760+
}
732761
if (resultCacheKey === null) {
733762
return registry;
734763
}
@@ -786,7 +815,11 @@ export function resolvePluginSetupProviderCore(params: {
786815
},
787816
});
788817

789-
if (!runSetupRegistration(setupRegistration.register, api)) {
818+
if (
819+
!runSetupRegistration(setupRegistration.register, api, (error) => {
820+
log.warn(`plugin setup [${record.id}] setup-registration-failed: ${String(error)}`);
821+
})
822+
) {
790823
return undefined;
791824
}
792825

@@ -846,7 +879,11 @@ export function resolvePluginSetupCliBackend(params: {
846879
},
847880
});
848881

849-
if (!runSetupRegistration(setupRegistration.register, api)) {
882+
if (
883+
!runSetupRegistration(setupRegistration.register, api, (error) => {
884+
log.warn(`plugin setup [${record.id}] setup-registration-failed: ${String(error)}`);
885+
})
886+
) {
850887
return undefined;
851888
}
852889

0 commit comments

Comments
 (0)