|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | 3 | import asyncio |
4 | | -from collections.abc import AsyncGenerator, AsyncIterator, Generator |
| 4 | +from collections.abc import AsyncGenerator, AsyncIterator, Coroutine, Generator |
5 | 5 | import copy |
6 | 6 | from datetime import datetime, timedelta |
7 | 7 | import gzip |
@@ -226,44 +226,144 @@ async def child() -> art.Trajectory: |
226 | 226 | art.current_trajectory(require=True) |
227 | 227 |
|
228 | 228 |
|
229 | | -async def test_group_context_and_async_helpers() -> None: |
230 | | - with art.TrajectoryGroup() as group: |
231 | | - with art.Trajectory() as first: |
232 | | - pass |
233 | | - with art.Trajectory() as second: |
234 | | - pass |
235 | | - assert group.trajectories == [first, second] |
| 229 | +def test_no_capture_hides_enclosing_trajectory_but_allows_nested_capture() -> None: |
| 230 | + def capture_exchange() -> None: |
| 231 | + state, token = begin( |
| 232 | + "POST", |
| 233 | + "https://example.test/v1/chat/completions", |
| 234 | + {"model": "test/model", "messages": []}, |
| 235 | + ) |
| 236 | + reset(token) |
| 237 | + if state is not None: |
| 238 | + state.status_code = 200 |
| 239 | + state.add(json.dumps(CHAT).encode()) |
| 240 | + state.finish() |
| 241 | + |
| 242 | + with art.Trajectory() as outer: |
| 243 | + capture_exchange() |
| 244 | + with art.no_capture(): |
| 245 | + assert art.current_trajectory() is None |
| 246 | + with pytest.raises(RuntimeError, match="No trajectory"): |
| 247 | + art.current_trajectory(require=True) |
| 248 | + capture_exchange() |
| 249 | + with art.Trajectory() as inner: |
| 250 | + capture_exchange() |
| 251 | + with art.no_capture(): |
| 252 | + assert art.current_trajectory() is None |
| 253 | + capture_exchange() |
| 254 | + with art.Trajectory() as nested: |
| 255 | + capture_exchange() |
| 256 | + assert art.current_trajectory() is None |
| 257 | + assert art.current_trajectory() is inner |
| 258 | + capture_exchange() |
| 259 | + assert art.current_trajectory() is None |
| 260 | + assert art.current_trajectory() is outer |
| 261 | + capture_exchange() |
| 262 | + |
| 263 | + with pytest.raises(ValueError, match="restore"): |
| 264 | + with art.no_capture(): |
| 265 | + raise ValueError("restore") |
| 266 | + assert art.current_trajectory() is outer |
| 267 | + |
| 268 | + assert len(outer.exchanges.chat_completions) == 2 |
| 269 | + assert len(inner.exchanges.chat_completions) == 2 |
| 270 | + assert len(nested.exchanges.chat_completions) == 1 |
| 271 | + |
| 272 | + |
| 273 | +def test_no_capture_uses_the_scope_active_when_a_request_begins() -> None: |
| 274 | + with art.Trajectory() as trajectory: |
| 275 | + state, token = begin( |
| 276 | + "POST", |
| 277 | + "https://example.test/v1/chat/completions", |
| 278 | + {"model": "test/model", "messages": []}, |
| 279 | + ) |
| 280 | + reset(token) |
| 281 | + assert state is not None |
| 282 | + |
| 283 | + with art.no_capture(): |
| 284 | + state.status_code = 200 |
| 285 | + state.add(json.dumps(CHAT).encode()) |
| 286 | + state.finish() |
| 287 | + ignored, ignored_token = begin( |
| 288 | + "POST", |
| 289 | + "https://example.test/v1/chat/completions", |
| 290 | + {"model": "test/model", "messages": []}, |
| 291 | + ) |
| 292 | + reset(ignored_token) |
| 293 | + assert ignored is None |
| 294 | + |
| 295 | + assert len(trajectory.exchanges.chat_completions) == 1 |
| 296 | + |
| 297 | + |
| 298 | +async def test_no_capture_context_is_copied_when_tasks_are_created() -> None: |
| 299 | + async def current_after_scheduling() -> art.Trajectory | None: |
| 300 | + await asyncio.sleep(0) |
| 301 | + return art.current_trajectory() |
| 302 | + |
| 303 | + with art.Trajectory() as outer: |
| 304 | + inherited = asyncio.create_task(current_after_scheduling()) |
| 305 | + with art.no_capture(): |
| 306 | + detached = asyncio.create_task(current_after_scheduling()) |
| 307 | + assert await current_after_scheduling() is None |
| 308 | + assert await inherited is outer |
| 309 | + assert await detached is None |
| 310 | + |
| 311 | + |
| 312 | +async def test_async_helpers_and_group_aggregation_are_isolated() -> None: |
| 313 | + def capture_exchange() -> None: |
| 314 | + state, token = begin( |
| 315 | + "POST", |
| 316 | + "https://example.test/v1/chat/completions", |
| 317 | + {"model": "test/model", "messages": []}, |
| 318 | + ) |
| 319 | + reset(token) |
| 320 | + if state is not None: |
| 321 | + state.status_code = 200 |
| 322 | + state.add(json.dumps(CHAT).encode()) |
| 323 | + state.finish() |
236 | 324 |
|
237 | 325 | async def rollout() -> None: |
238 | 326 | await asyncio.sleep(0) |
| 327 | + capture_exchange() |
239 | 328 |
|
240 | 329 | captured = await art.trajectory(rollout()) |
241 | 330 | assert isinstance(captured, art.Trajectory) |
| 331 | + assert len(captured.exchanges.chat_completions) == 1 |
242 | 332 | task = asyncio.create_task(rollout()) |
243 | 333 | with pytest.raises(TypeError, match="raw coroutine"): |
244 | 334 | # Passing a Task is deliberately a static type error and a runtime error. |
245 | 335 | await art.trajectory(task) # ty: ignore[invalid-argument-type] |
246 | 336 | await task |
247 | 337 |
|
| 338 | + async def unscoped() -> art.Trajectory: |
| 339 | + assert art.current_trajectory() is None |
| 340 | + capture_exchange() |
| 341 | + return art.Trajectory() |
| 342 | + |
248 | 343 | async def failed() -> art.Trajectory: |
249 | 344 | raise ValueError("boom") |
250 | 345 |
|
251 | | - successful = art.trajectory(rollout()) |
252 | | - result = await art.trajectory_group([successful, failed()], return_exceptions=True) |
253 | | - assert len(result.trajectories) == 1 |
254 | | - assert result.exceptions[0].message == "boom" |
255 | | - |
256 | | - |
257 | | -def test_failed_trajectory_context_records_exception_without_trajectory() -> None: |
258 | | - group = art.TrajectoryGroup() |
| 346 | + def generated() -> Generator[Coroutine[Any, Any, art.Trajectory], None, None]: |
| 347 | + capture_exchange() |
| 348 | + yield art.trajectory(rollout()) |
259 | 349 |
|
260 | | - with pytest.raises(ValueError, match="boom"): |
261 | | - with group: |
262 | | - with art.Trajectory() as failed: |
263 | | - raise ValueError("boom") |
| 350 | + with art.Trajectory() as outer: |
| 351 | + successful = art.trajectory(rollout()) |
| 352 | + result = await art.trajectory_group( |
| 353 | + [successful, unscoped(), failed()], |
| 354 | + return_exceptions=True, |
| 355 | + ) |
| 356 | + generated_result = await art.trajectory_group(generated()) |
| 357 | + with pytest.raises(ValueError, match="boom"): |
| 358 | + await art.trajectory_group([failed()]) |
| 359 | + assert art.current_trajectory() is outer |
264 | 360 |
|
265 | | - assert failed not in group.trajectories |
266 | | - assert [exception.message for exception in group.exceptions] == ["boom"] |
| 361 | + assert len(result.trajectories) == 2 |
| 362 | + assert len(result.trajectories[0].exchanges.chat_completions) == 1 |
| 363 | + assert not result.trajectories[1].exchanges |
| 364 | + assert result.exceptions[0].message == "boom" |
| 365 | + assert len(generated_result.trajectories[0].exchanges.chat_completions) == 1 |
| 366 | + assert not outer.exchanges |
267 | 367 |
|
268 | 368 |
|
269 | 369 | def test_sync_group_generator_initializes_once( |
|
0 commit comments