From 8d3c4501263886fa2ca91e59b75b9d578a4685cd Mon Sep 17 00:00:00 2001 From: teknium1 <127238744+teknium1@users.noreply.github.com> Date: Wed, 1 Jul 2026 00:38:10 -0700 Subject: [PATCH] refactor(gateway): reuse looks_like_telegram_private_chat_id helper The handoff seed path inlined its own int(chat_id) > 0 private-chat check; delivery.py already had the identical heuristic. Promote it to a public name and reuse it from both sites instead of duplicating. --- cron/scheduler.py | 4 ++-- gateway/delivery.py | 13 ++++++++++--- gateway/run.py | 12 +++++------- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/cron/scheduler.py b/cron/scheduler.py index eb43196a7dd..7c82829ebf5 100644 --- a/cron/scheduler.py +++ b/cron/scheduler.py @@ -1245,13 +1245,13 @@ def _deliver_result(job: dict, content: str, adapters=None, loop=None) -> Option DeliveryRouter, DeliveryTarget, _looks_like_int, - _looks_like_telegram_private_chat_id, + looks_like_telegram_private_chat_id, ) is_private_dm_topic = ( platform == Platform.TELEGRAM and thread_id is not None - and _looks_like_telegram_private_chat_id(str(chat_id)) + and looks_like_telegram_private_chat_id(str(chat_id)) and _looks_like_int(str(thread_id)) ) if is_private_dm_topic: diff --git a/gateway/delivery.py b/gateway/delivery.py index 58280371ce1..304ceecd7ae 100644 --- a/gateway/delivery.py +++ b/gateway/delivery.py @@ -59,7 +59,14 @@ from .session import SessionSource from .dead_targets import DeadTargetRegistry -def _looks_like_telegram_private_chat_id(chat_id: Optional[str]) -> bool: +def looks_like_telegram_private_chat_id(chat_id: Optional[str]) -> bool: + """True when ``chat_id`` is a positive int — Telegram's private-chat shape. + + Telegram private chats use positive chat IDs; groups/channels/supergroups + use negative IDs. This is the single source of truth for that heuristic, + reused by the handoff seed path in ``gateway/run.py`` so handoff-created + DM topics key the same way as inbound DM-topic messages. + """ if chat_id is None: return False try: @@ -467,7 +474,7 @@ class DeliveryRouter: target_thread_id = target.thread_id is_named_telegram_private_topic = ( target.platform == Platform.TELEGRAM - and _looks_like_telegram_private_chat_id(target.chat_id) + and looks_like_telegram_private_chat_id(target.chat_id) and not _looks_like_int(target_thread_id) and "thread_id" not in send_metadata and "message_thread_id" not in send_metadata @@ -490,7 +497,7 @@ class DeliveryRouter: send_metadata["telegram_dm_topic_created_for_send"] = True elif ( target.platform == Platform.TELEGRAM - and _looks_like_telegram_private_chat_id(target.chat_id) + and looks_like_telegram_private_chat_id(target.chat_id) and "thread_id" not in send_metadata and "message_thread_id" not in send_metadata and not has_explicit_direct_topic diff --git a/gateway/run.py b/gateway/run.py index 0fbc776cbcb..053f95f8793 100644 --- a/gateway/run.py +++ b/gateway/run.py @@ -1676,7 +1676,7 @@ from gateway.session import ( build_session_key, is_shared_multi_user_session, ) -from gateway.delivery import DeliveryRouter +from gateway.delivery import DeliveryRouter, looks_like_telegram_private_chat_id from gateway.authz_mixin import GatewayAuthorizationMixin from gateway.kanban_watchers import GatewayKanbanWatchersMixin from gateway.slash_commands import GatewaySlashCommandsMixin @@ -6823,12 +6823,10 @@ class GatewayRunner(GatewayAuthorizationMixin, GatewayKanbanWatchersMixin, Gatew # handoff turn binds a generic `thread` session key while real replies # arrive on a `dm` session key. home_chat_id = str(home.chat_id) - is_telegram_private_chat = False - if platform == Platform.TELEGRAM: - try: - is_telegram_private_chat = int(home_chat_id) > 0 - except (TypeError, ValueError): - is_telegram_private_chat = False + is_telegram_private_chat = ( + platform == Platform.TELEGRAM + and looks_like_telegram_private_chat_id(home_chat_id) + ) if new_thread_id and not is_telegram_private_chat: dest_chat_type = "thread"