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.

fix(speech): resolve Vosk models from the install data directory (#722)

Uddoo696a392d0abe
diff --git a/jarvis/speech/wake_constants.py b/jarvis/speech/wake_constants.py
index 37d875ad8a..224dce4885 100644
--- a/jarvis/speech/wake_constants.py
+++ b/jarvis/speech/wake_constants.py
@@ -1,9 +1,10 @@
"""Single source of truth for the configurable wake-word vocabulary.
-Pure stdlib — NO ``jarvis.*`` imports, NO heavy third-party imports — so the
+Pure stdlib at import time — no eager ``jarvis.*`` or third-party imports — so the
matcher module (``wake_phrase``), the Pydantic config (``core/config.py``), and
the tests can all import this without dragging in ``faster_whisper``,
-``sounddevice``, or the openWakeWord runtime.
+``sounddevice``, or the openWakeWord runtime. Vosk path lookup loads only the
+stdlib-backed directory helpers, lazily.
Why this module exists:
- ``WAKE_ENGINES`` is the SoT for the five-layer enum (Python ↔ TOML ↔ Pydantic
@@ -14,6 +15,7 @@ Why this module exists:
engine chain in ``wake_phrase.resolve_wake_plan`` (user-trained custom .onnx
-> any-word Vosk KWS -> local-Whisper transcript match -> honest degrade).
"""
+
from __future__ import annotations
import os
@@ -31,9 +33,7 @@ from pathlib import Path
#: Vosk model directory — see ``resolve_vosk_model_path``).
#: ``stt_match`` — local-Whisper transcript match (any phrase, needs whisper).
#: ``custom_onnx`` — a user-supplied/trained .onnx model (CPU, any phrase).
-WAKE_ENGINES: tuple[str, ...] = (
- "auto", "openwakeword", "vosk_kws", "stt_match", "custom_onnx"
-)
+WAKE_ENGINES: tuple[str, ...] = ("auto", "openwakeword", "vosk_kws", "stt_match", "custom_onnx")
DEFAULT_WAKE_PHRASE = "" # empty = neutral pre-onboarding default; user must opt in
@@ -52,9 +52,9 @@ STT_HALLUCINATION_RE = re.compile(
r"im\s+auftrag\s+des|"
r"untertitel\s+(von|der|im\s+auftrag)|" # i18n-allow: German Whisper-noise STT vocabulary
r"untertitelung\s+des\s+(zdf|wdr|ndr|swr|br|ard|arte)"
- r"(\s+(fuer|für|fur)\s+funk)?(\s*,?\s*\d{4})?|" # i18n-allow: German Whisper-noise STT vocabulary
+ r"(\s+(fuer|für|fur)\s+funk)?(\s*,?\s*\d{4})?|" # i18n-allow: Whisper-noise STT vocabulary
r"(eine\s+)?(sendung|produktion|redaktion|programm)\s+"
- r"(des|der|von)\s+(zdf|wdr|ndr|swr|br|ard|arte)" # i18n-allow: German Whisper-noise STT vocabulary
+ r"(des|der|von)\s+(zdf|wdr|ndr|swr|br|ard|arte)" # i18n-allow: Whisper-noise STT vocabulary
r"(\s*,?\s*\d{4})?|"
r"(zdf|wdr|ndr|swr|br|ard|arte)\s+"
r"(fernsehen|mediagroup|rundfunk)(\s*,?\s*\d{4})?|"
@@ -231,15 +231,20 @@ def sound_fold(token: str) -> str:
# Vosk model directory resolution (vosk_kws engine)
# ---------------------------------------------------------------------------
-def _vosk_models_root() -> Path:
+
+def _vosk_models_root(data_dir: str | None = None) -> Path:
"""Per-install Vosk model store: ``<data>/wake_models/vosk/<lang>/``.
- Honours the same data-dir env seam the rest of the app uses. Models are
- fetched once at setup for the configured language (a ~45 MB small model
- per language — too heavy to bundle for every locale) and are fully
- offline afterwards.
+ Explicit fetch paths and the memory-data override take precedence over the
+ runtime data directory. Verification, prefetch and wake-plan selection all
+ use this resolver so they agree even outside the install directory.
"""
- base = os.environ.get("JARVIS__MEMORY__DATA_DIR") or "data"
+ base = data_dir or os.environ.get("JARVIS__MEMORY__DATA_DIR")
+ if not base:
+ from jarvis.core.instance import resolve_data_dir
+ from jarvis.core.paths import repo_root
+
+ base = str(resolve_data_dir(repo_root()))
return Path(base) / "wake_models" / "vosk"
diff --git a/jarvis/speech/wake_model_fetch.py b/jarvis/speech/wake_model_fetch.py
index 7882927ee8..7716606e6b 100644
--- a/jarvis/speech/wake_model_fetch.py
+++ b/jarvis/speech/wake_model_fetch.py
@@ -10,12 +10,12 @@ empty hash is treated as "not yet pinned" and accepted unverified).
Pure-ish: stdlib only at import time; ``httpx`` (the repo's HTTP standard) is
imported lazily inside ``_http_get`` so importing this module stays light.
"""
+
from __future__ import annotations
import hashlib
import io
import logging
-import os
import shutil
import tempfile
import zipfile
@@ -119,9 +119,10 @@ def resolve_wake_language(cfg: Any) -> str:
def _models_root(data_dir: str | None) -> Path:
- """Same layout ``wake_constants._vosk_models_root`` resolves — do not diverge."""
- base = data_dir or os.environ.get("JARVIS__MEMORY__DATA_DIR") or "data"
- return Path(base) / "wake_models" / "vosk"
+ """Use the same data root as runtime wake-plan selection."""
+ from jarvis.speech.wake_constants import _vosk_models_root
+
+ return _vosk_models_root(data_dir)
def _is_model_dir(cand: Path) -> bool:
diff --git a/tests/unit/setup/test_vosk_data_dir.py b/tests/unit/setup/test_vosk_data_dir.py
new file mode 100644
index 0000000000..a397e1feca
--- /dev/null
+++ b/tests/unit/setup/test_vosk_data_dir.py
@@ -0,0 +1,149 @@
+"""Vosk verification and prefetch must use the install's data directory."""
+
+from pathlib import Path
+from types import SimpleNamespace
+
+import pytest
+
+from jarvis.core import paths
+from jarvis.setup import model_report, prefetch
+from jarvis.speech import wake_constants, wake_model_fetch
+from jarvis.speech.wake_phrase import resolve_wake_plan
+
+
+def install_root(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path:
+ root = tmp_path / "install"
+ root.mkdir()
+ monkeypatch.setattr(paths, "repo_root", lambda: root)
+ for name in ("JARVIS_DATA_DIR", "JARVIS__MEMORY__DATA_DIR", "JARVIS_INSTANCE"):
+ monkeypatch.delenv(name, raising=False)
+ return root
+
+
+def _model(data_dir: Path, language: str = "en") -> Path:
+ model = data_dir / "wake_models" / "vosk" / language
+ (model / "am").mkdir(parents=True)
+ return model
+
+
[email protected]("source", ["default", "dev", "runtime_env", "memory_env", "explicit"])
+def test_presence_and_prefetch_keep_data_directory_precedence(
+ install_root: Path,
+ tmp_path: Path,
+ monkeypatch: pytest.MonkeyPatch,
+ source: str,
+) -> None:
+ data_dir = install_root / ("data-dev" if source == "dev" else "data")
+ explicit = None
+ if source == "dev":
+ monkeypatch.setenv("JARVIS_INSTANCE", "dev")
+ if source in ("runtime_env", "memory_env", "explicit"):
+ data_dir = tmp_path / "runtime-data"
+ monkeypatch.setenv("JARVIS_DATA_DIR", str(data_dir))
+ if source in ("memory_env", "explicit"):
+ data_dir = tmp_path / "memory-data"
+ monkeypatch.setenv("JARVIS__MEMORY__DATA_DIR", str(data_dir))
+ if source == "explicit":
+ data_dir = tmp_path / "explicit-data"
+ explicit = str(data_dir)
+ expected = _model(data_dir)
+
+ foreign = tmp_path / "elsewhere"
+ foreign.mkdir()
+ monkeypatch.chdir(foreign)
+ downloads: list[str] = []
+
+ def no_download(url: str) -> bytes:
+ downloads.append(url)
+ raise AssertionError("An installed model must not be downloaded again")
+
+ assert wake_model_fetch.vosk_model_present("en", data_dir=explicit)
+ assert (
+ wake_model_fetch.ensure_vosk_model(
+ "en", data_dir=explicit, http_get=no_download, echo=lambda _line: None
+ )
+ == expected
+ )
+ assert downloads == []
+ assert not (foreign / "data").exists()
+ if source != "explicit":
+ assert wake_constants.resolve_vosk_model_path("en") == str(expected)
+ assert wake_constants.resolve_vosk_model_paths("en") == [str(expected)]
+ plan = resolve_wake_plan(
+ SimpleNamespace(phrase="Hey Nova", engine="auto", custom_model_path=""),
+ local_whisper_available=False,
+ language="en",
+ vosk_available=True,
+ )
+ assert plan.engine == "vosk_kws"
+ assert plan.vosk_model_path == str(expected)
+ assert plan.vosk_model_paths == (str(expected),)
+ assert plan.wake_available
+
+
[email protected]("full_profile", [False, True])
+def test_report_finds_installed_models_from_any_cwd(
+ install_root: Path,
+ tmp_path: Path,
+ monkeypatch: pytest.MonkeyPatch,
+ full_profile: bool,
+) -> None:
+ for language in wake_model_fetch.VOSK_MODELS:
+ _model(install_root / "data", language)
+ cfg = SimpleNamespace(stt=SimpleNamespace(language="en"))
+ monkeypatch.setattr(model_report, "_wake_backbone_present", lambda: True)
+ monkeypatch.setattr(model_report, "_vad_present", lambda: True)
+ monkeypatch.setattr(model_report, "_neural_wake_runtime_available", lambda: False)
+ monkeypatch.setattr(model_report, "_silero_runtime_available", lambda: False)
+ monkeypatch.setattr(model_report, "_webrtc_vad_available", lambda: False)
+ monkeypatch.setattr(model_report, "_faster_whisper_available", lambda: False)
+
+ for cwd in (install_root, tmp_path, tmp_path / "elsewhere"):
+ cwd.mkdir(exist_ok=True)
+ monkeypatch.chdir(cwd)
+ report = model_report.voice_model_report(cfg, full_profile=full_profile)
+ wake = [item for item in report if "custom-wake model" in item.label]
+ assert len(wake) == (len(wake_model_fetch.VOSK_MODELS) if full_profile else 1)
+ assert all(item.present and item.detail == "ready" for item in wake)
+ assert all(item.required == full_profile for item in wake)
+
+
+def test_foreign_cwd_model_does_not_hide_missing_install_model(
+ install_root: Path, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ _model(tmp_path / "data")
+ monkeypatch.chdir(tmp_path)
+
+ assert not wake_model_fetch.vosk_model_present("en")
+ assert wake_constants.resolve_vosk_model_path("en") is None
+ assert wake_constants.resolve_vosk_model_paths("en") == []
+ plan = resolve_wake_plan(
+ SimpleNamespace(phrase="Hey Nova", engine="auto", custom_model_path=""),
+ local_whisper_available=False,
+ language="en",
+ vosk_available=True,
+ )
+ assert plan.engine == "none"
+ assert not plan.wake_available
+ assert not (install_root / "data").exists()
+
+
+def test_prefetch_reuses_install_model_from_foreign_cwd(
+ install_root: Path, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ _model(install_root / "data")
+ monkeypatch.chdir(tmp_path)
+ monkeypatch.setattr(prefetch, "_wakeword_bundle_present", lambda: True)
+ monkeypatch.setattr(prefetch, "_load_config", lambda: SimpleNamespace())
+ monkeypatch.setattr(prefetch, "_faster_whisper_available", lambda: False)
+ downloads: list[str] = []
+
+ def no_download(url: str) -> bytes:
+ downloads.append(url)
+ raise AssertionError("Prefetch must reuse the installed model")
+
+ monkeypatch.setattr(wake_model_fetch, "_http_get", no_download)
+ assert prefetch.prefetch_all(echo=lambda _line: None) == 0
+ assert downloads == []
+ assert not (tmp_path / "data").exists()