Skip to content

The agent that grows with you

fix(gateway): finish the media-cache offload sweep (teams NameError, buzz, photon)

kshitijk4poor568b16122da6
diff --git a/plugins/platforms/buzz/adapter.py b/plugins/platforms/buzz/adapter.py
index 74abfe29bc8..fe8667e54df 100644
--- a/plugins/platforms/buzz/adapter.py
+++ b/plugins/platforms/buzz/adapter.py
@@ -173,7 +173,7 @@ from gateway.platforms.base import (
SendResult,
MessageEvent,
MessageType,
- cache_media_bytes,
+ cache_media_bytes_async,
)
from gateway.config import Platform
@@ -2376,7 +2376,7 @@ class BuzzAdapter(BasePlatformAdapter):
logger.warning("Buzz: attachment SHA-256 does not match imeta")
return None
try:
- return cache_media_bytes(
+ return await cache_media_bytes_async(
bytes(data),
filename=metadata["filename"],
mime_type=metadata["mime_type"],
@@ -2863,7 +2863,7 @@ class BuzzAdapter(BasePlatformAdapter):
media_kinds: List[str] = []
from gateway.platforms.base import (
- cache_media_bytes,
+ cache_media_bytes_async,
validate_inbound_media_size,
)
@@ -2894,7 +2894,7 @@ class BuzzAdapter(BasePlatformAdapter):
mimetypes.guess_type(download_path.name)[0]
or "application/octet-stream"
)
- cached = cache_media_bytes(
+ cached = await cache_media_bytes_async(
download_path.read_bytes(),
filename=download_path.name,
mime_type=mime_type,
diff --git a/plugins/platforms/photon/adapter.py b/plugins/platforms/photon/adapter.py
index 0d07a150097..f4129cad435 100644
--- a/plugins/platforms/photon/adapter.py
+++ b/plugins/platforms/photon/adapter.py
@@ -1254,7 +1254,7 @@ class PhotonAdapter(BasePlatformAdapter):
media_urls: List[str] = []
media_types: List[str] = []
- def _normalize_binary_payload(
+ async def _normalize_binary_payload(
payload: Dict[str, Any]
) -> tuple[str, MessageType, List[str], List[str]]:
is_voice = payload.get("type") == "voice"
@@ -1266,8 +1266,10 @@ class PhotonAdapter(BasePlatformAdapter):
if not is_voice and (name.lower().endswith(".caf") or mime == "audio/x-caf"):
is_voice = True
mtype = MessageType.VOICE if is_voice else _attachment_message_type(mime)
- cached = _cache_inbound_attachment(
- payload, name, mime, force_audio=is_voice
+ # Base64 decode + media-cache write (fsync-free but still disk
+ # I/O on possibly multi-MB payloads) — keep it off the event loop.
+ cached = await asyncio.to_thread(
+ _cache_inbound_attachment, payload, name, mime, force_audio=is_voice
)
if cached:
return (
@@ -1415,7 +1417,7 @@ class PhotonAdapter(BasePlatformAdapter):
text = content.get("text") or ""
mtype = MessageType.TEXT
elif ctype in {"attachment", "voice"}:
- text, mtype, media_urls, media_types = _normalize_binary_payload(content)
+ text, mtype, media_urls, media_types = await _normalize_binary_payload(content)
elif ctype == "richlink":
text = _format_richlink_content(content)
mtype = MessageType.TEXT
@@ -1438,7 +1440,7 @@ class PhotonAdapter(BasePlatformAdapter):
text_parts.append(_format_richlink_content(item_content))
continue
if item_type in {"attachment", "voice"}:
- marker, item_mtype, item_urls, item_types = _normalize_binary_payload(
+ marker, item_mtype, item_urls, item_types = await _normalize_binary_payload(
item_content
)
if mtype == MessageType.TEXT:
diff --git a/plugins/platforms/teams/adapter.py b/plugins/platforms/teams/adapter.py
index b0182381ff5..9c0881bb4df 100644
--- a/plugins/platforms/teams/adapter.py
+++ b/plugins/platforms/teams/adapter.py
@@ -1117,7 +1117,7 @@ class TeamsAdapter(BasePlatformAdapter):
# bearer token; the generic cache helper sends none.
data = await self._fetch_attachment_bytes(content_url)
ext = content_type.split("/")[-1].split(";")[0] or "png"
- cached_m = cache_media_bytes(
+ cached_m = await cache_media_bytes_async(
data,
filename=att_name or f"image.{ext}",
mime_type=content_type,
diff --git a/tests/gateway/test_async_media_cache.py b/tests/gateway/test_async_media_cache.py
index e28b8e096ef..1745d9fbe61 100644
--- a/tests/gateway/test_async_media_cache.py
+++ b/tests/gateway/test_async_media_cache.py
@@ -64,6 +64,27 @@ async def test_async_cache_wrapper_propagates_validation_errors(monkeypatch):
await base.cache_image_from_bytes_async(b"not-an-image", ".png")
+async def test_cache_media_bytes_async_runs_off_loop_and_forwards_kwargs(monkeypatch):
+ loop_thread = threading.get_ident()
+ observed = {}
+
+ def fake_cache_media_bytes(data, *, filename="", mime_type="", default_kind=None):
+ observed["thread"] = threading.get_ident()
+ observed["call"] = (data, filename, mime_type, default_kind)
+ return "cached-media"
+
+ monkeypatch.setattr(base, "cache_media_bytes", fake_cache_media_bytes)
+
+ result = await base.cache_media_bytes_async(
+ b"payload", filename="report.pdf", mime_type="application/pdf", default_kind="document"
+ )
+
+ assert result == "cached-media"
+ assert observed["call"] == (b"payload", "report.pdf", "application/pdf", "document")
+ assert observed["thread"] != loop_thread
+
+
@pytest.mark.asyncio
async def test_async_cache_wrapper_uses_active_profile_home(monkeypatch, tmp_path):
profile_home = tmp_path / "profile"
diff --git a/tests/gateway/test_buzz_adapter.py b/tests/gateway/test_buzz_adapter.py
index 18587187f0a..bee8af29e34 100644
--- a/tests/gateway/test_buzz_adapter.py
+++ b/tests/gateway/test_buzz_adapter.py
@@ -1134,8 +1134,8 @@ class TestInboundAttachments:
)
monkeypatch.setattr(
_buzz_mod,
- "cache_media_bytes",
- MagicMock(side_effect=OSError(36, "File name too long")),
+ "cache_media_bytes_async",
+ AsyncMock(side_effect=OSError(36, "File name too long")),
)
adapter = _make_adapter()
diff --git a/tests/gateway/test_teams.py b/tests/gateway/test_teams.py
index dc3b489e2fe..fe7ff4c8a78 100644
--- a/tests/gateway/test_teams.py
+++ b/tests/gateway/test_teams.py
@@ -702,12 +702,12 @@ class TestTeamsBotFrameworkAttachments:
adapter._fetch_attachment_bytes = AsyncMock(return_value=b"\x89PNG fake")
adapter._get_botframework_token = AsyncMock(return_value="tok")
- def fake_cache_media_bytes(data, **kwargs):
+ async def fake_cache_media_bytes(data, **kwargs):
return SimpleNamespace(
path="/tmp/img.png", media_type="image/png", kind="image"
)
- with patch.object(_teams_mod, "cache_media_bytes", fake_cache_media_bytes):
+ with patch.object(_teams_mod, "cache_media_bytes_async", fake_cache_media_bytes):
activity = self._make_activity([self._bf_image_attachment()])
await adapter._on_message(self._make_ctx(activity))
@@ -943,7 +943,10 @@ class TestTeamsBotFrameworkAttachments:
adapter = self._make_adapter()
adapter._fetch_attachment_bytes = AsyncMock(return_value=b"<html>error page</html>")
- with patch.object(_teams_mod, "cache_media_bytes", lambda *a, **kw: None):
+ async def _no_media(*a, **kw):
+ return None
+
+ with patch.object(_teams_mod, "cache_media_bytes_async", _no_media):
with patch.object(_teams_mod.logger, "warning") as warn:
activity = self._make_activity([self._bf_image_attachment()])
await adapter._on_message(self._make_ctx(activity))