Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 32 additions & 7 deletions plugins/platforms/slack/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -4418,6 +4418,24 @@ def _channel_prompt_with_identity(self, channel_id: str, team_id: str) -> Option
else identity_prompt)
return channel_prompt

async def _resolve_turn_prompt_inputs(
self, channel_id: str, team_id: str, user_id: str,
) -> Tuple[str, str, Optional[str], Optional[List[str]]]:
"""Resolve prompt-sensitive inputs shared by Slack turn producers.

Keeping these inputs behind one boundary prevents ordinary messages and slash commands
from drifting apart in the session-context pin, channel prompt, or new-session skill load.
"""
user_name = await self._resolve_user_name(user_id, chat_id=channel_id, team_id=team_id)
channel_name = await self._resolve_channel_name(channel_id, team_id=team_id)
from gateway.platforms.base import resolve_channel_skills
return (
channel_name,
user_name,
self._channel_prompt_with_identity(channel_id, team_id),
resolve_channel_skills(self.config.extra, channel_id, None),
)

def _track_reacting_message(self, team_id: str, ts: str) -> None:
"""Mark ``ts`` for the reaction lifecycle, evicting oldest-ts-first past the cap."""
self._reacting_message_ids.add(self._workspace_message_marker(team_id, ts))
Expand Down Expand Up @@ -4699,8 +4717,8 @@ async def _build_message_event(
if is_command_text:
text = command_probe_text
msg_type = MessageType.COMMAND if is_command_text else self._media_message_type(media_types)
user_name = await self._resolve_user_name(user_id, chat_id=channel_id, team_id=team_id)
channel_name = await self._resolve_channel_name(channel_id, team_id=team_id)
channel_name, user_name, channel_prompt, auto_skill = (
await self._resolve_turn_prompt_inputs(channel_id, team_id, user_id))
# Best-effort: title the DM thread from the prompt for Slack's AI Agent Messages tab.
if is_dm and thread_ts and msg_type != MessageType.COMMAND:
await self._set_assistant_thread_title(
Expand All @@ -4717,7 +4735,6 @@ async def _build_message_event(
# Workflow/app posts have user=None; flag them so the SLACK_ALLOW_BOTS bypass can
# authorize them. Same predicate as the drop gate (api_human_users stay human).
is_bot=self._event_declares_bot_sender(event))
from gateway.platforms.base import resolve_channel_skills
# Remaining ``<@UID>`` are OTHER participants (own mention stripped
# above); render as ``@DisplayName`` so the agent knows who is addressed.
text = await self._humanize_user_mentions(text, chat_id=channel_id, team_id=team_id)
Expand All @@ -4731,12 +4748,12 @@ async def _build_message_event(
media_types=media_types,
media_text_inlined=media_text_inlined,
reply_to_message_id=thread_ts if thread_ts != ts else None,
channel_prompt=self._channel_prompt_with_identity(channel_id, team_id),
channel_prompt=channel_prompt,
channel_context=channel_context,
reply_expected=reply_expected,
# thread_ts is the thread root, not an explicit reply (root is in channel_context).
reply_to_text=None,
auto_skill=resolve_channel_skills(self.config.extra, channel_id, None),
auto_skill=auto_skill,
metadata={
"slack_team_id": team_id, "slack_channel_id": channel_id,
"slack_thread_ts": thread_ts})
Expand Down Expand Up @@ -6058,13 +6075,21 @@ async def _handle_slash_command(self, command: dict) -> None:
"[Slack] Ignoring slash command from DM because Slack DMs are disabled: channel=%s user=%s",
channel_id, user_id)
return
# Match ordinary-message ingress: rejected senders must not trigger name-resolution API
# calls before the gateway rejects the turn.
if self._early_reject_unauthorized(user_id, channel_id, is_dm):
Comment thread
JoaoMarcos44 marked this conversation as resolved.
Resolved
Comment thread
JoaoMarcos44 marked this conversation as resolved.
Resolved
return
Comment thread
JoaoMarcos44 marked this conversation as resolved.
Resolved
Comment thread
JoaoMarcos44 marked this conversation as resolved.
Resolved
channel_name, user_name, channel_prompt, auto_skill = (
Comment thread
JoaoMarcos44 marked this conversation as resolved.
Resolved
await self._resolve_turn_prompt_inputs(channel_id, team_id, user_id))
Comment thread
JoaoMarcos44 marked this conversation as resolved.
Resolved
Comment thread
JoaoMarcos44 marked this conversation as resolved.
Resolved
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=channel_name,
chat_type="dm" if is_dm else "group", user_id=user_id, user_name=user_name,
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=channel_prompt, auto_skill=auto_skill)
Comment thread
JoaoMarcos44 marked this conversation as resolved.
Resolved
# 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", "")
Expand Down
48 changes: 48 additions & 0 deletions tests/gateway/test_slack.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,54 @@ async def test_channel_slash_command_uses_group_session_semantics(self, adapter)
assert event.source.user_id == "U123"
assert event.source.scope_id == "T123"

@pytest.mark.asyncio
async def test_slash_turn_matches_message_prompt_inputs(self, adapter):
adapter.config.extra.update({
"channel_prompts": {"C123": "Answer in haiku."},
"channel_skill_bindings": [{"id": "C123", "skill": "triage"}],
})
adapter._bot_display_name = "HermesBot"
adapter._app.client.conversations_info = AsyncMock(
return_value={"ok": True, "channel": {"id": "C123", "name": "ops"}})
adapter.set_authorization_check(lambda _user, _type, _chat: True)

await adapter._handle_slash_command({
"command": "/hermes", "text": "what broke?", "user_id": "U123",
"channel_id": "C123", "team_id": "T123",
})
slash = adapter.handle_message.await_args.args[0]

message = await adapter._build_message_event(
Comment thread
JoaoMarcos44 marked this conversation as resolved.
Resolved
{"user": "U123", "channel": "C123"},
text="what broke?", original_text="what broke?", command_probe_text="what broke?",
is_command_text=False, channel_id="C123", team_id="T123", ts="171.001",
user_id="U123", thread_ts=None, is_dm=False, media_urls=[], media_types=[],
media_text_inlined=[], channel_context=None)

assert message.channel_prompt
assert 'bot "@HermesBot"' in message.channel_prompt
assert "Answer in haiku." in message.channel_prompt
assert slash.channel_prompt == message.channel_prompt
assert slash.auto_skill == message.auto_skill == ["triage"]
assert (slash.source.chat_name, slash.source.user_name) == (
message.source.chat_name, message.source.user_name) == ("ops", "Test User")

@pytest.mark.asyncio
async def test_unauthorized_slash_stops_before_name_enrichment(self, adapter):
adapter.set_authorization_check(lambda _user, _type, _chat: False)
adapter._app.client.conversations_info = AsyncMock(
return_value={"ok": True, "channel": {"id": "C123", "name": "ops"}})
adapter._app.client.users_info.reset_mock()

await adapter._handle_slash_command({
"command": "/hermes", "text": "what broke?", "user_id": "U_BAD",
"channel_id": "C123", "team_id": "T123",
})

adapter._app.client.conversations_info.assert_not_awaited()
adapter._app.client.users_info.assert_not_awaited()
adapter.handle_message.assert_not_awaited()


class TestSlackWorkspaceCollisionIsolation:
@pytest.mark.asyncio
Expand Down