Skip to content

The agent that grows with you

fix(gateway): keep Telegram private restart notices profile-scoped

funky-xamarin0536b2a1470d
diff --git a/gateway/run_notifications.py b/gateway/run_notifications.py
index 21d50135be9..036ea7ca430 100644
--- a/gateway/run_notifications.py
+++ b/gateway/run_notifications.py
@@ -51,12 +51,13 @@ def _served_notice_target_key(profile: Optional[str], platform_value: str, chat_
platform_value if profile is None else f"{profile}:{platform_value}", chat_id, thread_id)
-def _delivery_target_key(platform_value: str, chat_id, thread_id) -> tuple:
- """Dedupe key for one DELIVERED chat, profile-independent.
-
- Two served profiles can share a single home chat (one Telegram group for the whole host);
- keyed per profile they would each post their own "Gateway online" notice into it.
- """
+def _delivery_target_key(platform_value: str, chat_id, thread_id, *, profile: Optional[str] = None) -> tuple:
+ """Dedupe shared chats across profiles, but not Telegram bot-private conversations."""
+ # Telegram private chat IDs identify the user, not the conversation with a particular bot.
+ # Groups have negative IDs and remain one shared destination across served profiles.
+ chat = str(chat_id)
+ if platform_value == "telegram" and chat.isdecimal() and int(chat) > 0:
+ return _served_notice_target_key(profile, platform_value, chat_id, thread_id)
return _notice_target_key(platform_value, chat_id, thread_id)
@@ -1014,7 +1015,7 @@ class GatewayNotificationsMixin:
targets = list(self._served_home_channel_transports())
# A chat already notified for ANOTHER profile is not notified again.
notified_chats = {
- _delivery_target_key(platform.value, home.chat_id, home.thread_id)
+ _delivery_target_key(platform.value, home.chat_id, home.thread_id, profile=profile)
for profile, platform, _cfg, home, _transport in targets
if _served_notice_target_key(profile, platform.value, home.chat_id, home.thread_id) in skipped
}
@@ -1028,7 +1029,7 @@ class GatewayNotificationsMixin:
target = _served_notice_target_key(profile, platform.value, home.chat_id, home.thread_id)
if target in skipped or target in delivered:
continue
- chat = _delivery_target_key(platform.value, home.chat_id, home.thread_id)
+ chat = _delivery_target_key(platform.value, home.chat_id, home.thread_id, profile=profile)
if chat in notified_chats:
delivered.add(target)
continue
diff --git a/tests/gateway/test_planned_restart_notice_multiplex.py b/tests/gateway/test_planned_restart_notice_multiplex.py
index f7b93e4907f..0ad5048d4f2 100644
--- a/tests/gateway/test_planned_restart_notice_multiplex.py
+++ b/tests/gateway/test_planned_restart_notice_multiplex.py
@@ -95,6 +95,52 @@ async def test_marker_survives_until_a_served_profile_is_reachable(multiplex_run
assert not marker.exists()
[email protected]("outcome", ["success", "failure", "exception", "disconnected", "disabled", "empty-home"])
+async def test_private_home_notices_reach_both_bots(multiplex_runner, outcome):
+ """Regression for #118233: equal user IDs are distinct bot conversations."""
+ runner, marker = multiplex_runner
+ runner.config = _home_config(Platform.TELEGRAM, "8776018003")
+ launch, coder = _adapter(), _adapter()
+ runner.adapters = {Platform.TELEGRAM: launch}
+ cfg = _home_config(Platform.TELEGRAM, "8776018003")
+ runner._profile_configs = {"coder": cfg}
+ runner._profile_adapters = {"coder": {Platform.TELEGRAM: coder}}
+ if outcome == "failure":
+ coder.send.return_value = SendResult(success=False, error="temporary failure")
+ elif outcome == "exception":
+ coder.send.side_effect = RuntimeError("temporary failure")
+ elif outcome == "disconnected":
+ runner._profile_adapters["coder"] = {}
+ elif outcome == "disabled":
+ cfg.platforms[Platform.TELEGRAM].gateway_restart_notification = False
+ elif outcome == "empty-home":
+ cfg.platforms[Platform.TELEGRAM].home_channel = None
+
+ await runner._replay_pending_planned_restart_notification()
+
+ launch.send.assert_awaited_once()
+ if outcome in {"disconnected", "disabled", "empty-home"}:
+ coder.send.assert_not_awaited()
+ else:
+ coder.send.assert_awaited_once()
+ if outcome in {"failure", "exception", "disconnected"}:
+ assert marker.exists(), "the second bot conversation is still owed its notice"
+ recorded = json.loads(marker.read_text(encoding="utf-8"))["delivered_targets"]
+ assert ["telegram", "8776018003", None] in recorded
+ assert ["coder:telegram", "8776018003", None] not in recorded
+ coder.send.reset_mock()
+ coder.send.side_effect = None
+ coder.send.return_value = SendResult(success=True, message_id="recovered")
+ runner._profile_adapters["coder"][Platform.TELEGRAM] = coder
+ await runner._replay_pending_planned_restart_notification()
+ coder.send.assert_awaited_once()
+ launch.send.assert_awaited_once()
+ assert not marker.exists()
+ await runner._replay_pending_planned_restart_notification()
+ launch.send.assert_awaited_once()
+
+
@pytest.mark.asyncio
async def test_profiles_sharing_one_home_chat_get_one_notice(tmp_path, monkeypatch):
"""One host process restarting once owes a shared chat ONE notice, not one per profile.