The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): chat-scope match must not alias a chat_id or a reply thread
kshitijk4poor04c0bec90549
diff --git a/gateway/run_busy.py b/gateway/run_busy.pyindex 756dba86595..74a4b25a51a 100644--- a/gateway/run_busy.py+++ b/gateway/run_busy.py@@ -52,7 +52,10 @@ def _same_chat_key_slots(rest = slots[3:]if rest[1] == chat_id:return rest[0], rest[2:]- if len(rest) >= 3 and rest[2] == chat_id:+ # Only Slack ever carries the scope slot (``build_session_key``). Tolerating it on other+ # platforms would let a group key's trailing participant id alias a chat_id: a Telegram DM+ # keys ``chat_id`` as the USER id, so ``group:<chat>:<user>`` would read as "<user>'s chat".+ if platform == Platform.SLACK.value and len(rest) >= 3 and rest[2] == chat_id:if scope_id and rest[1] != str(scope_id):return Nonereturn rest[0], rest[3:]@@ -1108,16 +1111,17 @@ class GatewayBusySessionMixin:so the handler falls back chat-wide on an exact + thread-sibling miss — which is also whatlets a human stop a peer's per-sender group run (see ``_same_chat_runs``).- A run in a DIFFERENT thread of the same channel is a different conversation and stays- untouched: only the caller's own thread, a slotless run (top-level channel turn, rolling- DM) and non-thread keys (group/channel participant slots) are reachable. Callers gate on- authz.+ A stop sent from INSIDE a thread only reaches runs whose own thread slot is that thread (or+ that carry no thread slot at all — a top-level channel turn's relay-stamped slot is the+ reply thread it belongs to, the rolling-DM shape is slotless). Anything else in the channel+ is a different conversation: another reply thread, or a peer's top-level run. Callers gate+ on authz."""- thread_id = getattr(source, "thread_id", None)+ thread_id = str(getattr(source, "thread_id", None) or "")return [key- for key, key_chat_type, tail in self._same_chat_runs(source, own_key)- if not (key_chat_type == "thread" and thread_id and tail and tail[0] != str(thread_id))+ for key, _key_chat_type, tail in self._same_chat_runs(source, own_key)+ if not (thread_id and tail and tail[0] != thread_id)]def _is_stale_restart_redelivery(self, event: MessageEvent) -> bool:diff --git a/tests/gateway/test_stop_chat_scope.py b/tests/gateway/test_stop_chat_scope.pyindex 049ae5089a0..62bbee01b72 100644--- a/tests/gateway/test_stop_chat_scope.py+++ b/tests/gateway/test_stop_chat_scope.py@@ -124,6 +124,37 @@ async def test_stop_does_not_reach_a_different_thread_of_the_same_channel():assert result == t("gateway.stop.no_active")+async def test_stop_does_not_reach_another_reply_thread_of_a_channel_keyed_run():+ # A top-level channel turn keeps chat_type "channel" with the relay-stamped reply-thread ts,+ # so the SAME boundary must apply to it: a stop inside thread .100 must not reach the run+ # whose reply thread is .200.+ other_reply_thread = build_session_key(_slack_source("channel", "C9", thread_id="170.200"))+ stop_source = _slack_source("thread", "C9", thread_id="170.100")++ _, interrupted, result = await _stop(stop_source, other_reply_thread)++ assert interrupted == []+ assert result == t("gateway.stop.no_active")+++async def test_stop_in_a_dm_does_not_reach_a_group_run_that_ends_in_the_same_user_id():+ # Non-Slack DMs key chat_id as the USER id (Telegram), and a per-sender group key ends with+ # that same user id — the group run is a different chat and must stay untouched.+ group_run = build_session_key(+ SessionSource(platform=Platform.TELEGRAM, chat_type="group", chat_id="-100123",+ user_id="777")+ )+ dm_stop = SessionSource(platform=Platform.TELEGRAM, chat_type="dm", chat_id="777",+ user_id="777")++ _, interrupted, result = await _stop(dm_stop, group_run)++ assert interrupted == []+ assert result == t("gateway.stop.no_active")++@pytest.mark.asyncioasync def test_chat_scope_fallback_is_authorization_gated():running_key = build_session_key(_slack_source("channel", "C9", thread_id="170.100"))@@ -169,7 +200,11 @@ async def test_chat_scope_fallback_interrupts_only_the_callers_chat(other_chat_i@pytest.mark.asyncioasync def test_chat_scope_fallback_does_not_cross_workspace_scope_or_profile():+ # A same-chat run stays live beside the foreign ones, so a parser that matched NOTHING+ # cannot pass this test.+ same_chat = build_session_key(_slack_source("channel", "C9", thread_id="170.100"))running_keys = [+ same_chat,# Same chat_id, different Slack workspace (scope_id).build_session_key(_slack_source("channel", "C9", thread_id="170.100", scope_id="T2")),# Same chat_id, different profile namespace.@@ -179,5 +214,5 @@ async def test_chat_scope_fallback_does_not_cross_workspace_scope_or_profile():_, interrupted, result = await _stop(stop_source, running_keys)- assert interrupted == []- assert result == t("gateway.stop.no_active")+ assert interrupted == [(same_chat, "stop_command_chat_scope")]+ assert result == t("gateway.stop.stopped")diff --git a/website/docs/user-guide/sessions.md b/website/docs/user-guide/sessions.mdindex 72e0a6e5b94..d5f2c6228de 100644--- a/website/docs/user-guide/sessions.md+++ b/website/docs/user-guide/sessions.md@@ -801,9 +801,10 @@ By default, Hermes uses `group_sessions_per_user: true` in `config.yaml`. That m`/stop` means "stop what is running in this chat": it first tries the caller's own session key,then other participants' runs in the caller's own thread, then any live turn in the same room —-authorization-gated, and never another room, workspace, profile or thread. So an idle Alice's-`/stop` can end a turn Bob (or a bot) started in the room she is in; it cannot reach a turn in a-different thread of the same channel.+authorization-gated, and never another room, workspace or profile. So an idle Alice's `/stop` can+end a turn Bob (or a bot) started in the room she is in. A `/stop` sent from *inside* a thread is+narrower: it only reaches runs belonging to that thread (or to no thread at all), never a turn in+another thread of the same channel.If you want one shared "room brain" instead, set: