The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(tui): one voice-mode state for a shared-owner session
Teknium72e6776db919
diff --git a/gateway/session_config.py b/gateway/session_config.pyindex 24b98f1f0e2..84d36706596 100644--- a/gateway/session_config.py+++ b/gateway/session_config.py@@ -123,6 +123,35 @@ def local_voice_enabled(session_id):return None+def set_local_voice_mode(session_id, mode):+ """The one writer for a local session's voice mode outside ``/voice`` (wake, spoken stop): sets+ and persists the owner's per-session mode. True when the running owner holds the session, else+ None (the sidecar's env flag applies)."""+ from gateway.run_api import _ACTIVE_RUNNER+ from gateway.session_authorities import all_authorities+ if _ACTIVE_RUNNER is None or not isinstance(session_id, str) or not session_id:+ return None+ for authority in all_authorities(_ACTIVE_RUNNER):+ live = getattr(authority, 'sessions', {}).get(session_id)+ if live is not None:+ _ACTIVE_RUNNER._voice_mode[_ACTIVE_RUNNER._voice_key_for_source(live.source)] = mode+ _ACTIVE_RUNNER._save_voice_modes()+ local_voice_transition(live.source, mode)+ return True+ return None+++def local_voice_transition(source, mode):+ """A local session's voice mode went ``off``: end the push-to-talk capture it started in this+ process (and drop its late callbacks), as the sidecar's ``voice.toggle off`` did. Another+ session's capture is left alone."""+ import sys+ from gateway.config import Platform+ server = sys.modules.get('tui_gateway.server')+ if mode == 'off' and server is not None and getattr(source, 'platform', None) == Platform.LOCAL:+ server._stop_session_voice_capture(source.chat_id)++def _voice(connection, ref):"""The session's own ``/voice`` mode, so Ink mirrors the gateway's per-session state instead ofthe sidecar's process-global ``HERMES_VOICE`` (T9)."""diff --git a/gateway/slash_commands.py b/gateway/slash_commands.pyindex 6e2a2a62099..61ac9e38cb1 100644--- a/gateway/slash_commands.py+++ b/gateway/slash_commands.py@@ -689,6 +689,8 @@ class GatewaySlashCommandsMixin(def _set_mode(mode: str) -> None:self._voice_mode[voice_key] = modeself._save_voice_modes()+ from gateway.session_config import local_voice_transition+ local_voice_transition(event.source, mode)if not adapter:returnif mode == "off":diff --git a/tests/tui_gateway/test_canonical_voice_mode_writers.py b/tests/tui_gateway/test_canonical_voice_mode_writers.pynew file mode 100644index 00000000000..13b0056a254--- /dev/null+++ b/tests/tui_gateway/test_canonical_voice_mode_writers.py@@ -0,0 +1,133 @@+"""One voice-mode state for a shared-owner session (T9 review round 1).++Ink's ``/voice on|off`` runs the owner's per-session ``/voice`` handler and ``voice.record`` gates on+that mode. Every other writer must use the same state: ``/voice off`` stops the session's live+capture (and its late callbacks), a spoken stop phrase turns the owner mode off, and the wake path+can turn it on. Real handlers; only the microphone is fake.+"""++import asyncio+import sys+from types import SimpleNamespace++import pytest++from gateway import run_api+from gateway.config import Platform+from gateway.slash_commands import GatewaySlashCommandsMixin+from tui_gateway import server+from tui_gateway.transport import bind_transport, reset_transport+++class _Mic:+ def __init__(self):+ self.active, self.stops, self.callbacks = False, 0, {}++ def start_continuous(self, **kw):+ self.active, self.callbacks = True, kw+ return True++ def stop_continuous(self, **kw):+ self.active = False+ self.stops += 1++ def set_voice_busy_probe(self, *a):+ pass+++class _Sink:+ def __init__(self):+ self.frames = []++ def write(self, obj):+ self.frames.append(obj)+ return True++ def close(self):+ pass+++class _Runner:+ def __init__(self, sessions):+ self._voice_mode = {}+ self.session_authority = SimpleNamespace(sessions=sessions)++ def _voice_key_for_source(self, source):+ return source.chat_id++ def _delivery_adapter_for(self, source):+ return None++ def _save_voice_modes(self):+ pass++ def _get_guild_id(self, event):+ return None+++def owner(monkeypatch):+ mic, sink = _Mic(), _Sink()+ monkeypatch.setitem(sys.modules, "hermes_cli.voice", mic)+ monkeypatch.setattr(server, "_load_cfg", dict)+ monkeypatch.setattr(server, "_tts_lease_async", lambda *a: None)+ monkeypatch.setattr(server, "_stdio_transport", sink)+ monkeypatch.setattr(server, "_voice_event_sid", "")+ monkeypatch.setenv("HERMES_VOICE", "0")+ sources = {sid: SimpleNamespace(chat_id=sid, platform=Platform.LOCAL) for sid in ("owner", "other")}+ runner = _Runner({sid: SimpleNamespace(source=src) for sid, src in sources.items()})+ monkeypatch.setattr(run_api, "_ACTIVE_RUNNER", runner)++ def rpc(method, **params):+ token = bind_transport(sink)+ try:+ return server.handle_request({"id": method, "method": method, "params": params})+ finally:+ reset_transport(token)++ def slash(sid, arg):+ event = SimpleNamespace(source=sources[sid], get_command_args=lambda: arg)+ return asyncio.run(GatewaySlashCommandsMixin._handle_voice_command(runner, event))++ return SimpleNamespace(mic=mic, sink=sink, runner=runner, rpc=rpc, slash=slash)+++def _transcripts(sink):+ return [f["params"]["payload"] for f in sink.frames if f.get("params", {}).get("type") == "voice.transcript"]+++def test_voice_off_stops_the_sessions_capture_and_drops_late_text(owner):+ owner.slash("owner", "on")+ assert owner.rpc("voice.record", action="start", session_id="owner")["result"]["status"] == "recording"+ owner.slash("owner", "off")+ assert owner.runner._voice_mode["owner"] == "off"+ assert owner.mic.active is False and owner.mic.stops == 1+ owner.mic.callbacks["on_transcript"]("AFTER_VOICE_OFF")+ assert _transcripts(owner.sink) == []+++def test_another_sessions_voice_off_leaves_this_capture_running(owner):+ owner.slash("owner", "on")+ owner.rpc("voice.record", action="start", session_id="owner")+ owner.slash("other", "off")+ assert owner.mic.active is True and owner.mic.stops == 0+ owner.mic.callbacks["on_transcript"]("still mine")+ assert [p["text"] for p in _transcripts(owner.sink)] == ["still mine"]+++def test_spoken_stop_turns_the_owner_mode_off(owner):+ owner.slash("owner", "on")+ owner.rpc("voice.record", action="start", session_id="owner")+ owner.mic.callbacks["on_stop_phrase"]("stop")+ assert owner.runner._voice_mode["owner"] == "off"+ assert server._session_voice_enabled("owner") is False+ assert _transcripts(owner.sink)[-1]["stop_phrase"] is True+++def test_no_speech_limit_turns_the_owner_mode_off(owner):+ owner.slash("owner", "on")+ owner.rpc("voice.record", action="start", session_id="owner")+ owner.mic.callbacks["on_silent_limit"]()+ assert owner.runner._voice_mode["owner"] == "off"+ assert _transcripts(owner.sink)[-1]["no_speech_limit"] is True+diff --git a/tui_gateway/methods_voice.py b/tui_gateway/methods_voice.pyindex 978beb6f094..4e15d7d8f8d 100644--- a/tui_gateway/methods_voice.py+++ b/tui_gateway/methods_voice.py@@ -22,6 +22,10 @@ _voice_wake_owner: Optional[Transport] = None# The transport that started the capture. A shared-owner (canonical) session is not in this process's# ``_sessions``, so ``write_json`` cannot route its voice events by sid; they go to this caller instead._voice_event_transport: Optional[Transport] = None+# The session whose push-to-talk capture is live, and a generation that invalidates its callbacks+# when that session's voice mode goes off (a late transcript must not land after /voice off).+_voice_capture_sid: str = ""+_voice_capture_gen = 0def _caller_transport():@@ -63,6 +67,42 @@ def _session_voice_enabled(session_id) -> bool:return _voice_mode_enabled() if held is None else held+def _set_session_voice_off(session_id) -> bool:+ """Turn a shared-owner session's own voice mode off (spoken stop, no-speech limit); False when+ no owner in this process holds it (the sidecar's env flag is the state)."""+ try:+ from gateway.session_config import set_local_voice_mode+ return set_local_voice_mode(session_id, "off") is True+ except ImportError:+ return False+++def _stop_session_voice_capture(session_id) -> None:+ """End *session_id*'s live capture (not another session's) and drop its late text callbacks."""+ global _voice_capture_gen+ with _voice_sid_lock:+ if not session_id or session_id != _voice_capture_sid:+ return+ _voice_capture_gen += 1+ with contextlib.suppress(Exception):+ from hermes_cli.voice import stop_continuous+ stop_continuous()+ _resume_voice_wake()+++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:+ gen = _voice_capture_gen++ def call(*args):+ with _voice_sid_lock:+ current = gen == _voice_capture_gen+ if current:+ fn(*args)+ return call++def _voice_tts_enabled() -> bool:return os.environ.get("HERMES_VOICE_TTS", "").strip() == "1"@@ -721,9 +761,15 @@ 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)_vr_transcript({"stop_phrase": True, "text": t})+def _vr_on_silent_limit():+ _set_session_voice_off(_voice_capture_sid)+ _vr_transcript({"no_speech_limit": True})++def _vr_on_status(state):_voice_emit("voice.status", {"state": state})if state == "idle":@@ -743,12 +789,14 @@ def _(rid, params: dict) -> dict:if wake_owner is not None and wake_owner is not transport:return _ok(rid, {"status": "busy", "reason": "wake_owned"})try:- global _voice_event_sid, _voice_event_transport, _voice_wake_owner+ global _voice_event_sid, _voice_event_transport, _voice_wake_owner, _voice_capture_sidif action == "start" and not _session_voice_enabled(params.get("session_id")):return _err(rid, 4015, "voice mode is off — enable with /voice on")with _voice_sid_lock:_voice_event_sid = params.get("session_id") 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_continuousstop_continuous(force_transcribe=True)@@ -780,13 +828,15 @@ def _(rid, params: dict) -> dict:if wake_paused:with _voice_sid_lock:_voice_wake_owner = transport+ live = _voice_capture_callbackstarted = start_continuous(- on_transcript=lambda t: _vr_transcript({"text": t}), on_status=_vr_on_status,- on_silent_limit=lambda: _vr_transcript({"no_speech_limit": True}),+ on_transcript=live(lambda t: _vr_transcript({"text": t})), on_status=_vr_on_status,+ on_silent_limit=live(_vr_on_silent_limit),silence_threshold=_voice_cfg_number(voice_cfg.get("silence_threshold"), 200),silence_duration=_voice_cfg_number(voice_cfg.get("silence_duration"), 3.0),auto_restart=False, max_recording_seconds=max_rec if max_rec > 0 else 0.0,- on_stop_phrase=_vr_on_stop_phrase, on_partial=lambda t: _voice_emit("voice.partial", {"text": t}))+ on_stop_phrase=live(_vr_on_stop_phrase),+ on_partial=live(lambda t: _voice_emit("voice.partial", {"text": t})))if started is False:_resume_voice_wake()return _ok(rid, {"status": "busy" if started is False else "recording"})diff --git a/ui-tui/src/__tests__/createGatewayEventHandler.test.ts b/ui-tui/src/__tests__/createGatewayEventHandler.test.tsindex 0f48af0132a..e03cfc6be77 100644--- a/ui-tui/src/__tests__/createGatewayEventHandler.test.ts+++ b/ui-tui/src/__tests__/createGatewayEventHandler.test.ts@@ -1215,6 +1215,20 @@ describe('createGatewayEventHandler', () => {expect(ctx.voice.setVoiceEnabled).toHaveBeenCalledWith(true)})+ it('turns on the shared owner\'s per-session voice mode, not the sidecar flag, before a wake capture', async () => {+ const ctx = buildCtx([])+ ;(ctx.gateway.gw as any).isCanonical = true+ patchUiState({ sid: 'owner-session' })++ createGatewayEventHandler(ctx)({ payload: { start_new_session: false }, type: 'wake.detected' } as any)++ await vi.waitFor(() =>+ expect(ctx.gateway.rpc).toHaveBeenCalledWith('voice.record', { action: 'start', session_id: 'owner-session' })+ )+ expect(ctx.gateway.gw.request).toHaveBeenCalledWith('slash.exec', { command: 'voice on', session_id: 'owner-session' })+ expect(ctx.gateway.rpc).not.toHaveBeenCalledWith('voice.toggle', expect.anything())+ })+it('keeps the current session when wake detection disables session creation', async () => {const ctx = buildCtx([])patchUiState({ sid: 'current-session' })diff --git a/ui-tui/src/app/createGatewayEventHandler.ts b/ui-tui/src/app/createGatewayEventHandler.tsindex 1353fae0fd9..fbdbf9f5f09 100644--- a/ui-tui/src/app/createGatewayEventHandler.ts+++ b/ui-tui/src/app/createGatewayEventHandler.ts@@ -1338,7 +1338,11 @@ export function createGatewayEventHandler(ctx: GatewayEventHandlerContext): (ev:}setVoiceEnabled(true)- await rpc('voice.toggle', { action: 'on' })+ // A shared-owner session's voice mode is the owner's per-session /voice state (T9), the+ // one voice.record gates on; the sidecar's voice.toggle flips only its global flag.+ await (gw.isCanonical+ ? gw.request('slash.exec', { command: 'voice on', session_id: sid })+ : rpc('voice.toggle', { action: 'on' }))await rpc('voice.record', { action: 'start', session_id: sid })})().catch((e: unknown) => {sys(t('gatewayMsg.wake.failed', rpcErrorMessage(e)))