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): prime the wake model inside the prefetch - warm_up adopts a ready engine

Ruben Lütkef6201f0bf52c
diff --git a/jarvis/plugins/stt/fwhisper.py b/jarvis/plugins/stt/fwhisper.py
index fff24b438b..5fa5ff9eee 100644
--- a/jarvis/plugins/stt/fwhisper.py
+++ b/jarvis/plugins/stt/fwhisper.py
@@ -313,6 +313,10 @@ class FasterWhisperProvider:
# scratch — a load cascade that turned a ~4 s model load into 114.7 s
# on the boot path (TTU forensic 2026-07-02).
self._warm = False
+ # True when _ensure_model adopted a boot-prefetched engine that was
+ # ALREADY primed in the prefetch thread — warm_up then skips its own
+ # priming inference (TTU iteration 11). Reset by recover().
+ self._adopted_primed = False
@property
def is_warm(self) -> bool:
@@ -345,10 +349,12 @@ class FasterWhisperProvider:
with _PREFETCH_LOCK:
prefetched = _PREFETCHED_MODELS.pop(key, None)
if prefetched is not None:
- self._model = prefetched
+ model, primed = prefetched
+ self._model = model
+ self._adopted_primed = bool(primed)
log.info(
- "Adopted boot-prefetched Whisper model (%s/%s/%s).",
- model_name, device, compute_type,
+ "Adopted boot-prefetched Whisper model (%s/%s/%s, primed=%s).",
+ model_name, device, compute_type, primed,
)
return
try:
@@ -393,6 +399,9 @@ class FasterWhisperProvider:
self._model = None
self._infer_lock = threading.Lock()
self._warm = False
+ self._adopted_primed = False
+ # (recover() drops a wedged engine; the next _ensure_model must load
+ # and prime FRESH — never inherit the primed shortcut, AP-24.)
def warm_up(self) -> None:
"""Prime the engine with one throwaway inference so the FIRST live
@@ -416,6 +425,12 @@ class FasterWhisperProvider:
on the first real transcribe), so priming never breaks boot.
"""
self._ensure_model()
+ if self._adopted_primed:
+ # The boot prefetch already primed this exact engine off the
+ # critical path (TTU iteration 11) — a second priming inference
+ # here would just re-pay ~1-2 s on the usable path.
+ self._warm = True
+ return
try:
# ~1 s of very low-amplitude noise: enough signal to exercise the
# full encoder + decoder/beam kernels (pure zeros can early-exit on
diff --git a/tests/unit/stt/test_fwhisper_prefetch.py b/tests/unit/stt/test_fwhisper_prefetch.py
index 7edcbdbd3f..4d863ed98f 100644
--- a/tests/unit/stt/test_fwhisper_prefetch.py
+++ b/tests/unit/stt/test_fwhisper_prefetch.py
@@ -143,6 +143,85 @@ def test_in_flight_prefetch_is_awaited_not_double_loaded(monkeypatch) -> None:
assert len(loads) == 1, "consumer must wait for the in-flight load, not duplicate it"
+class _PrimableModel:
+ """Fake WhisperModel that counts priming transcribes."""
+
+ def __init__(self) -> None:
+ self.transcribes = 0
+
+ def transcribe(self, audio, **kwargs): # noqa: ANN001, ANN003
+ self.transcribes += 1
+ return iter(()), None
+
+
+def test_prefetch_primes_and_warmup_skips_second_prime(monkeypatch) -> None:
+ """TTU iteration 11: the prefetch thread primes the engine too, so
+ ``warm_up`` adopts a READY model and must not re-pay the priming
+ inference on the usable path."""
+ fake = _PrimableModel()
+ monkeypatch.setattr(fw, "_new_whisper_model", lambda *a, **k: fake)
+ assert prefetch_model("base", "cpu", "int8", 2) is True
+ assert fake.transcribes == 1, "prefetch must prime once"
+
+ p = _provider()
+ provider_primes: list[int] = []
+ monkeypatch.setattr(
+ p, "_transcribe_sync", lambda *a, **k: provider_primes.append(1)
+ )
+ p.warm_up()
+ assert p._model is fake
+ assert p.is_warm is True
+ assert provider_primes == [], "warm_up must skip the second prime"
+ assert fake.transcribes == 1
+
+
+def test_unprimed_adoption_still_primes_in_warmup(monkeypatch) -> None:
+ """A prefetch whose priming failed hands over the loaded engine; warm_up
+ then primes it itself — readiness is never faked."""
+ unprimable = object() # no .transcribe -> prefetch priming fails
+ monkeypatch.setattr(fw, "_new_whisper_model", lambda *a, **k: unprimable)
+ assert prefetch_model("base", "cpu", "int8", 2) is True
+
+ p = _provider()
+ provider_primes: list[int] = []
+ monkeypatch.setattr(
+ p, "_transcribe_sync", lambda *a, **k: provider_primes.append(1)
+ )
+ p.warm_up()
+ assert p._model is unprimable
+ assert provider_primes == [1], "warm_up must prime an unprimed adoption"
+ assert p.is_warm is True
+
+
+def test_recover_resets_the_primed_shortcut(monkeypatch) -> None:
+ """After recover() the next warm_up must load AND prime fresh — it may
+ never inherit the primed shortcut from a dropped (possibly wedged)
+ engine (AP-24)."""
+ fake = _PrimableModel()
+ fresh = object()
+ loads: list[int] = []
+
+ def _new(*a, **k): # noqa: ANN002, ANN003
+ loads.append(1)
+ return fake if len(loads) == 1 else fresh
+
+ monkeypatch.setattr(fw, "_new_whisper_model", _new)
+ prefetch_model("base", "cpu", "int8", 2)
+
+ p = _provider()
+ p.warm_up()
+ assert p._adopted_primed is True
+ p.recover()
+ assert p._adopted_primed is False
+ provider_primes: list[int] = []
+ monkeypatch.setattr(
+ p, "_transcribe_sync", lambda *a, **k: provider_primes.append(1)
+ )
+ p.warm_up()
+ assert p._model is fresh
+ assert provider_primes == [1], "post-recover warm_up must prime fresh"
+
+
def test_second_prefetch_call_is_noop(monkeypatch) -> None:
loads: list[int] = []
monkeypatch.setattr(