Skip to content

Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.

fix(realtime): report the two ways the provider voice silently loses audio

Ruben Lütke5618b7f23d50
diff --git a/jarvis/realtime/webrtc_transport.py b/jarvis/realtime/webrtc_transport.py
index 7498e20f51..3cfa5a3028 100644
--- a/jarvis/realtime/webrtc_transport.py
+++ b/jarvis/realtime/webrtc_transport.py
@@ -36,6 +36,19 @@ _JARVIS_RATE = 24_000
_SEND_QUEUE_MAX = 200
_RECV_QUEUE_MAX = 200
_ICE_TIMEOUT_S = 15.0
+# One line per 50 dropped frames == per second of lost audio at 20 ms frames.
+_DROP_LOG_EVERY = 50
+
+
+def _is_clean_track_end(exc: BaseException) -> bool:
+ """Whether a remote-track exception is the ordinary end of a call.
+
+ aiortc signals a finished track with ``MediaStreamError``; everything else
+ means the assistant's voice stopped for a reason worth reporting. Matched
+ by NAME so the module keeps importing aiortc lazily (AP-26) and so an
+ aiortc refactor cannot turn this into an import error at call time.
+ """
+ return type(exc).__name__ == "MediaStreamError"
class WebRtcTransportUnavailable(RuntimeError):
@@ -229,6 +242,7 @@ class RealtimeWebRtcAudioEndpoint:
resampler = AudioResampler(
format="s16", layout="mono", rate=_JARVIS_RATE
)
+ dropped = 0
try:
while True:
frame = await track.recv()
@@ -241,13 +255,43 @@ class RealtimeWebRtcAudioEndpoint:
except asyncio.QueueFull:
# Backpressure: drop the OLDEST frame so live speech
# stays current instead of drifting further behind.
+ # Say so — this is a hole punched in the assistant's
+ # own voice, and it used to leave no trace anywhere
+ # (AP-30). Report the first one and then every full
+ # second of loss, so a wedged consumer is visible
+ # without the log becoming the next bottleneck.
with_suppress_get(self._recv_queue)
self._recv_queue.put_nowait(pcm)
+ dropped += 1
+ if dropped == 1 or dropped % _DROP_LOG_EVERY == 0:
+ log.warning(
+ "Realtime WebRTC receive queue is full; dropped "
+ "%d provider audio frame(s) — the reply loses "
+ "that audio",
+ dropped,
+ )
except asyncio.CancelledError:
raise
except Exception as exc: # noqa: BLE001 - the track ends with the call
- log.debug("Realtime WebRTC remote track ended (%s)", type(exc).__name__)
+ if _is_clean_track_end(exc):
+ log.debug(
+ "Realtime WebRTC remote track ended (%s)", type(exc).__name__
+ )
+ else:
+ # Anything else means the voice just went mute mid-call. At
+ # DEBUG that was invisible to everyone including the user.
+ log.warning(
+ "Realtime WebRTC remote track failed; the provider voice "
+ "stops here",
+ exc_info=True,
+ )
finally:
+ if dropped:
+ log.warning(
+ "Realtime WebRTC remote track dropped %d audio frame(s) in "
+ "total on a full receive queue",
+ dropped,
+ )
await self._finish_stream()
async def _finish_stream(self) -> None:
diff --git a/tests/unit/realtime/test_webrtc_transport.py b/tests/unit/realtime/test_webrtc_transport.py
new file mode 100644
index 0000000000..67013ab231
--- /dev/null
+++ b/tests/unit/realtime/test_webrtc_transport.py
@@ -0,0 +1,108 @@
+"""Reporting guarantees of the in-process WebRTC audio endpoint.
+
+Both defects pinned here are silent-failure defects (AP-30): the assistant's
+voice stops, or loses a piece, and nothing above DEBUG says so. On a live
+media track that audio is gone for good — the player never fills the hole —
+so the log is the only place the loss can still be seen.
+"""
+
+from __future__ import annotations
+
+import asyncio
+import logging
+
+import pytest
+
+import jarvis.realtime.webrtc_transport as webrtc_transport
+
+
+class MediaStreamError(Exception):
+ """Same NAME aiortc uses for the ordinary end of a track."""
+
+
+class _Track:
+ def __init__(self, frames=(), *, failure: BaseException | None = None) -> None:
+ self._frames = list(frames)
+ self._failure = failure or MediaStreamError("track ended")
+
+ async def recv(self):
+ if self._frames:
+ return self._frames.pop(0)
+ raise self._failure
+
+
+class _Stub:
+ """Only what ``_drain_remote`` touches — no peer connection required."""
+
+ def __init__(self, maxsize: int = 200) -> None:
+ self._recv_queue: asyncio.Queue = asyncio.Queue(maxsize=maxsize)
+ self.finished = 0
+
+ async def _finish_stream(self) -> None:
+ self.finished += 1
+
+
+def _frame():
+ import av # noqa: PLC0415
+ import numpy as np # noqa: PLC0415
+
+ frame = av.AudioFrame.from_ndarray(
+ np.zeros((1, 960), dtype=np.int16), format="s16", layout="mono"
+ )
+ frame.sample_rate = 48_000
+ frame.pts = 0
+ return frame
+
+
+async def _drain(stub: _Stub, track: _Track) -> None:
+ await webrtc_transport.RealtimeWebRtcAudioEndpoint._drain_remote(stub, track)
+
+
+async def test_an_ordinary_track_end_stays_quiet(caplog) -> None:
+ caplog.set_level(logging.DEBUG)
+ stub = _Stub()
+
+ await _drain(stub, _Track())
+
+ assert stub.finished == 1
+ assert not [
+ record for record in caplog.records if record.levelno >= logging.WARNING
+ ]
+
+
+async def test_a_failing_track_reports_that_the_voice_stopped(caplog) -> None:
+ """This used to be a DEBUG line: the voice went mute mid-call and the log
+ said nothing a user or maintainer would ever see."""
+ caplog.set_level(logging.DEBUG)
+ stub = _Stub()
+
+ await _drain(stub, _Track(failure=RuntimeError("decoder exploded")))
+
+ assert stub.finished == 1
+ warnings = [
+ record.getMessage()
+ for record in caplog.records
+ if record.levelno >= logging.WARNING
+ ]
+ assert any("provider voice" in message for message in warnings)
+
+
+async def test_dropped_audio_frames_are_reported(caplog) -> None:
+ """A full receive queue drops the oldest frame to stay current. That is a
+ hole in the reply, and it used to leave no trace at all."""
+ caplog.set_level(logging.INFO)
+ stub = _Stub(maxsize=1)
+ stub._recv_queue.put_nowait(b"\x00\x00") # already full
+
+ await _drain(stub, _Track(frames=[_frame(), _frame()]))
+
+ dropped = [
+ record.getMessage()
+ for record in caplog.records
+ if "dropped" in record.getMessage()
+ ]
+ assert dropped
+ assert any("in total" in message for message in dropped)