Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(channels): QQ construction no longer creates the media directory

shadowinlifec45d3c95e855
diff --git a/agent/src/channels/qq.py b/agent/src/channels/qq.py
index 74a98fa77..cb122ab04 100644
--- a/agent/src/channels/qq.py
+++ b/agent/src/channels/qq.py
@@ -177,7 +177,13 @@ class QQChannel(BaseChannel):
# ---------------------------
def _init_media_root(self) -> Path:
- """Choose a directory for saving inbound attachments."""
+ """Choose a directory for saving inbound attachments (side-effect free).
+
+ Ephemeral validation instances (web-config Test/Save) construct the
+ channel before anything is persisted and must not touch the
+ filesystem; the directory itself is created by ``_ensure_media_root``
+ from ``start()``.
+ """
if self.config.media_dir:
root = Path(self.config.media_dir).expanduser()
elif get_media_dir:
@@ -188,10 +194,13 @@ class QQChannel(BaseChannel):
else:
root = Path.home() / ".vibe-trading" / "media" / "qq"
- root.mkdir(parents=True, exist_ok=True)
self.logger.info("media directory: %s", str(root))
return root
+ def _ensure_media_root(self) -> None:
+ """Create the media root; called from ``start()``, never ``__init__``."""
+ self._media_root.mkdir(parents=True, exist_ok=True)
+
async def start(self) -> None:
"""Start the QQ bot with auto-reconnect loop."""
# (stdlib logging handles botpy SDK output via propagation)
@@ -203,6 +212,15 @@ class QQChannel(BaseChannel):
self.logger.error("app_id and secret not configured")
return
+ try:
+ self._ensure_media_root()
+ except OSError as exc:
+ # An unwritable media dir must not stop a text-capable bot;
+ # each download re-attempts the mkdir and fails on its own.
+ self.logger.warning(
+ "cannot create media directory %s: %s", self._media_root, exc
+ )
+
self._running = True
self._http = aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(total=120))
diff --git a/agent/tests/test_qq_connection_test.py b/agent/tests/test_qq_connection_test.py
index ac9a3b7c1..21deba306 100644
--- a/agent/tests/test_qq_connection_test.py
+++ b/agent/tests/test_qq_connection_test.py
@@ -41,8 +41,8 @@ def _make_channel(
) -> QQChannel:
"""Build a QQ channel that has NOT been started (no ``self._http``).
- ``QQChannel.__init__`` creates its media directory, so it is pinned under
- ``tmp_path`` to keep the test sandbox clean.
+ ``media_dir`` is pinned under ``tmp_path`` so any media-root creation
+ (``_ensure_media_root``, downloads) stays inside the test sandbox.
"""
return QQChannel(
{
@@ -283,6 +283,30 @@ def test_server_error_reports_network(
assert result["code"] == "network"
+# --------------------------------------------------------------------------- #
+# Construction side effects
+# --------------------------------------------------------------------------- #
+
+
+def test_construction_never_creates_the_media_directory(tmp_path: Path) -> None:
+ """Ephemeral validation instances must not touch the filesystem.
+
+ The web-config Test/Save routes construct the channel to validate a
+ section before anything is persisted; a constructor that mkdir'd a
+ user-controlled path created directories for configs that were then
+ rejected. Creation belongs to ``start()`` via ``_ensure_media_root``.
+ """
+ media = tmp_path / "qq-media"
+
+ channel = _make_channel(tmp_path)
+
+ assert channel._media_root == media
+ assert not media.exists()
+
+ channel._ensure_media_root()
+ assert media.is_dir()
+
+
# --------------------------------------------------------------------------- #
# Secret hygiene
# --------------------------------------------------------------------------- #