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(voice): honor explicit language corrections across turns

rubenluetke10-beep82f11f777149
diff --git a/jarvis/brain/manager.py b/jarvis/brain/manager.py
index b5d18a62cc..7f2d128099 100644
--- a/jarvis/brain/manager.py
+++ b/jarvis/brain/manager.py
@@ -79,7 +79,6 @@ from jarvis.core.redact import safe_preview
from jarvis.core.turn_language import (
DEFAULT_LOCALE,
detect_text_language,
- is_substantive_turn,
resolve_output_language,
resolve_turn_language,
)
@@ -3888,9 +3887,8 @@ class BrainManager:
"""Resolve this turn's language and maintain the sticky conversation
language, applied at the top of ``generate()``.
- Stickiness: a thin interjection ("Now", "Stop") inherits the running
- ``conversation_language`` rather than flipping it; only a substantive
- turn with a clear signal (re)defines the conversation. An explicit pin
+ Stickiness and explicit corrections use the same central resolver as
+ realtime and speech output, including evidence for a switch. A pin
leaves ``_turn_detected_lang`` empty so ``_reply_language_directive``
uses the pin; genuinely ambiguous text stays ``"unknown"`` so the
directive keeps its soft "mirror the user" form.
@@ -3898,10 +3896,13 @@ class BrainManager:
if self._reply_language in _REPLY_LANG_NAMES:
self._turn_detected_lang = ""
return
- if self._conversation_language and not is_substantive_turn(user_text):
- self._turn_detected_lang = self._conversation_language
- return
- detected = detect_text_language(user_text)
+ detected = resolve_output_language(
+ self._reply_language,
+ None,
+ user_text,
+ default="unknown",
+ conversation_language=self._conversation_language,
+ )
self._turn_detected_lang = detected
if detected in _REPLY_LANG_NAMES:
self._conversation_language = detected
diff --git a/jarvis/core/turn_language.py b/jarvis/core/turn_language.py
index 3e01d685b0..a26a781529 100644
--- a/jarvis/core/turn_language.py
+++ b/jarvis/core/turn_language.py
@@ -36,6 +36,7 @@ from typing import Literal
__all__ = [
"DEFAULT_LOCALE",
"OutputLanguageValidation",
+ "detect_language_request",
"detect_text_language",
"is_substantive_turn",
"normalize_language_tag",
@@ -59,13 +60,39 @@ _REPLY_PINS: frozenset[str] = frozenset({"de", "en", "es"})
#: A turn with at most this many word tokens is a "thin" turn — a one- or
#: two-word interjection ("Now", "Stop now", "jetzt", a lone loanword). A thin
#: turn must NOT redefine an established conversation's language; it is spoken in
-#: the conversation language instead. Only a longer (substantive) turn may switch
-#: the conversation. Natural-flow forensic 2026-06-18: a single English "Now" in
+#: the conversation language instead. Explicit corrections are substantive even
+#: when short. Natural-flow forensic 2026-06-18: a single English "Now" in
#: a German voice chat flipped the whole turn to English.
_THIN_TURN_MAX_TOKENS = 2
_TOKEN_RE = re.compile(r"\b[\w']+\b", re.UNICODE)
+# Match complete language instructions, never a language name embedded in a
+# translation, quotation, negation, or task. The vocabulary is speech input.
+_LANGUAGE_REQUEST_RE = re.compile(
+ r"(?:jarvis[\s,]+)?"
+ r"(?:(?:please|bitte|por favor)[\s,]+)?" # i18n-allow
+ r"(?:(?:can|could|would) you\s+|(?:kannst|könntest) du\s+|puedes\s+)?" # i18n-allow
+ r"(?:(?:please|bitte|por favor)\s+)?" # i18n-allow
+ r"(?:(?:speak|reply|respond|answer|continue|switch)(?:\s+to me)?\s+"
+ r"|(?:sprich|spreche|antworte|antworten|rede|wechsel|wechsle)\s+" # i18n-allow
+ r"|(?:sollst|du sollst)\s+" # i18n-allow
+ r"|(?:habla|háblame|responde|contesta|cambia)\s+)?"
+ r"(?:(?:please|bitte|por favor)\s+)?" # i18n-allow
+ r"(?:(?:in|auf|en|to|zu|a)\s+)?" # i18n-allow
+ r"(?P<language>deutsch|german|alemán|aleman|english|englisch|inglés|ingles" # i18n-allow
+ r"|español|espanol|spanish|spanisch)" # i18n-allow
+ r"(?:\s+(?:antworten|sprechen))?" # i18n-allow
+ r"(?:[\s,]+(?:please|bitte|por favor|now|jetzt|ahora))?" # i18n-allow
+ r"[\s.!?]*",
+ re.IGNORECASE,
+)
+_REQUEST_LANGUAGE_CODES = {
+ "deutsch": "de", "german": "de", "alemán": "de", "aleman": "de", # i18n-allow
+ "english": "en", "englisch": "en", "inglés": "en", "ingles": "en", # i18n-allow
+ "español": "es", "espanol": "es", "spanish": "es", "spanisch": "es", # i18n-allow
+}
+
# Output validation deliberately ignores code and links. They frequently carry
# English keywords or non-Latin identifiers regardless of the language of the
# surrounding answer, so treating them as prose would create false positives.
@@ -362,15 +389,46 @@ def resolve_transcript_language(reported: object, text: str) -> str:
def is_substantive_turn(text: str) -> bool:
- """True if *text* is long enough to (re)define the conversation language.
+ """True if *text* is substantial or explicitly requests a reply language.
A one- or two-word interjection ("Now", "Stop now", "jetzt", a lone
loanword) is NOT substantive — it inherits the running conversation language
rather than switching it. Used by the conversation-stickiness logic so a
- stray English word never flips an established German chat (forensic
- 2026-06-18).
+ stray English word never flips an established German chat. A short explicit
+ language correction provides intent without needing a longer utterance.
"""
- return len(_TOKEN_RE.findall(text or "")) > _THIN_TURN_MAX_TOKENS
+ return (
+ bool(detect_language_request(text))
+ or len(_TOKEN_RE.findall(text or "")) > _THIN_TURN_MAX_TOKENS
+ )
+
+
+def detect_language_request(text: str) -> str:
+ """Return the requested reply language of a complete instruction, else "".
+
+ Full matching deliberately excludes quoted/reported speech, translation
+ content, and negative commands. A bare language name alone is ambiguous;
+ it needs a command, preposition, or politeness marker to establish intent.
+ """
+ value = unicodedata.normalize("NFC", text or "").strip()
+ match = _LANGUAGE_REQUEST_RE.fullmatch(value)
+ if match is None or len(_TOKEN_RE.findall(value)) < 2:
+ return ""
+ return _REQUEST_LANGUAGE_CODES[match.group("language").lower()]
+
+
+def _has_language_switch_evidence(text: str, detected: str) -> bool:
+ """Require independent vocabulary evidence before replacing sticky state.
+
+ A lone accented name, repeated loanword, or provider language tag is too
+ weak to redefine a conversation. Existing text detection still handles a
+ first turn, for which there is no established language to protect.
+ """
+ tokens = {token.lower() for token in _TOKEN_RE.findall(text or "")}
+ scores = {code: len(tokens & vocabulary) for code, vocabulary in _SETS}
+ score = scores.get(detected, 0)
+ runner_up = max(value for code, value in scores.items() if code != detected)
+ return score >= 2 and score - runner_up >= 2
def resolve_output_language(
@@ -394,14 +452,16 @@ def resolve_output_language(
1. an explicit ``brain.reply_language`` pin (``de``/``en``/``es``) — the
user-selected language wins over everything, including what STT heard;
- 2. else, in auto mode, conversation stickiness: a "thin" turn (a one- or
+ 2. else an explicit language instruction, even a two-word correction;
+ 3. else, in auto mode, conversation stickiness: a "thin" turn (a one- or
two-word interjection like "Now"/"Stop"/"jetzt", or a lone loanword) is
spoken in ``conversation_language`` — it must NOT flip an established
- conversation. Only a substantive turn may switch the language;
- 3. else the detected input language of the turn (``resolve_turn_language``:
+ conversation. A switch needs independent text evidence; STT tags alone
+ cannot replace an established language;
+ 4. else the detected input language of the turn (``resolve_turn_language``:
text heuristic first, STT tag breaks ties), an ambiguous substantive turn
inheriting ``conversation_language`` when one is set;
- 4. else the configured ``default`` locale (``DEFAULT_LOCALE``).
+ 5. else the configured ``default`` locale (``DEFAULT_LOCALE``).
``reply_language`` is tolerant: case/whitespace-insensitive, and any value
that is not a pin (``"auto"``, ``""``, ``None``, a typo) means "no pin —
@@ -411,8 +471,16 @@ def resolve_output_language(
pin = str(reply_language or "").strip().lower()
if pin in _REPLY_PINS:
return pin
+ requested = detect_language_request(text)
+ if requested:
+ return requested
conv = str(conversation_language or "").strip().lower()
conv = conv if conv in _REPLY_PINS else ""
if conv and len(_TOKEN_RE.findall(text or "")) <= _THIN_TURN_MAX_TOKENS:
return conv
+ if conv:
+ detected = detect_text_language(text)
+ if detected == conv or not _has_language_switch_evidence(text, detected):
+ return conv
+ return detected
return resolve_turn_language(stt_language, text, default=(conv or default))
diff --git a/jarvis/realtime/session.py b/jarvis/realtime/session.py
index 44ef01d176..96fc93c336 100644
--- a/jarvis/realtime/session.py
+++ b/jarvis/realtime/session.py
@@ -57,6 +57,7 @@ from jarvis.core.protocols import AudioChunk, BrainMessage
from jarvis.core.redact import safe_preview
from jarvis.core.tool_budget import VOICE_TOOL_BUDGET_S
from jarvis.core.turn_language import (
+ detect_language_request,
is_substantive_turn,
normalize_language_tag,
resolve_output_language,
@@ -2991,8 +2992,7 @@ class RealtimeVoiceSession:
established = bool(getattr(self, "_conversation_established", False))
# The resolver's stickiness input must be an ESTABLISHED conversation
# language, never the session's own opening default wearing that hat
- # (the input lied; the resolver itself is correct and stays untouched
- # — §1 doctrine).
+ # rather than treating the opening default as evidence from the user.
if established:
conversation = getattr(self, "_language", "")
else:
@@ -3001,6 +3001,7 @@ class RealtimeVoiceSession:
text
and not established
and 0 < voiced_ms < _CONVERSATION_LANGUAGE_MIN_VOICED_MS
+ and not detect_language_request(text)
):
# Duration gate, never spelling (AP-27 class): a sub-half-second
# first fragment carries too little audio to trust its words for
@@ -5126,11 +5127,14 @@ class RealtimeVoiceSession:
text=transcript, voiced_ms=voiced_ms
)
if not self._conversation_established and (
- is_substantive_turn(transcript)
- and (
- voiced_ms == 0
- or voiced_ms
- >= _CONVERSATION_LANGUAGE_MIN_VOICED_MS
+ detect_language_request(transcript)
+ or (
+ is_substantive_turn(transcript)
+ and (
+ voiced_ms == 0
+ or voiced_ms
+ >= _CONVERSATION_LANGUAGE_MIN_VOICED_MS
+ )
)
):
# From here on the call language sticks; a later
diff --git a/tests/unit/brain/test_conversation_language.py b/tests/unit/brain/test_conversation_language.py
index 2f12a75a3b..201219d805 100644
--- a/tests/unit/brain/test_conversation_language.py
+++ b/tests/unit/brain/test_conversation_language.py
@@ -9,6 +9,8 @@ a substantive turn switches it. An explicit reply_language pin still wins.
"""
from __future__ import annotations
+import pytest
+
from jarvis.brain.manager import BrainManager
@@ -59,3 +61,32 @@ def test_ambiguous_first_turn_keeps_soft_mirror_and_no_conversation() -> None:
def test_conversation_language_property_defaults_empty() -> None:
m = _mgr()
assert m.conversation_language == ""
+
+
+ ("previous", "correction", "expected"),
+ [
+ ("es", "auf Deutsch", "de"), # i18n-allow: incident correction
+ ("en", "sprich Deutsch", "de"), # i18n-allow
+ ("de", "in English", "en"),
+ ("en", "en español", "es"),
+ ("es", "in English", "en"),
+ ],
+)
+def test_correction_updates_brain_directive_and_following_turn(
+ previous: str, correction: str, expected: str,
+) -> None:
+ manager = _mgr(conv=previous)
+ manager._update_turn_language(correction)
+ assert manager.conversation_language == expected
+ assert manager._turn_detected_lang == expected
+ manager._update_turn_language("Okay")
+ assert manager.conversation_language == expected
+ assert manager._turn_detected_lang == expected
+
+
+def test_weak_accented_name_does_not_switch_brain_conversation() -> None:
+ manager = _mgr(conv="de")
+ manager._update_turn_language("Álvaro García Madrid")
+ assert manager.conversation_language == "de"
+ assert manager._turn_detected_lang == "de"
diff --git a/tests/unit/core/test_turn_language.py b/tests/unit/core/test_turn_language.py
index f1ee15a405..2a9e6e216e 100644
--- a/tests/unit/core/test_turn_language.py
+++ b/tests/unit/core/test_turn_language.py
@@ -19,6 +19,7 @@ import pytest
from jarvis.core.turn_language import (
DEFAULT_LOCALE,
+ detect_language_request,
detect_text_language,
normalize_language_tag,
resolve_output_language,
@@ -218,6 +219,66 @@ def test_conversation_language_used_as_default_for_ambiguous_substantive() -> No
) == "de"
[email protected]("previous", ["de", "en", "es"])
+ ("text", "expected"),
+ [
+ ("auf Deutsch", "de"), # i18n-allow: spoken correction
+ ("sprich Deutsch", "de"), # i18n-allow: spoken correction
+ ("Sprich bitte Deutsch", "de"), # i18n-allow: medial politeness
+ ("Antworte bitte auf Englisch", "en"), # i18n-allow: medial politeness
+ ("Sollst auf deutsch antworten.", "de"), # i18n-allow: incident final turn
+ ("Jarvis, bitte auf Deutsch!", "de"), # i18n-allow: spoken correction
+ ("Could you please reply in German?", "de"),
+ ("in English", "en"),
+ ("Please speak English.", "en"),
+ ("Kannst du bitte auf Englisch antworten?", "en"), # i18n-allow
+ ("en español", "es"),
+ ("Por favor, habla en español.", "es"),
+ ("Habla por favor en inglés", "en"),
+ ("Cambia a inglés", "en"),
+ ("Please switch to Spanish", "es"),
+ ],
+)
+def test_explicit_correction_overrides_stickiness_and_wrong_stt(
+ previous: str, text: str, expected: str,
+) -> None:
+ wrong_tag = "es" if expected != "es" else "de"
+ assert resolve_output_language(
+ "auto", wrong_tag, text, conversation_language=previous,
+ ) == expected
+
+
+ "text",
+ [
+ "Do not speak Spanish", "Don't reply in German", "No hables en inglés",
+ "Sprich nicht Deutsch", # i18n-allow: negative instruction
+ 'Translate "in English" into Spanish', 'He said "auf Deutsch".', # i18n-allow
+ '"in English"', "`en español`", "What does speak English mean?",
+ "Write a poem in Spanish", "English", "German shepherd",
+ "Please explain how to speak German",
+ ],
+)
+def test_language_mentions_are_not_explicit_corrections(text: str) -> None:
+ assert detect_language_request(text) == ""
+
+
[email protected]("language", ["de", "en", "es"])
[email protected]("text", ["Spotify Netflix Berlin", "Álvaro García Madrid", "now now now"])
+def test_weak_evidence_cannot_flip_established_language(language: str, text: str) -> None:
+ for tag in ("de", "en", "es"):
+ assert resolve_output_language(
+ "auto", tag, text, conversation_language=language,
+ ) == language
+
+
+def test_language_correction_respects_explicit_ui_pin() -> None:
+ assert resolve_output_language(
+ "en", "es", "auf Deutsch", conversation_language="es", # i18n-allow
+ ) == "en"
+
+
# ---------------------------------------------------------------------------
# resolve_transcript_language — whose word list may DELETE tokens (2026-07-30)
# ---------------------------------------------------------------------------
diff --git a/tests/unit/realtime/test_language_correction.py b/tests/unit/realtime/test_language_correction.py
new file mode 100644
index 0000000000..d0c46cf15b
--- /dev/null
+++ b/tests/unit/realtime/test_language_correction.py
@@ -0,0 +1,145 @@
+"""RUB-19: short corrections, sticky follow-ups, and language-safe voice output."""
+
+import asyncio
+
+import pytest
+
+from jarvis.realtime.protocol import RealtimeEvent
+from jarvis.realtime.session import RealtimeVoiceSession
+from tests.unit.realtime.test_session import FakeProvider, FakeSession, _cfg
+
+
+ ("initial", "correction", "expected", "name"),
+ [
+ ("es", "auf Deutsch", "de", "German"), # i18n-allow: incident correction
+ ("en", "sprich Deutsch", "de", "German"), # i18n-allow
+ ("de", "in English", "en", "English"),
+ ("en", "en español", "es", "Spanish"),
+ ("es", "in English", "en", "English"),
+ ],
+)
+async def test_short_correction_updates_provider_voice_and_stays_for_followup(
+ initial: str, correction: str, expected: str, name: str,
+) -> None:
+ # A short first correction must establish the conversation, even when the
+ # measured audio duration is below the usual first-fragment threshold.
+ provider = FakeProvider([
+ RealtimeEvent(type="input_transcript", text=correction, is_final=True, voiced_ms=200),
+ RealtimeEvent(type="turn_complete"),
+ RealtimeEvent(type="input_transcript", text="Okay", is_final=True),
+ RealtimeEvent(type="turn_complete"),
+ ])
+ session = RealtimeVoiceSession(
+ session_id="language-correction",
+ send_binary=lambda _data: asyncio.sleep(0),
+ send_json=lambda _message: asyncio.sleep(0),
+ provider=provider,
+ config=_cfg(reply_language="auto", stt_language=initial),
+ bus=None,
+ )
+ await session.handle_control({"type": "audio_start", "sample_rate": 16_000})
+ await session.wait_finished()
+ await session.end(reason="test")
+
+ assert session._conversation_established
+ assert session._language == expected
+ assert len(provider.session.session_updates) >= 2
+ for update in provider.session.session_updates[-2:]:
+ assert update["language"] == expected # Provider TTS voice language.
+ assert f"Reply only in {name} for this turn" in update["instructions"]
+ assert session._output_language_failure_phrase() == (
+ session._output_language_failure_phrase(expected)
+ )
+
+
+async def test_incident_turns_five_to_seven_do_not_trap_conversation_in_spanish() -> None:
+ # Establish German, then repeat the incident's weak Spanish evidence,
+ # two-word correction, and longer correction under a conflicting STT tag.
+ provider = FakeProvider([
+ event
+ for transcript in (
+ "Wie ist das Wetter heute?", # i18n-allow
+ "Álvaro García Madrid",
+ "auf Deutsch", # i18n-allow
+ "Sollst auf deutsch antworten.", # i18n-allow
+ )
+ for event in (
+ RealtimeEvent(type="input_transcript", text=transcript, is_final=True),
+ RealtimeEvent(type="turn_complete"),
+ )
+ ])
+ session = RealtimeVoiceSession(
+ session_id="incident-language-sequence",
+ send_binary=lambda _data: asyncio.sleep(0),
+ send_json=lambda _message: asyncio.sleep(0),
+ provider=provider,
+ config=_cfg(reply_language="auto", stt_language="es"),
+ bus=None,
+ )
+ await session.handle_control({"type": "audio_start", "sample_rate": 16_000})
+ await session.wait_finished()
+ await session.end(reason="test")
+
+ assert len(provider.session.session_updates) == 4
+ assert all(update["language"] == "de" for update in provider.session.session_updates)
+ assert session._language == "de"
+
+
+ ("correction", "expected", "name"),
+ [
+ ("auf Deutsch", "de", "German"), # i18n-allow: incident correction
+ ("in English", "en", "English"),
+ ("en español", "es", "Spanish"),
+ ],
+)
+async def test_mismatched_output_retries_and_speaks_fallback_in_corrected_language(
+ correction: str, expected: str, name: str,
+) -> None:
+ wrong_text = "这是一个完整的中文回答,包含足够多的文字来可靠地识别语言。"
+
+ class MismatchingSession(FakeSession):
+ supports_prompted_response_retry = True
+ renders_surface_fallback = True
+
+ async def receive(self):
+ yield RealtimeEvent(type="input_transcript", text=correction, is_final=True)
+ for attempt in range(2):
+ yield RealtimeEvent(type="output_transcript_delta", text=wrong_text)
+ for _ in range(100):
+ if len(self.text_inputs) > attempt:
+ break
+ await asyncio.sleep(0.01)
+ yield RealtimeEvent(type="turn_complete")
+
+ class MismatchingProvider(FakeProvider):
+ async def open_session(self, cfg):
+ self.opened_with = cfg
+ self.session = MismatchingSession([])
+ return self.session
+
+ provider = MismatchingProvider([])
+ session = RealtimeVoiceSession(
+ session_id="corrected-fallback-language",
+ send_binary=lambda _data: asyncio.sleep(0),
+ send_json=lambda _message: asyncio.sleep(0),
+ provider=provider,
+ config=_cfg(reply_language="auto", stt_language="es" if expected != "es" else "de"),
+ bus=None,
+ )
+ # Reproduce a conversation that already got stuck in the wrong language.
+ session._conversation_established = True
+ await session.handle_control({"type": "audio_start", "sample_rate": 16_000})
+ await session.wait_finished()
+ await session.end(reason="test")
+
+ assert session._language == expected
+ assert session._output_language_failures == 1
+ assert len(provider.session.text_inputs) == 2
+ assert name in provider.session.text_inputs[0]
+ assert session._output_language_failure_phrase(expected) in provider.session.text_inputs[1]
+ assert provider.session.session_updates[-1]["language"] == expected