fix(gateway): don't claim deleted head chunks as delivered in the empty-fallback recovery

Follow-up to #79669. That PR routed the three fallback recorder sites through
_record_turn_final_payload so a split turn would record the unsplit ledger
instead of a tail-only payload. For two of them that is right. For
_send_empty_fallback_final it is wrong, and it reintroduces the #78541 swallow
at the one site that was supposed to be fixed.

_send_empty_fallback_final is a *replacement* recovery: it sends the completed
text as a fresh message and deletes every tracked segment preview -- which on an
overflow split includes the sealed head chunks. After it runs, the only thing
on screen is the message it just sent. Recording the ledger there claims
delivery for text the same function just removed, so delivered_final_matches()
returns True, the gateway suppresses its own send, and the user is left with a
fraction of the answer.

Observed with a probe driving the real run() loop (543-char reply, 475-char head
sealed then deleted, 67-char tail committed):

  before this fix        recorded=543  matches=True   -> suppressed, 67/543 on screen
  after  this fix        recorded=67   matches=False  -> gateway sends the full answer

Record final_text verbatim here instead. The sibling site in
_send_fallback_final keeps the recorder: its delete is gated on
`continuation == final_text` and targets only the single active partial, never
the sealed heads, so the ledger correctly describes what survives.

The distinction is whether a recovery ADDS to what is on screen or REPLACES it.
Additive paths may record the ledger; replacing paths must record only what they
leave behind. _try_fresh_final is the same shape and #79669 handled it by
refusing the route on split turns.

Test drives the real seal-then-delete sequence and asserts the mismatch, so the
gateway is required to re-send. Mutation-checked: restoring the recorder call
turns it red.
This commit is contained in:
kshitij 2026-08-06 03:13:32 +05:30
parent 392e3a8c53
commit 68ebb198c7
2 changed files with 51 additions and 3 deletions

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

View File

@ -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