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): give half-duplex an unmute path the session owns itself

rubenluetke10-beep748cae77fdb3
diff --git a/jarvis/realtime/session.py b/jarvis/realtime/session.py
index 08ae6616da..f02524eb39 100644
--- a/jarvis/realtime/session.py
+++ b/jarvis/realtime/session.py
@@ -1066,6 +1066,11 @@ class RealtimeVoiceSession:
# the condition is visible instead of invisible (AP-30).
self._half_duplex_muted_since: float | None = None
self._half_duplex_mute_reported = 0.0
+ # When provider audio last actually reached the surface. A reply that
+ # is still playing must never be cut short by the mute release below,
+ # and "is it still playing" is a question only this timestamp answers:
+ # ``_output_active`` says a turn was opened, not that it is alive.
+ self._last_output_audio_at = 0.0
# Per-turn stall watchdog (see _TURN_STALL_TIMEOUT_S). Armed by
# _ensure_turn_started, cancelled by _reset_turn_tracking, so its
# lifetime is exactly one turn and it can never fire between turns.
@@ -1952,11 +1957,20 @@ class RealtimeVoiceSession:
)
def _note_half_duplex_mute(self) -> None:
- """Report a microphone that half-duplex has been holding shut too long.
-
- A reply lasts seconds; a mute that outlives one is a stuck turn, and
- the user experiences it as "it just stopped listening to me" with
- nothing anywhere to say why.
+ """Release, or failing that report, a microphone held shut too long.
+
+ A reply lasts seconds; a mute that outlives one with no audio still
+ flowing is a turn that ended without ever saying so, and the user
+ experiences it as "it just stopped listening to me". Every clear of
+ ``_output_active`` needs an event that arrives on the provider stream,
+ and a turn ended by a recoverable error or by a missing terminal item
+ produces none — so the mute had no exit at all and the six-second
+ warning was the only trace it ever left.
+
+ The release is deliberately gated on SILENCE rather than on elapsed
+ mute time alone: a long reply that is still playing keeps its
+ microphone shut, exactly as half-duplex intends. Only a turn that is
+ both overdue AND no longer producing audio is treated as over.
"""
now = time.monotonic()
if self._half_duplex_muted_since is None:
@@ -1965,6 +1979,21 @@ class RealtimeVoiceSession:
muted_s = now - self._half_duplex_muted_since
if muted_s < _HALF_DUPLEX_MUTE_ALERT_S:
return
+ silent_since = self._last_output_audio_at or self._half_duplex_muted_since
+ silent_s = now - silent_since
+ if silent_s >= _HALF_DUPLEX_MUTE_ALERT_S:
+ log.warning(
+ "realtime[%s] releasing a half-duplex mute held %.1fs with no "
+ "provider audio for %.1fs - the turn ended without a boundary, "
+ "so the microphone is reopened rather than left deaf",
+ self.session_id,
+ muted_s,
+ silent_s,
+ )
+ self._reset_output_state(reason="half-duplex mute outlived its turn")
+ self._half_duplex_muted_since = None
+ self._half_duplex_mute_reported = 0.0
+ return
if now - self._half_duplex_mute_reported < _HALF_DUPLEX_MUTE_REPEAT_S:
return
self._half_duplex_mute_reported = now
@@ -1996,7 +2025,12 @@ class RealtimeVoiceSession:
self._rebuild_drop_reported = 0.0
if self._half_duplex and self._output_active:
self._note_half_duplex_mute()
- return
+ if self._output_active:
+ return
+ # The mute was just released. Let THIS frame through rather than
+ # dropping it: it is the first sound of whatever the user is
+ # saying, and swallowing it would clip the very utterance the
+ # release exists to rescue.
self._half_duplex_muted_since = None
self._half_duplex_mute_reported = 0.0
try:
@@ -6321,6 +6355,7 @@ class RealtimeVoiceSession:
except Exception: # noqa: BLE001, S110 — best-effort telemetry
pass
self._note_audio_flow(pcm, chunk)
+ self._last_output_audio_at = time.monotonic()
self._output_samples_sent += len(pcm) // 2
# Real provider audio: advance the echo guard's playback horizon by
# this chunk's audible duration (BUG-089).
diff --git a/tests/integration/realtime/test_codex_multi_turn.py b/tests/integration/realtime/test_codex_multi_turn.py
index d6886d65b4..6da3edd3bf 100644
--- a/tests/integration/realtime/test_codex_multi_turn.py
+++ b/tests/integration/realtime/test_codex_multi_turn.py
@@ -30,6 +30,7 @@ import pytest
from jarvis.core.bus import EventBus
from jarvis.core.protocols import AudioChunk
from jarvis.realtime.protocol import RealtimeEvent
+from jarvis.realtime import session as session_mod
from jarvis.realtime.session import RealtimeVoiceSession
# Every await in this module is bounded by this. A regression must report as a
@@ -478,28 +479,24 @@ async def test_a_reply_split_by_a_long_internal_pause_stays_one_answer() -> None
await _end(session)
- strict=True,
- reason=(
- "MT-1: an audible turn that never receives a terminal boundary leaves "
- "_output_active latched True, and under half duplex handle_audio_frame "
- "then drops every microphone frame for the rest of the call "
- "(session.py:1911-1913). Nothing clears it on a timer: "
- "_note_half_duplex_mute (session.py:1868-1891) only LOGS the condition "
- "every 10 s, and its own docstring names the symptom — 'it just stopped "
- "listening to me'. Today the codex adapter's 1.2 s quiescence backstop "
- "masks this, so the session depends on provider goodwill for a "
- "guarantee it owns itself."
- ),
-)
@pytest.mark.asyncio
-async def test_a_turn_without_any_terminal_item_does_not_wedge_the_next_one() -> None:
+async def test_a_turn_without_any_terminal_item_does_not_wedge_the_next_one(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
"""ChatGPT-Live's terminal response item is not guaranteed to arrive.
- When it does not, the turn never closes — and under half duplex the
+ When it does not, the turn never closes - and under half duplex the
microphone stays muted, so the user is talking into a session that cannot
- hear them. The next genuine utterance must still land.
+ hear them. The next genuine utterance must still land, which means the
+ session has to own an unmute path instead of trusting the provider to send
+ a well-formed boundary.
+
+ The release is deliberately gated on a mute that is BOTH overdue and no
+ longer producing audio, so this shortens that window rather than waiting
+ out the production value - and a reply still streaming audio keeps its
+ microphone shut, which the sibling tests cover.
"""
+ monkeypatch.setattr(session_mod, "_HALF_DUPLEX_MUTE_ALERT_S", 0.05)
session, _provider, wire, surface = await _open_call()
try:
wire.push(
@@ -511,6 +508,11 @@ async def test_a_turn_without_any_terminal_item_does_not_wedge_the_next_one() ->
)
await surface.wait_binary(at_least=1)
+ # One frame arms the mute clock; the window then passes with no further
+ # provider audio, which is what marks the turn as over.
+ await session.handle_audio_frame(_pcm())
+ await asyncio.sleep(0.08)
+
# The next utterance must reach the provider and open its own turn.
before = len(wire.sent_audio)
await asyncio.wait_for(session.handle_audio_frame(_pcm()), TIMEOUT_S)
@@ -526,23 +528,10 @@ async def test_a_turn_without_any_terminal_item_does_not_wedge_the_next_one() ->
await _end(session)
- strict=True,
- reason=(
- "MT-2: same latched _output_active as MT-1, but on a far more reachable "
- "trigger. A RECOVERABLE provider error arriving mid-reply is routine on "
- "this transport — the adapter emits one for a failed notification "
- "stream (codex_subscription.py:828-839) and for a dead media track "
- "(:841-849), neither of which is followed by a turn boundary. The "
- "session logs the error, keeps the call alive, and never unmutes the "
- "microphone, so the user's next words reach nothing. Half duplex has NO "
- "unmute path the session owns itself: every clear of _output_active "
- "(session.py:3035, 3746, 3796, 4611, 6325) needs the provider to send a "
- "well-formed boundary first."
- ),
-)
@pytest.mark.asyncio
-async def test_a_provider_error_closing_a_turn_still_releases_the_microphone() -> None:
+async def test_a_provider_error_closing_a_turn_still_releases_the_microphone(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
"""Scopes MT-1: which non-``turn_complete`` endings DO release the mute.
The adapter ends a turn on an error without a terminal boundary in several
@@ -550,6 +539,7 @@ async def test_a_provider_error_closing_a_turn_still_releases_the_microphone() -
microphone there, MT-1 is narrow; if it does not, half duplex has no
unmute path at all except a well-behaved provider.
"""
+ monkeypatch.setattr(session_mod, "_HALF_DUPLEX_MUTE_ALERT_S", 0.05)
session, _provider, wire, surface = await _open_call()
try:
since = surface.mark()
@@ -570,10 +560,15 @@ async def test_a_provider_error_closing_a_turn_still_releases_the_microphone() -
since=since,
)
+ # One frame arms the mute clock; the window then passes with no further
+ # provider audio, which is what marks the turn as over.
+ await session.handle_audio_frame(_pcm())
+ await asyncio.sleep(0.08)
+
before = len(wire.sent_audio)
await asyncio.wait_for(session.handle_audio_frame(_pcm()), TIMEOUT_S)
assert len(wire.sent_audio) > before, (
- "an error boundary left the microphone muted too — half duplex has "
+ "an error boundary left the microphone muted too - half duplex has "
"no unmute path that the session itself owns"
)
finally: