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): keep session alive after barge-in abort

Ruben Lütke9efc0afb586f
diff --git a/jarvis/realtime/desktop.py b/jarvis/realtime/desktop.py
index e9275e254c..24f110f2ce 100644
--- a/jarvis/realtime/desktop.py
+++ b/jarvis/realtime/desktop.py
@@ -215,6 +215,13 @@ class DesktopRealtimePlayback:
if current is not None and current.cancelling():
raise
# The playback worker was canceled by a concurrent barge-in.
+ except Exception:
+ if self._task is task:
+ raise
+ # A concurrent cancel detached this task and aborted its PortAudio
+ # stream. The blocked write can then unwind with "Stream is
+ # stopped"; that is the expected result of barge-in, not a failed
+ # realtime session.
finally:
# Keep the active task discoverable while it drains. A user can
# barge in after the provider has sent turn_complete but before the
@@ -226,6 +233,7 @@ class DesktopRealtimePlayback:
async def cancel(self) -> None:
queue, task = self._detach()
+ task_was_done = task is not None and task.done()
self._player.stop()
if queue is None or task is None:
return
@@ -246,6 +254,12 @@ class DesktopRealtimePlayback:
await task
except asyncio.CancelledError:
pass
+ except Exception:
+ if task_was_done:
+ raise
+ # stop() deliberately aborts the live PortAudio stream. A write
+ # already running in its worker thread can report that abort as a
+ # playback exception while the task unwinds.
async def close(self) -> None:
await self.cancel()
diff --git a/tests/unit/realtime/test_desktop.py b/tests/unit/realtime/test_desktop.py
index c8dbff6dcc..611bbb583c 100644
--- a/tests/unit/realtime/test_desktop.py
+++ b/tests/unit/realtime/test_desktop.py
@@ -138,6 +138,46 @@ async def test_cancel_can_interrupt_a_turn_already_draining() -> None:
assert player.chunks == []
+async def test_cancel_during_drain_treats_stopped_stream_as_expected() -> None:
+ stop_event = asyncio.Event()
+
+ class AbortedStreamPlayer(FakePlayer):
+ def stop(self) -> None:
+ super().stop()
+ stop_event.set()
+
+ async def play_chunks(self, chunks) -> None:
+ async for _chunk in chunks:
+ await stop_event.wait()
+ raise RuntimeError("Stream is stopped [PaErrorCode -9983]")
+
+ player = AbortedStreamPlayer()
+ playback = DesktopRealtimePlayback(player)
+ await playback.send_binary(b"\x01\x00" * 8)
+ drain = asyncio.create_task(playback.finish_turn())
+ await asyncio.sleep(0)
+
+ await playback.cancel()
+ await drain
+
+ assert player.stopped == 1
+
+
+async def test_finish_turn_still_surfaces_an_unrelated_playback_failure() -> None:
+ class FailingPlayer(FakePlayer):
+ async def play_chunks(self, chunks) -> None:
+ async for _chunk in chunks:
+ raise RuntimeError("output device disappeared")
+
+ playback = DesktopRealtimePlayback(FailingPlayer())
+ await playback.send_binary(b"\x01\x00" * 8)
+
+ with pytest.raises(RuntimeError, match="output device disappeared"):
+ await playback.finish_turn()
+
+
@pytest.mark.asyncio
async def test_handshake_sample_rate_is_used_for_the_next_turn():
player = FakePlayer()