Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(quantlib): refuse negative comps bridge magnitudes

Lanre Shittuef4fe5294a4c
diff --git a/agent/src/quantlib/valuation/comps.py b/agent/src/quantlib/valuation/comps.py
index 06990aff7..9833aa62a 100644
--- a/agent/src/quantlib/valuation/comps.py
+++ b/agent/src/quantlib/valuation/comps.py
@@ -384,14 +384,10 @@ def _bridge_delta(
`(delta, omitted_components)`.
"""
omitted: list[str] = []
- if not math.isfinite(total_debt):
- raise ValuationError(
- f"comps: total_debt must be a finite number, got {total_debt!r}"
- )
- if not math.isfinite(cash_and_equivalents):
- raise ValuationError(
- f"comps: cash_and_equivalents must be a finite number, got {cash_and_equivalents!r}"
- )
+ total_debt = _require_nonnegative(total_debt, "total_debt")
+ cash_and_equivalents = _require_nonnegative(
+ cash_and_equivalents, "cash_and_equivalents"
+ )
total = total_debt - cash_and_equivalents
for name, value in (
("minority_interest", minority_interest),
@@ -401,15 +397,28 @@ def _bridge_delta(
if value is None:
omitted.append(name)
continue
- if not math.isfinite(value):
- raise ValuationError(
- f"comps: {name} must be a finite number, got {value!r}"
- )
+ value = _require_nonnegative(value, name)
sign = -1.0 if name == "investments_in_associates" else 1.0
total += sign * value
return total, tuple(omitted)
+def _require_nonnegative(value: float, name: str) -> float:
+ """Check a value that is a signed bridge formula's magnitude, so must be >= 0.
+
+ A negative magnitude here would silently flip the sign the bridge
+ formula already applies (e.g. negative cash would be ADDED to EV
+ instead of subtracted) -- the same guard `dcf.equity_bridge` applies
+ to these identical fields.
+ """
+ if not math.isfinite(value) or value < 0.0:
+ raise ValuationError(
+ f"comps: {name} must be supplied as a non-negative magnitude "
+ f"(its sign is applied by the bridge formula), got {value!r}"
+ )
+ return value
+
+
def enterprise_value(
*,
market_cap: float,
diff --git a/agent/tests/quantlib/valuation/test_comps.py b/agent/tests/quantlib/valuation/test_comps.py
index dceeac97a..1603dd797 100644
--- a/agent/tests/quantlib/valuation/test_comps.py
+++ b/agent/tests/quantlib/valuation/test_comps.py
@@ -825,6 +825,36 @@ def test_enterprise_value_finite_bridge_is_unchanged():
assert result.enterprise_value == pytest.approx(280.0, abs=EXACT)
+def test_enterprise_value_refuses_negative_bridge_magnitudes():
+ # A negative magnitude here would silently flip the sign the bridge
+ # applies (e.g. negative cash gets ADDED to EV instead of subtracted).
+ with pytest.raises(ValuationError):
+ enterprise_value(market_cap=1000.0, total_debt=200.0, cash_and_equivalents=-150.0)
+ with pytest.raises(ValuationError):
+ enterprise_value(market_cap=1000.0, total_debt=-200.0, cash_and_equivalents=150.0)
+ with pytest.raises(ValuationError):
+ enterprise_value(
+ market_cap=1000.0,
+ total_debt=200.0,
+ cash_and_equivalents=150.0,
+ minority_interest=-50.0,
+ )
+ with pytest.raises(ValuationError):
+ enterprise_value(
+ market_cap=1000.0,
+ total_debt=200.0,
+ cash_and_equivalents=150.0,
+ preferred_stock=-50.0,
+ )
+ with pytest.raises(ValuationError):
+ enterprise_value(
+ market_cap=1000.0,
+ total_debt=200.0,
+ cash_and_equivalents=150.0,
+ investments_in_associates=-50.0,
+ )
+
+
def test_calendarise_metric_refuses_non_finite_period_values():
with pytest.raises(ValuationError):
calendarise_metric(