"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(journal): reject an unknown analysis_type instead of returning an empty ok result (#1911)
Rudy Mizrahi Celekli9fc2f7e45d4f
diff --git a/agent/src/tools/trade_journal_tool.py b/agent/src/tools/trade_journal_tool.pyindex ddbefffe6..f6cbc0356 100644--- a/agent/src/tools/trade_journal_tool.py+++ b/agent/src/tools/trade_journal_tool.py@@ -669,6 +669,9 @@ def _json_safe(value: Any) -> Any:return value+_ANALYSIS_TYPES = frozenset({"full", "profile", "behavior", "strategy"})++def analyze_trade_journal(file_path: str, analysis_type: str = "full", filter_expr: str = "") -> str:"""Parse a trade journal and return a JSON analysis.@@ -684,6 +687,17 @@ def analyze_trade_journal(file_path: str, analysis_type: str = "full", filter_exJSON string. Keys: status, file, format_detected, total_records,date_range, market, profile / behavior (when applicable)."""+ if not isinstance(analysis_type, str) or analysis_type not in _ANALYSIS_TYPES:+ return json.dumps(+ {+ "status": "error",+ "error": (+ f"Unknown analysis_type {analysis_type!r}. "+ f"Expected one of: {', '.join(sorted(_ANALYSIS_TYPES))}"+ ),+ },+ ensure_ascii=False,+ )try:path = safe_user_path(file_path)except ValueError as exc:@@ -791,6 +805,6 @@ class TradeJournalTool(BaseTool):def execute(self, **kwargs: Any) -> str:return analyze_trade_journal(file_path=kwargs["file_path"],- analysis_type=kwargs.get("analysis_type", "full"),+ analysis_type=kwargs.get("analysis_type") or "full",filter_expr=kwargs.get("filter_expr", ""),)diff --git a/agent/tests/test_trade_journal_analysis_type.py b/agent/tests/test_trade_journal_analysis_type.pynew file mode 100644index 000000000..72a8d1848--- /dev/null+++ b/agent/tests/test_trade_journal_analysis_type.py@@ -0,0 +1,22 @@+"""An unknown ``analysis_type`` must be an error, not an empty ``ok`` result."""++from __future__ import annotations++import json++import pytest++from src.tools.trade_journal_tool import analyze_trade_journal++[email protected]("bad", ["profil", "Profile", "all", ["profile"], {"type": "profile"}])+def test_unknown_analysis_type_is_an_error(bad):+ out = json.loads(analyze_trade_journal("trades.csv", analysis_type=bad))+ assert out["status"] == "error"+ assert "analysis_type" in out["error"]++[email protected]("good", ["full", "profile", "behavior", "strategy"])+def test_known_analysis_types_pass_validation(good):+ out = json.loads(analyze_trade_journal("trades.csv", analysis_type=good))+ assert "analysis_type" not in out.get("error", "")