Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(tools): surface inverted journal date filter as error envelope (follow-up #729)

Haozhe Wu668389bdeaf4
diff --git a/agent/src/tools/trade_journal_tool.py b/agent/src/tools/trade_journal_tool.py
index 95fca8549..1f22a7931 100644
--- a/agent/src/tools/trade_journal_tool.py
+++ b/agent/src/tools/trade_journal_tool.py
@@ -444,7 +444,10 @@ def analyze_trade_journal(file_path: str, analysis_type: str = "full", filter_ex
)
df = records_to_dataframe(records)
- filtered = _apply_filter(df, filter_expr)
+ try:
+ filtered = _apply_filter(df, filter_expr)
+ except ValueError as exc:
+ return json.dumps({"status": "error", "error": str(exc)}, ensure_ascii=False)
result: dict[str, Any] = {
"status": "ok",
diff --git a/agent/tests/test_trade_journal.py b/agent/tests/test_trade_journal.py
index 8032de6d9..bda1bacc4 100644
--- a/agent/tests/test_trade_journal.py
+++ b/agent/tests/test_trade_journal.py
@@ -417,11 +417,13 @@ def test_apply_filter_preserves_full_date_and_mixed_precision() -> None:
dates = _apply_filter(df, "2024-02-01 to 2024-02-15")
mixed = _apply_filter(df, "2024-02-15 to 2024-02")
- reversed_range = _apply_filter(df, "2024-03 to 2024-02")
assert list(dates["symbol"]) == ["START", "MIDDLE"]
assert list(mixed["symbol"]) == ["MIDDLE", "END"]
- assert reversed_range.empty
+ # Inverted/degenerate ranges now fail fast rather than silently returning
+ # an empty frame that a caller could mistake for "no trades in range" (#729).
+ with pytest.raises(ValueError, match="inverted date filter"):
+ _apply_filter(df, "2024-03 to 2024-02")
def test_apply_filter_symbol_equals() -> None:
@@ -510,6 +512,17 @@ def test_analyze_with_filter(allow_tmp: Path) -> None:
assert result["filter_applied"] == "symbol=AAPL"
+def test_analyze_with_inverted_date_filter_returns_error_envelope(allow_tmp: Path) -> None:
+ """An inverted date range must surface as an error envelope, not a raw raise."""
+ result = json.loads(
+ analyze_trade_journal(
+ str(_write_full_journal(allow_tmp)), filter_expr="2026-03 to 2026-01"
+ )
+ )
+ assert result["status"] == "error"
+ assert "inverted date filter" in result["error"]
+
+
def test_qualify_a_share_rejects_empty() -> None:
with pytest.raises(ValueError, match="empty"):
_qualify_a_share("")