-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathtest_data_driven_task_manager.py
More file actions
483 lines (397 loc) · 18.8 KB
/
Copy pathtest_data_driven_task_manager.py
File metadata and controls
483 lines (397 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
"""
Tests for data-driven evaluation functionality in TaskManager.
This module specifically tests the enhanced TaskManager capabilities for:
- Loading and processing JSONL datasets
- Executing multiple rollouts per sample
- Aggregating results across data-driven evaluations
- Error handling and edge cases
"""
import asyncio
import json
import tempfile
from pathlib import Path
from typing import Any, Dict
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
from eval_protocol.agent.orchestrator import Orchestrator
from eval_protocol.agent.task_manager import TaskManager
from eval_protocol.models import TaskDefinitionModel
class TestDataDrivenTaskManager:
"""Tests for data-driven evaluation in TaskManager."""
def setup_method(self):
"""Set up a TaskManager instance for each test."""
self.task_manager = TaskManager()
def create_test_dataset(self, samples: list) -> str:
"""Helper to create a temporary JSONL dataset file."""
with tempfile.NamedTemporaryFile(mode="w", suffix=".jsonl", delete=False) as f:
for sample in samples:
json.dump(sample, f)
f.write("\n")
return f.name
def create_test_task_definition(
self, dataset_path: str = None, num_rollouts_per_sample: int = 1
) -> TaskDefinitionModel:
"""Helper to create a test task definition."""
task_def_dict = {
"name": "test_data_driven_task",
"description": "Test task for data-driven evaluation",
"resource_type": "http_rollout",
"base_resource_config": {"base_url": "http://localhost:8080"},
"reward_function_path": "test.reward_function",
"messages": [{"role": "user", "content": "Test message"}],
}
if dataset_path:
task_def_dict["dataset_path"] = dataset_path
task_def_dict["num_rollouts_per_sample"] = num_rollouts_per_sample
else:
task_def_dict["num_rollouts"] = 3
return TaskDefinitionModel(**task_def_dict)
class TestDatasetLoading(TestDataDrivenTaskManager):
"""Tests for dataset loading functionality."""
def test_load_simple_dataset(self):
"""Test loading a simple JSONL dataset."""
samples = [
{"id": "sample1", "seed": 42, "difficulty": "easy"},
{"id": "sample2", "seed": 123, "difficulty": "medium"},
{"id": "sample3", "seed": 999, "difficulty": "hard"},
]
dataset_path = self.create_test_dataset(samples)
try:
loaded_samples = self.task_manager._load_dataset_samples(dataset_path)
assert len(loaded_samples) == 3
assert loaded_samples == samples
finally:
Path(dataset_path).unlink()
def test_load_dataset_with_empty_lines(self):
"""Test loading dataset that contains empty lines."""
# Create dataset with empty lines
with tempfile.NamedTemporaryFile(mode="w", suffix=".jsonl", delete=False) as f:
f.write('{"id": "sample1", "seed": 42}\n')
f.write("\n") # Empty line
f.write(" \n") # Whitespace only line
f.write('{"id": "sample2", "seed": 123}\n')
dataset_path = f.name
try:
loaded_samples = self.task_manager._load_dataset_samples(dataset_path)
assert len(loaded_samples) == 2
assert loaded_samples[0]["id"] == "sample1"
assert loaded_samples[1]["id"] == "sample2"
finally:
Path(dataset_path).unlink()
def test_load_dataset_with_complex_objects(self):
"""Test loading dataset with complex nested objects."""
samples = [
{
"id": "complex_sample1",
"seed": 42,
"config": {
"map_size": "4x4",
"slippery": True,
"holes": [[1, 1], [2, 3]],
},
"metadata": {"author": "test", "version": "1.0"},
},
{
"id": "complex_sample2",
"seed": 123,
"config": {"map_size": "8x8", "slippery": False, "holes": []},
"metadata": {"author": "test", "version": "1.1"},
},
]
dataset_path = self.create_test_dataset(samples)
try:
loaded_samples = self.task_manager._load_dataset_samples(dataset_path)
assert len(loaded_samples) == 2
assert loaded_samples == samples
# Verify complex objects are preserved
assert loaded_samples[0]["config"]["holes"] == [[1, 1], [2, 3]]
assert loaded_samples[1]["metadata"]["version"] == "1.1"
finally:
Path(dataset_path).unlink()
def test_load_dataset_error_handling(self):
"""Test error handling for various dataset loading scenarios."""
# Test nonexistent file
samples = self.task_manager._load_dataset_samples("nonexistent_file.jsonl")
assert samples == []
# Test malformed JSON
with tempfile.NamedTemporaryFile(mode="w", suffix=".jsonl", delete=False) as f:
f.write('{"valid": "json"}\n')
f.write("{invalid json}\n")
f.write('{"another": "valid"}\n')
malformed_path = f.name
try:
loaded_samples = self.task_manager._load_dataset_samples(malformed_path)
# Should skip malformed line and load valid ones
assert len(loaded_samples) == 2
assert loaded_samples[0]["valid"] == "json"
assert loaded_samples[1]["another"] == "valid"
finally:
Path(malformed_path).unlink()
@pytest.mark.asyncio
class TestDataDrivenExecution(TestDataDrivenTaskManager):
"""Tests for data-driven execution logic."""
async def test_execute_data_driven_rollouts_basic(self):
"""Test basic data-driven rollout execution."""
samples = [{"id": "sample1", "seed": 42}, {"id": "sample2", "seed": 123}]
# Mock the orchestrator execution
with (
patch.object(self.task_manager, "_start_resource_server", return_value=8080),
patch.object(self.task_manager, "_stop_resource_server"),
patch("eval_protocol.agent.task_manager.Orchestrator") as mock_orchestrator_class,
):
# Set up mock orchestrator
mock_orchestrator = AsyncMock()
mock_orchestrator.setup_base_resource = AsyncMock()
mock_orchestrator.execute_task_poc = AsyncMock(
side_effect=[
{"score": 1.0, "reason": "Success"},
{"score": 0.0, "reason": "Failed"},
]
)
mock_orchestrator.base_resource = AsyncMock()
mock_orchestrator_class.return_value = mock_orchestrator
# Create task definition
task_def = self.create_test_task_definition(num_rollouts_per_sample=1)
self.task_manager.register_task("test_task", task_def)
# Execute data-driven rollouts
results = await self.task_manager._execute_data_driven_rollouts(
"test_task", samples, rollouts_per_sample=1, max_concurrency=2
)
assert len(results) == 2
assert results[0]["score"] == 1.0
assert results[1]["score"] == 0.0
assert results[0]["sample_data"] == samples[0]
assert results[1]["sample_data"] == samples[1]
async def test_execute_multiple_rollouts_per_sample(self):
"""Test executing multiple rollouts per sample."""
samples = [{"id": "sample1", "seed": 42}]
rollouts_per_sample = 3
with (
patch.object(self.task_manager, "_start_resource_server", return_value=8080),
patch.object(self.task_manager, "_stop_resource_server"),
patch("eval_protocol.agent.task_manager.Orchestrator") as mock_orchestrator_class,
):
# Set up mock orchestrator to return different scores for each rollout
mock_orchestrator = AsyncMock()
mock_orchestrator.setup_base_resource = AsyncMock()
mock_orchestrator.execute_task_poc = AsyncMock(
side_effect=[
{"score": 1.0, "reason": "Success"},
{"score": 0.5, "reason": "Partial"},
{"score": 0.0, "reason": "Failed"},
]
)
mock_orchestrator.base_resource = AsyncMock()
mock_orchestrator_class.return_value = mock_orchestrator
# Create task definition
task_def = self.create_test_task_definition(num_rollouts_per_sample=rollouts_per_sample)
self.task_manager.register_task("test_task", task_def)
# Execute data-driven rollouts
results = await self.task_manager._execute_data_driven_rollouts(
"test_task",
samples,
rollouts_per_sample=rollouts_per_sample,
max_concurrency=2,
)
# Should have 3 results (1 sample × 3 rollouts)
assert len(results) == 3
# All results should have same sample data but different rollout indices
for i, result in enumerate(results):
assert result["sample_data"] == samples[0]
assert result["rollout_index"] == i
assert result["sample_index"] == 0
async def test_execute_data_driven_with_failures(self):
"""Test handling of failures during data-driven execution."""
samples = [{"id": "sample1", "seed": 42}, {"id": "sample2", "seed": 123}]
with (
patch.object(self.task_manager, "_start_resource_server", return_value=8080),
patch.object(self.task_manager, "_stop_resource_server"),
patch("eval_protocol.agent.task_manager.Orchestrator") as mock_orchestrator_class,
):
# Set up mock orchestrator with one success and one failure
mock_orchestrator = AsyncMock()
mock_orchestrator.setup_base_resource = AsyncMock()
mock_orchestrator.execute_task_poc = AsyncMock(
side_effect=[
{"score": 1.0, "reason": "Success"},
Exception("Execution failed"),
]
)
mock_orchestrator.base_resource = AsyncMock()
mock_orchestrator_class.return_value = mock_orchestrator
# Create task definition
task_def = self.create_test_task_definition(num_rollouts_per_sample=1)
self.task_manager.register_task("test_task", task_def)
# Execute data-driven rollouts
results = await self.task_manager._execute_data_driven_rollouts(
"test_task", samples, rollouts_per_sample=1, max_concurrency=2
)
assert len(results) == 2
# First should be successful
assert results[0]["score"] == 1.0
assert "error" not in results[0]
# Second should have error
assert "error" in results[1]
assert "Execution failed" in results[1]["error"]
async def test_concurrency_limiting(self):
"""Test that concurrency is properly limited."""
samples = [{"id": f"sample{i}", "seed": i} for i in range(10)]
max_concurrency = 3
# Track concurrent executions
concurrent_count = 0
max_concurrent_observed = 0
async def mock_execute(*args, **kwargs):
nonlocal concurrent_count, max_concurrent_observed
concurrent_count += 1
max_concurrent_observed = max(max_concurrent_observed, concurrent_count)
# Simulate some async work
await asyncio.sleep(0.1)
concurrent_count -= 1
return {"score": 1.0, "reason": "Success"}
with (
patch.object(self.task_manager, "_start_resource_server", return_value=8080),
patch.object(self.task_manager, "_stop_resource_server"),
patch("eval_protocol.agent.task_manager.Orchestrator") as mock_orchestrator_class,
):
mock_orchestrator = AsyncMock()
mock_orchestrator.setup_base_resource = AsyncMock()
mock_orchestrator.execute_task_poc = AsyncMock(side_effect=mock_execute)
mock_orchestrator.base_resource = AsyncMock()
mock_orchestrator_class.return_value = mock_orchestrator
# Create task definition
task_def = self.create_test_task_definition(num_rollouts_per_sample=1)
self.task_manager.register_task("test_task", task_def)
# Execute with concurrency limit
results = await self.task_manager._execute_data_driven_rollouts(
"test_task",
samples,
rollouts_per_sample=1,
max_concurrency=max_concurrency,
)
assert len(results) == 10
# Should not exceed max concurrency
assert max_concurrent_observed <= max_concurrency
@pytest.mark.asyncio
class TestTaskExecutionFlow(TestDataDrivenTaskManager):
"""Tests for complete task execution flow with data-driven evaluation."""
async def test_execute_tasks_data_driven_vs_traditional(self):
"""Test that TaskManager correctly chooses between data-driven and traditional execution."""
# Create dataset
samples = [{"id": "sample1", "seed": 42}]
dataset_path = self.create_test_dataset(samples)
try:
# Create data-driven task
data_driven_task = self.create_test_task_definition(dataset_path=dataset_path, num_rollouts_per_sample=2)
# Create traditional task
traditional_task = self.create_test_task_definition() # No dataset_path
self.task_manager.register_task("data_driven", data_driven_task)
self.task_manager.register_task("traditional", traditional_task)
with (
patch.object(self.task_manager, "_execute_data_driven_rollouts") as mock_data_driven,
patch.object(self.task_manager, "_execute_batch_rollouts") as mock_traditional,
):
mock_data_driven.return_value = [{"score": 1.0}]
mock_traditional.return_value = [{"score": 0.5}]
# Execute both tasks
results = await self.task_manager.execute_tasks(["data_driven", "traditional"], max_concurrency=2)
# Verify correct execution methods were called
mock_data_driven.assert_called_once()
mock_traditional.assert_called_once()
# Verify results
assert len(results) == 2
assert "data_driven" in results
assert "traditional" in results
finally:
Path(dataset_path).unlink()
async def test_data_driven_evaluation_with_empty_dataset(self):
"""Test handling of empty dataset."""
# Create empty dataset
empty_dataset_path = self.create_test_dataset([])
try:
task_def = self.create_test_task_definition(dataset_path=empty_dataset_path, num_rollouts_per_sample=1)
self.task_manager.register_task("empty_dataset_task", task_def)
results = await self.task_manager.execute_tasks(["empty_dataset_task"])
# Should handle empty dataset gracefully
assert "empty_dataset_task" in results
assert "error" in results["empty_dataset_task"]
assert "empty" in results["empty_dataset_task"]["error"].lower()
finally:
Path(empty_dataset_path).unlink()
async def test_data_driven_evaluation_file_not_found(self):
"""Test handling of missing dataset file."""
task_def = self.create_test_task_definition(
dataset_path="nonexistent_dataset.jsonl", num_rollouts_per_sample=1
)
self.task_manager.register_task("missing_file_task", task_def)
results = await self.task_manager.execute_tasks(["missing_file_task"])
# Should handle missing file gracefully
assert "missing_file_task" in results
assert "error" in results["missing_file_task"]
class TestResultAggregation(TestDataDrivenTaskManager):
"""Tests for result aggregation in data-driven evaluation."""
def test_aggregate_data_driven_results(self):
"""Test aggregation of results from data-driven evaluation."""
# Simulate results from multiple samples and rollouts
rollout_results = [
# Sample 0, rollouts 0-2
{
"score": 1.0,
"sample_index": 0,
"rollout_index": 0,
"sample_data": {"seed": 42},
},
{
"score": 0.5,
"sample_index": 0,
"rollout_index": 1,
"sample_data": {"seed": 42},
},
{
"score": 0.0,
"sample_index": 0,
"rollout_index": 2,
"sample_data": {"seed": 42},
},
# Sample 1, rollouts 0-1
{
"score": 1.0,
"sample_index": 1,
"rollout_index": 0,
"sample_data": {"seed": 123},
},
{
"score": 1.0,
"sample_index": 1,
"rollout_index": 1,
"sample_data": {"seed": 123},
},
]
aggregated = self.task_manager._aggregate_results(rollout_results)
# Check basic aggregation
assert aggregated["total_rollouts"] == 5
assert aggregated["successful_rollouts"] == 5 # No failed rollouts
assert aggregated["success_rate"] == 1.0
# Check score statistics
expected_avg_score = (1.0 + 0.5 + 0.0 + 1.0 + 1.0) / 5 # 0.7
assert abs(aggregated["average_score"] - expected_avg_score) < 0.001
# Check detailed results are preserved
assert "detailed_results" in aggregated
assert len(aggregated["detailed_results"]) == 5
def test_aggregate_results_with_failures(self):
"""Test aggregation when some rollouts failed."""
rollout_results = [
{"score": 1.0, "sample_index": 0, "rollout_index": 0},
{"error": "Failed", "sample_index": 0, "rollout_index": 1},
{"score": 0.5, "sample_index": 1, "rollout_index": 0},
{"error": "Another failure", "sample_index": 1, "rollout_index": 1},
]
aggregated = self.task_manager._aggregate_results(rollout_results)
assert aggregated["total_rollouts"] == 4
assert aggregated["successful_rollouts"] == 2
assert aggregated["failed_rollouts"] == 2
assert aggregated["success_rate"] == 0.5
# Average should only consider successful rollouts
expected_avg_score = (1.0 + 0.5) / 2 # 0.75
assert abs(aggregated["average_score"] - expected_avg_score) < 0.001
if __name__ == "__main__":
pytest.main([__file__, "-v"])