"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(channels): save inbound IM media under the allowed uploads root (#465)
Haozhe Wu3e0d95b4696f
diff --git a/agent/src/channels/matrix.py b/agent/src/channels/matrix.pyindex 103ef7815..bf59316f1 100644--- a/agent/src/channels/matrix.py+++ b/agent/src/channels/matrix.py@@ -51,7 +51,8 @@ except ImportError as e:from src.channels.bus.events import OutboundMessagefrom src.channels.bus.queue import MessageBusfrom src.channels.base import BaseChannel-from src.channels.utils import get_media_dir+from src.channels.utils import get_media_dir, get_runtime_subdir+from src.config.paths import get_data_dirfrom pydantic import BaseModelfrom src.channels.utils import safe_filenamefrom src.utils.logging_bridge import redirect_lib_logging@@ -256,7 +257,16 @@ class MatrixChannel(BaseChannel):self._started_at_ms = int(time.time() * 1000)redirect_lib_logging("nio", level="WARNING")- self.store_path = get_media_dir("matrix") / "matrix-store"+ # The nio E2E store + session.json hold credentials, so they live under+ # the runtime dir (NOT the agent-readable uploads root that+ # get_media_dir now returns). Deployments that already have a store at+ # the pre-#465 location keep using it so E2E sessions survive.+ legacy_store = get_data_dir() / "matrix" / "matrix-store"+ self.store_path = (+ legacy_store+ if legacy_store.exists()+ else get_runtime_subdir("matrix") / "matrix-store"+ )self.store_path.mkdir(parents=True, exist_ok=True)self.session_path = self.store_path / "session.json"diff --git a/agent/src/channels/utils.py b/agent/src/channels/utils.pyindex 251bbe58a..8c792e10f 100644--- a/agent/src/channels/utils.py+++ b/agent/src/channels/utils.py@@ -14,8 +14,13 @@ _UNSAFE_CHARS = re.compile(r"[/\\:*?\"<>|]")def get_media_dir(channel_name: str) -> Path:- """Return a media directory for *channel_name* under the VT data dir."""- p = get_data_dir() / channel_name+ """Return a media directory for *channel_name* under the VT uploads root.++ Inbound media must land inside ``~/.vibe-trading/uploads`` — one of the+ default allowed file roots — so the agent's file-reading tools can open+ what users send over IM channels without extra configuration (#465).+ """+ p = get_data_dir() / "uploads" / channel_namep.mkdir(parents=True, exist_ok=True)return pdiff --git a/agent/tests/test_channel_media_roots.py b/agent/tests/test_channel_media_roots.pynew file mode 100644index 000000000..8208a7e04--- /dev/null+++ b/agent/tests/test_channel_media_roots.py@@ -0,0 +1,27 @@+"""Inbound IM media must be saved where the agent's read tools can open it (#465)."""++from __future__ import annotations++from pathlib import Path+++def test_media_dir_lives_under_uploads_root(tmp_path, monkeypatch):+ monkeypatch.setattr(Path, "home", classmethod(lambda cls: tmp_path), raising=False)++ from src.channels.utils import get_media_dir++ media = get_media_dir("napcat")+ assert media == tmp_path / ".vibe-trading" / "uploads" / "napcat"+ assert media.is_dir()+++def test_media_dir_is_within_allowed_file_roots(tmp_path, monkeypatch):+ monkeypatch.setattr(Path, "home", classmethod(lambda cls: tmp_path), raising=False)++ from src.channels.utils import get_media_dir+ from src.tools.path_utils import allowed_file_roots++ media = get_media_dir("weixin").resolve()+ assert any(+ root == media or root in media.parents for root in allowed_file_roots()+ ), f"{media} is not readable under any allowed file root"