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(wake): vosk verify - re-score confidence + span-localised confirm kill room-speech ghosts

Ruben Lütkee53e5355a461
diff --git a/jarvis/plugins/wake/vosk_kws_provider.py b/jarvis/plugins/wake/vosk_kws_provider.py
index 0bbd419f24..c2e12a227f 100644
--- a/jarvis/plugins/wake/vosk_kws_provider.py
+++ b/jarvis/plugins/wake/vosk_kws_provider.py
@@ -51,11 +51,16 @@ from jarvis.speech.wake_constants import normalize_phrase_for_match, sound_fold
log = logging.getLogger("jarvis.wake.vosk")
-# Minimum per-word grammar confidence on a FINAL result. Spike distribution:
-# genuine wakes p25..max = 1.0 (one outlier 0.483), false accepts p50 = 0.707
-# — so 0.5 keeps nearly every genuine hit and trims the weakest ghosts before
-# the (decisive) sound confirm.
-_MIN_FINAL_CONF = 0.5
+# Minimum per-word grammar confidence for the verify RE-SCORE over the ring
+# window. This is the precision anchor (live forensic 2026-07-06, "Hey Ruben"
+# fired on plain room speech): genuine wakes re-score at ~1.0 (spike
+# distribution p25..max = 1.0), room speech pulled onto the phrase re-scores
+# lower. Calibrated on 6 min of judged continuous room speech vs the
+# hardest common-sound phrase: conf>=0.9 + the permissive 0.55 sound ratio =
+# 0 false fires at Ruben 20/24 / Luca 8/8 recall; conf 0.5 leaked
+# 0.84 fires/min. Raising the sound ratio instead costs recall (AP-27) —
+# keep the ratio permissive and let the REAL confidence carry precision.
+_MIN_FINAL_CONF = 0.9
# Sound-similarity floor for the free-decode confirm. Spike sweep: 0.55 keeps
# 79-100 % recall at 1/0/0 % false accepts; 0.45 lifts recall ~8 points but
@@ -243,11 +248,31 @@ class VoskKwsProvider:
return (False, 1.0) # partials carry no conf; the confirm decides
return None
- def _free_confirm(self, window: np.ndarray) -> bool:
- """Unconstrained decode over the ring window + permissive sound match.
-
- Fail-OPEN on infrastructure errors: a broken confirm must never eat a
- real wake (mirrors the stt_match echo-confirm contract).
+ def _verify_candidate(self, window: np.ndarray) -> bool:
+ """Three checks over the ring 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
+ detector fast carries no confidence — its 1.00 placeholder sailed
+ through the conf gate — and the old confirm compared the phrase
+ against the BEST two-word window anywhere in ~3 s of speech, so for a
+ phrase built from common German sounds SOME pair ("herr oben",
+ "bei ihm") always eventually cleared 0.55. Both holes were
+ word-dependent — exactly the class this engine exists to kill.
+
+ 1. **Grammar re-score**: a fresh grammar pass over the window must
+ re-hear the phrase as a FINAL — yielding a real per-word confidence
+ (gate: ``min_final_conf``) and the phrase's TIME SPAN.
+ 2. **Energy**: the word-agnostic RMS floor is measured over that span
+ (not the whole ring, where surrounding silence dilutes it — AP-27:
+ silence gates on energy, never transcript).
+ 3. **Localised sound confirm**: the free decode keeps only words
+ OVERLAPPING the span (±0.3 s) — the permissive sound match then
+ 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.
"""
try:
from vosk import KaldiRecognizer
@@ -258,17 +283,65 @@ class VoskKwsProvider:
window * min(self._target_peak / peak, self._max_gain), -1.0, 1.0
)
pcm = (window * 32767.0).astype(np.int16).tobytes()
- rec = KaldiRecognizer(self._ensure_model(), self._sample_rate)
- rec.AcceptWaveform(pcm)
- free_text = json.loads(rec.FinalResult()).get("text", "")
+
+ # 1) grammar re-score: real confidence + time span for the phrase
+ g = self._new_grammar_rec()
+ g.AcceptWaveform(pcm)
+ gres = json.loads(g.FinalResult())
+ gwords = [
+ w for w in gres.get("result", [])
+ if w.get("word") in self._grammar_words
+ ]
+ if self._phrase.lower() not in gres.get("text", "") or not gwords:
+ log.info(
+ "vosk-kws: verify SUPPRESSED — re-score did not re-hear "
+ "%r (heard %r)",
+ self._phrase, gres.get("text", "")[:60],
+ )
+ return False
+ conf = min(w.get("conf", 0.0) for w in gwords)
+ if conf < self._min_final_conf:
+ log.info(
+ "vosk-kws: verify SUPPRESSED — re-score conf %.2f < %.2f "
+ "for %r", conf, self._min_final_conf, self._phrase,
+ )
+ return False
+ span_a = min(w.get("start", 0.0) for w in gwords) - 0.3
+ span_b = max(w.get("end", 0.0) for w in gwords) + 0.3
+
+ # 2) word-agnostic energy over the phrase span
+ a = max(0, int(span_a * self._sample_rate))
+ b = min(len(window), int(span_b * self._sample_rate))
+ segment = window[a:b] if b > a else window
+ rms = float(np.sqrt(np.mean(segment * segment) + 1e-12)) if len(segment) else 0.0
+ if rms < self._match_min_rms:
+ self._stat_gated_rms += 1
+ log.info(
+ "vosk-kws: verify SUPPRESSED — span rms %.4f < %.4f "
+ "(silence can never fire)", rms, self._match_min_rms,
+ )
+ return False
+
+ # 3) free decode, localised to the phrase span
+ f = KaldiRecognizer(self._ensure_model(), self._sample_rate)
+ f.SetWords(True)
+ f.AcceptWaveform(pcm)
+ fres = json.loads(f.FinalResult())
+ local = [
+ w.get("word", "") for w in fres.get("result", [])
+ 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: free confirm failed (%s) — accepting.", exc)
+ log.warning("vosk-kws: verify failed (%s) — accepting.", exc)
return True
- ok = sound_confirm(free_text, self._phrase, ratio=self._confirm_ratio)
+ ok = sound_confirm(free_local, self._phrase, ratio=self._confirm_ratio)
log.info(
- "vosk-kws: confirm %s — free ear heard %r vs phrase %r",
+ "vosk-kws: verify %s — free ear heard %r at the candidate span "
+ "(conf=%.2f) vs phrase %r",
"OK" if ok else "SUPPRESSED",
- free_text[:60],
+ free_local[:60],
+ conf,
self._phrase,
)
return ok
@@ -321,13 +394,7 @@ class VoskKwsProvider:
continue
now = time.time()
window = self._ring_window()
- rms = float(np.sqrt(np.mean(window * window) + 1e-12)) if len(window) else 0.0
- if rms < self._match_min_rms:
- # AP-27: silence is rejected on raw ENERGY, never on transcript
- self._stat_gated_rms += 1
- rec = self._new_grammar_rec()
- continue
- confirmed = await asyncio.to_thread(self._free_confirm, window)
+ confirmed = await asyncio.to_thread(self._verify_candidate, window)
if not confirmed:
self._stat_suppressed_confirm += 1
rec = self._new_grammar_rec()
@@ -335,11 +402,9 @@ class VoskKwsProvider:
self._stat_fired += 1
last_fire_t = now
log.info(
- "vosk-kws: WAKE fired for %r (%s, conf=%.2f, rms=%.4f)",
+ "vosk-kws: WAKE fired for %r (%s candidate)",
self._phrase,
"final" if is_final else "partial",
- conf,
- rms,
)
yield self._keyword
rec = self._new_grammar_rec()
diff --git a/tests/unit/plugins/wake/test_vosk_kws_provider.py b/tests/unit/plugins/wake/test_vosk_kws_provider.py
index f383e23e78..563732cfc5 100644
--- a/tests/unit/plugins/wake/test_vosk_kws_provider.py
+++ b/tests/unit/plugins/wake/test_vosk_kws_provider.py
@@ -51,12 +51,23 @@ def test_sound_confirm_finds_phrase_inside_longer_speech() -> None:
# --- fake vosk runtime ----------------------------------------------------------
+def _timed_words(text: str, conf: float, start: float = 0.5) -> list[dict]:
+ out = []
+ t = start
+ for w in text.split():
+ out.append({"word": w, "start": t, "end": t + 0.3, "conf": conf})
+ t += 0.35
+ return out
+
+
class _FakeRecognizer:
"""Scriptable KaldiRecognizer stand-in.
Grammar mode (grammar arg passed): after ``fire_after`` chunks the partial
- contains the phrase. Free mode (no grammar): FinalResult returns
- ``free_text`` — the knob the confirm-path tests turn.
+ contains the phrase; FinalResult re-hears the phrase with timed words at
+ ``model.grammar_conf`` (the verify re-score input). Free mode (no
+ grammar): FinalResult returns ``model.free_text`` with timed words in the
+ same span — the knob the confirm-path tests turn.
"""
def __init__(self, model, rate, grammar=None): # noqa: ANN001
@@ -80,7 +91,16 @@ class _FakeRecognizer:
return json.dumps({"text": ""})
def FinalResult(self): # noqa: N802
- return json.dumps({"text": self._model.free_text})
+ if self._grammar is not None:
+ phrase = self._model.phrase.lower()
+ return json.dumps({
+ "text": phrase,
+ "result": _timed_words(phrase, self._model.grammar_conf),
+ })
+ return json.dumps({
+ "text": self._model.free_text,
+ "result": _timed_words(self._model.free_text, 0.9),
+ })
class _FakeModel:
@@ -88,6 +108,7 @@ class _FakeModel:
self.path = path
self.phrase = "hey nova"
self.free_text = "hey nova"
+ self.grammar_conf = 0.95
self.fire_after = 3
@@ -183,7 +204,7 @@ async def test_near_silent_candidate_is_gated_on_energy(fake_vosk) -> None:
def test_confirm_infrastructure_error_fails_open(fake_vosk, monkeypatch) -> None:
# A broken confirm must never eat a real wake (mirrors the echo-confirm
- # contract on the stt_match path): _free_confirm returns True on ANY
+ # contract on the stt_match path): _verify_candidate returns True on ANY
# infrastructure exception.
p = VoskKwsProvider("Hey Nova", model_path="fake", keyword="nova")
@@ -191,7 +212,30 @@ def test_confirm_infrastructure_error_fails_open(fake_vosk, monkeypatch) -> None
raise RuntimeError("confirm infra down")
monkeypatch.setattr(p, "_ensure_model", _boom)
- assert p._free_confirm(np.full(1600, 0.2, dtype=np.float32)) is True
+ assert p._verify_candidate(np.full(1600, 0.2, dtype=np.float32)) is True
+
+
+def test_low_rescore_confidence_suppresses(fake_vosk) -> None:
+ # Live forensic 2026-07-06: the streaming PARTIAL has no confidence (its
+ # 1.00 placeholder passed the gate), so room speech fired constantly.
+ # The verify re-score must supply a REAL confidence and gate on it.
+ p = VoskKwsProvider("Hey Nova", model_path="fake", keyword="nova")
+ p._ensure_model()
+ fake_vosk["model"].grammar_conf = 0.2 # weak re-hear -> not a wake
+ assert p._verify_candidate(np.full(48000, 0.2, dtype=np.float32)) is False
+
+
+def test_free_words_outside_the_span_cannot_confirm(fake_vosk) -> None:
+ # The sound confirm must judge what was said AT the candidate's position;
+ # a sound-close pair elsewhere in the 3 s window must not count.
+ p = VoskKwsProvider("Hey Nova", model_path="fake", keyword="nova")
+ p._ensure_model()
+ m = fake_vosk["model"]
+ m.free_text = "ganz andere worte hier hey nowa" # i18n-allow: utterance under test
+ # free words start at 0.5s and stride 0.35s -> "hey nowa" sits ~1.9-2.6s,
+ # far outside the grammar span (0.5-1.15s +-0.3) -> localised confirm
+ # sees only unrelated words and rejects.
+ assert p._verify_candidate(np.full(48000, 0.2, dtype=np.float32)) is False
async def test_cooldown_suppresses_immediate_refire(fake_vosk) -> None: