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): let an utterance end on a real microphone, not only on digital silence
rubenluetke10-beepb9b78b42ded1
diff --git a/jarvis/realtime/input_transcription.py b/jarvis/realtime/input_transcription.pyindex e32e4a7b5c..444be0cf12 100644--- a/jarvis/realtime/input_transcription.py+++ b/jarvis/realtime/input_transcription.py@@ -44,9 +44,8 @@ log = logging.getLogger(__name__)# down against the session's own noise floor — see ``_NoiseFloor``._SPEECH_RMS = 700.0_SILENCE_RMS = 500.0-# The hysteresis between "speech" and "silence" is a RATIO, not two independent-# numbers, so a calibrated speech gate carries the silence gate down with it and-# the two can never cross.+#: The legacy relationship between the two absolute gates, reused to scale the+#: silence gate whenever the speech gate is calibrated down._SILENCE_RATIO = _SILENCE_RMS / _SPEECH_RMS_SPEECH_START_MS = 120_SILENCE_END_MS = 700@@ -76,13 +75,35 @@ _MIN_VOICED_MS = 300# The word-agnostic discriminator (AP-27) survives BY CONSTRUCTION: silence and# speaker echo sit AT the floor while the gate sits at ``_SPEECH_GATE_K`` x the# floor, at ANY mic gain. Nothing here looks at what was said — only at energy.-_FLOOR_INIT = 600.0+_FLOOR_INIT = 350.0_FLOOR_ABS_MIN = 7.0 # a muted mic's numeric noise must never define a floor_FLOOR_ALPHA = 0.05 # EMA weight per quiet chunk_FLOOR_QUIET_BAND = 1.5 # a chunk this close to the floor counts as quiet-_SPEECH_GATE_K = 1.2+# The floor must also be able to rise. A room noisier than the initial estimate+# puts every non-speech chunk ABOVE the quiet band, so a purely downward+# estimator can never learn it: the silence gate stays under the room tone and+# the utterance only ever ends at ``_MAX_UTTERANCE_MS`` — a transcript up to+# 20 s late. A sustained run of loud NON-SPEECH chunks cannot be one utterance,+# so it is the room, and the floor follows it. Capped at ``_SPEECH_RMS`` so a+# leaking speaker can never desensitize the gate past its legacy value.+_FLOOR_RISE_AFTER = 25 # consecutive above-band non-speech chunks (~0.5 s)+_FLOOR_RISE_ALPHA = 0.25+_SPEECH_GATE_K = 2.0_SPEECH_GATE_ABS_MIN = 50.0+# The SILENCE gate is anchored to the floor FROM ABOVE, never derived from the+# speech gate from below. Deriving it downward is a trap with a sharp edge: the+# floor is by definition the level real silence sits at, so any gate placed+# BELOW the floor can never be crossed — ``_silence_ms`` never accumulates, the+# utterance never closes, and the recognizer is never called. The endpointer+# then produces one ``speech_started`` and no transcript at all, which on this+# transport reads as "the first turn worked and then it went mute forever".+#+# So: sit a clear margin ABOVE the measured floor, and clamp strictly below the+# speech gate so the hysteresis can never invert.+_SILENCE_GATE_FLOOR_K = 1.4+_SILENCE_GATE_MAX_FRACTION = 0.9+# How long after the microphone last carried real speech a server-side user# transcript is still plausible. The far end transcribes with its own latency,# so a genuine transcript trails the audio; a hallucinated one arrives while@@ -120,11 +141,16 @@ _EVENT_QUEUE_MAX = 256SPEECH_STARTED = "speech_started"TRANSCRIPT = "transcript"TRANSCRIPT_FAILED = "transcript_failed"-#: An utterance the endpointer opened but then discarded as too short to be-#: speech. It carries no text and must never become a turn — but it must not-#: vanish either: ``speech_started`` already told the transport a human spoke,-#: and a boundary that never arrives is a turn that hangs.-SPEECH_DISCARDED = "speech_discarded"++# A discarded (too short) utterance is reported through the LOG, deliberately+# not as a new event kind. This stream has exactly one reader, and it branches+# on the two kinds above and routes everything else through a fallback that+# wipes the provider's own user-transcript preview — so a new kind would make+# every cough destroy the very fallback the failure path exists to promote.+# Nothing waits on a close for a discarded utterance either (the transport ends+# its turns on its own boundaries, not on this stream), so the honest fix is+# observability, not a wire change.+_DISCARD_STREAK_WARN = 5class RecognizerBusy(RuntimeError):@@ -184,15 +210,32 @@ class _NoiseFloor:self._abs_min = float(abs_min)self._alpha = float(alpha)self._quiet_band = float(quiet_band)+ self._above_band = 0@propertydef floor(self) -> float:return self._floordef update(self, rms: float) -> None:+ """Fold one NON-SPEECH chunk into the estimate.++ Only the caller knows whether an utterance is open; feeding speech in+ here would let one loud sentence redefine the room.+ """if rms < self._floor * self._quiet_band:+ self._above_band = 0floor = (1.0 - self._alpha) * self._floor + self._alpha * rmsself._floor = max(floor, self._abs_min)+ return+ # Above the band, and not speech: either the room is louder than the+ # current estimate, or this is a transient. Only a sustained run is+ # allowed to move the floor upward.+ self._above_band += 1+ if self._above_band < _FLOOR_RISE_AFTER:+ return+ self._above_band = 0+ floor = (1.0 - _FLOOR_RISE_ALPHA) * self._floor + _FLOOR_RISE_ALPHA * rms+ self._floor = min(max(floor, self._abs_min), _SPEECH_RMS)def gate(self, configured: float, k: float, abs_min: float) -> float:"""Lower-only effective gate: never above ``configured``."""@@ -248,6 +291,7 @@ class LocalInputTranscriber:self._build_lock = asyncio.Lock()self._consecutive_failures = 0self._consecutive_busy = 0+ self._discarded_streak = 0self._closed = Falseself._unavailable_logged = Falseself._rate_mismatch_logged = False@@ -277,6 +321,23 @@ class LocalInputTranscriber:def _speech_gate(self) -> float:return self._floor.gate(_SPEECH_RMS, _SPEECH_GATE_K, _SPEECH_GATE_ABS_MIN)+ def _silence_gate(self, speech_gate: float) -> float:+ """The level below which a chunk counts as silence, for THIS session.++ Anchored above the measured noise floor rather than scaled down from+ the speech gate: real silence sits AT the floor, so a gate underneath it+ is unreachable and the utterance never ends. The scaled legacy value is+ the lower bound (so a quiet room keeps the historical behaviour) and the+ speech gate the upper bound (so the hysteresis cannot invert).+ """+ return min(+ speech_gate * _SILENCE_GATE_MAX_FRACTION,+ max(+ speech_gate * _SILENCE_RATIO,+ self._floor.floor * _SILENCE_GATE_FLOOR_K,+ ),+ )+# -- feeding -------------------------------------------------------def feed(self, pcm: bytes, sample_rate: int) -> None:"""Accept one microphone chunk; never blocks the audio path."""@@ -328,7 +389,7 @@ class LocalInputTranscriber:self._utterance_ms += duration_msif level >= speech_gate:self._voiced_ms += duration_ms- if level < speech_gate * _SILENCE_RATIO:+ if level < self._silence_gate(speech_gate):self._silence_ms += duration_mselse:self._silence_ms = 0@@ -338,6 +399,39 @@ class LocalInputTranscriber:):self._finish_utterance()+ def _note_discarded(self, voiced_ms: int) -> None:+ """Report an utterance that opened but was too short to be speech.++ Individually this is correct and uninteresting — a cough must not buy a+ recognizer call. A RUN of them without a single transcript in between is+ the interesting signal: it means the gate is opening on something that+ never turns into speech, which is what a mis-calibrated floor or a+ half-open microphone looks like from here. Saying only the first half+ would be a discard that reports nothing actionable (AP-30).+ """+ self._discarded_streak += 1+ log.debug(+ "Local input transcription discarded a %d ms utterance "+ "(minimum %d ms voiced, gate %.0f, floor %.0f)",+ voiced_ms,+ _MIN_VOICED_MS,+ self._speech_gate(),+ self._floor.floor,+ )+ if self._discarded_streak != _DISCARD_STREAK_WARN:+ return+ log.warning(+ "Local input transcription has discarded %d utterances in a row as "+ "too short (last %d ms, minimum %d ms voiced, gate %.0f, floor "+ "%.0f). Speech is opening the gate but never qualifying, so this "+ "call is producing no user transcripts at all.",+ self._discarded_streak,+ voiced_ms,+ _MIN_VOICED_MS,+ self._speech_gate(),+ self._floor.floor,+ )+def _begin_utterance(self) -> None:self._in_speech = True# The frames that proved speech started are already voiced audio.@@ -360,24 +454,11 @@ class LocalInputTranscriber:self._silence_ms = 0self._speech_ms = 0if voiced_ms < _MIN_VOICED_MS or not pcm:- # Too short to be speech. It still needs a boundary: the matching- # ``speech_started`` already told the transport a human spoke, and a- # turn that opens without ever closing hangs (AP-30 — a discard that- # neither logs nor signals is how a turn disappears silently).- log.debug(- "Local input transcription discarded a %d ms utterance "- "(minimum %d ms voiced, gate %.0f, floor %.0f)",- voiced_ms,- _MIN_VOICED_MS,- self._speech_gate(),- self._floor.floor,- )- self._emit(- InputTranscriptEvent(kind=SPEECH_DISCARDED, voiced_ms=voiced_ms)- )+ self._note_discarded(voiced_ms)return# Only a qualifying utterance vouches for a server-side transcript; a# cough must not open the door that the energy gate just closed.+ self._discarded_streak = 0self._last_speech_end = time.monotonic()task = asyncio.create_task(self._transcribe(pcm, voiced_ms), name="realtime-local-input-transcribe"@@ -534,7 +615,6 @@ class LocalInputTranscriber:"recognizer is still busy with an earlier segment",voiced_ms,)- await self._note_busy()self._emit(InputTranscriptEvent(kind=TRANSCRIPT_FAILED, voiced_ms=voiced_ms))@@ -545,12 +625,11 @@ class LocalInputTranscriber:"the recognizer is treated as wedged",voiced_ms,)- await self._note_failure("timeout")self._emit(InputTranscriptEvent(kind=TRANSCRIPT_FAILED, voiced_ms=voiced_ms))return- except Exception as exc: # noqa: BLE001 - one failed utterance is not fatal+ except Exception: # noqa: BLE001 - one failed utterance is not fatalif self._stt is None:self._log_unavailable()else:@@ -558,21 +637,10 @@ class LocalInputTranscriber:"Local input transcription failed for one utterance",exc_info=True,)- # A provider reporting its OWN engine as busy (the non-blocking- # in-flight guard the local Whisper provider raises) is the same- # "slow but alive" signal as our own skip, not an independent- # failure. Recognized by shape, never by importing the provider —- # the realtime path must stay importable without the local-voice- # extra installed.- if type(exc).__name__ == "TranscribeBusy":- await self._note_busy()- else:- await self._note_failure(type(exc).__name__)self._emit(InputTranscriptEvent(kind=TRANSCRIPT_FAILED, voiced_ms=voiced_ms))return- self._note_success()if not text:# Real speech that produced no words: the far end's own transcript# of the same audio is now the best thing Jarvis has.@@ -621,27 +689,57 @@ class LocalInputTranscriber:return ""if self._recognition_busy:+ await self._note_busy()raise RecognizerBusy("the local recognizer is already transcribing another segment")++ from jarvis.core.protocols import AudioChunk # noqa: PLC0415++ async def chunks(): # noqa: ANN202 - one-shot adapter+ yield AudioChunk(+ pcm=payload,+ sample_rate=target_rate,+ timestamp_ns=0,+ channels=1,+ )++ # Health accounting lives HERE, at the one choke point both callers pass+ # through — the streaming utterance path and the transport's+ # output-transcript recovery. Counting it only in the caller above left+ # a wedge first hit by recovery invisible to the leash, so the self-heal+ # needed four lost utterances instead of two.self._recognition_busy = Truetry:- from jarvis.core.protocols import AudioChunk # noqa: PLC0415-- async def chunks(): # noqa: ANN202 - one-shot adapter- yield AudioChunk(- pcm=payload,- sample_rate=target_rate,- timestamp_ns=0,- channels=1,- )-result = await asyncio.wait_for(stt.transcribe(chunks()),timeout=self._recognition_timeout_s(payload, target_rate),)- finally:+ except TimeoutError:self._recognition_busy = False+ await self._note_failure("timeout")+ raise+ except Exception as exc: # noqa: BLE001 - classified, counted, re-raised+ self._recognition_busy = False+ # A provider reporting its OWN engine as busy (the non-blocking+ # in-flight guard the local Whisper provider raises) is the same+ # "slow but alive" signal as our own skip, not an independent+ # failure. Recognized by shape, never by importing the provider —+ # the realtime path must stay importable without the local-voice+ # extra installed.+ if type(exc).__name__ == "TranscribeBusy":+ await self._note_busy()+ else:+ await self._note_failure(type(exc).__name__)+ raise+ except BaseException:+ # Cancellation is the CALLER giving up, not the engine failing. It+ # does abandon the worker thread, but the next call's busy report is+ # what proves that — counting it here would double-count one wedge.+ self._recognition_busy = False+ raise+ self._recognition_busy = False+ self._note_success()return str(getattr(result, "text", "") or "").strip()def _to_recognizer_rate(self, pcm: bytes, target_rate: int) -> bytes:@@ -718,7 +816,6 @@ class LocalInputTranscriber:__all__ = [- "SPEECH_DISCARDED","SPEECH_STARTED","TRANSCRIPT","TRANSCRIPT_FAILED",diff --git a/tests/unit/realtime/test_input_transcription.py b/tests/unit/realtime/test_input_transcription.pyindex 8d2377315b..8cbba5b62d 100644--- a/tests/unit/realtime/test_input_transcription.py+++ b/tests/unit/realtime/test_input_transcription.py@@ -44,9 +44,24 @@ def _loud(samples: int = CHUNK_SAMPLES) -> bytes:def _quiet(samples: int = CHUNK_SAMPLES) -> bytes:+ """Digital silence — a muted or disconnected input, NOT a real room.++ Kept for the mute cases only. Endpointing must never be judged on it: it is+ the one input level at which a silence gate placed below the noise floor+ still appears to work, which is exactly how a gate that can never close on a+ real microphone shipped green. Use ``_room_tone`` for that.+ """return b"\x00\x00" * samples+def _room_tone(level: float, samples: int = CHUNK_SAMPLES, *, seed: int = 7) -> bytes:+ """Low-amplitude noise at ``level`` RMS — what a real quiet room measures."""+ import numpy as np++ rng = np.random.default_rng(seed)+ return rng.normal(0.0, level, samples).astype(np.int16).tobytes()++class TranscribeBusy(RuntimeError):"""The class NAME the local Whisper provider raises when its engine is busy.@@ -160,14 +175,41 @@ async def test_a_blip_of_noise_never_becomes_a_transcript() -> None:transcriber = LocalInputTranscriber(sample_rate=RATE, stt_factory=lambda: stt)_speak(transcriber, speech_chunks=8) # 160 ms - too short to be speech- events = await _drain(transcriber, 2)+ events = await _drain(transcriber, 1)assert events[0].kind == module.SPEECH_STARTED- # It must not become a turn - but it must not vanish either: the boundary- # tells the transport that the turn opened by ``speech_started`` is over.- assert events[1].kind == module.SPEECH_DISCARDED- assert events[1].text == ""- assert events[1].voiced_ms < module._MIN_VOICED_MS # noqa: SLF001+ assert stt.calls == 0+ # Nothing follows it on the wire. The discard is reported through the log,+ # NOT as a new event kind: this stream's one reader routes any unknown kind+ # through a fallback that wipes the provider's own user-transcript preview,+ # so a per-cough event would destroy the very fallback that the failure path+ # exists to promote.+ with pytest.raises(asyncio.TimeoutError):+ await asyncio.wait_for(transcriber.next_event(), timeout=0.05)+ await transcriber.close()+++async def test_a_run_of_discarded_utterances_reports_itself(caplog) -> None:+ """One cough is uninteresting; a run of them without a transcript is not.++ It means the gate opens on something that never becomes speech — a+ mis-calibrated floor or a half-open microphone — and the call is producing+ no user transcripts at all (AP-30: report what silence would hide).+ """+ stt = _FakeSTT()+ transcriber = LocalInputTranscriber(sample_rate=RATE, stt_factory=lambda: stt)++ with caplog.at_level("WARNING"):+ for _ in range(module._DISCARD_STREAK_WARN): # noqa: SLF001+ _speak(transcriber, speech_chunks=8)++ warnings = [+ record+ for record in caplog.records+ if "discarded" in record.getMessage() and "in a row" in record.getMessage()+ ]+ assert len(warnings) == 1assert stt.calls == 0await transcriber.close()@@ -483,6 +525,78 @@ def test_the_recognition_bound_scales_with_the_segment() -> None:assert long <= module._RECOGNITION_TIMEOUT_MAX_S # noqa: SLF001+# -- endpointing against REAL room tone, at every plausible noise floor ------++[email protected]("floor_level", [0, 20, 40, 80, 150, 300, 600])+async def test_an_utterance_ends_on_room_tone_at_any_noise_floor(+ floor_level: int,+) -> None:+ """The regression that made this file's own suite worthless.++ Real silence sits AT the session's noise floor — that is what the floor+ estimates. A silence gate derived DOWNWARD from the calibrated speech gate+ lands below it, so ``_silence_ms`` can never accumulate, the utterance never+ closes and the recognizer is never called: one ``speech_started``, no+ transcript, forever. On this transport that removes the grounding evidence+ for every later turn, so the provider's answers get rejected as self-echo+ and interrupted — "the first turn worked and then it went mute".++ Every earlier endpointing test closed its utterance with digital zeros, the+ single input level at which the broken gate still worked.+ """+ stt = _FakeSTT("heard in a real room")+ transcriber = LocalInputTranscriber(sample_rate=RATE, stt_factory=lambda: stt)++ def tone() -> bytes:+ return _quiet() if floor_level == 0 else _room_tone(floor_level)++ for _ in range(180): # ~3.6 s of room tone establishes the floor+ transcriber.feed(tone(), RATE)+ for _ in range(50): # 1 s of speech+ transcriber.feed(_loud(), RATE)+ for _ in range(200): # 4 s of the SAME room tone must close the utterance+ transcriber.feed(tone(), RATE)++ events = await _drain(transcriber, 2)+ assert events[0].kind == module.SPEECH_STARTED+ assert events[1].kind == module.TRANSCRIPT, (+ f"floor {floor_level}: the utterance never ended — the silence gate "+ f"({transcriber._silence_gate(transcriber._speech_gate()):.0f}) is not " # noqa: SLF001+ f"above the measured floor ({transcriber.noise_floor:.0f})"+ )+ await transcriber.close()++[email protected]("floor_level", [0, 20, 40, 80, 150, 300, 600])+def test_the_silence_gate_always_sits_between_the_floor_and_the_speech_gate(+ floor_level: int,+) -> None:+ """The invariant behind the test above, checked directly.++ Below the floor the utterance can never end; above the speech gate the+ hysteresis inverts and speech would close its own utterance.+ """+ transcriber = LocalInputTranscriber(sample_rate=RATE)+ tone = _quiet() if floor_level == 0 else _room_tone(floor_level)+ for _ in range(200):+ transcriber.feed(tone, RATE)++ floor = transcriber.noise_floor+ speech_gate = transcriber._speech_gate() # noqa: SLF001+ silence_gate = transcriber._silence_gate(speech_gate) # noqa: SLF001++ assert silence_gate > floor, (+ f"floor {floor_level}: silence gate {silence_gate:.0f} is at or below "+ f"the measured floor {floor:.0f} — silence can never be detected"+ )+ assert silence_gate < speech_gate, (+ f"floor {floor_level}: silence gate {silence_gate:.0f} is not below the "+ f"speech gate {speech_gate:.0f} — the hysteresis has inverted"+ )++# -- AP-23: the gate must not be a property of the maintainer's mic ----------