Skip to content

The agent that grows with you

fix(telegram): read dm_topic_auto_transfer from the receiving bot

Teknium42e5a8578fa9
diff --git a/gateway/run_topics.py b/gateway/run_topics.py
index aa5303f7c7b..3a5e40152cd 100644
--- a/gateway/run_topics.py
+++ b/gateway/run_topics.py
@@ -199,17 +199,16 @@ class GatewayTopicThreadsMixin:
return recovered if recovered and recovered != inbound else None
return None
- def _telegram_dm_topic_auto_transfer_configured(self) -> bool:
- """``gateway.platforms.telegram.extra.dm_topic_auto_transfer``; default False. Off keeps the
- v0.22 lobby recovery (#136567). Config only: no state.db read, safe on the event loop."""
- config = getattr(self, "config", None)
- platforms = getattr(config, "platforms", None) if config is not None else None
- platform_cfg = platforms.get(Platform.TELEGRAM) if platforms else None
+ def _telegram_dm_topic_auto_transfer_configured(self, source: SessionSource) -> bool:
+ """The RECEIVING bot's ``extra.dm_topic_auto_transfer`` (default False): under multiplexing each
+ bot owns its opt-in. Off keeps the v0.22 lobby recovery (#136567). No state.db read."""
+ adapter = self._delivery_adapter_for(source)
+ platform_cfg = getattr(adapter, "config", None) if adapter is not None else None
return is_truthy_value((getattr(platform_cfg, "extra", None) or {}).get("dm_topic_auto_transfer"))
def _telegram_topic_auto_transfer_enabled(self, source: SessionSource) -> bool:
"""The key is on and ``source`` is a ``/topic``-mode root DM (reads state.db: call off-loop)."""
- return self._telegram_dm_topic_auto_transfer_configured() and self._is_telegram_topic_root_lobby(source)
+ return self._telegram_dm_topic_auto_transfer_configured(source) and self._is_telegram_topic_root_lobby(source)
async def _transfer_telegram_root_dm(self, source: SessionSource) -> Optional[tuple[str, bool]]:
"""Adapter-ingress transfer hook for an opted-in root-DM message (#139136).
@@ -218,7 +217,7 @@ class GatewayTopicThreadsMixin:
creating it failed, so the message falls back to lobby recovery (the last bound topic).
``None``: not applicable (key off, not a topic-mode root DM) or nothing to recover to.
"""
- if (not source.chat_id or not self._telegram_dm_topic_auto_transfer_configured()
+ if (not source.chat_id or not self._telegram_dm_topic_auto_transfer_configured(source)
or not await asyncio.to_thread(self._is_telegram_topic_root_lobby, source)):
return None
adapter = self._delivery_adapter_for(source)
diff --git a/tests/gateway/test_telegram_dm_topic_auto_transfer.py b/tests/gateway/test_telegram_dm_topic_auto_transfer.py
index 8d8f265117b..7786e1c38ca 100644
--- a/tests/gateway/test_telegram_dm_topic_auto_transfer.py
+++ b/tests/gateway/test_telegram_dm_topic_auto_transfer.py
@@ -173,3 +173,69 @@ def test_top_level_telegram_yaml_key_reaches_extra(monkeypatch):
extras = _apply_yaml_config({}, {"dm_topic_auto_transfer": True}) or {}
assert extras.get("dm_topic_auto_transfer") is True
+
+
[email protected]("launch,secondary", [(False, True), (True, False), (False, False), (True, True)])
+async def test_receiving_secondary_bot_controls_opt_in(tmp_path, monkeypatch, launch, secondary):
+ """Multiplexed: the bot that RECEIVED the root DM owns ``dm_topic_auto_transfer``, not the launch bot."""
+ from tests.waiting import async_wait_until
+
+ runner, primary, db, _runs = _rig(tmp_path, monkeypatch, extra={"dm_topic_auto_transfer": launch})
+ runner.config.multiplex_profiles = True
+ runner._primary_profile_name = "default"
+ second = TelegramAdapter(PlatformConfig(enabled=True, token="***", extra={"dm_topic_auto_transfer": secondary}))
+ second._bot = _fake_bot()
+ second.gateway_runner = runner
+ second.set_owner_profile("probe-secondary")
+ runner._profile_adapters = {"probe-secondary": {Platform.TELEGRAM: second}}
+ db.enable_telegram_topic_mode(chat_id="208214988", user_id="208214988", profile_name="probe-secondary")
+ db.create_session(session_id="secondary-old", source="telegram", user_id="208214988",
+ profile_name="probe-secondary")
+ db.bind_telegram_topic(chat_id="208214988", thread_id="333", user_id="208214988",
+ profile_name="probe-secondary", session_key="secondary-old-route",
+ session_id="secondary-old")
+ done = asyncio.Event()
+
+ async def capture(_event):
+ done.set()
+
+ runner._wire_adapter_handlers(second, message_handler=capture, authorization_check=lambda *_a, **_k: True,
+ platform_event_handler=AsyncMock())
+ source = second.build_source(chat_id="208214988", user_id="208214988", chat_type="dm", message_id="4242")
+ event = MessageEvent(text="root prompt", source=source, message_id="4242")
+ second._text_batch_delay_seconds = second._text_batch_split_delay_seconds = 0
+ second._enqueue_text_event(event)
+ await asyncio.wait_for(done.wait(), 10)
+ await async_wait_until(lambda: not second._session_tasks and not second._pending_text_batch_tasks,
+ what="secondary batch settles")
+ db.close()
+ assert second._bot.create_forum_topic.await_count == int(secondary)
+ assert event.source.thread_id == (str(NEW_TOPIC_ID) if secondary else "333")
+ primary._bot.create_forum_topic.assert_not_awaited()
+
+
+async def test_new_topic_reply_anchor_survives_native_codec(tmp_path, monkeypatch):
+ """A durably admitted (snapshot -> restore) transferred event still sends without quoting the root DM."""
+ from gateway.session_envelope import restore_native, snapshot_native
+ from gateway.session_ingress import deliver_response
+ from plugins.platforms.telegram.telegram_topic_transfer import apply_root_dm_topic_transfer
+
+ runner, adapter, db, _runs = _rig(tmp_path, monkeypatch, extra={"dm_topic_auto_transfer": True})
+ runner.config.multiplex_profiles = False
+ runner._is_user_authorized = lambda _source, **_kwargs: True
+ event = MessageEvent(text="root prompt", source=_make_source(), message_id="4242")
+ await apply_root_dm_topic_transfer(adapter, event)
+ assert event.source.thread_id == str(NEW_TOPIC_ID)
+ payload = snapshot_native(runner, event)
+ restored = restore_native(payload, runner)
+ assert restored.message_id == "4242" # inbound identity is kept
+ await deliver_response(adapter, restored, payload["native_text_v1"]["route"], "restored reply")
+ send = adapter._bot.send_message.await_args.kwargs
+ db.close()
+ assert send.get("message_thread_id") == NEW_TOPIC_ID
+ assert send.get("reply_to_message_id") is None
+ # The default (None) override stays out of the envelope, so older retries keep their fingerprint.
+ plain = MessageEvent(text="x", source=_make_source(), message_id="4243")
+ assert "reply_anchor_override" not in snapshot_native(runner, plain)["native_text_v1"]["event"]