Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 43 additions & 15 deletions gateway/stream_consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1119,6 +1119,14 @@ async def run(self) -> None:
return

if commentary_text is not None:
# Same #8124 hazard as the segment-break reset below: this
# reset wipes _accumulated, so any text the send above
# failed to make visible has to be delivered first. (The
# second reset, after _send_commentary, needs no flush —
# _accumulated is already empty by then.)
await self._flush_undelivered_tail_before_reset(
current_update_visible
)
self._reset_segment_state()
await self._send_commentary(commentary_text)
self._last_edit_time = time.monotonic()
Expand All @@ -1139,21 +1147,18 @@ async def run(self) -> None:
# a real string like "msg_1", not "__no_edit__", so that case
# still resets and creates a fresh segment as intended.)
if got_segment_break:
# If the segment-break edit failed to deliver the
# accumulated content (flood control that has not yet
# promoted to fallback mode, or fallback mode itself),
# _accumulated still holds pre-boundary text the user
# never saw. Flush that tail as a continuation message
# before the reset below wipes _accumulated — otherwise
# text generated before the tool boundary is silently
# dropped (issue #8124).
if (
self._accumulated
and not current_update_visible
and self._message_id
and self._message_id != "__no_edit__"
):
await self._flush_segment_tail_on_edit_failure()
# If the send that should have shown this segment did not
# deliver — a failed edit under flood control that has not
# yet promoted to fallback mode, fallback mode itself, or a
# failed *first* send after the previous boundary cleared
# _message_id — _accumulated still holds pre-boundary text
# the user never saw. Flush that tail as a continuation
# message before the reset below wipes _accumulated —
# otherwise text generated before the tool boundary is
# silently dropped (issue #8124).
await self._flush_undelivered_tail_before_reset(
current_update_visible
)
self._reset_segment_state(preserve_no_edit=True)

# Flush barrier satisfied: the buffered segment (if any) has now
Expand Down Expand Up @@ -1749,6 +1754,29 @@ async def _send_draft_frame(self, text: str) -> bool:
self._last_sent_text = text
return True

async def _flush_undelivered_tail_before_reset(
self, current_update_visible: bool
) -> None:
"""Flush pre-boundary text the user never saw, before a segment reset.

``_reset_segment_state`` wipes ``_accumulated``/``_stream_ledger``/
``_last_sent_text``. When the send that should have shown the current
segment never became visible, that buffer holds the only copy of the
text, so it must be delivered first (issue #8124).

Deliberately does NOT test ``_message_id`` for truthiness: a *first*
send that fails returns from ``_send_or_edit`` without ever assigning
it, leaving it ``None`` from the previous boundary's reset — which is
precisely the case that loses text. The ``__no_edit__`` sentinel is
still excluded: the matching reset preserves state for
``_send_fallback_final``, so flushing there would double-send.
"""
if not self._accumulated or current_update_visible:
return
if self._message_id == "__no_edit__":
return
await self._flush_segment_tail_on_edit_failure()

async def _flush_segment_tail_on_edit_failure(self) -> None:
"""Deliver un-sent tail content before a segment-break reset.

Expand Down
180 changes: 180 additions & 0 deletions tests/gateway/test_stream_consumer_first_send_tail.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
"""A failed *first* send must not silently delete the pre-boundary segment.

Sibling of the #8124 guard. ``_reset_segment_state`` clears ``_message_id``
at every tool boundary, so the next text segment re-enters the first-send
branch of ``_send_or_edit``. That branch returns without ever assigning
``_message_id`` when the send fails, which is exactly the case the original
recovery flush excluded (it required ``self._message_id`` to be truthy). The
result: the paragraph written between two tool calls is buffered, never shown,
and then wiped by the reset — the user sees the reply jump from one tool
bubble to the next with the explanation missing.

The fakes below fail only the *streaming-preview* send path, which the
consumer marks with ``expect_edits`` in its metadata (see
``_metadata_for_send``). Plain sends — the ones the tail flush and the
commentary use — still succeed. That mirrors a real platform split: Telegram
keeps editable previews on the legacy send path, so a preview render that the
platform rejects (or rate-limits) can fail for a whole turn while ordinary
messages go through.

Assertions are on delivered payloads, never on internal attributes.
"""

import asyncio
from types import SimpleNamespace
from unittest.mock import AsyncMock, MagicMock

import pytest

from gateway.stream_consumer import GatewayStreamConsumer, StreamConsumerConfig

_SEGMENT_ONE = "Here's what I found in the config, let me check the processes too."
_SEGMENT_TWO = "The gateway is running under systemd."
_COMMENTARY = "Using the shell tool..."


def _is_preview_send(kwargs) -> bool:
"""True for sends the consumer routes through the editable-preview path."""
metadata = kwargs.get("metadata")
return isinstance(metadata, dict) and bool(metadata.get("expect_edits"))


def _preview_hostile_adapter():
"""Adapter whose editable-preview sends all fail; plain sends succeed.

``adapter.delivered`` collects only the payloads the platform accepted --
a rejected send is an attempt, not something the user saw, so assertions
must not read it out of ``send.call_args_list``.
"""
adapter = MagicMock()
adapter.delivered = []

async def _send(**kwargs):
if _is_preview_send(kwargs):
return SimpleNamespace(success=False, error="preview send rejected")
adapter.delivered.append(kwargs["content"])
return SimpleNamespace(
success=True, message_id="msg_%d" % len(adapter.delivered),
)

adapter.send = AsyncMock(side_effect=_send)
adapter.edit_message = AsyncMock(return_value=SimpleNamespace(success=True))
adapter.MAX_MESSAGE_LENGTH = 4096
return adapter


def _delivered_count(adapter, needle):
return sum(1 for payload in adapter.delivered if needle in payload)


def _consumer(adapter):
config = StreamConsumerConfig(
edit_interval=0.01, buffer_threshold=5, cursor=" ▉",
)
return GatewayStreamConsumer(adapter, "chat_123", config)


@pytest.mark.asyncio
async def test_failed_first_send_tail_survives_tool_boundary():
"""The segment-break reset must not discard a never-delivered buffer.

Every preview send fails here, so ``_message_id`` is still ``None`` when
the tool boundary arrives. The old guard required a truthy
``_message_id``, so it skipped the flush and the reset wiped the only copy
of the text.
"""
adapter = _preview_hostile_adapter()
consumer = _consumer(adapter)

consumer.on_delta(_SEGMENT_ONE)
task = asyncio.create_task(consumer.run())
await asyncio.sleep(0.08)
consumer.on_delta(None) # tool boundary
await asyncio.sleep(0.08)
consumer.on_delta(_SEGMENT_TWO)
consumer.finish()
await task

assert _delivered_count(adapter, "let me check the processes too") == 1, (
"pre-boundary segment was dropped by the reset: "
"delivered=%r" % (adapter.delivered,)
)


@pytest.mark.asyncio
async def test_failed_first_send_tail_survives_commentary_reset():
"""The commentary reset had no guard at all, so it dropped the same buffer.

``_send_commentary`` is preceded by an unconditional
``_reset_segment_state()``; if the preview send for the current segment
never became visible, that reset destroys it and the user sees only the
interim status line.
"""
adapter = _preview_hostile_adapter()
consumer = _consumer(adapter)

consumer.on_delta(_SEGMENT_ONE)
task = asyncio.create_task(consumer.run())
await asyncio.sleep(0.08)
consumer.on_commentary(_COMMENTARY)
await asyncio.sleep(0.08)
consumer.on_delta(_SEGMENT_TWO)
consumer.finish()
await task

payloads = adapter.delivered
assert _delivered_count(adapter, "let me check the processes too") == 1, (
"text buffered before the commentary reset was dropped: "
"delivered=%r" % (payloads,)
)
# The commentary itself must still be delivered, and after the prose it
# was interrupting.
assert _COMMENTARY in payloads
prose_at = next(
i for i, p in enumerate(payloads) if "let me check the processes too" in p
)
assert prose_at < payloads.index(_COMMENTARY)


@pytest.mark.asyncio
async def test_no_edit_sentinel_does_not_double_send():
"""The ``__no_edit__`` exclusion must survive the widened guard.

When a platform accepts a message but returns no editable id, the
consumer parks ``_message_id`` on the ``__no_edit__`` sentinel and the
matching reset preserves the segment so ``_send_fallback_final`` can
deliver the whole continuation once. Flushing there would re-send text
the user has already seen, so dropping the ``_message_id`` truthiness
term must not drop this exclusion too.
"""
adapter = MagicMock()
adapter.delivered = []

async def _send(**kwargs):
# Accepted, but with no editable id -- the __no_edit__ trigger.
adapter.delivered.append(kwargs["content"])
return SimpleNamespace(success=True, message_id=None)

adapter.send = AsyncMock(side_effect=_send)
adapter.edit_message = AsyncMock(return_value=SimpleNamespace(success=True))
adapter.MAX_MESSAGE_LENGTH = 4096
consumer = _consumer(adapter)

consumer.on_delta(_SEGMENT_ONE)
task = asyncio.create_task(consumer.run())
await asyncio.sleep(0.08)
consumer.on_delta(None) # tool boundary
await asyncio.sleep(0.08)
consumer.on_delta(" " + _SEGMENT_TWO)
consumer.finish()
await task

payloads = adapter.delivered
assert _delivered_count(adapter, "let me check the processes too") == 1, (
"the __no_edit__ segment was delivered more than once: "
"delivered=%r" % (payloads,)
)
assert _delivered_count(adapter, _SEGMENT_TWO) == 1, (
"post-boundary continuation was lost or duplicated: "
"delivered=%r" % (payloads,)
)
Loading