#7141: fix(telegram): unify network error detection to prevent poll crashes
channel: telegram
Cluster:
Network Error Handling Improvements
## Summary
- Unify network error detection by expanding `RECOVERABLE_MESSAGE_SNIPPETS` and removing duplicate `isNetworkRelatedError` function
- Prevents Telegram monitor crashes when fetch errors are wrapped in cause chains
- Added test coverage for the new message snippets
## Problem
The Telegram monitor had two separate network error detection mechanisms:
1. `isRecoverableTelegramNetworkError` (in `network-errors.ts`) - walks error cause chains
2. `isNetworkRelatedError` (in `monitor.ts`) - only checks top-level message
When fetch errors were wrapped (e.g., `{ cause: TypeError }`), `isNetworkRelatedError` missed them because it didn't walk the cause chain, causing the monitor to crash.
## Solution
1. **Expand `RECOVERABLE_MESSAGE_SNIPPETS`** to include snippets from the old `isNetworkRelatedError`: "network", "timeout", "socket", "econnreset", "econnrefused"
2. **Remove `isNetworkRelatedError`** from `monitor.ts` - it's now redundant
3. **Rely on `isRecoverableTelegramNetworkError`** which already handles cause chain traversal via `collectErrorCandidates()`
## Test plan
- [x] Unit tests pass (`npm test -- src/telegram/network-errors.test.ts`)
- [x] Added tests for the new message snippets
- [x] Added tests verifying send context skips message-only matching
- [x] Build passes (`npm run build`)
- [x] Lint passes (`npm run lint`)
Fixes #6077
🤖 Generated with [Claude Code](https://claude.com/claude-code)
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
This PR removes the Telegram monitor’s duplicate `isNetworkRelatedError` check and relies solely on `isRecoverableTelegramNetworkError`, which already traverses error cause chains. To preserve prior behavior, it expands `RECOVERABLE_MESSAGE_SNIPPETS` and adds unit tests covering the new snippets and the send-context message-matching bypass.
The change centralizes network error classification in `src/telegram/network-errors.ts`, simplifying `src/telegram/monitor.ts` retry logic and aiming to prevent poll crashes when fetch/undici errors are wrapped.
<h3>Confidence Score: 4/5</h3>
- This PR is likely safe to merge, with the main risk being broader-than-intended retry classification from generic message snippets.
- Changes are localized and covered by tests, and the core behavior (cause-chain traversal) is already implemented. The primary concern is behavioral: adding very generic message substrings can cause false positives in polling/webhook contexts, potentially masking real failures behind retries.
- src/telegram/network-errors.ts (message snippet broadness), src/telegram/monitor.ts (retry behavior implications)
<!-- greptile_other_comments_section -->
<!-- /greptile_comment -->
Most Similar PRs
#12870: fix: recover from telegram fetch errors (issue #12835)
by ambicuity · 2026-02-09
85.8%
#6463: fix(telegram): improve timeout handling and prevent channel exits
by ai-fanatic · 2026-02-01
84.9%
#8166: fix(telegram): lifecycle fixes for duplicate messages and auto-reco...
by cheenu1092-oss · 2026-02-03
83.2%
#7247: fix(telegram): abort stale getUpdates connections after long-poll t...
by JanderV · 2026-02-02
82.4%
#6447: fix(telegram): auto-restart polling when grammY runner exits silently
by AugmentAdvertise · 2026-02-01
81.4%
#3186: fix(telegram): sanitize update offset + lock polling
by daxiong888 · 2026-01-28
79.8%
#19375: telegram: align tool-error summaries
by NorthyIE · 2026-02-17
79.7%
#7558: fix: Handle Grammy/Telegram network errors to prevent gateway crashes
by kaigritun · 2026-02-03
79.7%
#9232: Fix: Add automatic retry for network errors in message runs
by vishaltandale00 · 2026-02-05
79.0%
#23727: Fix Telegram channel resolution drift across announce + message sen...
by SmithLabsLLC · 2026-02-22
78.7%