"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(market-data): validate codes, date range, and source before fetching
cgycoreyd5413fdc9626
diff --git a/agent/src/tools/market_data_tool.py b/agent/src/tools/market_data_tool.pyindex 3fc5ded9b..220d19cfc 100644--- a/agent/src/tools/market_data_tool.py+++ b/agent/src/tools/market_data_tool.py@@ -2,12 +2,39 @@from __future__ import annotations+import json+import re+from datetime import date+from typing import Anyfrom src.agent.tools import BaseToolfrom src.market_data import DEFAULT_MAX_ROWS, fetch_market_data_json+_ISO_DATE_RE = re.compile(r"^\d{4}-\d{2}-\d{2}$")+++def _error(message: str) -> str:+ return json.dumps({"ok": False, "error": message}, ensure_ascii=False)+++def _valid_iso_date(value: str) -> bool:+ """True only for strict ``YYYY-MM-DD`` calendar dates.++ ``date.fromisoformat`` alone is not enough: on Python 3.11+ it also+ accepts the compact ``YYYYMMDD`` form, which loaders downstream reject+ or mis-parse. Enforce the exact shape first, then the calendar.+ """+ if not _ISO_DATE_RE.match(value):+ return False+ try:+ date.fromisoformat(value)+ except ValueError:+ return False+ return True++class MarketDataTool(BaseTool):"""Fetch normalized OHLCV data through repository loaders."""@@ -95,11 +122,48 @@ class MarketDataTool(BaseTool):repeatable = Truedef execute(self, **kwargs: Any) -> str:+ """Validate inputs, then fetch and return strict JSON.++ Args:+ **kwargs: ``codes``, ``start_date``, ``end_date``, optional+ ``source``, ``interval``, ``max_rows``.++ Returns:+ Strict JSON envelope with per-symbol OHLCV panels plus+ ``_provenance``, or an error envelope on invalid inputs.+ """+ codes = kwargs.get("codes")+ if not isinstance(codes, list) or not codes:+ return _error("codes must be a non-empty list of strings")+ if any(not isinstance(code, str) or not code.strip() for code in codes):+ return _error("every code must be a non-empty string")+ codes = [code.strip() for code in codes]++ start_date = kwargs.get("start_date")+ end_date = kwargs.get("end_date")+ if not isinstance(start_date, str) or not start_date.strip():+ return _error("start_date must be a non-empty YYYY-MM-DD string")+ if not isinstance(end_date, str) or not end_date.strip():+ return _error("end_date must be a non-empty YYYY-MM-DD string")+ start_date = start_date.strip()+ end_date = end_date.strip()+ if not _valid_iso_date(start_date) or not _valid_iso_date(end_date):+ return _error("start_date and end_date must be valid YYYY-MM-DD dates")+ if start_date > end_date:+ return _error(+ f"start_date ({start_date}) must not be after end_date ({end_date})"+ )++ source = kwargs.get("source", "auto")+ source_enum = self.parameters["properties"]["source"]["enum"]+ if source not in source_enum:+ return _error(f"source must be one of {list(source_enum)}")+return fetch_market_data_json(- codes=kwargs["codes"],- start_date=kwargs["start_date"],- end_date=kwargs["end_date"],- source=kwargs.get("source", "auto"),+ codes=codes,+ start_date=start_date,+ end_date=end_date,+ source=source,interval=kwargs.get("interval", "1D"),max_rows=kwargs.get("max_rows", DEFAULT_MAX_ROWS),include_provenance=True,diff --git a/agent/tests/test_market_data_tool.py b/agent/tests/test_market_data_tool.pyindex 22644b4ca..f45faea43 100644--- a/agent/tests/test_market_data_tool.py+++ b/agent/tests/test_market_data_tool.py@@ -246,3 +246,69 @@ def test_worker_prompt_prioritizes_get_market_data_for_ohlcv():assert "Market Data Tool Policy" in promptassert "call `get_market_data` before writing raw provider scripts" in prompt+++def test_market_data_tool_rejects_empty_codes():+ from src.tools.market_data_tool import MarketDataTool++ out = json.loads(MarketDataTool().execute(codes=[], start_date="2026-01-01", end_date="2026-02-01"))+ assert out == {"ok": False, "error": "codes must be a non-empty list of strings"}+++def test_market_data_tool_rejects_blank_code():+ from src.tools.market_data_tool import MarketDataTool++ out = json.loads(MarketDataTool().execute(codes=[""], start_date="2026-01-01", end_date="2026-02-01"))+ assert out == {"ok": False, "error": "every code must be a non-empty string"}+++def test_market_data_tool_rejects_non_list_codes():+ from src.tools.market_data_tool import MarketDataTool++ out = json.loads(MarketDataTool().execute(codes="AAPL.US", start_date="2026-01-01", end_date="2026-02-01"))+ assert out == {"ok": False, "error": "codes must be a non-empty list of strings"}+++def test_market_data_tool_rejects_missing_start_date():+ from src.tools.market_data_tool import MarketDataTool++ out = json.loads(MarketDataTool().execute(codes=["AAPL.US"], end_date="2026-02-01"))+ assert out == {"ok": False, "error": "start_date must be a non-empty YYYY-MM-DD string"}+++def test_market_data_tool_rejects_malformed_dates():+ from src.tools.market_data_tool import MarketDataTool++ out = json.loads(MarketDataTool().execute(codes=["AAPL.US"], start_date="banana", end_date="2026-02-01"))+ assert out == {"ok": False, "error": "start_date and end_date must be valid YYYY-MM-DD dates"}+++def test_market_data_tool_rejects_inverted_date_range():+ from src.tools.market_data_tool import MarketDataTool++ out = json.loads(MarketDataTool().execute(codes=["AAPL.US"], start_date="2026-08-21", end_date="2026-08-01"))+ assert out == {"ok": False, "error": "start_date (2026-08-21) must not be after end_date (2026-08-01)"}+++def test_market_data_tool_rejects_whitespace_only_code():+ from src.tools.market_data_tool import MarketDataTool++ # Validation strips codes before the emptiness check, so a whitespace-only+ # code is caught after stripping.+ out = json.loads(MarketDataTool().execute(codes=[" "], start_date="2026-01-01", end_date="2026-02-01"))+ assert out == {"ok": False, "error": "every code must be a non-empty string"}+++def test_market_data_tool_rejects_compact_date_form():+ """fromisoformat accepts YYYYMMDD on 3.11+; loaders do not. Reject it."""+ from src.tools.market_data_tool import MarketDataTool++ out = json.loads(MarketDataTool().execute(codes=["AAPL.US"], start_date="20260101", end_date="2026-02-01"))+ assert out == {"ok": False, "error": "start_date and end_date must be valid YYYY-MM-DD dates"}+++def test_market_data_tool_rejects_unknown_source():+ from src.tools.market_data_tool import MarketDataTool++ out = json.loads(MarketDataTool().execute(codes=["AAPL.US"], start_date="2026-01-01", end_date="2026-02-01", source="bogus"))+ assert out == {"ok": False, "error": "source must be one of ['auto', 'longbridge', 'yfinance', 'yahoo', 'okx', 'ccxt', 'tushare', 'baostock', 'tencent', 'akshare', 'mootdx', 'eastmoney', 'sina', 'stooq', 'finnhub', 'alphavantage', 'tiingo', 'fmp', 'mt5', 'pykrx']"}