Skip to content

Preserve forwarded message provenance - #43477

Open
tymrtn wants to merge 4 commits into
NousResearch:mainfrom
tymrtn:fix/forwarded-message-context
Open

Preserve forwarded message provenance#43477
tymrtn wants to merge 4 commits into
NousResearch:mainfrom
tymrtn:fix/forwarded-message-context

Conversation

@tymrtn

@tymrtn tymrtn commented Jun 10, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #43397.

This preserves forwarded-message provenance in gateway inbound events instead of treating forwarded content as ordinary pasted text.

  • Adds a platform-neutral MessageContextRef model on MessageEvent.
  • Extracts Telegram forward_origin / is_automatic_forward metadata into context refs.
  • Prepends a compact provenance block before the user message body.
  • Renders provenance after @ context-reference preprocessing so untrusted forwarded metadata cannot trigger @file / @url expansion.
  • Sanitizes forwarded names/titles/signatures/quotes into bounded single-line fragments to avoid prompt-line spoofing.
  • Preserves context refs through Telegram text/photo/album batching plus shared pending-message and busy-text debounce merge paths.

Tests

Focused local verification with dev dependencies via uv run --extra dev:

uv run --extra dev python -m pytest \
  tests/gateway/test_forward_context_rendering.py \
  tests/gateway/test_telegram_forward_context.py \
  tests/gateway/test_telegram_text_batching.py \
  tests/gateway/test_active_session_text_merge.py \
  tests/gateway/test_session_race_guard.py \
  -q -o addopts=''

Result: 60 passed in 9.94s in the feature worktree; 60 passed in 9.71s after cherry-picking into Tyler's tyler/live branch.

Busy-session preservation smoke after local live cherry-pick:

uv run --extra dev python -m pytest \
  tests/gateway/test_busy_session_ack.py \
  tests/gateway/test_busy_session_buttons.py \
  tests/gateway/test_busy_session_auth_bypass.py \
  tests/gateway/test_busy_session_runner.py \
  tests/gateway/test_telegram_busy_controls.py \
  tests/gateway/test_running_agent_session_toggles.py \
  tests/cli/test_busy_input_mode_command.py \
  tests/cli/test_cli_steer_busy_path.py \
  -q -o addopts=''

Result: 90 passed in 1.77s.

Review

Codex review was run against the feature diff. It initially found merge-path and metadata sanitization gaps; those were fixed and covered by tests. Final Codex review result: APPROVED.

Notes

This PR intentionally avoids exposing Telegram handles as literal @handle strings inside synthetic provenance headers, because those headers share the same prompt surface as user-authored text.

Independent review

Codex CLI (gpt-5.6-sol, ultra reasoning) reviewed follow-up commit 503aca350 and reported no blocking findings: “The change correctly preserves forwarded-message context while avoiding list aliasing. Relevant queue, rendering, batching, and session tests passed.”

@alt-glitch alt-glitch added type/feature New feature or request comp/gateway Gateway runner, session dispatch, delivery platform/telegram Telegram bot adapter P3 Low — cosmetic, nice to have labels Jun 10, 2026
@austinpickett

austinpickett commented Jun 10, 2026

Copy link
Copy Markdown
Collaborator

Reviewed by Hermes Agent — deferred to team for product sign-off. Feature PRs (type/feature) need a product/design decision on whether this capability is wanted before merge review proceeds. Please await team feedback.

Hermes Agent triage

@tymrtn

tymrtn commented Jun 10, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @austinpickett. Best way to get a maintainer on these?

@teknium1

Copy link
Copy Markdown
Contributor

Thanks for the comprehensive forwarding-context implementation. The premise is confirmed on current main: Telegram event construction does not extract forward_origin (plugins/platforms/telegram/adapter.py:8479), and inbound preparation has no provenance renderer (gateway/run.py:10592-10697). The sanitization and post-@-preprocessing placement are appropriate hardening over closed PR #43465.

Problems

  • gateway/run.py:9301-9318 manually builds the next-turn event for /queue, but it copies no context_refs. A forwarded /queue ... command therefore drops the new provenance before the queued turn. This is a shared queued-message path outside the merge helpers covered by this PR.

Suggested changes

  • Copy context_refs into queued_event at gateway/run.py:9301 and add a focused regression test covering forwarded /queue input.

Automated hermes-sweeper review.

@teknium1 teknium1 added sweeper:risk-session-state Sweeper risk: may lose/corrupt/mis-associate session or context state 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 sweeper:blast-moderate Sweeper blast radius: moderate — a subsystem or single platform labels Jul 14, 2026
…tion

The /queue handler rebuilds the inbound MessageEvent field-by-field but
omitted context_refs, so forwarding a message via /queue dropped its
[Forwarded message] origin block when the queued turn ran. Copy
context_refs into the reconstructed event (fresh list to avoid aliasing
the source event's list), matching the media/reply payload handling
already present. Add a focused runtime regression test driving the real
/queue handler and asserting the refs survive.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@tymrtn

tymrtn commented Jul 15, 2026

Copy link
Copy Markdown
Contributor Author

Addressed the /queue provenance gap in 503aca350. The reconstructed queued event now copies context_refs into a fresh list, and a focused runtime regression test proves forwarded provenance survives the real /queue path without aliasing.

Verification:

  • scripts/run_tests.sh tests/gateway/test_queue_command.py tests/gateway/test_queue_consumption.py tests/gateway/test_telegram_forward_context.py tests/gateway/test_forward_context_rendering.py -q — 35 passed
  • Codex independent review — no blocking findings

Thank you for catching the secondary reconstruction path.

@alt-glitch alt-glitch removed the sweeper:risk-security-boundary Sweeper risk: may affect sandboxing, auth, credentials, or sensitive data label Jul 15, 2026
@tymrtn

tymrtn commented Jul 28, 2026

Copy link
Copy Markdown
Contributor Author

@teknium1 The sweeper-requested /queue provenance gap is addressed in 503aca350; the focused gateway slice passed (35 tests) and independent Codex review found no blockers. Cross-PR triage now identifies #43477 as the best consolidation target, pending product/design sign-off. Could you or the appropriate maintainer give it a product/merge look when bandwidth allows?

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-moderate Sweeper blast radius: moderate — a subsystem or single platform sweeper:risk-message-delivery Sweeper risk: may drop, duplicate, misroute, or suppress messages sweeper:risk-session-state Sweeper risk: may lose/corrupt/mis-associate session or context state type/feature New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Gateway should preserve forwarded-message metadata for Telegram and other platforms

4 participants