Skip to content

Commit 4f59053

Browse files
authored
fix(channels): resolve registered plugins before agent workspace (#124709)
* fix(channels): resolve registered plugins before agent workspace * fix(channels): scope registered plugin fast path to directory * fix(channels): restrict directory fast path to loaded plugins
1 parent e8e656f commit 4f59053

4 files changed

Lines changed: 117 additions & 2 deletions

File tree

‎src/cli/directory-cli.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ describe("registerDirectoryCli", () => {
123123
const installArgs = firstRecordArg(mocks.resolveInstallableChannelPlugin);
124124
expect(installArgs.rawChannel).toBe("demo-directory");
125125
expect(installArgs.allowInstall).toBe(true);
126+
expect(installArgs.preferRegisteredPlugin).toBe(true);
126127
expect(mocks.replaceConfigFile).toHaveBeenCalledTimes(1);
127128
const replaceArgs = firstRecordArg(mocks.replaceConfigFile);
128129
expect(replaceArgs.nextConfig).toEqual({

‎src/cli/directory-cli.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ export function registerDirectoryCli(program: Command) {
120120
runtime: defaultRuntime,
121121
rawChannel: explicitChannel,
122122
allowInstall: true,
123+
preferRegisteredPlugin: true,
123124
supports: (plugin) => Boolean(plugin.directory),
124125
})
125126
: null;

‎src/commands/channel-setup/channel-plugin-resolution.test.ts‎

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const mocks = vi.hoisted(() => ({
99
listChannelPluginCatalogEntries: vi.fn(),
1010
getChannelPluginCatalogEntry: vi.fn(),
1111
getChannelPlugin: vi.fn(),
12+
getLoadedChannelPlugin: vi.fn(),
1213
loadChannelSetupPluginRegistrySnapshotForChannel: vi.fn(),
1314
ensureChannelSetupPluginInstalled: vi.fn(),
1415
createClackPrompter: vi.fn(() => ({}) as never),
@@ -26,6 +27,7 @@ vi.mock("../../channels/plugins/catalog.js", () => ({
2627

2728
vi.mock("../../channels/plugins/index.js", () => ({
2829
getChannelPlugin: mocks.getChannelPlugin,
30+
getLoadedChannelPlugin: mocks.getLoadedChannelPlugin,
2931
normalizeChannelId: (value: unknown) => (typeof value === "string" ? value.trim() || null : null),
3032
}));
3133

@@ -78,14 +80,104 @@ function firstMockArg(mock: { mock: { calls: ReadonlyArray<ReadonlyArray<unknown
7880
describe("resolveInstallableChannelPlugin", () => {
7981
beforeEach(() => {
8082
vi.clearAllMocks();
83+
mocks.resolveAgentWorkspaceDir.mockReturnValue("/tmp/workspace");
84+
mocks.resolveDefaultAgentId.mockReturnValue("default");
8185
mocks.getChannelPlugin.mockReturnValue(undefined);
86+
mocks.getLoadedChannelPlugin.mockReturnValue(undefined);
8287
mocks.getChannelPluginCatalogEntry.mockReturnValue(undefined);
8388
mocks.ensureChannelSetupPluginInstalled.mockResolvedValue({
8489
cfg: {},
8590
installed: false,
8691
});
8792
});
8893

94+
it("returns a registered plugin before resolving a multi-agent workspace owner", async () => {
95+
const registeredPlugin = {
96+
...createPlugin("telegram"),
97+
directory: { self: vi.fn() },
98+
} as ChannelPlugin;
99+
mocks.getLoadedChannelPlugin.mockReturnValue(registeredPlugin);
100+
mocks.resolveDefaultAgentId.mockImplementation(() => {
101+
throw new Error("agent selection required");
102+
});
103+
104+
const result = await resolveInstallableChannelPlugin({
105+
cfg: {
106+
agents: {
107+
list: [{ id: "alpha" }, { id: "beta" }],
108+
},
109+
},
110+
runtime: {} as never,
111+
rawChannel: "telegram",
112+
allowInstall: true,
113+
preferRegisteredPlugin: true,
114+
supports: (plugin) => Boolean(plugin.directory),
115+
});
116+
117+
expect(result).toMatchObject({
118+
channelId: "telegram",
119+
plugin: registeredPlugin,
120+
configChanged: false,
121+
pluginInstalled: false,
122+
supportsRequestedCapability: true,
123+
});
124+
expect(mocks.resolveDefaultAgentId).not.toHaveBeenCalled();
125+
expect(mocks.getLoadedChannelPlugin).toHaveBeenCalledWith("telegram");
126+
expect(mocks.getChannelPlugin).not.toHaveBeenCalled();
127+
expect(mocks.listChannelPluginCatalogEntries).not.toHaveBeenCalled();
128+
expect(mocks.loadChannelSetupPluginRegistrySnapshotForChannel).not.toHaveBeenCalled();
129+
});
130+
131+
it("requires a workspace owner before falling back to an unloaded bundled plugin", async () => {
132+
mocks.getChannelPlugin.mockReturnValue(createPlugin("telegram"));
133+
mocks.resolveDefaultAgentId.mockImplementation(() => {
134+
throw new Error("agent selection required");
135+
});
136+
137+
await expect(
138+
resolveInstallableChannelPlugin({
139+
cfg: {
140+
agents: {
141+
list: [{ id: "alpha" }, { id: "beta" }],
142+
},
143+
},
144+
runtime: {} as never,
145+
rawChannel: "telegram",
146+
allowInstall: false,
147+
preferRegisteredPlugin: true,
148+
}),
149+
).rejects.toThrow("agent selection required");
150+
151+
expect(mocks.getLoadedChannelPlugin).toHaveBeenCalledWith("telegram");
152+
expect(mocks.getChannelPlugin).not.toHaveBeenCalled();
153+
expect(mocks.resolveDefaultAgentId).toHaveBeenCalledTimes(1);
154+
});
155+
156+
it("still requires a workspace owner before resolving an unregistered plugin", async () => {
157+
mocks.resolveDefaultAgentId.mockImplementation(() => {
158+
throw new Error("agent selection required");
159+
});
160+
161+
await expect(
162+
resolveInstallableChannelPlugin({
163+
cfg: {
164+
agents: {
165+
list: [{ id: "alpha" }, { id: "beta" }],
166+
},
167+
},
168+
runtime: {} as never,
169+
rawChannel: "workspace-channel",
170+
allowInstall: false,
171+
preferRegisteredPlugin: true,
172+
}),
173+
).rejects.toThrow("agent selection required");
174+
175+
expect(mocks.getLoadedChannelPlugin).toHaveBeenCalledWith("workspace-channel");
176+
expect(mocks.getChannelPlugin).not.toHaveBeenCalled();
177+
expect(mocks.resolveDefaultAgentId).toHaveBeenCalledTimes(1);
178+
expect(mocks.listChannelPluginCatalogEntries).not.toHaveBeenCalled();
179+
});
180+
89181
it("ignores untrusted workspace channel shadows during setup resolution", async () => {
90182
const config = {
91183
agents: { entries: { ops: {}, research: {} } },

‎src/commands/channel-setup/channel-plugin-resolution.ts‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ import {
55
listRawChannelPluginCatalogEntries,
66
type ChannelPluginCatalogEntry,
77
} from "../../channels/plugins/catalog.js";
8-
import { getChannelPlugin, normalizeChannelId } from "../../channels/plugins/index.js";
8+
import {
9+
getChannelPlugin,
10+
getLoadedChannelPlugin,
11+
normalizeChannelId,
12+
} from "../../channels/plugins/index.js";
913
import type { ChannelPlugin } from "../../channels/plugins/types.plugin.js";
1014
import type { ChannelId } from "../../channels/plugins/types.public.js";
1115
import type { OpenClawConfig } from "../../config/types.openclaw.js";
@@ -118,11 +122,28 @@ export async function resolveInstallableChannelPlugin(params: {
118122
channelId?: ChannelId;
119123
agentId?: string;
120124
allowInstall?: boolean;
125+
preferRegisteredPlugin?: boolean;
121126
prompter?: WizardPrompter;
122127
supports?: (plugin: ChannelPlugin) => boolean;
123128
}): Promise<ResolveInstallableChannelPluginResult> {
124129
const supports = params.supports ?? (() => true);
125130
let nextCfg = params.cfg;
131+
const directChannelId = params.channelId ?? normalizeChannelId(params.rawChannel);
132+
const registeredPlugin =
133+
params.preferRegisteredPlugin && directChannelId
134+
? getLoadedChannelPlugin(directChannelId)
135+
: undefined;
136+
if (params.preferRegisteredPlugin && directChannelId && registeredPlugin) {
137+
return {
138+
cfg: nextCfg,
139+
channelId: directChannelId,
140+
plugin: registeredPlugin,
141+
configChanged: false,
142+
pluginInstalled: false,
143+
supportsRequestedCapability: supports(registeredPlugin),
144+
};
145+
}
146+
126147
const workspaceDir = resolveWorkspaceDir(nextCfg, params.agentId);
127148
const catalogEntry =
128149
(params.rawChannel
@@ -135,7 +156,7 @@ export async function resolveInstallableChannelPlugin(params: {
135156
})
136157
: undefined);
137158
const channelId =
138-
params.channelId ??
159+
directChannelId ??
139160
resolveResolvedChannelId({
140161
rawChannel: params.rawChannel,
141162
catalogEntry,

0 commit comments

Comments
 (0)