Skip to content

armgit / avibe

Public Imported from GitHub
0

Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.

fix(slack): dedupe connect mention events

cyh4a025b45c39d
diff --git a/modules/im/slack.py b/modules/im/slack.py
index fe9ec5308..a7466e26e 100644
--- a/modules/im/slack.py
+++ b/modules/im/slack.py
@@ -73,6 +73,7 @@ class SlackBot(BaseIMClient):
# Controller reference for update button handling (will be injected later)
self._controller = None
self._recent_event_ids: Dict[str, float] = {}
+ self._processed_mention_event_keys: Dict[str, float] = {}
self._user_info_cache: Dict[str, Dict[str, Any]] = {}
self._channel_info_cache: Dict[str, Dict[str, Any]] = {}
self._bot_user_id: Optional[str] = None
@@ -207,6 +208,23 @@ class SlackBot(BaseIMClient):
self._recent_event_ids[event_id] = now
return False
+ def _mark_mention_event_processed(self, channel_id: Optional[str], message_id: Optional[str]) -> bool:
+ """Track mention handling across Slack message/app_mention event pairs."""
+ if not channel_id or not message_id:
+ return True
+ now = time.time()
+ expiry = now - 300
+ for key in list(self._processed_mention_event_keys.keys()):
+ if self._processed_mention_event_keys[key] < expiry:
+ del self._processed_mention_event_keys[key]
+
+ key = f"{channel_id}:{message_id}"
+ if key in self._processed_mention_event_keys:
+ logger.debug("Ignoring duplicate Slack mention event for %s", key)
+ return False
+ self._processed_mention_event_keys[key] = now
+ return True
+
def get_default_parse_mode(self) -> str:
"""Get the default parse mode for Slack"""
return "markdown"
@@ -1398,6 +1416,11 @@ class SlackBot(BaseIMClient):
logger.debug("Ignoring Slack message with empty text and no files")
return
+ if handled_bot_mention_in_message_event and not self._mark_mention_event_processed(
+ channel_id, event.get("ts")
+ ):
+ return
+
# Check if we require mention in channels (not DMs)
# For threads: only respond if the bot is active in that thread
is_thread_reply = event.get("thread_ts") is not None
@@ -1510,6 +1533,8 @@ class SlackBot(BaseIMClient):
# Handle @mentions
channel_id = event.get("channel")
user_id_mention = event.get("user")
+ if not self._mark_mention_event_processed(channel_id, event.get("ts")):
+ return
# Remove the mention from the text first so we can parse commands for auth
text = self._strip_specific_mention(
diff --git a/tests/test_slack_dm_mentions.py b/tests/test_slack_dm_mentions.py
index 5317845f9..eefd8d19b 100644
--- a/tests/test_slack_dm_mentions.py
+++ b/tests/test_slack_dm_mentions.py
@@ -857,6 +857,94 @@ class SlackDmMentionTests(unittest.IsolatedAsyncioTestCase):
self.assertEqual(marked, [("U123", "C_CONNECT", "1710000000.000360")])
+ async def test_slack_connect_message_then_app_mention_is_handled_once(self):
+ slack = SlackBot(SlackConfig(bot_token="xoxb-test", require_mention=True))
+ received = []
+
+ class _WebClient:
+ async def conversations_info(self, channel):
+ return {"channel": {"id": channel, "is_ext_shared": True}}
+
+ async def _on_message(_context, text):
+ received.append(text)
+
+ slack.web_client = _WebClient()
+ slack.register_callbacks(on_message=_on_message)
+
+ message_payload = {
+ "event_id": "evt-slack-connect-message-first",
+ "team_id": "T1",
+ "authorizations": [{"user_id": "U_BOT"}],
+ "event": {
+ "type": "message",
+ "channel": "C_CONNECT",
+ "user": "U123",
+ "text": "<@U_BOT> please help",
+ "ts": "1710000000.000370",
+ },
+ }
+ app_mention_payload = {
+ "event_id": "evt-slack-connect-app-mention-second",
+ "team_id": "T1",
+ "authorizations": [{"user_id": "U_BOT"}],
+ "event": {
+ "type": "app_mention",
+ "channel": "C_CONNECT",
+ "user": "U123",
+ "text": "<@U_BOT> please help",
+ "ts": "1710000000.000370",
+ },
+ }
+
+ await slack._handle_event(message_payload)
+ await slack._handle_event(app_mention_payload)
+
+ self.assertEqual(received, ["please help"])
+
+ async def test_slack_connect_app_mention_then_message_is_handled_once(self):
+ slack = SlackBot(SlackConfig(bot_token="xoxb-test", require_mention=True))
+ received = []
+
+ class _WebClient:
+ async def conversations_info(self, channel):
+ return {"channel": {"id": channel, "is_ext_shared": True}}
+
+ async def _on_message(_context, text):
+ received.append(text)
+
+ slack.web_client = _WebClient()
+ slack.register_callbacks(on_message=_on_message)
+
+ app_mention_payload = {
+ "event_id": "evt-slack-connect-app-mention-first",
+ "team_id": "T1",
+ "authorizations": [{"user_id": "U_BOT"}],
+ "event": {
+ "type": "app_mention",
+ "channel": "C_CONNECT",
+ "user": "U123",
+ "text": "<@U_BOT> please help",
+ "ts": "1710000000.000380",
+ },
+ }
+ message_payload = {
+ "event_id": "evt-slack-connect-message-second",
+ "team_id": "T1",
+ "authorizations": [{"user_id": "U_BOT"}],
+ "event": {
+ "type": "message",
+ "channel": "C_CONNECT",
+ "user": "U123",
+ "text": "<@U_BOT> please help",
+ "ts": "1710000000.000380",
+ },
+ }
+
+ await slack._handle_event(app_mention_payload)
+ await slack._handle_event(message_payload)
+
+ self.assertEqual(received, ["please help"])
+
async def test_dm_preserves_non_bot_mentions(self):
slack = SlackBot(SlackConfig(bot_token="xoxb-test"))
received = {}