Skip to content

The agent that grows with you

fix(slack): carry channel prompt and source names on slash-command turns

John Paul Soliva1424ffa4293d
diff --git a/plugins/platforms/slack/adapter.py b/plugins/platforms/slack/adapter.py
index 70323726358..3cac42eadd4 100644
--- a/plugins/platforms/slack/adapter.py
+++ b/plugins/platforms/slack/adapter.py
@@ -6073,13 +6073,20 @@ class SlackAdapter(BasePlatformAdapter):
"[Slack] Ignoring slash command from DM because Slack DMs are disabled: channel=%s user=%s",
channel_id, user_id)
return
+ # A slash turn is a human turn: it re-pins the channel prompt and session context, so it
+ # must carry the same names and prompt as a message here or the next message flips them.
source = self.build_source(
- chat_id=channel_id, chat_type="dm" if is_dm else "group", user_id=user_id,
+ chat_id=channel_id,
+ chat_name=await self._resolve_channel_name(channel_id, team_id=team_id),
+ chat_type="dm" if is_dm else "group",
+ user_id=user_id,
+ user_name=await self._resolve_user_name(user_id, chat_id=channel_id, team_id=team_id),
thread_id=thread_id, scope_id=team_id or None)
event = MessageEvent(
text=text,
message_type=(MessageType.COMMAND if text.startswith("/") else MessageType.TEXT),
- source=source, raw_message=command)
+ source=source, raw_message=command,
+ channel_prompt=self._channel_prompt_with_identity(channel_id, team_id))
# Stash response_url so the first reply for this channel+user goes ephemeral. COMMAND
# events only: free-form "/hermes <question>" replies must stay public.
response_url = command.get("response_url", "")
diff --git a/tests/plugins/platforms/slack/test_slash_command_channel_prompt.py b/tests/plugins/platforms/slack/test_slash_command_channel_prompt.py
new file mode 100644
index 00000000000..9fc2c31a143
--- /dev/null
+++ b/tests/plugins/platforms/slack/test_slash_command_channel_prompt.py
@@ -0,0 +1,53 @@
+"""Slack slash-command turns carry the same channel prompt and source names as messages.
+
+A ``/hermes <question>`` turn is a human turn, so the gateway re-pins ``channel_pin`` and the
+session-context key from it. Built without ``channel_prompt``, ``chat_name`` and ``user_name``,
+it ran without the configured channel prompt and flipped both pins, and the next ordinary
+message flipped them back: two agent rebuilds and two prompt-cache misses per slash turn.
+"""
+
+from unittest.mock import AsyncMock, MagicMock
+
+import pytest
+
+from gateway.config import PlatformConfig
+from plugins.platforms.slack.adapter import SlackAdapter
+
+
+def _adapter(channel_id: str) -> SlackAdapter:
+ a = SlackAdapter(PlatformConfig(
+ enabled=True, token="xoxb-fake", extra={"channel_prompts": {channel_id: "Answer in haiku."}}))
+ a._app = MagicMock()
+ a._app.client = AsyncMock()
+ a._app.client.users_info = AsyncMock(
+ return_value={"user": {"profile": {"display_name": "Alice"}, "real_name": "Alice"}})
+ a._app.client.conversations_info = AsyncMock(
+ return_value={"ok": True, "channel": (
+ {"id": channel_id, "is_im": True, "user": "U_ALICE"} if channel_id.startswith("D")
+ else {"id": channel_id, "name": "ops"})})
+ a._bot_user_id = "U_BOT"
+ a._bot_display_name = "HermesBot"
+ a._running = True
+ a.handle_message = AsyncMock()
+ return a
+
+
[email protected]("channel_id, channel_type, chat_name", [
+ ("C_OPS", "channel", "ops"), ("D_ALICE", "im", "Alice")])
+async def test_slash_turn_matches_message_turn_prompt_and_names(channel_id, channel_type, chat_name):
+ adapter = _adapter(channel_id)
+ await adapter._handle_slash_command(
+ {"command": "/hermes", "text": "what broke?", "user_id": "U_ALICE",
+ "channel_id": channel_id, "team_id": "T1"})
+ await adapter._handle_slack_message(
+ {"text": "<@U_BOT> what broke?", "user": "U_ALICE", "channel": channel_id,
+ "channel_type": channel_type, "ts": "1700.000100"},
+ {"team_id": "T1"})
+ assert adapter.handle_message.await_count == 2
+ slash, message = (c.args[0] for c in adapter.handle_message.await_args_list)
+ assert message.channel_prompt and "Answer in haiku." in message.channel_prompt
+ assert slash.channel_prompt == message.channel_prompt
+ assert (message.source.chat_name, message.source.user_name) == (chat_name, "Alice")
+ assert (slash.source.chat_name, slash.source.user_name) == (
+ message.source.chat_name, message.source.user_name)