Skip to content

fix(gateway): resolve NameError for _history_media_paths - #72373

Closed
frannnnk wants to merge 1 commit into
NousResearch:mainfrom
frannnnk:fix/gateway-media-nameerror-dedupe
Closed

frannnnk wants to merge 1 commit into
NousResearch:mainfrom
frannnnk:fix/gateway-media-nameerror-dedupe

Conversation

@frannnnk

Copy link
Copy Markdown

Summary

This PR fixes a NameError in the gateway's message handler.

The Problem

In gateway/run.py, the variable _history_media_paths was being passed to _deliver_media_from_response but was never defined within the _handle_message_with_agent function. This caused the gateway to crash with a NameError whenever the agent attempted to deliver media (images/audio) via the streaming delivery path or when processing messages with MEDIA: tags.

The Fix

The _history_media_paths variable is now correctly initialized by calling _collect_history_media_paths(history) after loading the transcript, matching the pattern used in other parts of the gateway.

Verification

  • Verified the fix locally: the NameError no longer occurs when sending media.
  • Verified that media deduplication (the purpose of this variable) functions correctly with this initialization.

Initialize _history_media_paths in _handle_message_with_agent to prevent
NameError when delivering media from response (e.g. via streaming fallback
or manual MEDIA: tags).

This variable was being referenced in _deliver_media_from_response call
but was not defined in the function scope.
@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 duplicate This issue or pull request already exists labels Jul 27, 2026
@alt-glitch

Copy link
Copy Markdown
Contributor

This was generated by AI during triage.

Duplicate of #72326: both initialize _history_media_paths for the same post-stream media-delivery NameError. #72326 also includes focused regression coverage.

@monerostar monerostar 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.

Win11 native verification (monerostar)

Environment

  • Windows 11 Build 26200 · native (not WSL agent runtime)
  • Hermes Agent v0.19.0 (2026.7.20) · install git 0fa5e41c at %LOCALAPPDATA%\hermes\hermes-agent
  • Git Bash / MSYS · ripgrep 15.1.0
  • PR tip checked out: 2d362c1ec (pr-72373 worktree)

Symptom on this host (production Telegram DM)
After a successful streamed reply (already_sent), gateway finalization crashes and Telegram shows:

Sorry, I encountered an unexpected error. Try again or use /reset…

Log signature (tech gateway handling agent:main:telegram:dm:…):

ERROR gateway.run: Agent error in session agent:main:telegram:dm:…
NameError: name '_history_media_paths' is not defined
  File gateway/run.py, in _handle_message_with_agent
    history_media_paths=_history_media_paths,

Count tonight on this box: ≥8 identical NameError lines in errors.log / gateway.log (2026-07-26 evening), including turns with document + image attachments where the body already streamed correctly.

Control (unpatched install)

Static scope check of _handle_message_with_agent on install tree:

Assigns of _history_media_paths in fn 0
Uses line ~14612 (history_media_paths=_history_media_paths under already_sent and not failed)
Prior assign before use none → NameError risk

The other gateway path (~line 22033) already does _history_media_paths = _collect_history_media_paths(agent_history) — only the streaming/already_sent handoff in _handle_message_with_agent was missing the bind.

PR #72373

Diff +1 line after load_transcript
Assign _history_media_paths = _collect_history_media_paths(history) @ ~13211
Use under already_sent ~14604 — prior assign present → OK

Helper smoke (PR tree on PATH):

_collect_history_media_paths([]) → set()
_collect_history_media_paths([{"role":"assistant","content":"hi MEDIA:C:/tmp/x.png"}]) → {'C:/tmp/x.png'}

Sibling PRs

Several closed attempts on the same NameError (#72317, #72326, #72344, #72321/#72341, etc.). This open PR is the right collapse target — minimal, correct site, matches the live failure mode.

Formal Approve

External collaborator on NousResearch/hermes-agentcomment review only (cannot Approve). From a native Win11 daily-driver Telegram host: this fix matches the bug we hit; LGTM on root cause + call-site binding.

Optional follow-up (not blocking): a tiny unit/static test that _handle_message_with_agent source assigns _history_media_paths before the already_sent deliver block, so this class cannot regress silently in run.py.

@monerostar

Copy link
Copy Markdown
Contributor

Follow-up ops context (not a competing PR)

The post-stream _history_media_paths NameError (#72373) is separate from silent dead Telegram commands after multi-profile gateway restarts.

Ops write-up for single getUpdates owner / stale state / in-gateway restart footgun on Win11 multi-profile: #72664.

Local install still carries the one-liner bind until this PR merges; no second PR from monerostar.

@teknium1

Copy link
Copy Markdown
Collaborator

Thanks for identifying the undefined media-history binding.

Automated hermes-sweeper review found this is already implemented on current main:

  • gateway/run.py:17262 now calls _deliver_media_from_response(response, event, _media_adapter) without a history-media argument.
  • gateway/run.py:18359 defines the post-stream helper without a history_media_paths parameter.
  • Commit 646761c7831ff4c4cd0d6ac711ed791d487fb665 removed that parameter and call-site plumbing, with focused streaming regression coverage.
  • The retained auto-append dedup path initializes _history_media_paths at gateway/run.py:4786 before its use at gateway/run.py:5284.

The prior duplicate note for #72326 was considered; current main has superseded this one-line fix.

@teknium1 teknium1 closed this Jul 30, 2026
@teknium1 teknium1 added the sweeper:implemented-on-main Sweeper: behavior already present on current main label Jul 30, 2026
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 duplicate This issue or pull request already exists P2 Medium — degraded but workaround exists sweeper:implemented-on-main Sweeper: behavior already present on current main 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.

4 participants