From 7cf507f654ec11fc11f2a48e2340fc1a8e931793 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BF=85=E8=86=80?= Date: Fri, 23 Oct 2026 23:22:29 +0800 Subject: [PATCH] fix(discord): reuse nonces after lost responses A successful create may lose its response. Reuse a stable nonce with enforce_nonce so retries recover that message instead of posting duplicates or accepting an unreadable success. --- server/lib/drivers/discord/index.ts | 8 +++++++- server/lib/drivers/discord/rest.ts | 24 ++++++++++++++---------- server/lib/types.ts | 7 +++++++ 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/server/lib/drivers/discord/index.ts b/server/lib/drivers/discord/index.ts index ee04cdc..1c15923 100644 --- a/server/lib/drivers/discord/index.ts +++ b/server/lib/drivers/discord/index.ts @@ -12,7 +12,13 @@ export class DiscordDriver implements PlatformDriver { return { ok: false, error: "target.channelId is required", errorCode: "NO_TARGET" }; } const token = env.DISCORD_TOKEN ?? ""; - return sendMessage(token, channelId, renderNeutralMessage(message), target.threadId); + return sendMessage( + token, + channelId, + renderNeutralMessage(message), + target.threadId, + message.sendKey, + ); } async edit( diff --git a/server/lib/drivers/discord/rest.ts b/server/lib/drivers/discord/rest.ts index a31e58a..e389e26 100644 --- a/server/lib/drivers/discord/rest.ts +++ b/server/lib/drivers/discord/rest.ts @@ -81,15 +81,11 @@ async function request( }; } - let messageId: string | undefined; - try { - const data = (await res.json()) as DiscordMessage; - messageId = data.id; - } catch { - // ignore malformed success body - } - - return { ok: true, status: res.status, messageId, attempts: attempt + 1 }; + // A success whose body cannot be read (cut off, timed out) is not known + // to have happened: tried again — a create carries its nonce, so it is + // never doubled. + const data = (await res.json()) as DiscordMessage | null; + return { ok: true, status: res.status, messageId: data?.id, attempts: attempt + 1 }; } catch (err) { log.error({ err, attempt, channelId }, "Failed to send message"); if (attempt === 2) @@ -106,11 +102,19 @@ export async function sendMessage( channelId: string, message: unknown, threadId?: string, + sendKey?: string, ): Promise { // Ids are path segments: encoded, so no stored value can address another // resource (`x/../123` is never the channel `123`). const url = `${DISCORD_API}/channels/${encodeURIComponent(threadId || channelId)}/messages`; - return request(url, "POST", token, message, channelId); + // One nonce for every attempt of this send — and, given a `sendKey`, for + // every attempt of the delivery: when a create went through but its + // response was lost, Discord answers the retry with that same message + // instead of posting a second one (`enforce_nonce`, a few minutes' window). + const nonce = (sendKey ?? crypto.randomUUID().replace(/-/g, "")).slice(0, 25); + const body = + message && typeof message === "object" ? { ...message, nonce, enforce_nonce: true } : message; + return request(url, "POST", token, body, channelId); } export async function editMessage( diff --git a/server/lib/types.ts b/server/lib/types.ts index 5cf1f45..92faac7 100644 --- a/server/lib/types.ts +++ b/server/lib/types.ts @@ -326,6 +326,13 @@ export interface NeutralMessage { * only to find a message tracked under the old key (then re-keyed). */ legacyUpdateKey?: string; + /** + * Stable identity of this message for one target, the same in every + * attempt of a delivery (set by dispatch from delivery × route × target). + * Platforms with request idempotency (Discord `nonce`, Feishu `uuid`) use + * it, so a retry after a lost response never posts the message twice. + */ + sendKey?: string; /** * Discord role ids to mention in the message content (set by dispatch from * the route's `discordRoleIds`). Only used by the Discord driver.