← Back to PRs

#16995: fix(telegram): record update ID before processing to prevent crash replay

by AI-Reviewer-QS open 2026-02-15 09:17 View on GitHub →
channel: telegram stale size: S
## Summary - **Bug:** `recordUpdateId(ctx)` was called after `await next()` in the logging middleware (`src/telegram/bot.ts:215-228`). If the process crashed during `next()` (while processing the message), the update offset was never persisted. On restart, Telegram would redeliver the same update, causing duplicate message processing. - **Fix:** Move `recordUpdateId(ctx)` to before `await next()` so the offset is recorded before the handler runs. `recordUpdateId` has no dependency on the result of `next()` — it only reads `ctx.update.update_id` and persists it via `onUpdateId`. - Added a test that verifies `onUpdateId` is called before `next()` completes. ## Test plan - [x] New test `bot.create-telegram-bot.records-update-id-before-processing.test.ts` asserts `onUpdateId` fires before `next()` - [x] Existing dedupe and update-offset-store tests pass unchanged <!-- greptile_comment --> <h3>Greptile Summary</h3> Moved `recordUpdateId(ctx)` before `await next()` in the Telegram logging middleware to prevent duplicate message processing after crashes. Previously, if the process crashed during message handling, the update offset wasn't persisted, causing Telegram to redeliver the same update on restart. The fix is correct because `recordUpdateId` only reads `ctx.update.update_id` and has no dependency on the result of `next()`. <h3>Confidence Score: 5/5</h3> - This PR is safe to merge - it fixes a critical duplicate message bug with a minimal, well-tested change - The fix is a simple two-line swap with clear logic: recording the update ID before processing ensures it's persisted even if the process crashes. The recordUpdateId function has no dependencies on the result of next(), making the reordering safe. The new test directly validates the execution order, and existing tests remain unchanged, confirming no regressions - No files require special attention <sub>Last reviewed commit: 6a72472</sub> <!-- greptile_other_comments_section --> <!-- /greptile_comment -->

Most Similar PRs