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(realtime): preserve novel follow-up turns

Ruben Lütke71b0c692448c
diff --git a/jarvis/realtime/session.py b/jarvis/realtime/session.py
index a3087236ec..de49f43c2b 100644
--- a/jarvis/realtime/session.py
+++ b/jarvis/realtime/session.py
@@ -1758,13 +1758,20 @@ class RealtimeVoiceSession:
interrupt_provider=event.type == "speech_started"
)
elif event.type == "tool_call":
- await self._ensure_turn_started()
if str(getattr(event, "tool_name", "") or "") == "end_call":
+ await self._ensure_turn_started()
# Session lifecycle, not a bridge tool: works without
# a tool bridge and must not be held back by the
# missing-transcript guard below.
await self._handle_end_call(event)
elif not self._last_user_text:
+ # Providers may emit a speculative tool call before the
+ # input transcript carried by the same response. Buffer
+ # it without opening a persisted turn: a later genuine
+ # transcript opens the turn and releases the call, while
+ # a self-echo transcript is dropped and the call is
+ # rejected at the boundary. Opening here produced the
+ # contentless 5.7-second turn in the 2026-07-19 Mac run.
self._pending_tool_events.append(event)
if self._tool_transcript_task is None:
self._tool_transcript_task = asyncio.create_task(
@@ -1772,6 +1779,7 @@ class RealtimeVoiceSession:
name=f"rt-tool-transcript-{self.session_id}",
)
else:
+ await self._ensure_turn_started()
await self._handle_tool_call(event)
elif event.type == "turn_complete":
if self._pending_tool_events:
diff --git a/jarvis/speech/echo_guard.py b/jarvis/speech/echo_guard.py
index 4e71d54d45..54b05b701b 100644
--- a/jarvis/speech/echo_guard.py
+++ b/jarvis/speech/echo_guard.py
@@ -8,7 +8,7 @@ speaker echo that slipped every acoustic gate, never a turn to answer.
Without this, ONE missed false barge loops forever: reply → echo transcribed
→ brain answers itself → new reply → new echo (the Mac test machine's
multi-turn self-conversation, 2026-07-18). Conservative by design:
-near-total containment in the assistant's own recent words is required, so a
+total fuzzy containment in the assistant's own recent words is required, so a
genuine user answer that ADDS anything is always kept (fail-open).
The logic originated as pipeline-private methods (BUG-084); it lives here so
@@ -34,7 +34,6 @@ class SelfEchoGuard:
WINDOW_S = 6.0
REF_TTL_S = 30.0
MIN_TOKENS = 3
- MIN_OVERLAP = 0.8
# 0.8, not lower: at 0.75 near-misses like "gut"→"guten" (ratio exactly
# 0.75) count as contained and a genuine short user answer built from the
# assistant's own words plus one inflected token would be eaten. Real STT
@@ -93,11 +92,13 @@ class SelfEchoGuard:
outside that window a user may echo the assistant verbatim all they
want. Tokens match fuzzily (``difflib``-ratio ≥ ``FUZZY_CUTOFF``)
because STT garbles echo (see the ratio anchors at ``FUZZY_CUTOFF``
- above). Utterances shorter than ``MIN_TOKENS`` are never judged —
- short commands ("stopp") must always reach their handlers. References
- are checked per spoken phrase plus the concatenation of the two newest
+ above), but every utterance token must still match. One genuinely novel
+ token can change the meaning of a short follow-up and therefore fails
+ open. Utterances shorter than ``MIN_TOKENS`` are never judged — short
+ commands ("stopp") must always reach their handlers. References are
+ checked per spoken phrase plus the concatenation of the two newest
phrases, so an echo spanning a sentence boundary still matches without
- building a big vocabulary union that common words could false-match.
+ building a large session-wide vocabulary union.
"""
if self.activity_ns <= 0:
return False
@@ -114,17 +115,16 @@ class SelfEchoGuard:
candidates = list(fresh)
if len(fresh) >= 2:
candidates.append(fresh[-2] + fresh[-1])
- allowed_novel = len(utterance) // 6
for reference in candidates:
- matched = sum(
- 1
+ if all(
+ token in reference
+ or difflib.get_close_matches(
+ token,
+ reference,
+ n=1,
+ cutoff=self.FUZZY_CUTOFF,
+ )
for token in utterance
- if token in reference
- or difflib.get_close_matches(token, reference, n=1, cutoff=self.FUZZY_CUTOFF)
- )
- if (
- matched / len(utterance) >= self.MIN_OVERLAP
- and (len(utterance) - matched) <= allowed_novel
):
return True
return False
diff --git a/tests/unit/realtime/test_session_self_echo.py b/tests/unit/realtime/test_session_self_echo.py
index 037d4ec7ae..9d7c9fa50e 100644
--- a/tests/unit/realtime/test_session_self_echo.py
+++ b/tests/unit/realtime/test_session_self_echo.py
@@ -147,7 +147,7 @@ async def test_canned_phrase_echo_never_becomes_a_turn():
type="input_transcript",
# Garbled echo of the canned provider-down apology — exactly
# the Mac loop's fuel.
- text="mein Sprachmodell ist im Moment nicht erreichbar", # i18n-allow: garbled echo under test
+ text="mein Sprachmodell ist im Moment nicht erreichbar", # i18n-allow: STT
is_final=True,
),
RealtimeEvent(type="turn_complete"),
@@ -174,7 +174,7 @@ async def test_provider_output_transcript_echo_is_dropped():
[
RealtimeEvent(
type="output_transcript_delta",
- text="Moin, bei mir ist alles bestens und ich bin bereit.", # i18n-allow: voice fixture
+ text="Moin, bei mir ist alles bestens und ich bin bereit.", # i18n-allow: voice
),
RealtimeEvent(
type="audio_delta",
@@ -183,7 +183,7 @@ async def test_provider_output_transcript_echo_is_dropped():
RealtimeEvent(type="turn_complete"),
RealtimeEvent(
type="input_transcript",
- text="bei mir ist alles bestens ich bin bereit", # i18n-allow: garbled echo under test
+ text="bei mir ist alles bestens ich bin bereit", # i18n-allow: STT
is_final=True,
),
]
@@ -209,7 +209,7 @@ async def test_genuine_user_turn_with_novel_content_passes():
fake, jsons = await _run(
provider,
arm=lambda sess: sess._surface_speech_message(
- "Tut mir leid, mein Sprachmodell ist im Moment nicht erreichbar." # i18n-allow: voice fixture
+ "Tut mir leid, mein Sprachmodell ist im Moment nicht erreichbar." # i18n-allow: voice
),
)
assert fake.response_requests == 1
@@ -245,7 +245,7 @@ async def test_echo_drop_interrupts_auto_response_provider():
[
RealtimeEvent(
type="input_transcript",
- text="mein Sprachmodell ist im Moment nicht erreichbar", # i18n-allow: garbled echo under test
+ text="mein Sprachmodell ist im Moment nicht erreichbar", # i18n-allow: STT
is_final=True,
),
]
@@ -260,7 +260,7 @@ async def test_echo_drop_interrupts_auto_response_provider():
bus=None,
)
sess._surface_speech_message(
- "Tut mir leid, mein Sprachmodell ist im Moment nicht erreichbar." # i18n-allow: voice fixture
+ "Tut mir leid, mein Sprachmodell ist im Moment nicht erreichbar." # i18n-allow: voice
)
await sess.handle_control({"type": "audio_start", "sample_rate": 16000})
await asyncio.sleep(0.05)
@@ -270,6 +270,62 @@ async def test_echo_drop_interrupts_auto_response_provider():
assert dropped_flag is True
+async def test_echo_followed_by_tool_call_never_opens_empty_turn():
+ """A speculative call for dropped echo stays outside persisted turns."""
+ provider = AutoResponseFakeProvider(
+ [
+ RealtimeEvent(
+ type="input_transcript",
+ text="an einem bestimmten Projekt arbeiten", # i18n-allow: speaker echo fixture
+ is_final=True,
+ ),
+ RealtimeEvent(
+ type="tool_call",
+ call_id="echo-call",
+ tool_name="jarvis_action",
+ tool_args={},
+ ),
+ RealtimeEvent(type="turn_complete"),
+ ]
+ )
+ jsons: list[dict[str, object]] = []
+ sess = RealtimeVoiceSession(
+ session_id="echo-tool",
+ send_binary=lambda _data: asyncio.sleep(0),
+ send_json=lambda message: jsons.append(message) or asyncio.sleep(0),
+ provider=provider,
+ config=_cfg(),
+ bus=None,
+ )
+ sess._surface_speech_message(
+ "Möchtest du an einem bestimmten Projekt arbeiten?" # i18n-allow: voice fixture
+ )
+
+ await sess.handle_control({"type": "audio_start", "sample_rate": 16_000})
+ await asyncio.sleep(0.05)
+ opened_turns = sess._turn_index
+ await sess.end(reason="test")
+
+ assert opened_turns == 0
+ assert provider.session.interrupts == 1
+ assert provider.session.tool_results == [
+ (
+ "echo-call",
+ "jarvis_action",
+ {
+ "success": False,
+ "error": (
+ "The input transcript was unavailable, so the action was not "
+ "executed. Ask the user to repeat the request."
+ ),
+ },
+ )
+ ]
+ assert _user_transcripts(jsons) == []
+ assert not [message for message in jsons if message.get("type") == "error_spoken"]
+
+
@pytest.mark.asyncio
async def test_playback_horizon_future_dates_the_guard():
"""Long provider audio arms the guard past the plain wall-clock window."""
diff --git a/tests/unit/speech/test_echo_guard.py b/tests/unit/speech/test_echo_guard.py
index 14d1553f9a..c5d408de8e 100644
--- a/tests/unit/speech/test_echo_guard.py
+++ b/tests/unit/speech/test_echo_guard.py
@@ -29,6 +29,24 @@ def test_user_answer_with_novel_word_is_kept() -> None:
assert guard.is_echo("bei mir läuft alles gut") is False # i18n-allow
+def test_live_follow_up_with_one_novel_word_is_kept() -> None:
+ """A semantic pivot must survive even when five surrounding words repeat."""
+ guard = SelfEchoGuard()
+ guard.register(
+ "Wenn du mich fragst, ist das eine gute Wahl, wie ich finde." # i18n-allow: voice fixture
+ )
+ guard.register(
+ "Möchtest du an einem bestimmten Projekt arbeiten?" # i18n-allow: voice fixture
+ )
+
+ assert (
+ guard.is_echo(
+ "Wie willst du an Projekten arbeiten?" # i18n-allow: captured user follow-up
+ )
+ is False
+ )
+
+
def test_slot_reregistration_replaces_instead_of_appending() -> None:
guard = SelfEchoGuard()
guard.register("Ich bin bereit", slot="turn:1") # i18n-allow: voice fixture