"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(slack): fix missing DM pairing code
Lanre Shittu47ecd24ef59e
diff --git a/agent/src/channels/slack.py b/agent/src/channels/slack.pyindex b9986757f..1231b21da 100644--- a/agent/src/channels/slack.py+++ b/agent/src/channels/slack.py@@ -18,7 +18,12 @@ from src.channels.bus.queue import MessageBusfrom src.channels.base import BaseChannelfrom src.channels.utils import get_media_dirfrom pydantic import BaseModel-from src.channels.pairing import is_approved+from src.channels.pairing import (+ PAIRING_CODE_META_KEY,+ format_pairing_reply,+ generate_code,+ is_approved,+)from src.channels.utils import safe_filename, split_message@@ -368,11 +373,18 @@ class SlackChannel(BaseChannel):if not self._is_allowed(sender_id, chat_id, channel_type):if channel_type == "im" and self.config.dm.enabled:- await self._handle_message(- sender_id=sender_id,- chat_id=chat_id,- content="",- is_dm=True,+ # is_allowed() is overridden below to always return True+ # (Slack does its own channel-aware check via _is_allowed+ # above), so BaseChannel._handle_message's own pairing-code+ # branch never fires. Issue the code directly here instead.+ code = generate_code(self.name, str(sender_id))+ await self.send(+ OutboundMessage(+ channel=self.name,+ chat_id=str(chat_id),+ content=format_pairing_reply(code),+ metadata={PAIRING_CODE_META_KEY: code},+ ))returndiff --git a/agent/tests/test_slack_dm_pairing.py b/agent/tests/test_slack_dm_pairing.pynew file mode 100644index 000000000..aa3cd5f65--- /dev/null+++ b/agent/tests/test_slack_dm_pairing.py@@ -0,0 +1,90 @@+"""An unauthorized DM sender under dm.policy="allowlist" must receive a+pairing code, not have their message silently forwarded (or dropped with+no feedback at all)."""++from __future__ import annotations++import asyncio+from unittest.mock import AsyncMock++from slack_sdk.socket_mode.request import SocketModeRequest++from src.channels.bus.events import OutboundMessage+from src.channels.pairing import PAIRING_CODE_META_KEY+from src.channels.slack import SlackChannel, SlackConfig+++def _channel() -> SlackChannel:+ config = SlackConfig(enabled=True, bot_token="xoxb-test", app_token="xapp-test")+ config.dm.policy = "allowlist"+ config.dm.allow_from = ["U_OWNER"]+ channel = object.__new__(SlackChannel)+ channel.config = config+ channel._bot_user_id = None+ channel.logger = __import__("logging").getLogger("test.slack")+ return channel+++def test_unauthorized_dm_sender_gets_a_pairing_code() -> None:+ async def scenario() -> None:+ channel = _channel()+ channel.send = AsyncMock()++ client = AsyncMock()+ req = SocketModeRequest(+ type="events_api",+ envelope_id="env-1",+ payload={+ "event": {+ "type": "message",+ "user": "U_RANDOM",+ "channel": "D123",+ "channel_type": "im",+ "text": "hello",+ }+ },+ )++ await channel._on_socket_request(client, req)++ channel.send.assert_awaited_once()+ sent: OutboundMessage = channel.send.await_args.args[0]+ assert sent.chat_id == "D123"+ assert PAIRING_CODE_META_KEY in sent.metadata++ asyncio.run(scenario())+++def test_authorized_dm_sender_is_unaffected() -> None:+ async def scenario() -> None:+ channel = _channel()+ channel.send = AsyncMock()+ channel._handle_message = AsyncMock()+ channel._should_respond_in_channel = lambda *a, **k: True+ channel._strip_bot_mention = lambda text: text+ channel._with_thread_context = AsyncMock(return_value="hello")+ channel._web_client = None+ channel.config.reply_in_thread = False++ client = AsyncMock()+ req = SocketModeRequest(+ type="events_api",+ envelope_id="env-2",+ payload={+ "event": {+ "type": "message",+ "user": "U_OWNER",+ "channel": "D123",+ "channel_type": "im",+ "text": "hello",+ "ts": "1.1",+ }+ },+ )++ await channel._on_socket_request(client, req)++ channel.send.assert_not_awaited()+ channel._handle_message.assert_awaited_once()++ asyncio.run(scenario())