Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.
Python agent-orchestrationai-agentai-coding-assistantclaude-codecoding-agentcomputer-usedesktop-applocal-firstmcppersonal-jarvisvoice-assistantvoice-coding
feat(realtime): speaking during a running action now interrupts it
rubenluetke10-beepfc74d67109ee
diff --git a/jarvis/realtime/session.py b/jarvis/realtime/session.pyindex aed8e44940..610c3216bf 100644--- a/jarvis/realtime/session.py+++ b/jarvis/realtime/session.py@@ -70,6 +70,11 @@ from jarvis.sessions.constants import ()from jarvis.speech.echo_guard import SelfEchoGuardfrom jarvis.speech.hangup import END_CALL_SIGNAL, HANGUP_RE+from jarvis.speech.interrupt_intent import (+ INTERRUPT_NONE,+ INTERRUPT_STOP,+ classify_interrupt,+)log = logging.getLogger(__name__)@@ -1028,6 +1033,36 @@ def _pick_delegate_bridge_text(language: str) -> str:return random.choice(_delegate_bridge_texts(language)) # noqa: S311+#: Spoken when the user interrupts a running action and it is actually+#: abandoned. One short, honest sentence: the user needs to know the work+#: stopped, because a silent cancellation is indistinguishable from a session+#: that simply ignored them — which is the failure this whole path exists to+#: end. Same locale coverage as every other runtime pool (CLAUDE.md §1).+_INTERRUPT_ACK_TEXTS: dict[str, tuple[str, ...]] = {+ "de": ( # i18n-allow: localized runtime voice output+ "Okay, ich habe das gestoppt.", # i18n-allow+ "Alles klar, ich breche das ab.", # i18n-allow+ "Okay, ich lasse das.", # i18n-allow+ ),+ "en": (+ "Okay, I stopped that.",+ "Alright, cancelling that.",+ "Okay, dropping that.",+ ),+ "es": ( # i18n-allow: localized runtime voice output+ "Vale, lo he detenido.",+ "De acuerdo, lo cancelo.",+ "Vale, lo dejo.",+ ),+}+++def _pick_interrupt_ack_text(language: str) -> str:+ pool = _INTERRUPT_ACK_TEXTS.get(language, _INTERRUPT_ACK_TEXTS["en"])+ # noqa comment: variety, not security — any pool member is equally safe.+ return random.choice(pool) # noqa: S311++def _normalized_bridge_text(text: str) -> str:return " ".join(str(text or "").strip().rstrip(".!?¡¿").casefold().split())@@ -3457,6 +3492,56 @@ class RealtimeVoiceSession:)).strip()if event.is_final and input_observed:+ # BARGE-IN DURING AN ACTION. Everything below this+ # point routes the utterance as a REQUEST; a request+ # to abandon the running action has to be answered+ # before that, because none of the routing can express+ # "undo what you are doing". Ordered deliberately:+ #+ # - the mic probe first, so a hesitation inside one+ # sentence can never read as a stop (the provider+ # commits on ITS VAD, mid-utterance, and "warte"+ # is also just a word people say while thinking);+ # - the two open-question probes next, so a bare+ # "no" answering a clarify question or an ask-tier+ # confirmation stays an ANSWER;+ # - only then the words.+ interrupt_kind = INTERRUPT_NONE+ if not (+ self._user_is_speaking()+ or self._answers_open_delegate_question()+ or self._brain_awaits_voice_confirm()+ ):+ # THIS chunk first, then the whole turn. Finals+ # without an item id APPEND (``_note_user_final``),+ # so on a provider that never split the turn the+ # accumulated text reads "Write this to my wiki.+ # Stop." — and a stop word in the middle is not a+ # stop. The chunk is what the user just said.+ interrupt_kind = classify_interrupt(+ transcript+ ) or classify_interrupt(self._last_user_text)+ if interrupt_kind != INTERRUPT_NONE and (+ self._turn_has_pending_delegate(self._turn_id)+ or self._has_pending_delegate_from_earlier_turn()+ or self._late_delegate_results+ ):+ cancelled = await self._cancel_running_delegates(+ reason=interrupt_kind+ )+ if cancelled and interrupt_kind == INTERRUPT_STOP:+ # Nothing replaces the cancelled order, so+ # this turn is complete once it is confirmed.+ # Claiming the response here also stops the+ # provider — whose context still holds the+ # order — from answering the request the user+ # just withdrew.+ self._delegate_required_for_turn = False+ await self._acknowledge_interrupt()+ # A REDIRECT keeps falling through: the remainder+ # ("…I meant Rome") is a real order and is routed+ # by the ordinary path below, now that the order+ # it replaces is gone.turn_plan = self._plan_turn(self._last_user_text)if (turn_plan.requires_orchestrator@@ -7154,6 +7239,16 @@ class RealtimeVoiceSession:):return Falsetext = str(self._last_user_text or "").strip()+ if classify_interrupt(text) != INTERRUPT_NONE:+ # "Stop", "warte mal", "no, I meant Rome" — every one of these is+ # SHORT and carries no planner reason of its own, so all four+ # continuation probes below hold and the fragment would be folded+ # into the running order and answered with a progress line. That+ # is the exact shape of the reported bug: speaking during an+ # action did nothing except make Jarvis say he was still working+ # on it. An explicit stop is never a continuation of the thing it+ # asks to stop.+ return Falseif not text or len(text.split()) > _CONTINUATION_FRAGMENT_MAX_TOKENS:return Falseif turn_plan.reasons & _SELF_STANDING_ORDER_REASONS:@@ -7322,6 +7417,103 @@ class RealtimeVoiceSession:)await self._send_json(self._surface_speech_message(status_text))+ async def _cancel_running_delegates(self, *, reason: str) -> int:+ """Abandon every still-running delegated action. Returns the count.++ The counterpart to ``_retain_detached_delegate_task``: that path keeps+ an action alive because the user moved on to something ELSE and still+ wants the result. This one runs when the user said to stop, so the+ result is not merely late — it is unwanted, and delivering it later+ would be the assistant ignoring an explicit instruction.++ Three things have to go, or the cancelled work comes back:++ 1. the task itself (reaped through the heartbeat-bounded helper, never+ a bare await after ``cancel()`` — see ``_cancel_and_reap``);+ 2. any result ALREADY queued for the late flush, which would otherwise+ be spoken minutes later as a follow-up nobody asked for;+ 3. the turn state's delivery latch, so a delegate finishing inside the+ cancellation window cannot re-queue itself on the way out.+ """+ turn_ids = [+ turn_id+ for turn_id, tasks in self._delegate_tasks_by_turn.items()+ if any(not task.done() for task in tasks)+ ]+ pending = [+ task+ for turn_id in turn_ids+ for task in tuple(self._delegate_tasks_by_turn.get(turn_id, ()))+ if not task.done()+ ]+ # Queued results are dropped even when no task is still running: the+ # action may have completed microseconds before the user said stop,+ # and its follow-up is exactly as unwanted.+ dropped = self._drop_queued_delegate_results(turn_ids)+ if not pending and not dropped:+ return 0+ for turn_id in turn_ids:+ state = self._delegate_turns.get(turn_id)+ if state is None:+ continue+ # Latch the delivery so _queue_late_delegate_result refuses this+ # turn from now on, whatever order the cancellation resolves in.+ state.delivery_started = True+ if state.delivery_id:+ self._delegate_delivery_status[state.delivery_id] = (+ "cancelled_by_user"+ )+ for task in pending:+ await self._cancel_and_reap(task)+ log.info(+ "realtime[%s] user interrupt (%s) cancelled %d running action(s) "+ "and dropped %d queued result(s)",+ self.session_id,+ reason,+ len(pending),+ dropped,+ )+ return len(pending) + dropped++ def _drop_queued_delegate_results(self, turn_ids: list[str]) -> int:+ """Discard late results belonging to ``turn_ids``. Returns the count.++ Keyed by delivery id rather than turn id because ``_LateDelegateResult``+ carries only the former; the turn states supply the mapping.+ """+ delivery_ids = {+ state.delivery_id+ for turn_id in turn_ids+ if (state := self._delegate_turns.get(turn_id)) is not None+ and state.delivery_id+ }+ if not delivery_ids or not self._late_delegate_results:+ return 0+ keep = [+ pending+ for pending in self._late_delegate_results+ if pending.delivery_id not in delivery_ids+ ]+ dropped = len(self._late_delegate_results) - len(keep)+ self._late_delegate_results = keep+ return dropped++ async def _acknowledge_interrupt(self) -> None:+ """Own this turn with one short confirmation that the action stopped.++ Deliberately the SAME shape as ``_speak_pending_action_status``: the+ orchestrator speaks a closed-pool line through the surface TTS and+ drops the provider's freestyle response for the turn. The provider+ cannot be trusted with it — its context still holds the order it was+ told to carry out, and left to itself it answers the cancelled request+ instead of confirming the cancellation.+ """+ ack_text = _pick_interrupt_ack_text(self._language)+ self._response_requested_for_turn = True+ self._drop_provider_output_until_user_turn = True+ self._output_transcript.append(ack_text)+ await self._send_json(self._surface_speech_message(ack_text))+async def _handle_tool_call(self, event: Any) -> None:if self._session is None:returndiff --git a/jarvis/speech/interrupt_intent.py b/jarvis/speech/interrupt_intent.pynew file mode 100644index 0000000000..a353723852--- /dev/null+++ b/jarvis/speech/interrupt_intent.py@@ -0,0 +1,242 @@+"""Shared barge-in intent detection — "stop what you are doing", not "hang up".++Sibling of :mod:`jarvis.speech.hangup`, and deliberately its opposite number:+``HANGUP_RE`` ends the whole call, this module ends the *current action* while+the call stays open. Both surfaces that run a live conversation need it — the+realtime orchestrator (``jarvis/realtime/session.py``) and, in future, the+classic pipeline — so it lives here rather than inside either one.++Why it exists+-------------+A realtime turn that routes to the tool model is silent for as long as the+action runs. Speaking into that silence used to be absorbed: the orchestrator+classified a short fragment as a continuation of the order already executing+and answered with a canned progress line instead of taking the floor (see+``_continues_executing_order``). That is correct for a sentence split by a+thinking pause ("…the skill system doesn't work" / "you know, recognize the+skills") and wrong for a user saying "stop". Only the words can tell the two+apart, so the words get their own deterministic probe.++Design rules+------------+* **Regex only.** This sits on the live-turn hot path, next to the VAD edge; a+ model call here would add exactly the latency barge-in exists to remove+ (AP-9/AP-11), and prompt compliance is not a correctness boundary+ (BUG-047 class rule).+* **Every locale equal** (CLAUDE.md §1). German, English and Spanish carry the+ same vocabulary depth; adding a locale means adding its tokens, never+ special-casing a "default" language.+* **Whole-utterance anchored.** A stop token only counts at the START of the+ utterance and only when everything before it is a filler. "Don't stop the+ music" must never cancel an action.+* **Two strengths.** ``_HARD_STOP`` tokens are unambiguous enough to also+ cancel when the user keeps talking ("wait, I meant Rome"). ``_SOFT_STOP``+ tokens (a bare "no") only count when they are the ENTIRE utterance —+ otherwise "no problem" would abort a running action.+* **Hang-up wins.** "stopp jarvis" is already a closing command in+ ``HANGUP_RE``; anything that phrase matches is refused here so the two+ intents can never race.++Standard library only (``re``), like ``hangup.py``: ``jarvis/speech/__init__.py``+is intentionally empty, so importing this module pulls in nothing else and any+surface can use it without dragging in audio dependencies.+"""+from __future__ import annotations++import re+from typing import Final++from jarvis.speech.hangup import HANGUP_RE++#: Nothing in the utterance asks to stop.+INTERRUPT_NONE: Final[str] = ""+#: The utterance is nothing BUT a stop request ("stop", "warte mal", "no").+#: The running action is obsolete and no new order replaces it.+INTERRUPT_STOP: Final[str] = "stop"+#: A stop request that carries a replacement ("wait, I meant Rome"). The+#: running action is obsolete AND the remainder is a new order of its own.+INTERRUPT_REDIRECT: Final[str] = "redirect"++# Discourse particles that may precede the stop token without changing it.+# Kept generous: a hesitation sound ("ähm") reaches us # i18n-allow: STT output+# spelled a dozen different ways, and a filler is never evidence either way.+_FILLER: Final[str] = (+ r"(?:okay|okey|ok|oke|hey|hallo|jarvis|bitte|please|por\s+favor|vale|oye|"+ r"äh+m?|ah+|eh+|uh+|um+|hm+|mhm|also|na|pues|bueno|so)" # i18n-allow: STT fillers+)++# Unambiguous "abandon what you are doing" tokens. Strong enough that the user+# may keep talking after them — the remainder is then a replacement order.+_HARD_STOP_PATTERNS: Final[tuple[str, ...]] = (+ # --- German ---------------------------------------------------------- #+ # One family for both locales: German "stopp" and English "stop" are the+ # same spoken token, and two separate alternatives would let the shorter+ # one shadow the longer ("Stop it" matching only "Stop").+ r"stopp?(?:\s+(?:it|that|mal))?", # i18n-allow: spoken command+ r"halt", # i18n-allow+ r"warte(?:t|n)?(?:\s+(?:mal|kurz|bitte))*", # i18n-allow+ r"wart", # i18n-allow+ r"moment(?:\s+mal)?", # i18n-allow+ r"(?:einen|ein)\s+moment", # i18n-allow+ r"abbrechen", # i18n-allow+ r"abbruch", # i18n-allow+ r"brich(?:\s+(?:das|es))?\s+ab", # i18n-allow+ r"brech(?:\s+(?:das|es))?\s+ab", # i18n-allow+ r"h(?:ö|oe)r(?:\s+mal)?\s+auf", # i18n-allow+ r"lass(?:\s+(?:es|das|mal|stecken|gut\s+sein))+", # i18n-allow+ r"vergiss(?:\s+(?:es|das))+", # i18n-allow+ r"(?:doch|lieber)\s+nicht", # i18n-allow+ r"nicht\s+mehr", # i18n-allow+ r"egal", # i18n-allow+ r"quatsch", # i18n-allow+ # --- English --------------------------------------------------------- #+ r"wait(?:\s+(?:a\s+(?:second|sec|moment|minute)|up))?",+ r"hold\s+(?:on|up)",+ r"hang\s+on",+ r"never\s*mind",+ r"forget\s+(?:it|that)",+ r"cancel(?:\s+(?:it|that))?",+ r"abort",+ r"scratch\s+that",+ r"drop\s+it",+ r"my\s+bad",+ r"one\s+(?:second|sec|moment)",+ # --- Spanish --------------------------------------------------------- #+ r"p(?:a|á)ra(?:te)?", # i18n-allow+ r"alto", # i18n-allow+ r"espera(?:te)?", # i18n-allow+ r"esp(?:é|e)rate", # i18n-allow+ r"(?:un\s+)?momento", # i18n-allow+ r"cancela(?:lo)?", # i18n-allow+ r"canc(?:é|e)lalo", # i18n-allow+ r"anula(?:lo)?", # i18n-allow+ r"olv(?:í|i)dalo", # i18n-allow+ r"olvida\s+(?:eso|lo)", # i18n-allow+ r"d(?:é|e)jalo", # i18n-allow+ r"deja\s+eso", # i18n-allow+ r"no\s+importa", # i18n-allow+ r"mejor\s+no", # i18n-allow+)++# Bare negations. A genuine stop when they are the WHOLE utterance, ordinary+# speech the moment anything follows them ("no problem", "nein danke").+_SOFT_STOP_PATTERNS: Final[tuple[str, ...]] = (+ r"nein", # i18n-allow+ r"nee", # i18n-allow+ r"n(?:ö|oe)", # i18n-allow+ r"no",+ r"nope",+ r"nah",+)++_HARD_STOP: Final[str] = "|".join(_HARD_STOP_PATTERNS)+_ANY_STOP: Final[str] = "|".join((*_HARD_STOP_PATTERNS, *_SOFT_STOP_PATTERNS))++# A run of stop tokens (optionally repeated: "no no", "stop stop") preceded by+# any number of fillers. ``rest`` is whatever the user kept saying.+#+# The trailing ``\b`` after every token group is load-bearing: Python's+# alternation is leftmost-first, so without it the shorter alternative wins and+# swallows the longer one ("no" matching the head of "nope", "moment" matching+# the head of "momento") — the remainder then looks like ordinary speech and+# the interrupt is lost. The boundary forces the backtrack to the full token.+_HARD_LEAD_RE: Final[re.Pattern[str]] = re.compile(+ rf"^\s*(?:{_FILLER}\b[\s,.!?]+)*(?:{_HARD_STOP})\b"+ rf"(?:[\s,]+(?:{_ANY_STOP})\b)*"+ rf"(?P<rest>[\s,.!?…]*.*)$",+ re.IGNORECASE | re.DOTALL,+)+_ANY_LEAD_RE: Final[re.Pattern[str]] = re.compile(+ rf"^\s*(?:{_FILLER}\b[\s,.!?]+)*(?:{_ANY_STOP})\b"+ rf"(?:[\s,]+(?:{_ANY_STOP})\b)*"+ rf"(?P<rest>[\s,.!?…]*.*)$",+ re.IGNORECASE | re.DOTALL,+)+# Trailing politeness that must not turn a pure stop into a redirect.+_TAIL_RE: Final[re.Pattern[str]] = re.compile(+ rf"^(?:[\s,.!?…]|{_FILLER}|danke|thanks|thank\s+you|gracias)*$", # i18n-allow+ re.IGNORECASE,+)++# A bare negation is too weak to cancel an action on its own once the user+# keeps talking — "no problem" and "nein danke" are ordinary speech. It becomes+# a redirect only in the one shape that is unmistakably a correction, which is+# also the shape the classic pipeline already knows as the "nein, ich meinte X"+# command (``jarvis/speech/turn_buffer.py``).+_CORRECTION_RE: Final[re.Pattern[str]] = re.compile(+ r"^(?:"+ r"ich\s+(?:meinte|meine|wollte|sagte)|" # i18n-allow+ r"eigentlich|sondern|stattdessen|lieber|besser|" # i18n-allow+ r"mach(?:\s+(?:lieber|stattdessen|es))?|nimm|" # i18n-allow+ r"i\s+(?:meant|mean|said|wanted)|"+ r"actually|instead|rather|make\s+it|"+ r"quer(?:í|i)a\s+decir|quiero\s+decir|" # i18n-allow+ r"en\s+realidad|m(?:á|a)s\s+bien|mejor" # i18n-allow+ r")\b",+ re.IGNORECASE,+)++#: A replacement order longer than this is treated as ordinary speech that+#: merely happens to open with a stop word. Keeps a monologue beginning with+#: "wait" from cancelling an action the user never mentioned again.+_REDIRECT_MAX_TOKENS: Final[int] = 24+++def classify_interrupt(text: str | None) -> str:+ """Classify ``text`` as a stop request, a redirect, or neither.++ Returns :data:`INTERRUPT_STOP` when the utterance is nothing but a request+ to abandon the current action, :data:`INTERRUPT_REDIRECT` when it opens+ with an unambiguous stop token and carries a replacement order, and+ :data:`INTERRUPT_NONE` otherwise.++ Never raises: a malformed transcript answers "no interrupt", because the+ fail-safe direction here is letting the action finish.+ """+ utterance = " ".join(str(text or "").split())+ if not utterance:+ return INTERRUPT_NONE+ # Hang-up owns its own phrases; a closing command must end the CALL, not+ # merely cancel an action and leave the session listening.+ if HANGUP_RE.search(utterance):+ return INTERRUPT_NONE++ match = _ANY_LEAD_RE.match(utterance)+ if match is not None and _TAIL_RE.match(match.group("rest") or ""):+ # Everything after the stop run is punctuation or politeness.+ return INTERRUPT_STOP++ hard = _HARD_LEAD_RE.match(utterance)+ if hard is not None:+ rest = str(hard.group("rest") or "").strip(" ,.!?…")+ if not rest:+ return INTERRUPT_STOP+ if len(rest.split()) > _REDIRECT_MAX_TOKENS:+ return INTERRUPT_NONE+ return INTERRUPT_REDIRECT++ # Soft opener with a remainder: only the explicit correction shape counts.+ if match is None:+ return INTERRUPT_NONE+ rest = str(match.group("rest") or "").strip(" ,.!?…")+ if (+ rest+ and len(rest.split()) <= _REDIRECT_MAX_TOKENS+ and _CORRECTION_RE.match(rest)+ ):+ return INTERRUPT_REDIRECT+ return INTERRUPT_NONE+++def is_interrupt_intent(text: str | None) -> bool:+ """True when ``text`` asks to abandon the running action in any form."""+ return classify_interrupt(text) != INTERRUPT_NONE+++__all__ = [+ "INTERRUPT_NONE",+ "INTERRUPT_REDIRECT",+ "INTERRUPT_STOP",+ "classify_interrupt",+ "is_interrupt_intent",+]diff --git a/tests/unit/realtime/test_session_action_interrupt.py b/tests/unit/realtime/test_session_action_interrupt.pynew file mode 100644index 0000000000..87474501f2--- /dev/null+++ b/tests/unit/realtime/test_session_action_interrupt.py@@ -0,0 +1,303 @@+"""Barge-in while a delegated action runs — the "thinking" phase.++The reported bug: speaking while Jarvis was working on something did nothing.+The session deferred the provider's speech edge (``_pending_delegate_needs_+endpoint_protection``), the confirm path never cancelled anything, and a short+utterance was folded into the running order by ``_continues_executing_order``+and answered with a canned progress line. The action kept running and its+result was still delivered.++These tests pin the four behaviours that fix it, and the four that must NOT+change with it.+"""+from __future__ import annotations++import asyncio++import pytest++from jarvis.realtime.protocol import RealtimeEvent+from tests.unit.realtime.test_session import (+ FakeBrain,+ FakeProvider,+ FakeSession,+ _session,+)++ORDER_TEXT = "Write this to my wiki."+++def _scripted_provider(script):+ """A provider whose session yields ``script`` — a list or a coroutine fn."""++ class _ScriptedSession(FakeSession):+ async def receive(self):+ async for event in script():+ yield event++ class _ScriptedProvider(FakeProvider):+ async def open_session(self, cfg):+ self.opened_with = cfg+ self.session = _ScriptedSession([])+ return self.session++ return _ScriptedProvider([])+++def _spoken(jsons):+ """Surface-TTS lines the orchestrator spoke itself."""+ return [+ str(message.get("text") or "")+ for message in jsons+ if message.get("type") == "error_spoken"+ ]+++async def _run(provider, brain, jsons):+ sess = _session(provider, brain=brain, jsons=jsons)+ await sess.handle_control({"type": "audio_start", "sample_rate": 16_000})+ await sess.wait_finished()+ return sess+++ "stop_phrase",+ ["Stop.", "Warte mal.", "Nein.", "Forget it.", "Olvídalo."], # i18n-allow+)+async def test_a_stop_during_a_running_action_cancels_it(stop_phrase):+ """The action is abandoned — not detached, not merely un-spoken."""+ gate = asyncio.Event()+ dispatched = asyncio.Event()++ class _SignallingBrain(FakeBrain):+ async def generate(self, text, **kwargs):+ dispatched.set()+ return await super().generate(text, **kwargs)++ brain = _SignallingBrain(replies=("Wiki updated.",), gate=gate)++ async def script():+ yield RealtimeEvent(type="input_transcript", text=ORDER_TEXT, is_final=True)+ await dispatched.wait()+ yield RealtimeEvent(type="speech_started")+ yield RealtimeEvent(type="input_transcript", text=stop_phrase, is_final=True)++ jsons = []+ sess = await _run(_scripted_provider(script), brain, jsons)+ gate.set()+ await asyncio.sleep(0.1)++ assert brain.cancelled, "the delegated action kept running after a stop"+ assert not sess._late_delegate_results, (+ "the cancelled action still queued its result as a follow-up"+ )+ await sess.end(reason="test")+++async def test_b_the_stop_is_confirmed_out_loud():+ """A silent cancellation is indistinguishable from being ignored."""+ gate = asyncio.Event()+ dispatched = asyncio.Event()++ class _SignallingBrain(FakeBrain):+ async def generate(self, text, **kwargs):+ dispatched.set()+ return await super().generate(text, **kwargs)++ brain = _SignallingBrain(replies=("Wiki updated.",), gate=gate)++ async def script():+ yield RealtimeEvent(type="input_transcript", text=ORDER_TEXT, is_final=True)+ await dispatched.wait()+ yield RealtimeEvent(type="speech_started")+ yield RealtimeEvent(type="input_transcript", text="Stop.", is_final=True)++ jsons = []+ sess = await _run(_scripted_provider(script), brain, jsons)+ gate.set()+ await asyncio.sleep(0.1)++ lines = _spoken(jsons)+ assert lines, "the user got no confirmation that the action stopped"+ # ...and it is a cancellation, never the "still working on it" line the+ # old continuation guard produced for exactly this utterance.+ assert not any(+ "still" in line.lower() or "moment" in line.lower() for line in lines+ ), f"a stop was answered with a progress line: {lines}"+ await sess.end(reason="test")+++async def test_c_a_redirect_cancels_the_old_order_and_keeps_the_new_one():+ """"Wait, I meant Rome" must abandon Paris AND book Rome."""+ gate = asyncio.Event()+ dispatched = asyncio.Event()++ class _SignallingBrain(FakeBrain):+ async def generate(self, text, **kwargs):+ dispatched.set()+ return await super().generate(text, **kwargs)++ brain = _SignallingBrain(replies=("Booked.",), gate=gate)++ async def script():+ yield RealtimeEvent(+ type="input_transcript", text="Book me a flight to Paris.", is_final=True+ )+ await dispatched.wait()+ yield RealtimeEvent(type="speech_started")+ yield RealtimeEvent(+ type="input_transcript", text="Wait, I meant Rome.", is_final=True+ )++ jsons = []+ sess = await _run(_scripted_provider(script), brain, jsons)+ gate.set()+ await asyncio.sleep(0.1)++ assert brain.cancelled, "the superseded order was not abandoned"+ # A redirect does NOT take the turn with a cancellation line — the+ # replacement order owns it and is routed normally.+ assert not any(+ "stopped" in line.lower() or "cancel" in line.lower()+ for line in _spoken(jsons)+ )+ await sess.end(reason="test")+++async def test_d_a_continuation_fragment_still_does_not_cancel():+ """The 2026-08-12 guard must survive: a split sentence is not a stop."""+ gate = asyncio.Event()+ dispatched = asyncio.Event()++ class _SignallingBrain(FakeBrain):+ async def generate(self, text, **kwargs):+ dispatched.set()+ return await super().generate(text, **kwargs)++ brain = _SignallingBrain(replies=("Done.",), gate=gate)++ async def script():+ yield RealtimeEvent(+ type="input_transcript",+ text="Brief the pane on the skill system.",+ is_final=True,+ )+ await dispatched.wait()+ yield RealtimeEvent(type="speech_started")+ yield RealtimeEvent(+ type="input_transcript",+ text="you know, recognize the skills",+ is_final=True,+ )++ jsons = []+ sess = await _run(_scripted_provider(script), brain, jsons)+ gate.set()+ await asyncio.sleep(0.1)++ assert not brain.cancelled, (+ "an ordinary continuation fragment cancelled the running order"+ )+ await sess.end(reason="test")+++async def test_e_a_bare_no_answering_a_clarify_question_is_not_a_stop():+ """An open delegate question owns the next short answer.++ Two independent guards keep it that way, and this pins the second one.+ The first is structural: a delegate that asked a question has COMPLETED,+ so nothing is running and the cancellation path is never reached. The+ second covers the overlap — action A still running while action B's+ clarify question is open — where only ``_answers_open_delegate_question``+ can tell the answer from a stop. That latch is recomputed at every turn+ boundary (``session.py`` ~6565) and is false while A is unfinished, so it+ is asserted directly rather than through a latch the flow would clear.+ """+ gate = asyncio.Event()+ dispatched = asyncio.Event()++ class _SignallingBrain(FakeBrain):+ async def generate(self, text, **kwargs):+ dispatched.set()+ return await super().generate(text, **kwargs)++ brain = _SignallingBrain(replies=("Which one?",), gate=gate)++ async def script():+ yield RealtimeEvent(type="input_transcript", text=ORDER_TEXT, is_final=True)+ await dispatched.wait()+ yield RealtimeEvent(type="speech_started")+ yield RealtimeEvent(type="input_transcript", text="No.", is_final=True)++ jsons = []+ sess = _session(_scripted_provider(script), brain=brain, jsons=jsons)+ sess._answers_open_delegate_question = lambda: True+ await sess.handle_control({"type": "audio_start", "sample_rate": 16_000})+ await sess.wait_finished()+ gate.set()+ await asyncio.sleep(0.1)++ assert not brain.cancelled, "a clarify ANSWER was treated as a stop"+ await sess.end(reason="test")+++async def test_g_a_hesitation_mid_sentence_is_not_a_stop():+ """The microphone outranks the words while the user is still talking.++ "Warte" is also just a word people say while thinking. The provider+ commits on ITS VAD, so a filler pause can surface as a final transcript+ in the middle of one spoken sentence — cancelling there would abandon the+ order the user is still in the middle of giving.+ """+ gate = asyncio.Event()+ dispatched = asyncio.Event()++ class _SignallingBrain(FakeBrain):+ async def generate(self, text, **kwargs):+ dispatched.set()+ return await super().generate(text, **kwargs)++ brain = _SignallingBrain(replies=("Done.",), gate=gate)++ async def script():+ yield RealtimeEvent(type="input_transcript", text=ORDER_TEXT, is_final=True)+ await dispatched.wait()+ yield RealtimeEvent(type="speech_started")+ yield RealtimeEvent(type="input_transcript", text="Warte.", is_final=True) # i18n-allow++ jsons = []+ sess = _session(_scripted_provider(script), brain=brain, jsons=jsons)+ # The mic still carries the user's voice: mid-utterance, not a barge-in.+ sess._user_is_speaking = lambda: True+ await sess.handle_control({"type": "audio_start", "sample_rate": 16_000})+ await sess.wait_finished()+ gate.set()+ await asyncio.sleep(0.1)++ assert not brain.cancelled, (+ "a hesitation inside one sentence cancelled the running order"+ )+ await sess.end(reason="test")+++async def test_f_a_stop_with_nothing_running_cancels_nothing():+ """No action in flight — the stop is ordinary speech for the provider."""++ async def script():+ yield RealtimeEvent(type="input_transcript", text="Stop.", is_final=True)++ brain = FakeBrain(replies=("ok",))+ jsons = []+ sess = await _run(_scripted_provider(script), brain, jsons)++ assert not brain.cancelled+ # Nothing was cancelled, so the orchestrator never claimed the turn.+ assert not any("stopped" in line.lower() for line in _spoken(jsons))+ await sess.end(reason="test")diff --git a/tests/unit/speech/test_interrupt_intent.py b/tests/unit/speech/test_interrupt_intent.pynew file mode 100644index 0000000000..3579c9dd5c--- /dev/null+++ b/tests/unit/speech/test_interrupt_intent.py@@ -0,0 +1,150 @@+"""Barge-in intent detection: stop the ACTION, never the call.++The asymmetry that decides every tie here: a missed stop costs the user one+repeat (today's behaviour, which is what the probe exists to fix), a false stop+cancels work the user asked for. So the ambiguous cases below are asserted as+NON-interrupts on purpose.+"""+from __future__ import annotations++import pytest++from jarvis.speech.interrupt_intent import (+ INTERRUPT_NONE,+ INTERRUPT_REDIRECT,+ INTERRUPT_STOP,+ classify_interrupt,+ is_interrupt_intent,+)+++ "utterance",+ [+ # German+ "Stopp", # i18n-allow: spoken command under test+ "stopp!", # i18n-allow+ "Halt", # i18n-allow+ "warte", # i18n-allow+ "warte mal", # i18n-allow+ "Warte mal kurz", # i18n-allow+ "okay, warte mal", # i18n-allow+ "ähm, moment mal", # i18n-allow+ "einen Moment", # i18n-allow+ "abbrechen", # i18n-allow+ "brich das ab", # i18n-allow+ "hör auf", # i18n-allow+ "hoer mal auf", # i18n-allow+ "lass es", # i18n-allow+ "lass gut sein", # i18n-allow+ "vergiss es", # i18n-allow+ "doch nicht", # i18n-allow+ "lieber nicht", # i18n-allow+ "egal", # i18n-allow+ "nein", # i18n-allow+ "nee", # i18n-allow+ "nein, nein", # i18n-allow+ # English+ "stop",+ "Stop it.",+ "stop stop stop",+ "wait",+ "wait a second",+ "hold on",+ "hang on",+ "never mind",+ "nevermind",+ "forget it",+ "cancel that",+ "abort",+ "scratch that",+ "no",+ "no no",+ "nope",+ # Spanish+ "para", # i18n-allow+ "espera", # i18n-allow+ "un momento", # i18n-allow+ "cancélalo", # i18n-allow+ "olvídalo", # i18n-allow+ "déjalo", # i18n-allow+ "no importa", # i18n-allow+ "mejor no", # i18n-allow+ ],+)+def test_pure_stop_requests_are_recognized(utterance: str) -> None:+ assert classify_interrupt(utterance) == INTERRUPT_STOP+ assert is_interrupt_intent(utterance)+++ ("utterance", "expected_tail"),+ [+ ("warte, ich meinte Rom", "ich meinte Rom"), # i18n-allow+ ("stopp, mach lieber Rom", "mach lieber Rom"), # i18n-allow+ ("nein, ich meinte Rom", "ich meinte Rom"), # i18n-allow+ ("nein, eigentlich Rom", "eigentlich Rom"), # i18n-allow+ ("wait, I meant Rome", "I meant Rome"),+ ("no, actually book Rome", "actually book Rome"),+ ("hold on, make it Rome instead", "make it Rome instead"),+ ("espera, quería decir Roma", "quería decir Roma"), # i18n-allow+ ],+)+def test_stop_with_replacement_is_a_redirect(+ utterance: str, expected_tail: str+) -> None:+ """A correction cancels the running action AND carries the new order."""+ assert classify_interrupt(utterance) == INTERRUPT_REDIRECT+ assert expected_tail # documents the remainder the caller re-dispatches+++ "utterance",+ [+ # A stop word buried mid-sentence is ordinary speech.+ "don't stop the music",+ "erzähl mir nicht, dass ich warten soll", # i18n-allow+ "the wait times at the airport",+ # Bare negations that continue into ordinary speech.+ "no problem",+ "no worries",+ "nein danke, das passt so", # i18n-allow+ "nope, that one is fine actually let me think about it",+ # A monologue that merely opens with a stop word.+ (+ "wait until the whole deployment pipeline has finished and then "+ "tell me which of the seventeen integration tests are still "+ "failing on the windows runner please"+ ),+ # Empty / junk.+ "",+ " ",+ ],+)+def test_ordinary_speech_is_not_an_interrupt(utterance: str) -> None:+ assert classify_interrupt(utterance) == INTERRUPT_NONE+ assert not is_interrupt_intent(utterance)+++ "utterance",+ [+ "auflegen", # i18n-allow+ "stopp jarvis", # i18n-allow+ "stop jarvis",+ "jarvis stop",+ "hang up",+ "goodbye",+ "tschüss", # i18n-allow+ ],+)+def test_hangup_phrases_never_classify_as_an_action_interrupt(+ utterance: str,+) -> None:+ """Ending the CALL outranks ending the action; the two must not race."""+ assert classify_interrupt(utterance) == INTERRUPT_NONE+++def test_none_input_is_safe() -> None:+ assert classify_interrupt(None) == INTERRUPT_NONE+ assert not is_interrupt_intent(None)