Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(qveris): stop a caller-supplied quote from bypassing the budget gate

Haozhe Wub4e4803965d5
diff --git a/agent/src/tools/qveris_tool.py b/agent/src/tools/qveris_tool.py
index b13f4e907..77ee7ebbb 100644
--- a/agent/src/tools/qveris_tool.py
+++ b/agent/src/tools/qveris_tool.py
@@ -474,21 +474,67 @@ class QVerisExecuteTool(_QVerisBaseTool):
def _quote(
self, client: QVerisClient, tool_id: str, kwargs: dict[str, Any]
) -> dict[str, Any]:
- quote = {
- "tool_id": tool_id,
- "expected_cost": kwargs.get("expected_cost"),
- "billing_rule": kwargs.get("billing_rule"),
- }
- if quote["expected_cost"] is not None or quote["billing_rule"] is not None:
- return quote
- inspected = client.inspect(
- [tool_id],
- search_id=kwargs.get("search_id"),
- session_id=kwargs.get("session_id"),
+ """Return the quote the budget reservation is computed from.
+
+ A caller-supplied quote is a hint, never the authority. ``expected_cost``
+ and ``billing_rule`` are ordinary tool arguments, so they are written by
+ whatever drives the tool — an LLM on the agent path, an arbitrary client
+ over MCP. Previously either one short-circuited the server lookup and
+ was adopted verbatim, so ``expected_cost=0`` reserved nothing and every
+ call cleared the pre-check regardless of the real price.
+
+ The server is consulted and the reservation takes the LARGER of the two
+ figures, so an honest caller is unaffected and an understating one
+ cannot buy a free pass. A non-positive cost is treated as "unknown"
+ rather than "free" unless the server itself attested it, because a
+ caller can always write zero.
+
+ Args:
+ client: Marketplace client used for the authoritative lookup.
+ tool_id: Capability being priced.
+ kwargs: Raw tool arguments, possibly carrying a caller quote.
+
+ Returns:
+ Quote dict whose ``expected_cost`` is the effective figure, plus
+ ``quote_source`` and ``server_attested`` for the audit trail.
+ """
+ supplied = _parse_expected_cost(kwargs.get("expected_cost"))
+ server_quote: dict[str, Any] = {}
+ server_cost: float | None = None
+ try:
+ inspected = client.inspect(
+ [tool_id],
+ search_id=kwargs.get("search_id"),
+ session_id=kwargs.get("session_id"),
+ )
+ except Exception: # noqa: BLE001 - an unreachable marketplace must not
+ inspected = None # crash the caller; it degrades to the hint below.
+ if isinstance(inspected, dict):
+ results = inspected.get("results")
+ first = results[0] if isinstance(results, list) and results else None
+ server_quote = _quote_from_tool(first)
+ server_cost = _parse_expected_cost(server_quote.get("expected_cost"))
+
+ candidates = [cost for cost in (supplied, server_cost) if cost is not None]
+ effective = max(candidates) if candidates else None
+
+ quote = dict(server_quote)
+ quote["tool_id"] = tool_id
+ quote["expected_cost"] = effective
+ quote["billing_rule"] = server_quote.get("billing_rule") or kwargs.get(
+ "billing_rule"
)
- results = inspected.get("results") if isinstance(inspected, dict) else None
- first = results[0] if isinstance(results, list) and results else None
- return _quote_from_tool(first)
+ quote["server_attested"] = server_cost is not None
+ quote["caller_supplied_cost"] = supplied
+ if server_cost is not None and supplied is not None and supplied < server_cost:
+ quote["quote_source"] = "server_overrode_lower_caller_quote"
+ elif server_cost is not None:
+ quote["quote_source"] = "server"
+ elif supplied is not None:
+ quote["quote_source"] = "caller_hint_unverified"
+ else:
+ quote["quote_source"] = "unavailable"
+ return quote
def execute(self, **kwargs: Any) -> str:
"""Execute a QVeris capability behind the per-session credit budget.
@@ -530,8 +576,13 @@ class QVerisExecuteTool(_QVerisBaseTool):
with _SESSION_BUDGET_LOCK:
spent = _SESSION_SPEND.get(session_id, 0.0)
reserved = _SESSION_RESERVED.get(session_id, 0.0)
+ # A zero reservation is only trustworthy when the marketplace itself
+ # quoted it. A caller can always write zero, and that used to clear
+ # the pre-check while reserving nothing.
valid_quote = (
- expected is not None and math.isfinite(expected) and expected >= 0.0
+ expected is not None
+ and math.isfinite(expected)
+ and (expected > 0.0 or (expected == 0.0 and quote.get("server_attested")))
)
if (
not valid_quote
diff --git a/agent/tests/test_qveris_tool.py b/agent/tests/test_qveris_tool.py
index df90fc53a..f6a5953ee 100644
--- a/agent/tests/test_qveris_tool.py
+++ b/agent/tests/test_qveris_tool.py
@@ -289,3 +289,107 @@ def test_execute_hydrates_truncated_full_content():
assert payload["result"]["full_content"] == {"rows": [{"close": 1.23}]}
assert payload["result"]["full_content_downloaded"] is True
assert fake.calls[1]["headers"] is None
+
+
+def test_zero_expected_cost_from_the_caller_cannot_clear_the_budget_gate(monkeypatch):
+ """A caller-written quote of 0 must not reserve nothing and sail through.
+
+ ``expected_cost`` is an ordinary tool argument, so it is written by an LLM
+ on the agent path and by an arbitrary client over MCP. It used to
+ short-circuit the marketplace lookup and be adopted verbatim, so 0 cleared
+ the pre-check with a zero reservation whatever the real price was.
+ """
+ qt.save_qveris_config(
+ qt.QVerisConfig(
+ enabled=True,
+ api_key="sk-live",
+ mode="paid",
+ budget_credits_per_session=1.0,
+ )
+ )
+
+ class FakeClient:
+ def inspect(self, *args, **kwargs):
+ del args, kwargs
+ return {"results": [{"tool_id": "tool_1", "expected_cost": "5 credits"}]}
+
+ def execute(self, *args, **kwargs): # pragma: no cover - must not run
+ raise AssertionError("execute must be blocked by the budget gate")
+
+ monkeypatch.setattr(qt.QVerisExecuteTool, "_client", lambda self: FakeClient())
+
+ payload = json.loads(
+ qt.QVerisExecuteTool().execute(
+ tool_id="tool_1",
+ parameters={},
+ session_id="attack",
+ expected_cost=0.0,
+ )
+ )
+
+ assert payload["status"] == "budget_exceeded"
+ assert payload["quote"]["expected_cost"] == 5.0
+ assert payload["quote"]["quote_source"] == "server_overrode_lower_caller_quote"
+ assert qt._SESSION_SPEND.get("attack", 0.0) == 0.0
+
+
+def test_an_unverifiable_zero_quote_fails_closed(monkeypatch):
+ """With no server quote to corroborate it, 0 means unknown, not free."""
+ qt.save_qveris_config(
+ qt.QVerisConfig(
+ enabled=True,
+ api_key="sk-live",
+ mode="paid",
+ budget_credits_per_session=1.0,
+ )
+ )
+
+ class FakeClient:
+ def inspect(self, *args, **kwargs):
+ del args, kwargs
+ raise RuntimeError("marketplace unreachable")
+
+ def execute(self, *args, **kwargs): # pragma: no cover - must not run
+ raise AssertionError("execute must be blocked by the budget gate")
+
+ monkeypatch.setattr(qt.QVerisExecuteTool, "_client", lambda self: FakeClient())
+
+ payload = json.loads(
+ qt.QVerisExecuteTool().execute(
+ tool_id="tool_1", parameters={}, session_id="offline", expected_cost=0
+ )
+ )
+
+ assert payload["status"] == "budget_exceeded"
+
+
+def test_an_honest_caller_quote_is_still_honoured(monkeypatch):
+ """The caller hint keeps working when it is not understating the price."""
+ qt.save_qveris_config(
+ qt.QVerisConfig(
+ enabled=True,
+ api_key="sk-live",
+ mode="paid",
+ budget_credits_per_session=10.0,
+ )
+ )
+
+ class FakeClient:
+ def inspect(self, *args, **kwargs):
+ del args, kwargs
+ return {"results": [{"tool_id": "tool_1", "expected_cost": "1 credit"}]}
+
+ def execute(self, *args, **kwargs):
+ del args, kwargs
+ return {"success": True, "cost": 1.0, "result": {}}
+
+ monkeypatch.setattr(qt.QVerisExecuteTool, "_client", lambda self: FakeClient())
+
+ payload = json.loads(
+ qt.QVerisExecuteTool().execute(
+ tool_id="tool_1", parameters={}, session_id="honest", expected_cost="2 credits"
+ )
+ )
+
+ assert payload["ok"] is True
+ assert qt._SESSION_SPEND["honest"] == pytest.approx(1.0)