Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(backtest): guard local benchmark against swapped fallback loader

xor_xe8e4fd16646fc
diff --git a/agent/backtest/benchmark.py b/agent/backtest/benchmark.py
index 9591c8d7b..c9ed08815 100644
--- a/agent/backtest/benchmark.py
+++ b/agent/backtest/benchmark.py
@@ -66,11 +66,18 @@ def resolve_benchmark(
if ticker is None:
return None
+ offline = source == "local"
+ if offline and getattr(loader, "name", None) != source:
+ # The runtime fallback chain in fetch_data_map() may have swapped in a
+ # network loader while config["source"] still says local — never fetch
+ # the benchmark through it. Fail closed instead.
+ loader = None
+
try:
bench_df = _fetch_benchmark(
ticker, start_date, end_date, interval,
loader=loader,
- allow_fallback=source != "local",
+ allow_fallback=not offline,
)
except Exception:
return None
diff --git a/agent/tests/test_local_source_routing.py b/agent/tests/test_local_source_routing.py
index d500db2d1..f8081ea8e 100644
--- a/agent/tests/test_local_source_routing.py
+++ b/agent/tests/test_local_source_routing.py
@@ -37,6 +37,8 @@ class TestLocalSourceEngineRouting:
class _FakeLoader:
"""Loader stub returning a fixed close series for any requested code."""
+ name = "local"
+
def __init__(self, closes: List[float]) -> None:
self._closes = closes
self.fetched: List[str] = []
@@ -50,15 +52,29 @@ class _FakeLoader:
class _EmptyLoader:
+ name = "local"
+
def fetch(self, *args: object, **kwargs: object) -> Dict[str, pd.DataFrame]:
return {}
class _RaisingLoader:
+ name = "local"
+
def fetch(self, *args: object, **kwargs: object) -> Dict[str, pd.DataFrame]:
raise RuntimeError("boom")
+class _SwappedNetworkLoader:
+ """Simulates fetch_data_map's runtime fallback swapping in a network
+ loader while config['source'] still says local."""
+
+ name = "yahoo"
+
+ def fetch(self, *args: object, **kwargs: object) -> Dict[str, pd.DataFrame]:
+ raise AssertionError("network loader must not be fetched for source=local")
+
+
class TestBenchmarkLoaderForwarding:
def test_explicit_source_loader_is_used_instead_of_yfinance(
self, monkeypatch: pytest.MonkeyPatch,
@@ -83,12 +99,15 @@ class TestBenchmarkLoaderForwarding:
assert loader.fetched == ["AAPL.US"]
assert result.total_ret == pytest.approx(0.1)
- @pytest.mark.parametrize("loader", [_EmptyLoader(), _RaisingLoader(), None])
+ @pytest.mark.parametrize(
+ "loader", [_EmptyLoader(), _RaisingLoader(), _SwappedNetworkLoader(), None],
+ )
def test_local_source_fails_closed_without_yfinance(
self, monkeypatch: pytest.MonkeyPatch, loader: object,
) -> None:
- """source=local must never construct a yfinance loader, even when the
- local loader yields no benchmark data or raises."""
+ """source=local must never touch the network, even when the local
+ loader yields no benchmark data, raises, or was silently swapped for
+ a network loader by fetch_data_map's runtime fallback chain."""
def _no_network() -> None:
raise AssertionError("yfinance loader must not be created")