"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(financial-rigor): stop zero and negative references reading as agreement
Rudy Celekli5977e8591aad
diff --git a/agent/src/tools/financial_rigor_tool.py b/agent/src/tools/financial_rigor_tool.pyindex b540308b8..6b10f3dde 100644--- a/agent/src/tools/financial_rigor_tool.py+++ b/agent/src/tools/financial_rigor_tool.py@@ -157,8 +157,13 @@ def verify_market_cap("""p, s, r = _exact(price), _exact(shares), _exact(reported_cap)calculated = _CTX.multiply(p, s)- deviation = abs(float(calculated - r) / float(r)) * 100 if r != 0 else 0.0- if deviation > 5:+ # A zero reported cap has no percent deviation; it can only match a zero+ # calculated cap, so a non-zero product fails rather than reading as 0%.+ deviation = abs(float(calculated - r) / float(r)) * 100 if r != 0 else None+ if deviation is None:+ verdict = "pass" if calculated == 0 else "fail"+ deviation = 0.0 if calculated == 0 else None+ elif deviation > 5:verdict = "fail"elif deviation > 1:verdict = "warn"@@ -171,7 +176,7 @@ def verify_market_cap("calculated_market_cap": float(calculated),"calculated_market_cap_display": _fmt(float(calculated)),"reported_market_cap": float(r),- "deviation_pct": round(deviation, 4),+ "deviation_pct": round(deviation, 4) if deviation is not None else None,"verdict": verdict,}@@ -263,13 +268,19 @@ def cross_validate(per_source: list[dict[str, Any]] = []all_consistent = Truefor src, val in values.items():- dev = abs(float(val) - median) / median * 100 if median != 0 else 0.0- consistent = dev <= tolerance_pct+ # Against a zero reference only an exactly-zero value agrees; any+ # other value has no finite percent deviation and is inconsistent.+ if median != 0:+ dev = abs(float(val) - median) / abs(median) * 100+ consistent = dev <= tolerance_pct+ else:+ dev = 0.0 if float(val) == 0 else None+ consistent = dev is not Noneall_consistent = all_consistent and consistentper_source.append({"source": src,"value": float(val),- "deviation_pct": round(dev, 4),+ "deviation_pct": round(dev, 4) if dev is not None else None,"consistent": consistent,})return {diff --git a/agent/tests/test_financial_rigor_tool.py b/agent/tests/test_financial_rigor_tool.pyindex 9da19e57e..088111739 100644--- a/agent/tests/test_financial_rigor_tool.py+++ b/agent/tests/test_financial_rigor_tool.py@@ -91,6 +91,35 @@ def test_market_cap_fields() -> None:assert out["deviation_pct"] == pytest.approx(0.0839, abs=1e-3)+def test_market_cap_zero_reported_cap_fails_against_nonzero_product() -> None:+ out = verify_market_cap(510, 9.11e9, 0)+ assert out["verdict"] == "fail"+ assert out["deviation_pct"] is None+++def test_market_cap_zero_reported_cap_matches_zero_product() -> None:+ out = verify_market_cap(0, 9.11e9, 0)+ assert out["verdict"] == "pass"+ assert out["deviation_pct"] == 0.0+++def test_cross_validate_flags_a_value_against_a_zero_median() -> None:+ out = cross_validate("revenue", {"a": 0, "b": 0, "c": 5})+ assert out["all_consistent"] is False+ by_source = {row["source"]: row for row in out["per_source"]}+ assert by_source["c"]["consistent"] is False+ assert by_source["c"]["deviation_pct"] is None+ assert by_source["a"]["consistent"] is True+++def test_cross_validate_negative_median_uses_magnitude() -> None:+ out = cross_validate("net_income", {"a": -100, "b": -50, "c": -75})+ assert out["all_consistent"] is False+ by_source = {row["source"]: row for row in out["per_source"]}+ assert by_source["a"]["deviation_pct"] == pytest.approx(33.3333, abs=1e-3)+ assert by_source["a"]["consistent"] is False++# ── verify_valuation ──────────────────────────────────────────────────────