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(wake): mini-verify-gated early bar reveal for the vosk_kws engine

Ruben Lütke70ecb5b434ab
diff --git a/jarvis/plugins/wake/vosk_kws_provider.py b/jarvis/plugins/wake/vosk_kws_provider.py
index 51349db280..f512582d39 100644
--- a/jarvis/plugins/wake/vosk_kws_provider.py
+++ b/jarvis/plugins/wake/vosk_kws_provider.py
@@ -32,15 +32,31 @@ A raw-energy gate (word-agnostic RMS at the match site, AP-27) rejects
near-silent candidates before the confirm. The detector never emits
transcript text — its only output is the fired keyword (design criterion:
user speech must never double-enter the pipeline through the wake path).
+
+**Early candidate (visual-only, mini-verify gated).** A PARTIAL hit waits
+``confirm_tail_s`` before the authoritative verify, so the bar used to react
+~0.85-1.0 s after the phrase. The early candidate runs the SAME three verify
+checks immediately over the audio heard so far (truncated ring) in a worker
+thread and, only when ALL pass, tells ``early_candidate_listener`` to show
+the bar — typically ~0.1-0.25 s after the partial, i.e. around phrase end.
+Calibrated 2026-07-11 on real captured windows (400 pos / 2500 neg per
+phrase): a conf gate alone leaks 0.84-12 % of room-speech windows (the
+flicker that got the plain candidate reveal reverted, 5fe5c4d2); the full
+mini-verify leaks 0/2500 ("hey jarvis") and 1-2/2500 (worst-case single
+word) while early-revealing ~a third of eventual fires. A rejected or
+superseded candidate retracts; infrastructure errors fail CLOSED here
+(visual-only — the opposite polarity of the authoritative confirm, which
+fails open so it can never eat a real wake).
"""
from __future__ import annotations
import asyncio
+import contextlib
import json
import logging
import time
from collections import deque
-from collections.abc import AsyncIterator
+from collections.abc import AsyncIterator, Awaitable, Callable
from concurrent.futures import ThreadPoolExecutor
from difflib import SequenceMatcher
from typing import Any
@@ -145,6 +161,11 @@ class VoskKwsProvider:
# -3 dBFS (gain capped at 40 dB) exactly like the other wake paths.
target_peak: float = 0.7079,
max_gain: float = 100.0,
+ # Visual-only early candidate: awaited with True when the mini-verify
+ # passes at PARTIAL time, False when a shown candidate is retracted.
+ # Never carries transcript text; never fires unverified (see module
+ # docstring + tests). None = feature off (default).
+ early_candidate_listener: Callable[[bool], Awaitable[None]] | None = None,
) -> None:
self._phrase = phrase.strip()
self._keyword = keyword or "_".join(normalize_phrase_for_match(phrase)) or "wake"
@@ -179,6 +200,14 @@ class VoskKwsProvider:
self._ring: deque[bytes] = deque()
self._ring_len = 0
self._ring_max = int(_RING_SECONDS * sample_rate) * 2 # bytes
+ # Early-candidate state: the listener, whether a candidate is shown,
+ # the in-flight mini-verify task, and a generation counter so a LATE
+ # mini-verify completion can never show a candidate the authoritative
+ # verify already resolved.
+ self._early_listener = early_candidate_listener
+ self._early_active = False
+ self._early_task: asyncio.Task[None] | None = None
+ self._pending_gen = 0
# Session stats (parity with OpenWakeWordProvider.stats()).
self._stat_chunks = 0
self._stat_candidates = 0
@@ -186,6 +215,8 @@ class VoskKwsProvider:
self._stat_suppressed_confirm = 0
self._stat_suppressed_cooldown = 0
self._stat_fired = 0
+ self._stat_early_shown = 0
+ self._stat_early_retracted = 0
# -- lifecycle -----------------------------------------------------------
@@ -243,6 +274,13 @@ class VoskKwsProvider:
await asyncio.to_thread(self._warmup_decode)
async def stop(self) -> None:
+ # Invalidate any in-flight early check and retract a shown candidate
+ # so a detector swap can never leave the bar stuck "candidate".
+ self._pending_gen += 1
+ if self._early_task is not None:
+ self._early_task.cancel()
+ self._early_task = None
+ await self._notify_early(False)
self._model = None
self._ring.clear()
self._ring_len = 0
@@ -255,8 +293,65 @@ class VoskKwsProvider:
"suppressed_confirm": self._stat_suppressed_confirm,
"suppressed_cooldown": self._stat_suppressed_cooldown,
"fired": self._stat_fired,
+ "early_shown": self._stat_early_shown,
+ "early_retracted": self._stat_early_retracted,
}
+ # -- early candidate (visual-only) ----------------------------------------
+
+ def set_early_candidate_listener(
+ self, listener: Callable[[bool], Awaitable[None]] | None
+ ) -> None:
+ """Wire/unwire the visual candidate listener after construction."""
+ self._early_listener = listener
+
+ @property
+ def early_candidate_active(self) -> bool:
+ """Whether a shown early candidate is currently outstanding."""
+ return self._early_active
+
+ def consume_early_candidate(self) -> bool:
+ """Hand the shown-candidate state to the pipeline (reset, no event).
+
+ Called once per yielded keyword: the pipeline needs to know whether
+ the bar is already visible so a silently DROPPED wake (post-hangup
+ echo lock, app not activatable) retracts it instead of leaving it
+ stuck "candidate" with no session behind it. Resetting here also
+ re-arms the show guard for the next candidate.
+ """
+ was_shown = self._early_active
+ self._early_active = False
+ return was_shown
+
+ async def _notify_early(self, active: bool) -> None:
+ if self._early_listener is None or active == self._early_active:
+ return
+ self._early_active = active
+ if active:
+ self._stat_early_shown += 1
+ else:
+ self._stat_early_retracted += 1
+ try:
+ await self._early_listener(active)
+ except Exception as exc: # noqa: BLE001 — a UI listener error must
+ # never break wake detection.
+ log.debug("early-candidate listener failed: %s", exc)
+
+ async def _run_early_check(self, window: np.ndarray, gen: int) -> None:
+ """Mini-verify the truncated ring; show the candidate only if the
+ pending candidate that spawned this check is still unresolved."""
+ try:
+ ok = await asyncio.to_thread(self._early_check, window)
+ except Exception as exc: # noqa: BLE001 — visual-only, never disrupt
+ log.debug("early-candidate check errored: %s", exc)
+ return
+ if ok and gen == self._pending_gen:
+ log.info(
+ "vosk-kws: EARLY candidate shown for %r (mini-verify passed)",
+ self._phrase,
+ )
+ await self._notify_early(True)
+
# -- internals -----------------------------------------------------------
def _ring_push(self, pcm: bytes) -> None:
@@ -291,7 +386,19 @@ class VoskKwsProvider:
return None
def _verify_candidate(self, window: np.ndarray) -> bool:
- """Three checks over the ring window; ALL must pass before a fire.
+ """Authoritative confirm — fails OPEN (never eat a real wake)."""
+ return self._verify_window(window, fail_open=True)
+
+ def _early_check(self, window: np.ndarray) -> bool:
+ """Visual-only mini-verify on the truncated ring — fails CLOSED
+ (never flash the bar on a broken verifier)."""
+ try:
+ return self._verify_window(window, fail_open=False)
+ except Exception: # noqa: BLE001 — belt and braces for the UI path
+ return False
+
+ def _verify_window(self, window: np.ndarray, *, fail_open: bool) -> bool:
+ """Three checks over the given window; ALL must pass before a fire.
Why this shape (live forensic 2026-07-06, "Hey Ruben" fired on plain
room speech every few minutes): the streaming PARTIAL that makes the
@@ -313,8 +420,10 @@ class VoskKwsProvider:
judges what was said AT the candidate's position instead of
fishing the best pair out of three seconds of conversation.
- Fail-OPEN only on infrastructure errors (a broken confirm must never
- eat a real wake); a clean "the phrase is not there" is a rejection.
+ On infrastructure errors the ``fail_open`` polarity decides: the
+ authoritative confirm accepts (a broken confirm must never eat a real
+ wake), the visual-only early check rejects. A clean "the phrase is
+ not there" is always a rejection.
Latency (spawn-latency mission, 2026-07-10): the grammar re-score and
the free decode are independent Kaldi passes over the SAME audio —
@@ -409,9 +518,13 @@ class VoskKwsProvider:
if w.get("end", 0.0) >= span_a and w.get("start", 0.0) <= span_b
]
free_local = " ".join(local)
- except Exception as exc: # noqa: BLE001 — fail-open, never eat a wake
- log.warning("vosk-kws: verify failed (%s) — accepting.", exc)
- return True
+ except Exception as exc: # noqa: BLE001 — polarity via fail_open
+ log.warning(
+ "vosk-kws: verify failed (%s) — %s.",
+ exc,
+ "accepting" if fail_open else "rejecting (visual-only)",
+ )
+ return fail_open
ok = sound_confirm(free_local, self._phrase, ratio=self._confirm_ratio)
log.info(
"vosk-kws: verify %s — free ear heard %r at the candidate span "
@@ -473,14 +586,43 @@ class VoskKwsProvider:
if not is_final and self._confirm_tail_bytes > 0:
pending = (is_final, conf)
pending_tail = 0
+ # Visual-only early candidate: mini-verify the audio heard
+ # SO FAR in a worker thread while the confirm tail keeps
+ # accumulating. Typically done ~0.1-0.25 s later — the bar
+ # reacts around phrase end instead of ~0.85 s after it.
+ if self._early_listener is not None:
+ self._pending_gen += 1
+ self._early_task = asyncio.create_task(
+ self._run_early_check(
+ self._ring_window(), self._pending_gen
+ )
+ )
continue
now = time.time()
window = self._ring_window()
confirmed = await asyncio.to_thread(self._verify_candidate, window)
if not confirmed:
self._stat_suppressed_confirm += 1
+ # Resolve the pending candidate: invalidate any in-flight
+ # early check (generation bump) and retract a shown bar.
+ self._pending_gen += 1
+ if self._early_task is not None:
+ with contextlib.suppress(Exception):
+ await self._early_task
+ self._early_task = None
+ await self._notify_early(False)
rec = self._new_grammar_rec()
continue
+ # Confirmed: let a still-running early check finish first (in
+ # production it completed long ago — the tail wait dwarfs it) so
+ # show-then-wake ordering is deterministic. The shown flag stays
+ # set for the pipeline to CONSUME: it must know the bar is visible
+ # when it silently drops this wake (echo lock) and retract it.
+ if self._early_task is not None:
+ with contextlib.suppress(Exception):
+ await self._early_task
+ self._early_task = None
+ self._pending_gen += 1
self._stat_fired += 1
last_fire_t = now
log.info(
diff --git a/jarvis/speech/pipeline.py b/jarvis/speech/pipeline.py
index d752cab004..72b9c3750a 100644
--- a/jarvis/speech/pipeline.py
+++ b/jarvis/speech/pipeline.py
@@ -1141,6 +1141,7 @@ class SpeechPipeline:
phrase=wake_plan.phrase,
model_path=wake_plan.vosk_model_path or "",
keyword=wake_plan.oww_keyword,
+ early_candidate_listener=self._vosk_early_candidate_listener,
)
elif wake_plan is not None:
self._wake = OpenWakeWordProvider(
@@ -1857,6 +1858,7 @@ class SpeechPipeline:
phrase=plan.phrase,
model_path=getattr(plan, "vosk_model_path", None) or "",
keyword=plan.oww_keyword,
+ early_candidate_listener=self._vosk_early_candidate_listener,
)
self._openwakeword_enabled = True
if self._whisper_wake is not None and self._wake_matcher is not None:
@@ -4440,6 +4442,8 @@ class SpeechPipeline:
reject costs one brief bar flash. Custom ONNX and Vosk grammar stage-one
candidates both match ordinary speech frequently, so those engines wait
for the authoritative ``WakeWordDetected`` event after full verification.
+ (vosk_kws additionally gets the PROVIDER-gated early candidate below —
+ that one is mini-verified, not raw, and does not pass through here.)
"""
if self._state != PipelineState.IDLE:
return False
@@ -4448,6 +4452,24 @@ class SpeechPipeline:
return True
return getattr(plan, "engine", "") == "openwakeword"
+ async def _vosk_early_candidate_listener(self, active: bool) -> None:
+ """Visual-only bar signal from the vosk provider's mini-verify.
+
+ Deliberately NOT routed through ``_should_show_optimistic_candidate``:
+ that helper gates the RAW pre-verify reveal (openwakeword-only, see
+ revert 5fe5c4d2). The vosk provider only calls this after its
+ mini-verify passed — re-score confidence + span RMS + localized sound
+ confirm on the truncated ring — measured at 0/2500 room-speech windows
+ shown for "hey jarvis" (calibration 2026-07-11), so the flicker class
+ that forced the revert cannot recur. Retracts (active=False) always
+ pass through so a shown bar can never stay stuck.
+ """
+ if active and self._state != PipelineState.IDLE:
+ return # a session is already running; nothing to reveal
+ await self._publish_event(
+ WakeCandidateDetected(source_layer="speech", active=active)
+ )
+
async def _verify_oww_hit(self, pcm_snapshot: bytes) -> bool:
"""Second-stage gate: ask the utterance STT whether the few seconds
leading up to an OpenWakeWord hit actually contained the configured
@@ -4799,8 +4821,15 @@ class SpeechPipeline:
# retract the optimistic candidate instead of lying.
now = time.time()
locked = now < self._wake_lock_until
+ # The vosk provider may have shown a mini-verified
+ # early candidate for THIS wake — consume the flag
+ # either way; retract below if the wake is dropped.
+ consume = getattr(
+ self._wake, "consume_early_candidate", None
+ )
+ early_shown = consume() if callable(consume) else False
if locked or not self._activation_allowed():
- if locals().get("show_candidate"):
+ if locals().get("show_candidate") or early_shown:
await self._publish_event(
WakeCandidateDetected(
source_layer="speech", active=False
diff --git a/tests/unit/plugins/wake/test_vosk_kws_provider.py b/tests/unit/plugins/wake/test_vosk_kws_provider.py
index 936b0d4e65..3869567802 100644
--- a/tests/unit/plugins/wake/test_vosk_kws_provider.py
+++ b/tests/unit/plugins/wake/test_vosk_kws_provider.py
@@ -207,19 +207,98 @@ async def test_near_silent_candidate_is_gated_on_energy(fake_vosk) -> None:
assert p.stats()["gated_rms"] >= 1
-# --- unverified-candidate boundary -------------------------------------------
+# --- early-candidate boundary (mini-verify gated) ------------------------------
-def test_provider_has_no_unverified_candidate_callback() -> None:
- """Stage-one grammar hits are intentionally noisy and must remain internal.
+async def test_raw_stage_one_hit_never_reaches_the_early_listener(fake_vosk) -> None:
+ """Stage-one grammar hits stay internal (revert 5fe5c4d2): the ONLY
+ outward candidate signal is the mini-verify-gated early candidate.
- Only the confirmed keyword yielded by ``detect`` may cross into the speech
- pipeline. Exposing a pre-confirm callback previously made the JarvisBar
- flash for ordinary speech even though the verifier rejected every hit.
+ A weak re-score (conf 0.2) means the mini-verify rejects — the listener
+ must hear NOTHING even though stage one fired. This replaces the older
+ "no callback parameter exists" guard: the boundary is behavioural now —
+ unverified candidates never cross, verified ones may.
"""
- import inspect
+ events: list[bool] = []
- assert "on_candidate" not in inspect.signature(VoskKwsProvider).parameters
+ async def _listener(active: bool) -> None:
+ events.append(active)
+
+ p = VoskKwsProvider(
+ "Hey Nova", model_path="fake", keyword="nova",
+ early_candidate_listener=_listener,
+ )
+ p._ensure_model()
+ fake_vosk["model"].grammar_conf = 0.2 # weak re-hear -> mini-verify rejects
+ fired = await _run_detect(p, [_chunk() for _ in range(12)])
+ assert fired == []
+ assert events == []
+
+
+async def test_early_candidate_fires_after_mini_verify_then_wake(fake_vosk) -> None:
+ # Calibration 2026-07-11 (400 pos / 2500 neg real windows, "hey jarvis"):
+ # conf-gate alone leaks 0.84-0.92% of room-speech windows to the bar, the
+ # mini-verify (re-score conf + span RMS + localized sound confirm on the
+ # TRUNCATED ring) leaks 0 of 2500. The early candidate therefore runs the
+ # full mini-verify, and a passing one shows the bar BEFORE the 0.6s
+ # confirm tail + authoritative verify complete.
+ events: list[bool] = []
+
+ async def _listener(active: bool) -> None:
+ events.append(active)
+
+ p = VoskKwsProvider(
+ "Hey Nova", model_path="fake", keyword="nova",
+ early_candidate_listener=_listener,
+ )
+ fired = await _run_detect(p, [_chunk() for _ in range(12)])
+ assert fired == ["nova"]
+ assert events == [True] # shown once; the wake event supersedes (no retract)
+ assert p.stats()["early_shown"] == 1
+ assert p.stats()["early_retracted"] == 0
+ # The shown flag survives the fire for the pipeline to CONSUME (it needs
+ # to retract the bar if it silently drops this wake), then resets.
+ assert p.consume_early_candidate() is True
+ assert p.early_candidate_active is False
+ assert p.consume_early_candidate() is False
+
+
+async def test_early_candidate_retracts_when_confirm_rejects(
+ fake_vosk, monkeypatch
+) -> None:
+ # The bar must never stay stuck on a candidate the authoritative verify
+ # rejects: shown -> retracted, in that order.
+ events: list[bool] = []
+
+ async def _listener(active: bool) -> None:
+ events.append(active)
+
+ p = VoskKwsProvider(
+ "Hey Nova", model_path="fake", keyword="nova",
+ early_candidate_listener=_listener,
+ )
+ monkeypatch.setattr(p, "_early_check", lambda window: True)
+ p._ensure_model()
+ fake_vosk["model"].free_text = "das ist etwas ganz anderes" # i18n-allow: utterance under test
+ fired = await _run_detect(p, [_chunk() for _ in range(12)])
+ assert fired == []
+ assert events == [True, False]
+ assert p.stats()["early_shown"] == 1
+ assert p.stats()["early_retracted"] == 1
+
+
+def test_early_check_fails_closed_on_infra_error(fake_vosk, monkeypatch) -> None:
+ # Polarity contract: the AUTHORITATIVE confirm fails OPEN (never eat a
+ # real wake), the VISUAL-ONLY early check fails CLOSED (never flash the
+ # bar on a broken verifier).
+ p = VoskKwsProvider("Hey Nova", model_path="fake", keyword="nova")
+
+ def _boom():
+ raise RuntimeError("confirm infra down")
+
+ monkeypatch.setattr(p, "_ensure_model", _boom)
+ assert p._early_check(np.full(1600, 0.2, dtype=np.float32)) is False
+ assert p._verify_candidate(np.full(1600, 0.2, dtype=np.float32)) is True
def test_confirm_infrastructure_error_fails_open(fake_vosk, monkeypatch) -> None: