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(audio): recover wake mic after reopen failure
Ruben Lütkedc16c46dac76
diff --git a/jarvis/audio/capture.py b/jarvis/audio/capture.pyindex 720648ee5b..35350f33cd 100644--- a/jarvis/audio/capture.py+++ b/jarvis/audio/capture.py@@ -187,8 +187,8 @@ def _fallback_input_devices(primary_idx: int) -> list[int]:if not (0 <= primary_idx < len(devices)):return []primary = devices[primary_idx]- primary_name_root = str(primary.get("name", "")).split("(")[0].strip()- if not primary_name_root:+ primary_name = " ".join(str(primary.get("name", "")).casefold().split())+ if not primary_name:return []# Other indices with the same device name, sorted by host API preference.matches: list[tuple[int, dict]] = []@@ -197,8 +197,12 @@ def _fallback_input_devices(primary_idx: int) -> list[int]:continueif dev.get("max_input_channels", 0) <= 0:continue- name = str(dev.get("name", ""))- if primary_name_root.lower() not in name.lower():+ # Host-API twins carry the same PortAudio device name. Comparing the+ # complete normalized name matters: common labels such as+ # ``Microphone (External)`` and ``Microphone (Built-in)`` are different+ # physical inputs and must not be cached as interchangeable twins.+ name = " ".join(str(dev.get("name", "")).casefold().split())+ if name != primary_name:continue# Same WDM-KS exclusion as _resolve_input_device — fallbacks that# the resolver would never have picked in the first place should@@ -258,6 +262,83 @@ def _os_default_input_name(return name+def _rank_input_device_candidates(+ devices: Sequence[dict],+ hostapis: Sequence[dict],+ priority: Sequence[str] | None = None,+) -> list[tuple[int, dict]]:+ """Return every usable input device in automatic-selection order.++ Keeping the complete ranking separate from ``_resolve_input_device`` lets+ stream-open recovery move to another physical microphone without inventing+ a second, subtly different set of platform filters. The first entry remains+ the normal resolver choice; later entries are recovery candidates only.+ """+ user_priority = tuple(p for p in (priority or ()) if p)+ os_default_name = _os_default_input_name(devices, hostapis)+ effective_priority = (+ (*user_priority, os_default_name) if os_default_name else user_priority+ )++ candidates: list[tuple[int, dict]] = []+ for idx, dev in enumerate(devices):+ if dev.get("max_input_channels", 0) <= 0:+ continue+ name = str(dev.get("name", ""))+ if any(blocked.lower() in name.lower() for blocked in _BLOCKED_INPUT_SUBSTRINGS):+ continue+ if is_legacy_primary_mapper(idx, hostapis, devices, output=False):+ continue+ hostapi_idx = dev.get("hostapi", -1)+ if 0 <= hostapi_idx < len(hostapis):+ hostapi_name = hostapis[hostapi_idx].get("name", "")+ if hostapi_name in _HOSTAPI_BLOCKLIST:+ continue+ candidates.append((idx, dev))++ def _hostapi_rank(entry: tuple[int, dict]) -> int:+ hostapi_idx = entry[1].get("hostapi", -1)+ if 0 <= hostapi_idx < len(hostapis):+ hostapi_name = hostapis[hostapi_idx].get("name", "")+ return _HOSTAPI_PREFERENCE.get(hostapi_name, 99)+ return 99++ def _name_rank(entry: tuple[int, dict]) -> int:+ low = str(entry[1].get("name", "")).lower()+ for rank, substring in enumerate(effective_priority):+ if substring.lower() in low:+ return rank+ rank = len(effective_priority) + len(_INPUT_PRIORITY)+ for generic_rank, substring in enumerate(_INPUT_PRIORITY):+ if substring.lower() in low:+ rank = len(effective_priority) + generic_rank+ break+ if any(virtual.lower() in low for virtual in _INPUT_DEPRIORITIZE):+ rank += 1000+ return rank++ candidates.sort(key=lambda entry: (_name_rank(entry), _hostapi_rank(entry)))+ return candidates+++def _ranked_input_device_indices(+ priority: Sequence[str] | None = None,+) -> list[int]:+ """Enumerate usable microphones in resolver order for open recovery."""+ try:+ devices = sd.query_devices()+ hostapis = sd.query_hostapis()+ except Exception as exc: # noqa: BLE001 - recovery remains best-effort+ _log.debug("Mic recovery enumeration failed: {}", exc)+ return []+ return [+ idx+ for idx, _device in _rank_input_device_candidates(+ devices, hostapis, priority+ )+ ]++# Resolve-result cache: (device_spec, priority) -> (resolved, monotonic_ts).# WHY: resolving "auto-headset" enumerates every audio device (~0.4s on# Windows) and used to run on EVERY MicrophoneCapture construction — the@@ -442,8 +523,6 @@ def _resolve_input_device()# Fall through to the auto-headset heuristic below.- user_priority = tuple(p for p in (priority or ()) if p)-try:devices = sd.query_devices()hostapis = sd.query_hostapis()@@ -455,73 +534,10 @@ def _resolve_input_device()return None- # "Your device first": prefer the user's OS-selected default MICROPHONE over- # the generic guesses, UNLESS it is a loopback/monitor, the localized virtual- # mapper, or a virtual/AI mic that can go silent — then fall back to the- # heuristic so the wake loop gets a real mic. Injected as a NAME so the sort- # still picks the mic's best host-API twin (MME) and skips WDM-KS. Ranks- # BELOW an explicit input_device_priority.- os_default_name = _os_default_input_name(devices, hostapis)- effective_priority = (- (*user_priority, os_default_name) if os_default_name else user_priority- )-- candidates: list[tuple[int, dict]] = []- for idx, dev in enumerate(devices):- if dev.get("max_input_channels", 0) <= 0:- continue- name = str(dev.get("name", ""))- if any(blocked.lower() in name.lower() for blocked in _BLOCKED_INPUT_SUBSTRINGS):- continue- # Locale-independent skip of the MME "Sound Mapper" / DirectSound- # "Primary Sound Driver" recording mapper (translated display name).- if is_legacy_primary_mapper(idx, hostapis, devices, output=False):- continue- # Drop hostapis that can't serve PortAudio blocking-stream I/O —- # WDM-KS makes InputStream.start() raise PaErrorCode -9996 even- # when the device enumerates cleanly. Filtering here means the- # resolver returns None (→ system default) instead of handing- # back a broken index that the wake loop would loop-crash on.- hostapi_idx = dev.get("hostapi", -1)- if 0 <= hostapi_idx < len(hostapis):- hostapi_name = hostapis[hostapi_idx].get("name", "")- if hostapi_name in _HOSTAPI_BLOCKLIST:- continue- candidates.append((idx, dev))-- def _hostapi_rank(entry: tuple[int, dict]) -> int:- hostapi_idx = entry[1].get("hostapi", -1)- if 0 <= hostapi_idx < len(hostapis):- hostapi_name = hostapis[hostapi_idx].get("name", "")- return _HOSTAPI_PREFERENCE.get(hostapi_name, 99)- return 99-- def _name_rank(entry: tuple[int, dict]) -> int:- low = str(entry[1].get("name", "")).lower()- # Precedence: explicit user priority, then the OS-selected default mic- # (both carried in ``effective_priority``), then the generic list. A- # user / OS-default match ranks ahead of every generic match AND is exempt- # from the virtual/AI-mic deprioritize below — the OS-default name is only- # ever a real mic (``_os_default_input_name`` rejects virtual ones), and- # an explicit user name is honored deliberately.- for r, sub in enumerate(effective_priority):- if sub.lower() in low:- return r- rank = len(effective_priority) + len(_INPUT_PRIORITY)- for r, sub in enumerate(_INPUT_PRIORITY):- if sub.lower() in low:- rank = len(effective_priority) + r- break- # Push virtual / AI mics (NVIDIA Broadcast, voice changers, virtual- # cables) BEHIND every real hardware mic — they often deliver silence- # when their companion app is closed (see _INPUT_DEPRIORITIZE). They are- # not blocked, only ranked last, so they still serve as a fallback when- # no real mic exists.- if any(v.lower() in low for v in _INPUT_DEPRIORITIZE):- rank += 1000- return rank-- candidates.sort(key=lambda entry: (_name_rank(entry), _hostapi_rank(entry)))+ # The complete ranking is shared with open-time recovery. Its first entry+ # preserves the resolver contract: explicit user priority, then the usable+ # OS default, then generic real-microphone heuristics and host-API ranking.+ candidates = _rank_input_device_candidates(devices, hostapis, priority)if candidates:chosen_idx, chosen_dev = candidates[0]chosen_hostapi_idx = chosen_dev.get("hostapi", -1)@@ -597,6 +613,11 @@ class MicrophoneCapture:self._device = cachedelse:self._device = _resolve_input_device(device, self._device_priority)+ # Keep the originally resolved microphone separate from the currently+ # open one. If recovery temporarily moves to another physical input,+ # the preferred device is tried again on the next capture/restart.+ self._preferred_device: int | str | None = self._device+ self._using_physical_fallback = Falseself._sample_rate = sample_rateself._blocksize = blocksizeself._channels = channels@@ -714,39 +735,58 @@ class MicrophoneCapture:self._drops += 1async def _try_open_stream(self) -> None:- """Open loop with host API fallback. Raises on total failure.+ """Open the preferred mic, then recover through safe alternatives.- Extracted from __aenter__ so the stall watchdog can reuse the same- open logic for restarts.+ Extracted from ``__aenter__`` so the stall watchdog can reuse the same+ logic. Automatic/name-based selection may fail over to another physical+ input; an explicit numeric device remains pinned."""self._require_microphone_access()- attempts: list[int | str | None] = [self._device]+ preferred_attempts: list[int | str | None] = [self._preferred_device]try:- if isinstance(self._device, int):- attempts.extend(_fallback_input_devices(self._device))+ if isinstance(self._preferred_device, int):+ for fallback in _fallback_input_devices(self._preferred_device):+ if fallback not in preferred_attempts:+ preferred_attempts.append(fallback)except Exception as exc: # noqa: BLE001- _log.debug("Mic fallback enumeration failed: {}", exc)- # Phase one intentionally preserves the Windows recovery contract: try- # 16 kHz on the selected endpoint and every same-device MME/DirectSound- # twin before changing the hardware rate. Those host APIs perform their- # own reliable conversion and remain preferable to a WASAPI native-rate- # stream. Only after every endpoint rejects the contract rate do we try- # CoreAudio/ALSA/native rates and resample locally on the CPU.- def _open_candidates() -> Iterator[tuple[int | str | None, int]]:- for attempt in attempts:- yield attempt, self._sample_rate- # This half of the generator is lazy: device-rate queries do not run- # on the normal 16 kHz fast path. They begin only after every target-- # rate endpoint above failed.- for attempt in attempts:+ _log.debug("Mic host-API fallback enumeration failed: {}", exc)++ # A numeric device is an explicit pin. ``None`` and string specs are+ # automatic/name-based choices, so they may move to another real input+ # when CoreAudio, ALSA, or a Windows endpoint refuses to reopen after a+ # completed voice turn.+ # Preserve the Windows recovery contract within each physical group:+ # try 16 kHz on the selected endpoint and each same-device host-API twin+ # before changing the hardware rate. Only after preferred-device target+ # and native rates fail do we move to another physical microphone.+ def _open_candidates() -> Iterator[tuple[int | str | None, int, bool]]:+ for attempt in preferred_attempts:+ yield attempt, self._sample_rate, False+ # Lazy: native-rate queries and cross-device enumeration stay off+ # the normal 16 kHz fast path and therefore do not add latency to a+ # healthy wake→session microphone handover.+ for attempt in preferred_attempts:for rate in _native_input_rates(attempt, self._sample_rate):- yield attempt, rate+ yield attempt, rate, False+ if isinstance(self._device_spec, int):+ return+ alternate_attempts = [+ candidate+ for candidate in _ranked_input_device_indices(self._device_priority)+ if candidate not in preferred_attempts+ ]+ for attempt in alternate_attempts:+ yield attempt, self._sample_rate, True+ for attempt in alternate_attempts:+ for rate in _native_input_rates(attempt, self._sample_rate):+ yield attempt, rate, Truelast_error: Exception | None = Noneattempt_count = 0- for attempt, capture_rate in _open_candidates():+ for attempt, capture_rate, physical_fallback in _open_candidates():attempt_count += 1self._require_microphone_access()+ stream = Nonetry:capture_blocksize = (0@@ -779,10 +819,20 @@ class MicrophoneCapture:stream.start()self._stream = streamself._device = attempt- if isinstance(self._device_spec, str):+ self._using_physical_fallback = physical_fallback+ if not physical_fallback:+ self._preferred_device = attempt+ if isinstance(self._device_spec, str) and not physical_fallback:_touch_resolve_cache(self._device_spec, self._device_priority, attempt)+ if physical_fallback:+ _log.warning(+ "Preferred microphone device(s) {} could not be opened; "+ "using alternative input device {} for this capture.",+ preferred_attempts,+ attempt,+ )_log.info("Mic opened (device={}, capture_sr={}, output_sr={}, ""blocksize={}, dtype={}).",@@ -795,6 +845,18 @@ class MicrophoneCapture:returnexcept Exception as exc: # noqa: BLE001last_error = exc+ # ``InputStream`` can be constructed before ``start`` fails.+ # Close that partial stream so PortAudio/CoreAudio does not keep+ # a poisoned handle across every subsequent wake-loop retry.+ if stream is not None:+ try:+ stream.close()+ except Exception as close_exc: # noqa: BLE001+ _log.debug(+ "Mic-Open cleanup for device={} ignored ({}).",+ attempt,+ close_exc,+ )# A failed open means the cached/resolved device may be gone —# force the next construction through a fresh full resolve._invalidate_resolve_cache()@@ -805,6 +867,7 @@ class MicrophoneCapture:capture_rate,exc,)+ self._using_physical_fallback = False_log.error("Mic-Open failed completely ({} attempt(s)) — last error: {}",attempt_count,@@ -840,7 +903,10 @@ class MicrophoneCapture:# The stream is delivering — keep the resolve cache fresh so a# capture constructed moments after this one closes (the# wake→session handover) skips the ~0.4s device enumeration.- if isinstance(self._device_spec, str):+ if (+ isinstance(self._device_spec, str)+ and not self._using_physical_fallback+ ):_touch_resolve_cache(self._device_spec, self._device_priority, self._device)diff --git a/tests/unit/audio/test_capture_native_rate.py b/tests/unit/audio/test_capture_native_rate.pyindex a6bfe9548f..1ef4308d9d 100644--- a/tests/unit/audio/test_capture_native_rate.py+++ b/tests/unit/audio/test_capture_native_rate.py@@ -43,7 +43,7 @@ async def test_native_rate_fallback_resamples_to_16khz_contract(monkeypatch.setattr(capture.sd, "query_devices", fake_query_devices)monkeypatch.setattr(capture.sd, "InputStream", fake_input_stream)- mic = capture.MicrophoneCapture(device=3)+ mic = capture.MicrophoneCapture(device=3, access_gate=lambda: True)mic._loop = asyncio.get_running_loop()await mic._try_open_stream()@@ -90,7 +90,7 @@ async def test_16khz_host_api_twin_precedes_native_rate_fallback(monkeypatch) ->monkeypatch.setattr(capture.sd, "query_devices", unexpected_native_rate_query)monkeypatch.setattr(capture.sd, "InputStream", fake_input_stream)- mic = capture.MicrophoneCapture(device=10)+ mic = capture.MicrophoneCapture(device=10, access_gate=lambda: True)await mic._try_open_stream()assert open_calls == [(10, 16_000), (11, 16_000)]diff --git a/tests/unit/audio/test_capture_recovery.py b/tests/unit/audio/test_capture_recovery.pynew file mode 100644index 0000000000..dc768befd2--- /dev/null+++ b/tests/unit/audio/test_capture_recovery.py@@ -0,0 +1,166 @@+"""Regression coverage for microphone reopen failures between voice turns."""++from __future__ import annotations++from types import SimpleNamespace++import pytest++from jarvis.audio import capture++[email protected](autouse=True)+def _clean_resolve_cache() -> None:+ capture._invalidate_resolve_cache()+ yield+ capture._invalidate_resolve_cache()+++def _input_devices() -> list[dict]:+ return [+ {+ "name": "Microphone (Preferred External)",+ "max_input_channels": 1,+ "max_output_channels": 0,+ "hostapi": 0,+ "default_samplerate": 48_000,+ },+ {+ "name": "Built-in Speakers",+ "max_input_channels": 0,+ "max_output_channels": 2,+ "hostapi": 0,+ "default_samplerate": 48_000,+ },+ {+ "name": "Microphone (Backup Built-in)",+ "max_input_channels": 1,+ "max_output_channels": 0,+ "hostapi": 0,+ "default_samplerate": 48_000,+ },+ ]+++def _install_device_enumeration(monkeypatch, hostapi_name: str) -> list[dict]:+ devices = _input_devices()++ def fake_query_devices(device=None, kind=None):+ if device is None:+ return devices+ assert kind == "input"+ return devices[device]++ monkeypatch.setattr(capture.sd, "query_devices", fake_query_devices)+ monkeypatch.setattr(capture.sd, "query_hostapis", lambda: [{"name": hostapi_name}])+ monkeypatch.setattr(capture.sd, "default", SimpleNamespace(device=(0, 1)))+ return devices++[email protected]("hostapi_name", ["Core Audio", "ALSA", "Windows WASAPI"])+async def test_automatic_capture_recovers_on_another_physical_microphone(+ monkeypatch, hostapi_name: str+) -> None:+ """A post-turn PortAudio open failure must not kill always-on wake."""+ _install_device_enumeration(monkeypatch, hostapi_name)+ open_calls: list[tuple[int, int]] = []+ closed_calls: list[tuple[int, int]] = []++ class FakeStream:+ def __init__(self, device: int, rate: int) -> None:+ self.device = device+ self.rate = rate++ def start(self) -> None:+ if self.device == 0:+ raise RuntimeError("Internal PortAudio error [PaErrorCode -9986]")++ def close(self) -> None:+ closed_calls.append((self.device, self.rate))++ def fake_input_stream(**kwargs):+ call = (kwargs["device"], kwargs["samplerate"])+ open_calls.append(call)+ return FakeStream(*call)++ monkeypatch.setattr(capture.sd, "InputStream", fake_input_stream)++ mic = capture.MicrophoneCapture(device="auto-headset", access_gate=lambda: True)+ await mic._try_open_stream()++ assert open_calls == [+ (0, 16_000),+ (0, 48_000),+ (0, 44_100),+ (2, 16_000),+ ]+ assert closed_calls == open_calls[:3]+ assert mic._device == 2+ assert mic._using_physical_fallback is True++ # A temporary physical fallback is deliberately not cached forever. The+ # preferred/OS-default microphone is reconsidered for the next voice turn.+ assert capture._cached_resolve("auto-headset", ()) is None+ next_mic = capture.MicrophoneCapture(device="auto-headset")+ assert next_mic._device == 0+++async def test_explicit_numeric_microphone_does_not_switch_hardware(+ monkeypatch,+) -> None:+ """A numeric device is an intentional strict pin, even during recovery."""+ _install_device_enumeration(monkeypatch, "Core Audio")+ open_calls: list[tuple[int, int]] = []++ class FailingStream:+ def __init__(self, device: int, rate: int) -> None:+ self.device = device+ self.rate = rate++ def start(self) -> None:+ raise RuntimeError("Internal PortAudio error [PaErrorCode -9986]")++ def close(self) -> None:+ pass++ def fake_input_stream(**kwargs):+ call = (kwargs["device"], kwargs["samplerate"])+ open_calls.append(call)+ return FailingStream(*call)++ monkeypatch.setattr(capture.sd, "InputStream", fake_input_stream)++ mic = capture.MicrophoneCapture(device=0, access_gate=lambda: True)+ with pytest.raises(RuntimeError, match="PaErrorCode -9986"):+ await mic._try_open_stream()++ assert open_calls == [(0, 16_000), (0, 48_000), (0, 44_100)]+++async def test_healthy_automatic_open_keeps_recovery_enumeration_lazy(+ monkeypatch,+) -> None:+ """Recovery must not reintroduce latency on the successful 16 kHz path."""+ _install_device_enumeration(monkeypatch, "Core Audio")++ class HealthyStream:+ def start(self) -> None:+ pass++ mic = capture.MicrophoneCapture(device="auto-headset", access_gate=lambda: True)+ monkeypatch.setattr(capture, "_fallback_input_devices", lambda _device: [])++ def unexpected_recovery_enumeration(_priority):+ raise AssertionError("physical fallback enumeration must stay lazy")++ monkeypatch.setattr(capture, "_ranked_input_device_indices", unexpected_recovery_enumeration)+ monkeypatch.setattr(capture.sd, "InputStream", lambda **_kwargs: HealthyStream())++ await mic._try_open_stream()++ assert mic._device == 0+ assert mic._using_physical_fallback is False+ assert capture._cached_resolve("auto-headset", ()) == 0