Skip to content

fix: refresh Telegram DM topic binding after session split (#20470)#20485

Closed
vominh1919 wants to merge 1 commit into
NousResearch:mainfrom
vominh1919:fix/telegram-topic-binding-after-split
Closed

fix: refresh Telegram DM topic binding after session split (#20470)#20485
vominh1919 wants to merge 1 commit into
NousResearch:mainfrom
vominh1919:fix/telegram-topic-binding-after-split

Conversation

@vominh1919

Copy link
Copy Markdown
Contributor

Fixes #20470

Problem

When a session bound to a Telegram DM topic splits during mid-turn context compression, the gateway updates session_store._entries with the new session ID but does not update the corresponding row in telegram_dm_topic_bindings. On the next inbound message in that topic, the gateway reads the stale binding, calls switch_session() back to the pre-compression root session, reloads its full transcript, fires preflight compression, splits again — repeat forever.

Symptom: every message in a Telegram DM topic prints 📦 Preflight compression: ~N tokens >= threshold even after the conversation has been compressed multiple times. Each turn pays the full compression latency before any real work begins.

Fix

After session_store._save() updates the in-memory entry and persists it, call _record_telegram_topic_binding(source, entry) to also refresh the SQLite topic-binding row. The call is wrapped in try/except to guard against edge cases where bind_telegram_topic might raise (e.g. if the new session ID is already linked to a different topic).

_record_telegram_topic_binding already early-returns when source.thread_id is falsy, so this is safe across all platform surfaces — only Telegram DM topic bindings are affected.

Before vs After

Before After
Topic binding after split Points to pre-compression root session Points to post-split child session
Preflight compression on next message Triggers every time (infinite loop) Skipped (child session is small)
Compression latency per turn Full compression cost Zero (no re-compression needed)

…rch#20470)

When context compression splits a session, the gateway updates
session_store._entries but does not update the corresponding row
in telegram_dm_topic_bindings. The next inbound message reads the
stale binding, switches back to the pre-compression root session,
and re-triggers preflight compression in an infinite loop.

Fix by calling _record_telegram_topic_binding() after updating the
session store entry, guarded by try/except to handle edge cases.
@alt-glitch alt-glitch added type/bug Something isn't working P1 High — major feature broken, no workaround comp/gateway Gateway runner, session dispatch, delivery platform/telegram Telegram bot adapter labels May 6, 2026
@alt-glitch

Copy link
Copy Markdown
Collaborator

Competing fix for #20470 — see also #20486 which extracts the logic into a _sync_session_split_entry helper method and adds a dedicated regression test.

lucvan added a commit to lucvan/hermes-agent that referenced this pull request Jun 9, 2026
…sion split

Cherry-pick of upstream PR NousResearch#20485 (still open as of this commit).

When mid-run compression splits a session, the gateway updates
session_store with the new child session_id but does not update the
Telegram DM topic_bindings row in SQLite.  On the next inbound message
in that topic, the binding lookup at _handle_message_with_agent
resolves to the stale pre-compression root, reloads the full
transcript, and re-triggers preflight compression.  Loop.

Local log/DB evidence confirmed the pattern:
 - Session split detected: 20260512_140504_a4dc00f9 -> ... (repeated)
 - telegram_dm_topic_bindings.thread_id=1613 still pointed at the
   pre-compression root session even after the child was created.

Fix: after entry.session_id is rewritten on split, also call
_record_telegram_topic_binding(source, entry) so the binding tracks
the new child.  Exception-safe -- failure logs but does not abort the
turn.

Upstream issue: NousResearch#20470
Upstream PR:    NousResearch#20485

When the upstream PR merges, this commit will drop cleanly on the next
`git rebase upstream/main`.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
@teknium1

Copy link
Copy Markdown
Contributor

This looks implemented on current main by the later merged Telegram topic-binding fix. This is an automated hermes-sweeper review.

Evidence:

  • gateway/run.py:2586 defines _sync_telegram_topic_binding(), which documents the stale Telegram topic binding / repeated preflight compression loop and safely refreshes the binding through _record_telegram_topic_binding().
  • gateway/run.py:8491 updates session_entry.session_id, calls session_store._save(), then calls _sync_telegram_topic_binding(source, session_entry, reason="agent-result-compression"), matching this PR's requested behavior.
  • gateway/run.py:7781 also adds read-path self-healing via SessionDB.get_compression_tip(), so existing stale bindings are advanced and rewritten on the next inbound message.
  • tests/gateway/test_telegram_topic_mode.py:451 covers the stale parent-binding regression and asserts the binding row is rewritten to the compression child.
  • The implementing commit is db96fc60d0d3dc3f9e95dc6541d8edcccb2f2171 (fix(gateway): keep Telegram topic bindings aligned with compression children (#34409)), contained in tag v2026.6.5.

Thanks for the original fix. The final mainline change covers the same #20470 failure mode and adds the self-heal path for already-stale rows, so this PR can be closed as implemented on main.

@teknium1 teknium1 closed this Jun 11, 2026
@teknium1 teknium1 added the sweeper:implemented-on-main Sweeper: behavior already present on current main label Jun 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/gateway Gateway runner, session dispatch, delivery P1 High — major feature broken, no workaround platform/telegram Telegram bot adapter sweeper:implemented-on-main Sweeper: behavior already present on current main type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Telegram DM topic binding not refreshed after compression-induced session split — causes preflight compression loop

3 participants