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(voice): wake pipeline immediately from taskbar thread

Ruben Lütke9e443bc0baa9
diff --git a/jarvis/speech/pipeline.py b/jarvis/speech/pipeline.py
index fdba2361ae..9b4c05472a 100644
--- a/jarvis/speech/pipeline.py
+++ b/jarvis/speech/pipeline.py
@@ -4059,7 +4059,7 @@ class SpeechPipeline:
log.info("🎙 PTT DOWN — recording (hold to talk, release to send)")
self._ptt_mode = True
self._ptt_release_event.clear()
- self._call_event.set()
+ self._arm_call_event()
def _on_ptt_release(self) -> None:
"""Push-to-talk UP edge — stop recording and submit what was held.
@@ -4081,10 +4081,11 @@ class SpeechPipeline:
{cid}/speak``) calls this to start a session that already remembers a
past conversation — functionally "Hey Jarvis", but with seeded context.
- Same-loop, in-process: uvicorn and the pipeline share the orchestrator
- event loop (see ``server.py`` / ``desktop_app.py``), so this runs on
- the pipeline's own loop and may set ``_call_event`` directly — exactly
- as the wake/PTT arming paths do.
+ Web routes run on the pipeline loop, while the native Jarvis Bar calls
+ this from its dedicated Tk thread. The final arming edge is therefore
+ marshalled through the owner loop when necessary; setting an
+ ``asyncio.Event`` directly from Tk can leave the selector asleep until
+ unrelated I/O happens to wake it.
Returns ``False`` (no-op) when a session is already active or arming,
or when the desktop app is not visible (``_activation_allowed``). The
@@ -4112,9 +4113,32 @@ class SpeechPipeline:
self._ptt_mode = False # wake-style, not raw PTT recording
self._last_wake_keyword = "chat_resume"
log.info(
- "📞 request_voice_session — arming wake-style session (seeded=%d turns)",
+ "Voice session requested (wake-style, seeded=%d turns).",
len(seed_messages or []),
)
+ return self._arm_call_event()
+
+ def _arm_call_event(self) -> bool:
+ """Set the call edge on its owning asyncio loop.
+
+ Native overlay callbacks run on a Tk thread. ``asyncio.Event.set`` may
+ flip the flag from that thread without waking the loop's selector, so
+ the state task resumes only after an unrelated timer or socket event.
+ ``call_soon_threadsafe`` provides both the wakeup and the memory edge.
+ Same-loop callers retain the direct, allocation-free path.
+ """
+ owner = getattr(self, "_runtime_loop", None)
+ try:
+ current = asyncio.get_running_loop()
+ except RuntimeError:
+ current = None
+ if owner is not None and owner.is_running() and current is not owner:
+ try:
+ owner.call_soon_threadsafe(self._call_event.set)
+ return True
+ except RuntimeError:
+ log.warning("Voice session request failed: owner loop is closed.")
+ return False
self._call_event.set()
return True
diff --git a/tests/unit/speech/test_request_voice_session.py b/tests/unit/speech/test_request_voice_session.py
index b47d5844a2..e93fd36670 100644
--- a/tests/unit/speech/test_request_voice_session.py
+++ b/tests/unit/speech/test_request_voice_session.py
@@ -8,6 +8,8 @@ pattern) so we don't drag in stt/tts/audio.
from __future__ import annotations
import asyncio
+import threading
+import time
from jarvis.speech.pipeline import PipelineState, SpeechPipeline, TurnTakingState
@@ -32,6 +34,7 @@ def _pipe(*, state=PipelineState.IDLE, gate=True, ptt=False, brain=None):
p._muted = False
p._last_wake_keyword = ""
p._brain = brain
+ p._runtime_loop = None
return p
@@ -98,3 +101,41 @@ def test_arms_without_seed_messages() -> None:
assert p.request_voice_session() is True
assert p._call_event.is_set()
assert brain.seeded is None # seed_history never called
+
+
+async def test_taskbar_thread_wakes_owner_loop_immediately() -> None:
+ """A Tk-thread request must wake the pipeline loop without another timer.
+
+ ``asyncio.Event.set()`` is not thread-safe. Calling it directly from the
+ Jarvis Bar's Tk thread only flips the flag; the selector can remain asleep
+ until unrelated I/O or a timer happens to wake it, which created the
+ variable 100-300 ms click-to-listen delay. The public request method must
+ marshal the edge through the pipeline loop's thread-safe scheduler.
+ """
+ p = _pipe()
+ owner = asyncio.get_running_loop()
+ p._runtime_loop = owner
+ ready = threading.Event()
+ release = threading.Event()
+
+ def _click_from_taskbar() -> None:
+ ready.set()
+ release.wait(timeout=0.5)
+ assert p.request_voice_session() is True
+
+ thread = threading.Thread(target=_click_from_taskbar, daemon=True)
+ thread.start()
+ assert ready.wait(timeout=0.1)
+ waiter = asyncio.create_task(p._call_event.wait())
+ await asyncio.sleep(0) # register the waiter before the Tk-thread edge
+
+ began = time.perf_counter()
+ release.set()
+ await asyncio.wait_for(waiter, timeout=0.5)
+ elapsed = time.perf_counter() - began
+ thread.join(timeout=0.1)
+
+ assert elapsed < 0.2, (
+ "taskbar request did not wake the owner loop promptly; it likely set "
+ "asyncio.Event directly from the Tk thread"
+ )