Skip to content

The agent that grows with you

fix(tui_gateway): Bot Chat streaming never shows or speaks a closing silence marker

Teknium3e7461cd4bdd
diff --git a/gateway/response_filters.py b/gateway/response_filters.py
index 0b8b993faca..27428148843 100644
--- a/gateway/response_filters.py
+++ b/gateway/response_filters.py
@@ -112,6 +112,39 @@ def ends_with_partial_silence_marker(text: Any) -> bool:
return bool(head.strip()) and is_partial_silence_marker(tail)
+class SilenceMarkerStreamHold:
+ """Delta gate for a live reply stream that may be, or end in, a silence marker.
+
+ :meth:`feed` returns the part of the reply that is safe to show (or speak) now: nothing
+ while the whole reply could still become a bare marker, everything but the last line while
+ that line could still become a closing one. :meth:`finish` releases what the completed reply
+ delivers, so a held line that never became a marker (``"NO problem"``) still arrives in full.
+ """
+
+ def __init__(self) -> None:
+ self._buf = ""
+ self._sent = 0
+
+ def feed(self, delta: str) -> str:
+ buf = self._buf = self._buf + delta
+ if is_partial_silence_marker(buf):
+ return ""
+ if ends_with_partial_silence_marker(buf):
+ return self._release(len(_split_trailing_line(buf)[0]))
+ return self._release(len(buf))
+
+ def finish(self) -> str:
+ if is_intentional_silence_response(self._buf):
+ return ""
+ return self._release(len(strip_trailing_silence_marker(self._buf)))
+
+ def _release(self, upto: int) -> str:
+ if upto <= self._sent:
+ return ""
+ out, self._sent = self._buf[self._sent:upto], upto
+ return out
+
+
def is_autonomous_silence_response(response: Any) -> bool:
"""Loose silence matcher for autonomous lanes (cron, webhook).
diff --git a/tests/tui_gateway/test_bot_mode_silence_delivery.py b/tests/tui_gateway/test_bot_mode_silence_delivery.py
index a6ae636609b..f48c6f4a0b0 100644
--- a/tests/tui_gateway/test_bot_mode_silence_delivery.py
+++ b/tests/tui_gateway/test_bot_mode_silence_delivery.py
@@ -91,3 +91,39 @@ def test_live_bot_chat_stream_holds_back_partial_silence_marker(monkeypatch):
assert _run("NO_REPLY", ["NO_", "REPLY"]) == ([], "")
assert _run("NO way, here is the answer.", ["NO", " way,", " here is the answer."]) == (
["NO way,", " here is the answer."], "NO way, here is the answer.")
+
+
+def test_live_bot_chat_stream_never_shows_or_speaks_a_closing_silence_marker(monkeypatch):
+ """A marker closing a real reply is held line-wise: no delta and no TTS chunk carries it,
+ while a last line that only starts like one ("NO problem", "NOTE: done", "NO") arrives whole."""
+ events = []
+ monkeypatch.setattr(srv, "_emit", lambda event, _sid, payload=None: events.append((event, payload)))
+ monkeypatch.setattr(srv, "_load_interim_assistant_messages", lambda: False)
+ monkeypatch.setattr(srv, "_start_usage_ticker", lambda _sid, _agent: (SimpleNamespace(set=lambda: None), SimpleNamespace(join=lambda: None)))
+
+ def _run(chunks):
+ events.clear()
+ spoken = []
+
+ def run_conversation(_message, **kwargs):
+ for chunk in chunks:
+ kwargs["stream_callback"](chunk)
+ return {"final_response": "".join(chunks)}
+
+ agent = SimpleNamespace(_session_title_hint="Bot Chat", run_conversation=run_conversation)
+ session = {"pending_title": None, "session_key": "k", "history_lock": contextlib.nullcontext(), "agent": agent}
+ st = srv._TurnRun(agent=agent, one_turn_restore=None, terminal_callback=None, receipt_committed=True)
+ st.tts_queue = SimpleNamespace(put=spoken.append)
+ srv._invoke_agent("sid", session, st, "ping", "ping", None, [], None, None)
+ return [p["text"] for e, p in events if e == "message.delta"], spoken
+
+ for chunks in (["Paris.", "\n\nNO", "_RE", "PLY"], ["Paris.\n", "\n[SIL", "ENT]\n"]):
+ deltas, spoken = _run(chunks)
+ assert "".join(deltas).strip() == "Paris."
+ assert "".join(spoken).strip() == "Paris."
+ assert not any(c in "".join(deltas + spoken) for c in ("NO", "SIL", "["))
+
+ for tail in ("NO problem", "NOTE: done", "NO"):
+ chunks = ["Paris.", "\n\nNO", tail[2:]] if len(tail) > 2 else ["Paris.", "\n\nNO"]
+ deltas, spoken = _run(chunks)
+ assert "".join(deltas) == "".join(spoken) == f"Paris.\n\n{tail}"
diff --git a/tui_gateway/prompt_turn.py b/tui_gateway/prompt_turn.py
index c6f7a576c2f..86cce2b2c32 100644
--- a/tui_gateway/prompt_turn.py
+++ b/tui_gateway/prompt_turn.py
@@ -747,21 +747,23 @@ def _invoke_agent(
``text`` is the turn's raw submit, matched against the row staged by prompt.submit."""
agent = st.agent
st.compression_count = getattr(getattr(agent, "context_compressor", None), "compression_count", None)
- # Bot Chat mirrors gateway.stream_consumer: deltas are withheld while the streamed buffer
- # could still resolve to a silence marker ("NO"->"NO_REPLY"), so a bare marker is never
- # shown and then retracted (the client keeps streamed text when message.complete is "").
- hold = {"buf": "", "held": ""} if _is_bot_mode_session(session) else None
+ # Bot Chat mirrors gateway.stream_consumer: deltas are withheld while the reply could still
+ # be a bare silence marker ("NO"->"NO_REPLY") or its last line a closing one
+ # ("Paris.\n\nNO"), so a marker is never shown or spoken (the client keeps streamed text
+ # when message.complete is "").
+ from gateway.response_filters import SilenceMarkerStreamHold
+ hold = SilenceMarkerStreamHold() if _is_bot_mode_session(session) else None
def _stream(delta):
if getattr(agent, "_mute_notification_reply", False):
return
if hold is not None and isinstance(delta, str):
- from gateway.response_filters import is_partial_silence_marker
- hold["buf"] += delta
- if is_partial_silence_marker(hold["buf"]):
- hold["held"] += delta
+ delta = hold.feed(delta)
+ if not delta:
return
- delta, hold["held"] = hold["held"] + delta, ""
+ _emit_delta(delta)
+
+ def _emit_delta(delta):
with session["history_lock"]:
_append_inflight_delta(session, delta)
payload = {"text": delta}
@@ -813,6 +815,9 @@ def _invoke_agent(
from agent.notification_presentation import notification_turn, event_presentation_muted
with notification_turn(agent, muted=event_presentation_muted("message.delta", sid), session_id=sid):
st.result = agent.run_conversation(run_message, **st.run_kwargs)
+ # A held last line that never became a marker is part of the reply: deliver it.
+ if hold is not None and (rest := hold.finish()) and not getattr(agent, "_mute_notification_reply", False):
+ _emit_delta(rest)
finally:
# Stop AND join before anything emits: a tick surviving past message.complete would
# roll the client's usage back to a stale snapshot (unbounded join: same worst case).