Skip to content

fix(gateway): skip MEDIA tags inside code blocks in extract_media#16585

Open
vominh1919 wants to merge 1 commit into
NousResearch:mainfrom
vominh1919:fix/media-tags-code-blocks
Open

fix(gateway): skip MEDIA tags inside code blocks in extract_media#16585
vominh1919 wants to merge 1 commit into
NousResearch:mainfrom
vominh1919:fix/media-tags-code-blocks

Conversation

@vominh1919

Copy link
Copy Markdown
Contributor

Fixes #16434

Problem

When the agent's response contains MEDIA: tags inside fenced code blocks (``` ... ```) or inline code (`...`), the gateway incorrectly treats them as real attachment directives and tries to deliver the files. This also mangles code examples in streamed display.

Root Cause

extract_media() in base.py and _clean_for_display() in stream_consumer.py use finditer() / sub() over raw content with no code-block filtering. Ironically, extract_local_files() in the same file already has this exact code-block awareness pattern.

Fix

Applied the same code-block-aware pattern already used by extract_local_files():

  1. base.pyextract_media(): Build code_spans list from fenced/inline code blocks, skip matches inside those spans, and only remove non-code MEDIA tags from cleaned content.

  2. stream_consumer.py_clean_for_display(): Same pattern — build code spans, use _safe_sub() to preserve MEDIA tags inside code blocks.

Files Changed

  • gateway/platforms/base.py — 20 lines added (code-block filtering in extract_media())
  • gateway/stream_consumer.py — 15 lines added (code-block filtering in _clean_for_display())

MEDIA:<path> tokens inside fenced code blocks and inline code were
incorrectly treated as real attachment directives, causing the gateway
to attempt delivery of non-existent files and producing noisy warnings.

The extract_local_files() method already had code-block awareness, but
extract_media() and _clean_for_display() did not. Applied the same
pattern: build code_spans list, skip matches inside those spans.

Fixes NousResearch#16434
@alt-glitch alt-glitch added type/bug Something isn't working P2 Medium — degraded but workaround exists comp/gateway Gateway runner, session dispatch, delivery labels Apr 27, 2026
@alt-glitch

Copy link
Copy Markdown
Collaborator

Likely duplicate of #16436 — both fix #16434 by adding code-block awareness to extract_media().

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 type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Gateway should not treat MEDIA examples or missing files as attachments

2 participants