The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(mcp): a charge built before the multiplex rekey books onto the moved total
Teknium4dc2d44f427d
diff --git a/tests/tools/test_mcp_tool_costs.py b/tests/tools/test_mcp_tool_costs.pyindex 8d94cc6bebc..e0bb1c3f667 100644--- a/tests/tools/test_mcp_tool_costs.py+++ b/tests/tools/test_mcp_tool_costs.py@@ -47,7 +47,8 @@ def session():patch.dict(mcp_tool._server_trust_levels, {}, clear=True), \patch.dict(mcp_tool._tool_read_only_hints, {}, clear=True), \patch.dict(mcp_tool._server_cost_policies, {}, clear=True), \- patch.dict(mcp_tool_costs._spent, {}, clear=True):+ patch.dict(mcp_tool_costs._spent, {}, clear=True), \+ patch.object(mcp_tool_costs, "_unscoped_moved_to", None):yield session@@ -123,9 +124,13 @@ def test_chats_and_profiles_never_share_a_tally(session):assert a is not None and b is not Nonea.book()assert a.spent() == 40 and b.spent() == 0- # A process that starts multiplexing moves bare-name policies under (scope, name); totals follow.+ # A process that starts multiplexing moves bare-name policies under (scope, name); totals follow,+ # and so does a charge built before the move but booked after it.+ in_flight = mcp_tool_costs.pending_charge("srv", "srv", "paid_render", "chat-a")+ assert in_flight is not Nonemcp_tool_costs.rekey_unscoped("launch")- assert mcp_tool_costs._spent[(("launch", "srv"), "chat-a")] == 120 # 40 + 40 + the approved 40+ in_flight.book()+ assert mcp_tool_costs._spent[(("launch", "srv"), "chat-a")] == 160 # 40 + 40 + the approved 40 + in flightdef test_declarations_validate_translate_and_survive_a_native_trust_override(tmp_path, caplog):diff --git a/tools/mcp_tool_costs.py b/tools/mcp_tool_costs.pyindex 3c11ab0b63f..b0bf9f61702 100644--- a/tools/mcp_tool_costs.py+++ b/tools/mcp_tool_costs.py@@ -104,14 +104,26 @@ _spent: dict[tuple, float] = {}_ask_locks: dict[tuple, threading.Lock] = {}+_unscoped_moved_to: Optional[str] = None # set once the process starts multiplexing++def rekey_unscoped(scope: str) -> None:"""Follow ``mcp_tool_scope.rekey_unscoped_connections``: a process that starts multiplexing moves its- bare-name policies under ``(scope, name)``, and the chats' totals must move with them."""+ bare-name policies under ``(scope, name)``, and the chats' totals must move with them. A charge built+ before the move (booked or refunded after it) resolves to the moved key too."""+ global _unscoped_moved_towith _lock:- for policy_key, session_key in [k for k in _spent if isinstance(k[0], str)]:- amount = _spent.pop((policy_key, session_key))- moved = ((scope, policy_key), session_key)- _spent[moved] = _spent.get(moved, 0.0) + amount+ _unscoped_moved_to = scope+ for key in [k for k in _spent if isinstance(k[0], str)]:+ amount = _spent.pop(key)+ _spent[_live(key)] = _spent.get(_live(key), 0.0) + amount+++def _live(key: tuple) -> tuple:+ policy_key, session_key = key+ if isinstance(policy_key, str) and _unscoped_moved_to is not None:+ return (_unscoped_moved_to, policy_key), session_key+ return key@dataclass(frozen=True)@@ -125,7 +137,7 @@ class Charge:def spent(self) -> float:with _lock:- return _spent.get(self.key, 0.0)+ return _spent.get(_live(self.key), 0.0)def over_limit(self, spent: float) -> bool:return self.policy.limit > 0 and spent + self.cost.high > self.policy.limit@@ -134,7 +146,7 @@ class Charge:"""Book the estimate when it keeps the chat within its limit; ``(booked, spent before)``.Check and book share the lock, so parallel calls cannot both slip under the limit."""with _lock:- spent = _spent.get(self.key, 0.0)+ spent = _spent.get(_live(self.key), 0.0)if self.over_limit(spent):return False, spentself._add_locked(self.cost.high)@@ -149,13 +161,14 @@ class Charge:self._add_locked(-self.cost.high)def _add_locked(self, amount: float) -> None:- _spent[self.key] = max(0.0, _spent.get(self.key, 0.0) + amount)+ key = _live(self.key)+ _spent[key] = max(0.0, _spent.get(key, 0.0) + amount)@contextmanagerdef asking(self) -> Iterator[None]:"""Hold this chat's budget-prompt slot while asking and booking."""with _lock:- slot = _ask_locks.setdefault(self.key, threading.Lock())+ slot = _ask_locks.setdefault(_live(self.key), threading.Lock())with slot:yield