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): drop provider-transcribed self-echo turns before they trigger a response (BUG-089)

Ruben Lütked777f4643d42
diff --git a/jarvis/realtime/session.py b/jarvis/realtime/session.py
index 5226940886..2c0879ea2b 100644
--- a/jarvis/realtime/session.py
+++ b/jarvis/realtime/session.py
@@ -38,6 +38,7 @@ from jarvis.sessions.constants import (
SPOKEN_KIND_REPLY,
SPOKEN_KIND_WITHHELD,
)
+from jarvis.speech.echo_guard import SelfEchoGuard
from jarvis.speech.hangup import HANGUP_RE
log = logging.getLogger(__name__)
@@ -62,6 +63,10 @@ _END_CALL_GRACE_S = 10.0
# Gemini emits is_final per transcript CHUNK, so hang-up matching runs on a
# per-turn accumulator; the tail-trim bounds it without losing recent words.
_HANGUP_BUFFER_MAX_CHARS = 300
+# Ceiling on how far ahead of wall-clock the echo guard's activity stamp may
+# be dated (estimated playback drain, BUG-089). Bounds a runaway estimate
+# from a mis-reported sample rate; real replies stay far below it.
+_ECHO_HORIZON_MAX_S = 120.0
# Declared to the realtime model alongside the bridge tools, but handled by
# the session itself: ending the call is surface lifecycle (like the hotkey),
# not a risk-tiered Jarvis tool, and must work even without a tool bridge.
@@ -788,6 +793,13 @@ class RealtimeVoiceSession:
self._user_transcript_parts: list[str] = []
self._input_turn_observed = False
self._output_transcript: list[str] = []
+ # BUG-089: text-level self-echo backstop. The realtime path's acoustic
+ # gates leak on open speakers next to a built-in mic (macOS), so every
+ # text this session makes audible is registered here and each final
+ # provider-transcribed input is judged against it BEFORE it can become
+ # a turn — otherwise the brain answers its own speaker echo forever.
+ self._echo_guard = SelfEchoGuard()
+ self._echo_playback_horizon = 0.0
self._provider_output_probe = ""
self._executed_tool_names: set[str] = set()
self._direct_tool_results: list[tuple[str, dict[str, Any]]] = []
@@ -1243,6 +1255,42 @@ class RealtimeVoiceSession:
transcript = str(event.text or "").strip()
transcription_failed = bool(event.error)
input_observed = bool(transcript or transcription_failed)
+ if event.is_final and transcript:
+ # BUG-089: judge the accumulated candidate BEFORE any
+ # turn side effect (deferred barge confirm, turn
+ # start, tool bridge, delegate, request_response). A
+ # final transcript that is fuzzily nothing but our
+ # own recent speech is the speaker echo that slipped
+ # the acoustic gates — dropping it here means no
+ # response is ever generated for it.
+ echo_probe = " ".join(
+ (*self._user_transcript_parts, transcript)
+ ).strip()
+ if self._echo_guard.is_echo(echo_probe):
+ log.info(
+ "realtime[%s] dropped provider-transcribed "
+ "self-echo before it became a turn: %r",
+ self.session_id,
+ echo_probe[:80],
+ )
+ if bool(
+ getattr(
+ self._session,
+ "creates_responses_automatically",
+ False,
+ )
+ ):
+ # The provider may already be answering its
+ # own echo — silence that generation until a
+ # genuine user turn opens.
+ self._drop_provider_output_until_user_turn = (
+ True
+ )
+ try:
+ await self._session.interrupt()
+ except Exception: # noqa: BLE001, S110 — best effort
+ pass
+ continue
if (
event.is_final
and input_observed
@@ -1514,6 +1562,14 @@ class RealtimeVoiceSession:
self._gate.drain()
continue
self._output_transcript.append(display)
+ # Cumulative snapshot under ONE slot: what the provider
+ # is audibly saying this turn, as an echo-guard reference
+ # (BUG-089). Slot replacement keeps the growing snapshot
+ # from evicting the other references.
+ self._register_spoken_reference(
+ "".join(self._output_transcript),
+ slot=f"turn:{self._turn_id or 'session'}",
+ )
await self._send_json(
{
"type": "transcript",
@@ -2100,6 +2156,54 @@ class RealtimeVoiceSession:
return ""
return scrub_for_voice(raw, language=self._language).cleaned.strip()
+ def _advance_echo_horizon(self, duration_s: float) -> None:
+ """Date the echo guard's activity forward to the estimated drain.
+
+ The surface never reports physical playback drain back to the
+ session, and providers send audio faster than realtime — a plain
+ "recently active" wall-clock stamp would lapse mid-playback on long
+ replies. Estimating the drain from emitted audio keeps the guard
+ armed exactly as long as the user can still hear us (BUG-089).
+ """
+ now = time.monotonic()
+ horizon = max(self._echo_playback_horizon, now) + max(0.0, duration_s)
+ horizon = min(horizon, now + _ECHO_HORIZON_MAX_S)
+ self._echo_playback_horizon = horizon
+ self._echo_guard.touch(time.time_ns() + int((horizon - now) * 1e9))
+
+ def _reset_echo_horizon(self) -> None:
+ """Playback stopped early (barge-in/cancel) — pull the horizon back.
+
+ ``force=True`` re-stamps activity to "now": the guard stays armed for
+ its short trailing window (audible reverb of what DID play) but no
+ longer claims the cancelled remainder as active playback.
+ """
+ self._echo_playback_horizon = time.monotonic()
+ self._echo_guard.touch(force=True)
+
+ def _register_spoken_reference(
+ self,
+ text: str,
+ *,
+ slot: str | None = None,
+ estimate_playback: bool = False,
+ ) -> None:
+ """Feed one about-to-be-audible text to the self-echo guard.
+
+ ``estimate_playback`` is for surface-spoken phrases whose PCM never
+ flows through this session: their horizon is estimated from word
+ count (~2.5 words/s plus a one-second lead-out). Provider-voiced
+ text must NOT estimate — its real audio advances the horizon in
+ ``_emit_audio`` and estimating twice would over-arm the guard.
+ """
+ cleaned = str(text or "").strip()
+ if not cleaned:
+ return
+ self._echo_guard.register(cleaned, slot=slot)
+ if estimate_playback:
+ words = len(cleaned.split())
+ self._advance_echo_horizon(words * 0.4 + 1.0)
+
def _surface_speech_message(self, text: str) -> dict[str, Any]:
"""Build one ``error_spoken`` payload for the surface's classic TTS.
@@ -2110,6 +2214,10 @@ class RealtimeVoiceSession:
``list_voices()``, so a foreign voice name never reaches a provider
that would reject it.
"""
+ # Every surface-spoken phrase is an echo-guard reference: the canned
+ # apologies are exactly what the Mac loop transcribed back as "user"
+ # input (BUG-089).
+ self._register_spoken_reference(text, estimate_playback=True)
message: dict[str, Any] = {
"type": "error_spoken",
"text": text,
@@ -2166,6 +2274,7 @@ class RealtimeVoiceSession:
pass
self._output_active = False
self._output_samples_sent = 0
+ self._reset_echo_horizon()
spoken_fallback = fallback_text or self._gate.fallback_phrase()
# The turn's answer is what the user actually hears. Keeping the
# aborted partial provider transcript here poisoned the NEXT turn:
@@ -3175,6 +3284,12 @@ class RealtimeVoiceSession:
turn_state.provider_ready.set()
if turn_state.result_complete and turn_state.result_payload:
turn_state.delivery_started = True
+ # Belt-and-braces echo reference: we know the exact reply we
+ # hand the provider to voice, even if its output
+ # transcription lags or garbles (BUG-089).
+ self._register_spoken_reference(
+ str(turn_state.last_reply or "")
+ )
self._drop_provider_output_until_new_response = False
await self._session.send_tool_result(
call_id,
@@ -3578,6 +3693,10 @@ class RealtimeVoiceSession:
self._queue_late_delegate_result(turn_state)
return
turn_state.delivery_started = True
+ # Belt-and-braces echo reference: the exact reply text, independent
+ # of the provider's (possibly lagging/garbled) readback
+ # transcription (BUG-089).
+ self._register_spoken_reference(str(turn_state.last_reply or ""))
drop_before_delivery = self._drop_provider_output_until_new_response
self._drop_provider_output_until_new_response = False
try:
@@ -3761,6 +3880,9 @@ class RealtimeVoiceSession:
return
try:
turn_state.delivery_started = True
+ # Belt-and-braces echo reference, same rationale as the
+ # deterministic delivery path (BUG-089).
+ self._register_spoken_reference(str(turn_state.last_reply or ""))
drop_before_delivery = self._drop_provider_output_until_new_response
self._drop_provider_output_until_new_response = False
for call_id, wire_name in tuple(turn_state.pending_tool_calls):
@@ -3914,6 +4036,10 @@ class RealtimeVoiceSession:
pass
self._note_audio_flow(pcm, chunk)
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).
+ rate = max(1, int(getattr(chunk, "sample_rate", 0) or 24_000))
+ self._advance_echo_horizon((len(pcm) / 2) / rate)
await self._send_binary(pcm)
def _note_audio_flow(self, pcm: bytes, chunk: Any) -> None:
@@ -3995,6 +4121,7 @@ class RealtimeVoiceSession:
pass
self._output_samples_sent = 0
self._output_active = False
+ self._reset_echo_horizon()
try:
await self._send_json({"type": "tts_cancel"})
except Exception: # noqa: BLE001, S110
diff --git a/tests/unit/realtime/test_session_self_echo.py b/tests/unit/realtime/test_session_self_echo.py
new file mode 100644
index 0000000000..037d4ec7ae
--- /dev/null
+++ b/tests/unit/realtime/test_session_self_echo.py
@@ -0,0 +1,325 @@
+"""BUG-089: the realtime session must never answer its own speaker echo.
+
+The Mac live test (2026-07-18) looped forever: a surface-spoken canned
+apology leaked from the speakers into the built-in mic, came back as a
+provider-transcribed "user" turn, and the session answered it — two voices
+conversing with each other. These tests pin the text-level backstop: every
+text the session makes audible is registered with its ``SelfEchoGuard``, and
+a final input transcript that is fuzzily nothing but that recent speech is
+dropped before ANY turn side effect (no response request, no user
+transcript, no barge). German fixture strings quote the runtime voice
+product surface (the actual Mac transcript).
+"""
+
+from __future__ import annotations
+
+import asyncio
+import time
+from types import SimpleNamespace
+
+import pytest
+
+from jarvis.core.protocols import AudioChunk
+from jarvis.realtime.protocol import RealtimeEvent
+from jarvis.realtime.session import RealtimeVoiceSession
+
+
+class FakeSession:
+ session_id = "fake"
+ supports_tool_updates = True
+ creates_responses_automatically = False
+ isolates_response_generations = False
+
+ def __init__(self, events):
+ self._events = events
+ self.sent_audio = []
+ self.tool_results = []
+ self.truncated = []
+ self.session_updates = []
+ self.response_requests = 0
+ self.text_inputs = []
+ self.interrupts = 0
+ self.closed = False
+
+ async def send_audio(self, chunk):
+ self.sent_audio.append(chunk)
+
+ async def receive(self):
+ for event in self._events:
+ yield event
+ await asyncio.sleep(0)
+
+ async def update_session(self, *, instructions=None, language=None, tools=None):
+ self.session_updates.append(
+ {"instructions": instructions, "language": language, "tools": tools}
+ )
+
+ async def request_response(self, *, required_tool=None):
+ del required_tool
+ self.response_requests += 1
+
+ async def send_text(self, text):
+ self.text_inputs.append(text)
+
+ async def truncate(self, audio_end_ms):
+ self.truncated.append(audio_end_ms)
+
+ async def interrupt(self):
+ self.interrupts += 1
+
+ async def send_tool_result(self, call_id, name, result):
+ self.tool_results.append((call_id, name, result))
+
+ async def close(self):
+ self.closed = True
+
+
+class AutoResponseFakeSession(FakeSession):
+ creates_responses_automatically = True
+
+
+class FakeProvider:
+ name = "fake"
+ supports_realtime = True
+ input_sample_rate = 16000
+ output_sample_rate = 24000
+ session_cls = FakeSession
+
+ def __init__(self, events):
+ self._events = events
+ self.opened_with = None
+
+ async def can_open_duplex_session(self):
+ return True
+
+ async def open_session(self, cfg):
+ self.opened_with = cfg
+ self.session = self.session_cls(self._events)
+ return self.session
+
+
+class AutoResponseFakeProvider(FakeProvider):
+ session_cls = AutoResponseFakeSession
+
+
+def _cfg():
+ return SimpleNamespace(
+ brain=SimpleNamespace(reply_language="auto", providers={}),
+ stt=SimpleNamespace(language="auto"),
+ voice=SimpleNamespace(mode="realtime"),
+ latency=SimpleNamespace(enabled=False),
+ )
+
+
+async def _run(provider, *, arm=None):
+ """Drive one scripted session; return (fake_session, json_messages)."""
+ jsons = []
+ sess = RealtimeVoiceSession(
+ session_id="echo-test",
+ send_binary=lambda _data: asyncio.sleep(0),
+ send_json=lambda message: jsons.append(message) or asyncio.sleep(0),
+ provider=provider,
+ config=_cfg(),
+ bus=None,
+ )
+ if arm is not None:
+ arm(sess)
+ await sess.handle_control({"type": "audio_start", "sample_rate": 16000})
+ await asyncio.sleep(0.05)
+ await sess.end(reason="test")
+ return provider.session, jsons
+
+
+def _user_transcripts(jsons):
+ return [
+ message
+ for message in jsons
+ if message.get("type") == "transcript" and message.get("role") == "user"
+ ]
+
+
+async def test_canned_phrase_echo_never_becomes_a_turn():
+ """THE loop regression: the spoken apology returns garbled as 'user' input."""
+ provider = FakeProvider(
+ [
+ RealtimeEvent(
+ type="input_transcript",
+ # Garbled echo of the canned provider-down apology — exactly
+ # the Mac loop's fuel.
+ text="mein Sprachmodell ist im Moment nicht erreichbar", # i18n-allow: garbled echo under test
+ is_final=True,
+ ),
+ RealtimeEvent(type="turn_complete"),
+ ]
+ )
+ fake, jsons = await _run(
+ provider,
+ arm=lambda sess: sess._surface_speech_message(
+ "Tut mir leid, mein Sprachmodell ist im Moment " # i18n-allow: voice fixture
+ "nicht erreichbar. Ich versuche es gleich erneut." # i18n-allow: voice fixture
+ ),
+ )
+ assert fake.response_requests == 0
+ assert fake.text_inputs == []
+ assert _user_transcripts(jsons) == []
+ # No second apology was triggered by the echo either.
+ assert not [m for m in jsons if m.get("type") == "error_spoken"]
+
+
+async def test_provider_output_transcript_echo_is_dropped():
+ """The provider's own voiced reply, echoed back after the turn, is dropped."""
+ provider = FakeProvider(
+ [
+ RealtimeEvent(
+ type="output_transcript_delta",
+ text="Moin, bei mir ist alles bestens und ich bin bereit.", # i18n-allow: voice fixture
+ ),
+ RealtimeEvent(
+ type="audio_delta",
+ audio=AudioChunk(pcm=b"\x01\x02" * 2_400, sample_rate=24000, timestamp_ns=0),
+ ),
+ RealtimeEvent(type="turn_complete"),
+ RealtimeEvent(
+ type="input_transcript",
+ text="bei mir ist alles bestens ich bin bereit", # i18n-allow: garbled echo under test
+ is_final=True,
+ ),
+ ]
+ )
+ fake, jsons = await _run(provider)
+ assert fake.response_requests == 0
+ assert _user_transcripts(jsons) == []
+
+
+async def test_genuine_user_turn_with_novel_content_passes():
+ """Fail-open pin: an answer that ADDS anything is a real turn."""
+ provider = FakeProvider(
+ [
+ RealtimeEvent(
+ type="input_transcript",
+ text="Ich will wissen was morgen für ein Tag ist", # i18n-allow: voice fixture
+ is_final=True,
+ ),
+ RealtimeEvent(type="turn_complete"),
+ ]
+ )
+ fake, jsons = await _run(
+ provider,
+ arm=lambda sess: sess._surface_speech_message(
+ "Tut mir leid, mein Sprachmodell ist im Moment nicht erreichbar." # i18n-allow: voice fixture
+ ),
+ )
+ assert fake.response_requests == 1
+ assert _user_transcripts(jsons)
+
+
+async def test_short_commands_are_never_judged():
+ """Sub-3-token turns always reach their handlers, echo window or not."""
+ provider = FakeProvider(
+ [
+ RealtimeEvent(
+ type="input_transcript",
+ text="stopp", # i18n-allow: voice fixture
+ is_final=True,
+ ),
+ RealtimeEvent(type="turn_complete"),
+ ]
+ )
+ fake, _jsons = await _run(
+ provider,
+ arm=lambda sess: sess._surface_speech_message(
+ "Soll ich wirklich stoppen und auflegen?" # i18n-allow: voice fixture
+ ),
+ )
+ assert fake.response_requests == 1
+
+
+async def test_echo_drop_interrupts_auto_response_provider():
+ """Auto-response adapters get a best-effort interrupt + output withhold."""
+ provider = AutoResponseFakeProvider(
+ [
+ RealtimeEvent(
+ type="input_transcript",
+ text="mein Sprachmodell ist im Moment nicht erreichbar", # i18n-allow: garbled echo under test
+ is_final=True,
+ ),
+ ]
+ )
+ jsons = []
+ sess = RealtimeVoiceSession(
+ session_id="echo-auto",
+ send_binary=lambda _data: asyncio.sleep(0),
+ send_json=lambda message: jsons.append(message) or asyncio.sleep(0),
+ provider=provider,
+ config=_cfg(),
+ bus=None,
+ )
+ sess._surface_speech_message(
+ "Tut mir leid, mein Sprachmodell ist im Moment nicht erreichbar." # i18n-allow: voice fixture
+ )
+ await sess.handle_control({"type": "audio_start", "sample_rate": 16000})
+ await asyncio.sleep(0.05)
+ dropped_flag = sess._drop_provider_output_until_user_turn
+ await sess.end(reason="test")
+ assert provider.session.interrupts == 1
+ assert dropped_flag is True
+
+
+async def test_playback_horizon_future_dates_the_guard():
+ """Long provider audio arms the guard past the plain wall-clock window."""
+ sess = RealtimeVoiceSession(
+ session_id="horizon",
+ send_binary=lambda _data: asyncio.sleep(0),
+ send_json=lambda _message: asyncio.sleep(0),
+ provider=FakeProvider([]),
+ config=_cfg(),
+ bus=None,
+ )
+ # 12 s of audio at 24 kHz, streamed far faster than realtime.
+ chunk = AudioChunk(pcm=b"\x01\x02" * (24_000 * 3), sample_rate=24000, timestamp_ns=0)
+ for _ in range(4):
+ await sess._emit_audio(chunk)
+ lead_ns = sess._echo_guard.activity_ns - time.time_ns()
+ assert lead_ns > int(8e9), "horizon must extend ~12s into the future"
+
+
+async def test_playback_horizon_is_capped():
+ """A mis-reported sample rate cannot arm the guard for hours."""
+ sess = RealtimeVoiceSession(
+ session_id="horizon-cap",
+ send_binary=lambda _data: asyncio.sleep(0),
+ send_json=lambda _message: asyncio.sleep(0),
+ provider=FakeProvider([]),
+ config=_cfg(),
+ bus=None,
+ )
+ chunk = AudioChunk(pcm=b"\x01\x02" * 24_000, sample_rate=1, timestamp_ns=0)
+ await sess._emit_audio(chunk)
+ lead_ns = sess._echo_guard.activity_ns - time.time_ns()
+ assert lead_ns <= int(121e9)
+
+
+async def test_barge_in_resets_the_horizon():
+ """A real barge-in pulls the armed horizon back to 'now'."""
+ sess = RealtimeVoiceSession(
+ session_id="horizon-reset",
+ send_binary=lambda _data: asyncio.sleep(0),
+ send_json=lambda _message: asyncio.sleep(0),
+ provider=FakeProvider([]),
+ config=_cfg(),
+ bus=None,
+ )
+ chunk = AudioChunk(pcm=b"\x01\x02" * (24_000 * 3), sample_rate=24000, timestamp_ns=0)
+ for _ in range(4):
+ await sess._emit_audio(chunk)
+ await sess._barge_in(interrupt_provider=False)
+ lead_ns = sess._echo_guard.activity_ns - time.time_ns()
+ assert lead_ns < int(1e9), "horizon must collapse to ~now after a barge"