Skip to content

Commit 06600e2

Browse files
committed
fix(discord): unblock ingress after retry exhaustion
1 parent ac11091 commit 06600e2

10 files changed

Lines changed: 512 additions & 56 deletions

File tree

‎docs/plugins/sdk-channel-outbound.md‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,15 @@ the transport callback instead of dispatching an event that was not made
4747
durable. At claim time it decodes the versioned payload, re-runs `inspect`, and
4848
rejects an id or lane mismatch before delivery.
4949

50-
`deliver` receives `onAdopted`, `onDeferred`, `onAdoptionFinalizing`,
51-
`onAbandoned`, and `abortSignal`. Returning without an explicit handoff marks a
52-
terminal no-dispatch event adopted. `admission` is always `exclusive`. A
53-
deferred handoff keeps the claim held, while shutdown or abort leaves unadopted
54-
work retryable. The monitor tracks delivery independently from claim settlement
55-
because adoption can tombstone a row before the channel's delivery promise
56-
returns.
50+
`deliver` receives `onAdopted`, `onDeferred`, `onAdoptionFinalizing`, `onFailed`,
51+
`onCancelled`, `onAbandoned`, and `abortSignal`. Use `onFailed` for delivery
52+
errors, `onCancelled` for explicit pre-adoption cancellation that must preserve
53+
retry accounting, and `onAbandoned` when a non-adopted turn should consume a
54+
retry attempt. Returning without an explicit handoff marks a terminal
55+
no-dispatch event adopted. `admission` is always `exclusive`. A deferred handoff
56+
keeps the claim held, while shutdown or abort leaves unadopted work retryable.
57+
The monitor tracks delivery independently from claim settlement because
58+
adoption can tombstone a row before the channel's delivery promise returns.
5759

5860
Optional settings include custom append delays, a `drain` option block for
5961
advanced drain ordering/concurrency/retry policy, an external `abortSignal`, a

‎extensions/discord/src/monitor/ingress.test.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,31 @@ describe("Discord durable ingress", () => {
116116
});
117117
});
118118

119+
it("rejects unstable message identity before durable allocation", async () => {
120+
await withQueue(async (queue) => {
121+
const dispatch = vi.fn();
122+
const monitor = createDiscordIngressMonitor({
123+
accountId: "default",
124+
client: {} as never,
125+
runtime: runtime(),
126+
queue,
127+
dispatch,
128+
});
129+
monitor.start();
130+
try {
131+
const missingMessageId = { ...createRawMessage("missing"), id: undefined };
132+
const missingChannelId = { ...createRawMessage("missing"), channel_id: undefined };
133+
134+
await expect(monitor.accept(missingMessageId as never)).rejects.toThrow("snowflake");
135+
await expect(monitor.accept(missingChannelId as never)).rejects.toThrow("channel_id");
136+
expect(await queue.listPending({ limit: "all" })).toEqual([]);
137+
expect(dispatch).not.toHaveBeenCalled();
138+
} finally {
139+
await monitor.stop();
140+
}
141+
});
142+
});
143+
119144
it("recovers a claimed row with a fresh drain and dispatches it exactly once", async () => {
120145
await withQueue(async (queue) => {
121146
const monitors: DiscordIngressMonitor[] = [];

‎extensions/discord/src/monitor/ingress.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { GatewayDispatchEvents, type APIMessage } from "discord-api-types/v10";
33
import {
44
createChannelIngressError,
55
createChannelIngressMonitor,
6+
DEFAULT_INGRESS_RETRY_MAX_ATTEMPTS,
67
type ChannelIngressQueue,
78
type ChannelIngressMonitorDeliveryResult,
89
type ChannelIngressMonitorLifecycle,
@@ -147,6 +148,10 @@ export function createDiscordIngressMonitor(params: {
147148
},
148149
appendRetryDelaysMs: [0],
149150
drain: {
151+
retryPolicy: {
152+
maxAttempts: DEFAULT_INGRESS_RETRY_MAX_ATTEMPTS,
153+
deadLetterMinAgeMs: 0,
154+
},
150155
resolveNonRetryableFailure: (error) => {
151156
if (error instanceof DiscordIngressPayloadError) {
152157
return { reason: "invalid-event", message: error.message };

‎extensions/discord/src/monitor/message-dispatcher.ts‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ export function createDiscordMessageDispatcher(
139139
}
140140
const abortSignal = last.abortSignal;
141141
if (abortSignal?.aborted) {
142-
await admissionLifecycle.onAbandoned();
142+
await ingress.lifecycle?.onCancelled?.();
143143
return;
144144
}
145145
try {
@@ -157,7 +157,7 @@ export function createDiscordMessageDispatcher(
157157
turnAdoptionLifecycle: admissionLifecycle,
158158
});
159159
if (abortSignal?.aborted) {
160-
await ingress.abandon(abortSignal.reason);
160+
await ingress.lifecycle?.onCancelled?.();
161161
return;
162162
}
163163
if (!ctx) {
@@ -211,7 +211,7 @@ export function createDiscordMessageDispatcher(
211211
turnAdoptionLifecycle: admissionLifecycle,
212212
});
213213
if (abortSignal?.aborted) {
214-
await ingress.abandon(abortSignal.reason);
214+
await ingress.lifecycle?.onCancelled?.();
215215
return;
216216
}
217217
if (!ctx) {
@@ -232,7 +232,10 @@ export function createDiscordMessageDispatcher(
232232
}
233233
messageRunQueue.enqueue(buildDiscordInboundJob(ctx, { ingressSettlement: ingress }));
234234
} catch (error) {
235-
await admissionLifecycle.onAbandoned();
235+
if (abortSignal?.aborted) {
236+
await ingress.lifecycle?.onCancelled?.();
237+
return;
238+
}
236239
throw error;
237240
}
238241
},
@@ -244,7 +247,7 @@ export function createDiscordMessageDispatcher(
244247
onCancel: (entries) => {
245248
for (const entry of entries) {
246249
pendingDebounceEntries.delete(entry);
247-
const settlement = Promise.resolve(entry.turnAdoptionLifecycle?.onAbandoned())
250+
const settlement = Promise.resolve(entry.turnAdoptionLifecycle?.onCancelled?.())
248251
.catch((error: unknown) => {
249252
params.runtime.error(
250253
danger(`discord ingress cancellation settlement failed: ${String(error)}`),
@@ -271,6 +274,10 @@ export function createDiscordMessageDispatcher(
271274
const reason = dispatcherShutdown.signal.aborted
272275
? (dispatcherShutdown.signal.reason ?? new Error("discord dispatcher shut down"))
273276
: (options?.abortSignal?.reason ?? new Error("discord dispatch aborted"));
277+
if (options?.turnAdoptionLifecycle?.onCancelled) {
278+
await options.turnAdoptionLifecycle.onCancelled();
279+
return { kind: "deferred" };
280+
}
274281
return { kind: "failed-retryable", error: reason };
275282
}
276283
// Filter bot-own messages before they enter the debounce queue.

0 commit comments

Comments
 (0)