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
fix(voice): eliminate wake-to-listen capture gap
Ruben Lütke1424116a963e
diff --git a/jarvis/speech/pipeline.py b/jarvis/speech/pipeline.pyindex 9b4c05472a..042b03591c 100644--- a/jarvis/speech/pipeline.py+++ b/jarvis/speech/pipeline.py@@ -1,17 +1,13 @@-"""Speech-Pipeline mit Call/Hangup-State-Machine + Parallel-Wake-Detection.+"""Speech pipeline with call/hangup state and parallel wake detection.-Wake-Detection läuft im IDLE-State über ZWEI parallele Pfade auf demselben-Mic-Stream (Fanout):- 1. openWakeWord — schnell (15-30 ms Latenz), fragil bei deutscher Aussprache- 2. Whisper-Wake — robust (800-1200 ms Latenz), versteht Deutsch nativ+Wake detection runs in IDLE through two paths sharing one microphone fanout:-Acknowledgment-Feedback:- - Sofort beim Wake/Call: kurzer Chime (180 ms, in-memory generiert)- - Dann pre-renderter "Ja?"-Ton (beim Startup einmal via Gemini-TTS)- - Dann ACTIVE-State+1. openWakeWord -- fast (15-30 ms), but less robust across pronunciations.+2. Whisper wake -- robust (800-1200 ms) and natively multilingual.-Hotkeys (call / hangup) sind parallel zum Wake IMMER aktiv — global-hotkeys-registriert Windows-weite Low-Level-Hooks.+Activation feedback is visual while capture is live, avoiding both an input+dead zone and speaker echo in the recorded utterance. Global call/hangup+hotkeys remain active alongside wake detection."""from __future__ import annotations@@ -26,6 +22,7 @@ import threadingimport timefrom collections import dequefrom collections.abc import AsyncIterator, Awaitable, Callable+from contextlib import asynccontextmanagerfrom pathlib import Pathfrom typing import TYPE_CHECKING, Anyfrom uuid import uuid4@@ -51,11 +48,12 @@ from jarvis.core.events import (AudioOutFirst,BrainTTFT,DictationTranscript,+ JarvisAgentAnnouncement,+ JarvisAgentBackgroundCompleted,LatencyTurnComplete,ListeningStarted,+ MessageSent,ObservationCaptured,- JarvisAgentAnnouncement,- JarvisAgentBackgroundCompleted,SpeechSpoken,TranscriptFinal,TranscriptionUpdate,@@ -77,7 +75,6 @@ from jarvis.core.turn_language import (from jarvis.plugins.stt.fwhisper import FasterWhisperProviderfrom jarvis.plugins.tts.gemini_flash_tts import GEMINI_TTS_SAMPLE_RATE, GeminiFlashTTSfrom jarvis.plugins.wake.openwakeword_provider import (- PRODUCTION_WAKE_THRESHOLD,OpenWakeWordProvider,)from jarvis.sessions.constants import (@@ -560,6 +557,160 @@ def _stt_retry_delay(exc: BaseException | None, attempt: int) -> float:return min(_STT_RETRY_CAP_S, _STT_RETRY_BASE_S * (2 ** attempt))+_SESSION_START_BUFFER_MAX_BYTES = 16_000 * 2 * 30 # 30 s of mono PCM16+++def _feed_live_mic_level(chunk: AudioChunk) -> None:+ """Publish one captured frame's RMS to the native overlay level channel."""+ if not mic_level.has_subscribers():+ return+ samples = pcm_bytes_to_np(chunk.pcm)+ if samples.size:+ mic_level.feed(float(np.sqrt(np.mean(np.square(samples)))))+++class _SessionInputBuffer:+ """Replayable bounded handoff for one continuously captured mic stream.++ Capture begins before ``VoiceSessionStarted`` is published, so the Jarvis+ Bar never advertises LISTENING ahead of the microphone. Frames arriving+ while start subscribers, realtime session assembly, or a provider handshake+ run are retained in order. Each new consumer starts+ at sequence zero, allowing classic STT to replay the opening if a realtime+ provider fails before accepting it. The byte cap is time-format based; if a+ consumer ever falls behind it, the stream fails explicitly instead of+ silently dropping the beginning of the user's command.+ """++ def __init__(+ self,+ *,+ initial: tuple[AudioChunk, ...] = (),+ max_buffer_bytes: int = _SESSION_START_BUFFER_MAX_BYTES,+ ) -> None:+ self._chunks: deque[tuple[int, AudioChunk]] = deque()+ self._max_buffer_bytes = max(1, int(max_buffer_bytes))+ self._retained_bytes = 0+ self._next_seq = 0+ self._updated = asyncio.Event()+ self._pump_task: asyncio.Task[None] | None = None+ self._error: BaseException | None = None+ self._source_done = False+ self._closed = False+ self._active_consumers = 0+ self.released = asyncio.Event()+ for chunk in initial:+ self._append(chunk, publish_level=False)++ def start(self, source: AsyncIterator[AudioChunk]) -> None:+ if self._pump_task is not None:+ return+ self._pump_task = asyncio.create_task(+ self._pump(source), name="voice-session-mic-buffer"+ )++ def put(self, chunk: AudioChunk) -> None:+ if not self._closed:+ # While no voice engine consumes the stream, this buffer is the+ # only place that can animate the visible startup bar. An active+ # classic/realtime consumer owns level feeding; switching back here+ # between consumers keeps fallback teardown from flattening the bar.+ self._append(chunk, publish_level=self._active_consumers == 0)++ def _append(self, chunk: AudioChunk, *, publish_level: bool) -> None:+ chunk_bytes = len(chunk.pcm)+ while (+ self._chunks+ and self._retained_bytes + chunk_bytes > self._max_buffer_bytes+ ):+ _seq, dropped = self._chunks.popleft()+ self._retained_bytes = max(0, self._retained_bytes - len(dropped.pcm))+ self._chunks.append((self._next_seq, chunk))+ self._retained_bytes += chunk_bytes+ self._next_seq += 1+ if publish_level:+ _feed_live_mic_level(chunk)+ self._updated.set()++ async def _pump(self, source: AsyncIterator[AudioChunk]) -> None:+ try:+ async for chunk in source:+ self.put(chunk)+ except asyncio.CancelledError:+ raise+ except BaseException as exc: # noqa: BLE001 - replay on the consumer+ self.finish(error=exc)+ finally:+ self.finish()++ def finish(self, *, error: BaseException | None = None) -> None:+ """Mark a manually fed source as terminal and wake its consumers."""+ if error is not None and self._error is None:+ self._error = error+ self._source_done = True+ self._updated.set()++ async def stream(self) -> AsyncIterator[AudioChunk]:+ self._active_consumers += 1+ cursor = 0+ try:+ while True:+ if self._chunks:+ earliest = self._chunks[0][0]+ if cursor < earliest:+ raise RuntimeError(+ "Voice input exceeded the 30-second replay window; "+ "refusing to drop the command prefix."+ )+ offset = cursor - earliest+ if 0 <= offset < len(self._chunks):+ _seq, chunk = self._chunks[offset]+ cursor += 1+ yield chunk+ continue+ if self._closed or self._source_done:+ if self._error is not None:+ raise self._error+ return+ self._updated.clear()+ if self._chunks and cursor < self._next_seq:+ continue+ await self._updated.wait()+ finally:+ self._active_consumers = max(0, self._active_consumers - 1)++ async def close(self) -> None:+ if self._closed:+ return+ self._closed = True+ self._updated.set()+ self.released.set()+ task = self._pump_task+ self._pump_task = None+ if task is None:+ return+ if not task.done():+ task.cancel()+ try:+ await task+ except asyncio.CancelledError:+ pass+++@asynccontextmanager+async def _wake_capture_with_release(+ capture: MicrophoneCapture,+ released: asyncio.Event,+) -> AsyncIterator[MicrophoneCapture]:+ """Signal only after the wake microphone has fully closed."""+ released.clear()+ try:+ async with capture as mic:+ yield mic+ finally:+ released.set()++class PipelineState(enum.Enum):IDLE = "idle"ACTIVE = "active"@@ -1440,6 +1591,21 @@ class SpeechPipeline:self._state = PipelineState.IDLEself._call_event = asyncio.Event()self._hangup_event = asyncio.Event()+ # Wake and session capture use the same physical input device. A wake+ # stream must close before the session stream opens, while frames heard+ # after the visible wake candidate are retained for the session.+ self._wake_capture_released = asyncio.Event()+ self._wake_capture_released.set()+ self._wake_stop_event = asyncio.Event()+ self._wake_cancel_event = asyncio.Event()+ self._wake_handoff_ready = asyncio.Event()+ self._wake_handoff_buffer: _SessionInputBuffer | None = None+ self._wake_preroll_active = False+ self._wake_preroll_confirmed = False+ # Candidate verification is bounded; retain its short post-reveal+ # window in full so the first spoken word can never fall off a+ # chunk-count deque before the live handoff is accepted.+ self._pending_session_preroll: deque[AudioChunk] = deque()# ``request_hangup`` is also called by the JarvisBar's dedicated Tk# thread. asyncio primitives are not thread-safe, so ``run()`` records# their owning loop and external callers marshal the hangup onto it.@@ -1447,6 +1613,10 @@ class SpeechPipeline:# A thread-safe edge latch keeps repeated X clicks from queueing the# expensive hard-stop path more than once during the same teardown.self._external_hangup_pending = threading.Event()+ # Distinguish a close that arrived after a wake stream was offered from+ # an older no-op hangup while truly idle. Both otherwise share the same+ # asyncio hangup event when the state loop begins.+ self._wake_handoff_hangup_pending = threading.Event()self._current_voice_session_id: str | None = None# Configured voice mode and effective in-flight engine are deliberately# separate. A settings write can happen while a call is already open;@@ -1458,12 +1628,12 @@ class SpeechPipeline:self._voice_engine_transitioning: bool = Falseself._reopen_after_engine_change: bool = Falseself._engine_change_reason: str = ""- # Optionale Event-Bus-Integration — wenn None, sind alle- # _transition/_emit-Calls no-ops (Rueckwaertskompatibilitaet)+ # Optional event-bus integration. With no bus, transition and emit+ # calls are backward-compatible no-ops.self._bus = busself._supervisor = supervisor- # Permanent-Vision (Wave-2 B7): optional injected. Bei None sind alle- # Vision-Hooks no-ops — Pipeline laeuft unveraendert wie vorher.+ # Permanent Vision (Wave 2 B7) is optional. Without an injected+ # provider, every vision hook is a no-op.self._config = configself._vision_provider = vision_providerself._activation_gate = activation_gate or (lambda: True)@@ -1512,7 +1682,7 @@ class SpeechPipeline:# mid-utterance. Preambles are dropped, not parked — they are stale by# the time the user finishes. See ``_on_announcement`` + ``_set_turn_state``.self._deferred_announcements: list[AnnouncementRequested] = []- # Pre-rendered ACK ("Ja?") als PCM-Bytes — beim Warm-up gefüllt+ # Optional pre-rendered acknowledgement PCM, populated during warmup.self._ack_pcm: bytes = b""# Pre-rendered Task-Ack-Phrasen ("Sofort.", "Right away." …) als PCM-Cache.# Key: (lang, phrase_text) → PCM-Bytes. Abspielrate siehe GeminiFlashTTS (24 kHz).@@ -4251,7 +4421,17 @@ class SpeechPipeline:next session starts."""pending = getattr(self, "_external_hangup_pending", None)- if pending is not None and pending.is_set():+ handoff_close = getattr(self, "_wake_handoff_hangup_pending", None)+ handoff_ready = bool(+ getattr(self, "_state", PipelineState.IDLE) is PipelineState.IDLE+ and getattr(self, "_wake_handoff_ready", None)+ and self._wake_handoff_ready.is_set()+ )+ if handoff_ready and handoff_close is not None:+ # Set synchronously on the caller thread. The owner-loop callback+ # can otherwise lose a race with the already-armed state task.+ handoff_close.set()+ if pending is not None and pending.is_set() and not handoff_ready:log.debug("request_hangup ignored: external hangup already pending")returnif pending is not None:@@ -4260,6 +4440,28 @@ class SpeechPipeline:def _dispatch() -> None:try:+ if (+ getattr(self, "_state", PipelineState.IDLE)+ is PipelineState.IDLE+ and getattr(self, "_wake_handoff_ready", None)+ and self._wake_handoff_ready.is_set()+ and handoff_close is not None+ ):+ handoff_close.set()+ if (+ getattr(self, "_state", PipelineState.IDLE)+ is PipelineState.IDLE+ and getattr(self, "_wake_preroll_active", False)+ ):+ # The bar can be visible during wake verification before a+ # real session exists. Its X cancels that candidate; it+ # must not be reinterpreted as a new voice-session click.+ cancel_event = getattr(self, "_wake_cancel_event", None)+ if cancel_event is not None:+ cancel_event.set()+ if pending is not None:+ pending.clear()+ returnself._trigger_voice_hangup()except Exception:# A failed dispatch must be retryable; successful requests stay@@ -4316,13 +4518,21 @@ class SpeechPipeline:where checking only the turn-state misclassifies the visible X as anidle-body click and calls ``request_voice_session()`` instead of hangingup. Treat either lifecycle signal as active so the close control remains- valid throughout startup, the live turn, and teardown.+ valid throughout startup, the live turn, and teardown. A visible wake+ candidate also counts as active for this control only: its close action+ routes to ``request_hangup()``, which retracts the pending candidate+ instead of accidentally starting a session."""return (getattr(self, "_state", PipelineState.IDLE) is not PipelineState.IDLEorgetattr(self, "_turn_state", TurnTakingState.IDLE)is not TurnTakingState.IDLE+ or bool(getattr(self, "_wake_preroll_active", False))+ or bool(+ getattr(self, "_wake_handoff_ready", None)+ and self._wake_handoff_ready.is_set()+ ))def _trigger_voice_hangup(self, *, stop_player: bool = True) -> None:@@ -4342,7 +4552,7 @@ class SpeechPipeline:try:self._player.stop()except Exception as exc: # noqa: BLE001- log.warning("Player-Stop bei Hangup fehlgeschlagen: %s", exc)+ log.warning("Stopping the player during hangup failed: %s", exc)# Player stopped => no TTS tail => the next session end uses the# SHORT wake-lock so the user can re-wake immediately. A farewell# hangup (stop_player=False) keeps the full speaker-tail guard.@@ -4386,20 +4596,172 @@ class SpeechPipeline:log.debug("ContinuationBuffer.discard() failed (non-fatal)", exc_info=True)# ------------------------------------------------------------------- # Wake-Loop mit Parallel-Detection+ # Wake loop and capture handoff# ------------------------------------------------------------------+ def _begin_wake_preroll(+ self, recent: tuple[AudioChunk, ...] = ()+ ) -> None:+ """Retain audio from the first truthful wake visual onward."""+ if self._wake_preroll_active:+ return+ self._pending_session_preroll.clear()+ self._pending_session_preroll.extend(recent)+ self._wake_preroll_active = True+ self._wake_preroll_confirmed = False++ def _discard_wake_preroll(self) -> None:+ self._wake_preroll_active = False+ self._wake_preroll_confirmed = False+ self._pending_session_preroll.clear()++ def _take_wake_preroll(self) -> tuple[AudioChunk, ...]:+ chunks = (+ tuple(self._pending_session_preroll)+ if self._wake_preroll_confirmed+ else ()+ )+ self._discard_wake_preroll()+ return chunks++ async def _claim_wake_capture_for_session(+ self,+ ) -> _SessionInputBuffer | None:+ """Transfer the live wake mic to the session without closing it."""+ if self._wake_handoff_ready.is_set():+ buffer = self._wake_handoff_buffer+ self._wake_handoff_buffer = None+ self._wake_handoff_ready.clear()+ if buffer is None:+ raise RuntimeError("Wake microphone offered an empty handoff.")+ return buffer+ if self._wake_capture_released.is_set():+ # The wake task may already have passed its IDLE check and be one+ # scheduling turn away from entering the capture context. Give it+ # that turn before deciding no wake microphone exists; otherwise a+ # hotkey can open a second native stream during the pre-open race.+ await asyncio.sleep(0)+ if self._wake_capture_released.is_set():+ return None+ self._wake_stop_event.set()+ handoff_wait = asyncio.create_task(+ self._wake_handoff_ready.wait(), name="wake-handoff-ready"+ )+ release_wait = asyncio.create_task(+ self._wake_capture_released.wait(), name="wake-capture-release"+ )+ try:+ await asyncio.wait(+ {handoff_wait, release_wait},+ return_when=asyncio.FIRST_COMPLETED,+ )+ finally:+ for task in (handoff_wait, release_wait):+ if not task.done():+ task.cancel()+ for task in (handoff_wait, release_wait):+ try:+ await task+ except asyncio.CancelledError:+ pass+ self._wake_stop_event.clear()+ # Prefer a concrete handoff if readiness and physical release raced in+ # the same loop tick. Otherwise the old wake stream is authoritatively+ # closed and the caller may safely open one fallback capture.+ if not self._wake_handoff_ready.is_set():+ if self._wake_capture_released.is_set():+ return None+ raise RuntimeError("Wake microphone ownership ended ambiguously.")+ buffer = self._wake_handoff_buffer+ self._wake_handoff_buffer = None+ self._wake_handoff_ready.clear()+ if buffer is None:+ raise RuntimeError("Wake microphone handoff produced no input buffer.")+ return buffer++ async def _abort_pending_wake_handoff(self) -> None:+ """Release an offered wake stream when the state loop rejects the call."""+ wake_capture_was_open = not self._wake_capture_released.is_set()+ preview_was_visible = bool(+ self._wake_preroll_active or self._wake_handoff_ready.is_set()+ )+ self._discard_wake_preroll()+ try:+ buffer = await self._claim_wake_capture_for_session()+ except Exception as exc: # noqa: BLE001 - rejection must not kill state loop+ log.warning("Pending wake handoff could not be released: %s", exc)+ self._wake_cancel_event.set()+ self._wake_handoff_hangup_pending.clear()+ self._external_hangup_pending.clear()+ self._hangup_event.clear()+ self._explicit_hard_hangup = False+ return+ if buffer is not None:+ await buffer.close()+ await self._wake_capture_released.wait()+ try:+ if preview_was_visible or wake_capture_was_open or buffer is not None:+ # WakeWordDetected may already have put the bar into LISTENING+ # even though no VoiceSessionStarted will follow. Explicitly+ # retract that preview so UI state matches the mic lease.+ await self._publish_event(+ WakeCandidateDetected(source_layer="speech", active=False)+ )+ finally:+ self._wake_handoff_hangup_pending.clear()+ self._external_hangup_pending.clear()+ self._hangup_event.clear()+ self._explicit_hard_hangup = False++ @asynccontextmanager+ async def _capture_first_session_input(+ self,+ ) -> AsyncIterator[_SessionInputBuffer]:+ """Borrow the wake mic or open exactly one fallback session mic."""+ buffer = await self._claim_wake_capture_for_session()+ if buffer is not None:+ try:+ yield buffer+ finally:+ await buffer.close()+ # ``buffer.released`` lets the wake owner begin teardown. Wait+ # for the stronger signal too so no disconnect sound or engine+ # re-arm can race the still-open native microphone.+ await self._wake_capture_released.wait()+ return++ async with MicrophoneCapture(+ device=self._input_device,+ max_queue_chunks=REALTIME_QUEUE_CHUNKS,+ device_priority=self._input_priority,+ ) as mic:+ buffer = _SessionInputBuffer()++ async def _unmuted_capture() -> AsyncIterator[AudioChunk]:+ async for chunk in mic.stream():+ # Filter at capture time, not only at the eventual consumer:+ # a realtime fallback may replay retained frames after the+ # user unmutes and must never resurrect muted audio.+ if not getattr(self, "_muted", False):+ yield chunk++ buffer.start(_unmuted_capture())+ try:+ yield buffer+ finally:+ await buffer.close()+def _wake_listening_enabled(self) -> bool:return self._openwakeword_enabled or self._whisper_wake_enabledasync def _wake_loop(self) -> None:- """Lauscht im IDLE-State auf Wake-Word über ZWEI parallele Pfade.+ """Listen for a wake phrase through two parallel paths while idle.- Ein Mic-Stream wird per Fanout in zwei Queues gesplittet:- (1) openWakeWord-Queue → schneller Primary-Detector- (2) Whisper-Wake-Queue → robuster Fallback+ One microphone stream fans out into two queues:+ (1) openWakeWord queue -> fast primary detector.+ (2) Whisper wake queue -> robust fallback.- Wer zuerst triggert, feuert `call_event` und beide Tasks werden gecancelt.+ The first detector hit arms ``call_event`` and cancels both detectors."""log.info("Wake-Loop gestartet (oww=%s, whisper=%s, gate=%s).",@@ -4457,7 +4819,7 @@ class SpeechPipeline:)await self._run_parallel_wake()except Exception as exc: # noqa: BLE001- log.exception("Wake-Loop Fehler: %s", exc)+ log.exception("Wake loop failed: %s", exc)await asyncio.sleep(0.5)def _should_show_optimistic_candidate(self) -> bool:@@ -4492,6 +4854,10 @@ class SpeechPipeline:"""if active and self._state != PipelineState.IDLE:return # a session is already running; nothing to reveal+ if active:+ self._begin_wake_preroll()+ else:+ self._discard_wake_preroll()await self._publish_event(WakeCandidateDetected(source_layer="speech", active=active))@@ -4650,7 +5016,7 @@ class SpeechPipeline:return Falseasync def _run_parallel_wake(self) -> None:- """Öffnet ein Mic, fannt zu 2 Detector-Queues, wartet auf ersten Hit."""+ """Fan one wake microphone into all detectors until the first hit."""oww_queue: asyncio.Queue = asyncio.Queue(maxsize=50)whisper_queue: asyncio.Queue = asyncio.Queue(maxsize=100)detector_queues = [oww_queue] if self._openwakeword_enabled else []@@ -4668,29 +5034,70 @@ class SpeechPipeline:# needs to re-arm fanout.ring_bytes = bytearray()RING_MAX = 16_000 * 2 * 3 # ~3 s+ recent_chunks: deque[AudioChunk] = deque(maxlen=2) # ~200 ms overlap+ wake_confirmed = False+ handoff_buffer: _SessionInputBuffer | None = Noneasync def _fanout(mic: MicrophoneCapture) -> None:- async for chunk in mic.stream():- # Input mute (Jarvis-scoped): if the user mutes while a wake- # session is already mid-wait, stop feeding the detectors so- # Jarvis goes deaf immediately — without touching the OS mic.- # (A fresh mute while IDLE is already handled by _wake_loop's- # _activation_allowed() gate, which never opens the mic.)- if getattr(self, "_muted", False):- continue- ring_bytes.extend(chunk.pcm)- if len(ring_bytes) > RING_MAX:- del ring_bytes[: len(ring_bytes) - RING_MAX]- for q in detector_queues:- try:- q.put_nowait(chunk)- except asyncio.QueueFull:- # Detector ist hinten dran — ältesten Chunk droppen+ source_error: BaseException | None = None+ try:+ async for chunk in mic.stream():+ # Input mute (Jarvis-scoped): if the user mutes while a wake+ # session is already mid-wait, stop feeding the detectors so+ # Jarvis goes deaf immediately — without touching the OS mic.+ # (A fresh mute while IDLE is already handled by _wake_loop's+ # _activation_allowed() gate, which never opens the mic.)+ if getattr(self, "_muted", False):+ continue+ if handoff_buffer is not None:+ handoff_buffer.put(chunk)+ continue+ ring_bytes.extend(chunk.pcm)+ if len(ring_bytes) > RING_MAX:+ del ring_bytes[: len(ring_bytes) - RING_MAX]+ recent_chunks.append(chunk)+ if self._wake_preroll_active:+ self._pending_session_preroll.append(chunk)+ _feed_live_mic_level(chunk)+ for q in detector_queues:try:- q.get_nowait()q.put_nowait(chunk)- except asyncio.QueueEmpty:- pass+ except asyncio.QueueFull:+ # Keep detector latency bounded by dropping its oldest frame.+ try:+ q.get_nowait()+ q.put_nowait(chunk)+ except asyncio.QueueEmpty:+ pass+ except asyncio.CancelledError:+ raise+ except BaseException as exc: # noqa: BLE001 - surface to consumers+ source_error = exc+ raise+ finally:+ if handoff_buffer is not None:+ handoff_buffer.finish(error=source_error)++ def _activate_handoff(*, confirmed: bool) -> _SessionInputBuffer:+ nonlocal handoff_buffer+ if handoff_buffer is not None:+ return handoff_buffer+ if confirmed:+ initial = self._take_wake_preroll()+ else:+ # An explicit click/hotkey may arrive while a wake candidate is+ # still being verified. Preserve that already-visible interval;+ # otherwise keep only one block around the explicit edge.+ initial = (+ tuple(self._pending_session_preroll)+ if self._wake_preroll_active+ else tuple(recent_chunks)[-1:]+ )+ self._discard_wake_preroll()+ handoff_buffer = _SessionInputBuffer(initial=initial)+ self._wake_handoff_buffer = handoff_buffer+ self._wake_handoff_ready.set()+ return handoff_bufferasync def _run_oww() -> str:async for kw in self._wake.detect(_queue_iter(oww_queue)):@@ -4699,7 +5106,7 @@ class SpeechPipeline:async def _run_whisper() -> str:if self._whisper_wake is None:- await asyncio.Event().wait() # nie+ await asyncio.Event().wait() # never completesreturn ""async for kw in self._whisper_wake.detect(_queue_iter(whisper_queue)):return f"whisper:{kw}"@@ -4714,8 +5121,11 @@ class SpeechPipeline:# which belong to the wake layer; this change deliberately does not touch# them. The drop-OLDEST overflow policy (capture ``_safe_put``) still# applies and is safe here.- async with MicrophoneCapture(- device=self._input_device, device_priority=self._input_priority+ async with _wake_capture_with_release(+ MicrophoneCapture(+ device=self._input_device, device_priority=self._input_priority+ ),+ self._wake_capture_released,) as mic:fanout_task = asyncio.create_task(_fanout(mic), name="fanout")oww_task = (@@ -4733,10 +5143,10 @@ class SpeechPipeline:whisper_task = None # type: ignore[assignment]async def _detector_heartbeat() -> None:- """Alle 10s: loggen dass die Detector-Tasks noch leben.+ """Log every ten seconds while all detector tasks remain alive.- Wenn hier nichts mehr kommt obwohl der Watchdog läuft, ist- ein Task silent gestorben (Queue-Deadlock, Exception-Swallow).+ Missing output while the watchdog is running exposes a silently+ failed task, such as a queue deadlock or swallowed exception."""while True:await asyncio.sleep(10.0)@@ -4751,7 +5161,7 @@ class SpeechPipeline:oww_queue.qsize(),whisper_queue.qsize(),)- # Wenn ein Detector-Task mit Exception gestorben ist, sichtbar machen+ # Surface a detector task that exited with an exception.for t in (oww_task, whisper_task):if t is None or not t.done():continue@@ -4760,7 +5170,7 @@ class SpeechPipeline:except asyncio.CancelledError:passexcept Exception as exc: # noqa: BLE001- log.error("Detector-Task %s gestorben: %s", t.get_name(), exc)+ log.error("Detector task %s failed: %s", t.get_name(), exc)heartbeat_task = asyncio.create_task(_detector_heartbeat(), name="wake-heartbeat")@@ -4771,13 +5181,41 @@ class SpeechPipeline:self._wake_reload_event.wait(), name="wake-reload")tasks.append(reload_task)+ handoff_task = asyncio.create_task(+ self._wake_stop_event.wait(), name="wake-session-handoff"+ )+ tasks.append(handoff_task)+ cancel_task = asyncio.create_task(+ self._wake_cancel_event.wait(), name="wake-candidate-cancel"+ )+ tasks.append(cancel_task)++ async def _cancel_candidate() -> None:+ self._wake_cancel_event.clear()+ if self._wake_preroll_active:+ await self._publish_event(+ WakeCandidateDetected(source_layer="speech", active=False)+ )+ self._discard_wake_preroll()try:- # Warten bis EIN Detector etwas yielded — oder ein Live-Reload.- detector_tasks = [t for t in tasks if t is not fanout_task]+ # Wait for one detector, a live reload, or a session handoff.done, _pending = await asyncio.wait(- detector_tasks, return_when=asyncio.FIRST_COMPLETED+ tasks, return_when=asyncio.FIRST_COMPLETED)+ if cancel_task in done:+ await _cancel_candidate()+ return+ if fanout_task in done:+ if not fanout_task.cancelled():+ exc = fanout_task.exception()+ if exc is not None:+ log.warning("Wake microphone fanout ended: %s", exc)+ return+ if handoff_task in done:+ buffer = _activate_handoff(confirmed=False)+ await buffer.released.wait()+ returnif reload_task in done:self._wake_reload_event.clear()log.info(@@ -4785,12 +5223,16 @@ class SpeechPipeline:)return # finally cancels detectors + closes mic; loop re-entersfor t in done:- if t is reload_task:+ if t in {reload_task, handoff_task, cancel_task}:continueresult = t.result()if result:- log.info("🎙 WAKE-Kandidat über %s", result)- # Prefix-Verifier: an OWW hit is only a *candidate*+ # The detector hit marks the earliest truthful visual+ # edge. Do not seed pre-hit audio: it contains the wake+ # phrase itself and could become a phantom user turn.+ self._begin_wake_preroll()+ log.info("🎙 Wake candidate from %s", result)+ # Prefix verifier: an OWW hit is only a *candidate*# until the cloud STT confirms "hey/hi/hallo + jarv"# in the few seconds before the trigger. Whisper-wake# already enforces the same pattern, so its hits skip@@ -4811,7 +5253,53 @@ class SpeechPipeline:source_layer="speech", active=True))- verified = await self._verify_oww_hit(bytes(ring_bytes))+ verify_task = asyncio.create_task(+ self._verify_oww_hit(bytes(ring_bytes)),+ name="wake-prefix-verify",+ )+ verify_done, _ = await asyncio.wait(+ {+ verify_task,+ handoff_task,+ cancel_task,+ fanout_task,+ },+ return_when=asyncio.FIRST_COMPLETED,+ )+ if cancel_task in verify_done:+ verify_task.cancel()+ try:+ await verify_task+ except asyncio.CancelledError:+ pass+ await _cancel_candidate()+ return+ if fanout_task in verify_done:+ verify_task.cancel()+ try:+ await verify_task+ except asyncio.CancelledError:+ pass+ await _cancel_candidate()+ if not fanout_task.cancelled():+ exc = fanout_task.exception()+ if exc is not None:+ log.warning(+ "Wake microphone fanout ended during "+ "candidate verification: %s",+ exc,+ )+ return+ if handoff_task in verify_done:+ verify_task.cancel()+ try:+ await verify_task+ except asyncio.CancelledError:+ pass+ buffer = _activate_handoff(confirmed=False)+ await buffer.released.wait()+ return+ verified = verify_task.result()if not verified:if show_candidate:await self._publish_event(@@ -4820,8 +5308,8 @@ class SpeechPipeline:))log.info(- "🚫 WAKE verworfen — kein 'Hey'-Prefix im "- "Transkript der letzten ~3 s"+ "🚫 Wake rejected: no greeting prefix in the "+ "last three seconds of transcript.")# Clear the detector's debounce cooldown: this# candidate was a false positive, so a genuine@@ -4833,7 +5321,7 @@ class SpeechPipeline:if callable(note_rejected):note_rejected()break- log.info("🎙 WAKE bestätigt über %s", result)+ log.info("🎙 Wake confirmed by %s", result)if self._state == PipelineState.IDLE:# The state-loop SILENTLY drops a wake that arrives# inside the post-hangup echo lock (or when the app@@ -4863,27 +5351,31 @@ class SpeechPipeline:)if locked:log.info(- "🔒 Wake-Lock aktiv — Emit verworfen "- "(noch %.1fs)",+ "🔒 Wake lock active; discarding detection "+ "for another %.1fs.",max(0.0, self._wake_lock_until - now),)else:log.info(- "Wake-Emit verworfen — App nicht "- "aktivierbar."+ "Wake detection discarded: desktop "+ "activation is unavailable.")break- # Event-Bus: WakeWordDetected + Supervisor-State- # werden fuer UI-Feedback gebraucht (Orb einblenden).+ # The UI consumes WakeWordDetected plus supervisor+ # state to reveal its listening surface.keyword = result.split(":", 1)[-1] if ":" in result else result+ self._wake_preroll_confirmed = True+ wake_confirmed = True+ buffer = _activate_handoff(confirmed=True)await self._emit_wake(keyword)- self._call_event.set()+ self._arm_call_event()+ await buffer.released.wait()breakfinally:heartbeat_task.cancel()try:await heartbeat_task- except (asyncio.CancelledError, Exception):+ except (asyncio.CancelledError, Exception): # noqa: S110 - teardownpassfor t in tasks:if not t.done():@@ -4891,8 +5383,22 @@ class SpeechPipeline:for t in tasks:try:await t- except (asyncio.CancelledError, Exception):+ except (asyncio.CancelledError, Exception): # noqa: S110 - teardownpass+ # An exception/cancellation between offering the buffer and the+ # state loop claiming it must not leave a ready latch pointing+ # at stale audio for the next activation.+ if (+ handoff_buffer is not None+ and self._wake_handoff_buffer is handoff_buffer+ ):+ self._wake_handoff_buffer = None+ self._wake_handoff_ready.clear()+ await handoff_buffer.close()+ self._wake_cancel_event.clear()+ self._wake_stop_event.clear()+ if not wake_confirmed:+ self._discard_wake_preroll()# ------------------------------------------------------------------# State-Loop@@ -4902,26 +5408,45 @@ class SpeechPipeline:while True:await self._call_event.wait()self._call_event.clear()- # Wake-Cooldown-Check: ignoriere Wake-Events die während- # der Lock-Zeit kommen (Speaker-Echo nach Hangup).+ # Ignore activation edges that arrive inside the speaker-echo lock.now = time.time()if not self._activation_allowed():- log.info("Voice-Call ignoriert: Desktop-App ist nicht sichtbar.")+ log.info("Voice call ignored: desktop activation is unavailable.")# A discarded call consumes any PTT arming with it — otherwise a# stale ``_ptt_mode`` would reroute the NEXT (wake-word) call# into the raw-recording path that has no key to release.self._ptt_mode = False+ await self._abort_pending_wake_handoff()continueif now < self._wake_lock_until:remaining = self._wake_lock_until - now- log.info("🔒 Wake-Lock aktiv — ignoriere (noch %.1fs)", remaining)+ log.info("Wake lock active; ignoring call for %.1fs.", remaining)self._ptt_mode = False+ await self._abort_pending_wake_handoff()continue- self._external_hangup_pending.clear()- self._hangup_event.clear()- # Fresh session: forget any hard-hangup flag left by a no-op hangup- # while idle, so ONLY this session's own ending decides its lock.- self._explicit_hard_hangup = False+ # Preserve a close accepted after the confirmed wake offered its+ # microphone but before this loop entered ACTIVE. Clearing it here+ # would reopen the visibly closed bar and submit the buffered words.+ handoff_offered = bool(+ self._wake_handoff_buffer is not None+ or self._wake_handoff_ready.is_set()+ )+ handoff_close_latch = getattr(+ self, "_wake_handoff_hangup_pending", None+ )+ preserve_handoff_close = bool(+ handoff_offered+ and handoff_close_latch is not None+ and handoff_close_latch.is_set()+ )+ if handoff_close_latch is not None:+ handoff_close_latch.clear()+ if not preserve_handoff_close:+ self._external_hangup_pending.clear()+ self._hangup_event.clear()+ # Forget a hard-hangup flag left by a no-op while truly idle.+ # A close after handoff is real and retains the short wake lock.+ self._explicit_hard_hangup = Falseself._state = PipelineState.ACTIVE# Reset per-session completeness signal state: a new session# starts "fresh" so the first INCOMPLETE gets an earcon, not a@@ -4940,44 +5465,45 @@ class SpeechPipeline:else (self._last_wake_keyword or "hotkey"))self._last_wake_keyword = ""- await self._publish_event(- VoiceSessionStarted(- source_layer="speech.pipeline",- session_id=session_id,- wake_keyword=wake_keyword,- language="de",- )- )hangup_reason = HANGUP_ERRORtry:- # VoiceSessionStarted dispatch can legitimately take time (for- # example while desktop audio ducking enumerates other apps).- # The JarvisBar X is already visible during that await. If it- # was pressed, do not resurrect LISTENING, play an ack, or open- # the microphone after the user has explicitly closed the call.- if (- self._hangup_event.is_set()- or self._external_hangup_pending.is_set()- ):- hangup_reason = HANGUP_HOTKEY- log.info(- "Voice session cancelled during startup; skipping audio open."- )- else:- log.info(- "Voice session accepted (configured engine=%s).",- self._active_voice_mode,+ # Reuse the already-open wake microphone when available. The+ # handoff never owns two native streams at once and retains all+ # frames heard after the visible wake candidate.+ async with self._capture_first_session_input() as input_buffer:+ # This event is the UI's authoritative "being listened to+ # now" signal. Capture is already armed before any+ # subscriber can reveal the listening bar.+ await self._publish_event(+ VoiceSessionStarted(+ source_layer="speech.pipeline",+ session_id=session_id,+ wake_keyword=wake_keyword,+ language="de",+ ))- # Reveal the voice surface and enable microphone feedback.- await self._set_turn_state(TurnTakingState.LISTENING)- await self._play_ack(ptt=self._ptt_mode)+ # A close accepted while a slow start subscriber runs must+ # release the borrowed capture without entering a provider.if (self._hangup_event.is_set()or self._external_hangup_pending.is_set()):hangup_reason = HANGUP_HOTKEY+ log.info("Voice session cancelled during startup.")else:- hangup_reason = await self._active_session()+ log.info(+ "Voice session accepted (configured engine=%s).",+ self._active_voice_mode,+ )+ await self._set_turn_state(TurnTakingState.LISTENING)+ # Activation feedback is now visual-only. Playing a+ # chime or spoken ACK while a portable desktop mic is+ # live forces an impossible choice: discard simultaneous+ # user speech or feed speaker echo into STT. The visible+ # bar is the truthful zero-loss acknowledgement.+ hangup_reason = await self._active_session(+ input_buffer=input_buffer+ )except Exception as exc: # noqa: BLE001log.exception("Voice session failed: %s", exc)finally:@@ -5070,10 +5596,10 @@ class SpeechPipeline:log.debug("Earcon playback skipped (%s).", exc)async def _play_ack(self, *, ptt: bool = False) -> None:- """Chime + pre-rendertes 'Ja?' — Gesamtdauer ~400-600 ms.+ """Play the legacy chime and optional pre-rendered acknowledgement.Push-to-talk plays ONLY the chime: the user is already holding the key- and talking, so a spoken "Ja?" would talk over their opening words. The+ and talking, so a spoken acknowledgement would cover their opening words. Thechime is immediate feedback that recording is live; speech is not."""try:@@ -5089,14 +5615,30 @@ class SpeechPipeline:returnif self._ack_pcm:await self._player.play_pcm(self._ack_pcm, sample_rate=24_000)- # Kurze Echo-Suppression: bei Open-Back-Kopfhoerern leakt der- # pre-renderte ACK ins Mic und triggert VAD auf dem TTS-Echo.- # 400ms Dead-Zone nach ACK verhindert Self-Retrigger-Loops.+ # Brief echo suppression keeps a pre-rendered acknowledgement from+ # leaking through open-back headphones and retriggering VAD.await asyncio.sleep(0.4)except Exception as exc: # noqa: BLE001- log.warning("ACK-Playback fehlgeschlagen: %s", exc)+ log.warning("Acknowledgement playback failed: %s", exc)++ @asynccontextmanager+ async def _session_input_source(+ self, input_buffer: _SessionInputBuffer | None+ ) -> AsyncIterator[AsyncIterator[AudioChunk]]:+ """Yield a pre-opened session stream or own a fallback capture."""+ if input_buffer is not None:+ yield input_buffer.stream()+ return+ async with MicrophoneCapture(+ device=self._input_device,+ max_queue_chunks=REALTIME_QUEUE_CHUNKS,+ device_priority=self._input_priority,+ ) as mic:+ yield mic.stream()- async def _active_session(self) -> str:+ async def _active_session(+ self, *, input_buffer: _SessionInputBuffer | None = None+ ) -> str:# Fresh session → never inherit a half-accumulated dictation from a# previous session that ended mid-carry (hangup / idle-timeout).self._carry_pcm = bytearray()@@ -5114,12 +5656,14 @@ class SpeechPipeline:self._active_realtime_provider = ""self._active_realtime_model = ""self._voice_engine_transitioning = False- return await self._ptt_session()+ return await self._ptt_session(input_buffer=input_buffer)requested_mode = self._configured_voice_mode()self._active_voice_mode = requested_modeself._voice_engine_transitioning = requested_mode == "realtime"if requested_mode == "realtime":- realtime_reason = await self._active_realtime_session()+ realtime_reason = await self._active_realtime_session(+ input_buffer=input_buffer+ )if realtime_reason is not None:return realtime_reason# Speak the "realtime unavailable" notice only when the user@@ -5132,18 +5676,32 @@ class SpeechPipeline:getattr(voice_cfg, "model_fields_set", None) or ())if explicitly_chosen:- await self._speak_realtime_unavailable()+ if input_buffer is None:+ await self._speak_realtime_unavailable()+ else:+ # Capture-first sessions cannot play a fallback notice into+ # their own live microphone without either recording the+ # speaker echo or discarding simultaneous user speech.+ lang = _phrase_lang(self._output_language(None, ""))+ await self._publish_event(+ MessageSent(+ source_layer="speech.pipeline",+ thread_id="voice",+ role="system",+ text=_REALTIME_UNAVAILABLE_PHRASE[lang],+ )+ )+ log.warning(+ "Realtime unavailable; continuing with classic voice "+ "with a visual status notice."+ )self._active_voice_mode = "pipeline"self._active_realtime_provider = ""self._active_realtime_model = ""self._voice_engine_transitioning = False- async with MicrophoneCapture(- device=self._input_device,- max_queue_chunks=REALTIME_QUEUE_CHUNKS,- device_priority=self._input_priority,- ) as mic:+ async with self._session_input_source(input_buffer) as input_chunks:vad_iter = self._vad.utterances(- self._session_input_stream(mic.stream())+ self._session_input_stream(input_chunks)).__aiter__()# The VAD ``__anext__`` task PERSISTS across idle windows. Cancelling# it kills the underlying async generator (the next ``__anext__``@@ -5254,7 +5812,7 @@ class SpeechPipeline:next_task = Nonereturn HANGUP_SHUTDOWNexcept Exception as exc: # noqa: BLE001- log.exception("VAD-Fehler: %s", exc)+ log.exception("VAD failed: %s", exc)next_task = Nonecontinuenext_task = None@@ -5269,7 +5827,9 @@ class SpeechPipeline:next_task.cancel()return HANGUP_HOTKEY- async def _active_realtime_session(self) -> str | None:+ async def _active_realtime_session(+ self, *, input_buffer: _SessionInputBuffer | None = None+ ) -> str | None:"""Run one desktop duplex session, or request classic fallback.Imports stay lazy so the optional realtime stack never enters the boot@@ -5289,16 +5849,18 @@ class SpeechPipeline:playback = DesktopRealtimePlayback(self._player)turn_complete = asyncio.Event()speaking = False+ semantic_turn_committed = Falseasync def _send_binary(pcm: bytes) -> None:- nonlocal speaking+ nonlocal semantic_turn_committed, speaking+ semantic_turn_committed = Trueif not speaking:speaking = Trueawait self._set_turn_state(TurnTakingState.JARVIS_SPEAKING)await playback.send_binary(pcm)async def _send_json(message: dict[str, Any]) -> None:- nonlocal speaking+ nonlocal semantic_turn_committed, speakingkind = str(message.get("type", ""))if kind == "audio_ready":playback.set_sample_rate(@@ -5320,21 +5882,32 @@ class SpeechPipeline:elif kind == "transcript":role = str(message.get("role", ""))if role == "user" and bool(message.get("is_final", False)):+ # A final user transcript can already have triggered a tool+ # inside RealtimeVoiceSession. Replaying its raw audio through+ # classic STT after a later provider failure could execute the+ # same side effect twice.+ semantic_turn_committed = Trueawait self._set_turn_state(TurnTakingState.PROCESSING)+ elif role == "assistant":+ semantic_turn_committed = Trueelif kind == "tts_cancel":speaking = Falseawait playback.cancel()await self._set_turn_state(TurnTakingState.LISTENING)elif kind == "hangup":+ semantic_turn_committed = Truespeaking = Falseawait playback.cancel()elif kind == "turn_complete":+ semantic_turn_committed = Trueawait playback.finish_turn()speaking = Falseawait self._set_turn_state(TurnTakingState.LISTENING)if not self._continue_listening_after_response:turn_complete.set()elif kind in {"provider_error", "error_spoken"}:+ if kind == "error_spoken":+ semantic_turn_committed = Truelog.warning("Realtime desktop status: %s", message)session_id = getattr(self, "_current_voice_session_id", None) or str(uuid4())@@ -5351,54 +5924,104 @@ class SpeechPipeline:# closure late-binds ``session``: the provider_ready gate only opens# after the session object exists.provider_ready = asyncio.Event()- # Cap ~6s; drop OLDEST beyond it (a longer handshake means a provider- # problem — memory stays bounded, freshest speech wins).- preroll: deque[bytes] = deque(maxlen=60)+ preroll: deque[bytes] = deque()+ preroll_bytes = 0wait_tasks: set[asyncio.Task[Any]] = set()reason = "desktop_fallback"session: Any | None = Nonemicrophone_task: asyncio.Task[Any] | None = Nonetry:- async with MicrophoneCapture(- device=self._input_device,- max_queue_chunks=REALTIME_QUEUE_CHUNKS,- device_priority=self._input_priority,- ) as mic:+ async with self._session_input_source(input_buffer) as input_chunks:async def _send_microphone() -> None:- async for chunk in self._session_input_stream(mic.stream()):+ nonlocal preroll_bytes+ async for chunk in self._session_input_stream(input_chunks):if not provider_ready.is_set():+ if (+ preroll_bytes + len(chunk.pcm)+ > _SESSION_START_BUFFER_MAX_BYTES+ ):+ raise RuntimeError(+ "Realtime startup exceeded the 30-second "+ "audio buffer; command prefix preserved, "+ "session aborted."+ )preroll.append(chunk.pcm)+ preroll_bytes += len(chunk.pcm)continuewhile preroll:- await session.handle_audio_frame(preroll.popleft())+ buffered = preroll.popleft()+ preroll_bytes = max(0, preroll_bytes - len(buffered))+ await session.handle_audio_frame(buffered)await session.handle_audio_frame(chunk.pcm)- microphone_task = asyncio.create_task(- _send_microphone(), name=f"rt-mic-{session_id}"+ # A shared capture buffer already owns and meters production+ # input, so leave it unread until the provider accepts audio.+ # The legacy direct-call path has no such producer and starts+ # its local pump immediately.+ if input_buffer is None:+ microphone_task = asyncio.create_task(+ _send_microphone(), name=f"rt-mic-{session_id}"+ )++ hangup_task = asyncio.create_task(+ self._hangup_event.wait(), name=f"rt-hangup-{session_id}"+ )+ wait_tasks.add(hangup_task)+ build_task = asyncio.create_task(+ asyncio.to_thread(+ build_realtime_session,+ cfg=self._config,+ bus=self._bus,+ session_id=session_id,+ send_binary=_send_binary,+ send_json=_send_json,+ half_duplex=True,+ surface="desktop",+ brain=getattr(self, "_brain", None),+ ),+ name=f"rt-build-{session_id}",)- session = build_realtime_session(- cfg=self._config,- bus=self._bus,- session_id=session_id,- send_binary=_send_binary,- send_json=_send_json,- half_duplex=True,- surface="desktop",- brain=getattr(self, "_brain", None),+ wait_tasks.add(build_task)+ done, _pending = await asyncio.wait(+ {build_task, hangup_task}, return_when=asyncio.FIRST_COMPLETED)+ if hangup_task in done and self._hangup_event.is_set():+ reason = HANGUP_HOTKEY+ build_task.cancel()+ return HANGUP_HOTKEY+ session = build_task.result()if session is None:return None- await session.handle_control(- {"type": "audio_start", "sample_rate": 16_000}+ handshake_task = asyncio.create_task(+ session.handle_control(+ {"type": "audio_start", "sample_rate": 16_000}+ ),+ name=f"rt-handshake-{session_id}",)+ wait_tasks.add(handshake_task)+ done, _pending = await asyncio.wait(+ {handshake_task, hangup_task},+ return_when=asyncio.FIRST_COMPLETED,+ )+ if hangup_task in done and self._hangup_event.is_set():+ reason = HANGUP_HOTKEY+ handshake_task.cancel()+ return HANGUP_HOTKEY+ handshake_task.result()+ # Completed startup tasks must not remain in the live-session+ # FIRST_COMPLETED set; either would make a healthy realtime+ # session unwind into classic voice immediately.+ wait_tasks.discard(build_task)+ wait_tasks.discard(handshake_task)provider_ready.set()+ if microphone_task is None:+ microphone_task = asyncio.create_task(+ _send_microphone(), name=f"rt-mic-{session_id}"+ )await self._set_turn_state(TurnTakingState.LISTENING)provider_task = asyncio.create_task(session.wait_finished(), name=f"rt-provider-{session_id}")- hangup_task = asyncio.create_task(- self._hangup_event.wait(), name=f"rt-hangup-{session_id}"- )wait_tasks.update({microphone_task, provider_task, hangup_task})if not self._continue_listening_after_response:wait_tasks.add(@@ -5434,14 +6057,32 @@ class SpeechPipeline:type(exc).__name__,exc,)- # A dead provider stream or microphone unwinds into the classic- # pipeline inside the same call, with an honest spoken notice.+ if semantic_turn_committed:+ # Once a final transcript/output exists, replaying from the+ # capture buffer's beginning can duplicate an already-run+ # tool or feed Jarvis's own output back into classic STT.+ # End honestly; the next activation starts with a clean mic.+ reason = HANGUP_ERROR+ log.warning(+ "Realtime session ended after a committed turn; refusing "+ "unsafe audio replay through classic voice."+ )+ return HANGUP_ERROR+ # A startup/early provider failure has committed no semantic+ # user turn, so classic may safely replay the retained opening.return Noneexcept asyncio.CancelledError:reason = "shutdown"raiseexcept Exception as exc: # noqa: BLE001 — classic fallback is load-bearinglog.warning("Realtime desktop session failed; using pipeline: %s", exc)+ if semantic_turn_committed:+ reason = HANGUP_ERROR+ log.warning(+ "Realtime failure followed a committed turn; refusing unsafe "+ "audio replay through classic voice."+ )+ return HANGUP_ERRORreturn Nonefinally:# The mic pump may not be in wait_tasks yet when the session@@ -5454,18 +6095,20 @@ class SpeechPipeline:for task in wait_tasks:try:await task- except (asyncio.CancelledError, Exception):+ except (asyncio.CancelledError, Exception): # noqa: S110 - teardownpassif microphone_task is not None:try:await microphone_task- except (asyncio.CancelledError, Exception):+ except (asyncio.CancelledError, Exception): # noqa: S110 - teardownpassif session is not None:await session.end(reason=reason)await playback.close()- async def _ptt_session(self) -> str:+ async def _ptt_session(+ self, *, input_buffer: _SessionInputBuffer | None = None+ ) -> str:"""Push-to-talk turn: record raw mic audio until the key is released,then submit the whole capture as ONE prompt (one-shot).@@ -5478,9 +6121,7 @@ class SpeechPipeline:"""buffer = bytearray()hung_up = False- async with MicrophoneCapture(- device=self._input_device, device_priority=self._input_priority- ) as mic:+ async with self._session_input_source(input_buffer) as input_chunks:mic_open_at = time.monotonic()await self._set_turn_state(TurnTakingState.LISTENING)await self._publish_event(ListeningStarted(source_layer="speech"))@@ -5489,7 +6130,7 @@ class SpeechPipeline:# Raw capture — no _session_input_stream TTS-echo filter: PTT# plays only a short chime (no spoken ACK), and the user is# holding the key with deliberate intent to record now.- async for chunk in mic.stream():+ async for chunk in input_chunks:buffer.extend(chunk.pcm)# PTT bypasses the VAD, where mic_level.feed normally lives,# so feed the live loudness here too — otherwise the overlaydiff --git a/jarvis/ui/jarvisbar/overlay.py b/jarvis/ui/jarvisbar/overlay.pyindex 02fbd85a61..f44d1263bd 100644--- a/jarvis/ui/jarvisbar/overlay.py+++ b/jarvis/ui/jarvisbar/overlay.py@@ -6,7 +6,8 @@ the bridge is reused unchanged. ``show(mode)`` selects the renderer state;the orb). Text/mouth methods are deliberate no-ops — the bar shows no text.Signals:-- LISTENING: the bridge starts its own ``MicListener`` that calls ``set_level``.+- LISTENING: the speech capture path publishes input RMS via ``mic_level``;+ ``OrbBusBridge`` forwards it to this surface without opening a second mic.- SPEAKING: the audio player publishes its output RMS via ``level_tap``, whichthis surface subscribes to on ``start()``.- THINKING: the renderer generates a synthetic wave (no external signal).diff --git a/tests/unit/speech/test_realtime_mode.py b/tests/unit/speech/test_realtime_mode.pyindex 7ad0d47421..67e3bc0e65 100644--- a/tests/unit/speech/test_realtime_mode.py+++ b/tests/unit/speech/test_realtime_mode.py@@ -3,12 +3,20 @@from __future__ import annotationsimport asyncio+import threadingfrom types import SimpleNamespaceimport pytestimport jarvis.speech.pipeline as pipeline_mod-from jarvis.sessions.constants import HANGUP_SHUTDOWN, HANGUP_TURN_COMPLETE+from jarvis.core.events import MessageSent+from jarvis.core.protocols import AudioChunk+from jarvis.sessions.constants import (+ HANGUP_ERROR,+ HANGUP_HOTKEY,+ HANGUP_SHUTDOWN,+ HANGUP_TURN_COMPLETE,+)from jarvis.speech.pipeline import SpeechPipeline@@ -47,6 +55,20 @@ class _EmptyVad:return _empty()+class _OneShotVad:+ def __init__(self, captured: list[bytes]) -> None:+ self._captured = captured++ def utterances(self, stream): # noqa: ANN001, ANN201+ async def _one():+ async for chunk in stream:+ self._captured.append(chunk.pcm)+ yield chunk.pcm+ return++ return _one()++class _FakeRealtimeSession:def __init__(self, send_binary, send_json) -> None:self._send_binary = send_binary@@ -86,6 +108,40 @@ class _FakeRealtimeSession:self.end_reason = reason+class _HandshakeOnlyRealtimeSession(_FakeRealtimeSession):+ def __init__(self, send_binary, send_json) -> None:+ super().__init__(send_binary, send_json)+ self.ready = asyncio.Event()++ async def handle_control(self, message) -> None:+ self.controls.append(message)+ await self._send_json(+ {+ "type": "audio_ready",+ "provider": "fake-live",+ "input_sample_rate": 16_000,+ "output_sample_rate": 24_000,+ }+ )+ self.ready.set()+++class _CommittedFailureRealtimeSession(_HandshakeOnlyRealtimeSession):+ async def handle_control(self, message) -> None:+ await super().handle_control(message)+ await self._send_json(+ {+ "type": "transcript",+ "role": "user",+ "text": "run the action",+ "is_final": True,+ }+ )++ async def wait_finished(self) -> None:+ return None++def _pipe(mode: str = "realtime") -> SpeechPipeline:pipe = SpeechPipeline.__new__(SpeechPipeline)pipe._config = SimpleNamespace(@@ -160,6 +216,66 @@ async def test_desktop_realtime_handshake_streams_audio_and_ends_single_turn(assert built["session"].end_reason == HANGUP_TURN_COMPLETE+async def test_completed_startup_tasks_do_not_end_healthy_realtime_session(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ """A completed build/handshake is not a live-session completion signal."""+ pipe = _pipe()+ built: dict[str, _HandshakeOnlyRealtimeSession] = {}+ built_ready = asyncio.Event()+ loop = asyncio.get_running_loop()++ def _build(**kwargs):+ session = _HandshakeOnlyRealtimeSession(+ kwargs["send_binary"], kwargs["send_json"]+ )+ built["session"] = session+ loop.call_soon_threadsafe(built_ready.set)+ return session++ monkeypatch.setattr("jarvis.realtime.factory.build_realtime_session", _build)+ monkeypatch.setattr(pipeline_mod, "MicrophoneCapture", lambda **_kwargs: _SilentMic())+ task = asyncio.create_task(pipe._active_realtime_session())+ await asyncio.wait_for(built_ready.wait(), timeout=0.5)+ await asyncio.wait_for(built["session"].ready.wait(), timeout=0.5)+ await asyncio.sleep(0)+ assert task.done() is False++ pipe._hangup_event.set()+ assert await asyncio.wait_for(task, timeout=0.5) == HANGUP_HOTKEY+ assert built["session"].end_reason == HANGUP_HOTKEY+++async def test_committed_realtime_turn_is_never_replayed_through_classic(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ """A provider failure after a final transcript must not duplicate tools."""+ pipe = _pipe()+ built: dict[str, _CommittedFailureRealtimeSession] = {}++ def _build(**kwargs):+ session = _CommittedFailureRealtimeSession(+ kwargs["send_binary"], kwargs["send_json"]+ )+ built["session"] = session+ return session++ monkeypatch.setattr("jarvis.realtime.factory.build_realtime_session", _build)+ buffer = pipeline_mod._SessionInputBuffer( # noqa: SLF001+ initial=(AudioChunk(pcm=b"command", sample_rate=16_000, timestamp_ns=1),)+ )++ reason = await asyncio.wait_for(+ pipe._active_realtime_session(input_buffer=buffer),+ timeout=1.0,+ )++ assert reason == HANGUP_ERROR+ assert built["session"].end_reason == HANGUP_ERROR++@pytest.mark.asyncioasync def test_realtime_mode_routes_before_the_classic_microphone(monkeypatch: pytest.MonkeyPatch,@@ -167,7 +283,7 @@ async def test_realtime_mode_routes_before_the_classic_microphone(pipe = _pipe()calls = 0- async def _run_realtime():+ async def _run_realtime(*, input_buffer=None): # noqa: ANN001nonlocal callscalls += 1return HANGUP_TURN_COMPLETE@@ -190,7 +306,7 @@ async def test_failed_realtime_session_explains_and_falls_back_in_same_call(pipe = _pipe()notices = 0- async def _failed_realtime():+ async def _failed_realtime(*, input_buffer=None): # noqa: ANN001return Noneasync def _notice():@@ -217,7 +333,7 @@ async def test_default_realtime_mode_falls_back_silently_without_notice(pipe._config.voice.model_fields_set = set()notices = 0- async def _failed_realtime():+ async def _failed_realtime(*, input_buffer=None): # noqa: ANN001return Noneasync def _notice():@@ -236,6 +352,43 @@ async def test_default_realtime_mode_falls_back_silently_without_notice(assert notices == 0+async def test_capture_first_realtime_fallback_is_visible_and_preserves_audio() -> None:+ """Fallback status is visible without speaking into the live microphone."""+ pipe = _pipe()+ opening = AudioChunk(pcm=b"opening", sample_rate=16_000, timestamp_ns=1)+ buffer = pipeline_mod._SessionInputBuffer(initial=(opening,)) # noqa: SLF001+ buffer.finish()+ captured: list[bytes] = []+ events: list[object] = []+ pipe._vad = _OneShotVad(captured)+ pipe._session_end_reason = HANGUP_TURN_COMPLETE++ async def _failed_realtime(*, input_buffer=None): # noqa: ANN001, ANN202+ return None++ async def _must_not_speak() -> None:+ pytest.fail("capture-first fallback must not speak into its live microphone")++ async def _publish(event) -> None: # noqa: ANN001+ events.append(event)++ async def _handle(_pcm: bytes) -> bool:+ return False++ pipe._active_realtime_session = _failed_realtime # type: ignore[method-assign]+ pipe._speak_realtime_unavailable = _must_not_speak # type: ignore[method-assign]+ pipe._publish_event = _publish # type: ignore[method-assign]+ pipe._handle_utterance = _handle # type: ignore[method-assign]++ assert await pipe._active_session(input_buffer=buffer) == HANGUP_TURN_COMPLETE+ assert captured == [b"opening"]+ notices = [event for event in events if isinstance(event, MessageSent)]+ assert len(notices) == 1+ assert notices[0].role == "system"+ assert "classic voice pipeline" in notices[0].text++@pytest.mark.asyncioasync def test_pipeline_mode_never_enters_realtime_branch(monkeypatch: pytest.MonkeyPatch,@@ -375,3 +528,79 @@ async def test_session_input_stream_muted_feeds_no_level() -> None:finally:unsubscribe()mic_level.reset_for_tests()+++async def test_shared_input_keeps_meter_live_while_realtime_build_is_blocked(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ """Provider/tool assembly must not starve the capture or Jarvis Bar."""+ from jarvis.audio import mic_level+ from jarvis.audio.capture import AudioChunk++ pipe = _pipe()+ build_entered = threading.Event()+ release_build = threading.Event()++ def _build(**kwargs):+ build_entered.set()+ assert release_build.wait(timeout=1.0)+ return _FakeRealtimeSession(kwargs["send_binary"], kwargs["send_json"])++ monkeypatch.setattr("jarvis.realtime.factory.build_realtime_session", _build)+ buffer = pipeline_mod._SessionInputBuffer() # noqa: SLF001+ mic_level.reset_for_tests()+ levels: list[float] = []+ unsubscribe = mic_level.subscribe(levels.append)+ task = asyncio.create_task(pipe._active_realtime_session(input_buffer=buffer))+ try:+ for _ in range(100):+ if build_entered.is_set():+ break+ await asyncio.sleep(0.001)+ assert build_entered.is_set()++ buffer.put(+ AudioChunk(+ pcm=b"\x00\x40" * 320,+ sample_rate=16_000,+ timestamp_ns=1,+ )+ )+ await asyncio.sleep(0)+ assert levels and levels[-1] > 0.0+ finally:+ release_build.set()++ assert await asyncio.wait_for(task, timeout=1.0) == HANGUP_TURN_COMPLETE+ unsubscribe()+ mic_level.reset_for_tests()+++async def test_hangup_cancels_realtime_start_while_builder_is_blocked(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ pipe = _pipe()+ build_entered = threading.Event()+ release_build = threading.Event()++ def _build(**kwargs):+ build_entered.set()+ release_build.wait(timeout=1.0)+ return _FakeRealtimeSession(kwargs["send_binary"], kwargs["send_json"])++ monkeypatch.setattr("jarvis.realtime.factory.build_realtime_session", _build)+ buffer = pipeline_mod._SessionInputBuffer() # noqa: SLF001+ task = asyncio.create_task(pipe._active_realtime_session(input_buffer=buffer))+ for _ in range(100):+ if build_entered.is_set():+ break+ await asyncio.sleep(0.001)+ assert build_entered.is_set()++ pipe._hangup_event.set()+ try:+ assert await asyncio.wait_for(task, timeout=0.5) == HANGUP_HOTKEY+ finally:+ release_build.set()diff --git a/tests/unit/speech/test_request_hangup_threadsafe.py b/tests/unit/speech/test_request_hangup_threadsafe.pyindex e38a7caa22..84579e5765 100644--- a/tests/unit/speech/test_request_hangup_threadsafe.py+++ b/tests/unit/speech/test_request_hangup_threadsafe.py@@ -13,7 +13,7 @@ import threadingimport pytest-from jarvis.speech.pipeline import SpeechPipeline+from jarvis.speech.pipeline import PipelineState, SpeechPipelinedef _pipeline(owner_loop) -> SpeechPipeline:@@ -52,6 +52,23 @@ async def test_owner_loop_hangup_runs_synchronously() -> None:assert called_on == [threading.get_ident()]+async def test_visible_wake_candidate_close_cancels_without_starting_session() -> None:+ loop = asyncio.get_running_loop()+ pipeline = _pipeline(loop)+ pipeline._state = PipelineState.IDLE+ pipeline._wake_preroll_active = True+ pipeline._wake_cancel_event = asyncio.Event()+ pipeline._trigger_voice_hangup = lambda: pytest.fail(+ "candidate close must not enter live-session hangup"+ )++ pipeline.request_hangup()++ assert pipeline._wake_cancel_event.is_set()+ assert pipeline._external_hangup_pending.is_set() is False++class _QueuedLoop:def __init__(self) -> None:self.callbacks = []diff --git a/tests/unit/speech/test_request_voice_session.py b/tests/unit/speech/test_request_voice_session.pyindex e93fd36670..961c07e6da 100644--- a/tests/unit/speech/test_request_voice_session.py+++ b/tests/unit/speech/test_request_voice_session.py@@ -74,6 +74,15 @@ def test_session_is_active_while_start_subscribers_are_still_running() -> None:assert p.is_session_active() is True+def test_visible_wake_candidate_routes_close_x_to_cancellation() -> None:+ """A pre-session candidate owns the visible close control."""+ p = _pipe()+ p._turn_state = TurnTakingState.IDLE+ p._wake_preroll_active = True++ assert p.is_session_active() is True++def test_noop_when_ptt_active() -> None:p = _pipe(ptt=True)assert p.request_voice_session() is Falsediff --git a/tests/unit/speech/test_session_start_capture.py b/tests/unit/speech/test_session_start_capture.pynew file mode 100644index 0000000000..53de006eab--- /dev/null+++ b/tests/unit/speech/test_session_start_capture.py@@ -0,0 +1,526 @@+"""Capture-first voice-session startup regression coverage.++The native Jarvis Bar is allowed to advertise LISTENING only after microphone+capture is armed. Audio that arrives while start subscribers or a realtime+provider starts must be buffered and delivered to the selected voice engine,+not discarded before ``_active_session`` begins consuming it.+"""++from __future__ import annotations++import asyncio+from collections.abc import AsyncIterator++import pytest++import jarvis.speech.pipeline as pipeline_mod+from jarvis.core.events import (+ VoiceSessionEnded,+ VoiceSessionStarted,+ WakeCandidateDetected,+ WakeWordDetected,+)+from jarvis.core.protocols import AudioChunk+from jarvis.sessions.constants import HANGUP_TURN_COMPLETE+from jarvis.speech.pipeline import PipelineState, SpeechPipeline, TurnTakingState+++class _FakeTTS:+ name = "fake-tts"+ supports_streaming = False++ async def synthesize(self, text: str, **_kwargs) -> AsyncIterator[bytes]:+ if False: # pragma: no cover - protocol-shaped empty iterator+ yield text.encode()+++class _ControlledMic:+ def __init__(self, order: list[str]) -> None:+ self.order = order+ self.frames: asyncio.Queue[AudioChunk] = asyncio.Queue()+ self.entered = asyncio.Event()+ self.open_count = 0+ self.close_count = 0+ self.closed = False++ async def __aenter__(self) -> _ControlledMic:+ self.open_count += 1+ self.order.append("mic_open")+ self.entered.set()+ return self++ async def __aexit__(self, *_exc: object) -> bool:+ self.closed = True+ self.close_count += 1+ self.order.append("mic_close")+ return False++ async def stream(self) -> AsyncIterator[AudioChunk]:+ while True:+ yield await self.frames.get()+++class _NeverWake:+ async def detect(self, chunks: AsyncIterator[AudioChunk]) -> AsyncIterator[str]:+ async for _chunk in chunks:+ await asyncio.Event().wait()+ if False: # pragma: no cover - protocol-shaped async iterator+ yield ""+++class _OneHitWake:+ async def detect(self, chunks: AsyncIterator[AudioChunk]) -> AsyncIterator[str]:+ await anext(chunks)+ yield "jarvis"+++def _handoff_pipeline() -> SpeechPipeline:+ return SpeechPipeline(tts=_FakeTTS(), bus=None, enable_whisper_wake=False)+++async def test_capture_precedes_listening_signal_and_preserves_startup_audio(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ """The first frame after the bar appears must reach the voice engine.++ This pins the complete ordering contract that the prior realtime-only+ preroll missed: microphone open -> VoiceSessionStarted/LISTENING -> active+ engine. A frame injected while start subscribers are still running is the+ user's opening word and must be delivered unchanged through the buffer.+ """+ order: list[str] = []+ mic = _ControlledMic(order)+ monkeypatch.setattr(pipeline_mod, "MicrophoneCapture", lambda **_kwargs: mic)++ pipeline = SpeechPipeline(tts=_FakeTTS(), bus=None, enable_whisper_wake=False)+ pipeline._activation_allowed = lambda: True # type: ignore[method-assign]++ ended = asyncio.Event()+ captured: list[bytes] = []++ async def _publish(event) -> None: # noqa: ANN001+ if isinstance(event, VoiceSessionStarted):+ order.append("session_started")+ assert order[0] == "mic_open"+ await mic.frames.put(+ AudioChunk(+ pcm=b"opening-word",+ sample_rate=16_000,+ timestamp_ns=1,+ )+ )+ await asyncio.sleep(0)+ elif isinstance(event, VoiceSessionEnded):+ ended.set()++ async def _set_turn_state(state, **_kwargs) -> None: # noqa: ANN001+ if state is TurnTakingState.LISTENING:+ order.append("listening")++ async def _play_ack(*, ptt: bool) -> None:+ order.append("ack")++ async def _active_session(*, input_buffer=None) -> str: # noqa: ANN001+ order.append("active_session")+ assert input_buffer is not None+ chunk = await asyncio.wait_for(anext(input_buffer.stream()), timeout=0.5)+ captured.append(chunk.pcm)+ return HANGUP_TURN_COMPLETE++ async def _play_earcon(*_args, **_kwargs) -> None:+ return None++ pipeline._publish_event = _publish # type: ignore[method-assign]+ pipeline._set_turn_state = _set_turn_state # type: ignore[method-assign]+ pipeline._play_ack = _play_ack # type: ignore[method-assign]+ pipeline._active_session = _active_session # type: ignore[method-assign]+ pipeline._play_earcon = _play_earcon # type: ignore[method-assign]+ pipeline._call_event.set()++ state_task = asyncio.create_task(pipeline._state_loop())+ try:+ await asyncio.wait_for(ended.wait(), timeout=1.0)+ finally:+ state_task.cancel()+ with pytest.raises(asyncio.CancelledError):+ await state_task++ assert captured == [b"opening-word"]+ assert order.index("mic_open") < order.index("session_started")+ assert order.index("session_started") < order.index("active_session")+ assert "ack" not in order+ assert mic.closed is True+++async def test_replay_buffer_never_silently_drops_the_command_prefix() -> None:+ """Crossing the byte cap must fail honestly, not start from a tail frame."""+ buffer = pipeline_mod._SessionInputBuffer(max_buffer_bytes=4) # noqa: SLF001+ buffer.put(AudioChunk(pcm=b"aaaa", sample_rate=16_000, timestamp_ns=1))+ buffer.put(AudioChunk(pcm=b"bbbb", sample_rate=16_000, timestamp_ns=2))++ with pytest.raises(RuntimeError, match="refusing to drop the command prefix"):+ await anext(buffer.stream())+++async def test_replay_buffer_can_restart_for_realtime_to_pipeline_fallback() -> None:+ """A failed realtime consumer must leave the opening available to VAD."""+ marker = AudioChunk(pcm=b"opening", sample_rate=16_000, timestamp_ns=1)+ buffer = pipeline_mod._SessionInputBuffer(initial=(marker,)) # noqa: SLF001++ first = buffer.stream()+ assert await anext(first) is marker+ await first.aclose()++ fallback = buffer.stream()+ assert await anext(fallback) is marker+ await fallback.aclose()+++async def test_live_wake_capture_is_reused_without_second_microphone(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ """Wake and active capture share one physical microphone lease."""+ mic = _ControlledMic([])+ factory_calls = 0++ def _mic_factory(**_kwargs) -> _ControlledMic:+ nonlocal factory_calls+ factory_calls += 1+ return mic++ monkeypatch.setattr(pipeline_mod, "MicrophoneCapture", _mic_factory)+ pipeline = SpeechPipeline(tts=_FakeTTS(), bus=None, enable_whisper_wake=False)+ pipeline._openwakeword_enabled = True+ pipeline._whisper_wake_enabled = False+ pipeline._wake = _NeverWake() # type: ignore[assignment]++ wake_task = asyncio.create_task(pipeline._run_parallel_wake())+ await asyncio.wait_for(mic.entered.wait(), timeout=0.5)++ marker = AudioChunk(pcm=b"first-word", sample_rate=16_000, timestamp_ns=1)+ async with pipeline._capture_first_session_input() as input_buffer:+ assert factory_calls == 1+ assert mic.closed is False+ await mic.frames.put(marker)+ assert await asyncio.wait_for(anext(input_buffer.stream()), timeout=0.5) is marker++ await asyncio.wait_for(wake_task, timeout=0.5)+ assert factory_calls == 1+ assert mic.open_count == 1+ assert mic.close_count == 1+ assert pipeline._wake_capture_released.is_set()+++async def test_candidate_close_cancels_verification_and_retracts_bar(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ """The visible X cancels a candidate instead of starting a voice turn."""+ mic = _ControlledMic([])+ monkeypatch.setattr(pipeline_mod, "MicrophoneCapture", lambda **_kwargs: mic)+ pipeline = SpeechPipeline(tts=_FakeTTS(), bus=None, enable_whisper_wake=False)+ pipeline._openwakeword_enabled = True+ pipeline._whisper_wake_enabled = False+ pipeline._wake = _OneHitWake() # type: ignore[assignment]+ pipeline._should_show_optimistic_candidate = lambda: True # type: ignore[method-assign]++ verify_entered = asyncio.Event()+ verify_cancelled = asyncio.Event()+ candidate_flags: list[bool] = []+ authoritative_wakes: list[WakeWordDetected] = []++ async def _verify(_pcm: bytes) -> bool:+ verify_entered.set()+ try:+ await asyncio.Event().wait()+ finally:+ verify_cancelled.set()+ return True++ async def _publish(event) -> None: # noqa: ANN001+ if isinstance(event, WakeCandidateDetected):+ candidate_flags.append(event.active)+ elif isinstance(event, WakeWordDetected):+ authoritative_wakes.append(event)++ pipeline._verify_oww_hit = _verify # type: ignore[method-assign]+ pipeline._publish_event = _publish # type: ignore[method-assign]+ await mic.frames.put(+ AudioChunk(pcm=b"candidate", sample_rate=16_000, timestamp_ns=1)+ )++ wake_task = asyncio.create_task(pipeline._run_parallel_wake())+ await asyncio.wait_for(verify_entered.wait(), timeout=0.5)+ assert pipeline.is_session_active() is True+ pipeline.request_hangup()+ await asyncio.wait_for(wake_task, timeout=0.5)++ assert verify_cancelled.is_set()+ assert candidate_flags == [True, False]+ assert authoritative_wakes == []+ assert pipeline._call_event.is_set() is False+ assert pipeline._hangup_event.is_set() is False+ assert pipeline._external_hangup_pending.is_set() is False+ assert pipeline._wake_cancel_event.is_set() is False+++async def test_gate_drop_releases_unoffered_wake_capture(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ """A rejected activation cannot leave the wake microphone detector-dead."""+ mic = _ControlledMic([])+ monkeypatch.setattr(pipeline_mod, "MicrophoneCapture", lambda **_kwargs: mic)+ pipeline = SpeechPipeline(tts=_FakeTTS(), bus=None, enable_whisper_wake=False)+ pipeline._openwakeword_enabled = True+ pipeline._whisper_wake_enabled = False+ pipeline._wake = _NeverWake() # type: ignore[assignment]+ pipeline._activation_allowed = lambda: False # type: ignore[method-assign]+ started: list[VoiceSessionStarted] = []+ candidate_flags: list[bool] = []++ async def _publish(event) -> None: # noqa: ANN001+ if isinstance(event, VoiceSessionStarted):+ started.append(event)+ elif isinstance(event, WakeCandidateDetected):+ candidate_flags.append(event.active)++ pipeline._publish_event = _publish # type: ignore[method-assign]+ wake_task = asyncio.create_task(pipeline._run_parallel_wake())+ await asyncio.wait_for(mic.entered.wait(), timeout=0.5)+ state_task = asyncio.create_task(pipeline._state_loop())+ pipeline._call_event.set()+ try:+ await asyncio.wait_for(wake_task, timeout=0.5)+ finally:+ state_task.cancel()+ with pytest.raises(asyncio.CancelledError):+ await state_task++ assert started == []+ assert candidate_flags == [False]+ assert pipeline._state is PipelineState.IDLE+ assert mic.close_count == 1+ assert pipeline._wake_capture_released.is_set()+++async def test_close_after_confirmed_handoff_survives_state_loop_start() -> None:+ """A close in the wake-event dispatch window must not be cleared as stale."""+ pipeline = SpeechPipeline(tts=_FakeTTS(), bus=None, enable_whisper_wake=False)+ pipeline._activation_allowed = lambda: True # type: ignore[method-assign]+ buffer = pipeline_mod._SessionInputBuffer() # noqa: SLF001+ pipeline._wake_capture_released.clear()+ pipeline._wake_handoff_buffer = buffer+ pipeline._wake_handoff_ready.set()+ ended = asyncio.Event()+ active_calls = 0+ hard_hangup_flags: list[bool] = []++ async def _wake_owner() -> None:+ await buffer.released.wait()+ pipeline._wake_capture_released.set()++ async def _publish(event) -> None: # noqa: ANN001+ if isinstance(event, VoiceSessionEnded):+ ended.set()++ async def _active_session(*, input_buffer=None) -> str: # noqa: ANN001+ nonlocal active_calls+ active_calls += 1+ return HANGUP_TURN_COMPLETE++ async def _play_earcon(*_args, **_kwargs) -> None:+ return None++ def _post_hangup_lock_seconds() -> float:+ hard_hangup_flags.append(pipeline._explicit_hard_hangup)+ return 0.0++ pipeline._publish_event = _publish # type: ignore[method-assign]+ pipeline._active_session = _active_session # type: ignore[method-assign]+ pipeline._play_earcon = _play_earcon # type: ignore[method-assign]+ pipeline._post_hangup_lock_seconds = _post_hangup_lock_seconds # type: ignore[method-assign]+ assert pipeline.is_session_active() is True+ pipeline.request_hangup()+ pipeline._call_event.set()+ owner_task = asyncio.create_task(_wake_owner())+ state_task = asyncio.create_task(pipeline._state_loop())+ try:+ await asyncio.wait_for(ended.wait(), timeout=0.5)+ finally:+ state_task.cancel()+ with pytest.raises(asyncio.CancelledError):+ await state_task+ await owner_task++ assert active_calls == 0+ assert hard_hangup_flags == [True]+ assert pipeline._state is PipelineState.IDLE+ assert pipeline._wake_capture_released.is_set()+++async def test_stale_idle_hangup_does_not_cancel_a_later_wake_handoff() -> None:+ """Only a close after handoff belongs to the newly confirmed session."""+ pipeline = SpeechPipeline(tts=_FakeTTS(), bus=None, enable_whisper_wake=False)+ pipeline._activation_allowed = lambda: True # type: ignore[method-assign]+ pipeline.request_hangup()+ assert pipeline._wake_handoff_hangup_pending.is_set() is False++ buffer = pipeline_mod._SessionInputBuffer() # noqa: SLF001+ pipeline._wake_capture_released.clear()+ pipeline._wake_handoff_buffer = buffer+ pipeline._wake_handoff_ready.set()+ ended = asyncio.Event()+ active_calls = 0++ async def _wake_owner() -> None:+ await buffer.released.wait()+ pipeline._wake_capture_released.set()++ async def _publish(event) -> None: # noqa: ANN001+ if isinstance(event, VoiceSessionEnded):+ ended.set()++ async def _active_session(*, input_buffer=None) -> str: # noqa: ANN001+ nonlocal active_calls+ active_calls += 1+ return HANGUP_TURN_COMPLETE++ async def _play_earcon(*_args, **_kwargs) -> None:+ return None++ pipeline._publish_event = _publish # type: ignore[method-assign]+ pipeline._active_session = _active_session # type: ignore[method-assign]+ pipeline._play_earcon = _play_earcon # type: ignore[method-assign]+ owner_task = asyncio.create_task(_wake_owner())+ state_task = asyncio.create_task(pipeline._state_loop())+ pipeline._call_event.set()+ try:+ await asyncio.wait_for(ended.wait(), timeout=0.5)+ finally:+ state_task.cancel()+ with pytest.raises(asyncio.CancelledError):+ await state_task+ await owner_task++ assert active_calls == 1+ assert pipeline._state is PipelineState.IDLE+++async def test_claim_waits_for_wake_release_before_opening_fallback() -> None:+ """A closing wake stream must win the ownership race without overlap."""+ pipeline = _handoff_pipeline()+ pipeline._wake_capture_released.clear()++ async def _close_wake_capture() -> None:+ await pipeline._wake_stop_event.wait()+ pipeline._wake_capture_released.set()++ close_task = asyncio.create_task(_close_wake_capture())+ claimed = await asyncio.wait_for(+ pipeline._claim_wake_capture_for_session(), # noqa: SLF001+ timeout=0.5,+ )+ await close_task++ assert claimed is None+ assert pipeline._wake_stop_event.is_set() is False+++async def test_claim_yields_to_wake_capture_entering_before_fallback() -> None:+ """A scheduled wake open must win over a simultaneous hotkey fallback."""+ pipeline = _handoff_pipeline()+ pipeline._wake_capture_released.set()+ offered = pipeline_mod._SessionInputBuffer() # noqa: SLF001++ async def _enter_wake_capture() -> None:+ pipeline._wake_capture_released.clear()+ await pipeline._wake_stop_event.wait()+ pipeline._wake_handoff_buffer = offered+ pipeline._wake_handoff_ready.set()++ enter_task = asyncio.create_task(_enter_wake_capture())+ claimed = await asyncio.wait_for(+ pipeline._claim_wake_capture_for_session(), # noqa: SLF001+ timeout=0.5,+ )+ await enter_task++ assert claimed is offered+ assert pipeline._wake_handoff_buffer is None+ assert pipeline._wake_handoff_ready.is_set() is False+++async def test_rejected_activation_releases_late_wake_handoff() -> None:+ """Rejecting a call must not orphan a handoff offered one tick later."""+ pipeline = _handoff_pipeline()+ pipeline._wake_capture_released.clear()+ offered = pipeline_mod._SessionInputBuffer() # noqa: SLF001++ async def _offer_after_request() -> None:+ await pipeline._wake_stop_event.wait()+ pipeline._wake_handoff_buffer = offered+ pipeline._wake_handoff_ready.set()+ await offered.released.wait()+ pipeline._wake_capture_released.set()++ offer_task = asyncio.create_task(_offer_after_request())+ await asyncio.wait_for(+ pipeline._abort_pending_wake_handoff(), # noqa: SLF001+ timeout=0.5,+ )+ await offer_task++ assert offered.released.is_set()+ assert pipeline._wake_handoff_buffer is None+ assert pipeline._wake_handoff_ready.is_set() is False+++async def test_manually_fed_buffer_finishes_without_hanging_consumer() -> None:+ """A wake fanout EOF must terminate the handed-off stream promptly."""+ marker = AudioChunk(pcm=b"opening", sample_rate=16_000, timestamp_ns=1)+ buffer = pipeline_mod._SessionInputBuffer(initial=(marker,)) # noqa: SLF001+ buffer.finish()+ stream = buffer.stream()++ assert await anext(stream) is marker+ with pytest.raises(StopAsyncIteration):+ await anext(stream)+++async def test_buffer_resumes_metering_between_engine_consumers() -> None:+ """Realtime teardown must not flatten the bar before classic fallback."""+ from jarvis.audio import mic_level++ marker = AudioChunk(pcm=b"\x00\x10" * 320, sample_rate=16_000, timestamp_ns=1)+ buffer = pipeline_mod._SessionInputBuffer(initial=(marker,)) # noqa: SLF001+ mic_level.reset_for_tests()+ levels: list[float] = []+ unsubscribe = mic_level.subscribe(levels.append)+ try:+ realtime = buffer.stream()+ assert await anext(realtime) is marker+ await realtime.aclose()++ buffer.put(+ AudioChunk(+ pcm=b"\x00\x40" * 320,+ sample_rate=16_000,+ timestamp_ns=2,+ )+ )+ assert levels and levels[-1] > 0.0+ finally:+ unsubscribe()+ mic_level.reset_for_tests()diff --git a/tests/unit/speech/test_session_start_hangup.py b/tests/unit/speech/test_session_start_hangup.pyindex e96cbcc0b2..76088d858a 100644--- a/tests/unit/speech/test_session_start_hangup.py+++ b/tests/unit/speech/test_session_start_hangup.py@@ -7,6 +7,7 @@ from collections.abc import AsyncIteratorimport pytest+import jarvis.speech.pipeline as pipeline_modfrom jarvis.core.events import VoiceSessionEnded, VoiceSessionStartedfrom jarvis.sessions.constants import HANGUP_HOTKEYfrom jarvis.speech.pipeline import PipelineState, SpeechPipeline, TurnTakingState@@ -21,14 +22,37 @@ class _FakeTTS:yield text.encode()+class _PreopenedMic:+ def __init__(self) -> None:+ self.opened = False+ self.closed = False++ async def __aenter__(self) -> _PreopenedMic:+ self.opened = True+ return self++ async def __aexit__(self, *_exc: object) -> bool:+ self.closed = True+ return False++ async def stream(self): # noqa: ANN201+ await asyncio.Event().wait()+ if False: # pragma: no cover - protocol-shaped async iterator+ yield None++@pytest.mark.asyncio-async def test_close_during_session_started_dispatch_skips_audio_startup() -> None:+async def test_close_during_session_started_dispatch_closes_preopened_capture(+ monkeypatch: pytest.MonkeyPatch,+) -> None:"""A close accepted while a slow start subscriber runs must stay closed.- The bar is already interactive while VoiceSessionStarted is being- dispatched. Once the dispatch returns, a pending hangup must bypass the- LISTENING transition, acknowledgement, and microphone/session startup.+ Capture is intentionally armed before VoiceSessionStarted makes the bar+ interactive. Once dispatch returns, a pending hangup must close that+ capture promptly and bypass LISTENING, acknowledgement, and provider startup."""+ mic = _PreopenedMic()+ monkeypatch.setattr(pipeline_mod, "MicrophoneCapture", lambda **_kwargs: mic)pipeline = SpeechPipeline(tts=_FakeTTS(), bus=None, enable_whisper_wake=False)pipeline._activation_allowed = lambda: True # type: ignore[method-assign]@@ -62,7 +86,7 @@ async def test_close_during_session_started_dispatch_skips_audio_startup() -> Noasync def _play_ack(*, ptt: bool) -> None:ack_calls.append(ptt)- async def _active_session() -> str:+ async def _active_session(*, input_buffer=None) -> str: # noqa: ANN001active_calls.append(True)return "unexpected"@@ -86,6 +110,8 @@ async def test_close_during_session_started_dispatch_skips_audio_startup() -> Noassert ack_calls == []assert active_calls == []+ assert mic.opened is True+ assert mic.closed is Trueassert pipeline._state is PipelineState.IDLEassert len(ended_events) == 1assert ended_events[0].hangup_reason == HANGUP_HOTKEYdiff --git a/tests/unit/ui/test_orb_bus_bridge_wake_candidate.py b/tests/unit/ui/test_orb_bus_bridge_wake_candidate.pyindex 0a83b8d6b0..0f12934fdc 100644--- a/tests/unit/ui/test_orb_bus_bridge_wake_candidate.py+++ b/tests/unit/ui/test_orb_bus_bridge_wake_candidate.py@@ -33,7 +33,11 @@ except ModuleNotFoundError: # pragma: no coverallow_module_level=True,)-from jarvis.core.events import WakeCandidateDetected, WakeWordDetected+from jarvis.core.events import ( # noqa: E402+ VoiceSessionStarted,+ WakeCandidateDetected,+ WakeWordDetected,+)class _FakeBus:@@ -118,3 +122,73 @@ async def test_candidate_does_not_suppress_real_wake_greet() -> None:assert ("show", "listen") in orb.callsassert ("play_animation", "wave") in orb.calls+++async def test_confirmed_preview_retracts_when_no_session_starts() -> None:+ """A gate drop after WakeWordDetected must restore the pre-wake state."""+ orb = _FakeOrb()+ bridge = _bridge(orb, hide_on_idle=True)++ await bridge._on_wake_candidate(WakeCandidateDetected(active=True)) # noqa: SLF001+ await bridge._on_wake_word_detected(WakeWordDetected(keyword="jarvis")) # noqa: SLF001+ orb.calls.clear()+ await bridge._on_wake_candidate(WakeCandidateDetected(active=False)) # noqa: SLF001++ assert ("hide", None) in orb.calls+ assert bridge._last_state == "IDLE" # noqa: SLF001+++async def test_retraction_does_not_hide_an_authoritative_session() -> None:+ orb = _FakeOrb()+ bridge = _bridge(orb, hide_on_idle=True)++ await bridge._on_wake_candidate(WakeCandidateDetected(active=True)) # noqa: SLF001+ await bridge._on_session_started( # noqa: SLF001+ VoiceSessionStarted(session_id="live")+ )+ orb.calls.clear()+ await bridge._on_wake_candidate(WakeCandidateDetected(active=False)) # noqa: SLF001++ assert ("hide", None) not in orb.calls+ assert bridge._last_state == "LISTENING" # noqa: SLF001+++async def test_candidate_forwards_wake_mic_level_before_session_state() -> None:+ """A visible candidate bar must react to the mic that revealed it."""+ orb = _FakeOrb()+ bridge = _bridge(orb, hide_on_idle=True)+ # A prior TTS/earcon level must not keep ownership of a newly visible+ # input affordance for the old 500 ms recency window.+ bridge._last_tts_level_t = float("inf") # noqa: SLF001++ await bridge._on_wake_candidate(WakeCandidateDetected(active=True)) # noqa: SLF001+ orb.calls.clear()+ bridge._on_mic_level(0.5) # noqa: SLF001++ assert ("set_level", 0.5) in orb.calls+++async def test_rejected_candidate_stops_forwarding_wake_mic_level() -> None:+ orb = _FakeOrb()+ bridge = _bridge(orb, hide_on_idle=True)++ await bridge._on_wake_candidate(WakeCandidateDetected(active=True)) # noqa: SLF001+ await bridge._on_wake_candidate(WakeCandidateDetected(active=False)) # noqa: SLF001+ orb.calls.clear()+ bridge._on_mic_level(0.5) # noqa: SLF001++ assert ("set_level", 0.5) not in orb.calls++[email protected]("state", ["THINKING", "SPEAKING"])+async def test_stale_candidate_never_overrides_active_output_bars(state: str) -> None:+ """A defensive stale latch must not clobber thinking or TTS animation."""+ orb = _FakeOrb()+ bridge = _bridge(orb, hide_on_idle=True)+ bridge._wake_candidate_active = True # noqa: SLF001+ bridge._last_state = state # noqa: SLF001+ bridge._last_tts_level_t = 0.0 # noqa: SLF001++ bridge._on_mic_level(0.5) # noqa: SLF001++ assert ("set_level", 0.5) not in orb.callsdiff --git a/ui/orb/bus_bridge.py b/ui/orb/bus_bridge.pyindex 03ec9b6fb2..79bef32bdc 100644--- a/ui/orb/bus_bridge.py+++ b/ui/orb/bus_bridge.py@@ -37,28 +37,26 @@ from typing import TYPE_CHECKING, Anyfrom jarvis.core.events import (AudioOutFirst,- ListeningStarted,JarvisAgentBackgroundCompleted,+ ListeningStarted,OrbResetRequested,ResponseGenerated,ShowWindowRequested,SystemStateChanged,TranscriptionUpdate,UserVisibleFeedback,- WakeCandidateDetected,- WakeWordDetected,VoiceBootStatus,VoiceMuteChanged,VoiceMuteToggleRequested,VoiceSessionEnded,VoiceSessionStarted,+ WakeCandidateDetected,+ WakeWordDetected,)-from ui.orb.animations import IDLE_ANIMATION_POOLif TYPE_CHECKING:from jarvis.core.bus import EventBus-from ui.orb.overlay import OrbOverlay@@ -164,8 +162,8 @@ class OrbBusBridge:def __init__(self,- bus: "EventBus",- orb: "OrbOverlay",+ bus: EventBus,+ orb: OrbOverlay,idle_animations_enabled: bool = True,hide_on_idle: bool = True,) -> None:@@ -179,6 +177,13 @@ class OrbBusBridge:# actually producing sound" instead — whoever makes sound drives bars.self._last_tts_level_t = 0.0self._last_state: str = "IDLE"+ self._voice_session_active = False+ self._wake_preview_origin_state = "IDLE"+ # A verified-enough wake candidate can reveal the listening bar before+ # the authoritative session state arrives. The wake microphone is+ # already capturing during that interval, so its live levels must be+ # allowed through without mutating the state-machine edge.+ self._wake_candidate_active = Falseself._idle_task: asyncio.Task | None = Noneself._idle_enabled = idle_animations_enabledself._hide_on_idle = hide_on_idle@@ -453,17 +458,29 @@ class OrbBusBridge:Deliberately does NOT mutate ``_last_state`` on show: the authoritative``_on_wake_word_detected`` that follows a confirmed wake must still seethe IDLE→LISTENING edge so it plays the greet 'wave' and sets the state- cleanly. Until that fires the equalizer mic-feed stays gated off (<1 s).+ cleanly. A separate candidate latch enables the equalizer during this+ preview without forging an authoritative session state."""if event.active:# Incoming speech candidate — cancel any pending idle hide and pop# the bar. _last_state untouched (see docstring).+ if not self._wake_candidate_active:+ self._wake_preview_origin_state = self._last_state+ self._wake_candidate_active = True+ # A fresh input affordance supersedes stale output recency from the+ # preceding turn. Startup no longer plays an ACK, so retaining the+ # old 500 ms TTS ownership window would keep truthful mic bars dark.+ self._last_tts_level_t = 0.0self._cancel_idle_scheduler()self._orb.show(mode="listen")return- # Retract a rejected candidate. A real session owns the bar → leave it.- if self._last_state in _ACTIVE_VOICE_STATES:+ self._wake_candidate_active = False+ # Retract a rejected or gate-dropped preview. Only an authoritative+ # VoiceSessionStarted owns the bar; WakeWordDetected alone is still a+ # pre-session preview and can legitimately be rolled back.+ if self._voice_session_active:return+ self._last_state = self._wake_preview_origin_stateif self._hide_on_idle:self._orb.hide()else:@@ -473,8 +490,12 @@ class OrbBusBridge:"""Pop the orb on the earliest confirmed wake signal."""log.info("OrbBridge._on_wake_word_detected: keyword=%s", event.keyword)prev_state = self._last_state+ if not self._wake_candidate_active:+ self._wake_preview_origin_state = prev_stateself._last_state_trace_id = str(event.trace_id)self._suppress_show_until_session = False+ self._wake_candidate_active = False+ self._last_tts_level_t = 0.0self._last_state = "LISTENING"self._orb.show(mode="listen")if prev_state in ("IDLE", "ERROR", "PAUSED"):@@ -509,8 +530,11 @@ class OrbBusBridge:the very first word."""log.info("OrbBridge._on_session_started: session=%s", event.session_id)+ self._voice_session_active = Trueprev_state = self._last_stateself._suppress_show_until_session = False+ self._wake_candidate_active = False+ self._last_tts_level_t = 0.0self._last_state = "LISTENING"# Enter the listening look now — robust to a deduplicated LISTENING state.self._orb.show(mode="listen")@@ -535,6 +559,8 @@ class OrbBusBridge:event (preserving the existing salute/grace animation); the latch onlyprevents the resurrection that follows."""+ self._voice_session_active = False+ self._wake_candidate_active = Falselog.info("OrbBridge._on_session_ended: session=%s reason=%s — late active ""states suppressed until next wake.",@@ -594,6 +620,11 @@ class OrbBusBridge:# orb is clickable, so the loop is always captured in time.self._remember_loop()state = event.new_state+ if state != "IDLE":+ # Any authoritative non-idle state supersedes a visual-only wake+ # preview. Keeping the latch beyond this edge could let a late mic+ # sample overwrite THINKING/SPEAKING bars.+ self._wake_candidate_active = False# ADR-0016: remember the trace_id so the next visibility snapshot# can correlate back to the state-transition that triggered it.self._last_state_trace_id = str(event.trace_id)@@ -958,7 +989,11 @@ class OrbBusBridge:state is LISTENING. Works for whichever surface is current."""if time.monotonic() - self._last_tts_level_t < self._TTS_OWNS_BARS_S:return # TTS is making sound → it drives the bars, not the silent mic- if self._last_state != "LISTENING":+ candidate_listening = (+ self._wake_candidate_active+ and self._last_state in {"IDLE", "ERROR", "PAUSED"}+ )+ if self._last_state != "LISTENING" and not candidate_listening:returntry:self._orb.set_level(level)