Skip to content

The agent that grows with you

fix(relay): restore voice-note STT — wire media[] MIMEs, message_type "voice", and a User-Agent for CDN downloads (#95274)

Ben Barclay29033a3fd583
diff --git a/gateway/relay/adapter.py b/gateway/relay/adapter.py
index d579c243d1e..c7ea8854362 100644
--- a/gateway/relay/adapter.py
+++ b/gateway/relay/adapter.py
@@ -1138,25 +1138,39 @@ class RelayAdapter(BasePlatformAdapter):
urls = list(getattr(event, "media_urls", None) or [])
if not urls:
return
+ # media_types is INDEXED IN PARALLEL with media_urls by every
+ # downstream classifier (_event_media_type_at). Any URL we drop or
+ # rewrite here must carry its MIME with it, or the surviving
+ # attachments inherit a neighbour's type and get mis-routed (an
+ # image classified by a PDF's mime is not treated as an image).
+ # Carry (url, mime) as PAIRS through the whole loop.
+ types = list(getattr(event, "media_types", None) or [])
+ pairs = [
+ (u, types[i] if i < len(types) else "") for i, u in enumerate(urls)
+ ]
client = self._get_media_client()
- localized: list[str] = []
- for url in urls:
+ localized: list[tuple[str, str]] = []
+ for url, mime in pairs:
if not isinstance(url, str) or not url:
continue
if client is None:
# No authenticated client: keep public URLs, drop re-hosts.
if "/relay/media/" not in url:
- localized.append(url)
+ localized.append((url, mime))
continue
path = await client.download(url)
if path:
- localized.append(path)
+ localized.append((path, mime))
elif "/relay/media/" not in url:
# A public URL that failed to download still has value as
# a URL (native adapters pass URLs to vision in some
# lanes); a dead re-host reference does not.
- localized.append(url)
- event.media_urls = localized
+ localized.append((url, mime))
+ event.media_urls = [u for u, _ in localized]
+ # Keep the parallel-array invariant: one mime slot per surviving
+ # url, always. A short/stale media_types would shift entries onto
+ # the wrong url the moment anything indexes or merges them.
+ event.media_types = [m for _, m in localized]
except Exception: # noqa: BLE001 - media localization must never break inbound
logger.debug("relay inbound media localization failed", exc_info=True)
diff --git a/gateway/relay/media.py b/gateway/relay/media.py
index 32da5490ef0..2ba218fd0ae 100644
--- a/gateway/relay/media.py
+++ b/gateway/relay/media.py
@@ -68,6 +68,14 @@ def media_base_url(relay_dial_url: str) -> str:
return raw
+# Discord's CDN (and other public hosts) reject urllib's default
+# ``Python-urllib/x.y`` User-Agent with HTTP 403 — which silently killed EVERY
+# Discord CDN pass-through download (voice notes, images, documents): the
+# localizer kept the raw URL and downstream consumers then tried to open a URL
+# as a file path. Always send a descriptive UA.
+_MEDIA_USER_AGENT = "HermesAgent-Relay/1.0 (+https://github.com/NousResearch/hermes-agent)"
+
+
class RelayMediaClient:
"""Authenticated client for the connector's ``/relay/media`` routes."""
@@ -128,6 +136,7 @@ class RelayMediaClient:
or "application/octet-stream"
)
headers = {
+ "User-Agent": _MEDIA_USER_AGENT,
"Authorization": f"Bearer {self._bearer()}",
"Content-Type": content_type,
"X-Media-Filename": (filename or path.name)[:255],
@@ -164,7 +173,7 @@ class RelayMediaClient:
needs_auth = self.is_relay_media_url(url)
if needs_auth and not self.enabled:
return None
- headers = {}
+ headers = {"User-Agent": _MEDIA_USER_AGENT}
if needs_auth:
headers["Authorization"] = f"Bearer {self._bearer()}"
diff --git a/gateway/relay/ws_transport.py b/gateway/relay/ws_transport.py
index 3f58069f282..5e90887c57a 100644
--- a/gateway/relay/ws_transport.py
+++ b/gateway/relay/ws_transport.py
@@ -209,6 +209,47 @@ def _normalize_slack_parent_command(
return normalized, normalized_type
+def _media_types_from_wire(raw: Dict[str, Any]) -> list[str]:
+ """Per-attachment MIME types, aligned to ``media_urls`` BY URL.
+
+ INVARIANT: the returned list is ALWAYS the same length as ``media_urls``
+ (padded with ``""``), or empty when there are no urls. Consumers index the
+ two lists by the same ``i`` (``_event_media_type_at``) AND concatenate them
+ pairwise (``merge_pending_message_event`` extends both), so a short
+ ``media_types`` is not a harmless absence — on a merge it shifts every
+ subsequent entry onto the wrong url.
+
+ Resolution is BY URL LOOKUP, never by position: ``media_urls`` (legacy,
+ flat) and ``media`` (rich, Phase 2) are independent wire fields that may
+ disagree in order, and a length check alone accepts a reordered pair —
+ attaching one attachment's MIME to another's url, which silently
+ mis-routes it. A url with no matching ``media[]`` entry keeps its slot as
+ ``""`` and falls back to message-level classification.
+ """
+ urls = raw.get("media_urls")
+ if not isinstance(urls, list) or not urls:
+ return []
+ media = raw.get("media")
+ mime_by_url: dict[str, str] = {}
+ if isinstance(media, list):
+ for m in media:
+ if isinstance(m, dict):
+ url = m.get("url")
+ if isinstance(url, str) and url:
+ mime_by_url[url] = m.get("mime") or ""
+ # One slot per url, ALWAYS — even with no media[] at all (all ""), so the
+ # parallel-array invariant holds for every consumer.
+ types = [mime_by_url.get(u, "") if isinstance(u, str) else "" for u in urls]
+ missing = sum(1 for t in types if not t)
+ if missing and mime_by_url:
+ logger.debug(
+ "relay inbound: %d/%d media_urls had no matching media[] mime",
+ missing,
+ len(types),
+ )
+ return types
+
+
def _event_from_wire(raw: Dict[str, Any]) -> MessageEvent:
"""Rebuild a MessageEvent from the connector's normalized inbound payload.
@@ -306,6 +347,23 @@ def _event_from_wire(raw: Dict[str, Any]) -> MessageEvent:
reply_to_author_name=(raw.get("reply_to") or {}).get("author"),
reply_to_is_own_message=bool((raw.get("reply_to") or {}).get("is_own", False)),
media_urls=raw.get("media_urls") or [],
+ # Per-attachment MIME types, parallel to media_urls, from the
+ # connector's rich media[] array. run.py's per-attachment classifiers
+ # (_event_media_type_at) consult media_types[i] FIRST and only fall
+ # back to the message-level type when it's empty — so this mapping is
+ # what lets a relayed image/document/audio attachment route exactly
+ # like its native-adapter equivalent (and what makes a kind:"voice"
+ # attachment STT-eligible). Entries without a mime keep positional
+ # alignment with an empty string.
+ #
+ # media_urls and media[] are independent wire fields. Today's
+ # connector builds both from the same list, but a malformed or
+ # future producer could disagree — and a LENGTH MISMATCH would
+ # silently misassociate a MIME with the wrong URL (worse than no
+ # MIME at all: it mis-routes an attachment). Fail safe: map only
+ # when the two agree, otherwise leave media_types empty and let the
+ # message-level type drive classification (pre-fix behaviour).
+ media_types=_media_types_from_wire(raw),
# Surrounding channel/group CONTEXT the connector attached for this
# addressed turn (design relay-channel-context): a read-only, oldest→
# newest list of nearby non-addressed messages (Model A pull / Model B
diff --git a/tests/gateway/relay/test_relay_media.py b/tests/gateway/relay/test_relay_media.py
index a7ee7b7f896..22ff3cfd2f0 100644
--- a/tests/gateway/relay/test_relay_media.py
+++ b/tests/gateway/relay/test_relay_media.py
@@ -176,3 +176,56 @@ async def test_client_upload_rejects_oversize_and_missing(tmp_path: Path):
empty = tmp_path / "empty.bin"
empty.write_bytes(b"")
assert await c.upload(str(empty)) is None
+
+async def test_download_sends_a_user_agent_on_every_request():
+ """Discord's CDN 403s the default ``Python-urllib/x.y`` User-Agent.
+
+ Live-verified on staging 2026-08-26: every Discord CDN pass-through
+ download failed with ``HTTP Error 403: Forbidden``, the localizer then kept
+ the raw URL, and the transcriber tried to open a URL as a file path
+ ("Audio file not found") — so voice notes, images and documents were ALL
+ silently dead on the Discord relay lane. Reproduced from a clean shell:
+ urllib with no UA → 403; the same URL with any descriptive UA → 200.
+
+ Assert the header on BOTH url classes (public pass-through and
+ bearer-authenticated re-host), because they take different header paths.
+ """
+ seen: list[dict] = []
+
+ class _Resp:
+ headers = {"Content-Type": "audio/ogg", "Content-Length": "4"}
+
+ def read(self, *_a):
+ return b"OggS"
+
+ def __enter__(self):
+ return self
+
+ def __exit__(self, *_a):
+ return False
+
+ def _fake_urlopen(req, timeout=None): # noqa: ARG001
+ seen.append(dict(req.headers))
+ return _Resp()
+
+ import urllib.request as _ur
+
+ orig = _ur.urlopen
+ _ur.urlopen = _fake_urlopen # type: ignore[assignment]
+ try:
+ c = RelayMediaClient("https://conn.example", "gw1", "sec")
+ assert await c.download("https://cdn.discordapp.com/attachments/1/2/v.ogg")
+ assert await c.download("https://conn.example/relay/media/deadbeef")
+ finally:
+ _ur.urlopen = orig # type: ignore[assignment]
+
+ assert len(seen) == 2
+ for headers in seen:
+ # urllib title-cases header keys on Request.
+ ua = headers.get("User-agent") or headers.get("User-Agent")
+ assert ua, f"no User-Agent sent; urllib would default to Python-urllib (403s on Discord CDN): {headers}"
+ assert "python-urllib" not in ua.lower()
+ # The re-host request must still carry its bearer (no regression).
+ rehost_headers = seen[1]
+ assert (rehost_headers.get("Authorization") or "").startswith("Bearer ")
diff --git a/tests/gateway/relay/test_wire_voice_media.py b/tests/gateway/relay/test_wire_voice_media.py
new file mode 100644
index 00000000000..833a3c51ad5
--- /dev/null
+++ b/tests/gateway/relay/test_wire_voice_media.py
@@ -0,0 +1,348 @@
+"""Unit tests for relay voice-note + per-attachment media-type mapping.
+
+The relay contract gained ``message_type: "voice"`` (connector PR: voice
+notes on Discord/Telegram/WhatsApp classify distinctly from audio-file
+uploads). The gateway side has two obligations:
+
+1. ``MessageType("voice")`` must parse — it already does, since the enum
+ predates the wire value — and
+2. the connector's rich ``media[]`` array (each entry carries the
+ per-attachment ``mime``) must land on ``event.media_types`` so run.py's
+ per-attachment classifiers (``_event_media_is_stt_input``, image vs
+ document routing) work for relayed events, not just native adapters.
+
+Live-verified failure (staging 2026-08-26): a voice note arrived as
+``MessageType.AUDIO`` with ``media_types == []`` — STT never fired and the
+agent got a "the user sent an audio file attachment" note instead.
+
+Pure unit tests: no socket, no websockets dependency.
+"""
+
+from __future__ import annotations
+
+from gateway.platforms.base import MessageType
+from gateway.relay.ws_transport import _event_from_wire
+
+
+def _wire_event(message_type: str, **extra):
+ src = {
+ "platform": "discord",
+ "chat_id": "chan-1",
+ "chat_type": "dm",
+ "user_id": "u-1",
+ "user_name": "ben",
+ }
+ return {
+ "text": "",
+ "message_type": message_type,
+ "source": src,
+ **extra,
+ }
+
+
+class TestVoiceMessageType:
+
+ def test_wire_voice_parses_to_message_type_voice(self):
+ ev = _event_from_wire(_wire_event("voice"))
+ assert ev.message_type == MessageType.VOICE
+
+ def test_wire_audio_still_parses_to_message_type_audio(self):
+ """Music files keep the non-STT type — the connector must not have
+ collapsed them (it doesn't), and the gateway must not either."""
+ ev = _event_from_wire(_wire_event("audio"))
+ assert ev.message_type == MessageType.AUDIO
+
+
+class TestMediaTypesMapping:
+
+ def test_media_mimes_land_on_event_media_types(self):
+ media = [
+ {
+ "url": "https://cdn.discordapp.com/attachments/1/2/voice-message.ogg",
+ "kind": "audio",
+ "mime": "audio/ogg",
+ "size": 19197,
+ "filename": "voice-message.ogg",
+ },
+ ]
+ ev = _event_from_wire(
+ _wire_event("voice", media=media, media_urls=[m["url"] for m in media])
+ )
+ assert ev.media_types == ["audio/ogg"]
+ # media_urls must remain the parallel legacy field (unchanged).
+ assert ev.media_urls == [media[0]["url"]]
+
+ def test_media_types_parallel_to_media_urls_for_multiple_attachments(self):
+ media = [
+ {"url": "https://x/photo.png", "kind": "image", "mime": "image/png"},
+ {"url": "https://x/doc.pdf", "kind": "document", "mime": "application/pdf"},
+ ]
+ ev = _event_from_wire(
+ _wire_event("image", media=media, media_urls=[m["url"] for m in media])
+ )
+ assert ev.media_types == ["image/png", "application/pdf"]
+ assert len(ev.media_types) == len(ev.media_urls)
+
+ def test_missing_mime_gives_empty_string_at_that_index(self):
+ """A media entry without a mime must not shift the alignment between
+ media_urls[i] and media_types[i] — run.py indexes both by position."""
+ media = [
+ {"url": "https://x/photo.png", "kind": "image", "mime": "image/png"},
+ {"url": "https://x/blob", "kind": "document"}, # no mime
+ ]
+ ev = _event_from_wire(
+ _wire_event("image", media=media, media_urls=[m["url"] for m in media])
+ )
+ assert ev.media_types == ["image/png", ""]
+
+ def test_no_media_field_means_empty_media_types(self):
+ """Older connectors (no media[]) — byte-identical to pre-fix."""
+ ev = _event_from_wire(_wire_event("text"))
+ assert ev.media_types == []
+
+ def test_length_mismatch_resolves_by_url_without_misaligning(self):
+ """media_urls and media[] are independent wire fields; consumers index
+ BOTH by the same i. Resolution is BY URL, so a length disagreement can
+ no longer misalign anything: each surviving URL keeps its own mime and
+ an unmatched URL degrades to "" (message-level classification)."""
+ media = [
+ {"url": "https://x/a.png", "kind": "image", "mime": "image/png"},
+ {"url": "https://x/b.pdf", "kind": "document", "mime": "application/pdf"},
+ ]
+ ev = _event_from_wire(
+ _wire_event("image", media=media, media_urls=["https://x/a.png"])
+ )
+ assert ev.media_urls == ["https://x/a.png"]
+ assert ev.media_types == ["image/png"]
+
+
+class TestSttGate:
+ """Direct assertions on run.py's STT gate against REAL wire-parsed
+ events. The PR's acceptance criterion is STT routing, not object
+ construction — pin the user-visible invariant here.
+
+ Note the deployment-ordering consequence: MessageType.VOICE parses on
+ ANY gateway (the enum predates this PR) and the gate accepts VOICE with
+ empty media_types, so a NEW connector + OLD gateway already fires STT
+ for voice notes. Desirable — but it means the gate's behaviour is part
+ of the wire contract, and it must not silently change."""
+
+ def test_new_connector_voice_event_is_stt_eligible(self):
+ from gateway.run import _event_media_is_stt_input
+
+ ev = _event_from_wire(
+ _wire_event(
+ "voice",
+ media=[{"url": "https://gw/relay/media/x", "kind": "voice", "mime": "audio/ogg"}],
+ media_urls=["https://gw/relay/media/x"],
+ )
+ )
+ assert _event_media_is_stt_input(ev, 0) is True
+
+ def test_voice_event_stt_eligible_even_without_media_types(self):
+ """A new-connector/old-gateway-shaped event — voice type, no
+ media[] — still fires STT (the gate's VOICE branch doesn't consult
+ media_types). Pins the rollout behaviour, not just the new one."""
+ from gateway.run import _event_media_is_stt_input
+
+ ev = _event_from_wire(
+ _wire_event("voice", media_urls=["https://gw/relay/media/x"])
+ )
+ # No media[] ⇒ no per-attachment mime, but the slot still exists so the
+ # parallel-array invariant holds (see TestParallelArrayLengthInvariant).
+ assert ev.media_types == [""]
+ assert _event_media_is_stt_input(ev, 0) is True
+
+ def test_legacy_audio_typed_voice_note_stays_out_of_stt(self):
+ from gateway.run import _event_media_is_stt_input
+
+ ev = _event_from_wire(
+ _wire_event(
+ "audio",
+ media=[{"url": "https://gw/relay/media/x", "kind": "voice", "mime": "audio/ogg"}],
+ media_urls=["https://gw/relay/media/x"],
+ )
+ )
+ assert _event_media_is_stt_input(ev, 0) is False
+
+ def test_music_upload_is_never_stt_eligible(self):
+ from gateway.run import _event_media_is_stt_input
+
+ ev = _event_from_wire(
+ _wire_event(
+ "audio",
+ media=[{"url": "https://cdn/x/song.mp3", "kind": "audio", "mime": "audio/mpeg"}],
+ media_urls=["https://cdn/x/song.mp3"],
+ )
+ )
+ assert _event_media_is_stt_input(ev, 0) is False
+
+class TestUrlMimePairingThroughLocalization:
+ """The end-to-end invariant my unit tests originally missed.
+
+ media_urls and media_types are indexed BY POSITION by every downstream
+ classifier. _localize_inbound_media drops entries (a dead connector
+ re-host) as a NORMAL best-effort path — so if it filters URLs without
+ filtering MIMEs in lockstep, every surviving attachment inherits a
+ neighbour's type. Drive the REAL path: wire parse -> localization ->
+ run.py classifier."""
+
+ def _adapter(self):
+ from gateway.relay.adapter import RelayAdapter
+
+ return RelayAdapter.__new__(RelayAdapter)
+
+ def test_dropped_first_attachment_does_not_shift_the_second_mime(self):
+ import asyncio
+
+ from gateway.run import _event_media_is_image, _event_media_type_at
+
+ rehost = "https://conn.example/relay/media/dead"
+ kept = "https://cdn.discordapp.com/attachments/1/2/kept.png"
+ ev = _event_from_wire(
+ _wire_event(
+ "document",
+ media=[
+ {"url": rehost, "kind": "document", "mime": "application/pdf"},
+ {"url": kept, "kind": "image", "mime": "image/png"},
+ ],
+ media_urls=[rehost, kept],
+ )
+ )
+ assert ev.media_types == ["application/pdf", "image/png"]
+
+ adapter = self._adapter()
+ adapter._media_client = None
+ adapter._get_media_client = lambda: None # type: ignore[method-assign]
+ asyncio.run(adapter._localize_inbound_media(ev))
+
+ # The dead re-host is dropped; the PNG must keep ITS OWN mime.
+ assert ev.media_urls == [kept]
+ assert ev.media_types == ["image/png"]
+ assert _event_media_type_at(ev, 0) == "image/png"
+ assert _event_media_is_image(ev, 0) is True
+
+ def test_reordered_media_vs_media_urls_resolves_by_url_not_position(self):
+ """Equal-length but differently-ordered wire fields must not pair up
+ positionally — resolve each URL's mime by lookup."""
+ png = "https://x/a.png"
+ pdf = "https://x/b.pdf"
+ ev = _event_from_wire(
+ _wire_event(
+ "image",
+ media=[
+ {"url": pdf, "mime": "application/pdf"},
+ {"url": png, "mime": "image/png"},
+ ],
+ media_urls=[png, pdf],
+ )
+ )
+ assert ev.media_types == ["image/png", "application/pdf"]
+
+ def test_url_with_no_matching_media_entry_gets_empty_mime(self):
+ known = "https://x/a.png"
+ orphan = "https://x/unknown.bin"
+ ev = _event_from_wire(
+ _wire_event(
+ "image",
+ media=[{"url": known, "mime": "image/png"}],
+ media_urls=[known, orphan],
+ )
+ )
+ assert ev.media_types == ["image/png", ""]
+
+ def test_media_without_media_urls_yields_no_types(self):
+ """No URL list to align to ⇒ the indices are meaningless."""
+ ev = _event_from_wire(
+ _wire_event("image", media=[{"url": "https://x/a.png", "mime": "image/png"}])
+ )
+ assert ev.media_urls == []
+ assert ev.media_types == []
+
+class TestParallelArrayLengthInvariant:
+ """media_types must ALWAYS have one slot per media_url.
+
+ Not merely an indexing nicety: ``merge_pending_message_event``
+ (gateway/platforms/base.py) EXTENDS both lists when a second media message
+ is merged into a pending one. If a media_types-less event merges with a
+ typed one, extend() concatenates lists of different lengths and shifts
+ every later mime onto the wrong url. Found by self-review after two
+ rounds of external review flagged this same bug class in adjacent seams."""
+
+ def test_media_urls_without_media_still_get_one_empty_slot_each(self):
+ """An older connector sends media_urls with no media[]. Padding keeps
+ the invariant instead of emitting a length-0 media_types."""
+ ev = _event_from_wire(
+ _wire_event("image", media_urls=["https://x/a.png", "https://x/b.png"])
+ )
+ assert ev.media_types == ["", ""]
+ assert len(ev.media_types) == len(ev.media_urls)
+
+ def test_merging_an_untyped_event_with_a_typed_one_keeps_mimes_on_their_urls(self):
+ from gateway.platforms.base import merge_pending_message_event
+ from gateway.run import _event_media_type_at
+
+ untyped = _event_from_wire(
+ _wire_event("image", media_urls=["https://x/old1.png", "https://x/old2.png"])
+ )
+ typed = _event_from_wire(
+ _wire_event(
+ "document",
+ media=[{"url": "https://x/new.pdf", "mime": "application/pdf"}],
+ media_urls=["https://x/new.pdf"],
+ )
+ )
+ pending = {"k": untyped}
+ merge_pending_message_event(pending, "k", typed)
+ merged = pending["k"]
+
+ assert len(merged.media_types) == len(merged.media_urls)
+ # The PDF's mime must stay on the PDF, not slide onto old1.png.
+ assert _event_media_type_at(merged, 0) == ""
+ assert _event_media_type_at(merged, 1) == ""
+ assert _event_media_type_at(merged, 2) == "application/pdf"
+
+ def test_localization_preserves_the_invariant_when_it_drops_entries(self):
+ import asyncio
+
+ from gateway.relay.adapter import RelayAdapter
+
+ rehost = "https://conn.example/relay/media/dead"
+ kept = "https://x/kept.png"
+ ev = _event_from_wire(
+ _wire_event("image", media_urls=[rehost, kept]) # no media[] at all
+ )
+ assert ev.media_types == ["", ""]
+
+ adapter = RelayAdapter.__new__(RelayAdapter)
+ adapter._media_client = None
+ adapter._get_media_client = lambda: None # type: ignore[method-assign]
+ asyncio.run(adapter._localize_inbound_media(ev))
+
+ assert ev.media_urls == [kept]
+ assert len(ev.media_types) == len(ev.media_urls)
+
+ def test_localization_normalizes_a_short_media_types_from_any_source(self):
+ """Defence in depth: an event that reaches the localizer with a SHORT
+ media_types (not produced by _event_from_wire — e.g. a synthetic or
+ replayed event) must come out with one slot per surviving url, not
+ with the short list passed through."""
+ import asyncio
+
+ from gateway.platforms.base import MessageEvent, MessageType
+ from gateway.relay.adapter import RelayAdapter
+
+ ev = MessageEvent(text="", message_type=MessageType.PHOTO)
+ ev.media_urls = ["https://x/a.png", "https://x/b.png"]
+ ev.media_types = [] # empty, while urls exist — the shape that must
+ # NOT be passed through: consumers would index/merge against a
+ # zero-length mime list and shift every later entry.
+
+ adapter = RelayAdapter.__new__(RelayAdapter)
+ adapter._media_client = None
+ adapter._get_media_client = lambda: None # type: ignore[method-assign]
+ asyncio.run(adapter._localize_inbound_media(ev))
+
+ assert ev.media_urls == ["https://x/a.png", "https://x/b.png"]
+ assert ev.media_types == ["", ""]
+ assert len(ev.media_types) == len(ev.media_urls)