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
fix(realtime): a delegate reply is spoken once — discard the server's second answer to the same request
Harald4ed384b620fd
diff --git a/docs/BUGS.md b/docs/BUGS.mdindex 4f702f5fdd..1a7e47d318 100644--- a/docs/BUGS.md+++ b/docs/BUGS.md@@ -9836,3 +9836,108 @@ really_opened_with`; `test_live_usage_metering.py` (Vertex Live defaultpriced); `test_gemini_live.py` / `test_openai_realtime.py` (`session.model`).The root `tests/conftest.py` redirects the feed to `tmp_path` per test so nosuite reaches the network or the developer's real cache file.++## BUG-143: the same delegate reply is spoken twice — "Ich habe work geöffnet: T eins." then "…: T1." — the server-VAD transport answers a second boundary for one request and the session plays it as a phantom turn (HIGH, FIXED 2026-08-18)++**Symptom.** Realtime voice session `7b20e182` (2026-08-18 14:24, Vertex Live,+delegate tool mode). Turn 4 ("open a new workspace … a terminal") was answered+"Ich habe work geöffnet: T eins." — and 16 ms after that turn's boundary the+provider spoke "Ich habe work geöffnet: T1." again. Turn 6 repeated the+whole 144-character terminal status verbatim. The recorder wrote turns 5 and+7 with EMPTY user text and their own `AudioOutFirst`/`realtime_usage` events+(69 and 208 output tokens): two separate provider generations, not a+transcript artifact and not the surface TTS. Same shape at 15:58 (session+`805bbee2`): a phantom turn re-rendered a delivered confirmation prompt and+tripped the surface-fallback stale-readback guard, which then SPOKE its+clarify line ("Das hatte ich gerade schon beantwortet …") to a user who had+said nothing.++**Reconstruction (recorder + flight recorder + desktop log).**++1. In both doubled turns the user kept talking after Gemini's VAD had closed+ the input turn (`holding the dispatch — the microphone still carries the+ user's voice`; `provider committed a boundary while the user is still+ audibly speaking`). The session correctly merged the tail into ONE turn,+ ran the action once, and injected the trusted result once+ (`_delegate_result_prompt` via `send_text`).+2. On the server that one turn carried TWO turn-ending inputs: the injected+ text (which closes the open audio activity — the behaviour the 2026-08-13+ 11:20 forensic in `deliver_announcement` recorded when an injected result+ "closed the sentence"), and the+ server's own silence detection closing that activity again moments later.+ Gemini answers each boundary with its own generation, serially. Every+ generation after the injected result re-renders that result, because the+ rendering order is the newest instruction in context — hence "T eins" and+ then "T1", two independent renderings of the same brain reply.+3. `_run_deterministic_delegate` → `_await_provider_response_boundary`+ injects immediately when `_drop_provider_output_until_new_response` is set+ ("the competing native response was already retired") — but on this+ transport `interrupt()` is a no-op (Gemini has no response cancel), so+ nothing was retired server-side. The injection races the VAD end.+4. `_ensure_turn_started` opens a turn for ANY provider output with no open+ turn. Nothing said "a generation that begins right after a rendered+ readback with no new user input is stale", so the second rendering was+ played and recorded as a user-less turn. Turns 0 and 3 of the same call+ did not double: their results were injected 2.5–5 s after the last voiced+ frame, by which time the server had already answered the trailing speech+ (natively, withheld). Only a result injected within ~1 s of the tail races+ it.++**Fix (`jarvis/realtime/session.py`, stale-generation guard).** When a turn+closes on a provider `turn_complete` after the provider rendered a text+Jarvis injected — a delegate result (`delivery_started`, provider audio was+heard, no surface fallback, no scrub cancel) or an out-of-band readback+(`_external_update`) — `_arm_stale_generation_guard` stamps the moment+(automatic-response transports only: a manual-response transport generates+nothing Jarvis did not ask for; capability, never a provider name, AP-21).+Every audio/transcript event is judged once at the top of the pump+(`_judge_stale_generation_event`): if the guard is armed, no turn is open, no+deliberate injection is pending, the microphone carried no voice since the+readback ended, no server speech edge was confirmed, and the 2.5 s window has+not run out, the generation is marked as being discarded and+`_output_withhold_reason` keeps every event of it out of the gate, the+surface, the recorder and the turn record until its own boundary. A tool+call from that generation (Gemini can answer the second boundary with a+`jarvis_action` or `end_call` instead of speech) is refused with an honest+result so the model can close the generation — never executed, never a turn;+a `handoff_requested` from it is ignored. The boundary is consumed+unconditionally: the guard only ever arms as a turn closes, so any turn open+at that moment was opened by a real transcript DURING the drop and keeps+waiting for its own answer instead of being closed as "empty" and re-routed+through the Brain chain. The discarded transcript is logged (bounded) and the+guard stands down. Any fresh evidence disarms it for good — a new input+transcript (`_disarm_stale_generation_guard` at the input-observed site),+local voice (`_last_voiced_input_monotonic`), an open turn,+`_external_update`, the window, a transport rebuild — and the drop itself is+bounded (`_STALE_GENERATION_DROP_MAX_S`, 20 s): a transport that loses the+one terminal frame releases the withhold with a warning instead of leaving+the call deaf. `_session_is_at_rest` and `deliver_announcement` treat a+discarded generation as "still busy" so no follow-up is injected into it. New+counter `stale_generations_dropped` on `RealtimeSessionPostmortem` (+ an info+finding in `realtime_forensics.py`).++**Not changed, on purpose.** The injection is still immediate: waiting for+the server's boundary would add the whole native generation (2–7 s live) of+dead air on every action turn, against the <2 s instant-ack target. The guard+is the deterministic net; the second generation still costs its tokens (the+counter makes that visible). The per-turn steering delta (`gemini_live.py`+`_flush_steering`, 37b4eca3) is a second text input on the same channel and+a candidate for the same double-boundary; the guard covers whichever input+triggers the extra generation.++**Class rule.** On a duplex transport that generates on its own VAD, "one+Jarvis turn" and "one server turn" are different units, and any text the+session injects is a turn-ending input of its own. After the session has+rendered what it asked for, a provider generation that arrives with no user+evidence behind it answers nothing — it must be refused as a unit, at the+pump, before any branch can open a turn for it. Play/record decisions belong+to the session, never to "the provider spoke, so someone must have asked".++**Guards.** `tests/unit/realtime/test_stale_generation_after_readback.py`+(the live failure: one rendering, one turn, no phantom; a stale generation's+`end_call` refused, not executed; a follow-up transcript landing mid-drop+keeps its own turn; and everything that must keep playing — after a new+transcript, after local microphone voice, after the window, a late result+Jarvis injected itself; the capability gate; every disarm reason; the drop+ceiling). `tests/unit/diagnostics/test_realtime_forensics.py` carries the+new finding.diff --git a/jarvis/core/events.py b/jarvis/core/events.pyindex be36404ec1..6f35d3a1fa 100644--- a/jarvis/core/events.py+++ b/jarvis/core/events.py@@ -1748,6 +1748,10 @@ class RealtimeSessionPostmortem(Event):delegate_delivery_duplicates_suppressed: int = 0#: In-flight delegates transferred from socket to process lifetime.delegate_deliveries_detached: int = 0+ #: Provider generations discarded whole: they began right after a rendered+ #: readback with no new user input — the server answered a second boundary+ #: for the same request (BUG-143). Zero in a healthy call.+ stale_generations_dropped: int = 0#: Opening responses cut at the cap because no user question existed yet.opening_responses_bounded: int = 0#: Self-dialogue verdicts that forced a transport replacement (BUG-124).diff --git a/jarvis/diagnostics/realtime_forensics.py b/jarvis/diagnostics/realtime_forensics.pyindex c109f62475..1be7263f84 100644--- a/jarvis/diagnostics/realtime_forensics.py+++ b/jarvis/diagnostics/realtime_forensics.py@@ -129,6 +129,20 @@ def evaluate_postmortem(pm: Mapping[str, Any]) -> list[RealtimeFinding]:))+ stale_generations = _count(pm, "stale_generations_dropped")+ if stale_generations:+ # The guard worked (nothing was heard twice), but the provider still+ # answered a second boundary for one request - the tokens were spent+ # and the trigger is worth a look (BUG-143).+ add(+ RealtimeFinding(+ "info",+ "stale-generation-dropped",+ f"{stale_generations} unprompted provider generation(s) after a "+ "delivered readback were discarded before playback",+ )+ )+detached_deliveries = _count(pm, "delegate_deliveries_detached")if detached_deliveries:add(diff --git a/jarvis/realtime/session.py b/jarvis/realtime/session.pyindex d7dc15038f..c4613bfdf9 100644--- a/jarvis/realtime/session.py+++ b/jarvis/realtime/session.py@@ -250,6 +250,30 @@ _DELEGATE_NATIVE_BOUNDARY_WAIT_S = 1.0# under one second after a tool result._DELEGATE_READBACK_WAIT_S = 2.5_DELEGATE_READBACK_POLL_S = 0.1+# Stale-generation guard after a delivered readback (live forensic 2026-08-18+# 14:25, session 7b20e182, turns 4/5 and 6/7 — BUG-143). On a transport that+# creates responses on its own VAD, ONE Jarvis turn can carry TWO turn-ending+# inputs on the server: the trusted result text Jarvis injects, and the end+# of the user's trailing speech (the words after the provider's first+# boundary, which the delegate already consumed). Text input closes the open+# audio turn early; the server's own silence detection then closes it AGAIN+# moments later. Both closings are answered — serially — and every answer+# after the injected result re-renders that same result, so the user heard+# "Ich habe work geöffnet: T eins." and then "Ich habe work geöffnet: T1."+# The session cannot cancel that second generation (Gemini has no response+# cancel), but it can refuse to play it: a generation that begins right after+# a provider-rendered delegate readback, with NO new user input in between,+# answers nothing the user asked. The window bounds the guard so a genuine+# later answer is never at risk; local microphone voice, a new transcript, or+# any deliberate injection disarms it immediately.+_STALE_GENERATION_WINDOW_S = 2.5+# A discarded generation is released by its own boundary. Every awaited state+# in this file carries a bound (the turn stall watchdog, the late-result+# flush), and this one is no exception: a transport that loses that single+# terminal frame while the socket stays open must not leave the session deaf+# for the rest of the call. Same order as _TURN_STALL_TIMEOUT_S.+_STALE_GENERATION_DROP_MAX_S = 20.0+_STALE_GENERATION_TRANSCRIPT_MAX_CHARS = 400# Mid-reply audio-flow diagnostics: an audible hole inside one spoken answer# has three distinct producers (scrub gate waiting for a late transcript, the# provider sending no audio, or silence embedded in the provider's own PCM).@@ -2089,6 +2113,19 @@ class RealtimeVoiceSession:# plain turn re-rendering one of them is a stale ghost repeat, not a# fresh answer (live forensic 2026-07-21 11:32).self._stale_readback_refs: list[str] = []+ # Stale-generation guard (BUG-143, see _STALE_GENERATION_WINDOW_S).+ # ``armed_at`` is the monotonic moment a provider-rendered delegate+ # readback turn closed; a generation that begins while it is armed —+ # no open turn, no new user input — is discarded WHOLE (``dropping``+ # stays set until that generation's own boundary), never played and+ # never opened as a turn. ``transcript`` keeps what was discarded for+ # the log line, bounded, so a live call can be checked afterwards.+ self._stale_generation_guard_armed_at = 0.0+ self._stale_generation_guard_reply = ""+ self._stale_generation_dropping = False+ self._stale_generation_dropping_since = 0.0+ self._stale_generation_transcript: list[str] = []+ self._stale_generations_dropped = 0self._hangup_reason = ""self._turn_final_text = ""self._end_after_turn = False@@ -3298,6 +3335,7 @@ class RealtimeVoiceSession:or self._turn_idor self._turn_has_activity()or self._output_active+ or self._stale_generation_drop_active()or self._delegate_tasksor self._pending_tool_eventsor self._response_requested_for_turn@@ -3494,6 +3532,18 @@ class RealtimeVoiceSession:# stall watchdog keeps refreshing on its own while the gate# holds PCM — the settle backstop would then never fire.self._interruption_deferred_at = time.monotonic()+ if event.type in {+ "audio_delta",+ "output_transcript_delta",+ "tool_call",+ "handoff_requested",+ }:+ # BUG-143: a generation that begins right after a delivered+ # readback, with no new user input, is discarded whole —+ # decided once, here, before any branch can open a turn+ # for it, hand a frame of it to the gate, or run an action+ # it asks for.+ self._judge_stale_generation_event(event)if event.type == "input_transcript":transcript = _dictionary_corrected(str(event.text or "").strip())transcription_failed = bool(event.error)@@ -3630,8 +3680,11 @@ class RealtimeVoiceSession:self._input_turn_observed = Trueself._user_speech_active = False# The user audibly opened this turn — a fallback-era- # suppression of stale provider output ends here.+ # suppression of stale provider output ends here, and+ # so does the post-readback stale-generation guard:+ # whatever the provider says next was asked for.self._drop_provider_output_until_user_turn = False+ self._disarm_stale_generation_guard()if (self._external_update is not Noneand self._output_samples_sent == 0@@ -4139,6 +4192,14 @@ class RealtimeVoiceSession:-_ANSWERED_INPUT_ID_MAX:])+ elif event.type == "handoff_requested" and (+ self._stale_generation_drop_active()+ ):+ log.warning(+ "realtime[%s] ignoring a provider handoff from a "+ "discarded stale generation — no action was dispatched",+ self.session_id,+ )elif event.type == "handoff_requested":# Client-managed handoffs are a provider control boundary,# never a public response boundary and never a direct tool@@ -4515,6 +4576,20 @@ class RealtimeVoiceSession:elif event.type == "speech_started":await self._begin_user_speech_turn()await self._barge_in(interrupt_provider=True)+ elif event.type == "tool_call" and self._stale_generation_drop_active():+ # An action (or a hang-up) requested by a generation the+ # session already ruled stale: nobody asked for it. Answer+ # the call with an honest refusal so the model can close+ # its generation — an unanswered function call keeps it+ # open with no boundary ever coming — and never open a+ # turn or execute anything for it (BUG-143).+ log.warning(+ "realtime[%s] refusing tool call %r from a discarded "+ "stale generation — the action was not executed",+ self.session_id,+ str(getattr(event, "tool_name", "") or ""),+ )+ await self._reject_stale_generation_tool_call(event)elif event.type == "tool_call":if str(getattr(event, "tool_name", "") or "") == "end_call":await self._ensure_turn_started()@@ -4540,6 +4615,21 @@ class RealtimeVoiceSession:await self._ensure_turn_started()await self._handle_tool_call(event)elif event.type == "turn_complete":+ if self._stale_generation_drop_active():+ # The discarded generation reached its own boundary.+ # It belongs to that generation alone: the guard only+ # ever arms as a turn closes, so any turn open NOW was+ # opened by a real transcript DURING the drop and is+ # still waiting for its own answer — closing it here+ # would route the user's fresh request through the+ # empty-turn recovery and mute the provider's real+ # answer as delegate-owned. Consume the boundary.+ self._finish_stale_generation_drop()+ self._gate.drain()+ # Same courtesy as a real boundary: a queued late+ # result may deliver now that the wire is quiet.+ self._schedule_late_delegate_flush()+ continueif self._output_language_retry_pending:if not self._output_language_retry_requested:await self._request_output_language_retry()@@ -4821,6 +4911,35 @@ class RealtimeVoiceSession:for chunk in final_chunks:await self._emit_audio(chunk)self._gate.drain()+ rendered_delegate_reply = bool(+ delegate_state is not None+ and delegate_state.delivery_started+ and not delegate_state.surface_fallback_spoken+ and not self._scrub_cancelled_for_turn+ and self._output_samples_sent > 0+ )+ rendered_injected_readback = bool(+ self._external_update is not None+ and self._output_samples_sent > 0+ )+ if rendered_delegate_reply or rendered_injected_readback:+ # The provider just rendered a text Jarvis injected (a+ # delegate result, a late result, an announcement) and+ # this boundary closes that turn. On a server-VAD+ # transport a SECOND generation can follow within+ # milliseconds — the answer to the user's trailing+ # speech, which re-renders the same text (BUG-143).+ # Arm the guard before the turn resets so that+ # generation is discarded instead of opening a phantom+ # turn and speaking the reply twice.+ self._arm_stale_generation_guard(+ str(delegate_state.last_reply or "")+ if rendered_delegate_reply and delegate_state is not None+ else str(+ getattr(self._external_update, "source_text", "")+ or ""+ )+ )await self._complete_surface_turn()if self._end_after_turn:# end_call was acknowledged; the model has now spoken@@ -5278,6 +5397,12 @@ class RealtimeVoiceSession:self._reset_output_state(reason="transport rebuild")self._drop_provider_output_until_new_response = Falseself._drop_provider_output_until_user_turn = False+ # The discarded generation died with its transport; the fresh session+ # owes the user nothing from before, and must not stay deaf to it.+ self._stale_generation_dropping = False+ self._stale_generation_dropping_since = 0.0+ self._stale_generation_transcript.clear()+ self._disarm_stale_generation_guard()# Input-item ids are scoped to the DEAD transport. A fresh provider# session may restart its numbering, and a collision here silently# swallows the next real utterance at the duplicate-item guard — the@@ -7451,6 +7576,173 @@ class RealtimeVoiceSession:return refreturn None+ # --- Stale-generation guard (BUG-143) --------------------------------++ def _arm_stale_generation_guard(self, reply: str) -> None:+ """Arm the guard as a provider-rendered delegate readback turn closes.++ Only a transport that creates responses on its own server VAD can+ answer a second boundary for the same request; a manual-response+ transport generates nothing Jarvis did not ask for, so the guard+ stays inert there (capability, never a provider name — AP-21).+ """+ if not bool(+ getattr(self._session, "creates_responses_automatically", False)+ ):+ return+ self._stale_generation_guard_armed_at = time.monotonic()+ self._stale_generation_guard_reply = str(reply or "")++ def _disarm_stale_generation_guard(self) -> None:+ self._stale_generation_guard_armed_at = 0.0+ self._stale_generation_guard_reply = ""++ def _stale_generation_guard_reason(self) -> str:+ """Why a generation beginning NOW would be discarded, or ``""``.++ Every ``""`` below is fresh evidence that whatever the provider is+ about to say was asked for: a turn is open (a transcript or a+ deliberate injection opened it), the microphone carried the user's+ voice after the readback ended, a server speech edge was confirmed,+ or the bounded window simply ran out. Each of them disarms the guard+ for good — it never survives into a later turn.+ """+ armed_at = self._stale_generation_guard_armed_at+ if not armed_at:+ return ""+ if (+ self._turn_id+ or self._external_update is not None+ or self._user_speech_active+ or self._last_voiced_input_monotonic > armed_at+ or time.monotonic() - armed_at > _STALE_GENERATION_WINDOW_S+ ):+ self._disarm_stale_generation_guard()+ return ""+ return (+ "a provider generation started after a delivered readback with no "+ "new user input"+ )++ def _stale_generation_drop_active(self) -> bool:+ """Whether a discarded generation is still being withheld.++ Bounded: past ``_STALE_GENERATION_DROP_MAX_S`` without the boundary+ that normally ends it, the drop is released with a warning — an+ unbounded withhold would keep every later reply, announcement and+ late result inaudible for the rest of the call.+ """+ if not self._stale_generation_dropping:+ return False+ held_for = time.monotonic() - self._stale_generation_dropping_since+ if held_for > _STALE_GENERATION_DROP_MAX_S:+ log.warning(+ "realtime[%s] the discarded stale generation sent no boundary "+ "for %.0fs; releasing the withhold so the call stays audible",+ self.session_id,+ held_for,+ )+ self._finish_stale_generation_drop()+ return False+ return True++ def _judge_stale_generation_event(self, event: Any) -> None:+ """Classify one provider output event against the armed guard.++ Called for every audio/transcript/tool event BEFORE its branch, so a+ generation that starts under the armed guard is marked as being+ discarded — and ``_output_withhold_reason`` plus the tool-call and+ handoff branches then keep every event of it out of the gate, the+ surface, the executor and the turn record until its own boundary+ arrives.+ """+ text = str(getattr(event, "text", "") or "") if (+ event.type == "output_transcript_delta"+ ) else ""+ if self._stale_generation_drop_active():+ if text:+ self._append_stale_generation_transcript(text)+ return+ if not self._stale_generation_guard_armed_at:+ return+ reason = self._stale_generation_guard_reason()+ if not reason:+ return+ self._stale_generation_dropping = True+ self._stale_generation_dropping_since = time.monotonic()+ self._stale_generations_dropped += 1+ self._stale_generation_transcript.clear()+ if text:+ self._append_stale_generation_transcript(text)+ log.warning(+ "realtime[%s] discarding a provider generation that started %.2fs "+ "after the delivered readback with no new user input — the "+ "provider answered a second boundary for the same request "+ "(delivered reply: %s)",+ self.session_id,+ time.monotonic() - self._stale_generation_guard_armed_at,+ safe_preview(self._stale_generation_guard_reply, max_chars=120),+ )++ def _append_stale_generation_transcript(self, text: str) -> None:+ kept = "".join(self._stale_generation_transcript)+ if len(kept) >= _STALE_GENERATION_TRANSCRIPT_MAX_CHARS:+ return+ self._stale_generation_transcript.append(+ text[: _STALE_GENERATION_TRANSCRIPT_MAX_CHARS - len(kept)]+ )++ def _finish_stale_generation_drop(self) -> None:+ """The discarded generation reached its boundary: log and stand down."""+ discarded = "".join(self._stale_generation_transcript).strip()+ heard = _normalize_for_repeat_match(discarded)+ delivered = _normalize_for_repeat_match(self._stale_generation_guard_reply)+ matched = bool(+ heard+ and delivered+ and (heard.startswith(delivered) or delivered.startswith(heard))+ )+ log.info(+ "realtime[%s] discarded stale provider generation ended "+ "(%s the delivered reply): %s",+ self.session_id,+ "re-rendered" if matched else "did not match",+ safe_preview(discarded, max_chars=160) or "<no transcript>",+ )+ self._stale_generation_dropping = False+ self._stale_generation_dropping_since = 0.0+ self._stale_generation_transcript.clear()+ self._disarm_stale_generation_guard()++ async def _reject_stale_generation_tool_call(self, event: Any) -> None:+ """Answer a stale generation's function call without executing it.++ The refusal text is for the model, never for the user: the rendering+ it provokes is still part of the discarded generation and stays+ withheld, and its boundary is what ends the drop.+ """+ if self._session is None or not self._session_takes_tool_results():+ return+ try:+ await self._session.send_tool_result(+ str(getattr(event, "call_id", "") or ""),+ str(getattr(event, "tool_name", "") or ""),+ {+ "success": False,+ "error": (+ "This request was already answered; the action was "+ "not executed. Wait for the user's next request."+ ),+ },+ )+ except Exception: # noqa: BLE001 — the drop ceiling still bounds this+ log.warning(+ "realtime[%s] could not answer a stale generation's tool call; "+ "the drop ceiling will release the withhold",+ self.session_id,+ exc_info=True,+ )+def _session_takes_tool_results(self) -> bool:"""Whether this transport can carry a tool result back to the model.@@ -7480,6 +7772,11 @@ class RealtimeVoiceSession:"""if self._drop_provider_output_until_new_response:return "awaiting a new response after a barge-in or delegation"+ if self._stale_generation_drop_active():+ return (+ "discarding a stale provider generation that started after a "+ "delivered readback"+ )if self._drop_provider_output_until_user_turn:return "awaiting the user's next turn after a surface fallback"if self._must_withhold_delegate_output():@@ -7992,6 +8289,9 @@ class RealtimeVoiceSession:or self._turn_idor self._turn_has_activity()or self._output_active+ # A discarded generation is still streaming from the provider;+ # a follow-up injected now would land inside it (BUG-143).+ or self._stale_generation_drop_active()or self._delegate_tasksor self._pending_tool_eventsor self._response_requested_for_turn@@ -10092,6 +10392,7 @@ class RealtimeVoiceSession:self._delegate_delivery_duplicates_suppressed),delegate_deliveries_detached=self._delegate_deliveries_detached,+ stale_generations_dropped=self._stale_generations_dropped,opening_responses_bounded=diag.get("opening_responses_bounded", 0),self_dialogue_rebuilds=diag.get("self_dialogue_rebuilds", 0),handoff_action_turns=self._handoff_action_turns,diff --git a/tests/unit/diagnostics/test_realtime_forensics.py b/tests/unit/diagnostics/test_realtime_forensics.pyindex 7b87248f6e..6ffaadb6db 100644--- a/tests/unit/diagnostics/test_realtime_forensics.py+++ b/tests/unit/diagnostics/test_realtime_forensics.py@@ -42,6 +42,7 @@ def test_every_failure_class_maps_to_its_finding() -> None:"ungrounded_responses_refused": 7,"self_dialogue_rebuilds": 1,"handoff_obligation_misses": 2,+ "stale_generations_dropped": 1,"mute_emergency_releases": 2,"max_loop_stall_ms": 15_000,"sender_pacing_resyncs": 1,@@ -61,6 +62,7 @@ def test_every_failure_class_maps_to_its_finding() -> None:"refusal-storm","self-dialogue","handoff-obligation-miss",+ "stale-generation-dropped","mute-emergency-release","event-loop-stall","sender-behind-wall-clock",diff --git a/tests/unit/realtime/test_stale_generation_after_readback.py b/tests/unit/realtime/test_stale_generation_after_readback.pynew file mode 100644index 0000000000..4a86153706--- /dev/null+++ b/tests/unit/realtime/test_stale_generation_after_readback.py@@ -0,0 +1,614 @@+"""Stale-generation guard after a delivered readback (BUG-143).++Live forensic 2026-08-18 14:25 (session 7b20e182, turns 4/5 and 6/7): on a+server-VAD transport one Jarvis turn carried TWO turn-ending inputs on the+server — the injected trusted result and the end of the user's trailing+speech — and the provider answered both, back to back. Every answer after+the injected result re-rendered that same result, so the user heard "Ich+habe work geöffnet: T eins." and then "Ich habe work geöffnet: T1.", and+the recorder wrote a phantom turn with no user text for the repeat.++The session cannot cancel that second generation (Gemini has no response+cancel). It refuses to play it instead: a generation that begins right after+a provider-rendered readback with NO new user input is discarded whole,+never opened as a turn. Fresh user input, local microphone voice, a+deliberate injection, or the bounded window each let the provider speak+again — the guard must never eat a genuine later answer.+"""++from __future__ import annotations++import asyncio+import time+from types import SimpleNamespace+from typing import Any++import pytest++import jarvis.realtime.session as session_module+from jarvis.brain.turn_planner import TurnPath, TurnPlan, TurnReason+from jarvis.core.protocols import AudioChunk+from jarvis.realtime.protocol import RealtimeEvent+from jarvis.realtime.session import RealtimeVoiceSession++REPLY = "I opened work: T1."+FOLLOW_UP_QUESTION = "And what time is it?"+FOLLOW_UP_ANSWER = "It is half past two."+PCM = AudioChunk(pcm=b"\x11\x00" * 480, sample_rate=24_000, timestamp_ns=0)+++class _DelegatingBrain:+ """Routes the opening request to the orchestrator; a follow-up is native."""++ conversation_language = "en"++ def __init__(self) -> None:+ self.calls: list[str] = []++ def plan_turn(self, text: str) -> TurnPlan:+ if text.strip().lower().startswith("open"):+ return TurnPlan(+ path=TurnPath.ORCHESTRATOR,+ reasons=frozenset({TurnReason.LOCAL_STATE}),+ requires_evidence=True,+ )+ return TurnPlan(path=TurnPath.NATIVE_REALTIME)++ async def __call__(self, text: str) -> str:+ self.calls.append(text)+ return REPLY+++class _FakeBus:+ def __init__(self) -> None:+ self.events: list[Any] = []++ async def publish(self, event: Any) -> None:+ self.events.append(event)++ def subscribe(self, *_args: Any, **_kwargs: Any) -> None:+ return None++ def unsubscribe(self, *_args: Any, **_kwargs: Any) -> None:+ return None+++class _ServerVadWire:+ """Gemini-shaped wire: automatic responses, no response cancel.++ ``script`` decides what follows the rendered readback of the delegate+ result:++ * ``second-generation`` — a second generation of the same reply starts+ immediately after the readback boundary, with no user input at all+ (the live failure).+ * ``after-user-input`` — the user asks a follow-up first; the provider's+ answer to it must play.+ * ``after-local-voice`` — no transcript yet, but the microphone carried+ the user's voice after the readback ended (a slow input transcription);+ the answer must play.+ * ``after-window`` — the second generation starts only after the guard's+ window ran out; it plays (fail-open bound).+ * ``late-result`` — Jarvis itself injects a late action result after the+ readback; the provider's rendering of THAT text must play.+ * ``stale-tool-call`` — the second generation asks for a tool (here the+ hang-up) instead of speaking; it must be refused, not executed.+ * ``user-input-mid-drop`` — the user's follow-up transcript lands while+ the stale generation is still streaming, BEFORE its boundary; that+ boundary must not close the follow-up's turn.+ """++ session_id = "server-vad-wire"+ supports_tool_updates = False+ creates_responses_automatically = True+ isolates_response_generations = True++ def __init__(self, *, script: str) -> None:+ self._script = script+ self.text_inputs: list[str] = []+ self.text_sent = asyncio.Event()+ self.second_text_sent = asyncio.Event()+ self.readback_done = asyncio.Event()+ self.before_second_generation: Any = None+ self.tool_results: list[tuple[Any, ...]] = []+ self.tool_result_sent = asyncio.Event()+ self.interrupts = 0+ self.closed = asyncio.Event()++ async def _readback(self, text: str):+ yield RealtimeEvent(type="output_transcript_delta", text=text)+ yield RealtimeEvent(type="audio_delta", audio=PCM)+ yield RealtimeEvent(type="turn_complete")++ async def receive(self):+ yield RealtimeEvent(+ type="input_transcript",+ text="Open a terminal for me.",+ is_final=True,+ item_id="request",+ )+ await self.text_sent.wait()+ async for event in self._readback(REPLY):+ yield event+ self.readback_done.set()+ # Let the session finish the readback turn before the next generation.+ await asyncio.sleep(0.02)+ if self.before_second_generation is not None:+ await self.before_second_generation()+ if self._script == "second-generation":+ async for event in self._readback(REPLY):+ yield event+ elif self._script == "after-user-input":+ yield RealtimeEvent(+ type="input_transcript",+ text=FOLLOW_UP_QUESTION,+ is_final=True,+ item_id="follow-up",+ )+ async for event in self._readback(FOLLOW_UP_ANSWER):+ yield event+ elif self._script in {"after-local-voice", "after-window"}:+ async for event in self._readback(FOLLOW_UP_ANSWER):+ yield event+ elif self._script == "late-result":+ # The injected late result prompt arrives as a second text input;+ # render it like Gemini would.+ await self.second_text_sent.wait()+ async for event in self._readback("Also, the download finished."):+ yield event+ elif self._script == "stale-tool-call":+ # The stale generation calls a function instead of speaking. A+ # function call ends the generation without a boundary; the model+ # renders whatever result it gets in a follow-up generation.+ yield RealtimeEvent(type="tool_call", tool_name="end_call", call_id="c1")+ await self.tool_result_sent.wait()+ async for event in self._readback("Alright, goodbye."):+ yield event+ elif self._script == "user-input-mid-drop":+ yield RealtimeEvent(type="output_transcript_delta", text=REPLY)+ yield RealtimeEvent(type="audio_delta", audio=PCM)+ yield RealtimeEvent(+ type="input_transcript",+ text=FOLLOW_UP_QUESTION,+ is_final=True,+ item_id="follow-up",+ )+ # The stale generation's own boundary arrives only now.+ yield RealtimeEvent(type="turn_complete")+ async for event in self._readback(FOLLOW_UP_ANSWER):+ yield event+ await self.closed.wait()++ async def send_audio(self, _chunk: Any) -> None:+ return None++ async def update_session(self, **_kwargs: Any) -> None:+ return None++ async def request_response(self, **_kwargs: Any) -> None:+ return None++ async def send_text(self, text: str) -> None:+ self.text_inputs.append(text)+ self.text_sent.set()+ if len(self.text_inputs) >= 2:+ self.second_text_sent.set()++ async def truncate(self, _audio_end_ms: int) -> None:+ return None++ async def interrupt(self) -> None:+ # Gemini has no response cancel: this is a no-op on the live wire too.+ self.interrupts += 1++ async def send_tool_result(self, *args: Any) -> None:+ self.tool_results.append(args)+ self.tool_result_sent.set()++ async def close(self) -> None:+ self.closed.set()+++class _ServerVadProvider:+ name = "server-vad"+ supports_realtime = True+ input_sample_rate = 16_000+ output_sample_rate = 24_000++ def __init__(self, *, script: str) -> None:+ self.session = _ServerVadWire(script=script)++ async def can_open_duplex_session(self) -> bool:+ return True++ async def open_session(self, _config: Any) -> _ServerVadWire:+ return self.session+++def _config() -> SimpleNamespace:+ return SimpleNamespace(+ brain=SimpleNamespace(reply_language="en", providers={}),+ stt=SimpleNamespace(language="auto"),+ voice=SimpleNamespace(mode="realtime", realtime_tool_mode="delegate"),+ latency=SimpleNamespace(enabled=False),+ )+++def _shorten_delegate_waits(monkeypatch: pytest.MonkeyPatch) -> None:+ monkeypatch.setattr(session_module, "_DELEGATE_INPUT_BOUNDARY_WAIT_S", 0.05)+ monkeypatch.setattr(session_module, "_DELEGATE_INPUT_BOUNDARY_POLL_S", 0.02)+ monkeypatch.setattr(session_module, "_DELEGATE_NATIVE_BOUNDARY_WAIT_S", 0.05)+ monkeypatch.setattr(session_module, "_DELEGATE_READBACK_WAIT_S", 0.5)+ monkeypatch.setattr(session_module, "_DELEGATE_READBACK_POLL_S", 0.02)+++async def _run(+ provider: _ServerVadProvider,+ *,+ settle_s: float = 0.4,+) -> tuple[RealtimeVoiceSession, list[dict[str, Any]], list[bytes], _FakeBus]:+ brain = _DelegatingBrain()+ messages: list[dict[str, Any]] = []+ binaries: list[bytes] = []+ bus = _FakeBus()+ session = RealtimeVoiceSession(+ session_id="stale-generation-guard",+ send_binary=lambda data: binaries.append(data) or asyncio.sleep(0),+ send_json=lambda message: messages.append(message) or asyncio.sleep(0),+ provider=provider,+ config=_config(),+ bus=bus,+ browser_sample_rate=16_000,+ surface="desktop",+ brain=brain,+ )+ await session.handle_control({"type": "audio_start", "sample_rate": 16_000})+ try:+ await asyncio.wait_for(provider.session.text_sent.wait(), timeout=10.0)+ await asyncio.wait_for(provider.session.readback_done.wait(), timeout=10.0)+ # The scripted continuation streams while we wait here.+ await asyncio.sleep(settle_s)+ finally:+ await session.end(reason="test")+ return session, messages, binaries, bus+++def _assistant_texts(messages: list[dict[str, Any]]) -> list[str]:+ return [+ str(m.get("text", ""))+ for m in messages+ if m.get("type") == "transcript" and m.get("role") == "assistant"+ ]+++def _turn_starts(bus: _FakeBus) -> int:+ return sum(1 for e in bus.events if type(e).__name__ == "VoiceTurnStarted")+++# ---------------------------------------------------------------------------+# The live failure+# ---------------------------------------------------------------------------+++async def test_second_generation_after_readback_is_discarded_whole(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ """Readback plays once; the provider's unprompted repeat never reaches+ the surface, the recorder, or the turn record."""+ _shorten_delegate_waits(monkeypatch)+ provider = _ServerVadProvider(script="second-generation")++ session, messages, binaries, bus = await _run(provider)++ assert provider.session.text_inputs, "the trusted result was never injected"+ assert REPLY in provider.session.text_inputs[0]+ # Exactly one rendering reached the speaker and the caption.+ assert len(binaries) == 1+ assert _assistant_texts(messages) == [REPLY]+ # One surface turn boundary, one recorded turn — no phantom turn.+ assert sum(1 for m in messages if m.get("type") == "turn_complete") == 1+ assert _turn_starts(bus) == 1+ assert session._stale_generations_dropped == 1+ # The guard stood down with the discarded generation's own boundary.+ assert session._stale_generation_dropping is False+ assert session._stale_generation_guard_armed_at == 0.0+++# ---------------------------------------------------------------------------+# Everything that must keep playing+# ---------------------------------------------------------------------------+++async def test_answer_after_new_user_input_plays(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ _shorten_delegate_waits(monkeypatch)+ provider = _ServerVadProvider(script="after-user-input")++ session, messages, binaries, bus = await _run(provider)++ assert len(binaries) == 2+ assert _assistant_texts(messages) == [REPLY, FOLLOW_UP_ANSWER]+ assert _turn_starts(bus) == 2+ assert session._stale_generations_dropped == 0+++async def test_answer_after_local_microphone_voice_plays(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ """A slow input transcription must not cost the user the answer: the+ microphone already carried their voice after the readback ended."""+ _shorten_delegate_waits(monkeypatch)+ provider = _ServerVadProvider(script="after-local-voice")+ holder: dict[str, RealtimeVoiceSession] = {}++ async def _user_speaks() -> None:+ # What handle_audio_frame stamps for a loud microphone frame.+ holder["session"]._last_voiced_input_monotonic = time.monotonic()++ provider.session.before_second_generation = _user_speaks++ brain = _DelegatingBrain()+ messages: list[dict[str, Any]] = []+ binaries: list[bytes] = []+ session = RealtimeVoiceSession(+ session_id="stale-generation-guard-voice",+ send_binary=lambda data: binaries.append(data) or asyncio.sleep(0),+ send_json=lambda message: messages.append(message) or asyncio.sleep(0),+ provider=provider,+ config=_config(),+ bus=None,+ browser_sample_rate=16_000,+ surface="desktop",+ brain=brain,+ )+ holder["session"] = session+ await session.handle_control({"type": "audio_start", "sample_rate": 16_000})+ try:+ await asyncio.wait_for(provider.session.readback_done.wait(), timeout=10.0)+ await asyncio.sleep(0.4)+ finally:+ await session.end(reason="test")++ assert len(binaries) == 2+ assert _assistant_texts(messages) == [REPLY, FOLLOW_UP_ANSWER]+ assert session._stale_generations_dropped == 0+++async def test_generation_after_the_window_plays(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ """The guard is bounded: past the window the provider speaks freely."""+ _shorten_delegate_waits(monkeypatch)+ monkeypatch.setattr(session_module, "_STALE_GENERATION_WINDOW_S", 0.05)+ provider = _ServerVadProvider(script="after-window")++ async def _wait_past_window() -> None:+ await asyncio.sleep(0.15)++ provider.session.before_second_generation = _wait_past_window++ session, messages, binaries, _bus = await _run(provider, settle_s=0.6)++ assert len(binaries) == 2+ assert _assistant_texts(messages) == [REPLY, FOLLOW_UP_ANSWER]+ assert session._stale_generations_dropped == 0+++async def test_late_result_injected_by_jarvis_plays(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ """A deliberate injection opens its own turn first, so its rendering is+ never mistaken for a stale generation."""+ _shorten_delegate_waits(monkeypatch)+ provider = _ServerVadProvider(script="late-result")+ holder: dict[str, RealtimeVoiceSession] = {}++ async def _inject_late_result() -> None:+ session = holder["session"]+ session._late_delegate_results.append(+ session_module._LateDelegateResult(+ text="Also, the download finished.",+ success=True,+ language="en",+ delivery_id="late-1",+ )+ )+ await session._flush_late_delegate_results()++ provider.session.before_second_generation = _inject_late_result++ brain = _DelegatingBrain()+ messages: list[dict[str, Any]] = []+ binaries: list[bytes] = []+ session = RealtimeVoiceSession(+ session_id="stale-generation-guard-late",+ send_binary=lambda data: binaries.append(data) or asyncio.sleep(0),+ send_json=lambda message: messages.append(message) or asyncio.sleep(0),+ provider=provider,+ config=_config(),+ bus=None,+ browser_sample_rate=16_000,+ surface="desktop",+ brain=brain,+ )+ holder["session"] = session+ await session.handle_control({"type": "audio_start", "sample_rate": 16_000})+ try:+ await asyncio.wait_for(provider.session.readback_done.wait(), timeout=10.0)+ await asyncio.sleep(0.6)+ finally:+ await session.end(reason="test")++ assert len(provider.session.text_inputs) == 2+ assert len(binaries) == 2+ assert _assistant_texts(messages) == [REPLY, "Also, the download finished."]+ assert session._stale_generations_dropped == 0+++async def test_tool_call_from_stale_generation_is_refused_not_executed(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ """A phantom generation asking for an action — here the hang-up — gets an+ honest refusal result (so the model can close its generation) and nothing+ else: no turn, no execution, no audible rendering of the refusal."""+ _shorten_delegate_waits(monkeypatch)+ provider = _ServerVadProvider(script="stale-tool-call")++ session, messages, binaries, bus = await _run(provider, settle_s=0.6)++ assert len(provider.session.tool_results) == 1+ call_id, name, result = provider.session.tool_results[0]+ assert (call_id, name) == ("c1", "end_call")+ assert result["success"] is False+ # The hang-up never fired and no phantom turn exists.+ assert session._end_after_turn is False+ assert _turn_starts(bus) == 1+ # The refusal's rendering stayed inside the discarded generation.+ assert len(binaries) == 1+ assert _assistant_texts(messages) == [REPLY]+ assert session._stale_generations_dropped == 1+ assert session._stale_generation_dropping is False+++async def test_follow_up_landing_mid_drop_keeps_its_own_turn(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ """The stale generation's late boundary must not close the follow-up turn+ a real transcript opened during the drop: that turn waits for its own+ answer, which then plays; the request is never re-routed as an empty+ turn."""+ _shorten_delegate_waits(monkeypatch)+ provider = _ServerVadProvider(script="user-input-mid-drop")++ brain = _DelegatingBrain()+ messages: list[dict[str, Any]] = []+ binaries: list[bytes] = []+ bus = _FakeBus()+ session = RealtimeVoiceSession(+ session_id="stale-generation-guard-mid-drop",+ send_binary=lambda data: binaries.append(data) or asyncio.sleep(0),+ send_json=lambda message: messages.append(message) or asyncio.sleep(0),+ provider=provider,+ config=_config(),+ bus=bus,+ browser_sample_rate=16_000,+ surface="desktop",+ brain=brain,+ )+ await session.handle_control({"type": "audio_start", "sample_rate": 16_000})+ try:+ await asyncio.wait_for(provider.session.readback_done.wait(), timeout=10.0)+ await asyncio.sleep(0.6)+ finally:+ await session.end(reason="test")++ # The opening request was the only Brain turn: the follow-up was answered+ # natively, never recovered as an "empty" turn.+ assert brain.calls == ["Open a terminal for me."]+ assert _assistant_texts(messages) == [REPLY, FOLLOW_UP_ANSWER]+ assert len(binaries) == 2+ assert _turn_starts(bus) == 2+ assert session._stale_generations_dropped == 1+++# ---------------------------------------------------------------------------+# Capability gate and predicate+# ---------------------------------------------------------------------------+++def test_guard_never_arms_on_a_manual_response_transport() -> None:+ session = RealtimeVoiceSession(+ session_id="manual",+ send_binary=lambda _data: asyncio.sleep(0),+ send_json=lambda _message: asyncio.sleep(0),+ provider=_ServerVadProvider(script="second-generation"),+ config=_config(),+ bus=None,+ browser_sample_rate=16_000,+ surface="desktop",+ brain=_DelegatingBrain(),+ )+ session._session = SimpleNamespace(creates_responses_automatically=False)+ session._arm_stale_generation_guard(REPLY)+ assert session._stale_generation_guard_armed_at == 0.0+ session._session = SimpleNamespace(creates_responses_automatically=True)+ session._arm_stale_generation_guard(REPLY)+ assert session._stale_generation_guard_armed_at > 0.0+ assert session._stale_generation_guard_reply == REPLY+++def test_guard_reason_disarms_on_every_kind_of_fresh_evidence() -> None:+ session = RealtimeVoiceSession(+ session_id="predicate",+ send_binary=lambda _data: asyncio.sleep(0),+ send_json=lambda _message: asyncio.sleep(0),+ provider=_ServerVadProvider(script="second-generation"),+ config=_config(),+ bus=None,+ browser_sample_rate=16_000,+ surface="desktop",+ brain=_DelegatingBrain(),+ )+ session._session = SimpleNamespace(creates_responses_automatically=True)++ def _reason_and_armed() -> tuple[str, float]:+ return (+ session._stale_generation_guard_reason(),+ session._stale_generation_guard_armed_at,+ )++ session._arm_stale_generation_guard(REPLY)+ assert session._stale_generation_guard_reason()+ assert session._stale_generation_guard_armed_at > 0.0++ # An open turn (transcript or deliberate injection) owns the output.+ session._turn_id = "turn"+ assert _reason_and_armed() == ("", 0.0)+ session._turn_id = ""++ # A confirmed server speech edge.+ session._arm_stale_generation_guard(REPLY)+ session._user_speech_active = True+ assert _reason_and_armed() == ("", 0.0)+ session._user_speech_active = False++ # Local microphone voice after the readback ended.+ session._arm_stale_generation_guard(REPLY)+ session._last_voiced_input_monotonic = time.monotonic() + 1.0+ assert _reason_and_armed() == ("", 0.0)+ session._last_voiced_input_monotonic = 0.0++ # The bounded window.+ session._arm_stale_generation_guard(REPLY)+ session._stale_generation_guard_armed_at -= session_module._STALE_GENERATION_WINDOW_S + 1.0+ assert _reason_and_armed() == ("", 0.0)+++def test_drop_without_a_boundary_is_released_at_the_ceiling() -> None:+ session = RealtimeVoiceSession(+ session_id="ceiling",+ send_binary=lambda _data: asyncio.sleep(0),+ send_json=lambda _message: asyncio.sleep(0),+ provider=_ServerVadProvider(script="second-generation"),+ config=_config(),+ bus=None,+ browser_sample_rate=16_000,+ surface="desktop",+ brain=_DelegatingBrain(),+ )+ session._stale_generation_dropping = True+ session._stale_generation_dropping_since = time.monotonic()+ assert session._stale_generation_drop_active() is True+ assert session._must_withhold_provider_output() is True+ session._stale_generation_dropping_since -= session_module._STALE_GENERATION_DROP_MAX_S + 1.0+ assert session._stale_generation_drop_active() is False+ assert session._stale_generation_dropping is False+ assert session._must_withhold_provider_output() is False