fix: correct start_index calculation for reduced slice

Sentry correctly identified that passing slice_start was wrong because
reduced_recent doesn't start at slice_start in messages_dict - it starts
at slice_start + (len(recent_messages) - keep_count).

The backwards search needs to start from the TRUE beginning of reduced_recent
in messages_dict, otherwise it will miss assistant messages between slice_start
and the actual start of reduced_recent.
This commit is contained in:
Otto
2026-02-01 19:23:33 +00:00
parent 39177b6e43
commit 3a939d1989

View File

@@ -1186,9 +1186,12 @@ async def _stream_chat_chunks(
# Ensure tool pairs stay intact in the reduced slice
# Note: Search in messages_dict (full conversation) not recent_messages
# (already sliced), so we can find assistants outside the current slice.
# Use slice_start (relative to messages_dict) for correct search range.
# Calculate where reduced_recent starts in messages_dict
reduced_start_in_dict = slice_start + max(
0, len(recent_messages) - keep_count
)
reduced_recent = _ensure_tool_pairs_intact(
reduced_recent, messages_dict, slice_start
reduced_recent, messages_dict, reduced_start_in_dict
)
if has_system_prompt:
messages = [