Skip to content

fix(gateway): preserve media dedup after streamed replies - #72321

Closed
TheEpTic wants to merge 1 commit into
NousResearch:mainfrom
TheEpTic:fix/history-media-paths-nameerror
Closed

TheEpTic wants to merge 1 commit into
NousResearch:mainfrom
TheEpTic:fix/history-media-paths-nameerror

Conversation

@TheEpTic

@TheEpTic TheEpTic commented Jul 27, 2026 •

Copy link
Copy Markdown

what does this PR do?

Fixes a gateway crash in the post-stream delivery path. When a streamed agent result has already_sent=True and a non-empty final response, _handle_message_with_agent passed _history_media_paths, which is only bound inside _run_agent_inner's nested run_sync function. The outer handler therefore raises NameError after the model request succeeds, turning an otherwise-complete reply into a generic platform error.

This happens for ordinary streamed text replies as well as responses containing MEDIA: directives, because the post-stream attachment-delivery helper is invoked for any non-empty response.

The fix derives the existing history-media dedup set from the handler's loaded transcript. This resolves the crash without disabling cross-turn MEDIA deduplication.

This is deliberately limited to the gateway fix and its regression coverage. The related open #72228 also modifies unrelated desktop performance/debug files; its fallback-to-empty-set guard avoids the exception but drops the existing dedup context.

related issue

No filed issue. Related PR: #72228.

type of change

  • 🐛 Bug fix (non-breaking change that fixes an issue)
  • ✅ Tests (adding or improving test coverage)

changes made

  • gateway/run.py: derive prior delivered MEDIA paths from the transcript before post-stream delivery.
  • tests/gateway/test_42039_duplicate_user_message.py: exercise the real already_sent branch with an ordinary text reply and assert prior MEDIA paths reach the delivery helper.

how to test

  1. On unpatched main, drive _handle_message_with_agent with already_sent=True, any non-empty response, and a platform adapter. It raises NameError: name '_history_media_paths' is not defined after the streamed response completes.
  2. Apply this patch and repeat. The post-stream helper receives the prior assistant MEDIA path and the reply completes normally.
  3. Run:
    scripts/run_tests.sh tests/gateway/test_42039_duplicate_user_message.py tests/gateway/test_post_stream_media_delivery.py tests/gateway/test_tts_media_routing.py
    Result: 17 passed.

checklist

  • I've read the Contributing Guide.
  • My commit message follows Conventional Commits.
  • I searched existing PRs; fix(gateway): handle undefined _history_media_paths in auto-resume path #72228 is the only exact open overlap and this PR does not duplicate its unrelated desktop changes.
  • My PR contains only changes related to this bug fix.
  • I've run pytest tests/ -q and all tests pass. Full hermetic scripts/run_tests.sh is running separately; the targeted canonical suite above passed.
  • I've added a regression test for this bug fix.
  • I've tested on Linux (Arch).
  • Documentation/config/tool-schema changes: N/A.
  • Cross-platform impact considered: pure in-memory history parsing, no platform-specific I/O or process behavior.

@alt-glitch alt-glitch added type/bug Something isn't working P2 Medium — degraded but workaround exists comp/gateway Gateway runner, session dispatch, delivery sweeper:risk-message-delivery Sweeper risk: may drop, duplicate, misroute, or suppress messages labels Jul 27, 2026
@Tornado-Truxton

Copy link
Copy Markdown

Independent production confirmation from a Fedora 44 Telegram gateway:

  • Current upstream main raised NameError: name _history_media_paths is not defined from _handle_message_with_agent on the already-streamed MEDIA-delivery path, yielding Telegram’s generic unexpected-error reply.
  • We applied the same pre-turn transcript approach locally: _collect_history_media_paths(history) immediately after loading the handler history, then restarted the gateway.
  • A fresh Telegram session subsequently completed a status request normally, and the gateway has logged no further NameError / agent-handler errors.

This preserves prior-turn MEDIA deduplication, so it is preferable to an empty-set fallback. Thanks for including real branch coverage here.

@hanzckernel

Copy link
Copy Markdown
Contributor

Independent macOS/Telegram confirmation: an ordinary text-only streamed reply completed successfully with finish_reason=stop and was fully delivered, after which _handle_message_with_agent raised NameError: name '_history_media_paths' is not defined and the gateway emitted its generic unexpected-error message.

No attachment directive was required to trigger the failure, so this affects ordinary streamed text replies as well as MEDIA-bearing replies. The model/provider request had already succeeded. Latest main still contains the undefined outer-scope reference.

#72321’s history-derived fix preserves the existing cross-turn deduplication contract and is preferable to an empty-set fallback.

@teknium1

Copy link
Copy Markdown
Collaborator

Merged via PR #72341 — your commit was cherry-picked onto current main with your authorship preserved in git log (c2ee503). Thanks for the clean fix and the real branch coverage!

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 P2 Medium — degraded but workaround exists sweeper:risk-message-delivery Sweeper risk: may drop, duplicate, misroute, or suppress messages type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants