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.

feat(device): add central CPU-first device selector, flip GPU-first defaults

Ruben Lütke914a1dd6b95b
diff --git a/jarvis/core/device.py b/jarvis/core/device.py
index 65ebbfc5e0..0f0187b831 100644
--- a/jarvis/core/device.py
+++ b/jarvis/core/device.py
@@ -61,6 +61,17 @@ def _looks_like_gpu(token: str) -> bool:
return token.split(":", 1)[0] in _GPU_TOKENS
+def _canonical_cuda(token: str) -> str:
+ """Normalize an honored GPU request to a backend-valid CUDA spec.
+
+ CTranslate2 / faster-whisper accept ``"cuda"`` and ``"cuda:N"`` but not the
+ ``"gpu"`` alias, so the canonical head is always ``cuda`` while any device
+ index (``:1``) is preserved verbatim.
+ """
+ _, sep, index = token.partition(":")
+ return f"cuda{sep}{index}" if sep else "cuda"
+
+
def resolve_device(
requested: str | None,
*,
@@ -105,10 +116,11 @@ def resolve_device(
return DeviceResolution(CPU, raw, False, reason)
if _looks_like_gpu(token):
+ cuda = _canonical_cuda(token)
if cuda_usable is True:
- reason = f"explicit GPU request honored (capability verified) -> {raw}"
+ reason = f"explicit GPU request honored (capability verified) -> {cuda}"
logger.info("Device%s: %s", tag, reason)
- return DeviceResolution(raw, raw, False, reason)
+ return DeviceResolution(cuda, raw, False, reason)
if cuda_usable is False:
reason = f"GPU requested ({raw!r}) but not usable on this host -> CPU fallback"
logger.warning("Device%s: %s", tag, reason)
@@ -118,7 +130,7 @@ def resolve_device(
"backend self-heals to CPU on failure"
)
logger.info("Device%s: %s", tag, reason)
- return DeviceResolution(raw, raw, False, reason)
+ return DeviceResolution(cuda, raw, False, reason)
reason = f"unrecognized device {raw!r} -> CPU (fail-closed to safe default)"
logger.warning("Device%s: %s", tag, reason)
diff --git a/jarvis/plugins/stt/__init__.py b/jarvis/plugins/stt/__init__.py
index 2b9bf6aa0d..a2162168d0 100644
--- a/jarvis/plugins/stt/__init__.py
+++ b/jarvis/plugins/stt/__init__.py
@@ -253,15 +253,23 @@ def build_stt_from_config(stt_cfg: Any) -> Any:
def _build_local_fallback(stt_cfg: Any, language: str | None) -> Any:
"""Construct the key-free local faster-whisper provider (the universal floor)."""
+ from jarvis.core.device import resolve_device
from jarvis.plugins.stt.fwhisper import FasterWhisperProvider
+ # Central CPU-first device resolution (ADR-0024). The utterance provider is
+ # latency-tolerant, so its capability verdict is left unverified (None): an
+ # explicit ``device = "cuda"`` in jarvis.toml is the user's opt-in and is
+ # honored, while ``auto`` / empty / unknown resolve to the cloud-first CPU
+ # floor — a stranger with no NVIDIA GPU is the baseline, not the maintainer's
+ # card. fwhisper's construction-time self-heal remains the runtime safety net.
+ device = resolve_device(
+ getattr(stt_cfg, "device", "cpu"),
+ cuda_usable=None,
+ purpose="stt-utterance",
+ ).device
return FasterWhisperProvider(
- # CPU-safe defaults: a stranger with no NVIDIA GPU is the baseline, not
- # the maintainer's card. If a real STTConfig is passed its explicit
- # device/compute_type win; the getattr defaults only bite duck-typed
- # stubs, and they must presume no GPU (fwhisper still self-heals to CPU).
model=getattr(stt_cfg, "model", "distil-large-v3"),
- device=getattr(stt_cfg, "device", "cpu"),
+ device=device,
compute_type=getattr(stt_cfg, "compute_type", "int8"),
language=language,
)
diff --git a/jarvis/plugins/stt/fwhisper.py b/jarvis/plugins/stt/fwhisper.py
index c69813df1f..c69246a105 100644
--- a/jarvis/plugins/stt/fwhisper.py
+++ b/jarvis/plugins/stt/fwhisper.py
@@ -277,7 +277,11 @@ class FasterWhisperProvider:
def __init__(
self,
model: str = "distil-large-v3",
- device: str = "cuda",
+ # CPU-first default (ADR-0024): a bare construction with no explicit
+ # device must never assume the maintainer's GPU. Real callers pass the
+ # config device, resolved through ``jarvis.core.device.resolve_device``
+ # at the STT factory before it reaches here; the cloud-first floor is CPU.
+ device: str = "cpu",
compute_type: str = "int8_float16",
language: str | None = None, # None = auto-detect (bilingual DE+EN)
beam_size: int = 5,
diff --git a/tests/unit/core/test_device.py b/tests/unit/core/test_device.py
new file mode 100644
index 0000000000..d745ed0220
--- /dev/null
+++ b/tests/unit/core/test_device.py
@@ -0,0 +1,89 @@
+"""CPU-first device-selection policy (``jarvis.core.device``).
+
+Pins the cloud-first contract of ADR-0024: the default is always CPU, a GPU is
+adopted only on an EXPLICIT request with a VERIFIED capability, and a known-bad
+GPU degrades to CPU with ``fell_back=True``. The capability verdict is injected
+(``cuda_usable``), so these tests never touch torch / ctranslate2.
+"""
+from __future__ import annotations
+
+from dataclasses import FrozenInstanceError
+
+import pytest
+
+from jarvis.core.device import CPU, DeviceResolution, resolve_device
+
+
[email protected]("value", ["cpu", "CPU", " cpu "])
+def test_explicit_cpu_stays_cpu(value: str) -> None:
+ res = resolve_device(value)
+ assert res.device == CPU
+ assert res.fell_back is False
+
+
[email protected]("value", ["", "auto", None])
+def test_auto_and_empty_default_to_cpu(value: str | None) -> None:
+ # auto / empty / None with no verified GPU -> cloud-first CPU floor.
+ assert resolve_device(value).device == CPU
+ assert resolve_device(value, cuda_usable=False).device == CPU
+
+
+def test_auto_opts_up_to_cuda_only_when_verified() -> None:
+ res = resolve_device("auto", cuda_usable=True)
+ assert res.device == "cuda"
+ assert res.fell_back is False
+
+
+def test_explicit_gpu_honored_when_verified() -> None:
+ res = resolve_device("cuda", cuda_usable=True)
+ assert res.device == "cuda"
+ assert res.fell_back is False
+
+
+def test_explicit_gpu_falls_back_when_known_unusable() -> None:
+ res = resolve_device("cuda", cuda_usable=False)
+ assert res.device == CPU
+ assert res.fell_back is True
+ assert "CPU" in res.reason
+
+
+def test_explicit_gpu_honored_when_capability_unknown() -> None:
+ # None = not pre-verified: honor the explicit opt-in, let the backend
+ # self-heal. This preserves a power user's `device = "cuda"` in jarvis.toml.
+ res = resolve_device("cuda", cuda_usable=None)
+ assert res.device == "cuda"
+ assert res.fell_back is False
+
+
+def test_indexed_cuda_spec_is_preserved() -> None:
+ res = resolve_device("cuda:1", cuda_usable=True)
+ assert res.device == "cuda:1"
+
+
+def test_indexed_gpu_falls_back_to_plain_cpu_when_unusable() -> None:
+ res = resolve_device("cuda:0", cuda_usable=False)
+ assert res.device == CPU
+ assert res.fell_back is True
+
+
[email protected]("value", ["mps", "xpu", "0", "banana"])
+def test_unrecognized_device_fails_closed_to_cpu(value: str) -> None:
+ # Anything the policy does not recognize resolves to the safe device, never
+ # a silent GPU escalation.
+ res = resolve_device(value, cuda_usable=True)
+ assert res.device == CPU
+ assert res.fell_back is False
+
+
+def test_gpu_alias_canonicalizes_to_cuda() -> None:
+ # The "gpu" alias is normalized to the backend-valid "cuda" spec when honored.
+ assert resolve_device("gpu", cuda_usable=True).device == "cuda"
+ assert resolve_device("gpu:1", cuda_usable=True).device == "cuda:1"
+ assert resolve_device("gpu", cuda_usable=False).device == CPU
+
+
+def test_result_is_a_frozen_resolution() -> None:
+ res = resolve_device("cpu")
+ assert isinstance(res, DeviceResolution)
+ with pytest.raises(FrozenInstanceError):
+ res.device = "cuda" # type: ignore[misc] # frozen dataclass