The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(slack): thread follow-ups and reaction triggers keep the silence-marker fallback
kshitijk4poor6c566fcd6dce
diff --git a/plugins/platforms/slack/adapter.py b/plugins/platforms/slack/adapter.pyindex d0a15decbf9..a19741e2a03 100644--- a/plugins/platforms/slack/adapter.py+++ b/plugins/platforms/slack/adapter.py@@ -85,14 +85,6 @@ _MODEL_PICKER_ACTION_IDS = ()-def slack_reply_expected(*, is_one_to_one_dm: bool, is_mentioned: bool, is_command_text: bool) -> bool:- """Was this inbound message addressed to the bot? A 1:1 DM, an @mention of this bot or a- command expects a reply, so the gateway must not let a bare silence marker vanish it. A message- admitted only through a free-response channel, a thread follow-up or ``ignore_other_user_mentions:- false`` may stay silent."""- return bool(is_one_to_one_dm or is_mentioned or is_command_text)--def _slack_unfurl_kwargs(extra: Optional[Dict[str, Any]]) -> Dict[str, bool]:"""Explicitly configured link-preview controls (omitted key = Slack default). String bools arecoerced (config tooling persists YAML bools as strings); junk is dropped, NOT coerced to False,@@ -4203,8 +4195,7 @@ class SlackAdapter(BasePlatformAdapter):thread_gated = self._slack_thread_require_mention() and is_thread_reply and not is_mentionedif force_process:return True- free_channel = channel_id not in self._slack_require_mention_channels() and (- channel_id in self._slack_free_response_channels() or not self._slack_require_mention())+ free_channel = self._slack_is_free_channel(channel_id)if not free_channel and self._slack_strict_mention() and not is_mentioned:return False # Strict mode: ignore until @-mentioned againif thread_gated:@@ -4671,8 +4662,10 @@ class SlackAdapter(BasePlatformAdapter):is_command_text=is_command_text, channel_id=channel_id, team_id=team_id, ts=ts,user_id=user_id, thread_ts=thread_ts, is_dm=is_dm, media_urls=media_urls,media_types=media_types, media_text_inlined=media_text_inlined, channel_context=channel_context,- reply_expected=slack_reply_expected(- is_one_to_one_dm=is_one_to_one_dm, is_mentioned=is_mentioned, is_command_text=is_command_text))+ reply_expected=self._slack_reply_expected(+ routing_text, {u for u in (bot_uid, self._bot_user_id) if u}, channel_id=channel_id,+ addressed=is_one_to_one_dm or is_mentioned or is_command_text or force_process,+ is_thread_reply=is_thread_reply))# React only when directly addressed; MPIMs are shared, so they need a# mention like any channel.if (is_one_to_one_dm or is_mentioned) and self._reactions_enabled():@@ -6360,6 +6353,24 @@ class SlackAdapter(BasePlatformAdapter):match = text and re.match(r"\s*<@([^>|\s]+)(?:\|[^>]*)?>", text)return bool(match) and match.group(1) not in self_uids+ def _slack_is_free_channel(self, channel_id: str) -> bool:+ """Does ``channel_id`` admit messages without an @mention?"""+ return channel_id not in self._slack_require_mention_channels() and (+ channel_id in self._slack_free_response_channels() or not self._slack_require_mention())++ def _slack_reply_expected(+ self, routing_text: str, self_uids: set, *, channel_id: str, addressed: bool,+ is_thread_reply: bool) -> Optional[bool]:+ """``MessageEvent.reply_expected`` for an admitted message. False (a bare silence marker may+ stand) only when it opens by @mentioning someone else, or is a top-level message a+ free-response channel admitted unaddressed. A plain reply in a thread the bot is part of is+ None: it is usually meant for the bot, so the gateway keeps its visible fallback (#110952)."""+ if addressed or self._slack_message_mentions_self(routing_text, self_uids):+ return True+ if self._slack_message_addressed_to_other_user(routing_text, self_uids):+ return False+ return False if not is_thread_reply and self._slack_is_free_channel(channel_id) else None+def _slack_message_mentions_self(self, text: str, self_uids: set) -> bool:"""True when ``text`` @-mentions this bot anywhere, in either ``<@U123>`` or``<@U123|name>`` form (``is_mentioned`` only recognises the former)."""diff --git a/tests/gateway/test_slack_reply_expected.py b/tests/gateway/test_slack_reply_expected.pyindex 928842d2bc8..8c202610216 100644--- a/tests/gateway/test_slack_reply_expected.py+++ b/tests/gateway/test_slack_reply_expected.py@@ -1,46 +1,42 @@-"""The Slack adapter stamps ``reply_expected`` on the event it hands the gateway.+"""The Slack adapter tells the gateway whether an admitted message was addressed to the bot.-A message admitted only because of a free-response channel, a thread follow-up, or a mention of-someone else carries ``reply_expected=False``; a 1:1 DM, an @mention of this bot, or a command-carries ``True``. The gateway lets a bare silence marker stand on ``False`` and keeps the visible-fallback on ``True`` (see tests/gateway/test_gateway_silence_tokens.py).+``MessageEvent.reply_expected`` decides whether a bare silence marker may stand+(tests/gateway/test_gateway_silence_tokens.py). Only a message that opens by @mentioning someone+else, or a top-level message a free-response channel admitted unaddressed, may stay silent; a plain+reply in a thread the bot is part of keeps the visible fallback (#110952). Driven through the real+``_handle_slack_message`` so the admission wiring, not just the rule, is covered."""-from unittest.mock import AsyncMock+from unittest.mock import AsyncMock, patchimport pytest-from tests.gateway.test_slack_mention import _make_adapter+from tests.gateway.test_slack_ignore_other_user_mentions import ( # noqa: F401 - fixtures+ BOT_USER_ID, CHANNEL_ID, OTHER_USER_ID, _redirect_cache, adapter,+)+THREAD = "1700000000.000010"-async def _event(adapter, **kw):- # _make_adapter builds the object without __init__; stub the network-backed resolvers only.- adapter._resolve_user_name = AsyncMock(return_value="alice")- adapter._resolve_channel_name = AsyncMock(return_value="general")- adapter._humanize_user_mentions = AsyncMock(side_effect=lambda text, **_: text)- adapter._channel_prompt_with_identity = lambda *_a, **_k: None- base = dict(- event={"user": "U1", "ts": "1.0", "channel": "C1", "text": "hi"}, text="hi", original_text="hi",- command_probe_text="hi", is_command_text=False, channel_id="C1", team_id="T1", ts="1.0",- user_id="U1", thread_ts=None, is_dm=False, media_urls=[], media_types=[],- media_text_inlined=[], channel_context=None,- )- base.update(kw)- return await adapter._build_message_event(**base)++def _message(text, ts, **extra):+ return {"channel": CHANNEL_ID, "channel_type": "channel", "user": "U_HUMAN", "text": text, "ts": ts, **extra}@pytest.mark.asyncio[email protected]("reply_expected", [True, False, None])-async def test_build_message_event_carries_reply_expected(reply_expected):- adapter = _make_adapter()- ev = await _event(adapter, reply_expected=reply_expected)- assert ev.reply_expected is reply_expected---def test_reply_expected_rule_matches_addressing():- """The value the inbound handler computes: DM, mention of this bot, or a command."""- from plugins.platforms.slack.adapter import slack_reply_expected- assert slack_reply_expected(is_one_to_one_dm=True, is_mentioned=False, is_command_text=False) is True- assert slack_reply_expected(is_one_to_one_dm=False, is_mentioned=True, is_command_text=False) is True- assert slack_reply_expected(is_one_to_one_dm=False, is_mentioned=False, is_command_text=True) is True- # Admitted via free channel / thread follow-up / peer mention with ignore_other_user_mentions off.- assert slack_reply_expected(is_one_to_one_dm=False, is_mentioned=False, is_command_text=False) is False[email protected]("extra, event, expected", [+ ({}, _message("hi", "1.1", channel="D0001", channel_type="im"), True),+ ({}, _message(f"<@{BOT_USER_ID}> hi", "1.2"), True),+ ({}, _message("reaction:added:eyes", "1.3", thread_ts=THREAD, _hermes_force_process=True), True),+ ({}, _message("done?", "1.4", thread_ts=THREAD), None),+ ({}, _message(f"<@{OTHER_USER_ID}> can you check?", "1.5", thread_ts=THREAD), False),+ ({"free_response_channels": CHANNEL_ID}, _message("side chatter", "1.6"), False),+], ids=["dm", "mention", "reaction", "thread-followup", "peer-addressed", "free-channel-top-level"])+async def test_admitted_message_carries_whether_it_was_addressed(adapter, extra, event, expected):+ adapter.config.extra.update(extra)+ adapter._mentioned_threads.add(THREAD)+ with patch.object(adapter, "_resolve_user_name", new=AsyncMock(return_value="human")), \+ patch.object(adapter, "_fetch_thread_context", new=AsyncMock(return_value=None)), \+ patch.object(adapter, "_fetch_thread_parent_text", new=AsyncMock(return_value="")), \+ patch.object(adapter, "_has_active_session_for_thread", return_value=False):+ await adapter._handle_slack_message(event)+ adapter.handle_message.assert_awaited_once()+ assert adapter.handle_message.await_args.args[0].reply_expected is expecteddiff --git a/website/docs/user-guide/messaging/index.md b/website/docs/user-guide/messaging/index.mdindex 03098e7afe3..493e5858505 100644--- a/website/docs/user-guide/messaging/index.md+++ b/website/docs/user-guide/messaging/index.md@@ -154,6 +154,8 @@ user: next messageFailed turns still surface as errors; Hermes does not hide failures just because the text resembles a silence token.+On a message from a person, a bare silence token is replaced by a short notice, because a message that needed a reply must not vanish. Internal wakes such as background-process notifications may stay silent, and so may a message the platform adapter reports as not addressed to the bot. Slack reports this for messages that open by @mentioning someone else and for unmentioned top-level messages in free-response channels; other platforms always get the notice.+## Quick SetupThe easiest way to configure messaging platforms is the interactive wizard:diff --git a/website/docs/user-guide/messaging/slack.md b/website/docs/user-guide/messaging/slack.mdindex d11f2453b31..818a708b3ce 100644--- a/website/docs/user-guide/messaging/slack.md+++ b/website/docs/user-guide/messaging/slack.md@@ -672,10 +672,13 @@ Set this to `true` in busy workspaces where Slack's default "the bot remembers t::::::tip When to use `ignore_other_user_mentions`-If the model returns a bare silence marker for a message that was not addressed to this bot, the gateway keeps it silent. On a direct message or an explicit mention of the bot, the same marker produces the visible fallback notice.Set this to `true` when the bot follows busy threads (via thread auto-engagement or `free_response_channels`) and butts in on messages humans address to each other. It is a narrower tool than `strict_mention`: plain follow-ups in an engaged thread still get answers; only messages that open by @mentioning another person are skipped. **1:1 DMs are unaffected**; group DMs (MPIMs) and channels both apply it, matching the shared-surface policy below. Broadcast tokens (`@here`, `@channel`) and channel references address the room, not a person, so they are never skipped.:::+:::note Silence markers on messages not addressed to the bot+When the bot answers a human message with only a [silence token](index.md#intentional-silence-tokens), Hermes normally posts a short notice instead so a question never goes unanswered. On Slack the token is allowed to stand when the message opened by @mentioning someone else, or was a top-level message in a `free_response_channels` channel without a mention. A 1:1 DM, a mention of the bot, a command, a reaction trigger, or a plain reply in a thread the bot is part of still gets the notice.+:::+:::infoSlack supports both patterns: `@mention` required to start a conversation by default, but you can opt specific channels out via `SLACK_FREE_RESPONSE_CHANNELS` (comma-separated channel IDs) or `slack.free_response_channels` in `config.yaml`. Once the bot has an active session in a thread, subsequent thread replies do not require a mention. In **1:1 DMs** the bot always responds without needing a mention.:::