Skip to content

The agent that grows with you

refactor(gateway): move rehome_inbound_media into run_inbound_media.py

Tekniume70a02a02968
diff --git a/gateway/run_inbound.py b/gateway/run_inbound.py
index 94615f39537..171e536612b 100644
--- a/gateway/run_inbound.py
+++ b/gateway/run_inbound.py
@@ -15,7 +15,6 @@ import dataclasses
import json
import os
import re
-import shutil
import time
from contextlib import suppress
from pathlib import Path
@@ -26,6 +25,7 @@ from gateway.platforms.base import EphemeralReply
from gateway.platforms.event import MessageEvent, MessageType
from gateway.run_busy import approval_input_words
from gateway.run_common import _UNSET
+from gateway.run_inbound_media import rehome_inbound_media
from gateway.run_inbound_unauthorized import (
UnauthorizedOwnerNotifier, pairing_code_reply, pairing_profile_arg, pairing_rate_limited_reply,
unauthorized_owner_hint,
@@ -45,45 +45,6 @@ if TYPE_CHECKING: # string annotations only; never imported at runtime (cycle)
logger = logging.getLogger("gateway.run")
-def rehome_inbound_media(event: MessageEvent) -> None:
- """Move adapter-cached attachments into the ACTIVE profile's ``cache/`` and repoint the event.
-
- Adapters download and cache an attachment BEFORE the gateway routes the event to a profile, so
- on a multiplexed gateway the file lands under the launch home while the routed turn's sandbox
- mounts (``get_cache_directory_mounts``) and vision's ``_media_cache_roots`` resolve the routed
- profile's ``cache/`` — the agent is handed a mounted, empty directory (#101134). Runs inside the
- routed scope at the shared preprocessing choke point (every adapter, every media kind); a no-op
- when the active home is the launch home, and idempotent (a moved entry is no longer under it).
- """
- if not event.media_urls:
- return
- from hermes_constants import get_hermes_home, get_routing_process_hermes_home, hermes_home_key
- active, launch = Path(get_hermes_home()), Path(get_routing_process_hermes_home())
- if hermes_home_key(active) == hermes_home_key(launch):
- return
- from tools.credential_files import to_agent_visible_cache_path
- rewritten = list(event.media_urls)
- for i, raw in enumerate(event.media_urls):
- src = Path(raw)
- try:
- rel = src.relative_to(launch / "cache")
- except ValueError:
- continue
- dest = active / "cache" / rel
- try:
- if not src.is_file():
- continue
- dest.parent.mkdir(parents=True, exist_ok=True)
- shutil.move(str(src), str(dest))
- except OSError:
- logger.warning("Could not move inbound attachment %s into the routed profile's cache", raw, exc_info=True)
- continue
- rewritten[i] = str(dest)
- if event.text and raw in event.text: # note an adapter already baked in (observed/replied media)
- event.text = event.text.replace(raw, to_agent_visible_cache_path(str(dest)))
- event.media_urls = rewritten
-
-
def discord_triggering_note(message_id: Any) -> str:
"""Model-facing routing note for a Discord turn (rides the API-bound user message only)."""
return (
diff --git a/gateway/run_inbound_media.py b/gateway/run_inbound_media.py
new file mode 100644
index 00000000000..bddb4ba74b0
--- /dev/null
+++ b/gateway/run_inbound_media.py
@@ -0,0 +1,51 @@
+"""Inbound attachment re-homing for multiplexed gateways (moved out of ``gateway/run_inbound.py``)."""
+
+from __future__ import annotations
+
+import logging
+import shutil
+from pathlib import Path
+
+from gateway.platforms.event import MessageEvent
+
+# Log-record parity with the origin module.
+logger = logging.getLogger("gateway.run")
+
+
+def rehome_inbound_media(event: MessageEvent) -> None:
+ """Move adapter-cached attachments into the ACTIVE profile's ``cache/`` and repoint the event.
+
+ Adapters download and cache an attachment BEFORE the gateway routes the event to a profile, so
+ on a multiplexed gateway the file lands under the launch home while the routed turn's sandbox
+ mounts (``get_cache_directory_mounts``) and vision's ``_media_cache_roots`` resolve the routed
+ profile's ``cache/`` — the agent is handed a mounted, empty directory (#101134). Runs inside the
+ routed scope at the shared preprocessing choke point (every adapter, every media kind); a no-op
+ when the active home is the launch home, and idempotent (a moved entry is no longer under it).
+ """
+ if not event.media_urls:
+ return
+ from hermes_constants import get_hermes_home, get_routing_process_hermes_home, hermes_home_key
+ active, launch = Path(get_hermes_home()), Path(get_routing_process_hermes_home())
+ if hermes_home_key(active) == hermes_home_key(launch):
+ return
+ from tools.credential_files import to_agent_visible_cache_path
+ rewritten = list(event.media_urls)
+ for i, raw in enumerate(event.media_urls):
+ src = Path(raw)
+ try:
+ rel = src.relative_to(launch / "cache")
+ except ValueError:
+ continue
+ dest = active / "cache" / rel
+ try:
+ if not src.is_file():
+ continue
+ dest.parent.mkdir(parents=True, exist_ok=True)
+ shutil.move(str(src), str(dest))
+ except OSError:
+ logger.warning("Could not move inbound attachment %s into the routed profile's cache", raw, exc_info=True)
+ continue
+ rewritten[i] = str(dest)
+ if event.text and raw in event.text: # note an adapter already baked in (observed/replied media)
+ event.text = event.text.replace(raw, to_agent_visible_cache_path(str(dest)))
+ event.media_urls = rewritten
diff --git a/tests/gateway/test_inbound_media_profile_scope.py b/tests/gateway/test_inbound_media_profile_scope.py
index 05b506499fb..aa031d82bdd 100644
--- a/tests/gateway/test_inbound_media_profile_scope.py
+++ b/tests/gateway/test_inbound_media_profile_scope.py
@@ -16,7 +16,8 @@ from agent import secret_scope as ss
from gateway.platforms.base import cache_media_bytes
from gateway.platforms.event import MessageEvent, MessageType
from gateway.run import _profile_runtime_scope
-from gateway.run_inbound import GatewayInboundMixin, rehome_inbound_media
+from gateway.run_inbound import GatewayInboundMixin
+from gateway.run_inbound_media import rehome_inbound_media
from tools.credential_files import from_agent_visible_cache_path, get_cache_directory_mounts