Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(alpha-bench): reject top=0 instead of silently using 20 (#1910)

Rudy Mizrahi Celekliee59a6a9094d
diff --git a/agent/src/tools/alpha_bench_tool.py b/agent/src/tools/alpha_bench_tool.py
index 65d2f3b6f..c4bee497d 100644
--- a/agent/src/tools/alpha_bench_tool.py
+++ b/agent/src/tools/alpha_bench_tool.py
@@ -1269,9 +1269,18 @@ def run_alpha_bench(**kwargs: Any) -> dict[str, Any]:
except ValueError as exc:
return {"status": "error", "error": str(exc)}
- top_n = int(kwargs.get("top", 20) or 20)
- if top_n <= 0:
- return {"status": "error", "error": "top must be > 0"}
+ top_raw = kwargs.get("top")
+ if top_raw is None:
+ top_n = 20
+ else:
+ try:
+ if isinstance(top_raw, bool) or (isinstance(top_raw, float) and not top_raw.is_integer()):
+ raise ValueError
+ top_n = int(top_raw)
+ except (TypeError, ValueError, OverflowError):
+ return {"status": "error", "error": "top must be an integer > 0"}
+ if top_n <= 0:
+ return {"status": "error", "error": "top must be > 0"}
try:
horizon = _validate_horizon(kwargs.get("horizon", 1))
diff --git a/agent/tests/test_alpha_bench_top_zero.py b/agent/tests/test_alpha_bench_top_zero.py
new file mode 100644
index 000000000..dc01e7e9d
--- /dev/null
+++ b/agent/tests/test_alpha_bench_top_zero.py
@@ -0,0 +1,25 @@
+"""``top=0`` must be rejected, not silently replaced by the default of 20."""
+
+from __future__ import annotations
+
+import pytest
+
+from src.tools.alpha_bench_tool import run_alpha_bench
+
+
[email protected]("bad", [0, -3, "0"])
+def test_non_positive_top_is_rejected(bad):
+ out = run_alpha_bench(universe="csi300", period="2020-2025", top=bad)
+ assert out == {"status": "error", "error": "top must be > 0"}
+
+
[email protected]("bad", ["abc", float("inf"), float("nan"), 1.5, True, [1]])
+def test_non_integer_top_is_an_error_envelope(bad):
+ out = run_alpha_bench(universe="csi300", period="2020-2025", top=bad)
+ assert out["status"] == "error"
+ assert "top" in out["error"]
+
+
+def test_omitted_top_is_not_rejected_as_top():
+ out = run_alpha_bench(universe="csi300", period="2020-2025")
+ assert "top must" not in out.get("error", "")