The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(tui): push-to-talk transcripts reach Ink on the shared gateway
Teknium1abd5ba50a55
diff --git a/tests/tui_gateway/test_canonical_voice_events.py b/tests/tui_gateway/test_canonical_voice_events.pynew file mode 100644index 00000000000..1177472449b--- /dev/null+++ b/tests/tui_gateway/test_canonical_voice_events.py@@ -0,0 +1,54 @@+"""Push-to-talk events for a shared-owner session reach the client that started the capture.++On the shared gateway the session lives in the authority, not in this process's ``_sessions``, so+``write_json`` cannot route a ``voice.transcript`` by session id. The STT callback runs on a worker+thread with no transport bound; before the fix the frame fell through to stdio and the TUI never+received its transcript.+"""++import sys+import threading+import types++from tui_gateway import server+from tui_gateway.transport import bind_transport, reset_transport+++class _Sink:+ def __init__(self):+ self.frames = []++ def write(self, obj):+ self.frames.append(obj)+ return True++ def close(self):+ pass+++def test_voice_transcript_for_an_unregistered_session_goes_to_the_capturing_client(monkeypatch):+ callbacks = {}+ monkeypatch.setitem(sys.modules, "hermes_cli.voice", types.SimpleNamespace(+ start_continuous=lambda **kw: callbacks.update(kw), stop_continuous=lambda **kw: None))+ monkeypatch.setenv("HERMES_VOICE", "1")+ monkeypatch.setattr(server, "_load_cfg", dict)+ ws, stdio = _Sink(), _Sink()+ monkeypatch.setattr(server, "_stdio_transport", stdio)+ monkeypatch.setattr(server, "_voice_event_sid", "")+ assert "owner-sid" not in server._sessions++ token = bind_transport(ws)+ try:+ resp = server.handle_request({"id": "r", "method": "voice.record",+ "params": {"action": "start", "session_id": "owner-sid"}})+ finally:+ reset_transport(token)+ assert resp["result"]["status"] == "recording"++ worker = threading.Thread(target=callbacks["on_transcript"], args=("hello there",))+ worker.start()+ worker.join()++ transcripts = [f["params"] for f in ws.frames if f.get("params", {}).get("type") == "voice.transcript"]+ assert [(t["session_id"], t["payload"]["text"]) for t in transcripts] == [("owner-sid", "hello there")]+ assert not [f for f in stdio.frames if f.get("params", {}).get("type") == "voice.transcript"]diff --git a/tui_gateway/methods_voice.py b/tui_gateway/methods_voice.pyindex 49803cdb5b1..b83e278e045 100644--- a/tui_gateway/methods_voice.py+++ b/tui_gateway/methods_voice.py@@ -19,6 +19,9 @@ method = _registry.method_voice_sid_lock = threading.Lock()_voice_event_sid: str = ""_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] = Nonedef _caller_transport():@@ -28,8 +31,13 @@ def _caller_transport():def _voice_emit(event: str, payload: dict | None = None) -> None:"""Emit toward the session that most recently turned voice on (empty sid = active session)."""with _voice_sid_lock:- sid = _voice_event_sid- _emit(event, sid, payload)+ sid, transport = _voice_event_sid, _voice_event_transport+ token = bind_transport(transport) if transport is not None else None+ try:+ _emit(event, sid, payload)+ finally:+ if token is not None:+ reset_transport(token)def _resume_voice_wake() -> None:@@ -724,11 +732,12 @@ 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_wake_owner+ global _voice_event_sid, _voice_event_transport, _voice_wake_ownerif action == "start" and not _voice_mode_enabled():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 = transportif action == "stop":from hermes_cli.voice import stop_continuousstop_continuous(force_transcribe=True)