Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(loaders): keep a stubbed config from writing cache into the working tree

Haozhe Wu612004243c62
diff --git a/agent/backtest/loaders/base.py b/agent/backtest/loaders/base.py
index dc74ef5a9..e19dca51b 100644
--- a/agent/backtest/loaders/base.py
+++ b/agent/backtest/loaders/base.py
@@ -249,18 +249,34 @@ _LOADER_CACHE_VERSION = 3
def loader_cache_enabled() -> bool:
- """Return whether the local market-data cache is explicitly enabled."""
+ """Return whether the local market-data cache is explicitly enabled.
+
+ Returns:
+ True only when the config yields a real ``True``. Any other value —
+ including a truthy non-bool from a stubbed config — leaves the opt-in
+ cache off.
+ """
from src.config.accessor import get_env_config
- return get_env_config().data.vibe_trading_data_cache
+ return get_env_config().data.vibe_trading_data_cache is True
def loader_cache_root() -> Path:
- """Return the root directory for opt-in loader cache files."""
+ """Return the root directory for opt-in loader cache files.
+
+ The configured override is honored only when it is a genuine non-blank
+ ``str``. A non-string value (e.g. a stubbed config in tests) would
+ otherwise reach ``Path()`` via ``__fspath__`` and yield a *relative* path,
+ which resolves against the CWD and writes market data inside the working
+ tree — the repository forbids caching data in the repo.
+
+ Returns:
+ The configured cache root, or the default under the user's home.
+ """
from src.config.accessor import get_env_config
root = get_env_config().data.vibe_trading_data_cache_root
- if root and root.strip():
+ if isinstance(root, str) and root.strip():
return Path(root).expanduser()
return Path.home() / ".vibe-trading" / "cache" / "loaders"
diff --git a/agent/tests/test_finnhub_interval_reject.py b/agent/tests/test_finnhub_interval_reject.py
index d04455ad8..1fb1a2c34 100644
--- a/agent/tests/test_finnhub_interval_reject.py
+++ b/agent/tests/test_finnhub_interval_reject.py
@@ -13,6 +13,10 @@ def test_unsupported_interval_does_not_hit_api() -> None:
with patch("src.config.accessor.get_env_config") as cfg:
c = MagicMock()
c.data.finnhub_api_key = "KEY"
+ # Explicit: a bare MagicMock reads truthy, which would switch the
+ # opt-in loader cache on and write parquet into the working tree.
+ c.data.vibe_trading_data_cache = False
+ c.data.vibe_trading_data_cache_root = ""
cfg.return_value = c
out = fh.DataLoader().fetch(
["AAPL"], "2024-01-01", "2024-01-31", interval="1H"
@@ -26,6 +30,10 @@ def test_four_hour_interval_also_rejected() -> None:
with patch("src.config.accessor.get_env_config") as cfg:
c = MagicMock()
c.data.finnhub_api_key = "KEY"
+ # Explicit: a bare MagicMock reads truthy, which would switch the
+ # opt-in loader cache on and write parquet into the working tree.
+ c.data.vibe_trading_data_cache = False
+ c.data.vibe_trading_data_cache_root = ""
cfg.return_value = c
out = fh.DataLoader().fetch(
["AAPL"], "2024-01-01", "2024-01-31", interval="4H"
@@ -48,6 +56,10 @@ def test_daily_interval_still_fetches() -> None:
with patch("src.config.accessor.get_env_config") as cfg:
c = MagicMock()
c.data.finnhub_api_key = "KEY"
+ # Explicit: a bare MagicMock reads truthy, which would switch the
+ # opt-in loader cache on and write parquet into the working tree.
+ c.data.vibe_trading_data_cache = False
+ c.data.vibe_trading_data_cache_root = ""
cfg.return_value = c
out = fh.DataLoader().fetch(
["AAPL"], "2024-01-01", "2024-01-31", interval="1D"
diff --git a/agent/tests/test_loader_retry_helpers.py b/agent/tests/test_loader_retry_helpers.py
index e2f61b732..52f349c92 100644
--- a/agent/tests/test_loader_retry_helpers.py
+++ b/agent/tests/test_loader_retry_helpers.py
@@ -251,6 +251,46 @@ def test_loader_cache_disabled_by_default_bypasses_home(tmp_path, monkeypatch):
assert not (home / ".vibe-trading").exists()
+def test_stubbed_config_cannot_enable_cache_or_redirect_root_into_cwd(monkeypatch, tmp_path):
+ """A MagicMock config must not switch the cache on or escape the home root.
+
+ A bare ``MagicMock`` reads truthy and its ``__fspath__`` returns a
+ *relative* string, so an unguarded implementation both enabled the opt-in
+ cache and resolved its root against the CWD — writing market data inside
+ the working tree, which the project forbids.
+ """
+ from unittest.mock import MagicMock
+
+ home = tmp_path / "home"
+ monkeypatch.setenv("HOME", str(home))
+ stub = MagicMock()
+ monkeypatch.setattr(
+ "src.config.accessor.get_env_config", lambda: stub, raising=False
+ )
+
+ assert base.loader_cache_enabled() is False
+
+ root = base.loader_cache_root()
+ assert root.is_absolute()
+ assert root == home / ".vibe-trading" / "cache" / "loaders"
+
+
+def test_loader_cache_root_honors_real_string_override(monkeypatch, tmp_path):
+ """A genuine string override is still respected."""
+ stub = SimpleNamespace(
+ data=SimpleNamespace(
+ vibe_trading_data_cache=True,
+ vibe_trading_data_cache_root=str(tmp_path / "custom"),
+ )
+ )
+ monkeypatch.setattr(
+ "src.config.accessor.get_env_config", lambda: stub, raising=False
+ )
+
+ assert base.loader_cache_enabled() is True
+ assert base.loader_cache_root() == tmp_path / "custom"
+
+
def test_loader_cache_key_partitions_source_symbol_timeframe_date_and_fields():
base_args = {
"source": "tushare",