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.
Python agent-orchestrationai-agentai-coding-assistantclaude-codecoding-agentcomputer-usedesktop-applocal-firstmcppersonal-jarvisvoice-assistantvoice-coding
feat(realtime): audio-hold voice-scrub gate (load-bearing safety, AP-11)
Ruben Lütkeb9de002ad158
diff --git a/jarvis/realtime/scrub_gate.py b/jarvis/realtime/scrub_gate.pynew file mode 100644index 0000000000..6bdd441ab4--- /dev/null+++ b/jarvis/realtime/scrub_gate.py@@ -0,0 +1,81 @@+"""Audio-hold voice-scrub gate for realtime duplex mode (AP-11 / ADR-0010).++A duplex model speaks audio natively and its transcript is co-timed but NOT+guaranteed to arrive before the matching audio is audible. So we buffer each+decoded audio delta and release it only once its transcript region has passed+``scrub_for_voice``. A hard leak (stacktrace / raw repr / shell command) drops+the buffered audio and signals the session to cancel + speak the fallback.+Regex-only, no LLM (AP-11).+"""++from __future__ import annotations++from jarvis.brain.output_filter import FALLBACK_PHRASES, scrub_for_voice+from jarvis.core.protocols import AudioChunk++_HARD_LEAK_ACTIONS = frozenset(+ {+ "replaced_stacktrace",+ "replaced_raw_repr",+ "replaced_shell_command",+ "replaced_with_fallback_residue",+ }+)+++class ScrubHoldGate:+ """Hold audio until its transcript is scrub-cleared; drop on a hard leak."""++ def __init__(self, language: str, *, lookahead_ms: int = 250) -> None:+ self._language = language if language in FALLBACK_PHRASES else "en"+ self._lookahead_ms = lookahead_ms+ self._pending: list[AudioChunk] = []+ self._cleared = False+ self._hard_leak = False++ def hard_leak_pending(self) -> bool:+ return self._hard_leak++ def fallback_phrase(self) -> str:+ return FALLBACK_PHRASES.get(self._language, FALLBACK_PHRASES["en"])++ async def feed_transcript(self, text: str) -> str:+ """Scrub a transcript boundary. Returns display-safe text.++ Sets the clear flag (audio may flow) on clean text; sets the hard-leak+ flag (audio dropped) on a hard leak.+ """+ result = scrub_for_voice(text, language=self._language)+ if result.fallback_used or (_HARD_LEAK_ACTIONS & set(result.actions)):+ self._hard_leak = True+ self._cleared = False+ self._pending.clear()+ return result.cleaned # the canned fallback phrase+ self._cleared = True+ return result.cleaned++ async def push_audio(self, chunk: AudioChunk) -> list[AudioChunk]:+ """Buffer or release an audio delta. Returns chunks safe to play now."""+ if self._hard_leak:+ return []+ if self._cleared:+ out = self._pending + [chunk]+ self._pending = []+ return out+ self._pending.append(chunk)+ return []++ def release_available(self) -> list[AudioChunk]:+ """Availability cap: release whatever is buffered (no transcript came)."""+ if self._hard_leak:+ return []+ out = self._pending+ self._pending = []+ self._cleared = True+ return out++ def drain(self) -> None:+ """Barge-in / turn-end: discard buffered audio and reset per-turn state."""+ self._pending.clear()+ self._cleared = False+ self._hard_leak = Falsediff --git a/tests/unit/realtime/test_scrub_gate.py b/tests/unit/realtime/test_scrub_gate.pynew file mode 100644index 0000000000..943ef5b661--- /dev/null+++ b/tests/unit/realtime/test_scrub_gate.py@@ -0,0 +1,53 @@+import pytest++from jarvis.core.protocols import AudioChunk+from jarvis.realtime.scrub_gate import ScrubHoldGate++_HARD_LEAK_ACTIONS = {"replaced_stacktrace", "replaced_raw_repr", "replaced_shell_command"}+++def _chunk(n: int) -> AudioChunk:+ return AudioChunk(pcm=b"\x00\x01" * n, sample_rate=24000, timestamp_ns=0)+++async def test_clean_transcript_releases_buffered_audio():+ gate = ScrubHoldGate(language="en")+ await gate.push_audio(_chunk(4))+ released = await gate.feed_transcript("Hello there, how can I help?")+ # display text returned; audio releasable after the clean boundary+ assert released == "Hello there, how can I help?" or released # scrubbed display+ out = await gate.push_audio(_chunk(4))+ assert gate.hard_leak_pending() is False+ assert out # buffered + new audio flows once transcript cleared+++async def test_hard_leak_transcript_marks_leak_and_drops_audio():+ gate = ScrubHoldGate(language="en")+ await gate.push_audio(_chunk(4))+ # A stacktrace transcript is a hard leak (scrub_for_voice early-returns fallback).+ await gate.feed_transcript("Traceback (most recent call last):\n File x\nValueError: y\n\n")+ assert gate.hard_leak_pending() is True+ # No audio may be released after a hard leak.+ out = await gate.push_audio(_chunk(4))+ assert out == []+ assert gate.fallback_phrase() == "An error occurred."+++async def test_scrub_is_regex_only_no_llm(monkeypatch):+ # Guard AP-11: the gate must call scrub_for_voice and nothing that awaits a model.+ import jarvis.realtime.scrub_gate as mod++ calls = {"n": 0}+ real = mod.scrub_for_voice++ def spy(text, **kw):+ calls["n"] += 1+ return real(text, **kw)++ monkeypatch.setattr(mod, "scrub_for_voice", spy)+ gate = ScrubHoldGate(language="en")+ await gate.feed_transcript("A normal sentence.")+ assert calls["n"] >= 1