Skip to content

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.

perf(boot): prefetch the audio-device settle off the voice-ready path

Ruben Lütke2cf20cf1bbf7
diff --git a/jarvis/audio/device_init.py b/jarvis/audio/device_init.py
index b36e64415a..ecdcce129b 100644
--- a/jarvis/audio/device_init.py
+++ b/jarvis/audio/device_init.py
@@ -26,11 +26,16 @@ installed) and never raises — audio robustness must never break boot.
from __future__ import annotations
import contextlib
+import threading
import time
from collections.abc import Callable
from typing import Any
-__all__ = ["wait_for_stable_audio_devices"]
+__all__ = [
+ "wait_for_stable_audio_devices",
+ "start_audio_device_prefetch",
+ "get_prefetched_audio_result",
+]
def _get_sd() -> Any | None:
@@ -122,3 +127,51 @@ def wait_for_stable_audio_devices(
"waited_s": round(now - start, 3),
}
sleep(poll_interval_s)
+
+
+# --- Boot-path prefetch ---------------------------------------------------
+# The device settle is a blocking ~1.5 s poll (``stable_window_s``). On the
+# desktop boot it sits directly on the path to ``VoiceBootStatus(ready=True)``
+# (the "VOICE STARTING…" spinner). The launcher can start it eagerly in a daemon
+# thread at process start so it runs concurrently with the brain build +
+# ``server.start()``; by the time Phase-A warm-up needs it, it has already
+# settled and the warm-up reuses the result instead of paying the wait again.
+_PREFETCH_EVENT = threading.Event()
+_PREFETCH_RESULT: dict[str, Any] | None = None
+
+
+def start_audio_device_prefetch() -> threading.Thread | None:
+ """Run :func:`wait_for_stable_audio_devices` eagerly in a daemon thread.
+
+ Returns ``None`` when sounddevice is unavailable (headless VPS / no
+ ``[desktop]`` extra) — nothing to settle. Never raises; audio robustness
+ must not break boot. The result is published for
+ :func:`get_prefetched_audio_result`.
+ """
+ if _get_sd() is None:
+ return None
+
+ def _run() -> None:
+ global _PREFETCH_RESULT
+ try:
+ _PREFETCH_RESULT = wait_for_stable_audio_devices()
+ except Exception: # noqa: BLE001 — never break boot
+ _PREFETCH_RESULT = None
+ finally:
+ _PREFETCH_EVENT.set()
+
+ thread = threading.Thread(target=_run, name="audio-device-prefetch", daemon=True)
+ thread.start()
+ return thread
+
+
+def get_prefetched_audio_result() -> dict[str, Any] | None:
+ """Return the prefetch result iff it has already settled, else ``None``.
+
+ ``None`` means no prefetch was started OR it is still polling — the caller
+ then falls back to its own :func:`wait_for_stable_audio_devices` call (i.e.
+ today's behavior). Monotonically safe: it can only help, never slow boot.
+ """
+ if _PREFETCH_EVENT.is_set():
+ return _PREFETCH_RESULT
+ return None
diff --git a/jarvis/speech/pipeline.py b/jarvis/speech/pipeline.py
index 9d6269ac72..9c3695b0db 100644
--- a/jarvis/speech/pipeline.py
+++ b/jarvis/speech/pipeline.py
@@ -3377,7 +3377,16 @@ class SpeechPipeline:
stale/silent device. Fully guarded — must never block or break boot.
"""
try:
- info = await asyncio.to_thread(wait_for_stable_audio_devices)
+ # If the launcher prefetched the (blocking ~1.5 s) device settle in a
+ # daemon thread at boot, reuse its already-settled result instead of
+ # re-paying the poll wait on the wake-critical path. Falls back to a
+ # fresh settle when no prefetch ran / it is still polling — identical
+ # to today's behavior, so this can only help, never slow boot.
+ from jarvis.audio.device_init import get_prefetched_audio_result
+
+ info = get_prefetched_audio_result()
+ if info is None:
+ info = await asyncio.to_thread(wait_for_stable_audio_devices)
log.info(
"Audio-Geräte stabilisiert: %d Geräte (stable=%s, %.1fs, %d reinit).",
info.get("device_count", 0),
diff --git a/jarvis/ui/desktop_app.py b/jarvis/ui/desktop_app.py
index 83ab0ea14f..d90a27d110 100644
--- a/jarvis/ui/desktop_app.py
+++ b/jarvis/ui/desktop_app.py
@@ -580,6 +580,15 @@ class DesktopApp:
from jarvis.speech.warmup_prefetch import start_wake_import_prefetch
start_wake_import_prefetch()
+ # Same idea for the audio-device settle: the BUG-014 guard
+ # (wait_for_stable_audio_devices) is a blocking ~1.5 s poll that gates
+ # the voice Phase-A warm-up. Start it NOW in a daemon thread so it
+ # settles concurrently with the brain build + server.start(); Phase A
+ # then reuses the result instead of re-paying the wait. No-op on a
+ # headless host without sounddevice.
+ from jarvis.audio.device_init import start_audio_device_prefetch
+ start_audio_device_prefetch()
+
def _log_unhandled_async(loop_: asyncio.AbstractEventLoop, context: dict) -> None:
exc = context.get("exception")
msg = context.get("message", "<no message>")
diff --git a/tests/unit/audio/test_device_init.py b/tests/unit/audio/test_device_init.py
index 7d58c3b30c..45167ae76f 100644
--- a/tests/unit/audio/test_device_init.py
+++ b/tests/unit/audio/test_device_init.py
@@ -149,3 +149,66 @@ def test_respects_max_wait_when_never_stable(monkeypatch) -> None:
assert info["stable"] is False
assert clock.t <= 2.5 # bounded by max_wait_s, never unbounded
+
+
+# --- Boot-path prefetch (2026-06-24 boot-speed work) ----------------------
+# The launcher starts the (blocking ~1.5 s) device settle in a daemon thread at
+# process start so Phase-A warm-up reuses the settled result instead of
+# re-paying the poll wait. Monotonically safe: get_prefetched_audio_result()
+# returns None while still polling, so the caller falls back to a fresh settle.
+
+
+def _reset_prefetch() -> None:
+ di._PREFETCH_EVENT.clear()
+ di._PREFETCH_RESULT = None
+
+
+def test_prefetch_result_none_before_any_prefetch() -> None:
+ _reset_prefetch()
+ assert di.get_prefetched_audio_result() is None
+
+
+def test_prefetch_result_none_while_still_polling() -> None:
+ # Result present but event not set => still polling => caller must fall back.
+ _reset_prefetch()
+ di._PREFETCH_RESULT = {"stale": True}
+ assert di.get_prefetched_audio_result() is None
+
+
+def test_start_prefetch_noop_without_sounddevice(monkeypatch) -> None:
+ _reset_prefetch()
+ monkeypatch.setattr(di, "_get_sd", lambda: None)
+ assert di.start_audio_device_prefetch() is None
+ assert di.get_prefetched_audio_result() is None
+
+
+def test_prefetch_publishes_settled_result(monkeypatch) -> None:
+ _reset_prefetch()
+ sentinel = {"available": True, "device_count": 7, "stable": True}
+ monkeypatch.setattr(di, "_get_sd", lambda: object()) # sounddevice "present"
+ monkeypatch.setattr(di, "wait_for_stable_audio_devices", lambda: sentinel)
+
+ thread = di.start_audio_device_prefetch()
+ assert thread is not None
+ thread.join(timeout=2)
+
+ assert di.get_prefetched_audio_result() == sentinel
+ _reset_prefetch()
+
+
+def test_prefetch_swallows_settle_failure(monkeypatch) -> None:
+ _reset_prefetch()
+
+ def _boom() -> dict:
+ raise RuntimeError("portaudio exploded mid-scan")
+
+ monkeypatch.setattr(di, "_get_sd", lambda: object())
+ monkeypatch.setattr(di, "wait_for_stable_audio_devices", _boom)
+
+ thread = di.start_audio_device_prefetch()
+ assert thread is not None
+ thread.join(timeout=2)
+
+ # Event set, result None => caller falls back to a fresh settle (today's path).
+ assert di.get_prefetched_audio_result() is None
+ _reset_prefetch()