Skip to content

fix(gateway): match uppercase media extensions in MEDIA: pattern - #32695

Closed
InphinitiZ wants to merge 1 commit into
NousResearch:mainfrom
InphinitiZ:fix/media-regex-ignorecase-v2
Closed

fix(gateway): match uppercase media extensions in MEDIA: pattern#32695
InphinitiZ wants to merge 1 commit into
NousResearch:mainfrom
InphinitiZ:fix/media-regex-ignorecase-v2

Conversation

@InphinitiZ

Copy link
Copy Markdown

Summary

The MEDIA:<path> regex in BasePlatformAdapter.extract_media (gateway/platforms/base.py) hardcodes lowercase file extensions (png|jpe?g|gif|webp|mp4|...) and is compiled without re.IGNORECASE. As a result, any path the agent emits with an uppercase extension — common for camera-style filenames like IMG_0001.JPG — silently bypasses the MEDIA extraction path and gets shipped to the user as plain text instead of being delivered as a native media attachment.

In practice this affects anyone whose media library was sourced from a smartphone or DSLR (which routinely produce .JPG / .MP4 / .MOV), or whose OS / file picker preserves uppercase suffixes.

Reproduction

Before this fix:

import re
pattern = re.compile(
    r'''[`"']?MEDIA:\s*(?P<path>`[^`\n]+`|"[^"\n]+"|'[^'\n]+'|(?:~/|/)\S+(?:[^\S\n]+\S+)*?\.(?:png|jpe?g|gif|webp|mp4|mov|avi|mkv|webm|ogg|opus|mp3|wav|m4a|flac|epub|pdf|zip|rar|7z|docx?|xlsx?|pptx?|txt|csv|apk|ipa)(?=[\s`"',;:)\]}]|$))[`"']?'''
)
assert pattern.search('MEDIA:/photos/IMG_0001.jpg')   # OK
assert pattern.search('MEDIA:/photos/IMG_0001.JPG')   # AssertionError — None

End-to-end symptom: the user receives the literal string MEDIA:/photos/IMG_0001.JPG in their chat instead of the photo.

Fix

Add re.IGNORECASE to media_pattern. The regex body itself is unchanged.

extract_local_files's bare-path matcher already uses re.IGNORECASE, so no change is needed there — only the explicit MEDIA: tag path was affected.

Tests

Adds two regression tests to tests/gateway/test_platform_base.py:

  1. test_extract_media_uppercase_jpg_extension — single uppercase extension (IMG_0001.JPG).
  2. test_extract_media_mixed_case_extensions.PNG, .JPG, .Jpeg, .MP4 mixed in one response.

Both fail without the flag and pass with it.

$ pytest tests/gateway/test_platform_base.py -q
103 passed, 2 skipped

Relationship to #29620

PR #29620 also incidentally adds re.IGNORECASE (alongside a character-class reorder). Its stated motivation — that \] inside the lookahead character class prematurely terminates the class — does not actually reproduce; the original character class compiles and matches MEDIA:/tmp/test.png) / ] / } correctly. The real bug being fixed there is the missing re.IGNORECASE, which is what this PR isolates with a minimal diff and dedicated regression tests. Happy to defer to #29620 if reviewers prefer that branch.

@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 May 26, 2026
@alt-glitch

Copy link
Copy Markdown
Collaborator

Duplicate of #30722 (and #30535). The same re.IGNORECASE fix for media_pattern in gateway/platforms/base.py. Root issue tracked at #30526.

The MEDIA:<path> regex in extract_media hardcoded lowercase extensions
(png, jpe?g, mp4, ...) with no re.IGNORECASE flag, so paths emitted by
the agent with uppercase suffixes (e.g. MEDIA:/.../IMG_0001.JPG from a
folder of camera-style filenames) silently leaked through as plain text
instead of being delivered as media attachments.

Add re.IGNORECASE to media_pattern only — the regex body itself is
unchanged. extract_local_files already uses re.IGNORECASE on its
bare-path matcher, so no change needed there.
@InphinitiZ
InphinitiZ force-pushed the fix/media-regex-ignorecase-v2 branch from 015a9f0 to 6c61ab2 Compare May 26, 2026 16:17
@InphinitiZ

Copy link
Copy Markdown
Author

Thanks @alt-glitch for the pointer — closing as duplicate of #30535 / #30722. Will track #30526 instead.

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.

2 participants