Skip to content

fix(telegram): ack the webhook even when the inbound relay throws - #1285

Closed
lilyshen0722 wants to merge 1 commit into
feat/telegram-live-bridgefrom
fix/telegram-relay-idempotent-webhook
Closed

lilyshen0722 wants to merge 1 commit into
feat/telegram-live-bridgefrom
fix/telegram-relay-idempotent-webhook

Conversation

@lilyshen0722

Copy link
Copy Markdown
Contributor

Stacked on feat/telegram-live-bridge (#1282) so it can land with the demo rather than after it. Merges into that branch, not main.

The defect

routes/webhooks/telegram.ts awaits relayTelegramMessageToPod with no local catch, so a throw lands in the route's outer handler and returns 500. Telegram redelivers any update the webhook does not 2xx.

The relay is not idempotent, and the ordering is what makes that expensive:

  • telegramBridgeService.ts:239 — PGMessage.create(...) writes the pod row
  • telegramBridgeService.ts:248 — deliverMessageToAgents(...) wakes agents

Both are unguarded, so a throw in delivery happens after the row exists. Nothing dedupes on telegramMessage.message_id. The retry therefore writes the message a second time and wakes every mentioned agent a second time — and it fires precisely when the pod is busy enough for delivery to be the thing that failed.

The fix

Wrap the call, log, and sendStatus(200) regardless. A dropped relay is recoverable — the sender sees nothing arrive and retypes. A doubled one is not: the second wake is already in flight before anyone notices.

This does not make the relay idempotent, and is not meant to. Dedupe on message_id is the durable fix and wants a store; this removes the mechanism that turns any transient delivery failure into a duplicate.

Proof

Two route tests. Both mock the bridge, so they exercise the route's contract rather than the relay's internals:

  • the live-relay branch calls the bridge and does not fall through to the buffer provider
  • a throwing relay still acks 200

Mutation control — revert only the route change and re-run:

Tests: 1 failed, 4 passed, 5 total
  ✕ still acks 200 when the relay throws, so Telegram does not redeliver

Exactly one red, and it is the new assertion; the other four stay green, so the test discriminates on the fix rather than on the mocking.

Context: #1282 (comment)

Telegram redelivers any update the webhook does not 2xx, and
relayTelegramMessageToPod is not idempotent: it writes the pod row
(PGMessage.create) before it wakes agents (deliverMessageToAgents), and
nothing dedupes on message_id. A throw in delivery therefore reached the
route's outer catch, returned 500, and the retry duplicated both the pod
message and every agent wake it triggered.

Wrap the relay call and sendStatus(200) regardless. A dropped relay is
recoverable; a doubled one is not.

Two route tests: the live-relay branch bypasses the buffer provider, and
a throwing relay still acks 200. Reverting the route change turns the
second red and leaves the other four green.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant