The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): pass routed source.profile into telegram topic state
Celio Monteiro62be7043ffbf
diff --git a/gateway/run.py b/gateway/run.pyindex 73d2d0f5c8e..652c9043410 100644--- a/gateway/run.py+++ b/gateway/run.py@@ -8509,6 +8509,18 @@ class GatewayRunner(GatewayAuthorizationMixin, GatewayKanbanWatchersMixin, Gatewprofile=_profile,)+ @staticmethod+ def _telegram_topic_profile_name(source: SessionSource) -> str:+ """Profile namespace for Telegram topic-mode rows (issue #76423).++ Prefer the profile already stamped on the routed event+ (``source.profile``). Do **not** fall back to the process-global+ active profile here — under multiplex that can mis-attribute+ topic state across bots sharing one ``state.db``.+ """+ name = str(getattr(source, "profile", None) or "").strip()+ return name if name else "default"+def _telegram_topic_mode_enabled(self, source: SessionSource) -> bool:"""Return whether Telegram DM topic mode is active for this chat."""if source.platform != Platform.TELEGRAM or source.chat_type != "dm":@@ -8522,6 +8534,7 @@ class GatewayRunner(GatewayAuthorizationMixin, GatewayKanbanWatchersMixin, Gatewraw = session_db.is_telegram_topic_mode_enabled(chat_id=str(source.chat_id),user_id=str(source.user_id),+ profile_name=self._telegram_topic_profile_name(source),)except Exception:logger.debug("Failed to read Telegram topic mode state", exc_info=True)@@ -8623,6 +8636,7 @@ class GatewayRunner(GatewayAuthorizationMixin, GatewayKanbanWatchersMixin, Gatewuser_id=str(source.user_id or ""),session_key=session_entry.session_key,session_id=session_entry.session_id,+ profile_name=self._telegram_topic_profile_name(source),)def _sync_telegram_topic_binding(@@ -8690,6 +8704,7 @@ class GatewayRunner(GatewayAuthorizationMixin, GatewayKanbanWatchersMixin, Gatewtry:bindings = session_db.list_telegram_topic_bindings_for_chat(chat_id=str(source.chat_id),+ profile_name=self._telegram_topic_profile_name(source),)except Exception:logger.debug("topic-recover: read failed", exc_info=True)@@ -17095,6 +17110,9 @@ class GatewayRunner(GatewayAuthorizationMixin, GatewayKanbanWatchersMixin, Gatewif isinstance(text_modes, dict)else self._busy_text_mode)+ # Secondary adapters always carry the profile they serve so prune+ # paths namespace topic bindings correctly under multiplex (#76423).+ adapter._hermes_profile_name = profile_nameasync def _run_secondary_profile_reconnect(self, profile_name: str, platform: Platform@@ -20819,6 +20837,7 @@ class GatewayRunner(GatewayAuthorizationMixin, GatewayKanbanWatchersMixin, Gatewbinding = (await self._session_db.get_telegram_topic_binding(chat_id=str(source.chat_id),thread_id=str(source.thread_id),+ profile_name=self._telegram_topic_profile_name(source),)) if self._session_db else Noneexcept Exception:logger.debug("Failed to read Telegram topic binding", exc_info=True)@@ -25593,6 +25612,7 @@ class GatewayRunner(GatewayAuthorizationMixin, GatewayKanbanWatchersMixin, Gatewbinding = await session_db.get_telegram_topic_binding(chat_id=str(source.chat_id),thread_id=str(source.thread_id),+ profile_name=self._telegram_topic_profile_name(source),)if binding and str(binding.get("session_id") or "") != str(session_id):return@@ -25750,13 +25770,17 @@ class GatewayRunner(GatewayAuthorizationMixin, GatewayKanbanWatchersMixin, Gatewcurrently_enabled = await self._session_db.is_telegram_topic_mode_enabled(chat_id=chat_id,user_id=str(source.user_id or ""),+ profile_name=self._telegram_topic_profile_name(source),)except Exception:currently_enabled = Falseif not currently_enabled:return "Multi-session topic mode is not currently enabled for this chat."try:- await self._session_db.disable_telegram_topic_mode(chat_id=chat_id)+ await self._session_db.disable_telegram_topic_mode(+ chat_id=chat_id,+ profile_name=self._telegram_topic_profile_name(source),+ )except Exception as exc:logger.exception("Failed to disable Telegram topic mode")return f"Failed to disable topic mode: {exc}"@@ -25787,6 +25811,7 @@ class GatewayRunner(GatewayAuthorizationMixin, GatewayKanbanWatchersMixin, Gatewsessions = await self._session_db.list_unlinked_telegram_sessions_for_user(chat_id=str(source.chat_id),user_id=str(source.user_id),+ profile_name=self._telegram_topic_profile_name(source),limit=10,)except Exception:@@ -25836,9 +25861,11 @@ class GatewayRunner(GatewayAuthorizationMixin, GatewayKanbanWatchersMixin, Gatewreturn "That session does not belong to this Telegram user."linked = await self._session_db.is_telegram_session_linked_to_topic(session_id=session_id)+ topic_profile = self._telegram_topic_profile_name(source)current_binding = await self._session_db.get_telegram_topic_binding(chat_id=str(source.chat_id),thread_id=str(source.thread_id),+ profile_name=topic_profile,)if linked:if not current_binding or current_binding.get("session_id") != session_id:@@ -25853,6 +25880,7 @@ class GatewayRunner(GatewayAuthorizationMixin, GatewayKanbanWatchersMixin, Gatewsession_key=session_key,session_id=session_id,managed_mode="restored",+ profile_name=topic_profile,)except ValueError as exc:if "already linked" in str(exc):diff --git a/gateway/slash_commands.py b/gateway/slash_commands.pyindex d44eb29b475..2e7dcbaa0ed 100644--- a/gateway/slash_commands.py+++ b/gateway/slash_commands.py@@ -4977,6 +4977,7 @@ class GatewaySlashCommandsMixin:await self._session_db.enable_telegram_topic_mode(chat_id=str(source.chat_id),user_id=str(source.user_id),+ profile_name=self._telegram_topic_profile_name(source),has_topics_enabled=capabilities.get("has_topics_enabled"),allows_users_to_create_topics=capabilities.get("allows_users_to_create_topics"),)@@ -4992,6 +4993,7 @@ class GatewaySlashCommandsMixin:binding = await self._session_db.get_telegram_topic_binding(chat_id=str(source.chat_id),thread_id=str(source.thread_id),+ profile_name=self._telegram_topic_profile_name(source),)except Exception:logger.debug("Failed to read Telegram topic binding", exc_info=True)diff --git a/plugins/platforms/telegram/adapter.py b/plugins/platforms/telegram/adapter.pyindex b8ed8d11f0d..c81a9423684 100644--- a/plugins/platforms/telegram/adapter.py+++ b/plugins/platforms/telegram/adapter.py@@ -1709,8 +1709,13 @@ class TelegramAdapter(BasePlatformAdapter):if db is None or not hasattr(db, "delete_telegram_topic_binding"):returntry:+ # Prefer the profile stamped on this adapter under multiplex+ # (issue #76423). Fall back to "default" for single-profile bots.+ profile_name = getattr(self, "_hermes_profile_name", None) or "default"removed = db.delete_telegram_topic_binding(- chat_id=str(chat_id), thread_id=str(thread_id),+ chat_id=str(chat_id),+ thread_id=str(thread_id),+ profile_name=profile_name,)except Exception:logger.debug(diff --git a/tests/gateway/test_telegram_topic_profile_routing_76423.py b/tests/gateway/test_telegram_topic_profile_routing_76423.pynew file mode 100644index 00000000000..3ce892cea94--- /dev/null+++ b/tests/gateway/test_telegram_topic_profile_routing_76423.py@@ -0,0 +1,54 @@+"""Issue #76423 — Gateway routes source.profile into telegram topic state."""++from __future__ import annotations++from pathlib import Path+from types import SimpleNamespace++from hermes_state import SessionDB+from gateway.config import Platform+from gateway.session import SessionSource+++CHAT = "208214988"+++def _source(profile=None, thread_id="42"):+ return SessionSource(+ platform=Platform.TELEGRAM,+ user_id=CHAT,+ chat_id=CHAT,+ user_name="tester",+ chat_type="dm",+ thread_id=thread_id,+ profile=profile,+ )+++def test_gateway_uses_source_profile_not_global(tmp_path: Path):+ from gateway.run import GatewayRunner++ assert GatewayRunner._telegram_topic_profile_name(_source("coder")) == "coder"+ assert GatewayRunner._telegram_topic_profile_name(_source(None)) == "default"++ db = SessionDB(db_path=tmp_path / "state.db")+ db.create_session(session_id="sess-coder", source="telegram", user_id=CHAT, profile_name="coder")+ db.enable_telegram_topic_mode(chat_id=CHAT, user_id=CHAT, profile_name="coder")++ runner = object.__new__(GatewayRunner)+ runner._session_db = db+ assert runner._telegram_topic_mode_enabled(_source("coder")) is True+ assert runner._telegram_topic_mode_enabled(_source("other")) is False+ assert runner._telegram_topic_mode_enabled(_source(None)) is False++ runner._record_telegram_topic_binding(+ _source("coder", "42"),+ SimpleNamespace(session_key="k", session_id="sess-coder"),+ )+ assert db.get_telegram_topic_binding(+ chat_id=CHAT, thread_id="42", profile_name="coder",+ ) is not None+ assert db.get_telegram_topic_binding(+ chat_id=CHAT, thread_id="42", profile_name="default",+ ) is None+ db.close()