-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_usage_plugin.py
More file actions
177 lines (154 loc) · 7.06 KB
/
test_usage_plugin.py
File metadata and controls
177 lines (154 loc) · 7.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
"""Tests for usage tracking plugin persistence behavior."""
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
from lightspeed_agent.api.a2a import usage_plugin
class TestUsageTrackingPlugin:
"""Tests for persistence behavior in usage plugin callbacks."""
@pytest.mark.asyncio
async def test_before_run_persists_request_increment_when_order_present(self):
"""Persist request_count=1 for a valid request order."""
repo = MagicMock()
repo.increment_usage = AsyncMock()
original_get_repo = usage_plugin.get_usage_repository
original_get_order = usage_plugin.get_request_order_id
usage_plugin.get_usage_repository = lambda: repo
usage_plugin.get_request_order_id = lambda: "order-123"
try:
plugin = usage_plugin.UsageTrackingPlugin()
await plugin.before_run_callback(invocation_context=None)
finally:
usage_plugin.get_usage_repository = original_get_repo
usage_plugin.get_request_order_id = original_get_order
repo.increment_usage.assert_awaited_once_with(
order_id="order-123",
request_count=1,
input_tokens=0,
output_tokens=0,
tool_calls=0,
)
@pytest.mark.asyncio
async def test_before_run_skips_persistence_when_order_missing(self):
"""Do not persist increments if request context has no order."""
repo = MagicMock()
repo.increment_usage = AsyncMock()
original_get_repo = usage_plugin.get_usage_repository
original_get_order = usage_plugin.get_request_order_id
usage_plugin.get_usage_repository = lambda: repo
usage_plugin.get_request_order_id = lambda: None
try:
plugin = usage_plugin.UsageTrackingPlugin()
await plugin.before_run_callback(invocation_context=None)
finally:
usage_plugin.get_usage_repository = original_get_repo
usage_plugin.get_request_order_id = original_get_order
repo.increment_usage.assert_not_called()
@pytest.mark.asyncio
async def test_after_model_persists_input_output_tokens(self):
"""Persist LLM token counts for a valid order."""
repo = MagicMock()
repo.increment_usage = AsyncMock()
llm_response = MagicMock()
llm_response.usage_metadata = MagicMock(
prompt_token_count=11, candidates_token_count=7
)
original_get_repo = usage_plugin.get_usage_repository
original_get_order = usage_plugin.get_request_order_id
usage_plugin.get_usage_repository = lambda: repo
usage_plugin.get_request_order_id = lambda: "order-abc"
try:
plugin = usage_plugin.UsageTrackingPlugin()
await plugin.after_model_callback(callback_context=None, llm_response=llm_response)
finally:
usage_plugin.get_usage_repository = original_get_repo
usage_plugin.get_request_order_id = original_get_order
repo.increment_usage.assert_awaited_once_with(
order_id="order-abc",
request_count=0,
input_tokens=11,
output_tokens=7,
tool_calls=0,
)
@pytest.mark.asyncio
async def test_after_tool_persists_tool_call_increment(self):
"""Persist tool_calls=1 for tool callback events."""
repo = MagicMock()
repo.increment_usage = AsyncMock()
original_get_repo = usage_plugin.get_usage_repository
original_get_order = usage_plugin.get_request_order_id
usage_plugin.get_usage_repository = lambda: repo
usage_plugin.get_request_order_id = lambda: "order-tools"
tool = MagicMock()
tool.name = "test_tool"
try:
plugin = usage_plugin.UsageTrackingPlugin()
await plugin.after_tool_callback(
tool=tool,
tool_args={},
tool_context=None,
result={},
)
finally:
usage_plugin.get_usage_repository = original_get_repo
usage_plugin.get_request_order_id = original_get_order
repo.increment_usage.assert_awaited_once_with(
order_id="order-tools",
request_count=0,
input_tokens=0,
output_tokens=0,
tool_calls=1,
)
def _tool_context(invocation_id: str) -> MagicMock:
ctx = MagicMock()
ctx.invocation_id = invocation_id
return ctx
def _invocation_context(invocation_id: str) -> MagicMock:
ctx = MagicMock()
ctx.invocation_id = invocation_id
return ctx
class TestUsageToolCallBudget:
"""Per-invocation tool budget (before_tool_callback)."""
@pytest.mark.asyncio
async def test_before_tool_no_enforcement_when_limit_zero(self):
"""With limit 0, every before_tool call is allowed."""
settings = MagicMock(max_tool_calls_per_invocation=0)
tool = MagicMock()
tool.name = "t"
tc = _tool_context("inv-a")
with patch("lightspeed_agent.api.a2a.usage_plugin.get_settings", return_value=settings):
plugin = usage_plugin.UsageTrackingPlugin()
for _ in range(5):
assert await plugin.before_tool_callback(
tool=tool, tool_args={}, tool_context=tc
) is None
@pytest.mark.asyncio
async def test_before_tool_blocks_after_limit(self):
"""Allow exactly N tool starts, then return a short-circuit error dict."""
settings = MagicMock(max_tool_calls_per_invocation=2)
tool = MagicMock()
tool.name = "t"
tc = _tool_context("inv-limit")
with patch("lightspeed_agent.api.a2a.usage_plugin.get_settings", return_value=settings):
plugin = usage_plugin.UsageTrackingPlugin()
kwargs = {"tool": tool, "tool_args": {}, "tool_context": tc}
assert await plugin.before_tool_callback(**kwargs) is None
assert await plugin.before_tool_callback(**kwargs) is None
blocked = await plugin.before_tool_callback(**kwargs)
assert blocked is not None
assert blocked["code"] == usage_plugin._TOOL_LIMIT_CODE
assert "Exceeded maximum of 2" in blocked["error"]
@pytest.mark.asyncio
async def test_after_run_clears_budget_for_next_run_same_plugin(self):
"""after_run_callback drops counters so a new run can use the budget again."""
settings = MagicMock(max_tool_calls_per_invocation=1)
tool = MagicMock()
tool.name = "t"
tc = _tool_context("inv-reset")
inv = _invocation_context("inv-reset")
with patch("lightspeed_agent.api.a2a.usage_plugin.get_settings", return_value=settings):
plugin = usage_plugin.UsageTrackingPlugin()
kwargs = {"tool": tool, "tool_args": {}, "tool_context": tc}
assert await plugin.before_tool_callback(**kwargs) is None
blocked = await plugin.before_tool_callback(**kwargs)
assert blocked is not None
await plugin.after_run_callback(invocation_context=inv)
assert await plugin.before_tool_callback(**kwargs) is None