Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix: harden free market data failure handling

Haozhe Wua71d5312d9fc
diff --git a/agent/backtest/loader_health.py b/agent/backtest/loader_health.py
index 326532b86..15b2e3598 100644
--- a/agent/backtest/loader_health.py
+++ b/agent/backtest/loader_health.py
@@ -33,8 +33,11 @@ CANARY_SYMBOLS = {
"stooq": "AAPL.US",
"tencent": "601398.SH",
"wallex": "BTC-TMN",
- "yahoo": "AAPL",
- "yfinance": "AAPL",
+ # These loaders require the project's explicit US suffix; using bare
+ # ``AAPL`` here made the canary report empty data while real ``AAPL.US``
+ # requests were healthy.
+ "yahoo": "AAPL.US",
+ "yfinance": "AAPL.US",
}
EXCLUDED_PUBLIC_SOURCES = {"local": "operator files, not a public endpoint"}
DEPENDENCIES = {
diff --git a/agent/backtest/runner.py b/agent/backtest/runner.py
index d2c719dc7..1d9278f9a 100644
--- a/agent/backtest/runner.py
+++ b/agent/backtest/runner.py
@@ -1728,8 +1728,10 @@ def _fetch_auto(codes: List[str], config: dict, interval: str = "1D") -> dict:
)
if missing:
+ served = sorted(set(merged) - set(missing))
raise NoAvailableSourceError(
- f"incomplete data for {market}; missing symbols: {missing}"
+ f"incomplete data for {market}; missing symbols: {missing}; "
+ f"served symbols: {served}"
)
merged.update(market_result)
@@ -1848,51 +1850,56 @@ def fetch_data_map(config: dict) -> DataFetchResult:
# lines as a trace. Callers want snapshot provenance, not a padded row
# count.
if missing and not is_no_network_fallback_source(primary_source):
- market = _detect_market(codes[0])
- for fallback_source in FALLBACK_CHAINS.get(market, []):
- if not missing:
- break
- if (
- fallback_source == primary_source
- or fallback_source not in LOADER_REGISTRY
- ):
- continue
- fallback_loader = LOADER_REGISTRY[fallback_source]()
- if not fallback_loader.is_available():
- continue
- fallback_codes = _normalize_codes(missing, fallback_source)
- fallback_result = fallback_loader.fetch(
- fallback_codes,
- config.get("start_date", ""),
- config.get("end_date", ""),
- interval=interval,
- )
- mapped = _restore_original_codes(
- fallback_result, missing, fallback_codes
- )
- if mapped:
- data_map.update(mapped)
- missing = [code for code in missing if code not in mapped]
- fb_served_by = str(
- getattr(fallback_loader, "name", fallback_source)
- or fallback_source
+ missing_by_market: dict[str, list[str]] = {}
+ for code in missing:
+ missing_by_market.setdefault(_detect_market(code), []).append(code)
+ for market, market_missing in missing_by_market.items():
+ for fallback_source in FALLBACK_CHAINS.get(market, []):
+ if not market_missing:
+ break
+ if (
+ fallback_source == primary_source
+ or fallback_source not in LOADER_REGISTRY
+ ):
+ continue
+ fallback_loader = LOADER_REGISTRY[fallback_source]()
+ if not fallback_loader.is_available():
+ continue
+ fallback_codes = _normalize_codes(market_missing, fallback_source)
+ fallback_result = fallback_loader.fetch(
+ fallback_codes,
+ config.get("start_date", ""),
+ config.get("end_date", ""),
+ interval=interval,
)
- for code in mapped:
- caliber_stamps[code] = (
- fb_served_by,
- price_caliber(fb_served_by, _detect_market(code), code),
- )
- if not used_sources:
- source = fb_served_by
- loader = fallback_loader
- used_sources.append(fb_served_by)
- logger.info(
- "Runtime fallback: %s -> %s", primary_source, fb_served_by
+ mapped = _restore_original_codes(
+ fallback_result, market_missing, fallback_codes
)
+ if mapped:
+ data_map.update(mapped)
+ market_missing = [code for code in market_missing if code not in mapped]
+ fb_served_by = str(
+ getattr(fallback_loader, "name", fallback_source)
+ or fallback_source
+ )
+ for code in mapped:
+ caliber_stamps[code] = (
+ fb_served_by,
+ price_caliber(fb_served_by, market, code),
+ )
+ if not used_sources:
+ source = fb_served_by
+ loader = fallback_loader
+ used_sources.append(fb_served_by)
+ logger.info(
+ "Runtime fallback: %s -> %s", primary_source, fb_served_by
+ )
+ missing = [code for code in missing if code not in data_map]
if missing:
raise NoAvailableSourceError(
- f"incomplete data for source={primary_source}; missing symbols: {missing}"
+ f"incomplete data for source={primary_source}; missing symbols: {missing}; "
+ f"served symbols: {sorted(data_map)}"
)
data_map = {
diff --git a/agent/src/tools/market_data_tool.py b/agent/src/tools/market_data_tool.py
index 3a07edd18..95702492e 100644
--- a/agent/src/tools/market_data_tool.py
+++ b/agent/src/tools/market_data_tool.py
@@ -191,6 +191,18 @@ class MarketDataTool(BaseTool):
source = kwargs.get("source", "auto")
if source not in _SOURCE_ENUM:
return _error(f"source must be one of {_SOURCE_ENUM}")
+ if source == "auto":
+ bare_us = [
+ code for code in codes
+ if re.fullmatch(r"[A-Za-z][A-Za-z0-9.\-]*", code)
+ and "." not in code
+ and "-" not in code
+ ]
+ if bare_us:
+ return _error(
+ "US equity symbols must include the .US suffix, "
+ f"for example AAPL.US; received {bare_us}"
+ )
interval = kwargs.get("interval", "1D")
if not isinstance(interval, str):