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): make _barge_in the one owner of the reply-to-cut decision

rubenluetke10-beep8491b369fc43
diff --git a/jarvis/realtime/session.py b/jarvis/realtime/session.py
index cfa3d0daf2..7b753e7d74 100644
--- a/jarvis/realtime/session.py
+++ b/jarvis/realtime/session.py
@@ -4636,20 +4636,14 @@ class RealtimeVoiceSession:
return action_phrase(phrase_key, self._language), succeeded
async def _begin_user_speech_turn(self) -> None:
- """Close an interrupted reply before the next transcript opens a turn."""
- # Withhold ONLY when there is a reply to interrupt. Arming this on
- # every speech edge silently swallowed the HEAD of any answer the
- # server began before the local transcript landed — on a transport
- # whose local recognizer needs a network round trip that beheaded
- # the first reply of a call into a mid-sentence entry and re-added
- # seconds of perceived latency (live 2026-08-05 20:12: 105 audio
- # events dropped between the speech edge and the final transcript;
- # the user heard the answer only from "…ich bin ganz entspannt").
- # The rare late continuation of an already-closed turn playing into
- # the user's new utterance is the accepted trade: it becomes an
- # audible barge-in (output active) and is cut on the next edge.
- if self._output_active or self._response_requested_for_turn:
- self._drop_provider_output_until_new_response = True
+ """Close an interrupted reply before the next transcript opens a turn.
+
+ Deliberately decides NOTHING about withholding or draining: every
+ caller invokes ``_barge_in`` right after this, and that method is the
+ one owner of the "is there a reply to cut" decision. A second copy of
+ that decision here is exactly how a conditional fix became a no-op
+ (2dff5890 → independent review W1).
+ """
if self._turn_id and self._turn_has_activity():
self._mark_latency_named(
"REALTIME_CANCEL",
@@ -6591,14 +6585,26 @@ class RealtimeVoiceSession:
self._embedded_silence_ms = 0.0
async def _barge_in(self, *, interrupt_provider: bool = True) -> None:
+ # Evaluated BEFORE the reset below: there is a reply to cut only when
+ # one is audible or already requested for this turn. Without one, the
+ # incoming audio belongs to the answer of the utterance that triggered
+ # this very edge — arming the withhold and draining the gate here
+ # swallowed that answer's un-transcribed head, because a slow local
+ # recognizer lets the server answer first (live 2026-08-05 20:12:
+ # 105 withheld audio events, playback entering mid-sentence). This
+ # method is the ONE owner of that decision; _begin_user_speech_turn
+ # deliberately decides nothing (the two-owner split is how the no-op
+ # fix of 2dff5890 happened).
+ reply_to_cut = bool(
+ self._output_active or self._response_requested_for_turn
+ )
should_interrupt = bool(
- interrupt_provider
- and self._session is not None
- and (self._output_active or self._response_requested_for_turn)
+ interrupt_provider and self._session is not None and reply_to_cut
)
- self._drop_provider_output_until_new_response = True
+ if reply_to_cut:
+ self._drop_provider_output_until_new_response = True
+ self._gate.drain()
self._response_requested_for_turn = False
- self._gate.drain()
output_rate = int(getattr(self._provider, "output_sample_rate", 24_000) or 24_000)
audio_end_ms = (
int(self._output_samples_sent * 1000 / output_rate)
diff --git a/tests/unit/realtime/test_session.py b/tests/unit/realtime/test_session.py
index 73aa9728d2..3fd41dd91a 100644
--- a/tests/unit/realtime/test_session.py
+++ b/tests/unit/realtime/test_session.py
@@ -6930,15 +6930,26 @@ async def test_silent_provider_audio_does_not_advance_liveness_or_echo_horizon()
await sess.end(reason="test")
+async def _drive_speech_edge(sess) -> None:
+ """The REAL pump sequence for a speech edge: begin + barge-in.
+
+ Testing ``_begin_user_speech_turn`` alone is exactly how the first fix
+ became a no-op — ``_barge_in`` runs one line later in production and
+ used to re-arm the withhold unconditionally (independent review W1/W3).
+ """
+ await sess._begin_user_speech_turn()
+ await sess._barge_in(interrupt_provider=True)
+
+
@pytest.mark.asyncio
async def test_speech_edge_without_a_playing_reply_does_not_withhold_audio():
"""A fresh utterance must not swallow the head of the answer it earns.
- The speech edge armed the barge-in withhold unconditionally; on a
- transport whose local recognizer needs a network round trip, the server's
- answer regularly BEGINS before the final transcript lands, so its first
- seconds were dropped and playback entered mid-sentence (live 2026-08-05
- 20:12: 105 withheld audio events, the reply audible only from its middle).
+ On a transport whose local recognizer needs a network round trip, the
+ server's answer regularly BEGINS before the final transcript lands; the
+ unconditional edge withhold dropped its first seconds and playback
+ entered mid-sentence (live 2026-08-05 20:12: 105 withheld audio events,
+ the reply audible only from its middle).
"""
sess = RealtimeVoiceSession(
session_id="s-speech-edge",
@@ -6949,18 +6960,48 @@ async def test_speech_edge_without_a_playing_reply_does_not_withhold_audio():
bus=None,
)
- await sess._begin_user_speech_turn()
+ await _drive_speech_edge(sess)
assert sess._drop_provider_output_until_new_response is False
# With a reply audibly playing the same edge IS a barge-in: withhold.
sess._output_active = True
- await sess._begin_user_speech_turn()
+ await _drive_speech_edge(sess)
assert sess._drop_provider_output_until_new_response is True
# And a requested-but-not-yet-audible response counts the same way.
sess._output_active = False
sess._drop_provider_output_until_new_response = False
sess._response_requested_for_turn = True
- await sess._begin_user_speech_turn()
+ await _drive_speech_edge(sess)
assert sess._drop_provider_output_until_new_response is True
await sess.end(reason="test")
+
+
+async def test_speech_edge_keeps_the_untranscribed_answer_head_buffered():
+ """The gate buffer must survive an edge with no reply to cut (review W2).
+
+ The head of the incoming answer sits in the scrub gate awaiting its
+ transcript; draining it on every speech edge deleted exactly those
+ seconds even after the withhold itself was made conditional.
+ """
+ sess = RealtimeVoiceSession(
+ session_id="s-speech-edge-gate",
+ send_binary=lambda _pcm: asyncio.sleep(0),
+ send_json=lambda _message: asyncio.sleep(0),
+ provider=FakeProvider([]),
+ config=_cfg(),
+ bus=None,
+ )
+ head = AudioChunk(pcm=b"\x00\x01" * 24_000, sample_rate=24_000, timestamp_ns=0)
+ assert await sess._gate.push_audio(head) == []
+ assert sess._gate.pending_audio_ms > 0
+
+ await _drive_speech_edge(sess)
+ assert sess._gate.pending_audio_ms > 0
+
+ # A real barge-in still discards the interrupted reply's buffer.
+ sess._output_active = True
+ await _drive_speech_edge(sess)
+ assert sess._gate.pending_audio_ms == 0
+ await sess.end(reason="test")