diff --git a/tests/test_tool_date_enforcement.py b/tests/test_tool_date_enforcement.py index cb968d688..719bfa286 100644 --- a/tests/test_tool_date_enforcement.py +++ b/tests/test_tool_date_enforcement.py @@ -135,6 +135,23 @@ def test_direct_call_without_state_is_unchanged(): assert routed.call_args.args == ("get_news", "AAPL", "2026-09-01", "2026-09-08") +@pytest.mark.unit +@pytest.mark.parametrize("tool", ["get_news", "get_stock_data"]) +def test_a_start_date_that_is_not_a_date_is_sent_back_to_the_model(tool): + # A model can append a stray character to a date (#1476). No window can be + # guessed from it, so the vendor is not called and the model is told the form. + with mock.patch.object(tools, "route_to_vendor", return_value="ok") as routed: + out = getattr(tools, tool).func("NVDA", "2026-09-26\u0629", "2026-10-03", trade_date="2026-10-03") + assert not routed.called + assert "YYYY-MM-DD" in out and "start_date" in out + + +@pytest.mark.unit +def test_a_window_whose_start_is_not_a_date_is_refused(): + with pytest.raises(ValueError, match="start_date"): + as_of_window("2026-09-26\u0629", "2026-10-03", "2026-10-03") + + # --- the run date itself (#1319) ------------------------------------------------- @pytest.mark.unit diff --git a/tradingagents/agents/tools.py b/tradingagents/agents/tools.py index 5a4d65cdc..c0e9adf74 100644 --- a/tradingagents/agents/tools.py +++ b/tradingagents/agents/tools.py @@ -31,7 +31,10 @@ def get_stock_data( Returns: str: A formatted dataframe containing the price data for the instrument over the date range. """ - start_date, end_date = as_of_window(start_date, end_date, trade_date) + try: + start_date, end_date = as_of_window(start_date, end_date, trade_date) + except ValueError as e: + return str(e) return route_to_vendor("get_stock_data", symbol, start_date, end_date) @@ -181,7 +184,10 @@ def get_news( Returns: str: A formatted string containing news data """ - start_date, end_date = as_of_window(start_date, end_date, trade_date) + try: + start_date, end_date = as_of_window(start_date, end_date, trade_date) + except ValueError as e: + return str(e) return route_to_vendor("get_news", ticker, start_date, end_date) diff --git a/tradingagents/dataflows/date_window.py b/tradingagents/dataflows/date_window.py index 6c30051f7..9eff63010 100644 --- a/tradingagents/dataflows/date_window.py +++ b/tradingagents/dataflows/date_window.py @@ -92,10 +92,14 @@ def as_of_window(start_date: str, end_date: str, trade_date: str) -> tuple[str, """``[start, end]`` with its end clamped to the run date. A window wholly after the run date keeps its length and moves back to end there. + An unreadable end reads as the run date, but no window can be guessed from an + unreadable start, so it raises ValueError for the caller to send back (#1476). """ - end = as_of(end_date, trade_date) start, old_end = _parse(start_date), _parse(end_date) - if end == end_date or start is None or start <= _parse(end): + if start is None: + raise ValueError(f"start_date {start_date!r} is not a date; give it as YYYY-MM-DD") + end = as_of(end_date, trade_date) + if end == end_date or start <= _parse(end): return start_date, end span = (old_end - start) if old_end is not None and old_end >= start else timedelta(0) return f"{_parse(end) - span:%Y-%m-%d}", end