fix(telegram): harden DM topic binding — persist through switch_session, rebind on /new

Follow-up on @EmelyanenkoK's feat: add Telegram DM topic-mode sessions.

Three issues:

1. Split-brain session state. After get_or_create_session() returned a
   SessionEntry for a topic lane, the handler was mutating
   .session_id in place to the binding's target, but never persisting
   the switch through SessionStore. The sessions.json session_key →
   session_id map kept pointing at the lane's natural id; any reader
   that reloaded from disk saw the wrong id. Fixed by routing through
   SessionStore.switch_session(), which _save()s the mapping and ends
   the old session in SQLite like /resume does.

2. /new inside a topic was a one-message no-op. Reset created a new
   session but left the telegram_dm_topic_bindings row pointing at the
   old session_id, so the next message's binding lookup switched right
   back. Now _handle_reset_command rebinds the topic to the new
   session_id after reset.

3. is_telegram_session_linked_to_topic and
   list_unlinked_telegram_sessions_for_user both called
   apply_telegram_topic_migration() on read, contradicting the PR's
   own invariant that migration only runs on explicit /topic opt-in.
   They now tolerate missing topic tables and return empty/False.

Also: _telegram_topic_mode_enabled() now only treats True as enabled
(not any truthy return), so test fixtures with MagicMock session_db
don't accidentally flip every DM into lobby mode — this was breaking
4 pre-existing test_status_command tests.

Tests:
- New regression: /new inside a topic must update the binding row
  (test_new_inside_telegram_topic_rewrites_binding_to_new_session).
- _make_runner now stubs switch_session so existing restore tests
  still exercise the new code path.

Validated end-to-end with real SessionDB + SessionStore:
readers on fresh DB don't create topic tables; enable creates them;
binding override persists across SessionStore restart; /new rebinds
and the new id survives a restart.

Co-authored-by: EmelyanenkoK <emelyanenko.kirill@gmail.com>
This commit is contained in:
teknium1 2026-05-03 05:34:07 -07:00 committed by Teknium
parent 25065283b3
commit a7683d04a9
4 changed files with 184 additions and 44 deletions

View file

@ -1463,15 +1463,17 @@ class GatewayRunner:
if session_db is None:
return False
try:
return bool(
session_db.is_telegram_topic_mode_enabled(
chat_id=str(source.chat_id),
user_id=str(source.user_id),
)
raw = session_db.is_telegram_topic_mode_enabled(
chat_id=str(source.chat_id),
user_id=str(source.user_id),
)
except Exception:
logger.debug("Failed to read Telegram topic mode state", exc_info=True)
return False
# Only honor a real True from the SessionDB. Any other value
# (including MagicMock instances from test fixtures that didn't
# opt into topic mode) means topic mode is off for this chat.
return raw is True
def _is_telegram_topic_root_lobby(self, source: SessionSource) -> bool:
"""True for the main Telegram DM when topic mode has made it a lobby."""
@ -5902,7 +5904,16 @@ class GatewayRunner:
logger.debug("Failed to read Telegram topic binding", exc_info=True)
binding = None
if binding:
session_entry.session_id = str(binding.get("session_id") or session_entry.session_id)
bound_session_id = str(binding.get("session_id") or "")
if bound_session_id and bound_session_id != session_entry.session_id:
# Route the override through SessionStore so the session_key
# → session_id mapping is persisted to disk and the previous
# lane session is ended cleanly. Mutating session_entry in
# place here created a split-brain state where the JSON
# index pointed at one id but code downstream used another.
switched = self.session_store.switch_session(session_key, bound_session_id)
if switched is not None:
session_entry = switched
else:
try:
self._record_telegram_topic_binding(source, session_entry)
@ -7123,6 +7134,17 @@ class GatewayRunner:
_title_note = "\n⚠️ Title is empty after cleanup — session started untitled."
header = header + _title_note
# When /new runs inside a Telegram DM topic lane, rewrite the
# (chat_id, thread_id) → session_id binding so the next message
# uses the freshly-created session. Without this, the binding
# still points at the old session and the binding-lookup at the
# top of _handle_message_with_agent would switch right back.
if self._is_telegram_topic_lane(source) and new_entry is not None:
try:
self._record_telegram_topic_binding(source, new_entry)
except Exception:
logger.debug("Failed to rebind Telegram topic after /new", exc_info=True)
# Fire plugin on_session_reset hook (new session guaranteed to exist)
try:
from hermes_cli.plugins import invoke_hook as _invoke_hook