Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(live): mandate commit no longer widens an explicit zero exposure narrowing back to the order cap

Lanre Shittub83a311422c4
diff --git a/agent/src/live/mandate/commit.py b/agent/src/live/mandate/commit.py
index cb5a25dc7..ebb92df10 100644
--- a/agent/src/live/mandate/commit.py
+++ b/agent/src/live/mandate/commit.py
@@ -542,6 +542,20 @@ def commit_mandate(
}
+def _explicit_float(profile: Mapping[str, Any], key: str) -> float | None:
+ """Return ``profile[key]`` as a float, or ``None`` if the key is absent.
+
+ ``_resolve_profile`` lets a commit-time adjustment narrow a numeric limit
+ down to an explicit ``0`` (any value <= the rendered limit is a legal
+ narrowing, the same path used for leverage="none"). Combining
+ ``profile.get(key, default)`` with Python's ``x or default`` idiom treats
+ that explicit ``0`` the same as "key missing" and silently substitutes a
+ higher fallback, which is exactly the widening a commit must never do.
+ """
+ value = profile.get(key)
+ return float(value) if value is not None else None
+
+
def _profile_to_hard_caps(profile: Mapping[str, Any]) -> dict[str, Any]:
"""Map a clamped proposal profile to the mandate ``hard_caps`` section.
@@ -559,11 +573,24 @@ def _profile_to_hard_caps(profile: Mapping[str, Any]) -> dict[str, Any]:
leverage_raw = profile.get("leverage", "none")
max_leverage = 1.0 if leverage_raw in ("none", None) else float(leverage_raw)
instruments = list(profile.get("instruments") or ["equity"])
- funding = float(profile.get("account_funding_usd", profile.get("max_total_exposure_usd", 0.0)) or 0.0)
- max_order = float(profile.get("max_order_usd", 0.0) or 0.0)
- max_exposure = float(profile.get("max_total_exposure_usd", funding or max_order) or max_order)
+
+ funding_val = _explicit_float(profile, "account_funding_usd")
+ exposure_val = _explicit_float(profile, "max_total_exposure_usd")
+ max_order = _explicit_float(profile, "max_order_usd")
+ if max_order is None:
+ max_order = 0.0
+
+ if exposure_val is not None:
+ max_exposure = exposure_val
+ elif funding_val is not None:
+ max_exposure = funding_val
+ else:
+ max_exposure = max_order
+
+ account_funding_usd = funding_val if funding_val is not None else max_exposure
+
return {
- "account_funding_usd": funding or max_exposure,
+ "account_funding_usd": account_funding_usd,
"max_order_notional_usd": max_order,
"max_total_exposure_usd": max_exposure,
"max_leverage": max_leverage,
diff --git a/agent/tests/test_mandate_commit_security.py b/agent/tests/test_mandate_commit_security.py
index ba261a10a..753d2df51 100644
--- a/agent/tests/test_mandate_commit_security.py
+++ b/agent/tests/test_mandate_commit_security.py
@@ -113,3 +113,31 @@ def test_commit_mandate_rejects_proposal_id_traversal_to_external_json(live_runt
mandate_path = broker_dir("robinhood") / "mandate.json"
assert not mandate_path.exists()
+
+
+def test_commit_mandate_persists_an_explicit_zero_exposure_narrowing(live_runtime: Path) -> None:
+ """A legal narrowing to zero exposure must not be widened back on persist.
+
+ _resolve_profile allows an adjustment to narrow max_total_exposure_usd down
+ to 0 (any value <= the rendered limit is a legal narrowing). Before the
+ fix, _profile_to_hard_caps used the ``x or default`` idiom, which treated
+ that explicit 0 the same as "field missing" and silently substituted the
+ rendered max_order_usd instead, so the persisted mandate authorized more
+ exposure than the user consented to.
+ """
+ proposal_id = "mp_" + "c" * 32
+ save_proposal(_proposal(proposal_id))
+
+ result = commit_mandate(
+ proposal_id=proposal_id,
+ ordinal=1,
+ adjustments={"max_total_exposure_usd": 0.0},
+ consent_ack=True,
+ broker="robinhood",
+ account_ref="acct-zero",
+ )
+
+ assert result["resolved_profile"]["max_total_exposure_usd"] == 0.0
+ mandate_path = live_runtime / "live" / "robinhood" / "mandate.json"
+ mandate = json.loads(mandate_path.read_text(encoding="utf-8"))
+ assert mandate["hard_caps"]["max_total_exposure_usd"] == 0.0