Skip to content

The agent that grows with you

fix(photon): evaluate the group mention gate before caching inline attachments

teknium1ee9c0f1a32b7
diff --git a/plugins/platforms/photon/adapter.py b/plugins/platforms/photon/adapter.py
index 3f1497f520a..05733baeea7 100644
--- a/plugins/platforms/photon/adapter.py
+++ b/plugins/platforms/photon/adapter.py
@@ -427,6 +427,19 @@ _CONTENT_NORMALIZERS: Dict[Any, Callable[[Dict[str, Any]], _Normalized]] = {
_BINARY_CONTENT_TYPES = {"attachment", "voice", "group"} # may decode/cache media bytes → run off the event loop
+def _mention_gate_text(content: Dict[str, Any]) -> str:
+ """The user-typed text of a payload WITHOUT decoding or caching any attachment bytes,
+ so the group require_mention gate can run before ``_normalize_content`` persists media."""
+ ctype = content.get("type")
+ if ctype == "text":
+ return content.get("text") or ""
+ if ctype == "richlink":
+ return _format_richlink_content(content)
+ if ctype == "group":
+ return "\n".join(part for part in map(_mention_gate_text, _group_item_contents(content)) if part)
+ return ""
+
+
def _normalize_content(content: Dict[str, Any]) -> _Normalized:
"""Turn a sidecar ``content`` payload into (text, type, media_urls, media_types)."""
ctype = content.get("type")
@@ -782,15 +795,18 @@ class PhotonAdapter(BasePlatformAdapter):
return
await self.handle_message(_event(choice))
return
+ # Mention gate BEFORE normalising: _normalize_content persists inline attachment
+ # bytes to the media cache, and a dropped group message must not leave files behind.
+ gated = chat_type == "group" and self.require_mention
+ if gated and not self._message_matches_mention_patterns(_mention_gate_text(content)):
+ logger.debug("[photon] ignoring group message (require_mention=true, no mention pattern matched)")
+ return
if ctype in _BINARY_CONTENT_TYPES:
# Base64 decode + media-cache write of possibly multi-MB payloads — keep it off the event loop.
text, mtype, media_urls, media_types = await asyncio.to_thread(_normalize_content, content)
else:
text, mtype, media_urls, media_types = _normalize_content(content)
- if chat_type == "group" and self.require_mention:
- if not self._message_matches_mention_patterns(text):
- logger.debug("[photon] ignoring group message (require_mention=true, no mention pattern matched)")
- return
+ if gated:
text = self._clean_mention_text(text)
self._record_recent_richlink(space_id, _richlink_url_from_content(content) or text)
await self.handle_message(_event(text, mtype, media_urls=media_urls, media_types=media_types))
diff --git a/tests/plugins/platforms/photon/test_mention_gating.py b/tests/plugins/platforms/photon/test_mention_gating.py
index 202936f993d..5e120ce50f1 100644
--- a/tests/plugins/platforms/photon/test_mention_gating.py
+++ b/tests/plugins/platforms/photon/test_mention_gating.py
@@ -115,3 +115,34 @@ def test_invalid_pattern_skipped(monkeypatch: pytest.MonkeyPatch) -> None:
# Bad regex dropped, good one kept.
assert len(adapter._mention_patterns) == 1
assert adapter._message_matches_mention_patterns("a good thing") is True
+
+
[email protected]("caption, cached_calls, dispatched", [
+ ("holiday pic", 0, 0), # unmentioned group attachment: never persisted
+ ("hermes holiday pic", 1, 1), # mentioned: cached and dispatched
+])
+async def test_unmentioned_group_attachment_is_not_cached(
+ monkeypatch: pytest.MonkeyPatch, caption: str, cached_calls: int, dispatched: int) -> None:
+ """The group mention gate must run BEFORE inline attachment bytes hit the media cache."""
+ import plugins.platforms.photon.adapter as photon_adapter
+
+ adapter = _make_adapter(monkeypatch, extra={"require_mention": True})
+ captured = _capture(adapter, monkeypatch)
+ calls: List[str] = []
+
+ def fake_cache(content, name, mime, *, force_audio=False):
+ calls.append(name)
+ return "/tmp/cached.png"
+
+ monkeypatch.setattr(photon_adapter, "_cache_inbound_attachment", fake_cache)
+ payload = _group_payload(caption)
+ payload["content"] = {"type": "group", "items": [
+ {"content": {"type": "text", "text": caption}},
+ {"content": {"type": "attachment", "name": "pic.png", "mimeType": "image/png",
+ "data": "aGVsbG8=", "encoding": "base64"}},
+ ]}
+
+ await adapter._dispatch_inbound(payload)
+ assert len(calls) == cached_calls
+ assert len(captured) == dispatched