Skip to content

Commit 4c52d3f

Browse files
fix(cli): message send cannot address channels from npm-installed plugins (#126700)
* fix(cli): message send cannot address channels from npm-installed plugins Target resolution, channel enumeration, and target-prefix inference only consulted the process-root channel registry, so message CLI actions running against a scoped registry handle could not see installed channel plugins even though selection and send execution could. Carry the selection-resolved plugin into target resolution, fall back to the registry handle in scope for resolver-owned lookups, and list runtime-visible channel plugins for channel selection and prefix inference. * fix(cli): keep runtime-visible channel reads import-light Importing channel-resolution from the target-prefix leaf pulled the plugin bootstrap/loader graph into every consumer and reordered module loading under distant vi.mock factories (subagent-registry.steer-restart failed in CI with a hoisting TDZ). Move the scoped-registry reads into a dedicated import-light module, share its registry matcher with channel-resolution, and drop the mock workarounds the heavier graph had required. * chore(ui): re-baseline startup JS for the outbound scoped-registry reads CI measured 348285 B gzip on the merge ref (baseline 347023 + 1056 tolerance). The first CI round measured 347784 B, so most of the growth is main-side drift since the 2026-08-19 baseline; the outbound changes account for roughly 60 B in a local A/B. Updated with the documented --update-baseline --startup-js-bytes flow using the CI value. * Revert "chore(ui): re-baseline startup JS for the outbound scoped-registry reads" This reverts commit f60bd4c. * fix(cli): plan broadcast accounts from runtime-visible channel plugins The unscoped message broadcast --account planner still enumerated only process-root plugins, so a registry-scoped installed channel could not join broadcast candidate planning. Use the runtime-visible read and cover the scoped and no-scope paths. * fix(cli): honor scoped channel plugin precedence * fix(outbound): preserve loaded plugin fallback order --------- Co-authored-by: Patrick Erichsen <[email protected]>
1 parent 1362490 commit 4c52d3f

13 files changed

Lines changed: 481 additions & 48 deletions

‎src/infra/outbound/channel-resolution.ts‎

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// Channel resolution exposes read-only outbound runtime facades and performs
22
// optional bootstrap for deliverable channels that are not loaded yet.
3-
import { normalizeOptionalLowercaseString } from "@openclaw/normalization-core/string-coerce";
43
import type { ChannelMessageAdapterShape } from "../../channels/message/types.js";
54
import { getChannelPlugin, getLoadedChannelPlugin } from "../../channels/plugins/index.js";
65
import type { ChannelPlugin } from "../../channels/plugins/types.plugin.js";
@@ -14,6 +13,7 @@ import {
1413
normalizeMessageChannel,
1514
} from "../../utils/message-channel.js";
1615
import { bootstrapOutboundChannelPlugin } from "./channel-bootstrap.runtime.js";
16+
import { findChannelPluginInRegistry } from "./runtime-visible-channels.js";
1717

1818
/** Normalizes a raw channel id and rejects non-deliverable/internal channels. */
1919
export function normalizeDeliverableOutboundChannel(raw?: string | null): string | undefined {
@@ -81,25 +81,7 @@ function resolveDirectFromRegistry(
8181
registry: ReturnType<typeof getActivePluginRegistry>,
8282
channel: string,
8383
): ChannelPlugin | undefined {
84-
if (!registry) {
85-
return undefined;
86-
}
87-
const normalizedChannel = normalizeOptionalLowercaseString(channel);
88-
if (!normalizedChannel) {
89-
return undefined;
90-
}
91-
for (const entry of registry.channels) {
92-
const plugin = entry?.plugin;
93-
if (
94-
normalizeOptionalLowercaseString(plugin?.id) === normalizedChannel ||
95-
plugin?.meta?.aliases?.some(
96-
(alias) => normalizeOptionalLowercaseString(alias) === normalizedChannel,
97-
)
98-
) {
99-
return plugin;
100-
}
101-
}
102-
return undefined;
84+
return findChannelPluginInRegistry(registry, channel);
10385
}
10486

10587
function messageAdapterCanSendText(

‎src/infra/outbound/channel-selection.test.ts‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { defaultRuntime } from "../../runtime.js";
55

66
const mocks = vi.hoisted(() => ({
77
listChannelPlugins: vi.fn(),
8+
listRuntimeVisibleChannelPlugins: vi.fn(),
89
resolveOutboundChannelPlugin: vi.fn(),
910
missingOfficialExternalChannels: new Set<string>(),
1011
}));
@@ -39,6 +40,12 @@ vi.mock("./channel-resolution.js", () => ({
3940
resolveOutboundChannelPlugin: mocks.resolveOutboundChannelPlugin,
4041
}));
4142

43+
vi.mock("./runtime-visible-channels.js", () => ({
44+
// Defaults to the process-root list; scoped-registry tests override it.
45+
listRuntimeVisibleChannelPlugins: (...args: unknown[]) =>
46+
mocks.listRuntimeVisibleChannelPlugins(...args) ?? mocks.listChannelPlugins(...args),
47+
}));
48+
4249
vi.mock("../../plugins/official-external-plugin-repair-hints.js", () => ({
4350
resolveMissingOfficialExternalChannelPluginRepairHint: ({ channelId }: { channelId: string }) =>
4451
mocks.missingOfficialExternalChannels.has(channelId)
@@ -428,3 +435,33 @@ describe("resolveMessageChannelSelection", () => {
428435
await expect(expectResolvedSelection(params)).rejects.toThrow(expectedMessage);
429436
});
430437
});
438+
439+
describe("resolveMessageChannelSelection (registry-scoped channel plugins)", () => {
440+
beforeEach(() => {
441+
mocks.listChannelPlugins.mockReset();
442+
mocks.listChannelPlugins.mockReturnValue([]);
443+
mocks.listRuntimeVisibleChannelPlugins.mockReset();
444+
mocks.resolveOutboundChannelPlugin.mockReset();
445+
mocks.resolveOutboundChannelPlugin.mockImplementation(({ channel }: { channel: string }) => ({
446+
id: channel,
447+
}));
448+
});
449+
450+
it("defaults to the single configured channel seen only through the runtime-visible list", async () => {
451+
mocks.listRuntimeVisibleChannelPlugins.mockReturnValue([
452+
makePlugin({ id: "delta", resolveAccount: () => ({ enabled: true }) }),
453+
]);
454+
455+
const selection = await expectResolvedSelection({ cfg: {} as never });
456+
expect(selection.channel).toBe("delta");
457+
expect(selection.source).toBe("single-configured");
458+
});
459+
460+
it("still reports no configured channels when the visible list is empty", async () => {
461+
mocks.listRuntimeVisibleChannelPlugins.mockReturnValue([]);
462+
463+
await expect(expectResolvedSelection({ cfg: {} as never })).rejects.toThrow(
464+
"Channel is required (no configured channels detected).",
465+
);
466+
});
467+
});

‎src/infra/outbound/channel-selection.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { expectDefined } from "@openclaw/normalization-core";
22
// Channel selection chooses a deliverable message channel from explicit input,
33
// tool context fallback, or configured plugin accounts.
4-
import { listChannelPlugins } from "../../channels/plugins/index.js";
54
import type { ChannelPlugin } from "../../channels/plugins/types.plugin.js";
65
import { formatUnknownChannelMessage } from "../../cli/error-format.js";
76
import type { OpenClawConfig } from "../../config/types.openclaw.js";
@@ -22,6 +21,7 @@ import {
2221
normalizeDeliverableOutboundChannel,
2322
resolveOutboundChannelPlugin,
2423
} from "./channel-resolution.js";
24+
import { listRuntimeVisibleChannelPlugins } from "./runtime-visible-channels.js";
2525

2626
/** Source that explains how message channel selection chose its result. */
2727
type MessageChannelSelectionSource = "explicit" | "tool-context-fallback" | "single-configured";
@@ -182,7 +182,7 @@ async function isPluginConfigured(plugin: ChannelPlugin, cfg: OpenClawConfig): P
182182

183183
async function listConfiguredMessageChannelPlugins(cfg: OpenClawConfig): Promise<ChannelPlugin[]> {
184184
const plugins: ChannelPlugin[] = [];
185-
for (const plugin of listChannelPlugins()) {
185+
for (const plugin of listRuntimeVisibleChannelPlugins()) {
186186
if (!isDeliverableMessageChannel(plugin.id)) {
187187
continue;
188188
}

‎src/infra/outbound/channel-target-prefix.ts‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Target prefix helpers separate provider-owned prefixes from generic target
22
// kind prefixes and validate selected-channel mismatches.
33
import { normalizeOptionalLowercaseString } from "@openclaw/normalization-core/string-coerce";
4-
import { getActivePluginChannelRegistryFromState } from "../../plugins/runtime-channel-state.js";
54
import { normalizeMessageChannel } from "../../utils/message-channel-core.js";
5+
import { listRuntimeVisibleChannelPlugins } from "./runtime-visible-channels.js";
66

77
const TARGET_KIND_PREFIXES = new Set([
88
"channel",
@@ -63,9 +63,7 @@ function resolvePluginTargetPrefix(prefix: string): string | undefined {
6363
if (!normalizedPrefix) {
6464
return undefined;
6565
}
66-
const registry = getActivePluginChannelRegistryFromState();
67-
for (const entry of registry?.channels ?? []) {
68-
const plugin = entry.plugin;
66+
for (const plugin of listRuntimeVisibleChannelPlugins()) {
6967
const channelId = normalizeOptionalLowercaseString(plugin.id);
7068
const candidates = plugin.messaging?.targetPrefixes ?? [];
7169
if (

‎src/infra/outbound/message-account-selection.test.ts‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,38 @@ describe("validateExplicitMessageAccountSelection", () => {
3939
).toThrow('Unknown account "missing"');
4040
});
4141
});
42+
43+
describe("resolveMessageBroadcastAccountPlan (registry-scoped channel plugins)", () => {
44+
const scopedPlugin = {
45+
id: "line",
46+
config: {
47+
listAccountIds: () => ["ops"],
48+
resolveAccount: (_cfg: OpenClawConfig, accountId?: string | null) => ({
49+
accountId,
50+
enabled: true,
51+
}),
52+
},
53+
} as unknown as ChannelPlugin;
54+
const scopedCfg = { channels: { line: { enabled: true } } } as unknown as OpenClawConfig;
55+
56+
it("plans candidates from a channel plugin that is only registry-scoped", async () => {
57+
const { withPluginRuntimeRegistryScope } =
58+
await import("../../plugins/runtime/gateway-request-scope.js");
59+
const { resolveMessageBroadcastAccountPlan } = await import("./message-account-selection.js");
60+
61+
const plan = withPluginRuntimeRegistryScope(
62+
{ channels: [{ plugin: scopedPlugin }] } as never,
63+
() => resolveMessageBroadcastAccountPlan({ cfg: scopedCfg, accountId: "ops" }),
64+
);
65+
expect(plan?.candidateChannels).toContain("line");
66+
expect(plan?.secretChannels).toEqual(["line"]);
67+
});
68+
69+
it("does not see the scoped channel outside the scope", async () => {
70+
const { resolveMessageBroadcastAccountPlan } = await import("./message-account-selection.js");
71+
72+
const plan = resolveMessageBroadcastAccountPlan({ cfg: scopedCfg, accountId: "ops" });
73+
expect(plan?.candidateChannels).not.toContain("line");
74+
expect(plan?.secretChannels).toEqual([]);
75+
});
76+
});

‎src/infra/outbound/message-account-selection.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { normalizeOptionalString } from "@openclaw/normalization-core/string-coerce";
22
import { resolveChannelAccountEnabled } from "../../channels/account-summary.js";
33
import { resolveChannelDefaultAccountId } from "../../channels/plugins/helpers.js";
4-
import { getChannelPlugin, listChannelPlugins } from "../../channels/plugins/index.js";
4+
import { getChannelPlugin } from "../../channels/plugins/index.js";
55
import type { ChannelPlugin } from "../../channels/plugins/types.plugin.js";
66
import type { ChannelId } from "../../channels/plugins/types.public.js";
77
import type { OpenClawConfig } from "../../config/types.openclaw.js";
@@ -11,6 +11,7 @@ import { isDeliverableMessageChannel } from "../../utils/message-channel.js";
1111
import { resolveOutboundChannelPlugin } from "./channel-resolution.js";
1212
import { isConfiguredChannel } from "./channel-selection.js";
1313
import { MessageActionDeniedError } from "./message-action-denial.js";
14+
import { listRuntimeVisibleChannelPlugins } from "./runtime-visible-channels.js";
1415

1516
export type MessageBroadcastAccountPlan = {
1617
accountId: string;
@@ -173,7 +174,7 @@ export function resolveMessageBroadcastAccountPlan(params: {
173174
return undefined;
174175
}
175176

176-
const candidatePlugins = listChannelPlugins().filter((plugin) =>
177+
const candidatePlugins = listRuntimeVisibleChannelPlugins().filter((plugin) =>
177178
isPotentialConfiguredMessageChannel({ cfg: params.cfg, plugin }),
178179
);
179180
const secretChannels = candidatePlugins.flatMap((plugin) => {

‎src/infra/outbound/message-action-routing.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ async function resolveActionTarget(params: {
165165
action: ChannelMessageActionName;
166166
args: Record<string, unknown>;
167167
accountId?: string | null;
168+
plugin?: ChannelPlugin;
168169
}): Promise<ResolvedMessagingTarget | undefined> {
169170
let resolvedTarget: ResolvedMessagingTarget | undefined;
170171
const toRaw = normalizeOptionalString(params.args.to) ?? "";
@@ -174,6 +175,7 @@ async function resolveActionTarget(params: {
174175
channel: params.channel,
175176
input: toRaw,
176177
accountId: params.accountId ?? undefined,
178+
plugin: params.plugin,
177179
});
178180
params.args.to = resolved.to;
179181
resolvedTarget = resolved;
@@ -185,6 +187,7 @@ async function resolveActionTarget(params: {
185187
channel: params.channel,
186188
input: channelIdRaw,
187189
accountId: params.accountId ?? undefined,
190+
plugin: params.plugin,
188191
preferredKind: "group",
189192
validateResolvedTarget: (target) =>
190193
target.kind === "user"
@@ -205,6 +208,7 @@ async function resolveResolvedTargetOrThrow(params: {
205208
channel: ChannelId;
206209
input: string;
207210
accountId?: string;
211+
plugin?: ChannelPlugin;
208212
preferredKind?: "group" | "user" | "channel";
209213
validateResolvedTarget?: (target: ResolvedMessagingTarget) => string | undefined;
210214
}): Promise<ResolvedMessagingTarget> {
@@ -214,6 +218,7 @@ async function resolveResolvedTargetOrThrow(params: {
214218
input: params.input,
215219
accountId: params.accountId,
216220
preferredKind: params.preferredKind,
221+
plugin: params.plugin,
217222
});
218223
if (!resolved.ok) {
219224
throw resolved.error;
@@ -457,6 +462,7 @@ export async function resolveMessageTarget(params: {
457462
toolContext?: ChannelThreadingToolContext;
458463
agentId?: string | null;
459464
deferExternalTargetResolution?: boolean;
465+
plugin?: ChannelPlugin;
460466
}): Promise<ResolvedMessagingTarget | undefined> {
461467
const resolvedTarget = params.deferExternalTargetResolution
462468
? undefined
@@ -466,6 +472,7 @@ export async function resolveMessageTarget(params: {
466472
action: params.action,
467473
args: params.args,
468474
accountId: params.accountId,
475+
plugin: params.plugin,
469476
});
470477

471478
enforceCrossContextPolicy({

‎src/infra/outbound/message-action-runner.plugin-dispatch.test.ts‎

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,118 @@ describe("runMessageAction plugin dispatch", () => {
5353
vi.clearAllMocks();
5454
vi.unstubAllEnvs();
5555
});
56+
57+
it("uses the selected operation-local plugin for target resolution", async () => {
58+
const resolveTarget = vi.fn(async ({ input }: { input: string }) => ({
59+
to: `user:${input}`,
60+
kind: "user" as const,
61+
}));
62+
const handleScopedAction = vi.fn(async () => jsonResult({ ok: true }));
63+
const scopedPlugin = createGatewayActionPlugin({
64+
pluginId: "operation-local",
65+
label: "Operation Local",
66+
blurb: "Operation-local target resolution test plugin.",
67+
actions: ["react"],
68+
gatewayActions: [],
69+
messaging: {
70+
targetResolver: {
71+
looksLikeId: () => true,
72+
resolveTarget,
73+
},
74+
},
75+
handleAction: handleScopedAction,
76+
});
77+
78+
setActivePluginRegistry(createTestRegistry([]));
79+
mocks.resolveOutboundChannelPlugin.mockReturnValue(scopedPlugin);
80+
const result = await runMessageAction({
81+
cfg: {
82+
channels: {
83+
"operation-local": {
84+
enabled: true,
85+
},
86+
},
87+
} as OpenClawConfig,
88+
action: "react",
89+
params: {
90+
channel: "operation-local",
91+
target: "plugin-alias",
92+
messageId: "message-1",
93+
emoji: "eyes",
94+
},
95+
dryRun: true,
96+
});
97+
98+
expect(result).toMatchObject({ kind: "action", action: "react", handledBy: "dry-run" });
99+
expect(resolveTarget).toHaveBeenCalledWith(
100+
expect.objectContaining({ input: "plugin-alias", normalized: "plugin-alias" }),
101+
);
102+
expect(handleScopedAction).not.toHaveBeenCalled();
103+
});
104+
105+
it("uses the selected operation-local plugin for broadcast target resolution", async () => {
106+
const resolveTarget = vi.fn(async ({ input }: { input: string }) => ({
107+
to: `user:${input}`,
108+
kind: "user" as const,
109+
}));
110+
const handleScopedAction = vi.fn(async () => jsonResult({ ok: true }));
111+
const scopedPlugin = createGatewayActionPlugin({
112+
pluginId: "operation-local",
113+
label: "Operation Local",
114+
blurb: "Operation-local broadcast target resolution test plugin.",
115+
actions: ["send"],
116+
gatewayActions: [],
117+
messaging: {
118+
targetResolver: {
119+
looksLikeId: () => true,
120+
resolveTarget,
121+
},
122+
},
123+
handleAction: handleScopedAction,
124+
});
125+
126+
setActivePluginRegistry(createTestRegistry([]));
127+
mocks.resolveOutboundChannelPlugin.mockReturnValue(scopedPlugin);
128+
mocks.executeSendAction.mockResolvedValue({
129+
handledBy: "core",
130+
payload: { ok: true },
131+
sendResult: {
132+
channel: "operation-local",
133+
to: "user:plugin-alias",
134+
via: "direct",
135+
mediaUrl: null,
136+
},
137+
});
138+
const result = await runMessageAction({
139+
cfg: {
140+
channels: {
141+
"operation-local": {
142+
enabled: true,
143+
},
144+
},
145+
} as OpenClawConfig,
146+
action: "broadcast",
147+
params: {
148+
channel: "operation-local",
149+
targets: ["plugin-alias"],
150+
message: "hello",
151+
},
152+
dryRun: true,
153+
});
154+
155+
expect(result).toMatchObject({
156+
kind: "broadcast",
157+
action: "broadcast",
158+
payload: {
159+
results: [{ channel: "operation-local", to: "user:plugin-alias", ok: true }],
160+
},
161+
});
162+
expect(resolveTarget).toHaveBeenCalledWith(
163+
expect.objectContaining({ input: "plugin-alias", normalized: "plugin-alias" }),
164+
);
165+
expect(handleScopedAction).not.toHaveBeenCalled();
166+
});
167+
56168
it("rejects unsupported read actions before conversation authorization", async () => {
57169
await expect(
58170
runMessageAction({

0 commit comments

Comments
 (0)