Files
livekit-agents/tests/test_coverage_spans.py
Théo Monnom c0aa5612f4 Merge commit 'b58e83f5c' into HEAD
# Conflicts:
#	livekit-agents/livekit/agents/llm/utils.py
#	livekit-agents/livekit/agents/version.py
#	uv.lock
2026-10-12 14:05:55 -07:00

475 lines
19 KiB
Python

"""Coverage additions on existing spans: interruption detail on ``agent_turn``, the
``update_agent`` handoff span, and fallback-adapter events on the request span."""
from __future__ import annotations
import asyncio
import contextlib
import time
from collections.abc import Iterator
from typing import Any
from unittest.mock import AsyncMock
import pytest
from opentelemetry.sdk.trace import ReadableSpan, TracerProvider
from opentelemetry.sdk.trace.export import SimpleSpanProcessor
from opentelemetry.sdk.trace.export.in_memory_span_exporter import InMemorySpanExporter
from opentelemetry.trace import StatusCode
from livekit.agents import Agent, APIConnectionError, LiveSession, llm
from livekit.agents.llm import ChatContext, FallbackAdapter, LLMStream, Tool
from livekit.agents.telemetry import (
set_tracer_provider,
trace_types,
tracer,
utils as telemetry_utils,
)
from livekit.agents.tts import FallbackAdapter as TTSFallbackAdapter, StreamAdapter
from livekit.agents.types import DEFAULT_API_CONNECT_OPTIONS, APIConnectOptions
from livekit.agents.voice import generation
from livekit.agents.voice.agent_activity import AgentActivity
from livekit.agents.voice.io import ModelSettings
from livekit.agents.voice.transcription.synchronizer import _SyncedAudioOutput
from .fake_io import FakeAudioInput
from .fake_llm import FakeLLM, FakeLLMResponse
from .fake_session import FakeActions, create_session, run_session
from .fake_stt import FakeSTT
from .fake_tts import FakeTTS
from .trace_schema import assert_trace_well_formed
pytestmark = [pytest.mark.unit, pytest.mark.no_concurrent]
@pytest.fixture
def span_exporter() -> Iterator[InMemorySpanExporter]:
original_provider = tracer._tracer_provider
provider = TracerProvider()
exporter = InMemorySpanExporter()
provider.add_span_processor(SimpleSpanProcessor(exporter))
set_tracer_provider(provider)
try:
yield exporter
finally:
set_tracer_provider(original_provider)
provider.shutdown()
def _spans(exporter: InMemorySpanExporter, name: str) -> list[ReadableSpan]:
return [s for s in exporter.get_finished_spans() if s.name == name]
def _events(span: ReadableSpan, name: str) -> list[Any]:
return [e for e in span.events if e.name == name]
# -- interruption detail --
async def test_barge_in_records_source_and_playout_position(
span_exporter: InMemorySpanExporter,
) -> None:
speed = 2.0
actions = FakeActions()
actions.add_user_speech(0.5, 2.5, "Tell me a story.")
actions.add_llm("Here is a long story for you ... the end.")
actions.add_tts(10.0) # playout starts at ~3.5s
actions.add_user_speech(5.0, 6.0, "Stop!", stt_delay=0.2) # barge-in at ~5.5s
session = create_session(actions, speed_factor=speed)
await asyncio.wait_for(
run_session(session, Agent(instructions="test"), drain_delay=1.0), timeout=60
)
interrupted = [
s
for s in _spans(span_exporter, "agent_turn")
if (s.attributes or {}).get(trace_types.ATTR_SPEECH_INTERRUPTED) is True
]
assert len(interrupted) == 1
turn = interrupted[0]
assert (turn.attributes or {})[trace_types.ATTR_INTERRUPTION_SOURCE] == "audio_activity"
position = (turn.attributes or {})[trace_types.ATTR_PLAYOUT_POSITION]
assert isinstance(position, float)
# ~2 s of the 10 s story had played (5.5 - 3.5), scaled by the speed factor
assert 0.5 < position < 10.0 / speed
# the reply to "Stop!" was not interrupted and names no source
for turn in _spans(span_exporter, "agent_turn"):
if turn is not interrupted[0]:
assert trace_types.ATTR_INTERRUPTION_SOURCE not in (turn.attributes or {})
# the whole tree, not just the edges this test names (tests/trace_schema.py)
assert_trace_well_formed(span_exporter.get_finished_spans())
# -- agent handoff --
class _FirstAgent(Agent):
def __init__(self) -> None:
super().__init__(instructions="first")
class _SecondAgent(Agent):
def __init__(self) -> None:
super().__init__(instructions="second")
async def test_update_agent_span_groups_the_handoff(span_exporter: InMemorySpanExporter) -> None:
actions = FakeActions()
actions.add_user_speech(0.5, 1.5, "Hello", stt_delay=0.1)
actions.add_llm("Hi there", ttft=0.1, duration=0.2)
actions.add_tts(0.5, ttfb=0.1, duration=0.2)
session = create_session(actions, speed_factor=2.0)
audio_input = session.input.audio
assert isinstance(audio_input, FakeAudioInput)
stt = session.stt
assert isinstance(stt, FakeSTT)
# the synced audio output owns a transcript synchronizer that run_session normally closes;
# grab it now, the session detaches its outputs on close
synchronizer = (
session.output.audio._synchronizer
if isinstance(session.output.audio, _SyncedAudioOutput)
else None
)
await session.start(_FirstAgent())
audio_input.push(0.1)
await stt.fake_user_speeches_done
await asyncio.sleep(1.0)
session.update_agent(_SecondAgent())
await asyncio.sleep(1.0)
with contextlib.suppress(RuntimeError):
await session.drain()
await session.aclose()
if synchronizer is not None:
await synchronizer.aclose()
[root] = _spans(span_exporter, "agent_session")
[handoff] = _spans(span_exporter, "update_agent")
assert handoff.parent is not None and handoff.parent.span_id == root.context.span_id
attrs = handoff.attributes or {}
assert attrs[trace_types.ATTR_PREVIOUS_AGENT_LABEL] == "_first_agent"
assert attrs[trace_types.ATTR_AGENT_LABEL] == "_second_agent"
def _children(name: str) -> list[ReadableSpan]:
return [
s
for s in _spans(span_exporter, name)
if s.parent is not None and s.parent.span_id == handoff.context.span_id
]
# the old agent's drain (with on_exit inside it), then the new agent's start, under the handoff
[drain] = _children("drain_agent_activity")
exits = [
s
for s in _spans(span_exporter, "on_exit")
if s.parent is not None and s.parent.span_id == drain.context.span_id
]
assert len(exits) == 1
[start] = _children("start_agent_activity")
assert (start.attributes or {})[trace_types.ATTR_AGENT_LABEL] == "_second_agent"
# the initial start is not a handoff: it lives under session_start, not update_agent
[session_start] = _spans(span_exporter, "session_start")
initial = [
s
for s in _spans(span_exporter, "start_agent_activity")
if s.parent is not None and s.parent.span_id == session_start.context.span_id
]
assert len(initial) == 1
# the whole tree, not just the edges this test names (tests/trace_schema.py)
assert_trace_well_formed(span_exporter.get_finished_spans())
@pytest.mark.parametrize("phase", ["drain", "start"])
@pytest.mark.parametrize("redacted", [False, True])
async def test_update_agent_records_failure_once(
span_exporter: InMemorySpanExporter,
monkeypatch: pytest.MonkeyPatch,
phase: str,
redacted: bool,
) -> None:
monkeypatch.setattr(telemetry_utils, "redaction_enabled", lambda *_: redacted)
session = LiveSession()
failure = RuntimeError("private failure details")
try:
await session.start(_FirstAgent())
assert session._activity is not None
with monkeypatch.context() as patch:
if phase == "drain":
patch.setattr(session._activity, "drain", AsyncMock(side_effect=failure))
else:
patch.setattr(AgentActivity, "start", AsyncMock(side_effect=failure))
with pytest.raises(RuntimeError) as raised:
await session._update_activity_task(None, _SecondAgent())
assert raised.value is failure
finally:
await session.aclose()
[handoff] = _spans(span_exporter, "update_agent")
[event] = _events(handoff, "exception")
expected = telemetry_utils.REDACTED_EXCEPTION_MESSAGE if redacted else str(failure)
assert event.attributes[trace_types.ATTR_EXCEPTION_MESSAGE] == expected
assert handoff.status.description == expected
# -- fallback adapter events --
class _FailingLLMStream(LLMStream):
async def _run(self) -> None:
raise APIConnectionError("primary down")
class _FailingLLM(FakeLLM):
@property
def model(self) -> str:
return "broken-model"
@property
def provider(self) -> str:
return "broken"
def chat(
self,
*,
chat_ctx: ChatContext,
tools: list[Tool] | None = None,
conn_options: APIConnectOptions = DEFAULT_API_CONNECT_OPTIONS,
**kwargs: Any,
) -> LLMStream:
return _FailingLLMStream(
self, chat_ctx=chat_ctx, tools=tools or [], conn_options=conn_options
)
@pytest.mark.parametrize("through_node", [False, True], ids=["stream", "llm_node"])
async def test_llm_fallback_records_failed_and_serving_provider(
span_exporter: InMemorySpanExporter,
through_node: bool,
) -> None:
primary = _FailingLLM()
secondary = FakeLLM(
fake_responses=[FakeLLMResponse(input="hi", content="hello", ttft=0.01, duration=0.02)]
)
adapter = FallbackAdapter([primary, secondary], attempt_timeout=1.0, max_retry_per_llm=0)
chat_ctx = ChatContext()
chat_ctx.add_message(role="user", content="hi")
try:
if through_node:
def node(
chat_ctx: ChatContext, tools: list[Tool], model_settings: ModelSettings
) -> LLMStream:
return adapter.chat(chat_ctx=chat_ctx, tools=tools)
task, data = generation.perform_llm_inference(
node=node,
chat_ctx=chat_ctx,
tool_ctx=llm.ToolContext([]),
model_settings=ModelSettings(),
model=adapter.model,
provider=adapter.provider,
)
assert await task is True
response = data.generated_text
[caller] = _spans(span_exporter, "llm_node")
else:
with tracer.start_as_current_span("caller") as caller:
stream = adapter.chat(chat_ctx=chat_ctx)
chunks = [chunk async for chunk in stream]
await stream.aclose()
response = "".join(c.delta.content or "" for c in chunks if c.delta)
finally:
await asyncio.gather(*(s.recovering_task for s in adapter._status if s.recovering_task))
await adapter.aclose()
await primary.aclose()
await secondary.aclose()
assert response == "hello"
# each instance's request nests directly under the adapter's, with no attempt span between
[request] = _spans(span_exporter, "llm_fallback_adapter")
children = [
s
for s in _spans(span_exporter, "llm_request")
if s.parent is not None and s.parent.span_id == request.context.span_id
]
instance_requests = sorted(
(s for s in children if not (s.attributes or {}).get(trace_types.ATTR_FALLBACK_RECOVERY)),
key=lambda s: s.start_time or 0,
)
models = [
(s.attributes or {})[trace_types.ATTR_GEN_AI_REQUEST_MODEL] for s in instance_requests
]
assert models == [primary.model, secondary.model]
# the background check that the primary is back is marked, not mistaken for an attempt
[probe] = [s for s in children if s not in instance_requests]
assert (probe.attributes or {})[trace_types.ATTR_GEN_AI_REQUEST_MODEL] == primary.model
# the failed attempt names the model it asked on its own span
[failed_run] = [
s
for s in _spans(span_exporter, "llm_request_run")
if s.parent is not None and s.parent.span_id == instance_requests[0].context.span_id
]
assert failed_run.status.status_code is StatusCode.ERROR
assert (failed_run.attributes or {})[trace_types.ATTR_GEN_AI_REQUEST_MODEL] == primary.model
assert (failed_run.attributes or {})[
trace_types.ATTR_GEN_AI_PROVIDER_NAME
] == trace_types.gen_ai_provider_name(primary.provider)
# the adapter's span names the one that served: request = expected (the primary),
# response = who answered
request_attrs = request.attributes or {}
assert request_attrs[trace_types.ATTR_FALLBACK_LABEL] == secondary.label
assert request_attrs[trace_types.ATTR_FALLBACK_INDEX] == 1
assert trace_types.ATTR_GEN_AI_OPERATION_NAME not in request_attrs
assert request_attrs[trace_types.ATTR_GEN_AI_REQUEST_MODEL] == primary.model
assert request_attrs[trace_types.ATTR_GEN_AI_RESPONSE_MODEL] == secondary.model
# the caller's span gets the same response side, per request: a concurrent request on
# the same adapter may be served by a different instance, so this is not read back from
# the adapter's shared state once the node finishes
assert isinstance(caller, ReadableSpan)
caller_attrs = caller.attributes or {}
assert caller_attrs[trace_types.ATTR_GEN_AI_RESPONSE_MODEL] == secondary.model
assert caller_attrs[trace_types.ATTR_GEN_AI_PROVIDER_NAME] == trace_types.gen_ai_provider_name(
secondary.provider
)
if through_node:
assert caller_attrs[trace_types.ATTR_GEN_AI_REQUEST_MODEL] == primary.model
# and the adapter itself now reports who serves next
assert adapter.model == secondary.model and adapter.provider == secondary.provider
class _BrokenTTS(FakeTTS):
@property
def model(self) -> str:
return "broken-tts"
@property
def provider(self) -> str:
return "broken"
@pytest.mark.parametrize("streaming", [False, True], ids=["chunked", "stream"])
async def test_tts_fallback_nests_instance_requests_directly(
span_exporter: InMemorySpanExporter, streaming: bool
) -> None:
primary = _BrokenTTS(fake_exception=APIConnectionError("primary down"))
secondary = FakeTTS(fake_audio_duration=0.1)
adapter = TTSFallbackAdapter([primary, secondary], max_retry_per_tts=0)
try:
if streaming:
stream = adapter.stream(conn_options=APIConnectOptions(max_retry=0))
stream.push_text("hello world.")
stream.end_input()
else:
stream = adapter.synthesize("hello world.")
async with stream:
_ = [audio async for audio in stream]
finally:
for status in adapter._status:
for task in (status.recovering_synthesize_task, status.recovering_stream_task):
if task is not None:
await asyncio.gather(task, return_exceptions=True)
await adapter.aclose()
await primary.aclose()
await secondary.aclose()
[request] = _spans(span_exporter, "tts_fallback_adapter")
children = [
s
for s in span_exporter.get_finished_spans()
if s.parent is not None and s.parent.span_id == request.context.span_id
]
assert {s.name for s in children} == {"tts_request"}
probes = [s for s in children if (s.attributes or {}).get(trace_types.ATTR_FALLBACK_RECOVERY)]
attempts = sorted((s for s in children if s not in probes), key=lambda s: s.start_time or 0)
# the failed primary, then the secondary that served, each naming its model
models = [(s.attributes or {})[trace_types.ATTR_GEN_AI_REQUEST_MODEL] for s in attempts]
assert models == [primary.model, secondary.model]
[probe] = probes
assert (probe.attributes or {})[trace_types.ATTR_GEN_AI_REQUEST_MODEL] == primary.model
# the failed attempt names the model it asked on its own span
[failed_run] = [
s
for s in _spans(span_exporter, "tts_request_run")
if s.parent is not None and s.parent.span_id == attempts[0].context.span_id
]
assert failed_run.status.status_code is StatusCode.ERROR
failed_attrs = failed_run.attributes or {}
assert failed_attrs[trace_types.ATTR_GEN_AI_REQUEST_MODEL] == primary.model
assert failed_attrs[trace_types.ATTR_GEN_AI_PROVIDER_NAME] == "broken"
# the adapter's span: request = expected (the primary), response = who answered
request_attrs = request.attributes or {}
assert request_attrs[trace_types.ATTR_FALLBACK_LABEL] == secondary.label
assert request_attrs[trace_types.ATTR_FALLBACK_INDEX] == 1
assert request_attrs[trace_types.ATTR_GEN_AI_REQUEST_MODEL] == primary.model
assert request_attrs[trace_types.ATTR_GEN_AI_RESPONSE_MODEL] == secondary.model
async def test_tts_stream_adapter_nests_sentence_requests_directly(
span_exporter: InMemorySpanExporter,
) -> None:
async with StreamAdapter(tts=FakeTTS(fake_audio_duration=0.1)) as adapter:
stream = adapter.stream(conn_options=APIConnectOptions(max_retry=0))
stream.push_text("hello world.")
stream.end_input()
async with stream:
_ = [audio async for audio in stream]
[request] = _spans(span_exporter, "tts_stream_adapter")
children = [
s
for s in span_exporter.get_finished_spans()
if s.parent is not None and s.parent.span_id == request.context.span_id
]
assert children
assert {s.name for s in children} == {"tts_request"}
@pytest.mark.parametrize("streaming", [False, True])
@pytest.mark.parametrize("redacted", [False, True])
async def test_tts_retry_records_each_exception_once(
span_exporter: InMemorySpanExporter,
monkeypatch: pytest.MonkeyPatch,
streaming: bool,
redacted: bool,
) -> None:
monkeypatch.setattr(telemetry_utils, "redaction_enabled", lambda *_: redacted)
failure = APIConnectionError("private failure details")
async with FakeTTS(fake_exception=failure) as model:
conn_options = APIConnectOptions(max_retry=1, retry_interval=0)
if streaming:
stream = model.stream(conn_options=conn_options)
stream.push_text("hello")
stream.end_input()
else:
stream = model.synthesize("hello", conn_options=conn_options)
async with stream:
with pytest.raises(APIConnectionError):
async for _ in stream:
pass
attempts = _spans(span_exporter, "tts_request_run")
assert len(attempts) == 2
for attempt in attempts:
[event] = _events(attempt, "exception")
expected = telemetry_utils.REDACTED_EXCEPTION_MESSAGE if redacted else str(failure)
assert event.attributes[trace_types.ATTR_EXCEPTION_MESSAGE] == expected
def test_interrupt_source_first_wins() -> None:
from livekit.agents.voice.speech_handle import SpeechHandle
handle = SpeechHandle.create()
handle.interrupt(source="audio_activity")
handle.interrupt(source="user_turn") # already interrupted: the first cause stands
assert handle._interrupt_source == "audio_activity"
assert SpeechHandle.create().interrupt()._interrupt_source == "programmatic"
# unrelated: keep the module import used
assert llm is not None and time is not None