diff --git a/gateway/run.py b/gateway/run.py index f9bf9a38b9..327f8ae32a 100644 --- a/gateway/run.py +++ b/gateway/run.py @@ -485,17 +485,21 @@ def _resolve_hermes_bin() -> Optional[list[str]]: def _parse_session_key(session_key: str) -> "dict | None": """Parse a session key into its component parts. - Session keys follow the format ``agent:main:{platform}:{chat_type}:{chat_id}``. - Returns a dict with ``platform``, ``chat_type``, and ``chat_id`` keys, - or None if the key doesn't match the expected format. + Session keys follow the format + ``agent:main:{platform}:{chat_type}:{chat_id}[:{thread_id}[:{user_id}]]``. + Returns a dict with ``platform``, ``chat_type``, ``chat_id``, and + optionally ``thread_id`` keys, or None if the key doesn't match. """ parts = session_key.split(":") if len(parts) >= 5 and parts[0] == "agent" and parts[1] == "main": - return { + result = { "platform": parts[2], "chat_type": parts[3], "chat_id": parts[4], } + if len(parts) > 5: + result["thread_id"] = parts[5] + return result return None @@ -1526,7 +1530,7 @@ class GatewayRunner: # Include thread_id if present so the message lands in the # correct forum topic / thread. - thread_id = parts[5] if len(parts) > 5 else None + thread_id = _parsed.get("thread_id") metadata = {"thread_id": thread_id} if thread_id else None await adapter.send(chat_id, msg, metadata=metadata) diff --git a/tests/gateway/test_background_process_notifications.py b/tests/gateway/test_background_process_notifications.py index 68eb5e3043..eabf92be63 100644 --- a/tests/gateway/test_background_process_notifications.py +++ b/tests/gateway/test_background_process_notifications.py @@ -383,9 +383,15 @@ def test_parse_session_key_valid(): def test_parse_session_key_with_extra_parts(): - """Extra trailing parts (thread_id etc.) are ignored — only first 5 matter.""" + """Thread ID (6th part) is extracted; further parts are ignored.""" result = _parse_session_key("agent:main:discord:group:chan123:thread456") - assert result == {"platform": "discord", "chat_type": "group", "chat_id": "chan123"} + assert result == {"platform": "discord", "chat_type": "group", "chat_id": "chan123", "thread_id": "thread456"} + + +def test_parse_session_key_with_user_id_part(): + """7th part (user_id) is ignored — only up to thread_id is extracted.""" + result = _parse_session_key("agent:main:telegram:group:chat1:thread42:user99") + assert result == {"platform": "telegram", "chat_type": "group", "chat_id": "chat1", "thread_id": "thread42"} def test_parse_session_key_too_short():