Conversation
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.
monerostar
left a comment
There was a problem hiding this comment.
Win11 native verification (monerostar)
Environment
- Windows 11 Build 26200 · native (not WSL agent runtime)
- Hermes Agent v0.19.0 (2026.7.20) · install git
0fa5e41cat%LOCALAPPDATA%\hermes\hermes-agent - Git Bash / MSYS · ripgrep 15.1.0
- PR tip checked out:
2d362c1ec(pr-72373worktree)
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-agent — comment 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.
Follow-up ops context (not a competing PR)The post-stream Ops write-up for single Local install still carries the one-liner bind until this PR merges; no second PR from monerostar. |
|
Thanks for identifying the undefined media-history binding. Automated hermes-sweeper review found this is already implemented on current
The prior duplicate note for #72326 was considered; current main has superseded this one-line fix. |
Summary
This PR fixes a
NameErrorin the gateway's message handler.The Problem
In
gateway/run.py, the variable_history_media_pathswas being passed to_deliver_media_from_responsebut was never defined within the_handle_message_with_agentfunction. This caused the gateway to crash with aNameErrorwhenever the agent attempted to deliver media (images/audio) via the streaming delivery path or when processing messages withMEDIA:tags.The Fix
The
_history_media_pathsvariable 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
NameErrorno longer occurs when sending media.