fix(telegram): preserve reply and forwarded context - #54262
Conversation
tonydwb
left a comment
There was a problem hiding this comment.
Code Review Summary
Verdict: LGTM
Preserves reply and forwarded context in Telegram messages. The forwarded context detection covers both new (forward_origin) and legacy (forward_from/forward_sender_name) Telegram API fields. Reply truncation is removed so the full replied-to text reaches the agent. Tests verify forwarded text prefixing, caption forwarding, and anonymous forwarding.
Reviewed by Hermes Agent
teknium1
left a comment
There was a problem hiding this comment.
Thanks for preserving an important Telegram context signal. The underlying gaps remain on current main: plugins/platforms/telegram/adapter.py:8434-8491 does not carry forwarded provenance, and gateway/run.py:10599 still truncates reply pointers.
Problems
- The new adapter prefix reaches
gateway/run.py:10608before@-reference expansion. Sinceagent/context_references.py:18-20parses@file:,@folder:,@git:, and@url:, forwarded origin labels must be sanitized or rendered after preprocessing. - Current main deliberately tests the 500-character reply-pointer bound in
tests/gateway/test_reply_to_injection.py:163-182(introduced byd682f320b). The unbounded replacement conflicts with that current contract. - The observed-group branches bypass the new annotation: text at
plugins/platforms/telegram/adapter.py:7524-7527and media at:7773-7784.
Suggested changes
- Add a sanitized, bounded provenance annotation after reference preprocessing, with an
@file:regression test. - Apply it before normal versus observed-group routing diverges, and cover observed text/media.
- Preserve or explicitly redesign the existing reply-size bound.
Automated hermes-sweeper review.
| @@ -6575,6 +6575,7 @@ async def _handle_text_message(self, update: Update, context: ContextTypes.DEFAU | |||
|
|
|||
There was a problem hiding this comment.
This puts forwarded origin text into event.text before GatewayRunner._prepare_inbound_message_text() expands every @ reference (gateway/run.py:10608; agent/context_references.py:18-20). A sender/chat/signature containing @file:... can be interpreted as a context request. Please sanitize provenance and render it after reference preprocessing, with a regression test.
| @@ -9469,7 +9469,7 @@ async def _prepare_inbound_message_text( | |||
| # is referencing. History can contain the same or similar text | |||
There was a problem hiding this comment.
Current main now has tests/gateway/test_reply_to_injection.py:163-182, which explicitly requires the 500-character bound. This unbounded change conflicts with that later current-main contract; retain a bounded pointer or update the behavior and test together with a context-budget rationale.
Summary
Why
Replies and forwards are intent/context pointers. If the gateway truncates replied-to text or passes forwarded content as if it were authored directly by the user, the agent can answer with incomplete or misattributed context.
Tests
python -m compileall -q plugins/platforms/telegram/adapter.py gateway/run.py tests/gateway/test_telegram_reply_quote.py tests/gateway/test_session.pyruff check plugins/platforms/telegram/adapter.py gateway/run.py tests/gateway/test_telegram_reply_quote.py tests/gateway/test_session.pypython -m pytest tests/gateway/test_telegram_reply_quote.py tests/gateway/test_session.py::TestSenderPrefixWithBackfill tests/gateway/test_telegram_documents.py::TestMediaGroups tests/gateway/test_video_context_note.py -q -o 'addopts='git merge-treeagainstupstream/main: clean