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.
Python agent-orchestrationai-agentai-coding-assistantclaude-codecoding-agentcomputer-usedesktop-applocal-firstmcppersonal-jarvisvoice-assistantvoice-coding
fix(realtime): energy pre-gate in the desktop barge-in detector (BUG-062)
Ruben Lütke4f4dda263e06
diff --git a/docs/BUGS.md b/docs/BUGS.mdindex c2b4a41a71..26e9744a44 100644--- a/docs/BUGS.md+++ b/docs/BUGS.md@@ -4358,3 +4358,45 @@ platform must never sit unconditionally in the base lock: gate it withplatform markers and give the capability an honest degrade. Wheelmatrices shrink over time — "it resolved when pinned" is not "it resolveseverywhere forever".++## BUG-062: Realtime speech audibly choppy + answers cut short on a speakers+mic laptop (HIGH, PARTIALLY FIXED 2026-07-14, deep-dive documented)++**Symptom.** First real realtime-voice run (old Intel MacBook, CPU-only,+built-in speakers + mic): the assistant's TRANSCRIPT is complete, but the+audible speech stutters constantly and large parts are never heard.++**Deep-dive findings (full trace in the audit, two compounding causes).**++1. **Self-barge-in via speaker echo.** The desktop realtime path arms the+ Silero barge-in detector during playback but — unlike the classic+ pipeline — never arms any echo-suppression window+ (``_suppress_session_input_after_tts`` is classic-path only). Open+ speakers feed the assistant's own voice into the detector; a false+ confirm truncates the provider response AND aborts/drains playback —+ exactly "complete transcript, mostly unheard".+2. **Event-loop starvation from per-frame ONNX.** ``barge_detector.feed``+ runs ~3 Silero inferences per 100 ms mic block synchronously on the+ voice event loop for the whole answer; on a slow CPU this delays the+ 120 ms playback write batches → PortAudio underruns → steady stutter.+ (Resampling ruled out: 24 kHz == 24 kHz. Queue drops ruled out: the+ playback queue blocks, never drops.)++**Fix now (2026-07-14).** Energy pre-gate in+``DesktopRealtimeBargeInDetector.feed`` (``min_frame_rms``, default 0.010,+AP-27-anchored: silence ghosts <= 0.0043, quiet speech ~0.009): quiet+frames never reach the ONNX model — removing most of the per-frame CPU+load AND damping moderate speaker echo. Documented trade-off:+whisper-quiet barge-in no longer triggers. Guards:+``tests/unit/realtime/test_desktop.py`` (gate skips ONNX on quiet frames,+loud speech still confirms; logic tests pin ``min_frame_rms=0.0``).++**Follow-ups (need on-device validation, tracked).** (a) A realtime echo-+suppression window analogous to the classic path, or an energy comparison+against the currently playing output level; (b) offload the detector off+the audio-critical loop; (c) a small time-based release floor in+``ScrubHoldGate`` so audio is not strictly transcript-delta-clocked.++**Class rule.** Half-duplex voice on open speakers MUST treat its own+output as hostile input: any interrupt detector needs an energy floor or+echo reference before it may cancel playback, and nothing compute-heavy+belongs on the audio-critical loop per mic frame.diff --git a/jarvis/realtime/desktop.py b/jarvis/realtime/desktop.pyindex 24f110f2ce..1f4d95e509 100644--- a/jarvis/realtime/desktop.py+++ b/jarvis/realtime/desktop.py@@ -31,6 +31,18 @@ class DesktopRealtimeBargeInDetector:responsible for desktop echo suppression."""+ # Frames quieter than this (float RMS, 1.0 = full scale) skip the Silero+ # inference entirely and count as non-speech. Two field problems on a+ # speakers+mic laptop (old Intel MacBook, BUG-062) share this one gate:+ # (a) per-frame ONNX on the audio-critical loop starved the 120 ms write+ # batches -> constant playback stutter; (b) the assistant's own speaker+ # echo fed the detector and false barge-ins truncated the answer. AP-27+ # empirics anchor the value: silence ghosts sit <= 0.0043, quiet real+ # speech reaches ~0.009 — 0.010 keeps deliberate interruptions (normal+ # speaking volume) while dropping silence, hiss, and moderate echo.+ # Trade-off (documented): whisper-quiet barge-in no longer triggers.+ _DEFAULT_MIN_FRAME_RMS = 0.010+def __init__(self,*,@@ -38,12 +50,16 @@ class DesktopRealtimeBargeInDetector:speech_threshold: float = 0.97,consecutive_frames: int = 12,pre_speech_frames: int = 10,+ min_frame_rms: float | None = None,model: Any = None,) -> None:self._grace_s = max(0.0, float(grace_s))self._speech_threshold = min(1.0, max(0.0, float(speech_threshold)))self._consecutive_frames = max(1, int(consecutive_frames))self._pre_speech_frames = max(1, int(pre_speech_frames))+ self._min_frame_rms = (+ self._DEFAULT_MIN_FRAME_RMS if min_frame_rms is None else max(0.0, float(min_frame_rms))+ )self._model = model or SileroEndpointer(speech_threshold=self._speech_threshold)@@ -106,7 +122,14 @@ class DesktopRealtimeBargeInDetector:for index, frame in enumerate(frames):normalized = frame.astype(np.float32) / 32768.0- probability = float(self._model._prob(normalized))+ # Energy pre-gate (BUG-062): quiet frames never reach the ONNX+ # model — this is both the loop-load fix and the echo damper.+ if self._min_frame_rms > 0.0 and float(+ np.sqrt(np.mean(np.square(normalized)))+ ) < self._min_frame_rms:+ probability = 0.0+ else:+ probability = float(self._model._prob(normalized))if probability >= self._speech_threshold:if self._speech_run == 0:self._candidate_frames = [part.copy() for part in self._pre_buffer]diff --git a/tests/unit/realtime/test_desktop.py b/tests/unit/realtime/test_desktop.pyindex 611bbb583c..87453226a2 100644--- a/tests/unit/realtime/test_desktop.py+++ b/tests/unit/realtime/test_desktop.py@@ -46,6 +46,7 @@ def _pcm_frames(*amplitudes: int) -> bytes:def test_cpu_barge_detector_returns_prespeech_and_confirmed_user_audio() -> None:model = FakeVadModel([0.1, 0.2, 0.99, 0.99, 0.99])detector = DesktopRealtimeBargeInDetector(+ min_frame_rms=0.0,grace_s=0,consecutive_frames=3,pre_speech_frames=2,@@ -64,6 +65,7 @@ def test_cpu_barge_detector_returns_prespeech_and_confirmed_user_audio() -> Nonedef test_cpu_barge_detector_rejects_short_speaker_bleed() -> None:model = FakeVadModel([0.99, 0.1, 0.99, 0.99, 0.1])detector = DesktopRealtimeBargeInDetector(+ min_frame_rms=0.0,grace_s=0,consecutive_frames=3,model=model,@@ -188,3 +190,46 @@ async def test_handshake_sample_rate_is_used_for_the_next_turn():await playback.finish_turn()assert [chunk.sample_rate for chunk in player.chunks] == [16_000]+++def test_energy_pre_gate_skips_onnx_for_quiet_frames() -> None:+ # BUG-062: quiet frames (silence / hiss / moderate speaker echo) must+ # never reach the Silero model — that is both the loop-load fix (stutter+ # on slow CPUs) and the self-barge-in damper on speakers+mic laptops.+ calls: list[int] = []++ class _CountingModel:+ def _ensure_model(self) -> None: ...++ def _prob(self, _frame) -> float:+ calls.append(1)+ return 1.0++ detector = DesktopRealtimeBargeInDetector(+ grace_s=0.0, consecutive_frames=1, model=_CountingModel()+ )+ detector.warmup()+ detector.start_output()+ # Amplitude 100/32768 ≈ 0.003 RMS — below the 0.010 floor.+ assert detector.feed(_pcm_frames(100, 100, 100)) is None+ assert calls == [] # ONNX never consulted+++def test_energy_pre_gate_passes_loud_speech_through() -> None:+ calls: list[int] = []++ class _CountingModel:+ def _ensure_model(self) -> None: ...++ def _prob(self, _frame) -> float:+ calls.append(1)+ return 1.0++ detector = DesktopRealtimeBargeInDetector(+ grace_s=0.0, consecutive_frames=1, model=_CountingModel()+ )+ detector.warmup()+ detector.start_output()+ # Amplitude 2000/32768 ≈ 0.061 RMS — normal speaking volume.+ assert detector.feed(_pcm_frames(2000)) is not None+ assert calls # the model ran and confirmed