Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(tools): interval "1M" (one month) no longer silently case-folds to 1m (one minute)

shadowinlifef3af89db77d1
diff --git a/agent/src/tools/market_data_tool.py b/agent/src/tools/market_data_tool.py
index d5f4ff26d..2242f8645 100644
--- a/agent/src/tools/market_data_tool.py
+++ b/agent/src/tools/market_data_tool.py
@@ -13,12 +13,51 @@ from src.market_data import DEFAULT_MAX_ROWS, fetch_market_data_json
from backtest.loaders.registry import VALID_SOURCES
from backtest.runner import _VALID_INTERVALS
-# Canonical-case lookup: `_VALID_INTERVALS` mixes cases ("1m" minutes vs "1H"
-# hours), so a plain `.upper()` would turn valid "1m"/"5m"/"15m"/"30m" into
-# "1M"/"5M"/… which loaders reject. Map any case spelling to the canonical
-# form the loaders expect ("1d" -> "1D", "30M" -> "30m").
+# Canonical-case lookup. ``_VALID_INTERVALS`` mixes cases ("1m" minutes vs
+# "1H" hours), so a plain ``.upper()`` fold is unsafe: it maps the universal
+# monthly spelling "1M" onto "1m" (one minute), silently answering a monthly
+# question with minute bars (#1480). ``_canonicalize_interval`` resolves a
+# case-exact match first and only then falls back to this fold for the
+# unambiguous case variants ("1d" -> "1D", "30M" -> "30m").
_INTERVAL_CANON = {v.upper(): v for v in _VALID_INTERVALS}
+# Spellings that read as a request for a bar size this tool does not serve and
+# must never be case-folded onto a minute interval. "1M" is the near-universal
+# monthly convention (pandas ``resample('1M')``, ccxt / TradingView "1M"); "1W"
+# is weekly. Folding "1M" -> "1m" (one minute) is the #1480 footgun: the caller
+# receives minute bars labelled as the answer to a monthly question. These are
+# rejected with a pointed message instead of being silently mis-served. The
+# minute spellings "5M" / "15M" / "30M" deliberately keep folding to "5m" /
+# "15m" / "30m" — multi-month bars are not a convention anyone requests, and
+# that fold is the documented behaviour pinned by the regression tests.
+_UNSUPPORTED_PERIOD_SPELLINGS = {"1M": "one month", "1W": "one week"}
+
+
+def _canonicalize_interval(raw: str) -> str | None:
+ """Resolve a user interval spelling to the canonical loader form.
+
+ A case-exact match against ``_VALID_INTERVALS`` wins first, so the minute
+ interval ``"1m"`` and the monthly spelling ``"1M"`` are never conflated by
+ the case fold (#1480). Falls back to ``_INTERVAL_CANON`` for unambiguous
+ case variants (``"1d"`` -> ``"1D"``, ``"30M"`` -> ``"30m"``). Month/week
+ spellings that are not served resolve to ``None`` rather than folding onto
+ a minute interval.
+
+ Args:
+ raw: Interval string as supplied by the caller.
+
+ Returns:
+ The canonical interval token, or ``None`` when not served.
+ """
+ token = raw.strip()
+ if token in _VALID_INTERVALS:
+ return token
+ folded = token.upper()
+ if folded in _UNSUPPORTED_PERIOD_SPELLINGS:
+ return None
+ return _INTERVAL_CANON.get(folded)
+
+
# Source allow-list derived from the shared loader registry (the same set the
# backtest tool validates against), so the MCP/agent-facing surface can never
# silently drop a loader the registry serves. Sorted for a stable schema.
@@ -161,7 +200,23 @@ class MarketDataTool(BaseTool):
interval = kwargs.get("interval", "1D")
if not isinstance(interval, str):
return _error("interval must be a string like '1D', '1H', '4H', '30m'")
- normalized_interval = _INTERVAL_CANON.get(interval.strip().upper())
+ interval_token = interval.strip()
+ # Reject the month/week spellings the case fold would otherwise map onto
+ # a minute interval: "1M" (one month) must never silently become "1m"
+ # (one minute) (#1480). A case-exact served spelling is allowed through
+ # first, so this stays correct if monthly/weekly bars are ever added to
+ # ``_VALID_INTERVALS`` (#1479).
+ folded = interval_token.upper()
+ if (
+ interval_token not in _VALID_INTERVALS
+ and folded in _UNSUPPORTED_PERIOD_SPELLINGS
+ ):
+ return _error(
+ f"interval {interval_token!r} ({_UNSUPPORTED_PERIOD_SPELLINGS[folded]}) "
+ f"is not supported; supported: {sorted(_VALID_INTERVALS)}. "
+ f"Note '1m' (lowercase) is one minute."
+ )
+ normalized_interval = _canonicalize_interval(interval_token)
if normalized_interval is None:
return _error(
f"interval must be one of {sorted(_VALID_INTERVALS)} "
diff --git a/agent/tests/test_market_data_tool.py b/agent/tests/test_market_data_tool.py
index d5ed44933..0ffdb83b8 100644
--- a/agent/tests/test_market_data_tool.py
+++ b/agent/tests/test_market_data_tool.py
@@ -463,3 +463,38 @@ def test_market_data_tool_accepts_minute_intervals():
)
assert json.loads(out) == {}
assert [c["interval"] for c in calls] == ["1m", "5m", "15m", "30m", "30m", "1H", "1D"]
+
+
+def test_market_data_tool_rejects_month_and_week_spellings():
+ """'1M' (one month) must never case-fold to '1m' (one minute) (#1480)."""
+ import src.tools.market_data_tool as mod
+ from unittest import mock
+
+ with mock.patch.object(mod, "fetch_market_data_json", return_value="{}") as fetch:
+ for interval, meaning in (("1M", "one month"), ("1W", "one week")):
+ out = json.loads(
+ mod.MarketDataTool().execute(
+ codes=["AAPL.US"],
+ start_date="2026-08-20",
+ end_date="2026-08-21",
+ interval=interval,
+ )
+ )
+ assert out["ok"] is False
+ assert meaning in out["error"]
+ assert "one minute" in out["error"]
+ assert interval in out["error"]
+ # Silence is the failure mode (#1480): the fold must never reach a loader.
+ fetch.assert_not_called()
+
+
+def test_canonicalize_interval_keeps_minute_and_refuses_month():
+ """_canonicalize_interval resolves '1m' but refuses '1M'/'1W' (#1480)."""
+ from src.tools.market_data_tool import _canonicalize_interval
+
+ assert _canonicalize_interval("1m") == "1m"
+ assert _canonicalize_interval("1M") is None
+ assert _canonicalize_interval("1W") is None
+ assert _canonicalize_interval("1d") == "1D"
+ assert _canonicalize_interval("30M") == "30m"
+ assert _canonicalize_interval("1h") == "1H"