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): expire a direct-speech clearance once its audio must be over

rubenluetke10-beeped91fa3fc49c
diff --git a/jarvis/realtime/scrub_gate.py b/jarvis/realtime/scrub_gate.py
index d30d091fee..668e6dc312 100644
--- a/jarvis/realtime/scrub_gate.py
+++ b/jarvis/realtime/scrub_gate.py
@@ -72,6 +72,15 @@ _COVERAGE_MS_PER_CHAR = 55.0
# A finalize() tail this far beyond the coverage estimate cannot be explained
# by the deliberate underestimation alone; log it as a transcription stall.
_FINALIZE_EXCESS_LOG_MS = 5_000.0
+# Release budget for ONE injected direct-speech utterance — a generous
+# OVERestimate of its rendering time (the opposite direction of
+# _COVERAGE_MS_PER_CHAR's deliberate underestimate). Clamping the trusted
+# answer itself would recreate "the action ran and the user heard nothing",
+# so the bound exists only to stop the clearance from covering an UNBOUNDED
+# amount of foreign audio on a transport that generates its own responses:
+# without it, one injection opened the gate for the rest of the call.
+_DIRECT_SPEECH_MS_PER_CHAR = 150.0
+_DIRECT_SPEECH_BUDGET_FLOOR_MS = 10_000.0
class ScrubHoldGate:
@@ -116,6 +125,30 @@ class ScrubHoldGate:
# own audio has actually played.
self._direct_speech_pending = False
self._direct_speech_released_ms = 0.0
+ self._direct_speech_budget_ms = 0.0
+
+ def _expire_direct_speech_budget(self) -> None:
+ """Retire a direct-speech clearance whose audio must be over by now.
+
+ The injected utterance renders once; audio released far beyond its
+ generous rendering budget belongs to some OTHER response and goes back
+ to fail-closed vetting. When real vetted transcript chars exist, the
+ ordinary coverage machinery keeps its own verdict.
+ """
+ if not self._direct_speech_pending:
+ return
+ if self._direct_speech_released_ms <= self._direct_speech_budget_ms:
+ return
+ self._direct_speech_pending = False
+ if self._covered_chars <= 0:
+ self._coverage_active = False
+ self._cleared = False
+ log.info(
+ "scrub gate expired a direct-speech clearance after %d ms of "
+ "released audio (budget %d ms); later audio is vetted again",
+ int(self._direct_speech_released_ms),
+ int(self._direct_speech_budget_ms),
+ )
@property
def pending_audio_ms(self) -> float:
@@ -239,6 +272,7 @@ class ScrubHoldGate:
"""
if self._hard_leak:
return []
+ self._expire_direct_speech_budget()
if not self._cleared and not self._coverage_active:
if not self._pending and self._pending_since is None:
self._pending_since = time.monotonic()
@@ -258,6 +292,7 @@ class ScrubHoldGate:
def release_available(self) -> list[AudioChunk]:
"""Release buffered audio only after a transcript cleared the gate."""
+ self._expire_direct_speech_budget()
if self._hard_leak or not self._cleared:
return []
# Some providers send the transcript delta just before its matching
@@ -305,6 +340,12 @@ class ScrubHoldGate:
self._cleared = True
self._direct_speech_pending = True
self._direct_speech_released_ms = 0.0
+ # Bounded, not sticky: the clearance covers THIS utterance's rendering
+ # (generously overestimated), never the rest of the call.
+ self._direct_speech_budget_ms = (
+ _DIRECT_SPEECH_BUDGET_FLOOR_MS
+ + len(str(text or "")) * _DIRECT_SPEECH_MS_PER_CHAR
+ )
if text:
log.debug(
"scrub gate trusts %d chars of injected direct speech",
@@ -350,6 +391,7 @@ class ScrubHoldGate:
beyond the estimate means transcription lagged or died mid-turn;
log it so the next incident names this producer (BUG-069 review).
"""
+ self._expire_direct_speech_budget()
if self.fail_closed() or self._hard_leak:
return []
out = self._pending
diff --git a/tests/unit/realtime/test_scrub_gate.py b/tests/unit/realtime/test_scrub_gate.py
index c4a743ad0b..3aa4210535 100644
--- a/tests/unit/realtime/test_scrub_gate.py
+++ b/tests/unit/realtime/test_scrub_gate.py
@@ -534,3 +534,37 @@ async def test_drain_never_carries_a_hard_leak_forward():
assert gate.hard_leak_pending() is False
assert await gate.push_audio(_chunk(4)) == []
+
+
+async def test_direct_speech_clearance_expires_after_its_rendering_budget():
+ """One injection must not open the gate for the rest of the call.
+
+ On a transport that generates its own responses, a sticky clearance let
+ FOREIGN audio ride the trusted injection unvetted (live 2026-08-04: an
+ unrelated English fragment played instead of the delegated answer).
+ """
+ gate = ScrubHoldGate(language="en")
+ gate.trust_direct_speech("Short answer.")
+ five_seconds = AudioChunk(
+ pcm=b"\x00\x01" * (24_000 * 5), sample_rate=24_000, timestamp_ns=0
+ )
+ assert await gate.push_audio(five_seconds)
+ assert await gate.push_audio(five_seconds)
+ assert await gate.push_audio(five_seconds)
+ # The utterance's generous rendering budget is spent: whatever streams
+ # now belongs to another response and is vetted fail-closed again.
+ assert await gate.push_audio(five_seconds) == []
+
+
+async def test_direct_speech_budget_never_clips_a_long_trusted_answer():
+ """The bound must be an overestimate — clamping the trusted answer itself
+ would recreate 'the action ran and the user heard nothing'."""
+ gate = ScrubHoldGate(language="en")
+ gate.trust_direct_speech("word " * 200)
+ one_minute = AudioChunk(
+ pcm=b"\x00\x01" * (24_000 * 60), sample_rate=24_000, timestamp_ns=0
+ )
+ assert await gate.push_audio(one_minute)
+ assert await gate.push_audio(one_minute)