Skip to content

The agent that grows with you

fix(telegram): an edit of an older lobby message keeps its room (#136567)

Tekniumae596f49a0a8
diff --git a/plugins/platforms/telegram/telegram_topic_routing.py b/plugins/platforms/telegram/telegram_topic_routing.py
index 11caa16c7fb..ebee6fa18ca 100644
--- a/plugins/platforms/telegram/telegram_topic_routing.py
+++ b/plugins/platforms/telegram/telegram_topic_routing.py
@@ -39,6 +39,7 @@ class TelegramTopicRoutingMixin:
@staticmethod
def _sent_at(message) -> Optional[float]:
+ """Telegram's send stamp (whole seconds); an edit keeps its message's original ``date``."""
date = getattr(message, "date", None)
return date.timestamp() if hasattr(date, "timestamp") else None
@@ -83,14 +84,19 @@ class TelegramTopicRoutingMixin:
if chat_type != "dm" or not pending:
return thread_id
key = (self._chat_id_str(message), self._creator_key(message))
- record = pending.pop(key, None)
- if record is None or thread_id is not None or getattr(message, "message_thread_id", None) is not None:
+ record = pending.get(key)
+ sent_at = self._sent_at(message)
+ # An edit of a message sent before the creation stays where that message lived and leaves
+ # the record for the real opener.
+ if record is None or sent_at is None or sent_at < record[1]:
+ return thread_id
+ del pending[key]
+ if thread_id is not None or getattr(message, "message_thread_id", None) is not None:
return thread_id
created_thread_id, last_sent_at, _recorded = record
- sent_at = self._sent_at(message)
- if sent_at is None or sent_at - last_sent_at > self._NEW_TOPIC_FOLLOW_SECONDS:
+ if sent_at - last_sent_at > self._NEW_TOPIC_FOLLOW_SECONDS:
return thread_id
- pending[key] = (created_thread_id, max(sent_at, last_sent_at), time.monotonic())
+ pending[key] = (created_thread_id, sent_at, time.monotonic())
logger.info("[%s] Thread-less message %s adopted by new Telegram topic %s (chat=%s)",
self.name, getattr(message, "message_id", "?"), created_thread_id, key[0])
return created_thread_id
diff --git a/tests/gateway/test_telegram_new_topic_opener.py b/tests/gateway/test_telegram_new_topic_opener.py
index 0ec4553933a..d628feb08f9 100644
--- a/tests/gateway/test_telegram_new_topic_opener.py
+++ b/tests/gateway/test_telegram_new_topic_opener.py
@@ -24,12 +24,13 @@ T0 = datetime(2026, 10, 14, 13, 21, 2, tzinfo=UTC)
WINDOW = routing.TelegramTopicRoutingMixin._NEW_TOPIC_FOLLOW_SECONDS
-def _msg(message_id, sent, *, thread_id=None, created=False, user_id=USER, chat_id=CHAT, text="hi"):
+def _msg(message_id, sent, *, thread_id=None, created=False, user_id=USER, chat_id=CHAT, text="hi", edited=None):
chat = SimpleNamespace(id=chat_id, type="private", title=None, full_name="T")
return SimpleNamespace(
chat=chat, from_user=SimpleNamespace(id=user_id, full_name="T", is_bot=False),
text=None if created else text, message_thread_id=thread_id, is_topic_message=thread_id is not None,
message_id=message_id, reply_to_message=None, date=T0 + timedelta(seconds=sent),
+ edit_date=None if edited is None else T0 + timedelta(seconds=edited),
forum_topic_created=SimpleNamespace(name="New topic") if created else None)
@@ -71,6 +72,8 @@ async def test_creation_is_not_adopted_late_or_by_others(adapter):
await _create(adapter, 10)
assert _thread(adapter, _msg(11, 0.1, user_id=7)) is None # another sender
assert _thread(adapter, _msg(12, 0.1, chat_id=222)) is None # another chat
+ # An edit of a lobby message sent before the creation stays where that message lived.
+ assert _thread(adapter, _msg(5, -60, edited=1.0)) is None
assert _thread(adapter, _msg(13, WINDOW + 1)) is None # a lobby message sent well after the creation
await _create(adapter, 20, user_id=7) # creation by a user the adapter would reject
assert not adapter._pending_created_topics