Skip to content

The agent that grows with you

fix(gateway): only a path-shaped MEDIA: holds back the open streamed line

Teknium14847f40392c
diff --git a/gateway/stream_consumer.py b/gateway/stream_consumer.py
index aa2609c2d41..6ddf69b31be 100644
--- a/gateway/stream_consumer.py
+++ b/gateway/stream_consumer.py
@@ -58,9 +58,12 @@ _FUTURE_TYPES = (asyncio.Future, concurrent.futures.Future)
_DEFAULT_BOUNDARY_PLACEHOLDER = "⏸ 等待审批中..."
# A delivery directive still being streamed on the open (last) line: "MEDIA:" not yet
-# stripped by the cleaner, a word-start prefix of "MEDIA", or a prefix of a [[…]] flag.
+# stripped by the cleaner and opening a path the way MEDIA_TAG_CLEANUP_RE's does (quote,
+# ~/, /, a drive letter), a word-start prefix of "MEDIA", or a prefix of a [[…]] flag.
+# Prose such as "social media: X" is not a path, so it never holds the line back.
_PENDING_MEDIA_RE = re.compile(
- r"""MEDIA:|(?:^|\s)[`"'*_]{0,3}M(?:E(?:D(?:I(?:A)?)?)?)?$""", re.IGNORECASE)
+ r"""MEDIA:\s*(?:$|[`"'~/\\]|[A-Za-z](?::|$))|(?:^|\s)[`"'*_]{0,3}M(?:E(?:D(?:I(?:A)?)?)?)?$""",
+ re.IGNORECASE)
_PENDING_DIRECTIVE_RE = re.compile(r"\[\[?[A-Za-z_]*\]?$")
_STREAM_DIRECTIVES = ("[[audio_as_voice]]", "[[as_document]]")
diff --git a/tests/gateway/test_stream_consumer_pending_media.py b/tests/gateway/test_stream_consumer_pending_media.py
index eaaad29727e..ad4083b59ff 100644
--- a/tests/gateway/test_stream_consumer_pending_media.py
+++ b/tests/gateway/test_stream_consumer_pending_media.py
@@ -71,3 +71,12 @@ async def test_partial_voice_directive_is_never_shown():
async def test_prose_mentioning_media_still_streams():
adapter = await _stream(["La palabra MEDIA aparece ", "en un texto normal ", "y sigue."])
assert any("La palabra MEDIA aparece" in t for t in _shown(adapter))
+
+
+async def test_prose_with_media_colon_streams_before_the_line_ends():
+ # "media:" followed by words, not a path, is never a directive: the open line keeps streaming.
+ adapter = await _stream(["Platforms we cover, social media: ", "Twitter, Mastodon ", "and Bluesky",
+ " plus a few forums", " in one long line"])
+ interim = [t for t in _shown(adapter) if "in one long line" not in t]
+ assert any("Twitter, Mastodon" in t for t in interim), _shown(adapter)