Skip to content

The agent that grows with you

fix(gateway): a retried typed voice stop replays voice_stopped, never a turn

Teknium4ffb3580b016
diff --git a/gateway/session_controls.py b/gateway/session_controls.py
index 8d8d63aeed5..fbcf87b8d77 100644
--- a/gateway/session_controls.py
+++ b/gateway/session_controls.py
@@ -690,13 +690,43 @@ class AuthorityConnection:
return self.authority.sessions[ref.session_id].event_stream.since(epoch, sequence)
async def submit(self, ref, params):
- if await self._typed_voice_stop(ref, params):
+ if self._voice_stop_replay(ref, params) or await self._typed_voice_stop(ref, params):
+ self._record_voice_stop(ref, params)
return {'voice_stopped': True}
# The interactive WS is the one surface a restart's after-turn wait admits (#139041).
receipt = await self.authority.submit(self.actor, self.submission(ref, params),
queue_while_restarting=self._operator is True)
return asdict(receipt)
+ def _voice_stop_key(self, ref, params):
+ request_id = params.get('submission_id') or params.get('input_id')
+ if not isinstance(request_id, str) or not request_id:
+ return None
+ return (self.actor.subject, ref.profile_id, ref.session_id, request_id)
+
+ def _voice_stop_replay(self, ref, params):
+ """A retry of a stop this owner already consumed (its reply was lost; Ink offers Alt+K on the
+ same submission id). Voice is off by then, so the phrase check alone would admit ``stop`` as a
+ turn: the request identity answers it again, as an admission's replay would."""
+ key = self._voice_stop_key(ref, params)
+ if key is None or key not in getattr(self.authority, '_consumed_voice_stops', {}):
+ return False
+ if ref.session_id not in self.subscriptions:
+ return False # submission() refuses it as permission_denied
+ self.authority.authorize(self.actor, ref, 'session:submit')
+ return True
+
+ def _record_voice_stop(self, ref, params):
+ from collections import OrderedDict
+ key = self._voice_stop_key(ref, params)
+ if key is None:
+ return
+ consumed = self.authority.__dict__.setdefault('_consumed_voice_stops', OrderedDict())
+ consumed[key] = True
+ consumed.move_to_end(key)
+ while len(consumed) > 256:
+ consumed.popitem(last=False)
+
async def _typed_voice_stop(self, ref, params):
"""TUI-26: a bare stop phrase typed while the session's voice chat is on ends the chat
(the sidecar's ``methods_prompt._consume_typed_stop_phrase``, its typed twin of the spoken
diff --git a/tests/gateway/test_canonical_typed_voice_stop.py b/tests/gateway/test_canonical_typed_voice_stop.py
index 32afa6a3ef8..aa02f7c4f64 100644
--- a/tests/gateway/test_canonical_typed_voice_stop.py
+++ b/tests/gateway/test_canonical_typed_voice_stop.py
@@ -49,6 +49,22 @@ async def test_typed_stop_phrase_ends_the_voice_chat_and_admits_nothing(voice_ow
assert _ledger(voice_owner.db) == [], 'the stop phrase must never reach the agent'
+async def test_a_retried_stop_replays_voice_stopped_and_never_starts_a_turn(voice_owner):
+ """Its reply lost, Ink retries the SAME submission (Alt+K). Voice is already off by then, so
+ the stop-phrase check alone would admit ``stop`` as a turn: the request id answers it, also on
+ a fresh connection (a reconnect)."""
+ voice_owner.runner._voice_mode['s'] = 'voice_only'
+ with voice_owner.db:
+ first = await _call(_connection(voice_owner.authority), 'prompt.submit', text='stop', submission_id='r1')
+ retry = await _call(_connection(voice_owner.authority), 'prompt.submit', text='stop', submission_id='r1')
+ fresh = await _call(_connection(voice_owner.authority), 'prompt.submit', text='stop', submission_id='r2')
+ assert first.get('result') == {'voice_stopped': True}, first
+ assert retry.get('result') == {'voice_stopped': True}, retry
+ assert fresh['result']['status'] == 'queued', 'a new stop with voice off is an ordinary turn'
+ assert _ledger(voice_owner.db) == ['r2']
+
+
@pytest.mark.asyncio
@pytest.mark.parametrize('mode, text', [('off', 'stop'), ('voice_only', 'stop doing that')])
async def test_other_text_or_voice_off_is_admitted_as_a_turn(voice_owner, mode, text):