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.
Python agent-orchestrationai-agentai-coding-assistantclaude-codecoding-agentcomputer-usedesktop-applocal-firstmcppersonal-jarvisvoice-assistantvoice-coding
fix(wake): stop the verify from eating wakes it cannot spell
Ruben Lütkecafbbdc70890
diff --git a/jarvis/plugins/wake/vosk_kws_provider.py b/jarvis/plugins/wake/vosk_kws_provider.pyindex 4f71d92ff8..c26bbbaf4c 100644--- a/jarvis/plugins/wake/vosk_kws_provider.py+++ b/jarvis/plugins/wake/vosk_kws_provider.py@@ -129,6 +129,43 @@ _MERGED_MAX_LENGTH_RATIO = 1.20# energy, never on transcript content)._MATCH_MIN_RMS = 0.006+# --- word-agnostic candidate shape (AP-27, forensic 2026-07-13) -------------+# Everything above asks the free decoder to SPELL the wake word. An offline+# small model has no arbitrary proper noun in its lexicon, so it CANNOT.+# Replaying 159 real captured "Hey Ruben" calls (data/wake_debug) through this+# detector: the free decoder spelled the phrase in only 28 % of genuine calls+# and otherwise produced sound-alike garbage — "herum", "erhoben", "hey room",+# "hey oben", "heroes". Rejecting on that garbage ate 38 % of all real wakes+# (end-to-end recall 32 %; the user had to repeat the phrase four or five+# times) while false accepts sat at 0/400. And no spelling threshold can close+# it: the free transcript "herr oben" was produced BOTH by a genuine call and+# by room chatter. Spelling cannot discriminate an out-of-vocabulary word — and+# EVERY wake word is out-of-vocabulary for some installed language model. That+# is precisely AP-27, reappearing in the Vosk path.+#+# What DOES discriminate, without ever asking how the wake word is written, is+# the SHAPE of what the free ear heard AT the candidate span:+# * a wake call is short and stands alone (measured: 0.72 s, 2 words)+# * room speech is a longer word stream the (measured: 1.29 s, 5 words,+# decoder confidently recognises top word confidence ~1.0)+# Both bounds are derived from the CONFIGURED phrase, never from its spelling,+# so they hold for any phrase in any supported language. ``sound_confirm``+# remains a BONUS path that may only ACCEPT (a free ear that did spell the+# phrase still fires instantly), so this can never make the detector deaf.+#+# Calibrated on 250 positive / 1650 negative real captured windows: verify+# pass-rate on genuine calls 55 % -> 74 %, at 3 false accepts (two of which are+# genuine calls the corpus labels negative: "ey ruben", "hei ruben").+_SHAPE_MAX_VOICED_S_PER_TOKEN = 0.65+_SHAPE_MAX_OTHER_WORD_CONF = 0.98+# No slack: the free ear may not hear MORE words at the span than the phrase+# itself has. Allowing one extra token to absorb an ASR split ("Jarvis" ->+# "joe avis") measurably let compact room speech through (5 vs 3 false accepts+# on 1650 real negative windows) — and it is not needed: a split core is+# exactly what ``sound_confirm``'s core_sizes tolerance already accepts, so the+# split case is covered by the spelling path and must not be paid for twice.+_SHAPE_TOKEN_SLACK = 0+# Ring buffer length for the confirm pass — long enough to hold the full# spoken phrase plus lead-in at the moment the partial trigger fires._RING_SECONDS = 3.0@@ -261,6 +298,54 @@ def sound_confirm(free_text: str, phrase: str, *, ratio: float = _CONFIRM_RATIO)return False+def candidate_shape_ok(+ local_words: Sequence[dict],+ phrase: str,+ *,+ max_voiced_s_per_token: float = _SHAPE_MAX_VOICED_S_PER_TOKEN,+ max_other_word_conf: float = _SHAPE_MAX_OTHER_WORD_CONF,+) -> bool:+ """Does the free ear's output AT the candidate span look like a wake call?++ Word-agnostic by construction (AP-27): it reads only how much was said at+ the span and how sure the free decoder was — never how the wake word is+ spelled. ``local_words`` are the free decode's word dicts (``word``,+ ``start``, ``end``, ``conf``) already localised to the phrase span.++ Three word-agnostic questions, all scaled by the configured phrase:++ 1. **Not more words than the phrase has.** A wake call is the phrase; a+ forced grammar hit on conversation carries the surrounding words too.+ (A free decoder that SPLITS the name into two tokens — "Jarvis" ->+ "joe avis" — is already accepted by ``sound_confirm``'s core_sizes+ tolerance, so this gate does not need to pay for that case with the+ extra false accepts a token slack costs.)+ 2. **Not spoken for longer than the phrase could be.** The grammar happily+ stretches the phrase across flowing speech; a real call cannot last+ longer than its own tokens do.+ 3. **The free ear is not SURE it heard something else.** This is the+ positive signal an out-of-vocabulary wake word leaves behind: the free+ decoder does not know the word, so it guesses and its confidence drops.+ Ordinary speech ("engineering", "google") it recognises outright.++ Empty input rejects: the grammar claimed the phrase where the free ear+ heard no speech at all.+ """+ if not local_words:+ return False+ n_tokens = max(1, len(normalize_phrase_for_match(phrase)))+ if len(local_words) > n_tokens + _SHAPE_TOKEN_SLACK:+ return False+ voiced_s = sum(+ max(0.0, float(w.get("end", 0.0)) - float(w.get("start", 0.0)))+ for w in local_words+ )+ if voiced_s > max_voiced_s_per_token * n_tokens:+ return False+ top_conf = max(float(w.get("conf", 0.0)) for w in local_words)+ return top_conf <= max_other_word_conf++class VoskKwsProvider:"""Any-word wake detector — structurally compatible with `WakeWordProvider`.@@ -771,11 +856,11 @@ class VoskKwsProvider:return False# localise the (already-decoded) free words to the phrase span- local = [- w.get("word", "") for w in fres.get("result", [])+ local_words = [+ w 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)+ free_local = " ".join(w.get("word", "") for w in local_words)except Exception as exc: # noqa: BLE001 — polarity via fail_openlog.warning("vosk-kws: verify failed (%s) — %s.",@@ -783,12 +868,25 @@ class VoskKwsProvider:"accepting" if fail_open else "rejecting (visual-only)",)return fail_open+ # Two INDEPENDENT ways to confirm, either of which is sufficient:+ # (a) the free ear spelled the phrase -> sound_confirm+ # (b) it could not spell it (an offline model cannot spell an+ # arbitrary proper noun) but what it heard at the span has the+ # SHAPE of a wake call -> candidate_shape_ok (word-agnostic)+ # (a) alone was the AP-27 recall trap: it ate 38 % of real wakes,+ # because the wake word is out-of-vocabulary for the very decoder being+ # asked to write it down. (b) can never depend on the phrase's+ # spelling, so it holds for every wake word in every language.ok = sound_confirm(free_local, self._phrase, ratio=self._confirm_ratio)+ by_shape = False+ if not ok:+ ok = by_shape = candidate_shape_ok(local_words, self._phrase)log_method = log.info if ok else log.debuglog_method(- "vosk-kws: verify %s — free ear heard %r at the candidate span "+ "vosk-kws: verify %s (%s) — free ear heard %r at the candidate span ""(conf=%.2f) vs phrase %r","OK" if ok else "SUPPRESSED",+ ("shape" if by_shape else "spelled") if ok else "neither",free_local[:60],conf,self._phrase,@@ -1038,4 +1136,9 @@ def vosk_model_supports_phrase(model_path: str, phrase: str) -> bool:return "missing in vocabulary" not in tmp.read().lower()-__all__ = ["VoskKwsProvider", "sound_confirm", "vosk_model_supports_phrase"]+__all__ = [+ "VoskKwsProvider",+ "candidate_shape_ok",+ "sound_confirm",+ "vosk_model_supports_phrase",+]diff --git a/tests/unit/plugins/wake/test_vosk_wake_word_agnostic.py b/tests/unit/plugins/wake/test_vosk_wake_word_agnostic.pynew file mode 100644index 0000000000..bb60601106--- /dev/null+++ b/tests/unit/plugins/wake/test_vosk_wake_word_agnostic.py@@ -0,0 +1,205 @@+"""The wake verify must never depend on the free decoder SPELLING the wake word.++AP-27 in the Vosk path. Forensic (2026-07-13, 159 real captured "Hey Ruben"+calls from data/wake_debug, replayed through the production two-stage detector):++ * The free (unconstrained) decoder spelled the phrase in only 28 % of genuine+ calls. The rest came out as sound-alike garbage — "herum", "erhoben",+ "hey room", "hey oben", "hey ruhm", "heroes".+ * Rejecting on that garbage threw away 38 % of ALL real wakes; end-to-end+ recall sat at 32 % (the user had to repeat the wake word four or five+ times), while false accepts were 0/400 — the gate was far past the point of+ diminishing precision.+ * No spelling threshold can fix it: the free transcript "herr oben" was+ produced BOTH by a genuine call and by background chatter. Spelling is not+ a discriminator for an out-of-vocabulary proper noun, and every wake word is+ out-of-vocabulary for some installed language model.++The discriminator that DOES hold is the SHAPE of what the free ear heard at the+candidate position, which never asks how the wake word is written:++ * a wake call is short and stands alone (measured: 0.72 s, 2 words)+ * room speech is a longer stream of words (measured: 1.29 s, 5 words)++Both bounds derive from the configured phrase, so they hold for ANY phrase in+ANY supported language — the product requirement these tests exist to pin.+"""+from __future__ import annotations++import json++import numpy as np+import pytest++from jarvis.plugins.wake.vosk_kws_provider import (+ VoskKwsProvider,+ candidate_shape_ok,+ sound_confirm,+)+++def _w(word: str, start: float, end: float, conf: float = 0.5) -> dict:+ return {"word": word, "start": start, "end": end, "conf": conf}+++# --- the property that must hold for EVERY wake word -----------------------+++ ("phrase", "garbled"),+ (+ # Real free-decode output captured for genuine calls (2026-07-13).+ ("Hey Ruben", [_w("herum", 0.40, 1.02)]),+ ("Hey Ruben", [_w("hey", 0.40, 0.62), _w("room", 0.62, 1.05)]),+ ("Hey Ruben", [_w("erhoben", 0.35, 1.05)]),+ ("Hey Ruben", [_w("hey", 0.40, 0.60), _w("oben", 0.60, 1.02)]),+ # The same failure mode for other phrases/languages: an offline model+ # cannot spell an arbitrary proper noun.+ ("Hey Jarvis", [_w("age", 0.40, 0.62), _w("avis", 0.62, 1.05)]),+ ("Computer", [_w("kompott", 0.40, 1.00)]),+ ("Hola Nova", [_w("ola", 0.40, 0.70), _w("nofa", 0.70, 1.00)]),+ ),+)+def test_a_garbled_wake_is_still_a_wake(phrase: str, garbled: list[dict]) -> None:+ """The free ear mangled the wake word — the shape still says 'wake call'."""+ assert candidate_shape_ok(garbled, phrase) is True+++def test_a_split_name_is_covered_by_the_spelling_path() -> None:+ """A free decoder that SPLITS the name is handled by ``sound_confirm``.++ The shape gate deliberately allows no extra token (that cost real false+ accepts), so the two paths must between them still cover the split — this+ pins that division of labour.+ """+ assert sound_confirm("hey joe avis", "Hey Jarvis") is True+++def test_room_speech_is_rejected_by_its_shape() -> None:+ """A stream of confidently-recognised words is speech, not a wake call."""+ flowing = [+ _w("die", 0.20, 0.35, conf=1.0),+ _w("richtigen", 0.35, 0.75, conf=1.0),+ _w("harte", 0.75, 1.05, conf=1.0),+ _w("baums", 1.05, 1.40, conf=1.0),+ _w("gibt", 1.40, 1.70, conf=1.0),+ ]+ assert candidate_shape_ok(flowing, "Hey Ruben") is False+++def test_a_confidently_recognised_other_word_is_not_a_wake() -> None:+ """The free ear KNOWS this word — so it was not an unknown wake word."""+ known = [_w("google", 0.40, 1.00, conf=1.0)]+ assert candidate_shape_ok(known, "Hey Ruben") is False+++def test_an_overlong_utterance_is_not_a_wake_call() -> None:+ """Two words, but spoken over 1.8 s — the grammar stretched real speech."""+ stretched = [_w("herr", 0.20, 1.00), _w("oben", 1.00, 2.00)]+ assert candidate_shape_ok(stretched, "Hey Ruben") is False+++def test_silence_can_never_pass_the_shape_gate() -> None:+ assert candidate_shape_ok([], "Hey Ruben") is False+++def test_the_bound_scales_with_the_configured_phrase() -> None:+ """A three-token phrase legitimately takes longer than a one-token one."""+ three = [+ _w("gud", 0.2, 0.6),+ _w("morgen", 0.6, 1.1),+ _w("atlas", 1.1, 1.6),+ ]+ assert candidate_shape_ok(three, "Good Morning Atlas") is True+ # the very same audio shape is far too long for a single-word wake+ assert candidate_shape_ok(three, "Computer") is False+++# --- the two paths are complementary, and the bonus path may only ACCEPT ----+++def test_sound_confirm_stays_a_bonus_path_and_never_rejects() -> None:+ """A correctly-spelled free transcript still confirms instantly."""+ assert sound_confirm("hey ruben", "Hey Ruben") is True+++def test_the_shape_gate_catches_what_spelling_cannot() -> None:+ """The mishearings that spelling rules cannot rescue still fire.++ ``sound_confirm`` chases these with ever-looser similarity floors; each+ loosening buys one mishearing and risks the next false wake. The shape gate+ needs none of them, because it never looks at the spelling at all.+ """+ for heard in ("hey ho", "heroes", "harry", "hey euro"):+ assert sound_confirm(heard, "Hey Ruben") is False, heard+ assert candidate_shape_ok([_w(heard, 0.40, 1.02)], "Hey Ruben") is True, heard+++# --- end-to-end through the real verify -------------------------------------+++class _StubRec:+ """A KaldiRecognizer stub returning a scripted decode."""++ def __init__(self, result: dict) -> None:+ self._result = result++ def AcceptWaveform(self, pcm: bytes) -> bool: # noqa: N802 - vosk API+ return True++ def FinalResult(self) -> str: # noqa: N802 - vosk API+ return json.dumps(self._result)+++def _loud_window(seconds: float = 2.0) -> np.ndarray:+ rng = np.random.default_rng(7)+ return (rng.standard_normal(int(16_000 * seconds)) * 0.15).astype(np.float32)+++def test_verify_fires_on_a_wake_the_free_ear_could_not_spell(monkeypatch) -> None:+ """The live BUG: 'Hey Ruben' heard as 'herum' was thrown away."""+ p = VoskKwsProvider("Hey Ruben", model_path="fake", keyword="ruben")++ grammar = {+ "text": "hey ruben",+ "result": [+ {"word": "hey", "start": 0.40, "end": 0.62, "conf": 1.0},+ {"word": "ruben", "start": 0.62, "end": 1.05, "conf": 1.0},+ ],+ }+ free = {"text": "herum", "result": [_w("herum", 0.40, 1.02, conf=0.6)]}++ def _take(model_path, kind): # noqa: ANN001+ return _StubRec(grammar if kind == "grammar" else free)++ monkeypatch.setattr(p, "_take_verify_rec", _take)+ assert p._verify_window(_loud_window(), fail_open=True) is True+++def test_verify_still_rejects_room_speech_forced_onto_the_phrase(monkeypatch) -> None:+ """The precision contract the free-ear check was added for stays intact."""+ p = VoskKwsProvider("Hey Ruben", model_path="fake", keyword="ruben")++ grammar = {+ "text": "hey ruben",+ "result": [+ {"word": "hey", "start": 0.30, "end": 0.60, "conf": 1.0},+ {"word": "ruben", "start": 0.60, "end": 1.60, "conf": 1.0},+ ],+ }+ free = {+ "text": "die richtigen harte baums gibt",+ "result": [+ _w("die", 0.20, 0.35, conf=1.0),+ _w("richtigen", 0.35, 0.75, conf=1.0),+ _w("harte", 0.75, 1.05, conf=1.0),+ _w("baums", 1.05, 1.40, conf=1.0),+ _w("gibt", 1.40, 1.70, conf=1.0),+ ],+ }++ def _take(model_path, kind): # noqa: ANN001+ return _StubRec(grammar if kind == "grammar" else free)++ monkeypatch.setattr(p, "_take_verify_rec", _take)+ assert p._verify_window(_loud_window(), fail_open=True) is False