From 4287c21e773e34edc56911de6155ac8cd05886fa Mon Sep 17 00:00:00 2001 From: Mariano Belinky Date: Tue, 27 Jan 2026 17:28:37 +0100 Subject: [PATCH] fix: guard channel-tools listActions against plugin crashes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Wraps plugin.actions.listActions() in a try/catch so a single broken channel plugin cannot crash the entire agent boot sequence. Errors are logged once per plugin+message (deduped) via defaultRuntime.error() and the call gracefully returns an empty array instead of propagating the exception. Fixes: 'Cannot read properties of undefined (reading listActions)' after the clawdbot→moltbot rename left some plugin state undefined. --- src/agents/channel-tools.ts | 43 ++++++++++++++++++++++++++++++++++--- 1 file changed, 40 insertions(+), 3 deletions(-) diff --git a/src/agents/channel-tools.ts b/src/agents/channel-tools.ts index 437d326cb..27af3c5f9 100644 --- a/src/agents/channel-tools.ts +++ b/src/agents/channel-tools.ts @@ -1,8 +1,13 @@ import { getChannelDock } from "../channels/dock.js"; import { getChannelPlugin, listChannelPlugins } from "../channels/plugins/index.js"; import { normalizeAnyChannelId } from "../channels/registry.js"; -import type { ChannelAgentTool, ChannelMessageActionName } from "../channels/plugins/types.js"; +import type { + ChannelAgentTool, + ChannelMessageActionName, + ChannelPlugin, +} from "../channels/plugins/types.js"; import type { MoltbotConfig } from "../config/config.js"; +import { defaultRuntime } from "../runtime.js"; /** * Get the list of supported message actions for a specific channel. @@ -16,7 +21,7 @@ export function listChannelSupportedActions(params: { const plugin = getChannelPlugin(params.channel as Parameters[0]); if (!plugin?.actions?.listActions) return []; const cfg = params.cfg ?? ({} as MoltbotConfig); - return plugin.actions.listActions({ cfg }); + return runPluginListActions(plugin, cfg); } /** @@ -29,7 +34,7 @@ export function listAllChannelSupportedActions(params: { for (const plugin of listChannelPlugins()) { if (!plugin.actions?.listActions) continue; const cfg = params.cfg ?? ({} as MoltbotConfig); - const channelActions = plugin.actions.listActions({ cfg }); + const channelActions = runPluginListActions(plugin, cfg); for (const action of channelActions) { actions.add(action); } @@ -64,3 +69,35 @@ export function resolveChannelMessageToolHints(params: { .map((entry) => entry.trim()) .filter(Boolean); } + +const loggedListActionErrors = new Set(); + +function runPluginListActions( + plugin: ChannelPlugin, + cfg: MoltbotConfig, +): ChannelMessageActionName[] { + if (!plugin.actions?.listActions) return []; + try { + const listed = plugin.actions.listActions({ cfg }); + return Array.isArray(listed) ? listed : []; + } catch (err) { + logListActionsError(plugin.id, err); + return []; + } +} + +function logListActionsError(pluginId: string, err: unknown) { + const message = err instanceof Error ? err.message : String(err); + const key = `${pluginId}:${message}`; + if (loggedListActionErrors.has(key)) return; + loggedListActionErrors.add(key); + const stack = err instanceof Error && err.stack ? err.stack : null; + const details = stack ?? message; + defaultRuntime.error?.(`[channel-tools] ${pluginId}.actions.listActions failed: ${details}`); +} + +export const __testing = { + resetLoggedListActionErrors() { + loggedListActionErrors.clear(); + }, +};