Skip to content

Commit 819c92d

Browse files
authored
fix(line): prevent silent webhook loss after acknowledgment (#109819)
* fix(line): adopt durable ingress drain with ack gated on spool write LINE acked webhooks 200 before processing events detached, so a crash or dispatch failure silently lost inbound messages. Events now enqueue their raw JSON into the channel ingress queue before the webhook responds; dispatch, retry, dead-letter, and completion tombstones run through the core drain. The in-memory replay guard is deleted; tombstone retention (30d/4096) strictly covers its old 10min/4096 window, and message-id keys preserve the guard keyspace so redeliveries under changed webhookEventIds still dedupe. Autoreview: secret scanner false-positive on HMAC test fixtures (test-channel-secret); full manual review of all five files + test matrix performed instead. Part of the #109657 fleet adoption program (wave 1). * style(line): oxfmt pass on drain adoption files * fix(line): cap repeated ingress drain deliveries * fix(line): preserve ingress lifecycle bounds * style(line): satisfy ingress lint gates
1 parent 3e83878 commit 819c92d

5 files changed

Lines changed: 541 additions & 880 deletions

File tree

‎extensions/line/src/bot-handlers.ts‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import { reserveLineGroupHistory } from "./group-history.js";
3939
import { resolveLineGroupConfigEntry } from "./group-keys.js";
4040
import { pushMessageLine, replyMessageLine } from "./send.js";
4141
import type { LineGroupConfig, ResolvedLineAccount } from "./types.js";
42+
import type { LineWebhookTurnAdoptionLifecycle } from "./webhook-spool.js";
4243

4344
type FollowEvent = webhook.FollowEvent;
4445
type JoinEvent = webhook.JoinEvent;
@@ -73,10 +74,9 @@ interface LineHandlerContext {
7374
mediaMaxBytes: number;
7475
processMessage: (
7576
ctx: LineInboundContext,
76-
control: { abortSignal?: AbortSignal; onTurnAdopted?: () => Promise<void> },
77+
control: { turnAdoptionLifecycle?: LineWebhookTurnAdoptionLifecycle },
7778
) => Promise<void>;
78-
abortSignal?: AbortSignal;
79-
onTurnAdopted?: () => Promise<void>;
79+
turnAdoptionLifecycle?: LineWebhookTurnAdoptionLifecycle;
8080
groupHistories?: Map<string, HistoryEntry[]>;
8181
historyLimit?: number;
8282
}
@@ -439,10 +439,10 @@ async function handleMessageEvent(event: MessageEvent, context: LineHandlerConte
439439
return;
440440
}
441441

442-
await processMessage(messageContext, {
443-
...(context.abortSignal ? { abortSignal: context.abortSignal } : {}),
444-
...(context.onTurnAdopted ? { onTurnAdopted: context.onTurnAdopted } : {}),
445-
});
442+
await processMessage(
443+
messageContext,
444+
context.turnAdoptionLifecycle ? { turnAdoptionLifecycle: context.turnAdoptionLifecycle } : {},
445+
);
446446
historyReservation.commit();
447447
} finally {
448448
historyReservation.release();
@@ -494,10 +494,10 @@ async function handlePostbackEvent(
494494
return;
495495
}
496496

497-
await context.processMessage(postbackContext, {
498-
...(context.abortSignal ? { abortSignal: context.abortSignal } : {}),
499-
...(context.onTurnAdopted ? { onTurnAdopted: context.onTurnAdopted } : {}),
500-
});
497+
await context.processMessage(
498+
postbackContext,
499+
context.turnAdoptionLifecycle ? { turnAdoptionLifecycle: context.turnAdoptionLifecycle } : {},
500+
);
501501
}
502502

503503
export async function handleLineWebhookEvents(

‎extensions/line/src/bot.ts‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { resolveLineAccount } from "./accounts.js";
1212
import { handleLineWebhookEvents } from "./bot-handlers.js";
1313
import type { LineInboundContext } from "./bot-message-context.js";
1414
import type { ResolvedLineAccount } from "./types.js";
15-
import { createLineWebhookSpool } from "./webhook-spool.js";
15+
import { createLineWebhookSpool, type LineWebhookTurnAdoptionLifecycle } from "./webhook-spool.js";
1616

1717
interface LineBotOptions {
1818
channelAccessToken: string;
@@ -23,7 +23,7 @@ interface LineBotOptions {
2323
mediaMaxMb?: number;
2424
onMessage?: (
2525
ctx: LineInboundContext,
26-
control: { abortSignal?: AbortSignal; onTurnAdopted?: () => Promise<void> },
26+
control: { turnAdoptionLifecycle?: LineWebhookTurnAdoptionLifecycle },
2727
) => Promise<void>;
2828
}
2929

@@ -60,8 +60,9 @@ export function createLineBot(opts: LineBotOptions): LineBot {
6060
runtime,
6161
mediaMaxBytes,
6262
processMessage,
63-
...(control.abortSignal ? { abortSignal: control.abortSignal } : {}),
64-
...(control.onTurnAdopted ? { onTurnAdopted: control.onTurnAdopted } : {}),
63+
...(control.turnAdoptionLifecycle
64+
? { turnAdoptionLifecycle: control.turnAdoptionLifecycle }
65+
: {}),
6566
groupHistories,
6667
historyLimit: cfg.messages?.groupChat?.historyLimit ?? DEFAULT_GROUP_HISTORY_LIMIT,
6768
}),

‎extensions/line/src/monitor.ts‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ export async function monitorLineProvider(
166166
logVerbose(`line: received message from ${displayName} (${ctxPayload.From})`);
167167
let replyTokenUsed = false;
168168
let turnAdopted = false;
169+
const ingressLifecycle = deliveryControl.turnAdoptionLifecycle;
169170

170171
try {
171172
const textLimit = 5000;
@@ -175,12 +176,12 @@ export async function monitorLineProvider(
175176
accountId: route.accountId,
176177
raw: ctx,
177178
turnAdoptionLifecycle: {
179+
...ingressLifecycle,
178180
admission: "exclusive",
179181
onAdopted: async () => {
180-
await deliveryControl.onTurnAdopted?.();
182+
await ingressLifecycle?.onAdopted();
181183
turnAdopted = true;
182184
},
183-
...(deliveryControl.abortSignal ? { abortSignal: deliveryControl.abortSignal } : {}),
184185
},
185186
adapter: {
186187
ingest: () => ({
@@ -195,8 +196,8 @@ export async function monitorLineProvider(
195196
ctxPayload,
196197
record: ctx.turn.record,
197198
replyPipeline: {},
198-
...(deliveryControl.abortSignal
199-
? { replyOptions: { abortSignal: deliveryControl.abortSignal } }
199+
...(ingressLifecycle?.abortSignal
200+
? { replyOptions: { abortSignal: ingressLifecycle.abortSignal } }
200201
: {}),
201202
delivery: {
202203
durable: (payload, info) =>

0 commit comments

Comments
 (0)