fix: require_mention falls through to default agent instead of dropping message#376
Conversation
…ng message Two bugs made require_mention completely non-functional: 1. When require_mention caused a binding to not match, resolve_agent_for_message fell through to the default agent, which processed the message anyway. Split matches() into matches_route() + passes_require_mention() and return None from resolve_agent_for_message when a binding matched on routing but was blocked by require_mention. 2. The require_mention check was hardcoded to Discord only. Made it platform-agnostic by checking each adapter's *_mentions_or_replies_to_bot metadata key. Added the missing telegram_mentions_or_replies_to_bot key to the Telegram adapter.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (2)
WalkthroughBinding matching split into routing-only checks ( Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes 🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
|
||
| // DMs are inherently directed at the bot — always pass. | ||
| let is_dm = | ||
| message.source == "discord" && !message.metadata.contains_key("discord_guild_id"); |
There was a problem hiding this comment.
contains_key("discord_guild_id") treats null as present, so a DM could get misclassified and blocked when require_mention is on. Safer to mirror the old as_u64().is_some() behavior.
| message.source == "discord" && !message.metadata.contains_key("discord_guild_id"); | |
| let is_dm = message.source == "discord" | |
| && message | |
| .metadata | |
| .get("discord_guild_id") | |
| .and_then(|v| v.as_u64()) | |
| .is_none(); |
| // Matches the pattern used by Discord/Slack/Twitch adapters. | ||
| let mut mentions_or_replies_to_bot = false; | ||
|
|
||
| // Check text-based @mention (Telegram sends mentions as entities) |
There was a problem hiding this comment.
Small nit: this comment says mentions come via entities, but the implementation is a case-insensitive substring search on extracted text/caption. Either parse entities, or tweak the comment to match what’s happening.
| // Check text-based @mention (Telegram sends mentions as entities) | |
| // Check text-based @mention in message text/caption. |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@src/config/types.rs`:
- Around line 1541-1545: The DM-bypass only checks Discord; update the DM
detection to also recognize Telegram private chats so require_mention bindings
won't drop them: extend the is_dm condition (the variable named is_dm in this
code) to return true when message.source == "telegram" and message.metadata
contains telegram_chat_type == "private" (and/or when
telegram_mentions_or_replies_to_bot metadata is present/false for ordinary DM
text), then keep the existing early return; locate and modify the is_dm logic
that currently inspects message.source and message.metadata to include these
Telegram metadata keys.
In `@src/messaging/telegram.rs`:
- Around line 865-873: The code uses text_lower.contains(&mention) which allows
prefix matches like "@spacebot_extra"; instead, update the mention detection to
use Telegram entities or a username-boundary check: in the message handling
where you use bot_username, extract_text, and set mentions_or_replies_to_bot,
iterate message.entities (or parse MessageEntity entries) for
Mention/TextMention and compare the entity text (case-insensitive) to the exact
bot username, or if entities are not available, scan the text for occurrences of
"@{bot_username}" and ensure the character after the matched username is absent
or not in [A-Za-z0-9_] (and the char before is start or non-word) so you only
match whole Telegram usernames.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 294ac00c-b626-4e6a-8eee-b3044b3728bd
📒 Files selected for processing (3)
src/config/types.rssrc/main.rssrc/messaging/telegram.rs
- Fix discord_guild_id DM check: use .as_u64().is_none() instead of contains_key to handle null values correctly - Add Telegram private chat DM bypass so require_mention doesn't drop direct messages on Telegram - Fix Telegram mention prefix matching: add word-boundary check so @spacebot doesn't match @spacebot_extra
- Fix discord_guild_id DM check: use .as_u64().is_none() instead of contains_key to handle null values correctly - Add Telegram private chat DM bypass so require_mention doesn't drop direct messages on Telegram - Fix Telegram mention prefix matching: add word-boundary check so @spacebot doesn't match @spacebot_extra
…ention-fallthrough fix: require_mention falls through to default agent instead of dropping message
Summary
require_mentionhas never worked — when a binding rejected a message due to missing mention,resolve_agent_for_messagefell through to the default agent and processed it anywayChanges
src/config/types.rs— Splitmatches()intomatches_route()(routing criteria) andpasses_require_mention()(mention filter). Madepasses_require_mentionplatform-agnostic using each adapter's*_mentions_or_replies_to_botmetadata key. Changedresolve_agent_for_messageto returnOption<AgentId>— returnsNonewhen a binding matched on routing but was blocked byrequire_mention, so the message is dropped instead of falling through.src/messaging/telegram.rs— Addedtelegram_mentions_or_replies_to_botmetadata key (Telegram was the only adapter missing a combined mention/reply flag). Checks both@botusernametext mentions and reply-to-bot.src/main.rs— Handle theNonereturn withcontinueto drop suppressed messages.Repro
With this config, the bot responds to every message in the bound channels despite
require_mention = true:The binding rejects the message → falls through to
default_agent_id("main") → main processes it with no mention requirement.Note
Fix Summary: This PR fixes the
require_mentionfeature which was broken in two ways: mentioning only checks Discord and only as a routing filter, causing unmentioned messages to fall through to the default agent instead of being dropped. Now the check is platform-agnostic and properly suppresses messages before they reach fallback handling. Telegram adapter gains mention detection for the first time.Written by Tembo for commit badee64. This will update automatically on new commits.