Merge pull request #79693 from kshitijk4poor/fix/79669-empty-fallback-deleted-heads

fix(gateway): don't claim deleted head chunks as delivered in the empty-fallback recovery
This commit is contained in:
kshitij
2026-08-06 03:24:26 +05:30
committed by GitHub
2 changed files with 51 additions and 3 deletions
+11 -3
View File
@@ -1618,9 +1618,17 @@ class GatewayStreamConsumer:
self._final_response_sent = True
self._final_content_delivered = True
# Fresh commit of the complete answer after a failed finalize (#71643).
# Recorder-routed so a split turn records the unsplit ledger instead of
# a tail-only payload the gateway would read as stale (#78541).
self._record_turn_final_payload(final_text)
#
# Record ``final_text`` VERBATIM -- do not route through
# _record_turn_final_payload here. This recovery deleted the sealed
# segment previews just above, so the only thing left on screen is the
# message we just sent. On a split turn the ledger holds the sealed
# heads too, and recording it would claim delivery for text this path
# just removed -- the gateway would then suppress and the user would be
# left with a fraction of the answer (the #78541 swallow, reintroduced).
self._delivered_final_text = ensure_closed_code_fences(
self._clean_for_display(final_text or "")
).strip()
self._last_sent_text = final_text
self._fallback_prefix = ""
self._fallback_preserve_partial_messages = False
@@ -567,3 +567,43 @@ async def test_failed_final_edit_after_split_records_visible_payload():
assert await consumer._send_or_edit(full, finalize=True) is False
assert consumer._final_content_delivered is True
assert consumer.delivered_final_matches(full) is True
@pytest.mark.asyncio
async def test_empty_fallback_final_after_split_records_only_what_survives():
"""A recovery that DELETES the sealed heads must not claim them as delivered.
``_send_empty_fallback_final`` replaces the active segment: it sends the
completed text as a fresh message and deletes every tracked segment
preview -- including the sealed head chunks of an overflow split. Only the
new message is left on screen, so the recorded payload must be that message
verbatim, NOT the stream ledger. Recording the ledger would claim delivery
for text this path just removed, and the gateway would suppress its own
send and leave the user with a fraction of the answer (#78541).
"""
adapter = _SplittingAdapter()
consumer = GatewayStreamConsumer(
adapter,
"chat-split",
StreamConsumerConfig(
edit_interval=0.0, buffer_threshold=1, cursor="",
fresh_final_after_seconds=0.0,
),
)
head = "HEAD text. " * 40
tail = "TAIL text. " * 6
complete = head + tail
# Sealed head chunk is on screen and tracked as a segment preview.
head_id = await consumer._send_new_chunk(head, None, final=False)
consumer._turn_split_delivery = True
consumer._stream_ledger = complete
assert head_id in consumer._segment_preview_message_ids
# The recovery commits only ``tail`` and deletes the sealed head.
assert await consumer._send_empty_fallback_final(tail) == "delivered"
assert head_id in adapter.deleted, "expected the recovery to delete the head"
# The head is gone from the chat, so the complete answer was NOT delivered:
# the gateway must be told this is a mismatch and send it.
assert consumer.delivered_final_matches(complete) is False