Skip to content

The agent that grows with you

fix(tui): typed and full-duplex stop phrases end the session's own voice chat

Teknium3f38d5f3650b
diff --git a/tests/tui_gateway/test_canonical_voice_mode_writers.py b/tests/tui_gateway/test_canonical_voice_mode_writers.py
index 0d343578d26..fba5185350b 100644
--- a/tests/tui_gateway/test_canonical_voice_mode_writers.py
+++ b/tests/tui_gateway/test_canonical_voice_mode_writers.py
@@ -161,3 +161,38 @@ def test_another_session_may_start_once_the_capture_ended(owner):
owner.mic.active = False
assert owner.rpc("voice.record", action="start", session_id="other")["result"]["status"] == "recording"
assert server._voice_capture_sid == "other"
+
+
+def stop_phrase(monkeypatch):
+ import types
+
+ monkeypatch.setitem(sys.modules, "tools.voice_mode_transcript",
+ types.SimpleNamespace(is_voice_stop_phrase=lambda t: t.strip().lower() == "stop"))
+ monkeypatch.setattr(server, "_tts_stream_stop", lambda user_barge=False: None)
+
+
+def test_typed_stop_turns_the_owner_mode_off_not_the_sidecar_flag(owner, stop_phrase, monkeypatch):
+ owner.slash("owner", "on")
+ owner.rpc("voice.record", action="start", session_id="owner")
+ monkeypatch.setenv("HERMES_VOICE", "1") # a sidecar session's own voice chat
+ reply = owner.rpc("prompt.submit", session_id="owner", text="stop")
+ assert reply["result"] == {"voice_stopped": True}
+ assert owner.runner._voice_mode["owner"] == "off" and owner.mic.active is False
+ assert server._voice_mode_enabled() is True
+
+
+def test_typed_stop_reads_the_owner_mode(owner, stop_phrase):
+ owner.slash("owner", "on") # HERMES_VOICE stays "0": the owner mode is the gate
+ assert owner.rpc("prompt.submit", session_id="owner", text="stop")["result"] == {"voice_stopped": True}
+ assert owner.runner._voice_mode["owner"] == "off"
+
+
+def test_full_duplex_stop_turns_the_owner_mode_off_not_the_sidecar_flag(owner, stop_phrase, monkeypatch):
+ owner.slash("owner", "on")
+ owner.rpc("voice.record", action="start", session_id="owner")
+ monkeypatch.setenv("HERMES_VOICE", "1")
+ server._deliver_fd_transcript("stop")
+ assert owner.runner._voice_mode["owner"] == "off"
+ assert server._voice_mode_enabled() is True
+ assert _transcripts(owner.sink)[-1]["stop_phrase"] is True
diff --git a/tui_gateway/methods_prompt.py b/tui_gateway/methods_prompt.py
index 9b52995d44a..bc648f181f0 100644
--- a/tui_gateway/methods_prompt.py
+++ b/tui_gateway/methods_prompt.py
@@ -236,10 +236,10 @@ def _pending_reaction_notes(session: dict) -> str:
# ── prompt.submit pieces ────────────────────────────────────────────────────
-def _typed_stop_phrase_response(rid, text):
- """RPC reply ending the voice chat when a bare stop phrase is TYPED while backend voice
+def _typed_stop_phrase_response(rid, text, sid=None):
+ """RPC reply ending the voice chat when a bare stop phrase is TYPED while the session's voice
mode is on (typed twin of the spoken stop phrase), or None for a normal message."""
- if not (isinstance(text, str) and _voice_mode_enabled()):
+ if not (isinstance(text, str) and _session_voice_enabled(sid)):
return None
try:
# Typed bare stop phrase while backend voice mode is active ends the voice chat instead of sending
@@ -251,7 +251,7 @@ def _typed_stop_phrase_response(rid, text):
return None
except Exception:
return None
- _end_voice_chat(stop_loop=True, stop_tts=True)
+ _end_session_voice_chat(sid, stop_loop=True, stop_tts=True)
_voice_emit("voice.transcript", {"stop_phrase": True, "typed": True})
logger.info("prompt.submit: typed stop phrase — voice chat ended")
return _ok(rid, {"voice_stopped": True})
@@ -815,7 +815,7 @@ def _(rid, params: dict) -> dict:
if isinstance(title_preview, str) and title_preview.strip()
else None
)
- if (stopped := _typed_stop_phrase_response(rid, text)) is not None:
+ if (stopped := _typed_stop_phrase_response(rid, text, sid)) is not None:
return stopped
if params.get("interrupted"):
# Client-side barge-in: latch so this turn's model message carries the note.
diff --git a/tui_gateway/methods_voice.py b/tui_gateway/methods_voice.py
index 822fb479df4..716f66d560e 100644
--- a/tui_gateway/methods_voice.py
+++ b/tui_gateway/methods_voice.py
@@ -127,6 +127,16 @@ def _end_voice_chat(*, stop_loop: bool, stop_tts: bool) -> None:
_tts_stream_stop(user_barge=False)
+def _end_session_voice_chat(session_id, *, stop_loop: bool, stop_tts: bool) -> None:
+ """A stop phrase (spoken, typed or full-duplex) ends *session_id*'s voice chat: a shared-owner
+ session through its own mode (which ends only its capture); else the sidecar's env flags."""
+ if not _set_session_voice_off(session_id):
+ _end_voice_chat(stop_loop=stop_loop, stop_tts=stop_tts)
+ elif stop_tts:
+ with contextlib.suppress(Exception):
+ _tts_stream_stop(user_barge=False)
+
+
def _tts_lease_async(lease: str, active: bool) -> None:
"""Acquire/release a TTS lease off the RPC thread (acquiring warms a local engine; must not
block the toggle's reply). Best-effort."""
@@ -322,7 +332,9 @@ def _deliver_fd_transcript(text: str) -> None:
except Exception:
is_stop = False
if is_stop: # turn already interrupted / TTS cut at trip time; now end the chat
- _end_voice_chat(stop_loop=True, stop_tts=False)
+ with _voice_sid_lock:
+ sid = _voice_event_sid
+ _end_session_voice_chat(sid, stop_loop=True, stop_tts=False)
_voice_emit("voice.transcript", {"stop_phrase": True, "text": text} if is_stop else {"text": text})
@@ -768,8 +780,7 @@ def _vr_transcript(payload: dict) -> None:
def _vr_on_stop_phrase(t):
# A SPOKEN bare stop phrase: end the chat like /voice off and emit a distinct signal so
# clients end the conversation instead of treating it as a no-speech timeout.
- _end_voice_chat(stop_loop=False, stop_tts=True)
- _set_session_voice_off(_voice_capture_sid)
+ _end_session_voice_chat(_voice_capture_sid, stop_loop=False, stop_tts=True)
_vr_transcript({"stop_phrase": True, "text": t})