Skip to content

The agent that grows with you

fix: inline MEDIA data URLs on the API server despite a terminal <|eos|> sentinel

teknium113f908f10d8d
diff --git a/gateway/platforms/api_server.py b/gateway/platforms/api_server.py
index e293e90ce2e..84934c791d2 100644
--- a/gateway/platforms/api_server.py
+++ b/gateway/platforms/api_server.py
@@ -121,7 +121,8 @@ from gateway.platforms import api_server_room_grants as _room_grants
from gateway.platforms import api_server_runs as _api_runs
from gateway.platforms.api_server_openai_routes import OpenAICompatRoutesMixin
from gateway.platforms.base import (
- MEDIA_TAG_CLEANUP_RE, BasePlatformAdapter, SendResult, is_network_accessible, validate_media_delivery_path)
+ MEDIA_TAG_CLEANUP_RE, BasePlatformAdapter, SendResult, _terminal_sentinel_start, is_network_accessible,
+ validate_media_delivery_path)
from gateway.platforms.api_server_run_idempotency import RunIdempotencyStore
from agent.redact import redact_sensitive_text
from agent.interrupt_compat import request_hard_interrupt
@@ -870,7 +871,12 @@ def _resolve_media_to_data_urls(text: str) -> str:
def _repl(m: "re.Match[str]") -> str:
return _to_data_url(m.group("path")) or m.group(0)
try:
- return MEDIA_TAG_CLEANUP_RE.sub(_repl, text)
+ # A leaked terminal <|eos|> glued to the last tag is not a path terminator (#111046):
+ # scan without it, and drop it (control token, never content) only when a tag resolved.
+ sentinel_start = _terminal_sentinel_start(text)
+ scan = text[:sentinel_start] if sentinel_start >= 0 else text
+ resolved = MEDIA_TAG_CLEANUP_RE.sub(_repl, scan)
+ return text if resolved == scan else resolved
except Exception:
return text
diff --git a/gateway/platforms/base.py b/gateway/platforms/base.py
index 5836b972800..42599a58080 100644
--- a/gateway/platforms/base.py
+++ b/gateway/platforms/base.py
@@ -1327,10 +1327,12 @@ _TERMINAL_SENTINEL = "<|eos|>"
def _terminal_sentinel_start(text: str) -> int:
- """Offset of an exact ``<|eos|>`` closing ``text`` (trailing whitespace ignored), else -1."""
- end = len(text.rstrip())
- start = end - len(_TERMINAL_SENTINEL)
- return start if start >= 0 and text[start:end] == _TERMINAL_SENTINEL else -1
+ """Offset where the run of exact ``<|eos|>`` tokens closing ``text`` (trailing whitespace
+ ignored) begins, else -1; the run ends at ``len(text.rstrip())``."""
+ end = start = len(text.rstrip())
+ while start >= len(_TERMINAL_SENTINEL) and text[start - len(_TERMINAL_SENTINEL):start] == _TERMINAL_SENTINEL:
+ start -= len(_TERMINAL_SENTINEL)
+ return start if start < end else -1
def _mask_media_scan_text(text: str) -> str:
@@ -1341,7 +1343,7 @@ def _mask_media_scan_text(text: str) -> str:
masked = A._mask_json_string_media(A._mask_protected_spans(text))
start = _terminal_sentinel_start(text)
if start >= 0:
- masked = _blank_spans(masked, [(start, start + len(_TERMINAL_SENTINEL))])
+ masked = _blank_spans(masked, [(start, len(text.rstrip()))])
return masked
@@ -1351,7 +1353,7 @@ def _deliverable_tag_spans(text: str) -> list:
spans = _real_media_tag_spans(_mask_media_scan_text(text))
start = _terminal_sentinel_start(text)
if spans and start >= 0:
- spans.append((start, start + len(_TERMINAL_SENTINEL)))
+ spans.append((start, len(text.rstrip())))
return spans
diff --git a/tests/gateway/test_api_server_media_data_urls.py b/tests/gateway/test_api_server_media_data_urls.py
index 1f07fc8e30f..1903b987bc7 100644
--- a/tests/gateway/test_api_server_media_data_urls.py
+++ b/tests/gateway/test_api_server_media_data_urls.py
@@ -42,6 +42,13 @@ class TestResolveMediaToDataUrls(unittest.TestCase):
out = _resolve_media_to_data_urls(f"See `MEDIA:{p}` above")
self.assertIn("data:image/png;base64,", out)
+ def test_terminal_eos_sentinel_does_not_block_inlining(self):
+ """A leaked terminal ``<|eos|>`` glued to the tag (#111046) inlines exactly like the clean
+ response; the control token is dropped rather than returned to the HTTP client."""
+ p = self._write_png()
+ clean = f"Here you go: MEDIA:{p}"
+ self.assertEqual(_resolve_media_to_data_urls(clean + "<|eos|>"), _resolve_media_to_data_urls(clean))
+
def test_missing_file_left_untouched(self):
text = "MEDIA:/nonexistent/path/shot.png"
self.assertEqual(_resolve_media_to_data_urls(text), text)
diff --git a/tests/gateway/test_media_terminal_sentinel.py b/tests/gateway/test_media_terminal_sentinel.py
index 128a7bdc6f5..777cd506851 100644
--- a/tests/gateway/test_media_terminal_sentinel.py
+++ b/tests/gateway/test_media_terminal_sentinel.py
@@ -5,10 +5,11 @@ import pytest
from gateway.platforms.base import BasePlatformAdapter
[email protected]("sentinel", ["<|eos|>", "<|eos|><|eos|>"])
@pytest.mark.parametrize("filename", ["chart.png", "payload.weirdext", "Caddyfile"])
-def test_terminal_eos_sentinel_leaves_extraction_and_cleanup_unchanged(tmp_path, monkeypatch, filename):
- """Known-extension, unknown-extension and extension-less tags glued to ``<|eos|>`` extract
- and clean exactly like the same response without the sentinel."""
+def test_terminal_eos_sentinel_leaves_extraction_and_cleanup_unchanged(tmp_path, monkeypatch, filename, sentinel):
+ """Known-extension, unknown-extension and extension-less tags glued to a (possibly repeated)
+ ``<|eos|>`` extract and clean exactly like the same response without the sentinel."""
root = tmp_path / "media-cache"
root.mkdir()
media_file = root / filename
@@ -16,8 +17,8 @@ def test_terminal_eos_sentinel_leaves_extraction_and_cleanup_unchanged(tmp_path,
monkeypatch.setattr("gateway.platforms.base.MEDIA_DELIVERY_SAFE_ROOTS", (root,))
clean = f"Here is the file.\nMEDIA:{media_file}"
- assert BasePlatformAdapter.extract_media(clean + "<|eos|>") == BasePlatformAdapter.extract_media(clean)
- assert BasePlatformAdapter.strip_media_directives_for_display(clean + "<|eos|>") == "Here is the file."
+ assert BasePlatformAdapter.extract_media(clean + sentinel) == BasePlatformAdapter.extract_media(clean)
+ assert BasePlatformAdapter.strip_media_directives_for_display(clean + sentinel) == "Here is the file."
@pytest.mark.parametrize(