Skip to content

Commit f7e6297

Browse files
authored
fix(discord): honor reply visibility for session-busy notices (#135444)
Use the prepared reply mode when completing exhausted session conflicts, recording delivered or suppressed outcomes before ingress settlement. Preserve retryability when an allowed terminal notice fails to send. Cover ambient, message-tool, direct, downstream suppression, and replay cases.
1 parent 5128f34 commit f7e6297

4 files changed

Lines changed: 94 additions & 20 deletions

File tree

‎docs/channels/discord.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,8 @@ Once DMs work, you can turn your server into a full workspace where each channel
257257

258258
If Discord shows typing and the logs show token usage but no posted message, check whether the turn was configured as an ambient room event or opted into message-tool visible replies.
259259

260+
Session-busy notices also respect this reply policy. For ambient events and message-tool replies, Discord records the failure and suppressed notice in Gateway logs without posting to the room.
261+
260262
<Tabs>
261263
<Tab title="Ask your agent">
262264
> "Allow my agent to respond on this server without having to be @mentioned"

‎extensions/discord/src/monitor/message-handler.process.abort-retry.test.ts‎

Lines changed: 71 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ import {
44
BASE_CHANNEL_ROUTE,
55
createAutomaticSourceDeliveryContext,
66
createBaseContext,
7+
createDirectMessageContextOverrides,
78
createNoQueuedDispatchResult,
89
deliverDiscordReply,
910
dispatchInboundMessageForTest as dispatchInboundMessage,
1011
formatDiscordReplySkip,
1112
getLastDispatchCtx,
13+
getLastDispatchReplyOptions,
1214
logVerboseForTest as logVerbose,
1315
recordInboundSessionForTest as recordInboundSession,
1416
runProcessDiscordMessage,
@@ -115,25 +117,89 @@ describe("processDiscordMessage reply session init conflict retry", () => {
115117
sleepSpy.mockRestore();
116118
});
117119

118-
it("commits replay ownership after a visible terminal failure notice", async () => {
120+
it.each([
121+
{
122+
name: "an automatic group request",
123+
visibleReplies: "automatic",
124+
inboundEventKind: "user_request",
125+
direct: false,
126+
expectedMode: "automatic",
127+
expectedSends: 1,
128+
},
129+
{
130+
name: "an ambient room event",
131+
visibleReplies: "automatic",
132+
inboundEventKind: "room_event",
133+
direct: false,
134+
expectedMode: "message_tool_only",
135+
expectedSends: 0,
136+
},
137+
{
138+
name: "a group request requiring the message tool",
139+
visibleReplies: "message_tool",
140+
inboundEventKind: "user_request",
141+
direct: false,
142+
expectedMode: "message_tool_only",
143+
expectedSends: 0,
144+
},
145+
{
146+
name: "a direct request despite the group message-tool policy",
147+
visibleReplies: "message_tool",
148+
inboundEventKind: "user_request",
149+
direct: true,
150+
expectedMode: "automatic",
151+
expectedSends: 1,
152+
},
153+
] as const)("completes $name with a recorded terminal notice outcome", async (scenario) => {
119154
const sleepSpy = vi.mocked(sleepWithAbort).mockResolvedValue(undefined);
120155
const originalError = conflictError();
121156
dispatchInboundMessage.mockRejectedValue(originalError);
157+
const errorLog = vi.fn();
122158

123-
const ctx = await createBaseContext();
159+
const ctx = await createBaseContext({
160+
...(scenario.direct ? createDirectMessageContextOverrides() : {}),
161+
inboundEventKind: scenario.inboundEventKind,
162+
shouldRequireMention: false,
163+
effectiveWasMentioned: scenario.inboundEventKind !== "room_event",
164+
cfg: { messages: { groupChat: { visibleReplies: scenario.visibleReplies } } },
165+
runtime: { log: vi.fn(), error: errorLog },
166+
});
124167
await expect(runProcessDiscordMessage(ctx)).resolves.toBeUndefined();
125168

126169
expect(dispatchInboundMessage).toHaveBeenCalledTimes(4);
127170
expect(recordInboundSession).toHaveBeenCalledTimes(1);
128171
expect(sleepSpy).toHaveBeenNthCalledWith(1, 250, undefined);
129172
expect(sleepSpy).toHaveBeenNthCalledWith(2, 1_000, undefined);
130173
expect(sleepSpy).toHaveBeenNthCalledWith(3, 2_500, undefined);
131-
expectFreshFinalText(
132-
"⚠️ Couldn't process this message because the session stayed busy. Please try again in a moment.",
174+
expect(getLastDispatchReplyOptions()?.sourceReplyDeliveryMode).toBe(scenario.expectedMode);
175+
expect(deliverDiscordReply).toHaveBeenCalledTimes(scenario.expectedSends);
176+
expect(errorLog).toHaveBeenCalledExactlyOnceWith(
177+
expect.stringContaining(
178+
`terminal notice ${scenario.expectedSends === 1 ? "delivered" : "suppressed"}`,
179+
),
133180
);
181+
if (scenario.expectedSends === 1) {
182+
expectFreshFinalText(
183+
"⚠️ Couldn't process this message because the session stayed busy. Please try again in a moment.",
184+
);
185+
}
134186
sleepSpy.mockRestore();
135187
});
136188

189+
it("records downstream suppression without claiming its terminal notice was delivered", async () => {
190+
dispatchInboundMessage.mockRejectedValue(conflictError());
191+
deliverDiscordReply.mockResolvedValueOnce({ visibleReplySent: false });
192+
const errorLog = vi.fn();
193+
const ctx = await createBaseContext({ runtime: { log: vi.fn(), error: errorLog } });
194+
195+
await expect(runProcessDiscordMessage(ctx)).resolves.toBeUndefined();
196+
197+
expect(deliverDiscordReply).toHaveBeenCalledTimes(1);
198+
expect(errorLog).toHaveBeenCalledExactlyOnceWith(
199+
expect.stringContaining("terminal notice suppressed"),
200+
);
201+
});
202+
137203
it("keeps exhaustion retryable when the visible failure notice cannot land", async () => {
138204
const sleepSpy = vi.mocked(sleepWithAbort).mockResolvedValue(undefined);
139205
const originalError = conflictError();
@@ -157,8 +223,7 @@ describe("processDiscordMessage reply session init conflict retry", () => {
157223

158224
it("rebuilds a released replay without duplicating its pending history", async () => {
159225
const sleepSpy = vi.mocked(sleepWithAbort).mockResolvedValue(undefined);
160-
dispatchInboundMessage.mockRejectedValue(conflictError());
161-
deliverDiscordReply.mockRejectedValueOnce(new Error("Discord unavailable"));
226+
dispatchInboundMessage.mockRejectedValueOnce(new Error("dispatch failed before completion"));
162227
const guildHistories = new Map();
163228
const createReplayContext = () =>
164229
createBaseContext({

‎extensions/discord/src/monitor/message-handler.process.ts‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -693,8 +693,9 @@ async function processDiscordMessageInner(
693693
return;
694694
}
695695
dispatchError = true;
696-
const conflictCompleted = await completeDiscordSessionConflict(
696+
const conflictOutcome = await completeDiscordSessionConflict(
697697
err,
698+
sourceReplyDeliveryMode,
698699
(payload, info) =>
699700
deliverDiscordPayload(payload, {
700701
...info,
@@ -703,8 +704,12 @@ async function processDiscordMessageInner(
703704
}),
704705
onDiscordDeliveryError,
705706
);
706-
if (conflictCompleted) {
707-
// The visible terminal notice owns this event, so replay can commit.
707+
if (conflictOutcome) {
708+
runtime.error(
709+
`discord: reply session init conflict exhausted; terminal notice ${conflictOutcome} ` +
710+
`(sourceReplyDeliveryMode=${sourceReplyDeliveryMode}, message=${message.id}, session=${persistedSessionKey})`,
711+
);
712+
// Both a delivered notice and recorded policy suppression consume the event.
708713
return;
709714
}
710715
throw err;

‎extensions/discord/src/monitor/message-handler.retry.ts‎

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,34 @@
1+
import type { SourceReplyDeliveryMode } from "openclaw/plugin-sdk/reply-runtime";
2+
13
const REPLY_SESSION_INIT_CONFLICT_MESSAGE_RE = /^reply session initialization conflicted for \S+$/u;
24
const DISCORD_SESSION_CONFLICT_FAILURE_TEXT =
35
"⚠️ Couldn't process this message because the session stayed busy. Please try again in a moment.";
46

57
type TerminalFailureDelivery = (
68
payload: { text: string; isError: true },
79
info: { kind: "final" },
8-
) => Promise<unknown>;
10+
) => Promise<{ visibleReplySent: boolean }>;
911
type DeliveryErrorHandler = (error: unknown, info: { kind: string }) => void;
1012

11-
function isReplySessionInitConflictError(error: unknown): boolean {
12-
const message = error instanceof Error ? error.message : String(error);
13-
return REPLY_SESSION_INIT_CONFLICT_MESSAGE_RE.test(message);
14-
}
15-
1613
export async function completeDiscordSessionConflict(
1714
error: unknown,
15+
sourceReplyDeliveryMode: SourceReplyDeliveryMode,
1816
deliver: TerminalFailureDelivery,
1917
onDeliveryError: DeliveryErrorHandler,
20-
): Promise<boolean> {
21-
if (!isReplySessionInitConflictError(error)) {
22-
return false;
18+
): Promise<"delivered" | "suppressed" | undefined> {
19+
const message = error instanceof Error ? error.message : String(error);
20+
if (!REPLY_SESSION_INIT_CONFLICT_MESSAGE_RE.test(message)) {
21+
return undefined;
22+
}
23+
if (sourceReplyDeliveryMode === "message_tool_only") {
24+
return "suppressed";
2325
}
2426
try {
25-
await deliver(
27+
const result = await deliver(
2628
{ text: DISCORD_SESSION_CONFLICT_FAILURE_TEXT, isError: true },
2729
{ kind: "final" },
2830
);
29-
return true;
31+
return result.visibleReplySent ? "delivered" : "suppressed";
3032
} catch (deliveryError) {
3133
// Keep the conflict retryable when its visible terminal notice cannot land.
3234
onDeliveryError(deliveryError, { kind: "final" });

0 commit comments

Comments
 (0)