Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(tools): benford_check no longer misclassifies exact round leading digits

Lanre Shittu4f40d973fc55
diff --git a/agent/src/tools/financial_rigor_tool.py b/agent/src/tools/financial_rigor_tool.py
index 7922e1d0c..a2632cce5 100644
--- a/agent/src/tools/financial_rigor_tool.py
+++ b/agent/src/tools/financial_rigor_tool.py
@@ -299,9 +299,13 @@ def benford_check(values: list[Any]) -> dict[str, Any]:
v = abs(float(raw))
if v > 0 and math.isfinite(v):
sig = 10 ** (math.log10(v) - math.floor(math.log10(v)))
- d = int(sig)
- if 1 <= d <= 9:
- digits.append(d)
+ # sig is mathematically in [1, 10), but float round-trip through
+ # log10/pow can land a hair under an integer boundary (e.g. sig
+ # == 4.999999999999999 for v == 5000), so a bare int() truncates
+ # to the digit below. Round first, then clamp the 10 -> 9 edge.
+ d = int(round(sig, 9))
+ d = min(9, max(1, d))
+ digits.append(d)
n = len(digits)
if n < 50:
return {
diff --git a/agent/tests/test_financial_rigor_tool.py b/agent/tests/test_financial_rigor_tool.py
index 3792f63ae..a100fdff9 100644
--- a/agent/tests/test_financial_rigor_tool.py
+++ b/agent/tests/test_financial_rigor_tool.py
@@ -156,6 +156,17 @@ def test_benford_all_same_leading_digit_fails() -> None:
assert out["is_conforming"] is False
+def test_benford_exact_round_values_keep_their_true_leading_digit() -> None:
+ # 5000, 50000, ... all start with 5. A log10/floor round-trip can drift a
+ # hair under the integer boundary (5.0 -> 4.999999999999999), which used
+ # to truncate these into digit 4 instead of 5.
+ values = [5 * 10**n for n in range(3, 63)]
+ out = benford_check(values)
+ counts = {row["digit"]: row["observed"] for row in out["distribution"]}
+ assert counts[5] == pytest.approx(1.0)
+ assert counts.get(4, 0.0) == pytest.approx(0.0)
+
+
# ── exact_calc ────────────────────────────────────────────────────────────