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
perf(audio): resolve-cache for the wake-to-session mic handover gap
Ruben Lütke4daff6ba6c45
diff --git a/jarvis/audio/capture.py b/jarvis/audio/capture.pyindex 92d67481cb..749c44e7b3 100644--- a/jarvis/audio/capture.py+++ b/jarvis/audio/capture.py@@ -12,7 +12,7 @@ from __future__ import annotationsimport asyncioimport timefrom collections.abc import AsyncIterator, Sequence-from typing import TYPE_CHECKING+from typing import TYPE_CHECKING, Anyimport numpy as npfrom loguru import logger as _log@@ -217,6 +217,55 @@ def _os_default_input_name(return name+# 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+# dominant share of the wake→session mic handover gap, during which the+# user's first words after "Hey Jarvis" were simply not captured (live+# forensic 2026-07-11: bar visible instantly, speech only heard ~0.5-0.7s+# later). A capture whose stream is delivering frames TOUCHES its cache+# entry every watchdog tick, so the session mic that opens <1s after the+# wake mic closed reuses the proven device instantly. Freshness window is+# deliberately short (a few seconds past the last live frame) and any+# open FAILURE invalidates, so hot-plug/device-switch behaviour falls back+# to a full fresh resolve exactly as before.+_RESOLVE_CACHE: dict[tuple[Any, tuple[str, ...]], tuple[Any, float]] = {}+_RESOLVE_CACHE_FRESH_S = 5.0+++def _resolve_cache_key(+ device: int | str | None, priority: tuple[str, ...]+) -> tuple[Any, tuple[str, ...]]:+ return (device, priority)+++def _touch_resolve_cache(+ device_spec: int | str | None, priority: tuple[str, ...], resolved: Any+) -> None:+ """Mark ``resolved`` as live-and-working for ``device_spec`` right now."""+ _RESOLVE_CACHE[_resolve_cache_key(device_spec, priority)] = (+ resolved,+ time.monotonic(),+ )+++def _invalidate_resolve_cache() -> None:+ """Drop every cached resolve — called on open failures/stream stalls."""+ _RESOLVE_CACHE.clear()+++def _cached_resolve(+ device_spec: int | str | None, priority: tuple[str, ...]+) -> Any | None:+ entry = _RESOLVE_CACHE.get(_resolve_cache_key(device_spec, priority))+ if entry is None:+ return None+ resolved, ts = entry+ if time.monotonic() - ts > _RESOLVE_CACHE_FRESH_S:+ return None+ return resolved++def _resolve_input_device(device: int | str | None,priority: Sequence[str] | None = None,@@ -392,7 +441,23 @@ class MicrophoneCapture:# consulted BEFORE the generic _INPUT_PRIORITY default when resolving# "auto-headset". Empty = today's generic behavior.self._device_priority: tuple[str, ...] = tuple(device_priority or ())- self._device = _resolve_input_device(device, self._device_priority)+ # Original spec (e.g. "auto-headset") kept for the resolve cache; the+ # cache only serves STRING specs — ints/None resolve instantly anyway.+ self._device_spec: int | str | None = device+ cached = (+ _cached_resolve(device, self._device_priority)+ if isinstance(device, str)+ else None+ )+ if cached is not None:+ _log.info(+ "Mic-Resolve: cache hit for '{}' -> {} (device live moments ago).",+ device,+ cached,+ )+ self._device = cached+ else:+ self._device = _resolve_input_device(device, self._device_priority)self._sample_rate = sample_rateself._blocksize = blocksizeself._channels = channels@@ -501,6 +566,10 @@ class MicrophoneCapture:stream.start()self._stream = streamself._device = attempt+ if isinstance(self._device_spec, str):+ _touch_resolve_cache(+ self._device_spec, self._device_priority, attempt+ )_log.info("Mic opened (device={}, sr={}, blocksize={}, dtype={}).",attempt,@@ -511,6 +580,9 @@ class MicrophoneCapture:returnexcept Exception as exc: # noqa: BLE001last_error = exc+ # A failed open means the cached/resolved device may be gone —+ # force the next construction through a fresh full resolve.+ _invalidate_resolve_cache()_log.warning("Mic-Open on device={} failed ({}); trying next fallback.",attempt,@@ -548,7 +620,16 @@ class MicrophoneCapture:returnelapsed = time.monotonic() - self._last_chunk_monotonicif elapsed <= self._STALL_THRESHOLD_S:+ # 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):+ _touch_resolve_cache(+ self._device_spec, self._device_priority, self._device+ )continue+ # Stalled: whatever we knew about the device landscape is suspect.+ _invalidate_resolve_cache()self._restart_count += 1_log.warning("Mic stall detected ({:.1f}s without a frame) — restart #{} (device={}).",diff --git a/tests/unit/audio/test_capture_device.py b/tests/unit/audio/test_capture_device.pyindex 9caa67b9ce..016bcb7610 100644--- a/tests/unit/audio/test_capture_device.py+++ b/tests/unit/audio/test_capture_device.py@@ -120,3 +120,67 @@ def test_explicit_name_resolves_to_index(monkeypatch) -> None:monkeypatch.setattr(mod.sd, "query_hostapis", lambda: hostapis)assert capture._resolve_input_device("Microphone (Manual)") == 0+++def test_resolve_cache_serves_handover_and_invalidates_on_failure(monkeypatch) -> None:+ """The wake→session mic handover must not re-enumerate devices.++ Live forensic 2026-07-11: resolving "auto-headset" cost ~0.4s on every+ MicrophoneCapture construction — the dominant share of the gap in which+ the user's first words after "Hey Jarvis" were not captured. A capture+ whose stream delivers frames touches the resolve cache; a construction+ within the freshness window reuses the proven device WITHOUT calling+ query_devices. Any open failure or stall invalidates, restoring the full+ fresh resolve (hot-plug behaviour unchanged).+ """+ devices = [+ {+ "name": "Microphone (Logitech PRO X Gaming Headset)",+ "max_input_channels": 1,+ "max_output_channels": 0,+ "hostapi": 0,+ },+ ]+ hostapis = [{"name": "MME"}]+ calls = {"n": 0}++ def counting_query():+ calls["n"] += 1+ return devices++ monkeypatch.setattr(capture.sd, "query_devices", counting_query)+ monkeypatch.setattr(capture.sd, "query_hostapis", lambda: hostapis)+ capture._invalidate_resolve_cache()++ # First resolve: full enumeration.+ assert capture._resolve_input_device("auto-headset") == 0+ n_after_first = calls["n"]+ assert n_after_first >= 1++ # A live stream touches the cache (what the watchdog tick does)...+ capture._touch_resolve_cache("auto-headset", (), 0)+ # ...so a construction right after skips enumeration entirely.+ cap = capture.MicrophoneCapture(device="auto-headset")+ assert cap._device == 0+ assert calls["n"] == n_after_first++ # An open failure invalidates: the next construction resolves fresh.+ capture._invalidate_resolve_cache()+ cap2 = capture.MicrophoneCapture(device="auto-headset")+ assert cap2._device == 0+ assert calls["n"] > n_after_first+++def test_resolve_cache_expires_after_freshness_window(monkeypatch) -> None:+ capture._invalidate_resolve_cache()+ capture._touch_resolve_cache("auto-headset", (), 3)+ assert capture._cached_resolve("auto-headset", ()) == 3+ # Age the entry past the freshness window (bind the real clock FIRST —+ # the lambda must not call through its own patched self).+ real_monotonic = capture.time.monotonic+ monkeypatch.setattr(+ capture.time,+ "monotonic",+ lambda: real_monotonic() + capture._RESOLVE_CACHE_FRESH_S + 1,+ )+ assert capture._cached_resolve("auto-headset", ()) is None