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(dictation): detect the spoken language reliably, in every language

Ruben Lütke5eeaa995b81c
diff --git a/jarvis/speech/pipeline.py b/jarvis/speech/pipeline.py
index ecab5ca785..be69f478c8 100644
--- a/jarvis/speech/pipeline.py
+++ b/jarvis/speech/pipeline.py
@@ -9524,7 +9524,7 @@ class SpeechPipeline:
only postpones a read; here it ends it, and the seconds behind those
bytes are words the user said that nothing will ever transcribe.
"""
- nonlocal language, session_peak, lost_audio_bytes
+ nonlocal language, session_peak, lost_audio_bytes, session_language
from jarvis.dictation.segment import (
is_silent_segment,
quietest_cut,
@@ -9568,6 +9568,25 @@ class SpeechPipeline:
if ok:
if text:
parts.append(text)
+ # The FIRST piece to come back decides the language for
+ # the rest of this recording. Without it every piece is
+ # asked to detect independently, and the answers
+ # disagree inside one dictation — which is not a
+ # cosmetic mislabel, because a piece detected wrongly
+ # comes back TRANSLATED. That is the reported "it keeps
+ # switching between German and English mid-transcript":
+ # one paragraph in the language spoken, the next one in
+ # English, from a single continuous recording.
+ #
+ # This piece is a full segment (~8 s) rather than the
+ # ~4 s the live probe works with, so it is the best
+ # reading available here — and it is only consulted when
+ # neither a user pin nor the cross-dictation anchor has
+ # already answered, both of which outrank it.
+ if not session_language:
+ code = str(lang or "").strip().lower()
+ if code and code not in ("auto", "unknown", "und"):
+ session_language = code
dead_streak = 0
piece_read = True
break
@@ -9904,6 +9923,42 @@ class SpeechPipeline:
# guesses instead of waiting its turn behind them.
readings.extend([code] * (readings.maxlen if on_device else 1))
+ def _prime_dictation_language_anchor(self) -> None:
+ """Seed the anchor from the stored history, once per process.
+
+ Without this the anchor is empty after every app start, so the first
+ short dictation of a session is back to asking two seconds of audio
+ which language it is — the exact case this exists to avoid. The history
+ already holds the answer: what this person dictated in yesterday is a
+ better opening guess than nothing at all.
+
+ Cheap enough to do inline (measured: ~1.6 ms for a 147 KB history) and
+ it happens on the first dictation, never at boot (AP-26). Failure is a
+ no-op: an unreadable history costs an opening guess, never a dictation.
+ """
+ if getattr(self, "_dictation_anchor_primed", False):
+ return
+ self._dictation_anchor_primed = True
+ try:
+ from jarvis.dictation.history import DictationHistory
+
+ entries = DictationHistory().list_all(include_discarded=True)
+ recent = [
+ entry
+ for entry in entries
+ if float(getattr(entry, "duration_s", 0.0) or 0.0)
+ >= self._LANGUAGE_ANCHOR_MIN_S
+ ][: self._LANGUAGE_ANCHOR_HISTORY]
+ # Oldest first, so the newest readings are the ones the bounded
+ # window keeps — a person who switched language last week must not
+ # be anchored to the language they used before that.
+ for entry in reversed(recent):
+ self._remember_dictation_language(
+ str(getattr(entry, "language", "") or ""), on_device=False
+ )
+ except Exception: # noqa: BLE001 — an opening guess is never worth a failure
+ log.debug("dictation language anchor priming failed", exc_info=True)
+
def _recent_dictation_language(self) -> str:
"""The language recent dictations agree on, or ``""`` when unclear.
@@ -9912,6 +9967,7 @@ class SpeechPipeline:
than the last reading, so a single mis-detected clip cannot redirect the
next one; ties resolve to ``""``, which simply restores auto-detect.
"""
+ self._prime_dictation_language_anchor()
readings = getattr(self, "_dictation_language_readings", None)
if not readings:
return ""
diff --git a/tests/unit/speech/test_dictation_recognition_language.py b/tests/unit/speech/test_dictation_recognition_language.py
index eb5b16bd81..2d871ac842 100644
--- a/tests/unit/speech/test_dictation_recognition_language.py
+++ b/tests/unit/speech/test_dictation_recognition_language.py
@@ -115,10 +115,19 @@ class TestTheBugItself:
# ---------------------------------------------------------------------------
-def _pipeline():
+def _pipeline(*, primed: bool = True):
+ """A bare pipeline for the anchor logic.
+
+ ``primed`` marks the history seed as already done, so these tests exercise
+ the arithmetic on readings they control instead of on whatever the machine
+ running them happens to have dictated. The seeding itself is tested
+ separately, against a history built for the purpose.
+ """
from jarvis.speech.pipeline import SpeechPipeline
- return SpeechPipeline.__new__(SpeechPipeline)
+ pipe = SpeechPipeline.__new__(SpeechPipeline)
+ pipe._dictation_anchor_primed = primed
+ return pipe
def test_a_fresh_pipeline_anchors_nothing() -> None:
@@ -219,3 +228,55 @@ def test_a_user_pin_still_outranks_the_anchor() -> None:
)
== "de"
)
+
+
+def test_the_anchor_is_seeded_from_the_stored_history(tmp_path, monkeypatch) -> None:
+ """After an app start the anchor must not be empty.
+
+ Without seeding, the first short dictation of every session is back to
+ asking two seconds of audio which language it is — the exact case this
+ exists to avoid, reintroduced once per restart. The history already holds
+ the answer.
+
+ Only LONG entries are allowed to seed it, for the same reason only long
+ dictations vote: a short one is what the provider gets wrong, so letting it
+ seed would let the guess teach itself.
+ """
+ from jarvis.dictation.history import DictationHistory
+ from jarvis.speech.pipeline import SpeechPipeline
+
+ path = tmp_path / "history.json"
+ store = DictationHistory(path)
+ # Two long German dictations and a pile of short ones mis-tagged English —
+ # the real shape of the bug this seed is for.
+ for _ in range(6):
+ store.add(raw_text="x", text="x", language="en", duration_s=1.5)
+ for _ in range(2):
+ store.add(raw_text="y", text="y", language="de", duration_s=12.0)
+
+ monkeypatch.setattr(
+ "jarvis.dictation.history.default_history_path", lambda: path
+ )
+ pipe = SpeechPipeline.__new__(SpeechPipeline)
+
+ assert pipe._recent_dictation_language() == "de"
+
+
+def test_seeding_happens_once_even_when_the_history_is_unreadable(
+ tmp_path, monkeypatch
+) -> None:
+ """A broken history costs an opening guess, never a dictation."""
+ from jarvis.speech.pipeline import SpeechPipeline
+
+ calls: list[int] = []
+
+ def _boom():
+ calls.append(1)
+ raise OSError("history is on a disconnected drive")
+
+ monkeypatch.setattr("jarvis.dictation.history.default_history_path", _boom)
+ pipe = SpeechPipeline.__new__(SpeechPipeline)
+
+ assert pipe._recent_dictation_language() == ""
+ assert pipe._recent_dictation_language() == ""
+ assert len(calls) == 1, "the seed must not be retried on every dictation"