"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(financial-rigor): reject non-finite and boolean inputs
Jayson Lua8fae59c6f84
diff --git a/agent/src/tools/financial_rigor_tool.py b/agent/src/tools/financial_rigor_tool.pyindex c15495d24..493d65e25 100644--- a/agent/src/tools/financial_rigor_tool.py+++ b/agent/src/tools/financial_rigor_tool.py@@ -61,10 +61,18 @@ def _err(msg: str) -> str:def _exact(value: Any) -> Decimal:- """Convert any numeric to an exact Decimal, avoiding float binary traps."""- if isinstance(value, Decimal):- return value- return Decimal(str(value))+ """Convert any numeric to an exact Decimal, avoiding float binary traps.++ Raises:+ ValueError: If ``value`` is a bool or is NaN / infinite, so a bad input+ becomes an error envelope instead of an ``ok`` verdict.+ """+ if isinstance(value, bool):+ raise ValueError("boolean is not a valid number")+ number = value if isinstance(value, Decimal) else Decimal(str(value))+ if not number.is_finite():+ raise ValueError(f"number must be finite, got {value}")+ return numberdef _fmt(value: float) -> str:@@ -607,6 +615,16 @@ class FinancialRigorTool(BaseTool):{"status": "error", "command": command, "error": str(exc)},ensure_ascii=False,)- return json.dumps(- {"status": "ok", "command": command, **result}, ensure_ascii=False,- )+ try:+ # allow_nan=False: a finite input can still overflow float() to inf,+ # and NaN / Infinity are not valid JSON.+ return json.dumps(+ {"status": "ok", "command": command, **result},+ ensure_ascii=False, allow_nan=False,+ )+ except ValueError:+ return json.dumps(+ {"status": "error", "command": command,+ "error": "result is not representable as a finite number"},+ ensure_ascii=False,+ )diff --git a/agent/tests/test_financial_rigor_nonfinite_inputs.py b/agent/tests/test_financial_rigor_nonfinite_inputs.pynew file mode 100644index 000000000..bd3c16298--- /dev/null+++ b/agent/tests/test_financial_rigor_nonfinite_inputs.py@@ -0,0 +1,52 @@+"""Regression tests: financial_rigor rejects NaN / infinite / boolean inputs.++Before the fix, ``verify_market_cap`` and ``verify_valuation`` returned+``status="ok"`` with NaN or Infinity in the payload, which is not valid JSON.+"""++import json++import pytest++from src.tools.financial_rigor_tool import FinancialRigorTool+++def _run(**kwargs):+ return json.loads(FinancialRigorTool().execute(**kwargs))++[email protected]("bad", [float("nan"), float("inf"), float("-inf")])[email protected]("field", ["price", "shares", "reported_cap"])+def test_verify_market_cap_rejects_non_finite(field, bad):+ args = {"command": "verify_market_cap", "price": 10, "shares": 100, "reported_cap": 1000}+ args[field] = bad+ result = _run(**args)+ assert result["status"] == "error"++[email protected]("bad", [float("nan"), float("inf")])[email protected]("field", ["price", "eps", "bvps", "fcf_per_share", "dividend", "revenue_per_share"])+def test_verify_valuation_rejects_non_finite(field, bad):+ args = {"command": "verify_valuation", "price": 10, "eps": 2, "bvps": 5}+ args[field] = bad+ result = _run(**args)+ assert result["status"] == "error"+++def test_boolean_is_not_a_number():+ result = _run(command="verify_valuation", price=10, eps=True)+ assert result["status"] == "error"+++def test_overflowing_result_is_an_error_not_invalid_json():+ raw = FinancialRigorTool().execute(+ command="verify_market_cap", price=1e308, shares=100, reported_cap=1000,+ )+ assert "Infinity" not in raw and "NaN" not in raw+ assert json.loads(raw)["status"] == "error"+++def test_finite_inputs_still_pass():+ result = _run(command="verify_market_cap", price=10, shares=100, reported_cap=1000)+ assert result["status"] == "ok"+ assert result["verdict"] == "pass"