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): stop the polish pass replacing words the user said

Ruben Lütke4fd2fab4ae47
diff --git a/jarvis/dictation/history.py b/jarvis/dictation/history.py
index 01e80e86a1..f51363ee2d 100644
--- a/jarvis/dictation/history.py
+++ b/jarvis/dictation/history.py
@@ -283,6 +283,7 @@ class DictationHistory:
error: str | None = None,
max_entries: int = 200,
retention_days: int = 30,
+ **metadata: Any,
) -> DictationEntry | None:
"""Record one dictation and prune. ``None`` when nothing was stored.
@@ -290,6 +291,18 @@ class DictationHistory:
the user lost something (``failed`` / ``cancelled`` / ``empty``) — that
row is the only place a later Restore can start from. Without it, the
worst failure this feature has is also its most invisible one.
+
+ Any surplus keyword lands in :attr:`DictationEntry.metadata` instead of
+ raising ``TypeError``. That is not laxity, it is the fix for a real
+ defect: the polish pass passed ``polish_status`` / ``polish_provider`` /
+ ``polish_latency_ms`` here, the strict signature rejected them, and the
+ caller's ``except TypeError`` fell back to writing the row WITHOUT them.
+ The result was a generative pass that could rewrite a user's words while
+ leaving no record anywhere of whether it had run, which provider
+ answered, or which guard fired — the one question a bug report about
+ wrong words has to be able to answer. ``metadata`` is deliberately kept
+ out of :meth:`DictationEntry.to_dict`: it is diagnostic detail for the
+ sidecar, not part of the API shape.
"""
from jarvis.dictation.outcomes import is_recoverable
@@ -314,6 +327,7 @@ class DictationHistory:
else count_words(text or raw_text)
),
error=(str(error) if error else None),
+ metadata={k: v for k, v in metadata.items() if v not in ("", None)},
)
try:
with self._lock:
diff --git a/jarvis/dictation/polish_guards.py b/jarvis/dictation/polish_guards.py
index ce0eff6a18..39a1a1cbc3 100644
--- a/jarvis/dictation/polish_guards.py
+++ b/jarvis/dictation/polish_guards.py
@@ -40,7 +40,7 @@ import unicodedata
from collections.abc import Sequence
from typing import Final
-from jarvis.core.turn_language import detect_text_language
+from jarvis.core.turn_language import detect_text_language, normalize_language_tag
from jarvis.dictation.cleanup import count_words
from jarvis.dictation.polish_prompt import RAW_CLOSE_DELIMITER, RAW_OPEN_DELIMITER
@@ -257,9 +257,30 @@ def _token_haystack(text: str) -> str:
def _language_key(language: object) -> str:
- """The two-letter key into :data:`_COMMON_WORDS`, or ``""`` when unusable."""
- value = str(language or "").strip().lower().replace("_", "-").split("-", 1)[0]
- return value if value in _COMMON_WORDS else ""
+ """The two-letter key into :data:`_COMMON_WORDS`, or ``""`` when unusable.
+
+ Resolved through the canonical :func:`normalize_language_tag` rather than by
+ slicing the string, because the ways this argument arrives spelled are not
+ ours to control: the cloud Whisper APIs answer with the language NAME
+ ("German", "English"), and a key derived by slicing turns that into
+ ``"german"``, misses the table, and returns ``""`` — which does not raise,
+ does not log, and silently disables the rare-token half of
+ :func:`drift_reason` on the exact dictations where the recognizer was RIGHT.
+ A guard whose failure mode is looking like it passed has to be robust to its
+ caller, so the normalisation lives here as well as at the caller.
+
+ Anything the resolver cannot place still yields ``""``: a language we hold no
+ frequency table for genuinely cannot be reasoned about, and that no-op is
+ documented in this module's header.
+ """
+ raw = str(language or "").strip()
+ if not raw:
+ return ""
+ direct = raw.lower().replace("_", "-").split("-", 1)[0]
+ if direct in _COMMON_WORDS:
+ return direct
+ code = normalize_language_tag(raw.lower())
+ return code if code in _COMMON_WORDS else ""
def rare_tokens(text: str, *, language: str) -> frozenset[str]:
diff --git a/jarvis/speech/pipeline.py b/jarvis/speech/pipeline.py
index 2dfe505cae..c8bdf621ff 100644
--- a/jarvis/speech/pipeline.py
+++ b/jarvis/speech/pipeline.py
@@ -929,6 +929,22 @@ def resolve_dictation_language(*, pinned: str, reported: str, text: str) -> str:
cleanup a documented no-op (``reason="no_rules"``), which is the honest
answer for ~95 of the 100 recognition languages this feature supports.
+ **The answer is a CODE whenever one exists.** A tag we can place comes back
+ as ``de`` / ``en`` / ``es``, never as the provider's own spelling of it.
+ This is not cosmetic. The cloud Whisper APIs answer with the language NAME
+ ("German"), every consumer downstream keys off this string, and the polish
+ pass's rare-token guard — the one that stops a formatter from replacing
+ words the user actually said — looks its vocabulary up by two-letter code
+ and silently disables itself on anything else. Returning the raw tag
+ therefore armed that guard only when the provider had been WRONG (the
+ detector then supplied a real code) and disarmed it on every dictation the
+ provider got RIGHT, which is the overwhelming majority. Live evidence:
+ "kannst du es bitte mein download video abspeichern" was delivered as
+ "Kannst du es bitte herunterladen?" — three content words replaced, no
+ guard fired. # i18n-allow: verbatim German transcript under test
+ A tag with no code stays verbatim, so the honest no-op in point 3 is
+ unchanged.
+
A provider that reports nothing at all is the one case where the text is the
only signal there is: ``[dictation].language = auto`` (the shipped default)
plus a silent provider used to leave the cleanup with "no rules for this
@@ -942,8 +958,6 @@ def resolve_dictation_language(*, pinned: str, reported: str, text: str) -> str:
if pin not in ("", "auto"):
return pin
tag = str(reported or "")
- if not text:
- return tag
try:
from jarvis.core.turn_language import (
detect_text_language,
@@ -952,6 +966,10 @@ def resolve_dictation_language(*, pinned: str, reported: str, text: str) -> str:
lowered = tag.strip().lower()
tag_code = normalize_language_tag(lowered)
+ # No text to reason from (a hangup transcribed nothing): the tag is the
+ # only signal there is, but it still leaves as a code when it has one.
+ if not text:
+ return tag_code if tag_code != "unknown" else tag
detected = detect_text_language(text)
if detected != "unknown" and (
# Nothing to contradict: no tag at all, or one that says "I could
@@ -967,6 +985,12 @@ def resolve_dictation_language(*, pinned: str, reported: str, text: str) -> str:
tag,
)
return detected
+ # The tag stands — as a CODE when it has one. Reaching this line is the
+ # NORMAL case (the provider agreed with the text), which is exactly why
+ # handing the provider's own spelling on from here disarmed the guards
+ # on nearly every dictation.
+ if tag_code != "unknown":
+ return tag_code
except Exception: # noqa: BLE001 — a detection hiccup is not fatal
log.debug("dictation language detection failed", exc_info=True)
return tag
diff --git a/jarvis/ui/web/frontend/src/i18n/locales/en.json b/jarvis/ui/web/frontend/src/i18n/locales/en.json
index 18708fbb30..7b98c67815 100644
--- a/jarvis/ui/web/frontend/src/i18n/locales/en.json
+++ b/jarvis/ui/web/frontend/src/i18n/locales/en.json
@@ -2028,6 +2028,12 @@
"autostart_label": "Start Jarvis automatically at login"
}
},
+ "language_select": {
+ "search": "Search languages",
+ "empty": "No language matches your search.",
+ "group_common": "Common languages",
+ "group_all": "All languages"
+ },
"languages_view": {
"title": "Languages",
"subtitle": "Choose the UI language and the language {name} replies in.",
diff --git a/tests/unit/speech/test_dictation_no_audio_dropped.py b/tests/unit/speech/test_dictation_no_audio_dropped.py
index b6b3207d88..8dcd3a592d 100644
--- a/tests/unit/speech/test_dictation_no_audio_dropped.py
+++ b/tests/unit/speech/test_dictation_no_audio_dropped.py
@@ -301,7 +301,11 @@ async def test_fillers_are_removed_even_when_the_provider_names_no_language(
async def test_a_reported_language_still_wins_over_the_text(monkeypatch) -> None:
"""Detection is the LAST resort, not a second opinion: a provider that did
report a language keeps it, so a short or mixed transcript can never
- reclassify a dictation the provider was sure about."""
+ reclassify a dictation the provider was sure about.
+
+ It keeps its MEANING, not its spelling. "English" wins here — and leaves as
+ ``en``, because every consumer downstream keys off this string by code.
+ """
pipe = _delivery_pipeline()
recorded: dict[str, object] = {}
@@ -318,4 +322,88 @@ async def test_a_reported_language_still_wins_over_the_text(monkeypatch) -> None
hung_up=False,
)
- assert recorded.get("language") == "English"
+ assert recorded.get("language") == "en"
+
+
+#: ``(reported tag, transcript, expected answer)``. The transcript AGREES with
+#: the tag in every code case on purpose — that agreement is the normal path,
+#: and it is the one the defect lived on. (A tag the text CONTRADICTS is covered
+#: by its own test above: there the detection wins, which already worked.)
+_LANGUAGE_TAG_CASES = [
+ # What Groq and OpenAI actually answer: the language NAME, not a code.
+ ("German", "kannst du es bitte im ordner abspeichern", "de"), # i18n-allow
+ ("english", "could you please save it in that folder", "en"),
+ ("de-DE", "kannst du es bitte im ordner abspeichern", "de"), # i18n-allow
+ ("es", "puedes guardarlo en esa carpeta por favor", "es"),
+ # No code to place it by -> verbatim, the documented no-op.
+ ("French", "peux-tu enregistrer ceci dans le dossier", "French"),
+ ("ja", "それを保存してください", "ja"),
+]
+
+
[email protected](("reported", "text", "expected"), _LANGUAGE_TAG_CASES)
+def test_a_placeable_language_leaves_as_a_code(
+ reported: str, text: str, expected: str
+) -> None:
+ """The resolver answers with a CODE whenever the tag has one.
+
+ Regression pin for the defect that made the polish pass rewrite words the
+ user had said. The cloud Whisper APIs answer with the language NAME, the
+ resolver handed that spelling on unchanged whenever the provider AGREED with
+ the text — the normal case — and the polish pass's rare-token guard looks its
+ vocabulary up by two-letter code, so it silently disabled itself on nearly
+ every dictation. The guard was therefore armed only when the recognizer had
+ been WRONG. Live cost, verbatim from the history:
+ "kannst du es bitte mein download video abspeichern" was delivered as
+ "Kannst du es bitte herunterladen?" # i18n-allow: German transcript under test
+
+ A tag with no code still passes through verbatim: ``detect_text_language``
+ knows only de/en/es, and relabelling a French dictation as one of those
+ would run the wrong language's rules over it.
+ """
+ from jarvis.speech.pipeline import resolve_dictation_language
+
+ assert (
+ resolve_dictation_language(pinned="auto", reported=reported, text=text)
+ == expected
+ )
+
+
+def test_the_rare_token_guard_survives_a_provider_spelling() -> None:
+ """The guard is robust to its caller, not just to a well-behaved one.
+
+ Belt and braces with the resolver fix above: the guard normalises the
+ language itself, so a future caller that forwards a provider's own spelling
+ cannot silently disarm it. A guard whose failure mode looks exactly like
+ passing has to defend itself.
+ """
+ from jarvis.dictation.polish_guards import drift_reason
+
+ raw = "kannst du es bitte mein download video abspeichern" # i18n-allow: under test
+ rewritten = "Kannst du es bitte herunterladen?" # i18n-allow: under test
+
+ for spelling in ("de", "German", "de-DE", "GERMAN"):
+ assert (
+ drift_reason(
+ raw,
+ rewritten,
+ language=spelling,
+ protected=(),
+ max_shrink=0.55,
+ max_growth=1.20,
+ )
+ == "lost_term"
+ ), spelling
+
+ # ...while a genuine formatting pass still gets through untouched.
+ assert (
+ drift_reason(
+ "kannst du es bitte mal in downloads ordner abspeichern", # i18n-allow
+ "Kannst du es bitte im Downloads-Ordner abspeichern.", # i18n-allow
+ language="German",
+ protected=(),
+ max_shrink=0.55,
+ max_growth=1.20,
+ )
+ == ""
+ )