Skip to content

Commit 122c6e4

Browse files
authored
refactor(message): keep read fallback policy at call site (#108930)
1 parent 71bea81 commit 122c6e4

3 files changed

Lines changed: 17 additions & 53 deletions

File tree

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

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -276,40 +276,6 @@ describe("resolveMessageChannelSelection", () => {
276276
});
277277
});
278278

279-
it("rejects an explicit unknown channel instead of using the tool context", async () => {
280-
await expect(
281-
expectResolvedSelection({
282-
cfg: {} as never,
283-
channel: "channel:C123",
284-
fallbackChannel: "beta",
285-
requireExplicitChannelAvailable: true,
286-
}),
287-
).rejects.toThrow("Unknown channel: channel:c123");
288-
});
289-
290-
it("rejects an explicit unavailable channel instead of using the tool context", async () => {
291-
const cfg = {} as never;
292-
mocks.resolveOutboundChannelPlugin.mockImplementation(({ channel }: { channel: string }) =>
293-
channel === "beta" ? { id: "beta" } : undefined,
294-
);
295-
296-
await expect(
297-
expectResolvedSelection({
298-
cfg,
299-
channel: "alpha",
300-
fallbackChannel: "beta",
301-
requireExplicitChannelAvailable: true,
302-
}),
303-
).rejects.toThrow("Channel is unavailable: alpha");
304-
305-
expect(mocks.resolveOutboundChannelPlugin).toHaveBeenCalledTimes(1);
306-
expect(mocks.resolveOutboundChannelPlugin).toHaveBeenCalledWith({
307-
channel: "alpha",
308-
cfg,
309-
allowBootstrap: true,
310-
});
311-
});
312-
313279
it.each([
314280
{
315281
params: { cfg: {} as never, channel: "channel:C123", fallbackChannel: "not-a-channel" },

src/infra/outbound/channel-selection.ts

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -219,34 +219,28 @@ export async function resolveMessageChannelSelection(params: {
219219
cfg: OpenClawConfig;
220220
channel?: string | null;
221221
fallbackChannel?: string | null;
222-
requireExplicitChannelAvailable?: boolean;
223222
}): Promise<{
224223
channel: MessageChannelId;
225224
configured: MessageChannelId[];
226225
source: MessageChannelSelectionSource;
227226
}> {
228227
const normalized = normalizeMessageChannel(params.channel);
229228
if (normalized) {
230-
if (params.requireExplicitChannelAvailable && !isKnownChannel(normalized)) {
231-
throw new Error(`Unknown channel: ${normalized}`);
232-
}
233229
const availableExplicit = resolveAvailableKnownChannel({
234230
cfg: params.cfg,
235231
value: normalized,
236232
});
237233
if (!availableExplicit) {
238-
if (!params.requireExplicitChannelAvailable) {
239-
const fallback = resolveAvailableKnownChannel({
240-
cfg: params.cfg,
241-
value: params.fallbackChannel,
242-
});
243-
if (fallback) {
244-
return {
245-
channel: fallback,
246-
configured: [],
247-
source: "tool-context-fallback",
248-
};
249-
}
234+
const fallback = resolveAvailableKnownChannel({
235+
cfg: params.cfg,
236+
value: params.fallbackChannel,
237+
});
238+
if (fallback) {
239+
return {
240+
channel: fallback,
241+
configured: [],
242+
source: "tool-context-fallback",
243+
};
250244
}
251245
if (!isKnownChannel(normalized)) {
252246
throw new Error(`Unknown channel: ${normalized}`);

src/infra/outbound/message-action-runner.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -403,11 +403,15 @@ async function resolveChannel(
403403
toolContext?: { currentChannelProvider?: string },
404404
action?: ChannelMessageActionName,
405405
) {
406+
const channel = readStringParam(params, "channel");
407+
// Explicit reads must never switch to the source conversation when their
408+
// requested provider is unknown or unavailable.
409+
const fallbackChannel =
410+
action === "read" && channel ? undefined : toolContext?.currentChannelProvider;
406411
const selection = await resolveMessageChannelSelection({
407412
cfg,
408-
channel: readStringParam(params, "channel"),
409-
fallbackChannel: toolContext?.currentChannelProvider,
410-
requireExplicitChannelAvailable: action === "read",
413+
channel,
414+
fallbackChannel,
411415
});
412416
if (selection.source === "tool-context-fallback") {
413417
params.channel = selection.channel;

0 commit comments

Comments
 (0)