fix(gateway): stop MEDIA tag regex from absorbing following tag or text (#68773) - #68781
fix(gateway): stop MEDIA tag regex from absorbing following tag or text (#68773)#68781Enough1122 wants to merge 1 commit into
Conversation
|
Reviewed against #68773 — this correctly fixes the root cause (Bug 1), and I verified it locally. Correctness
Tests — ran the new One minor, non-blocking note LGTM. Focused change, correct root cause, real regression coverage. |
|
@PRATHAMESH75 thanks for the careful review and the local verification — much appreciated. Noted on the residual edge case for — written by Hermes Agent on behalf of @Enough1122 |
|
cc @PRATHAMESH75 @alt-glitch — thanks again for the LGTM and the local verification. Noted on the residual edge case for Branch — written by Hermes Agent on behalf of @Enough1122 |
…xt (NousResearch#68773) Two MEDIA:<path> tags emitted back-to-back without a separator (MEDIA:/a.pngMEDIA:/b.png) used to be merged into a single invalid path by both: 1. MEDIA_TAG_CLEANUP_RE — the path class used greedy \S+(?:[^\S\n]+\S+)*? which could capture the next MEDIA: keyword as part of the path. 2. MEDIA_EXTENSIONLESS_TAG_RE — the path class used greedy [^\s\n`"']+ with no boundary lookahead, so the same merge happened for extension-less tags. The merged path (/a.pngMEDIA:/b.png) failed validate_media_delivery_path and was silently dropped. On Ollama-hosted GLM models, the leftover MEDIA: text in the unstripped source then tripped _should_treat_stop_as_truncated and injected a spurious "[System: Your previous response was truncated...]" continuation prompt. This change makes both regexes non-greedy and adds MEDIA: to the trailing-lookahead boundary set, so each tag matches independently. Tests in tests/gateway/test_media_tag_separator.py cover: - known-extension regex splits glued tags - known-extension strip delivers both files - extensionless regex splits glued tags - extensionless regex stops at next MEDIA: - extensionless strip delivers both files - extensionless regex still matches well-formed paths - known-ext tag followed by text stays visible (no silent drop)
b4e703d to
ea76d75
Compare
|
cc @PRATHAMESH75 — rebased onto current upstream main. The fix is unchanged in content (2 files, +133/-4). Thanks again for the LGTM. The branch was rebuilt from upstream
— written by Hermes Agent on behalf of @Enough1122 |
|
Closing this stale PR for now: it has not reached a mergeable/reviewed state and is unlikely to be merged in its current form. Reopen or submit a fresh PR if the issue remains relevant. |
Fixes #68773.
Two
MEDIA:<path>tags emitted back-to-back without a separator (MEDIA:/a.pngMEDIA:/b.png) merged into a single invalid path and were silently dropped. The same happened for extension-less tags (MEDIA:/CaddyfileMEDIA:/Dockerfile).Root cause
Both regexes in
gateway/platforms/base.pyused greedy quantifiers in their path class:MEDIA_TAG_CLEANUP_RE—\S+(?:[^\S\n]+\S+)*?\.extcould capture the nextMEDIA:keyword as part of the path.MEDIA_EXTENSIONLESS_TAG_RE—[^\s\n"']+` with no boundary lookahead absorbed the next tag's content.The merged path (
/a.pngMEDIA:/b.png) failedvalidate_media_delivery_pathand was dropped. On Ollama-hosted GLM models, the leftoverMEDIA:text in the unstripped source then tripped_should_treat_stop_as_truncatedand injected a spurious "[System: Your previous response was truncated...]" continuation prompt.Fix
Make both regexes non-greedy and add
MEDIA:to the trailing-lookahead boundary set:MEDIA_TAG_CLEANUP_RE:\S+(?:[^\S\n]+\S+)*?\.ext→\S+?(?:[^\S\n]+\S+?)*?\.ext, plus quoted branches also become non-greedy. Lookahead adds|MEDIA:to its boundary set.MEDIA_EXTENSIONLESS_TAG_RE:[^\s\n"']+→[^\s\n`"']+?` followed by `(?=[`"'\s,;:)\]}]|MEDIA:|$)`.Tests
tests/gateway/test_media_tag_separator.py— 7 new tests:test_known_extension_regex_splits_glued_tags— primary regex matches each tag independently.test_strip_media_directives_handles_glued_known_extension_tags— both real .png files delivered.test_extensionless_regex_does_not_absorb_next_media_keyword— fallback regex stops at the nextMEDIA:.test_extensionless_regex_does_not_absorb_following_text— fallback regex stops at the nextMEDIA:even when surrounded by text.test_extensionless_regex_still_matches_normal_cases— well-formed paths still match.test_strip_media_directives_handles_glued_extensionless_tags— both real files delivered.test_strip_media_directives_does_not_drop_known_ext_tag_followed_by_text— known-extension tag glued to text leaves the text visible (no silent drop).All 7 pass. Pre-existing failures in
test_platform_base.pyare unchanged (verified by stashing my diff and running the same tests againstmain).— written by Hermes Agent on behalf of @Enough1122