Repository navigation
Expand file tree
/
Copy pathtest_evaluation.py
More file actions
524 lines (431 loc) · 18.8 KB
/
Copy pathtest_evaluation.py
File metadata and controls
524 lines (431 loc) · 18.8 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
"""Tests for check_evaluation behavior."""
from unittest.mock import AsyncMock, MagicMock, patch
from uuid import UUID
import pytest
from agent_control import evaluation
from agent_control.evaluation import EvaluationResult
from agent_control_models import Step
from pydantic import ValidationError
@pytest.mark.asyncio
async def test_check_evaluation_requires_step_name_before_server_call():
"""Typed request validation should reject steps without a name before server call."""
client = MagicMock()
client.http_client = AsyncMock()
client.http_client.post = AsyncMock()
with pytest.raises(ValidationError):
await evaluation.check_evaluation(
client=client,
agent_name=UUID("00000000-0000-0000-0000-000000000001"),
step={"type": "llm", "input": "hello"},
stage="pre",
)
client.http_client.post.assert_not_called()
@pytest.mark.asyncio
async def test_check_evaluation_returns_result_model():
"""check_evaluation returns a parsed EvaluationResult."""
class DummyResponse:
def raise_for_status(self) -> None:
return None
def json(self) -> dict[str, object]:
return {"is_safe": True, "confidence": 0.75, "reason": "ok"}
client = MagicMock()
client.http_client = MagicMock()
client.http_client.post = AsyncMock(return_value=DummyResponse())
result = await evaluation.check_evaluation(
client=client,
agent_name="Agent-Example_01",
step={"type": "llm", "name": "chat", "input": "hello"},
stage="pre",
)
assert result.is_safe is True
assert result.confidence == 0.75
assert result.reason == "ok"
client.http_client.post.assert_awaited_once_with(
"/api/v1/evaluation",
json={
"agent_name": "agent-example_01",
"step": {
"type": "llm",
"name": "chat",
"input": "hello",
"output": None,
"context": None,
"tools": None,
"ground_truth": None,
},
"stage": "pre",
"target_type": None,
"target_id": None,
},
headers=None,
)
@pytest.mark.asyncio
async def test_check_evaluation_sends_custom_step_type_to_server():
"""The server path sends one generic Step inside EvaluationRequest."""
class DummyResponse:
def raise_for_status(self) -> None:
return None
def json(self) -> dict[str, object]:
return {"is_safe": True, "confidence": 1.0}
client = MagicMock()
client.http_client = MagicMock()
client.http_client.post = AsyncMock(return_value=DummyResponse())
await evaluation.check_evaluation(
client=client,
agent_name="Agent-Example_01",
step={"type": "trace", "name": "trace-check", "input": "hello"},
stage="post",
)
request = client.http_client.post.await_args.kwargs["json"]
assert request["step"] == {
"type": "trace",
"name": "trace-check",
"input": "hello",
"output": None,
"context": None,
"tools": None,
"ground_truth": None,
}
@pytest.mark.asyncio
async def test_evaluate_controls_requires_server_url():
"""evaluate_controls should require server_url to be configured."""
with patch("agent_control.state.server_url", None):
with pytest.raises(RuntimeError, match="Server URL not configured"):
await evaluation.evaluate_controls(
step_name="chat",
input="hello",
stage="pre",
agent_name="test-bot",
)
@pytest.mark.asyncio
async def test_evaluate_controls_with_explicit_agent_name(monkeypatch):
"""evaluate_controls should call check_evaluation_with_local."""
mock_result = EvaluationResult(is_safe=True, confidence=1.0)
mock_check = AsyncMock(return_value=mock_result)
monkeypatch.setattr(evaluation, "check_evaluation_with_local", mock_check)
with patch("agent_control.state.server_url", "http://localhost:8000"):
with patch("agent_control.state.api_key", None):
result = await evaluation.evaluate_controls(
step_name="chat",
input="hello",
stage="pre",
agent_name="test-bot",
)
assert result.is_safe is True
assert result.confidence == 1.0
mock_check.assert_called_once()
@pytest.mark.asyncio
async def test_evaluate_controls_with_context(monkeypatch):
"""evaluate_controls should pass context through to evaluation."""
mock_result = EvaluationResult(is_safe=True, confidence=1.0)
mock_check = AsyncMock(return_value=mock_result)
monkeypatch.setattr(evaluation, "check_evaluation_with_local", mock_check)
with patch("agent_control.state.server_url", "http://localhost:8000"):
with patch("agent_control.state.api_key", None):
await evaluation.evaluate_controls(
step_name="chat",
input="hello",
context={"user_id": "123"},
stage="pre",
agent_name="test-bot",
)
assert mock_check.call_args is not None
@pytest.mark.parametrize("step_type", ["trace", "session"])
@pytest.mark.asyncio
async def test_evaluate_controls_accepts_custom_step_type(monkeypatch, step_type):
"""Direct evaluation preserves custom types in the runtime Step."""
mock_check = AsyncMock(return_value=EvaluationResult(is_safe=True, confidence=1.0))
monkeypatch.setattr(evaluation, "check_evaluation_with_local", mock_check)
with patch("agent_control.state.server_url", "http://localhost:8000"):
await evaluation.evaluate_controls(
step_name="trace_check",
step_type=step_type,
input="trace input",
output="trace output",
stage="post",
agent_name="test-bot",
)
step = mock_check.call_args.kwargs["step"]
assert step.type == step_type
assert step.name == "trace_check"
@pytest.mark.asyncio
async def test_evaluate_controls_rejects_empty_step_type(monkeypatch):
"""Direct evaluation rejects empty custom types before evaluation."""
mock_check = AsyncMock(return_value=EvaluationResult(is_safe=True, confidence=1.0))
monkeypatch.setattr(evaluation, "check_evaluation_with_local", mock_check)
with patch("agent_control.state.server_url", "http://localhost:8000"):
with pytest.raises(ValueError, match="non-empty string"):
await evaluation.evaluate_controls(
step_name="trace_check",
step_type="",
agent_name="test-bot",
)
mock_check.assert_not_called()
@pytest.mark.asyncio
async def test_evaluate_controls_preserves_explicit_tools_and_ground_truth(monkeypatch):
"""Explicit structured scorer context is preserved on the SDK Step."""
# Given: a configured SDK and local evaluation boundary
mock_check = AsyncMock(return_value=EvaluationResult(is_safe=True, confidence=1.0))
monkeypatch.setattr(evaluation, "check_evaluation_with_local", mock_check)
tools = [
{"name": "search", "description": "Search", "input_schema": {"type": "object"}}
]
# When: a direct SDK caller supplies tools and ground truth
with patch("agent_control.state.server_url", "http://localhost:8000"):
await evaluation.evaluate_controls(
step_name="chat",
input="question",
output="answer",
tools=tools,
ground_truth={"answer": "expected"},
stage="post",
agent_name="test-bot",
)
# Then: both fields are retained as structured JSON
step = mock_check.call_args.kwargs["step"]
assert step.tools == tools
assert step.ground_truth == {"answer": "expected"}
def test_step_legacy_serialization_is_unchanged_without_children():
"""A Step without children keeps the existing serialized payload."""
step = Step(type="llm", name="chat", input="hello")
assert step.model_dump(mode="json") == {
"type": "llm",
"name": "chat",
"input": "hello",
"output": None,
"context": None,
"tools": None,
"ground_truth": None,
}
assert Step(type="llm", name="chat", input="hello", children=None).children is None
def test_step_accepts_and_preserves_recursive_children():
"""Nested child steps retain all provider-neutral runtime fields."""
child_payload = {
"type": "llm",
"name": "child",
"input": {"prompt": "question"},
"output": "answer",
"context": {"conversation": ["earlier"]},
"tools": [{"name": "lookup", "input_schema": {"type": "object"}}],
"ground_truth": {"answer": "expected"},
"children": [
{"type": "retriever", "name": "search", "input": {"query": "q"}}
],
}
parent = Step(type="llm", name="parent", input="hello", children=[child_payload])
assert parent.children is not None
child = parent.children[0]
assert child.type == child_payload["type"]
assert child.name == child_payload["name"]
assert child.input == child_payload["input"]
assert child.output == child_payload["output"]
assert child.context == child_payload["context"]
assert child.tools == child_payload["tools"]
assert child.ground_truth == child_payload["ground_truth"]
assert child.children is not None
assert child.children[0].input == {"query": "q"}
def test_step_rejects_invalid_child_values_clearly():
with pytest.raises(ValidationError, match="children"):
Step(type="llm", name="parent", input="hello", children=["not a step"])
@pytest.mark.asyncio
async def test_evaluate_controls_passes_children_without_recursively_evaluating(monkeypatch):
"""Children are data on the current Step and are not evaluated separately."""
mock_check = AsyncMock(return_value=EvaluationResult(is_safe=True, confidence=1.0))
monkeypatch.setattr(evaluation, "check_evaluation_with_local", mock_check)
children = [
Step(
type="tool",
name="search",
input={"query": "nested"},
children=[Step(type="llm", name="nested", input="prompt")],
)
]
with patch("agent_control.state.server_url", "http://localhost:8000"):
await evaluation.evaluate_controls(
step_name="parent",
input="hello",
children=children,
stage="pre",
agent_name="test-bot",
)
mock_check.assert_awaited_once()
passed_step = mock_check.call_args.kwargs["step"]
assert passed_step.children == children
assert passed_step.children[0].children == children[0].children
@pytest.mark.asyncio
async def test_evaluate_controls_uses_session_api_key_header(monkeypatch):
"""evaluate_controls should pass init's API-key header into the client."""
mock_result = EvaluationResult(is_safe=True, confidence=1.0)
mock_check = AsyncMock(return_value=mock_result)
monkeypatch.setattr(evaluation, "check_evaluation_with_local", mock_check)
with patch("agent_control.state.server_url", "http://localhost:8000"), patch(
"agent_control.state.api_key", "test-key"
), patch("agent_control.state.api_key_header", "Galileo-API-Key"):
await evaluation.evaluate_controls(
step_name="chat",
input="hello",
stage="pre",
agent_name="test-bot",
)
client = mock_check.call_args.kwargs["client"]
assert client.api_key_header == "Galileo-API-Key"
@pytest.mark.asyncio
async def test_check_evaluation_forwards_target_context():
"""When target_type and target_id are supplied, they are forwarded to the server."""
class DummyResponse:
def raise_for_status(self) -> None:
return None
def json(self) -> dict[str, object]:
return {"is_safe": True, "confidence": 1.0}
client = MagicMock()
client.http_client = MagicMock()
client.http_client.post = AsyncMock(return_value=DummyResponse())
await evaluation.check_evaluation(
client=client,
agent_name="Agent-Example_01",
step={"type": "llm", "name": "chat", "input": "hello"},
stage="pre",
target_type="env",
target_id="prod",
)
sent = client.http_client.post.await_args.kwargs["json"]
assert sent["target_type"] == "env"
assert sent["target_id"] == "prod"
@pytest.mark.asyncio
async def test_evaluate_controls_forwards_target_context(monkeypatch):
"""evaluate_controls passes target_type/target_id into check_evaluation_with_local.
Per the V1 contract, per-call target must match the session target.
The test pins forwarding behavior with state.target_type/state.target_id
set to the same values as the per-call args.
"""
mock_result = EvaluationResult(is_safe=True, confidence=1.0)
mock_check = AsyncMock(return_value=mock_result)
monkeypatch.setattr(evaluation, "check_evaluation_with_local", mock_check)
with patch("agent_control.state.server_url", "http://localhost:8000"), patch(
"agent_control.state.api_key", None
), patch("agent_control._state.state.target_type", "env"), patch(
"agent_control._state.state.target_id", "prod"
):
await evaluation.evaluate_controls(
step_name="chat",
input="hello",
stage="pre",
agent_name="test-bot",
target_type="env",
target_id="prod",
)
kwargs = mock_check.call_args.kwargs
assert kwargs["target_type"] == "env"
assert kwargs["target_id"] == "prod"
@pytest.mark.asyncio
async def test_check_evaluation_does_not_default_target_from_state():
"""``check_evaluation`` is not session-bound.
The caller supplies its own client; the helper does not consult
``init()``-time session state. A caller that omits target params gets
a non-target-bearing request even when the session has a target set —
session-target enforcement lives on the session-bound entry point
(``evaluate_controls``).
"""
class DummyResponse:
def raise_for_status(self) -> None:
return None
def json(self) -> dict[str, object]:
return {"is_safe": True, "confidence": 1.0}
client = MagicMock()
client.http_client = MagicMock()
client.http_client.post = AsyncMock(return_value=DummyResponse())
with patch("agent_control._state.state.target_type", "env"), patch(
"agent_control._state.state.target_id", "prod"
):
await evaluation.check_evaluation(
client=client,
agent_name="Agent-Example_01",
step={"type": "llm", "name": "chat", "input": "hello"},
stage="pre",
)
sent = client.http_client.post.await_args.kwargs["json"]
assert sent["target_type"] is None
assert sent["target_id"] is None
@pytest.mark.asyncio
async def test_check_evaluation_partial_target_pair_rejected():
"""Per-call target params must be supplied together."""
client = MagicMock()
client.http_client = MagicMock()
client.http_client.post = AsyncMock()
with pytest.raises(ValueError):
await evaluation.check_evaluation(
client=client,
agent_name="Agent-Example_01",
step={"type": "llm", "name": "chat", "input": "hello"},
stage="pre",
target_type="env", # target_id missing
)
@pytest.mark.asyncio
async def test_evaluate_controls_per_call_target_must_match_session_target(monkeypatch):
"""A per-call target that disagrees with init()'s target is rejected.
The cached controls are fetched for the session target; accepting a
mismatched per-call target would drive stale local-first evaluation.
"""
mock_check = AsyncMock(return_value=EvaluationResult(is_safe=True, confidence=1.0))
monkeypatch.setattr(evaluation, "check_evaluation_with_local", mock_check)
session_agent = MagicMock(name="session_agent")
with patch("agent_control.state.server_url", "http://localhost:8000"), patch(
"agent_control.state.api_key", None
), patch("agent_control._state.state.current_agent", session_agent), patch(
"agent_control._state.state.target_type", "env"
), patch("agent_control._state.state.target_id", "prod"):
with pytest.raises(ValueError, match="must match the target context fixed at init"):
await evaluation.evaluate_controls(
step_name="chat",
input="hello",
stage="pre",
agent_name="test-bot",
target_type="env",
target_id="staging", # session is "prod"
)
@pytest.mark.asyncio
async def test_evaluate_controls_no_target_session_rejects_per_call_target(monkeypatch):
"""An init() without target context is itself a fixed (None, None) session.
The session's cached controls were fetched without target context, so a
per-call target that supplies an explicit (target_type, target_id) would
drive stale local-first evaluation. Reject the mismatch.
"""
mock_check = AsyncMock(return_value=EvaluationResult(is_safe=True, confidence=1.0))
monkeypatch.setattr(evaluation, "check_evaluation_with_local", mock_check)
session_agent = MagicMock(name="session_agent")
with patch("agent_control.state.server_url", "http://localhost:8000"), patch(
"agent_control.state.api_key", None
), patch("agent_control._state.state.current_agent", session_agent), patch(
"agent_control._state.state.target_type", None
), patch("agent_control._state.state.target_id", None):
with pytest.raises(
ValueError, match="must match the target context fixed at init"
):
await evaluation.evaluate_controls(
step_name="chat",
input="hello",
stage="pre",
agent_name="test-bot",
target_type="env",
target_id="prod",
)
@pytest.mark.asyncio
async def test_evaluate_controls_defaults_target_from_state(monkeypatch):
"""``evaluate_controls`` falls back to state target when params omitted."""
mock_result = EvaluationResult(is_safe=True, confidence=1.0)
mock_check = AsyncMock(return_value=mock_result)
monkeypatch.setattr(evaluation, "check_evaluation_with_local", mock_check)
with patch("agent_control.state.server_url", "http://localhost:8000"), patch(
"agent_control.state.api_key", None
), patch("agent_control._state.state.target_type", "env"), patch(
"agent_control._state.state.target_id", "prod"
):
await evaluation.evaluate_controls(
step_name="chat",
input="hello",
stage="pre",
agent_name="test-bot",
)
kwargs = mock_check.call_args.kwargs
assert kwargs["target_type"] == "env"
assert kwargs["target_id"] == "prod"