Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.
Python agent-orchestrationai-agentai-coding-assistantclaude-codecoding-agentcomputer-usedesktop-applocal-firstmcppersonal-jarvisvoice-assistantvoice-coding
feat(audio): auto-headset honors the user's OS-selected default device first
Ruben Lütkeef62580b7e33
diff --git a/jarvis/audio/capture.py b/jarvis/audio/capture.pyindex 426fa0fb61..4fec66a846 100644--- a/jarvis/audio/capture.py+++ b/jarvis/audio/capture.py@@ -180,6 +180,43 @@ def _fallback_input_devices(primary_idx: int) -> list[int]:return [idx for idx, _ in matches]+def _os_default_input_name(+ devices: Sequence[dict], hostapis: Sequence[dict]+) -> str | None:+ """Name of the user's OS-selected default INPUT (microphone) device, when it+ is a real, usable mic — else None.++ The "your device first" contract for capture: ``auto-headset`` prefers the+ user's system default microphone, EXCEPT when that default is a device the+ resolver exists to avoid — a loopback/monitor source, the localized virtual+ mapper, or a virtual/AI mic (NVIDIA Broadcast, VB-Audio, …) that goes silent+ when its companion app is closed. In those cases this returns None so the+ resolver falls back to the generic heuristic and picks a real hardware mic+ instead of feeding digital silence to the wake loop. The NAME is returned so+ the candidate sort still picks the mic's best host-API twin (MME/DirectSound+ resample to 16 kHz) and skips WDM-KS. A missing default / absent sounddevice+ yields None.+ """+ try:+ default_in = sd.default.device[0]+ except Exception: # noqa: BLE001 — no default / no sounddevice -> no preference+ return None+ if not isinstance(default_in, int) or not (0 <= default_in < len(devices)):+ return None+ dev = devices[default_in]+ name = str(dev.get("name", ""))+ if not name or dev.get("max_input_channels", 0) <= 0:+ return None+ low = name.lower()+ if any(b.lower() in low for b in _BLOCKED_INPUT_SUBSTRINGS):+ return None+ if is_legacy_primary_mapper(default_in, hostapis, devices, output=False):+ return None+ if any(v.lower() in low for v in _INPUT_DEPRIORITIZE):+ return None # virtual/AI mic as OS default -> fall back to a real mic+ return name++def _resolve_input_device(device: int | str | None,priority: Sequence[str] | None = None,@@ -217,6 +254,17 @@ def _resolve_input_device(_log.warning("Mic-Resolve: sd.query_devices() failed ({}). Falling back to system default.", exc)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:@@ -249,17 +297,19 @@ def _resolve_input_device(def _name_rank(entry: tuple[int, dict]) -> int:low = str(entry[1].get("name", "")).lower()- # User-configured names take strict precedence and are trusted as an- # explicit choice: they rank ahead of every generic match AND are exempt- # from the virtual/AI-mic deprioritize below (if the user deliberately- # names their virtual cable / broadcast mic, honor it).- for r, sub in enumerate(user_priority):+ # 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(user_priority) + len(_INPUT_PRIORITY)+ rank = len(effective_priority) + len(_INPUT_PRIORITY)for r, sub in enumerate(_INPUT_PRIORITY):if sub.lower() in low:- rank = len(user_priority) + r+ rank = len(effective_priority) + rbreak# Push virtual / AI mics (NVIDIA Broadcast, voice changers, virtual# cables) BEHIND every real hardware mic — they often deliver silencediff --git a/jarvis/audio/player.py b/jarvis/audio/player.pyindex 260479e1d8..75fbc61de0 100644--- a/jarvis/audio/player.py+++ b/jarvis/audio/player.py@@ -175,6 +175,39 @@ def _candidate_output_rates(source_rate: int, device_default: int = 0) -> list[ireturn candidates+def _os_default_output_name(+ devices: Sequence[Any], hostapis: Sequence[Any]+) -> str | None:+ """Name of the user's OS-selected default OUTPUT device, when it is a real,+ usable sink — else None.++ The "your device first" contract: ``auto-headset`` prefers whatever the user+ picked as their system default speaker, EXCEPT when that default is a device+ auto-headset exists to avoid — a monitor/HDMI or SPDIF output, or the+ localized MME/DirectSound virtual mapper. In those cases this returns None so+ the resolver falls back to the generic headset heuristic instead of routing+ to a wrong/dead sink. The NAME (not the index) is returned so the existing+ candidate sort still picks the device's best host-API twin (WASAPI over MME)+ and skips WDM-KS. A missing default / absent sounddevice yields None.+ """+ try:+ default_out = sd.default.device[1]+ except Exception: # noqa: BLE001 — no default / no sounddevice -> no preference+ return None+ if not isinstance(default_out, int) or not (0 <= default_out < len(devices)):+ return None+ dev = devices[default_out]+ name = str(dev.get("name", ""))+ if not name or dev.get("max_output_channels", 0) <= 0:+ return None+ low = name.lower()+ if any(b.lower() in low for b in _BLOCKED_OUTPUT_SUBSTRINGS):+ return None+ if is_legacy_primary_mapper(default_out, hostapis, devices, output=True):+ return None+ return name++def _resolve_output_device(device: int | str | None,priority: Sequence[str] | None = None,@@ -209,6 +242,17 @@ def _resolve_output_device(log.warning("Device query failed, using system default: %s", exc)return None+ # "Your device first": prefer the user's OS-selected default OUTPUT device+ # over the generic headset guesses, UNLESS it is a device auto-headset exists+ # to bypass (monitor/HDMI, SPDIF, the localized virtual mapper) — then fall+ # back to the heuristic below so playback never lands on a wrong/dead sink.+ # Injected as a NAME so the candidate sort still picks its best host-API twin+ # (WASAPI) and skips WDM-KS. Ranks BELOW an explicit output_device_priority.+ os_default_name = _os_default_output_name(devices, hostapis)+ effective_priority = (+ (*user_priority, os_default_name) if os_default_name else user_priority+ )+# Candidates: real output devices that are not on the blocklist.# WDM-KS is filtered when the same device (same name) is available on# another host API — otherwise, for example, the only Realtek Speakers@@ -260,17 +304,18 @@ def _resolve_output_device(def _name_rank(entry: tuple[int, dict]) -> int:low = entry[1].get("name", "").lower()- # User-configured names take strict precedence: any match in the user's- # priority list ranks ahead of every generic match. Both blocks keep- # "earlier entry = stronger". The generic block is offset by the user- # list length so a generic hit can never tie or beat a user hit.- for rank, sub in enumerate(user_priority):+ # Precedence: explicit user priority, then the OS-selected default device+ # (both carried in ``effective_priority``), then the generic headset list.+ # Each block keeps "earlier entry = stronger"; the generic block is offset+ # by len(effective_priority) so a generic hit can never tie or beat a+ # user / OS-default hit.+ for rank, sub in enumerate(effective_priority):if sub.lower() in low:return rankfor rank, sub in enumerate(_HEADSET_PRIORITY):if sub.lower() in low:- return len(user_priority) + rank- return len(user_priority) + len(_HEADSET_PRIORITY)+ return len(effective_priority) + rank+ return len(effective_priority) + len(_HEADSET_PRIORITY)# Primary sort key: name match; secondary: host API preference. Without a# name match the original order (system default) is preserved.diff --git a/tests/unit/audio/test_capture_device.py b/tests/unit/audio/test_capture_device.pyindex fba8654fea..beaebd593d 100644--- a/tests/unit/audio/test_capture_device.py+++ b/tests/unit/audio/test_capture_device.py@@ -1,8 +1,20 @@from __future__ import annotations+from types import SimpleNamespace++import pytest+from jarvis.audio import capture[email protected](autouse=True)+def _no_os_default(monkeypatch) -> None:+ """Pin "no OS-selected default device" so these pure-heuristic tests do not+ depend on the real test host's default microphone — the resolver now consults+ it for the "your device first" contract."""+ monkeypatch.setattr(capture.sd, "default", SimpleNamespace(device=(-1, -1)))++def test_auto_headset_prefers_real_microphone_over_loopback(monkeypatch) -> None:devices = [{diff --git a/tests/unit/audio/test_device_priority_and_mapper.py b/tests/unit/audio/test_device_priority_and_mapper.pyindex a8dd642d6c..b29f178891 100644--- a/tests/unit/audio/test_device_priority_and_mapper.py+++ b/tests/unit/audio/test_device_priority_and_mapper.py@@ -18,6 +18,8 @@ Two guarantees pinned here:"""from __future__ import annotations+from types import SimpleNamespace+import pytestimport jarvis.audio.capture as cap@@ -25,6 +27,23 @@ import jarvis.audio.player as plfrom jarvis.audio.device_select import is_legacy_primary_mapper[email protected](autouse=True)+def _neutralize_os_default(monkeypatch) -> None:+ """Simulate "no OS-selected default device" by default so the priority /+ mapper / generic tests here stay deterministic on any host. The "your device+ first" tests below re-patch ``sd.default`` to a specific system default;+ without this fixture the resolver would read the REAL machine's default and+ the outcome would depend on the test host."""+ monkeypatch.setattr(pl.sd, "default", SimpleNamespace(device=(-1, -1)))+ monkeypatch.setattr(cap.sd, "default", SimpleNamespace(device=(-1, -1)))+++def _set_os_default(monkeypatch, *, in_idx: int = -1, out_idx: int = -1) -> None:+ """Point ``sd.default.device`` (input, output) at the given fake-table indices."""+ monkeypatch.setattr(pl.sd, "default", SimpleNamespace(device=(in_idx, out_idx)))+ monkeypatch.setattr(cap.sd, "default", SimpleNamespace(device=(in_idx, out_idx)))++def _patch_out(monkeypatch, hostapis, devices) -> None:monkeypatch.setattr(pl.sd, "query_devices", lambda: devices)monkeypatch.setattr(pl.sd, "query_hostapis", lambda: hostapis)@@ -162,3 +181,117 @@ def test_mapper_helper_only_flags_mme_and_directsound() -> None:hostapis = [{"name": "Windows WASAPI", "devices": [0]}]devices = [{"name": "Speakers", "max_output_channels": 2, "hostapi": 0}]assert is_legacy_primary_mapper(0, hostapis, devices, output=True) is False+++# --------------------------------------------------------------------------- #+# 4. "Your device first" — the OS-selected default drives auto-headset #+# --------------------------------------------------------------------------- #+def test_output_os_default_wins_via_its_best_hostapi_twin(monkeypatch) -> None:+ """A real OS-default speaker beats the generic list AND is taken on its best+ host-API twin (WASAPI), even when it was named by an MME twin — proving the+ default is injected as a NAME, not a raw index."""+ hostapis = [{"name": "MME", "devices": [0, 1, 2]},+ {"name": "Windows WASAPI", "devices": [3, 4]}]+ devices = [+ {"name": "Microsoft Sound Mapper - Output", "max_output_channels": 2,+ "max_input_channels": 0, "hostapi": 0}, # idx0: MME mapper+ {"name": "Speakers (PRO X)", "max_output_channels": 2,+ "max_input_channels": 0, "hostapi": 0}, # idx1: generic pick+ {"name": "Desk Speakers (Focusrite)", "max_output_channels": 2,+ "max_input_channels": 0, "hostapi": 0}, # idx2: OS default (MME)+ {"name": "Speakers (PRO X)", "max_output_channels": 2,+ "max_input_channels": 0, "hostapi": 1}, # idx3+ {"name": "Desk Speakers (Focusrite)", "max_output_channels": 2,+ "max_input_channels": 0, "hostapi": 1}, # idx4: WASAPI twin+ ]+ _patch_out(monkeypatch, hostapis, devices)+ _set_os_default(monkeypatch, out_idx=2) # system default = Focusrite (MME)++ # Focusrite (the OS default) beats the generic PRO X and is taken on WASAPI.+ assert pl._resolve_output_device("auto-headset") == 4+++def test_output_junk_os_default_falls_back_to_headset(monkeypatch) -> None:+ """A monitor/HDMI OS default is rejected; the real headset is chosen."""+ hostapis = [{"name": "Windows WASAPI", "devices": [0, 1]}]+ devices = [+ {"name": "M28U (NVIDIA High Definition Audio)", "max_output_channels": 2,+ "max_input_channels": 0, "hostapi": 0}, # OS default = monitor via HDMI+ {"name": "Speakers (PRO X)", "max_output_channels": 2,+ "max_input_channels": 0, "hostapi": 0},+ ]+ _patch_out(monkeypatch, hostapis, devices)+ _set_os_default(monkeypatch, out_idx=0) # junk default (blocked HDMI name)++ assert pl._resolve_output_device("auto-headset") == 1+++def test_input_virtual_os_default_falls_back_to_real_mic(monkeypatch) -> None:+ """The exact maintainer case: the OS default mic is a virtual NVIDIA+ Broadcast, so auto-headset must pick the real headset mic instead of feeding+ the wake loop digital silence."""+ hostapis = [{"name": "MME", "devices": [0, 1]}]+ devices = [+ {"name": "Microphone (NVIDIA Broadcast)", "max_input_channels": 2,+ "max_output_channels": 0, "hostapi": 0}, # OS default = virtual mic+ {"name": "Microphone (PRO X)", "max_input_channels": 1,+ "max_output_channels": 0, "hostapi": 0}, # real headset mic+ ]+ _patch_in(monkeypatch, hostapis, devices)+ _set_os_default(monkeypatch, in_idx=0) # virtual mic as system default++ assert cap._resolve_input_device("auto-headset") == 1+++def test_input_real_os_default_wins_over_generic(monkeypatch) -> None:+ """A real OS-default mic beats a generic-list mic that would otherwise win."""+ hostapis = [{"name": "MME", "devices": [0, 1]}]+ devices = [+ {"name": "Microphone (PRO X)", "max_input_channels": 1,+ "max_output_channels": 0, "hostapi": 0}, # generic default winner+ {"name": "Podcast Mic (Focusrite)", "max_input_channels": 1,+ "max_output_channels": 0, "hostapi": 0}, # user's OS default+ ]+ _patch_in(monkeypatch, hostapis, devices)+ _set_os_default(monkeypatch, in_idx=1)++ assert cap._resolve_input_device("auto-headset") == 1+++def test_user_priority_still_beats_os_default(monkeypatch) -> None:+ """An explicit user priority outranks even the OS-selected default."""+ hostapis = [{"name": "Windows WASAPI", "devices": [0, 1]}]+ devices = [+ {"name": "Desk Speakers (Focusrite)", "max_output_channels": 2,+ "max_input_channels": 0, "hostapi": 0}, # OS default+ {"name": "Bose QuietComfort Ultra", "max_output_channels": 2,+ "max_input_channels": 0, "hostapi": 0}, # user-named+ ]+ _patch_out(monkeypatch, hostapis, devices)+ _set_os_default(monkeypatch, out_idx=0)++ # No user priority -> the OS default (Focusrite) wins.+ assert pl._resolve_output_device("auto-headset") == 0+ # User names Bose -> it beats even the OS default.+ assert pl._resolve_output_device("auto-headset", ["Bose"]) == 1+++def test_os_default_helper_rejects_junk_and_accepts_real(monkeypatch) -> None:+ """The helper itself: rejects a blocked/virtual/mapper default, accepts a+ real one — the guard that makes the fallback safe."""+ hostapis = [{"name": "MME", "devices": [0, 1, 2]}]+ devices = [+ {"name": "Microsoft Sound Mapper - Input", "max_input_channels": 2,+ "max_output_channels": 0, "hostapi": 0}, # idx0: mapper+ {"name": "Microphone (NVIDIA Broadcast)", "max_input_channels": 2,+ "max_output_channels": 0, "hostapi": 0}, # idx1: virtual+ {"name": "Podcast Mic (Focusrite)", "max_input_channels": 1,+ "max_output_channels": 0, "hostapi": 0}, # idx2: real+ ]+ # idx0 mapper -> None; idx1 virtual -> None; idx2 real -> its name.+ monkeypatch.setattr(cap.sd, "default", SimpleNamespace(device=(0, -1)))+ assert cap._os_default_input_name(devices, hostapis) is None+ monkeypatch.setattr(cap.sd, "default", SimpleNamespace(device=(1, -1)))+ assert cap._os_default_input_name(devices, hostapis) is None+ monkeypatch.setattr(cap.sd, "default", SimpleNamespace(device=(2, -1)))+ assert cap._os_default_input_name(devices, hostapis) == "Podcast Mic (Focusrite)"diff --git a/tests/unit/audio/test_resolve_output_wdmks.py b/tests/unit/audio/test_resolve_output_wdmks.pyindex 27f942a1ea..3d94855411 100644--- a/tests/unit/audio/test_resolve_output_wdmks.py+++ b/tests/unit/audio/test_resolve_output_wdmks.py@@ -15,9 +15,21 @@ excludes WDM-KS unconditionally whenever any safe output device exists."""from __future__ import annotations+from types import SimpleNamespace++import pytest+import jarvis.audio.player as player_mod[email protected](autouse=True)+def _no_os_default(monkeypatch) -> None:+ """Pin "no OS-selected default device" so these WDM-KS regression tests do+ not depend on the real test host's default speaker — the resolver now+ consults it for the "your device first" contract."""+ monkeypatch.setattr(player_mod.sd, "default", SimpleNamespace(device=(-1, -1)))++def _patch_devices(monkeypatch, hostapis, devices) -> None:monkeypatch.setattr(player_mod.sd, "query_devices", lambda: devices)monkeypatch.setattr(player_mod.sd, "query_hostapis", lambda: hostapis)