Skip to content

Commit 1224fe4

Browse files
committed
fix(gateway): prevent restart replay after final delivery
1 parent 0a6a1d9 commit 1224fe4

74 files changed

Lines changed: 2263 additions & 782 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎docs/.generated/plugin-sdk-api-baseline.sha256‎

Lines changed: 80 additions & 80 deletions
Large diffs are not rendered by default.

‎extensions/discord/src/monitor/agent-components.dispatch.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@ export async function dispatchDiscordComponentEvent(params: {
323323
chunkMode: resolveChunkMode(ctx.cfg, "discord", accountId),
324324
mediaLocalRoots,
325325
kind: info.kind,
326+
bindPendingFinalDelivery: info.bindPendingFinalDelivery,
326327
});
327328
if (result.visibleReplySent) {
328329
replyReference.markSent();

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,10 @@ async function processDiscordMessageInner(
271271

272272
const deliverDiscordPayload = async (
273273
payload: ReplyPayload,
274-
info: { kind: ReplyDispatchKind },
274+
info: {
275+
kind: ReplyDispatchKind;
276+
bindPendingFinalDelivery?: <T extends ReplyPayload>(payload: T) => T;
277+
},
275278
options?: {
276279
allowFallbackOnlyToolWarning?: boolean;
277280
allowProgressBlock?: boolean;
@@ -328,6 +331,7 @@ async function processDiscordMessageInner(
328331
threadBindings,
329332
mediaLocalRoots,
330333
kind: "block",
334+
bindPendingFinalDelivery: info.bindPendingFinalDelivery,
331335
});
332336
if (result.visibleReplySent) {
333337
replyReference.markSent();
@@ -484,6 +488,7 @@ async function processDiscordMessageInner(
484488
mediaLocalRoots,
485489
allowedMentions,
486490
kind: info.kind,
491+
bindPendingFinalDelivery: info.bindPendingFinalDelivery,
487492
});
488493
return deliveryResult.visibleReplySent;
489494
},
@@ -542,6 +547,7 @@ async function processDiscordMessageInner(
542547
threadBindings,
543548
mediaLocalRoots,
544549
kind: info.kind,
550+
bindPendingFinalDelivery: info.bindPendingFinalDelivery,
545551
});
546552
if (!result.visibleReplySent) {
547553
return result;

‎extensions/discord/src/monitor/reply-delivery.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,13 +229,16 @@ export async function deliverDiscordReply(params: {
229229
mediaLocalRoots?: readonly string[];
230230
allowedMentions?: DiscordAllowedMentions;
231231
kind: "tool" | "block" | "final";
232+
bindPendingFinalDelivery?: <T extends ReplyPayload>(payload: T) => T;
232233
}) {
233234
void params.runtime;
234235

235236
const delivery = resolveDiscordDeliveryOptions(params);
236237
const payloads = sanitizeDiscordFrontChannelReplyPayloads(params.replies, {
237238
kind: params.kind,
238-
}).map(formatDiscordReasoningPayload);
239+
})
240+
.map(formatDiscordReasoningPayload)
241+
.map((payload) => params.bindPendingFinalDelivery?.(payload) ?? payload);
239242
if (payloads.length === 0) {
240243
return {
241244
visibleReplySent: false,

‎extensions/discord/src/monitor/reply-safety.ts‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,7 @@ export function sanitizeDiscordFrontChannelReplyPayloads(
8686
: sanitizeDiscordFrontChannelText(payload.text)
8787
: payload.text;
8888
const nextPayload =
89-
safeText === payload.text
90-
? payload
91-
: ({ ...payload, text: safeText || undefined } as ReplyPayload);
89+
safeText === payload.text ? payload : { ...payload, text: safeText || undefined };
9290
const nextParts = resolveSendableOutboundReplyParts(nextPayload);
9391
if (!nextParts.hasContent && !hasNonTextReplyPayloadContent(nextPayload)) {
9492
continue;

‎extensions/discord/src/outbound-adapter.ts‎

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ async function maybeSendDiscordWebhookText(params: {
6565
accountId?: string | null;
6666
identity?: OutboundIdentity;
6767
replyToId?: string | null;
68+
onPlatformSendDispatch?: () => Promise<void>;
6869
}): Promise<{ messageId: string; channelId: string } | null> {
6970
if (params.threadId == null) {
7071
return null;
@@ -96,6 +97,7 @@ async function maybeSendDiscordWebhookText(params: {
9697
replyTo: params.replyToId ?? undefined,
9798
username: persona.username,
9899
avatarUrl: persona.avatarUrl,
100+
onPlatformSendDispatch: params.onPlatformSendDispatch,
99101
});
100102
return result;
101103
}
@@ -130,6 +132,7 @@ async function resolveDiscordOutboundMessageSend(params: DiscordOutboundMessageC
130132
await params.onDeliveryResult?.(attachChannelToResult("discord", result));
131133
}
132134
: undefined,
135+
onPlatformSendDispatch: params.onPlatformSendDispatch,
133136
},
134137
};
135138
}
@@ -173,16 +176,29 @@ export const discordOutbound: ChannelOutboundAdapter = {
173176
channel: "discord",
174177
sendText: async (ctx) => {
175178
if (!ctx.silent) {
176-
const webhookResult = await maybeSendDiscordWebhookText({
177-
cfg: ctx.cfg,
178-
text: ctx.text,
179-
threadId: ctx.threadId,
180-
accountId: ctx.accountId,
181-
identity: ctx.identity,
182-
replyToId: ctx.replyToId,
183-
}).catch(() => null);
184-
if (webhookResult) {
185-
return webhookResult;
179+
let webhookSelected = false;
180+
try {
181+
const webhookResult = await maybeSendDiscordWebhookText({
182+
cfg: ctx.cfg,
183+
text: ctx.text,
184+
threadId: ctx.threadId,
185+
accountId: ctx.accountId,
186+
identity: ctx.identity,
187+
replyToId: ctx.replyToId,
188+
onPlatformSendDispatch: ctx.onPlatformSendDispatch
189+
? async () => {
190+
webhookSelected = true;
191+
await ctx.onPlatformSendDispatch?.();
192+
}
193+
: undefined,
194+
});
195+
if (webhookResult) {
196+
return webhookResult;
197+
}
198+
} catch (error) {
199+
if (webhookSelected) {
200+
throw error;
201+
}
186202
}
187203
}
188204
const { send, target, options } = await resolveDiscordOutboundMessageSend(ctx);
@@ -202,6 +218,7 @@ export const discordOutbound: ChannelOutboundAdapter = {
202218
mediaAccess: ctx.mediaAccess,
203219
mediaLocalRoots: ctx.mediaLocalRoots,
204220
mediaReadFile: ctx.mediaReadFile,
221+
onPlatformSendDispatch: ctx.onPlatformSendDispatch,
205222
});
206223
}
207224
const mediaOptions = {
@@ -235,13 +252,14 @@ export const discordOutbound: ChannelOutboundAdapter = {
235252
}
236253
return await send(target, ctx.text, mediaOptions);
237254
},
238-
sendPoll: async ({ cfg, to, poll, accountId, threadId, silent }) =>
255+
sendPoll: async ({ cfg, to, poll, accountId, threadId, silent, onPlatformSendDispatch }) =>
239256
await (
240257
await loadDiscordSendRuntime()
241258
).sendPollDiscord(resolveDiscordOutboundTarget({ to, threadId }), poll, {
242259
accountId: accountId ?? undefined,
243260
silent: silent ?? undefined,
244261
cfg,
262+
onPlatformSendDispatch,
245263
}),
246264
}),
247265
afterDeliverPayload: async ({ cfg, target, payload, results }) => {

‎extensions/discord/src/outbound-payload.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ function resolveDiscordDeliveryOptions(
5757
accountId: ctx.accountId ?? undefined,
5858
silent: ctx.silent ?? undefined,
5959
cfg: ctx.cfg,
60+
onPlatformSendDispatch: ctx.onPlatformSendDispatch,
6061
};
6162
}
6263

‎extensions/discord/src/send.components.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ type DiscordComponentSendOpts = {
172172
allowedMentions?: DiscordAllowedMentions;
173173
/** Persist the concrete platform send before component bookkeeping can fail. */
174174
onDeliveryResult?: (result: DiscordSendResult) => Promise<void> | void;
175+
onPlatformSendDispatch?: () => Promise<void>;
175176
};
176177

177178
export function registerBuiltDiscordComponentMessage(params: {
@@ -291,6 +292,7 @@ export async function sendDiscordComponentMessage(
291292
tableMode: opts.tableMode,
292293
chunkMode: opts.chunkMode,
293294
onDeliveryResult: opts.onDeliveryResult,
295+
onPlatformSendDispatch: opts.onPlatformSendDispatch,
294296
...(opts.suppressEmbeds === undefined ? {} : { suppressEmbeds: opts.suppressEmbeds }),
295297
});
296298
}
@@ -321,6 +323,7 @@ export async function sendDiscordComponentMessage(
321323

322324
let result: { id: string; channel_id: string };
323325
try {
326+
await opts.onPlatformSendDispatch?.();
324327
result = (await request(
325328
() =>
326329
createChannelMessage<{ id: string; channel_id: string }>(rest, channelId, {

‎extensions/discord/src/send.outbound.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ type DiscordSendOpts = {
7070
allowedMentions?: DiscordAllowedMentions;
7171
/** Persist each concrete platform send before any later chunk can fail. */
7272
onDeliveryResult?: (result: DiscordSendResult) => Promise<void> | void;
73+
/** @internal Refresh durable custody immediately before Discord REST I/O. */
74+
onPlatformSendDispatch?: () => Promise<void>;
7375
};
7476

7577
type DiscordClientRequest = ReturnType<typeof createDiscordClient>["request"];
@@ -92,6 +94,7 @@ async function sendDiscordThreadTextChunks(params: {
9294
suppressEmbeds?: boolean;
9395
allowedMentions?: DiscordAllowedMentions;
9496
onResult?: DiscordSendProgress;
97+
onPlatformSendDispatch?: () => Promise<void>;
9598
}): Promise<void> {
9699
for (const chunk of params.chunks) {
97100
await sendDiscordText({
@@ -106,6 +109,7 @@ async function sendDiscordThreadTextChunks(params: {
106109
allowedMentions: params.allowedMentions,
107110
maxChars: params.maxChars,
108111
onResult: params.onResult,
112+
onPlatformSendDispatch: params.onPlatformSendDispatch,
109113
});
110114
}
111115
}
@@ -238,6 +242,7 @@ export async function sendMessageDiscord(
238242
});
239243
let threadRes: { id: string; message?: { id: string; channel_id: string } };
240244
try {
245+
await opts.onPlatformSendDispatch?.();
241246
threadRes = (await request(
242247
() =>
243248
createThread<{ id: string; message?: { id: string; channel_id: string } }>(
@@ -309,6 +314,7 @@ export async function sendMessageDiscord(
309314
allowedMentions: opts.allowedMentions,
310315
maxChars: textLimit,
311316
onResult: reportThreadResult,
317+
onPlatformSendDispatch: opts.onPlatformSendDispatch,
312318
});
313319
await sendDiscordThreadTextChunks({
314320
rest,
@@ -322,6 +328,7 @@ export async function sendMessageDiscord(
322328
suppressEmbeds,
323329
allowedMentions: opts.allowedMentions,
324330
onResult: reportThreadResult,
331+
onPlatformSendDispatch: opts.onPlatformSendDispatch,
325332
});
326333
} else {
327334
await sendDiscordThreadTextChunks({
@@ -336,6 +343,7 @@ export async function sendMessageDiscord(
336343
suppressEmbeds,
337344
allowedMentions: opts.allowedMentions,
338345
onResult: reportThreadResult,
346+
onPlatformSendDispatch: opts.onPlatformSendDispatch,
339347
});
340348
}
341349
} catch (err) {
@@ -391,6 +399,7 @@ export async function sendMessageDiscord(
391399
allowedMentions: opts.allowedMentions,
392400
maxChars: textLimit,
393401
onResult: reportResult,
402+
onPlatformSendDispatch: opts.onPlatformSendDispatch,
394403
});
395404
} else {
396405
result = await sendDiscordText({
@@ -408,6 +417,7 @@ export async function sendMessageDiscord(
408417
allowedMentions: opts.allowedMentions,
409418
maxChars: textLimit,
410419
onResult: reportResult,
420+
onPlatformSendDispatch: opts.onPlatformSendDispatch,
411421
});
412422
}
413423
} catch (err) {
@@ -447,6 +457,7 @@ export async function sendStickerDiscord(
447457
enforce_nonce: true,
448458
...(flags ? { flags } : {}),
449459
};
460+
await opts.onPlatformSendDispatch?.();
450461
const res = (await request(
451462
() => createChannelMessage<{ id: string; channel_id: string }>(rest, channelId, { body }),
452463
"sticker",
@@ -474,6 +485,7 @@ export async function sendPollDiscord(
474485
enforce_nonce: true,
475486
...(flags ? { flags } : {}),
476487
};
488+
await opts.onPlatformSendDispatch?.();
477489
const res = (await request(
478490
() => createChannelMessage<{ id: string; channel_id: string }>(rest, channelId, { body }),
479491
"poll",

‎extensions/discord/src/send.shared.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,7 @@ type DiscordTextSendParams = {
325325
suppressEmbeds?: boolean;
326326
maxChars?: number;
327327
onResult?: DiscordSendProgress;
328+
onPlatformSendDispatch?: () => Promise<void>;
328329
};
329330

330331
async function sendDiscordText(params: DiscordTextSendParams) {
@@ -343,6 +344,7 @@ async function sendDiscordText(params: DiscordTextSendParams) {
343344
suppressEmbeds,
344345
maxChars,
345346
onResult,
347+
onPlatformSendDispatch,
346348
} = params;
347349
if (!text.trim()) {
348350
throw new Error("Message must be non-empty for Discord sends");
@@ -369,6 +371,7 @@ async function sendDiscordText(params: DiscordTextSendParams) {
369371
flags,
370372
replyTo: chunkReplyTo,
371373
});
374+
await onPlatformSendDispatch?.();
372375
const result = (await request(
373376
() => createChannelMessage<{ id: string; channel_id: string }>(rest, channelId, { body }),
374377
"text",
@@ -429,6 +432,7 @@ async function sendDiscordMedia(params: DiscordMediaSendParams) {
429432
suppressEmbeds,
430433
maxChars,
431434
onResult,
435+
onPlatformSendDispatch,
432436
} = params;
433437
const media = await loadWebMedia(
434438
mediaUrl,
@@ -472,6 +476,7 @@ async function sendDiscordMedia(params: DiscordMediaSendParams) {
472476
});
473477
let res: { id: string; channel_id: string };
474478
try {
479+
await onPlatformSendDispatch?.();
475480
res = (await request(
476481
() => createChannelMessage<{ id: string; channel_id: string }>(rest, channelId, { body }),
477482
"media",
@@ -496,6 +501,7 @@ async function sendDiscordMedia(params: DiscordMediaSendParams) {
496501
allowedMentions,
497502
maxChars,
498503
onResult,
504+
onPlatformSendDispatch,
499505
});
500506
}
501507
await onResult?.(res, "media", reply?.messageId);

0 commit comments

Comments
 (0)