-
Notifications
You must be signed in to change notification settings - Fork 88
M3: Routing bootstrap and rejection visibility #6212
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,6 +26,21 @@ export function buildTelegramTransportMetadata(): { hints: string[]; uxBrief: st | |
| }; | ||
| } | ||
|
|
||
| // Rate limiter for routing rejection notices — at most one reply per chat | ||
| // within the cooldown window to avoid spamming the user. | ||
| const REJECTION_NOTICE_COOLDOWN_MS = 5 * 60 * 1000; // 5 minutes | ||
| const rejectionNoticeTimestamps = new Map<string, number>(); | ||
|
|
||
| function shouldSendRejectionNotice(chatId: string): boolean { | ||
| const now = Date.now(); | ||
| const lastSent = rejectionNoticeTimestamps.get(chatId); | ||
| if (lastSent !== undefined && now - lastSent < REJECTION_NOTICE_COOLDOWN_MS) { | ||
| return false; | ||
| } | ||
| rejectionNoticeTimestamps.set(chatId, now); | ||
| return true; | ||
|
Comment on lines
+32
to
+41
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Unbounded memory growth in The Root Cause and ImpactUnlike the const rejectionNoticeTimestamps = new Map<string, number>();
function shouldSendRejectionNotice(chatId: string): boolean {
const now = Date.now();
const lastSent = rejectionNoticeTimestamps.get(chatId);
if (lastSent !== undefined && now - lastSent < REJECTION_NOTICE_COOLDOWN_MS) {
return false;
}
rejectionNoticeTimestamps.set(chatId, now);
return true;
}In a scenario where the gateway is misconfigured (routing rejects all messages) and the bot is added to many Telegram groups or receives messages from many unique users, each distinct Impact: Slow memory leak in long-running gateway processes. Severity depends on traffic volume — a bot in many groups with misconfigured routing could accumulate thousands of entries that are never cleaned up. Prompt for agentsWas this helpful? React with 👍 or 👎 to provide feedback. |
||
| } | ||
|
|
||
| export function createTelegramWebhookHandler(config: GatewayConfig) { | ||
| const dedupCache = new DedupCache(); | ||
|
|
||
|
|
@@ -207,7 +222,24 @@ export function createTelegramWebhookHandler(config: GatewayConfig) { | |
| replyCallbackUrl: `http://127.0.0.1:${config.port}/deliver/telegram`, | ||
| }); | ||
|
|
||
| if (!result.forwarded && !result.rejected) { | ||
| if (result.rejected) { | ||
| log.warn( | ||
| { chatId, reason: result.rejectionReason }, | ||
| "Routing rejected inbound Telegram message", | ||
| ); | ||
| if (shouldSendRejectionNotice(chatId)) { | ||
| sendTelegramReply( | ||
| config, | ||
| chatId, | ||
| "\u26a0\ufe0f This message could not be routed to an assistant. Please check your gateway routing configuration.", | ||
| ).catch((err) => { | ||
| log.error({ err, chatId }, "Failed to send routing rejection notice"); | ||
| }); | ||
| } | ||
| return respond({ ok: true }); | ||
| } | ||
|
|
||
| if (!result.forwarded) { | ||
| log.error({ updateId: payload.update_id }, "Failed to forward inbound event"); | ||
| if (updateId !== undefined) dedupCache.unreserve(updateId); | ||
| return Response.json({ error: "Internal error" }, { status: 500 }); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rejectionNoticeTimestampsis updated for every new chat ID but never evicted, so a long-lived gateway handling many distinct Telegram chats will retain stale IDs forever and grow memory usage over time. This is especially likely for public bots/groups where unique chat IDs can accumulate continuously; adding TTL-based cleanup or a max-size eviction policy would prevent unbounded growth.Useful? React with 👍 / 👎.