fix(gateway): don't hijack brand-new Telegram DM topics into the previous topic#31088
Closed
dillweed wants to merge 1 commit into
Closed
fix(gateway): don't hijack brand-new Telegram DM topics into the previous topic#31088dillweed wants to merge 1 commit into
dillweed wants to merge 1 commit into
Conversation
…ious topic _recover_telegram_topic_thread_id rewrote the inbound thread_id of every brand-new topic to the user's most-recently-bound topic, hijacking the new conversation into the previous lane. The hijack is self-reinforcing: because the rewrite happens before _record_telegram_topic_binding, the new topic's binding row is never written, so the next inbound also looks 'unknown' and is hijacked again. A freshly-created topic never recovers on its own. The original commit (ede47a5) intended the 'unknown topic -> snap to most-recent' arm to catch cross-topic Reply leaks, but those are far rarer than legitimate new topics and self-correct on the next message in the right topic. Drop that arm; trust any explicit, non-lobby thread_id as-is. Lobby/missing-thread recovery (the stripped-plain-reply case from NousResearch#3206 / non-topic-mode users) is preserved. Updates the previously-passing test that encoded the buggy behaviour: test_recover_rewrites_unknown_thread_id_to_most_recent is renamed and inverted to test_recover_leaves_unknown_explicit_thread_id_alone. Fixes NousResearch#31086
Collaborator
This was referenced May 23, 2026
Contributor
|
Superseded by #31444 (#31444). You filed the canonical issue (#31086) with the root-cause analysis, the proposed patch, the local-repair SQL, and live verification — all of which became the basis for the salvage PR. You're credited as a co-author on the regression test commit. Thanks for the exemplary bug report. Closes #31086. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #31086.
_recover_telegram_topic_thread_idingateway/run.pyrewrote the inboundthread_idof every brand-new Telegram DM topic to the user's most-recently-bound topic, hijacking the new conversation into the previous lane. The hijack was self-reinforcing — because the rewrite happens before_record_telegram_topic_binding, the new topic's binding row was never written, so the next inbound also looked "unknown" and was hijacked again. A freshly-created topic could never recover on its own.The function was introduced in
ede47a54b("fix(gateway): pin Telegram DM-topic routing to user's current topic") to handle two real Telegram quirks:thread_id—_build_message_eventstrips the thread_id on plain replies (needed for non-topic users, see [Bug]: Telegram DM sends fail with 'Message thread not found' — spurious thread_id from reply chains #3206).message_thread_idwhen the user long-press-replies onto a message in a different topic.The fix for (1) is correct and worth preserving. The fix for (2) (the "unknown topic → snap to most-recent" arm) cannot tell a Reply-leak apart from a brand-new topic — both look like an explicit, non-lobby thread_id that isn't yet in
telegram_dm_topic_bindings. Since Reply leaks are rare and self-correct on the next message in the right topic, while new topics are common and trap permanently, the trade-off clearly favors trusting any explicit non-lobbythread_id.Change
Test
The previously-passing test
test_recover_rewrites_unknown_thread_id_to_most_recentencoded the buggy behaviour. It's been renamed and inverted totest_recover_leaves_unknown_explicit_thread_id_alone, which captures the regression directly.Verification on a live install
I patched a live gateway with this change, repaired the SQLite binding row for one previously-trapped topic, and ran two manual tests:
telegram topic recoverylog line; reply landed in the correct topic.thread_id.Before the fix, the gateway log showed every message in two separate fresh topics being redirected to a previously-active topic for hours:
After the fix:
Related
Sibling bug in the same subsystem: #20470 (post-split binding refresh). This PR does not address that; they are independent.