Repository navigation
fix(telegram): report lastEventAt to channel health monitor - #37850
jakozloski wants to merge 5 commits into
Conversation
Greptile SummaryThis PR fixes the Telegram channel health monitor's "stale-socket" false-positive by threading a However, there are two issues:
Confidence Score: 3/5
Last reviewed commit: 4f63343 |
| // Track lastInboundAt only for message-bearing updates (not reactions/callbacks). | ||
| if (opts.setStatus) { | ||
| const setStatus = opts.setStatus; | ||
| bot.on(["message", "channel_post"], async (_ctx, next) => { | ||
| setStatus({ lastInboundAt: Date.now() }); | ||
| await next(); | ||
| }); | ||
| } |
There was a problem hiding this comment.
The bot.on(["message", "channel_post"], ...) middleware for lastInboundAt is registered after registerTelegramHandlers(), which at lines 1468 and 1493 of bot-handlers.ts registers terminal bot.on("message", ...) and bot.on("channel_post", ...) handlers that never call next().
In grammY, middleware registered after a terminal filter handler is never reached for updates that were consumed by that handler. Because registerTelegramHandlers registers handlers with signatures like async (ctx) => { ... } (no next parameter), the chain stops there. The lastInboundAt middleware at line 424 will therefore never fire for real messages or channel posts.
Move this block to immediately after the lastEventAt middleware (around line 135), before any message-processing handlers are registered:
| // Track lastInboundAt only for message-bearing updates (not reactions/callbacks). | |
| if (opts.setStatus) { | |
| const setStatus = opts.setStatus; | |
| bot.on(["message", "channel_post"], async (_ctx, next) => { | |
| setStatus({ lastInboundAt: Date.now() }); | |
| await next(); | |
| }); | |
| } | |
| // Track lastInboundAt only for message-bearing updates (not reactions/callbacks). | |
| if (opts.setStatus) { | |
| const setStatus = opts.setStatus; | |
| bot.on(["message", "channel_post"], async (_ctx, next) => { | |
| setStatus({ lastInboundAt: Date.now() }); | |
| await next(); | |
| }); | |
| } |
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/telegram/bot.ts
Line: 421-428
Comment:
The `bot.on(["message", "channel_post"], ...)` middleware for `lastInboundAt` is registered **after** `registerTelegramHandlers()`, which at lines 1468 and 1493 of `bot-handlers.ts` registers terminal `bot.on("message", ...)` and `bot.on("channel_post", ...)` handlers that never call `next()`.
In grammY, middleware registered after a terminal filter handler is never reached for updates that were consumed by that handler. Because `registerTelegramHandlers` registers handlers with signatures like `async (ctx) => { ... }` (no `next` parameter), the chain stops there. The `lastInboundAt` middleware at line 424 will therefore **never fire** for real messages or channel posts.
Move this block to immediately after the `lastEventAt` middleware (around line 135), before any message-processing handlers are registered:
```suggestion
// Track lastInboundAt only for message-bearing updates (not reactions/callbacks).
if (opts.setStatus) {
const setStatus = opts.setStatus;
bot.on(["message", "channel_post"], async (_ctx, next) => {
setStatus({ lastInboundAt: Date.now() });
await next();
});
}
```
How can I resolve this? If you propose a fix, please make it concise.There was a problem hiding this comment.
Fixed in 0721511 — moved the lastInboundAt middleware block to before registerTelegramHandlers() so it runs before the terminal handlers.
| // API unreachable — don't update, let health monitor detect staleness | ||
| } | ||
| }; | ||
| webhookHeartbeat = setInterval(ping, 10 * 60_000); |
There was a problem hiding this comment.
setInterval(ping, 10 * 60_000) only fires after the first full 10-minute interval elapses. For an idle webhook channel (no inbound updates after startup), the health monitor will see a stale lastEventAt for up to 10 minutes before the first heartbeat runs.
Adding an immediate call to ping() after the interval is created would close this window and ensure the transport heartbeat activates on startup:
| webhookHeartbeat = setInterval(ping, 10 * 60_000); | |
| webhookHeartbeat = setInterval(ping, 10 * 60_000); | |
| void ping(); |
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/telegram/webhook.ts
Line: 274
Comment:
`setInterval(ping, 10 * 60_000)` only fires after the first full 10-minute interval elapses. For an idle webhook channel (no inbound updates after startup), the health monitor will see a stale `lastEventAt` for up to 10 minutes before the first heartbeat runs.
Adding an immediate call to `ping()` after the interval is created would close this window and ensure the transport heartbeat activates on startup:
```suggestion
webhookHeartbeat = setInterval(ping, 10 * 60_000);
void ping();
```
How can I resolve this? If you propose a fix, please make it concise.There was a problem hiding this comment.
Fixed in 0721511 — added void ping() immediately after setInterval creation so the first heartbeat fires at startup.
|
Both issues from the Greptile review are fixed in 0721511:
Also fixed the TypeScript error ( |
The Telegram provider never updated lastEventAt in the channel health snapshot, causing the health monitor to declare "stale-socket" and restart the connection every 30 minutes — even when the bot was healthy. Thread setStatus from the channel plugin through monitorTelegramProvider and createTelegramBot. Add: - grammY middleware to stamp lastEventAt on every inbound update - Separate lastInboundAt tracking only for message/channel_post - API transformer on getUpdates for transport-level heartbeat (proves polling is alive during idle periods without masking real failures) - Webhook mode: periodic getWebhookInfo ping with cleanup on shutdown Co-Authored-By: Claude Opus 4.6 <[email protected]>
…tup gap - Cast ctx.setStatus in channel.ts to fix TS2322 type mismatch - Move lastInboundAt middleware before registerTelegramHandlers so it actually runs (terminal handlers don't call next()) - Add immediate ping() at webhook startup to close 10-minute liveness gap Co-Authored-By: Claude Opus 4.6 <[email protected]>
setInterval already returns NodeJS.Timeout — no need for typeof guard or `as NodeJS.Timeout` cast. Co-Authored-By: Claude Opus 4.6 <[email protected]>
…fter module move The original PR targeted src/telegram/monitor.ts which was deleted on main and moved to extensions/telegram/src/monitor.ts. Thread setStatus through the new monitor and polling-session to complete the health monitoring plumbing.
bfcbcf4 to
343e773
Compare
…reaks require on Node 24)
5daea5e to
15151f8
Compare
|
This pull request has been automatically marked as stale due to inactivity. |
|
Closing as superseded by #51931. The newer PR threads a typed status sink into Telegram polling and updates the gateway health-policy tests around explicit polling mode, which is the safer shape for this liveness surface. Thanks for pushing the original direction here; it helped identify the right seam. |
Summary
lastEventAtin the channel health snapshot, causing the health monitor to declare "stale-socket" and restart the bot every 30 minutes — even when the connection was healthysetStatusfrom the channel plugin throughmonitorTelegramProvider→createTelegramBot→startTelegramWebhooklastEventAton all inbound updates andlastInboundAtonly for message/channel_postgetUpdatesAPI transformer (polling) and periodicgetWebhookInfoping (webhook) for idle-channel livenessTest plan
lastEventAtupdates, no more "stale-socket" restartspnpm test -- --grep telegram🤖 Generated with Claude Code