Skip to content

Commit 1dba34b

Browse files
Leon-SK668obviyus
andauthored
fix(cli): preserve structured core send failures (#129728)
Keep durable delivery outcomes available to CLI JSON formatting while preserving the throwing contract for non-CLI callers and leaving delivery policy unchanged. Co-authored-by: Leon-SK668 <[email protected]> Co-authored-by: Ayaan Zaidi <[email protected]>
1 parent ae49990 commit 1dba34b

2 files changed

Lines changed: 107 additions & 1 deletion

File tree

‎src/infra/outbound/message-action-send.core.test.ts‎

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
// Covers core message-action send fallback, TTS application, and durable send
22
// policy after plugin preparation is absent.
33
import { afterEach, describe, expect, it, vi } from "vitest";
4+
import {
5+
GATEWAY_CLIENT_MODES,
6+
GATEWAY_CLIENT_NAMES,
7+
} from "../../../packages/gateway-protocol/src/client-info.js";
48
import { getReplyPayloadMetadata } from "../../auto-reply/reply-payload.js";
59
import type { OpenClawConfig } from "../../config/config.js";
610
import { setActivePluginRegistry } from "../../plugins/runtime.js";
711
import { createOutboundTestPlugin, createTestRegistry } from "../../test-utils/channel-plugins.js";
12+
import { OutboundDeliveryError } from "./deliver-types.js";
813
import { runMessageAction } from "./message-action-runner.js";
914

1015
const ttsMocks = vi.hoisted(() => ({
@@ -77,6 +82,104 @@ describe("runMessageAction core send routing", () => {
7782
.mockReset()
7883
.mockImplementation(async (params: { payload: unknown }) => params.payload);
7984
});
85+
86+
it.each([
87+
{
88+
label: "failed",
89+
error: new Error("provider rejected the message"),
90+
expectedStatus: "failed" as const,
91+
expectedMessageId: undefined,
92+
},
93+
{
94+
label: "partial_failed",
95+
error: new OutboundDeliveryError("second send failed", {
96+
cause: new Error("provider rejected the attachment"),
97+
results: [{ channel: "testchat", messageId: "first-send-1" }],
98+
stage: "platform_send",
99+
}),
100+
expectedStatus: "partial_failed" as const,
101+
expectedMessageId: "first-send-1",
102+
},
103+
])(
104+
"returns structured $label core delivery outcomes to CLI callers",
105+
async ({ error, expectedStatus, expectedMessageId }) => {
106+
const sendText = vi.fn().mockRejectedValue(error);
107+
setActivePluginRegistry(
108+
createTestRegistry([
109+
{
110+
pluginId: "testchat",
111+
source: "test",
112+
plugin: createOutboundTestPlugin({
113+
id: "testchat",
114+
outbound: { deliveryMode: "direct", sendText },
115+
}),
116+
},
117+
]),
118+
);
119+
120+
const result = await runMessageAction({
121+
cfg: {
122+
channels: { testchat: { enabled: true } },
123+
} as OpenClawConfig,
124+
action: "send",
125+
params: {
126+
channel: "testchat",
127+
target: "channel:abc",
128+
message: "hello",
129+
},
130+
gateway: {
131+
clientName: GATEWAY_CLIENT_NAMES.CLI,
132+
mode: GATEWAY_CLIENT_MODES.CLI,
133+
},
134+
conversationReadOrigin: "direct-operator",
135+
dryRun: false,
136+
});
137+
138+
expect(result).toMatchObject({
139+
kind: "send",
140+
handledBy: "core",
141+
sendResult: {
142+
deliveryStatus: expectedStatus,
143+
error: expect.any(String),
144+
...(expectedMessageId ? { result: { messageId: expectedMessageId } } : {}),
145+
},
146+
});
147+
expect(sendText).toHaveBeenCalledOnce();
148+
},
149+
);
150+
151+
it("keeps throwing core delivery failures for non-CLI callers", async () => {
152+
const sendText = vi.fn().mockRejectedValue(new Error("provider rejected the message"));
153+
setActivePluginRegistry(
154+
createTestRegistry([
155+
{
156+
pluginId: "testchat",
157+
source: "test",
158+
plugin: createOutboundTestPlugin({
159+
id: "testchat",
160+
outbound: { deliveryMode: "direct", sendText },
161+
}),
162+
},
163+
]),
164+
);
165+
166+
await expect(
167+
runMessageAction({
168+
cfg: {
169+
channels: { testchat: { enabled: true } },
170+
} as OpenClawConfig,
171+
action: "send",
172+
params: {
173+
channel: "testchat",
174+
target: "channel:abc",
175+
message: "hello",
176+
},
177+
dryRun: false,
178+
}),
179+
).rejects.toThrow("provider rejected the message");
180+
expect(sendText).toHaveBeenCalledOnce();
181+
});
182+
80183
it("does not misclassify send as poll when zero-valued poll params are present", async () => {
81184
const sendMedia = vi.fn().mockResolvedValue({
82185
channel: "testchat",

‎src/infra/outbound/message.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import type { OutboundMediaAccess } from "../../media/load-options.js";
1818
import type { PollInput } from "../../polls.js";
1919
import { normalizePollInput } from "../../polls.js";
2020
import { createLazyRuntimeModule } from "../../shared/lazy-runtime.js";
21+
import { GATEWAY_CLIENT_NAMES } from "../../utils/message-channel.js";
2122
import type { DeliveryQueueCompletionRetention } from "../delivery-queue-sqlite.js";
2223
import { formatErrorMessage } from "../errors.js";
2324
import { resolveMessageChannelSelection } from "./channel-selection.js";
@@ -476,7 +477,9 @@ export async function sendMessage(params: MessageSendParams): Promise<MessageSen
476477
}
477478
: undefined,
478479
});
479-
if (!params.bestEffort && (send.status === "failed" || send.status === "partial_failed")) {
480+
const shouldThrowFailure =
481+
!params.bestEffort && params.gateway?.clientName !== GATEWAY_CLIENT_NAMES.CLI;
482+
if (shouldThrowFailure && (send.status === "failed" || send.status === "partial_failed")) {
480483
throw send.error;
481484
}
482485
const results = send.status === "sent" || send.status === "partial_failed" ? send.results : [];

0 commit comments

Comments
 (0)