Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(swarm): route grounding fetch through _detect_market(code)

Teerapat Vatpitak5731868a2097
diff --git a/agent/src/swarm/grounding.py b/agent/src/swarm/grounding.py
index f3c7b84bd..632fedf28 100644
--- a/agent/src/swarm/grounding.py
+++ b/agent/src/swarm/grounding.py
@@ -110,13 +110,18 @@ def fetch_grounding_data(
# Imported lazily so unit tests of the extraction / formatting layer
# don't have to drag in pandas + the loader graph just to import.
+ # ``resolve_loader`` expects a *market* key (``"us_equity"`` etc.), not a
+ # raw code; ``_detect_market`` is the function ``runner.py`` already uses
+ # to dispatch the same shapes we extract here, so reusing it keeps the
+ # routing identical to the rest of the codebase.
from backtest.loaders.registry import resolve_loader
+ from backtest.runner import _detect_market
out: dict[str, list[dict]] = {}
for code in symbols_list:
try:
- loader_cls = resolve_loader(code)
- loader = loader_cls()
+ market = _detect_market(code)
+ loader = resolve_loader(market) # already a ready-to-use instance
df_map = loader.fetch([code], start_str, end_str, interval="1D")
except Exception as exc: # pragma: no cover — depends on network
logger.warning(
diff --git a/agent/tests/test_swarm_grounding.py b/agent/tests/test_swarm_grounding.py
index bc53316f6..ae339aece 100644
--- a/agent/tests/test_swarm_grounding.py
+++ b/agent/tests/test_swarm_grounding.py
@@ -98,18 +98,25 @@ def _three_bar_frame() -> pd.DataFrame:
def test_fetch_returns_normalized_bars(monkeypatch) -> None:
+ """Real call path: ``_detect_market(code)`` → ``resolve_loader(market)``
+ returns a ready loader instance. The stub mirrors that contract so a
+ regression that drops or rewrites the dispatch shows up here.
+ """
frame = _three_bar_frame()
- monkeypatch.setattr(
- grounding,
- "fetch_grounding_data",
- grounding.fetch_grounding_data, # keep real fn, only stub the loader
- )
- # Patch the loader registry that fetch_grounding_data imports lazily.
import backtest.loaders.registry as reg
- monkeypatch.setattr(reg, "resolve_loader", lambda code: lambda: _StubLoader(frame))
+ captured_markets: list[str] = []
+
+ def _fake_resolve(market: str):
+ captured_markets.append(market)
+ return _StubLoader(frame)
+
+ monkeypatch.setattr(reg, "resolve_loader", _fake_resolve)
bars = grounding.fetch_grounding_data(["NVDA.US"], today=date(2026, 5, 9))
+ # ``NVDA.US`` must dispatch through the us_equity branch — guards
+ # against a regression where the code is passed as the market key.
+ assert captured_markets == ["us_equity"]
assert "NVDA.US" in bars
rows = bars["NVDA.US"]
assert len(rows) == 3
@@ -121,7 +128,7 @@ def test_fetch_skips_symbols_with_no_data(monkeypatch) -> None:
import backtest.loaders.registry as reg
monkeypatch.setattr(
reg, "resolve_loader",
- lambda code: lambda: _StubLoader(pd.DataFrame()), # empty frame
+ lambda market: _StubLoader(pd.DataFrame()), # empty frame
)
bars = grounding.fetch_grounding_data(["NOPE.US"])