ductor icon indicating copy to clipboard operation
ductor copied to clipboard

[codex] fix Telegram reply context mixing

Open attid opened this issue 7 months ago • 2 comments

Summary

This fixes Telegram reply handling so that when a user replies to a message and addresses the bot, the bot receives both the replied-to text and the user message.

Root Cause

Telegram reply metadata was only used to decide whether a message was addressed to the bot. The replied-to message content was never merged into the prompt sent to the orchestrator, so the model only saw the new message text.

Changes

  • add a small Telegram helper that merges reply context into the prompt
  • update _resolve_text() to include reply_to_message.text or reply_to_message.caption
  • add a regression test covering reply plus mention in group chat

Impact

Telegram users can now reply to an existing message and mention the bot without losing the original message context in the prompt.

Validation

  • uv run pytest tests/messenger/telegram/test_app.py tests/messenger/telegram/test_handlers.py -q

attid avatar May 03 '26 11:05 attid

I reviewed this draft and left it unmerged because it is still marked as draft. I did not find malicious-code indicators in the small Telegram reply-context diff, but I want it out of draft and rebased against v0.17.0 before a merge review. Thanks for the focused fix.

PleasePrompto avatar May 17 '26 08:05 PleasePrompto

Thanks — the problem is real (the replied-to message is currently lost) and still unsolved on main, so this is welcome. Three things before un-drafting:

  1. Origin check (functional defect): the most common reply in a DM is replying to the bot's own previous answer. As written, that answer gets re-injected as [REPLY TO] context even though the CLI session already has that turn in its history — redundant and confusing for the model. Please only inject context when reply.from_user.id != bot_id; the repo already has this exact pattern in messenger/telegram/media.py (is_message_addressed), so it can be reused instead of reinvented.
  2. Tests: currently only the happy path is covered. Please add: reply-to-bot (no injection), empty reply.text with caption fallback, media reply without caption.
  3. Untrusted quoted text: in groups the quoted message is third-party content and goes into the prompt unescaped — a crafted quote can fake the [USER MESSAGE] marker structure. Worth stripping @botname from the quoted text and noting the injection surface in the PR description.

Small naming nit: "context mixing" suggests messages got cross-wired; "include replied-to message context" describes the change better. 👍

PleasePrompto avatar Jun 12 '26 05:06 PleasePrompto