fix(gateway): skip MEDIA: tags inside code blocks and blockquotes - #35699
fix(gateway): skip MEDIA: tags inside code blocks and blockquotes#35699liuhao1024 wants to merge 1 commit into
Conversation
extract_media() scanned the full response text without distinguishing live delivery tags from example paths in fenced code blocks, inline code spans, and blockquotes. This caused false positives where the agent's explanation of MEDIA: syntax (or tool output containing example paths) was stripped from user-visible text and the path was added to the media delivery list. Added _mask_protected_spans() helper that replaces protected regions with equal-length whitespace before regex matching, preserving match offsets. The helper skips backtick-quoted paths in MEDIA: tags to maintain existing path extraction behavior. Fixes NousResearch#35695
tonydwb
left a comment
There was a problem hiding this comment.
Code Review Summary
Verdict: Approved
Overview
Well-crafted fix for extract_media() false positives where MEDIA:<path> tags inside fenced code blocks, inline code spans, and blockquotes were incorrectly extracted as real media delivery directives.
Looks Good
_mask_protected_spanscorrectly skips MEDIA: path quotes (e.g.MEDIA:`/path/to/file.png`)- Preserves character count for offset-valid regex matching
- 5 regression tests covering code blocks, inline code, blockquotes, mixed content, and real tag preservation
- Additive — no changes to existing extraction paths
- Low blast radius; mirrors existing
extract_local_filespattern
Reviewed by Hermes Agent
|
@daimon-nous[bot] Acknowledged. This PR passes all CI checks, while the competing PR #16585 has a test failure. If maintainers prefer the duplicate flag to take precedence, feel free to close this. Otherwise, I believe this PR is ready for merge. |
|
Thanks @liuhao1024 — salvaged into a batch PR alongside the related auto-append guard from #16721: #36275. Your code-block / inline-code / blockquote masking commit was cherry-picked with authorship preserved; it applied cleanly to current main. Verified together with the other fix — 139 passed, 2 skipped across the gateway media tests, including your new cases. Closing in favor of #36275. |
What does this PR do?
Fixes
extract_media()false positives whereMEDIA:<path>tags inside fenced code blocks, inline code spans, and blockquotes were incorrectly extracted as real media delivery directives. This caused example paths in agent responses to be stripped from user-visible text and potentially delivered as unintended files.Related Issue
Fixes #35695
Type of Change
Changes Made
gateway/platforms/base.py: Added_mask_protected_spans()helper that replaces content inside fenced code blocks, inline code spans, and blockquotes with equal-length whitespace before regex matching. Modifiedextract_media()to use masked content for bothfinditer()(extraction) andsub()(cleanup). The helper skips masking backtick-quoted paths inMEDIA:tags (e.g.MEDIA:`/path/to/file.png`) to preserve existing path extraction behavior.tests/gateway/test_platform_base.py: Added 5 regression tests covering MEDIA tags in fenced code blocks, inline code, blockquotes, mixed code+prose, and real tags outside protected regions.How to Test
pytest tests/gateway/test_platform_base.py -k "TestExtractMedia" -v— all 24 tests should pass (19 existing + 5 new)To send an image, include \MEDIA:/path/to/image.jpg`should NOT extract/path/to/image.jpg` as a media fileMEDIA:/tmp/report.pdfin normal prose should still be extractedpytest tests/gateway/test_send_image_file.py tests/gateway/test_media_extraction.py -v— all related tests should passChecklist
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/ACode Intelligence
BasePlatformAdapter.extract_media(callers: ~10 across gateway dispatch paths)BasePlatformAdapter._mask_protected_spans(new, called from extract_media only)extract_local_filesalready uses code-span detection (lines 2753-2761) for the same purpose; this fix applies the same concept toextract_media