Skip to content

The agent that grows with you

fix(tui): a second session cannot take over a live voice capture

Tekniume3bf070e6005
diff --git a/tests/tui_gateway/test_canonical_voice_mode_writers.py b/tests/tui_gateway/test_canonical_voice_mode_writers.py
index 13b0056a254..0d343578d26 100644
--- a/tests/tui_gateway/test_canonical_voice_mode_writers.py
+++ b/tests/tui_gateway/test_canonical_voice_mode_writers.py
@@ -24,9 +24,13 @@ class _Mic:
self.active, self.stops, self.callbacks = False, 0, {}
def start_continuous(self, **kw):
- self.active, self.callbacks = True, kw
+ if not self.active: # the real wrapper keeps the live capture's callbacks: a no-op True
+ self.active, self.callbacks = True, kw
return True
+ def is_continuous_active(self):
+ return self.active
+
def stop_continuous(self, **kw):
self.active = False
self.stops += 1
@@ -131,3 +135,29 @@ def test_no_speech_limit_turns_the_owner_mode_off(owner):
assert owner.runner._voice_mode["owner"] == "off"
assert _transcripts(owner.sink)[-1]["no_speech_limit"] is True
+
+
+def test_a_second_sessions_start_is_refused_while_another_captures(owner):
+ owner.slash("owner", "on")
+ owner.slash("other", "on")
+ owner.rpc("voice.record", action="start", session_id="owner")
+ refused = owner.rpc("voice.record", action="start", session_id="other")
+ assert refused["error"]["code"] == 4016 and "another session" in refused["error"]["message"]
+ owner.slash("other", "off")
+ assert owner.mic.active is True
+ owner.mic.callbacks["on_transcript"]("A_TEXT")
+ frames = [f["params"] for f in owner.sink.frames if f.get("params", {}).get("type") == "voice.transcript"]
+ assert [f["session_id"] for f in frames] == ["owner"]
+ owner.slash("owner", "off")
+ assert owner.mic.active is False
+ owner.mic.callbacks["on_transcript"]("A_AFTER_OFF")
+ assert len(_transcripts(owner.sink)) == 1
+
+
+def test_another_session_may_start_once_the_capture_ended(owner):
+ owner.slash("owner", "on")
+ owner.slash("other", "on")
+ owner.rpc("voice.record", action="start", session_id="owner")
+ owner.mic.active = False
+ assert owner.rpc("voice.record", action="start", session_id="other")["result"]["status"] == "recording"
+ assert server._voice_capture_sid == "other"
diff --git a/tui_gateway/methods_voice.py b/tui_gateway/methods_voice.py
index 4e15d7d8f8d..822fb479df4 100644
--- a/tui_gateway/methods_voice.py
+++ b/tui_gateway/methods_voice.py
@@ -90,6 +90,14 @@ def _stop_session_voice_capture(session_id) -> None:
_resume_voice_wake()
+def _capture_live() -> bool:
+ try:
+ from hermes_cli.voice import is_continuous_active
+ return bool(is_continuous_active())
+ except Exception:
+ return False
+
+
def _voice_capture_callback(fn):
"""*fn* while the capture it was registered for is still current; a no-op once invalidated."""
with _voice_sid_lock:
@@ -792,11 +800,12 @@ def _(rid, params: dict) -> dict:
global _voice_event_sid, _voice_event_transport, _voice_wake_owner, _voice_capture_sid
if action == "start" and not _session_voice_enabled(params.get("session_id")):
return _err(rid, 4015, "voice mode is off — enable with /voice on")
+ sid = params.get("session_id") or ""
+ if _voice_capture_sid and sid != _voice_capture_sid and _capture_live():
+ return _err(rid, 4016, "voice capture is busy in another session")
with _voice_sid_lock:
- _voice_event_sid = params.get("session_id") or _voice_event_sid
+ _voice_event_sid = sid or _voice_event_sid
_voice_event_transport = transport
- if action == "start":
- _voice_capture_sid = params.get("session_id") or ""
if action == "stop":
from hermes_cli.voice import stop_continuous
stop_continuous(force_transcribe=True)
@@ -839,6 +848,9 @@ def _(rid, params: dict) -> dict:
on_partial=live(lambda t: _voice_emit("voice.partial", {"text": t})))
if started is False:
_resume_voice_wake()
+ else:
+ with _voice_sid_lock:
+ _voice_capture_sid = sid
return _ok(rid, {"status": "busy" if started is False else "recording"})
except Exception as e:
if wake_paused or action == "stop":