fix(agent): strip MEDIA directives from compressor summarizer input (#14665)#15369
Closed
Tranquil-Flow wants to merge 2 commits into
Closed
fix(agent): strip MEDIA directives from compressor summarizer input (#14665)#15369Tranquil-Flow wants to merge 2 commits into
Tranquil-Flow wants to merge 2 commits into
Conversation
ilyasst
added a commit
to ilyasst/hermes-agent
that referenced
this pull request
May 13, 2026
Symptom: local Qwen3.5 via llama-server with --jinja occasionally generates "\nuser\n[<user_name>] ..." past the assistant stop boundary. Hermes formats user messages as "[<user_name>] ..." (gateway/run.py:5692), so the model learned that pattern as the user-turn delimiter; when sampling drifts past the assistant stop, it continues into a plausible user turn. The gateway delivers that verbatim to Telegram — the bot appears to literally echo "[ilyass] nudge" back at the user. Belt + suspenders: - run_agent.py: inject stop sequences ["\nuser\n[", "\n\nuser\n"] into chat_completions api_kwargs after _build_api_kwargs(). Cuts generation at the leak boundary, saving tokens too. Disable via HERMES_DISABLE_USER_PREFIX_STOP=1. - gateway/run.py: post-process final_response with a regex that detects and strips "\nuser\n[<name>] ..." patterns. Safety net for any leak that escapes the stop sequence (e.g., on streaming providers where stop detection differs). Logs a warning when it fires so we can monitor. Related upstream (all open, none merged): NousResearch#6272 (strip [CONTEXT COMPACTION] echoes), NousResearch#19887 (strip leaked template markers from gemma4 tool args), NousResearch#15369 (strip MEDIA directives), NousResearch#18529 (title leaks <thinking>).
c327156 to
ce1b4f8
Compare
Contributor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
_serialize_for_summary()passes full message content — includingMEDIA:/path/to/file.oggdirectives — to the summarizer LLM. If the summarizer echoes a MEDIA directive into the "## Critical Context" section, that summary gets re-injected into the next turn's message history viaSUMMARY_PREFIX. The downstream model sees what it believes is a prior output directive and may re-emit it, triggering unwanted media delivery.This PR strips MEDIA directives before they reach the summarizer, replacing them with a neutral
[media attachment]placeholder.Related Issue
Fixes #14665
Type of Change
Changes Made
agent/context_compressor.py— added_MEDIA_RE = re.compile(r'MEDIA:\S+')and stripped all MEDIA directives from content with[media attachment]placeholder before sending to the summarizer. Applied to all message roles (assistant, tool, user) uniformly.tests/agent/test_compressor_media_stripping.py— targeted tests (4)Validation table
"Here is the audio MEDIA:/tmp/voice.ogg done."MEDIA:/tmp/voice.oggvisible[media attachment]"The file path is /tmp/test.txt"How to Test
pytest tests/agent/test_compressor_media_stripping.py -v— 4 tests covering assistant content, tool results, preservation of non-MEDIA paths, and multiple directivesChecklist
Code
fix(scope):,feat(scope):, etc.)pytest tests/ -qand all tests passDocumentation & Housekeeping
docs/, docstrings) — or N/Acli-config.yaml.exampleif I added/changed config keys — or N/ACONTRIBUTING.mdorAGENTS.mdif I changed architecture or workflows — or N/AScreenshots / Logs
N/A — see commit description and PR diff.