voice: hold the wake word until the scheduler reaches a result

Gate the wake word on the scheduler's single slot being free: a wake that
fires while the current request is still running is ignored (traced
voice_wake_blocked), so a second command cannot start mid-run. The gate is
GatewayService.input_ready (False while a run executes, True when idle or
paused for the user's answer), handed to adapters via set_input_ready; the
voice adapter passes it to VoiceDaemon.input_ready.
This commit is contained in:
gabby committed 2026-10-09 23:52:57 +00:00
1 parent ff94ea938c
commit 88700898be
8 files changed
+130 -7

No files matched your search

+23
View File
@@ -128,6 +128,13 @@ class GatewayService:
humanize_cfg = humanize_cfg if isinstance(humanize_cfg, dict) else {}
self._humanize_default = bool(humanize_cfg.get("enabled", False))
self.humanizer = self._build_humanizer(humanize_cfg)
#: Let an adapter that needs it gate input on the scheduler being free
#: (the voice gateway holds the wake word until the current request
#: reaches a result). Generic hook; only some adapters implement it.
for adapter in self.adapters.values():
setter = getattr(adapter, "set_input_ready", None)
if callable(setter):
setter(self.input_ready)
self._owners: dict[str, Target] = {}
self._parents: dict[str, str] = {}
self._pending_owner: Target | None = None
@@ -275,6 +282,22 @@ class GatewayService:
return (platform, source[len(prefix):])
return None
def input_ready(self) -> bool:
"""Whether a new command may start now.
False while the single scheduler slot is executing a run; True when the
scheduler is idle or paused for the user's answer to a question (the run
is not running, so the answer must be accepted). The voice gateway uses
this to hold the wake word until the current request has reached a
result. Reads the scheduler's flags without its lock on purpose — the
lock is held for the whole run, so acquiring it here would block the
audio loop for the duration of the run.
"""
scheduler = self.scheduler
if getattr(scheduler, "pending", None) is not None:
return True
return getattr(scheduler, "current", None) is None
def handle_inbound(self, msg: InboundMessage, platform: str | None = None) -> None:
"""Handle one authorized inbound command. Called by the adapter."""
if platform is None:
+10
View File
@@ -53,6 +53,16 @@ class VoiceAdapter(GatewayAdapter):
def check_requirements(self) -> tuple[bool, str | None]:
return self.daemon.check_requirements()
def set_input_ready(self, ready) -> None:
"""Gate the wake word on the scheduler being able to accept a command.
`ready` is a callable returning whether a new command may start now
(`GatewayService.input_ready`). While it returns False the wake word is
ignored, so a second command cannot be started before the current
request has reached a result.
"""
self.daemon.input_ready = ready
# ---- transport ----
def run(
+18 -1
View File
@@ -633,11 +633,16 @@ class VoiceDaemon:
"""
def __init__(self, cfg: dict | None = None, trace=None, engines: VoiceEngines | None = None,
name: str = "voice"):
name: str = "voice", input_ready=None):
cfg = cfg or {}
self.config = cfg
self.trace = trace
self.name = name
#: Optional predicate: whether a new command may start now. While it
#: returns False the wake word is ignored (the scheduler's single slot
#: is still running the current request), so a second command cannot be
#: started before the first reaches a result. None = always ready.
self.input_ready = input_ready
self.wake_threshold = float((cfg.get("wake", {}) or {}).get("threshold", 0.2))
#: After the wake word, how long to wait for the command's speech to
#: begin before giving up (a natural pause after "hey <word>" — e.g.
@@ -766,6 +771,18 @@ class VoiceDaemon:
score = engines.wake.score(pcm)
if score < self.wake_threshold:
continue
if self.input_ready is not None and not self.input_ready():
# The single scheduler slot is still running the current
# request: hold the wake word until it reaches a result (or
# pauses for the user's answer), so a new command cannot start
# while one is in flight. Reset the detector so the same wake
# phrase does not immediately re-fire once the slot frees.
try:
engines.wake.reset()
except Exception:
pass
self._event("voice_wake_blocked", score=round(float(score), 3))
continue
self._event("voice_wake", score=round(float(score), 3))
try:
engines.wake.reset()