|
1 | 1 | # Copyright (c) Microsoft. All rights reserved. |
2 | 2 |
|
| 3 | +import asyncio |
3 | 4 | import logging |
4 | 5 | from collections.abc import AsyncIterable, Awaitable, Mapping, Sequence |
5 | 6 | from typing import Any, cast |
@@ -5249,7 +5250,212 @@ class FailingExecuteAgent(AgentTelemetryLayer, _FailingExecuteAgent): # type: i |
5249 | 5250 | assert agent_spans[0].status.status_code == StatusCode.ERROR |
5250 | 5251 |
|
5251 | 5252 |
|
5252 | | -# region Test heavy operations skipped when span is not recording |
| 5253 | +@pytest.mark.parametrize("enable_sensitive_data", [True], indirect=True) |
| 5254 | +async def test_agent_run_contextvars_safe_when_awaited_in_different_context( |
| 5255 | + span_exporter: InMemorySpanExporter, enable_sensitive_data |
| 5256 | +): |
| 5257 | + """``run()`` is a sync method that returns an awaitable; the telemetry contextvar set and reset |
| 5258 | + must happen in the same execution context so the returned coroutine can be awaited in a different |
| 5259 | + context. |
| 5260 | +
|
| 5261 | + Regression for background agents (``BackgroundAgentsProvider``), which do |
| 5262 | + ``asyncio.create_task(agent.run(...))``: ``run()`` executes synchronously in the parent context |
| 5263 | + while the returned coroutine is awaited in a fresh copied context. If the contextvar token were |
| 5264 | + created eagerly in the parent context but reset inside the coroutine, this raised |
| 5265 | + ``ValueError: <Token ...> was created in a different Context``. |
| 5266 | + """ |
| 5267 | + |
| 5268 | + class _SimpleAgent: |
| 5269 | + AGENT_PROVIDER_NAME = "test_provider" |
| 5270 | + |
| 5271 | + def __init__(self): |
| 5272 | + self._id = "simple" |
| 5273 | + self._name = "Simple" |
| 5274 | + self._description = "Agent that returns a response without raising" |
| 5275 | + self._default_options: dict[str, Any] = {} |
| 5276 | + |
| 5277 | + @property |
| 5278 | + def id(self): |
| 5279 | + return self._id |
| 5280 | + |
| 5281 | + @property |
| 5282 | + def name(self): |
| 5283 | + return self._name |
| 5284 | + |
| 5285 | + @property |
| 5286 | + def description(self): |
| 5287 | + return self._description |
| 5288 | + |
| 5289 | + @property |
| 5290 | + def default_options(self): |
| 5291 | + return self._default_options |
| 5292 | + |
| 5293 | + def run(self, messages=None, *, stream: bool = False, session=None, **kwargs): |
| 5294 | + async def _inner() -> AgentResponse: |
| 5295 | + return AgentResponse(messages=[Message(role="assistant", contents=["hi"])]) |
| 5296 | + |
| 5297 | + return _inner() |
| 5298 | + |
| 5299 | + class SimpleAgent(AgentTelemetryLayer, _SimpleAgent): # type: ignore[misc] |
| 5300 | + pass |
| 5301 | + |
| 5302 | + agent = SimpleAgent() |
| 5303 | + span_exporter.clear() |
| 5304 | + |
| 5305 | + # Mimic BackgroundAgentsProvider: invoke run() synchronously in this context, then await the |
| 5306 | + # returned coroutine inside a separate task (a different/copied context). |
| 5307 | + awaitable = agent.run(messages="Hello", stream=False) |
| 5308 | + |
| 5309 | + async def _runner(aw): |
| 5310 | + return await aw |
| 5311 | + |
| 5312 | + result = await asyncio.create_task(_runner(awaitable)) |
| 5313 | + assert isinstance(result, AgentResponse) |
| 5314 | + |
| 5315 | + spans = span_exporter.get_finished_spans() |
| 5316 | + agent_spans = [s for s in spans if s.attributes.get(OtelAttr.OPERATION.value) == OtelAttr.AGENT_INVOKE_OPERATION] # type: ignore[union-attr] # ty: ignore[unresolved-attribute] |
| 5317 | + assert len(agent_spans) == 1 |
| 5318 | + assert agent_spans[0].status.status_code != StatusCode.ERROR |
| 5319 | + |
| 5320 | + |
| 5321 | +@pytest.mark.parametrize("enable_sensitive_data", [True], indirect=True) |
| 5322 | +async def test_agent_run_error_path_contextvars_safe_when_awaited_in_different_context( |
| 5323 | + span_exporter: InMemorySpanExporter, enable_sensitive_data |
| 5324 | +): |
| 5325 | + """Error-path variant: the coroutine returned by ``run()`` raises, and is awaited in a different |
| 5326 | + context via ``asyncio.create_task``. The telemetry contextvars are set and reset inside the |
| 5327 | + coroutine (its ``finally``), so the reset on the exception path must not raise |
| 5328 | + ``ValueError: <Token ...> was created in a different Context``; the original error must surface. |
| 5329 | + """ |
| 5330 | + |
| 5331 | + class _FailingRunAgent: |
| 5332 | + AGENT_PROVIDER_NAME = "test_provider" |
| 5333 | + |
| 5334 | + def __init__(self): |
| 5335 | + self._id = "failing_run" |
| 5336 | + self._name = "Failing Run" |
| 5337 | + self._description = "Agent whose run coroutine raises" |
| 5338 | + self._default_options: dict[str, Any] = {} |
| 5339 | + |
| 5340 | + @property |
| 5341 | + def id(self): |
| 5342 | + return self._id |
| 5343 | + |
| 5344 | + @property |
| 5345 | + def name(self): |
| 5346 | + return self._name |
| 5347 | + |
| 5348 | + @property |
| 5349 | + def description(self): |
| 5350 | + return self._description |
| 5351 | + |
| 5352 | + @property |
| 5353 | + def default_options(self): |
| 5354 | + return self._default_options |
| 5355 | + |
| 5356 | + def run(self, messages=None, *, stream: bool = False, session=None, **kwargs): |
| 5357 | + async def _inner() -> AgentResponse: |
| 5358 | + raise RuntimeError("run failed") |
| 5359 | + |
| 5360 | + return _inner() |
| 5361 | + |
| 5362 | + class FailingRunAgent(AgentTelemetryLayer, _FailingRunAgent): # type: ignore[misc] |
| 5363 | + pass |
| 5364 | + |
| 5365 | + agent = FailingRunAgent() |
| 5366 | + span_exporter.clear() |
| 5367 | + |
| 5368 | + awaitable = agent.run(messages="Hello", stream=False) |
| 5369 | + |
| 5370 | + async def _runner(aw): |
| 5371 | + return await aw |
| 5372 | + |
| 5373 | + # The original RuntimeError must propagate unchanged — not a cross-context ValueError from the |
| 5374 | + # contextvar reset in the coroutine's finally block. |
| 5375 | + with pytest.raises(RuntimeError, match="run failed"): |
| 5376 | + await asyncio.create_task(_runner(awaitable)) |
| 5377 | + |
| 5378 | + spans = span_exporter.get_finished_spans() |
| 5379 | + agent_spans = [s for s in spans if s.attributes.get(OtelAttr.OPERATION.value) == OtelAttr.AGENT_INVOKE_OPERATION] # type: ignore[union-attr] # ty: ignore[unresolved-attribute] |
| 5380 | + assert len(agent_spans) == 1 |
| 5381 | + assert agent_spans[0].status.status_code == StatusCode.ERROR |
| 5382 | + |
| 5383 | + |
| 5384 | +@pytest.mark.parametrize("enable_sensitive_data", [True], indirect=True) |
| 5385 | +async def test_agent_streaming_contextvars_safe_when_consumed_in_different_context( |
| 5386 | + span_exporter: InMemorySpanExporter, enable_sensitive_data |
| 5387 | +): |
| 5388 | + """``run(stream=True)`` returns a ``ResponseStream`` synchronously, but its cleanup hooks (which |
| 5389 | + reset the telemetry contextvars) run when the stream is *consumed* — possibly in a different |
| 5390 | + context (e.g. ``stream = agent.run(stream=True)`` then ``await asyncio.create_task(consume(stream))``). |
| 5391 | +
|
| 5392 | + The contextvars are therefore set lazily on the first pull, in the consuming context, so the set |
| 5393 | + and the reset both run there. Otherwise this raised |
| 5394 | + ``ValueError: <Token ...> was created in a different Context``. |
| 5395 | + """ |
| 5396 | + from agent_framework import AgentResponseUpdate |
| 5397 | + |
| 5398 | + class _StreamingAgent: |
| 5399 | + AGENT_PROVIDER_NAME = "test_provider" |
| 5400 | + |
| 5401 | + def __init__(self): |
| 5402 | + self._id = "streaming_xctx" |
| 5403 | + self._name = "Streaming XCtx" |
| 5404 | + self._description = "Streaming agent for cross-context consumption" |
| 5405 | + self._default_options: dict[str, Any] = {} |
| 5406 | + |
| 5407 | + @property |
| 5408 | + def id(self): |
| 5409 | + return self._id |
| 5410 | + |
| 5411 | + @property |
| 5412 | + def name(self): |
| 5413 | + return self._name |
| 5414 | + |
| 5415 | + @property |
| 5416 | + def description(self): |
| 5417 | + return self._description |
| 5418 | + |
| 5419 | + @property |
| 5420 | + def default_options(self): |
| 5421 | + return self._default_options |
| 5422 | + |
| 5423 | + def run(self, messages=None, *, stream: bool = False, session=None, **kwargs): |
| 5424 | + if stream: |
| 5425 | + |
| 5426 | + async def _stream(): |
| 5427 | + yield AgentResponseUpdate(contents=[Content.from_text("Hello ")], role="assistant") |
| 5428 | + yield AgentResponseUpdate(contents=[Content.from_text("World")], role="assistant") |
| 5429 | + |
| 5430 | + return ResponseStream(_stream(), finalizer=AgentResponse.from_updates) |
| 5431 | + raise NotImplementedError |
| 5432 | + |
| 5433 | + class StreamingAgent(AgentTelemetryLayer, _StreamingAgent): # type: ignore[misc] |
| 5434 | + pass |
| 5435 | + |
| 5436 | + agent = StreamingAgent() |
| 5437 | + span_exporter.clear() |
| 5438 | + |
| 5439 | + # Create the stream synchronously in this context, then consume it inside a separate task (a |
| 5440 | + # different/copied context) — mirroring how a caller might hand the stream off to be drained. |
| 5441 | + stream = agent.run(messages="Hello", stream=True) |
| 5442 | + |
| 5443 | + async def _consume(s): |
| 5444 | + collected = [] |
| 5445 | + async for update in s: |
| 5446 | + collected.append(update) |
| 5447 | + await s.get_final_response() |
| 5448 | + return collected |
| 5449 | + |
| 5450 | + updates = await asyncio.create_task(_consume(stream)) |
| 5451 | + assert len(updates) == 2 |
| 5452 | + |
| 5453 | + spans = span_exporter.get_finished_spans() |
| 5454 | + agent_spans = [s for s in spans if s.attributes.get(OtelAttr.OPERATION.value) == OtelAttr.AGENT_INVOKE_OPERATION] # type: ignore[union-attr] # ty: ignore[unresolved-attribute] |
| 5455 | + assert len(agent_spans) == 1 |
| 5456 | + assert agent_spans[0].status.status_code != StatusCode.ERROR |
| 5457 | + |
| 5458 | + |
5253 | 5459 | # |
5254 | 5460 | # When ``ENABLE_INSTRUMENTATION`` is on (the default) but no OpenTelemetry |
5255 | 5461 | # tracer provider has been configured, the global provider is the |
|
0 commit comments