Skip to content

fix(telegram): preserve reply and forwarded context - #54262

Open
timsmykov wants to merge 1 commit into
NousResearch:mainfrom
timsmykov:tim/telegram-reply-forward-context-20260628
Open

fix(telegram): preserve reply and forwarded context#54262
timsmykov wants to merge 1 commit into
NousResearch:mainfrom
timsmykov:tim/telegram-reply-forward-context-20260628

Conversation

@timsmykov

Copy link
Copy Markdown

Summary

  • Preserve the full replied-to Telegram text when building the agent-facing reply pointer instead of truncating it to 500 characters.
  • Add an explicit forwarded-message context prefix for Telegram forwarded text and forwarded media captions/stickers.
  • Handle hidden-sender forwards with a generic forwarded marker.

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.py
  • ruff check plugins/platforms/telegram/adapter.py gateway/run.py tests/gateway/test_telegram_reply_quote.py tests/gateway/test_session.py
  • python -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='
  • Local git merge-tree against upstream/main: clean

Copilot AI review requested due to automatic review settings June 28, 2026 13:13

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@alt-glitch alt-glitch added type/feature New feature or request P3 Low — cosmetic, nice to have comp/gateway Gateway runner, session dispatch, delivery platform/telegram Telegram bot adapter sweeper:risk-message-delivery Sweeper risk: may drop, duplicate, misroute, or suppress messages labels Jun 28, 2026
@alt-glitch

Copy link
Copy Markdown
Collaborator

This was generated by AI during triage.

Related: implements the forwarded-message context preservation requested in #43397 (prior attempt #43465 was closed). Fresh implementation, not a duplicate — links the un-truncated reply text + forwarded-context prefix work to the tracking issue.

@tonydwb tonydwb left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 teknium1 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:10608 before @-reference expansion. Since agent/context_references.py:18-20 parses @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 by d682f320b). The unbounded replacement conflicts with that current contract.
  • The observed-group branches bypass the new annotation: text at plugins/platforms/telegram/adapter.py:7524-7527 and 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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread gateway/run.py
@@ -9469,7 +9469,7 @@ async def _prepare_inbound_message_text(
# is referencing. History can contain the same or similar text

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

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 P3 Low — cosmetic, nice to have platform/telegram Telegram bot adapter sweeper:blast-broad Sweeper blast radius: broad — a core path most sessions hit sweeper:risk-compatibility Sweeper risk: may break existing users, config, migrations, defaults, or upgrades sweeper:risk-message-delivery Sweeper risk: may drop, duplicate, misroute, or suppress messages sweeper:risk-security-boundary Sweeper risk: may affect sandboxing, auth, credentials, or sensitive data type/feature New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants