From 3a939d19897076ba32fc30d5e7dbe9eb753d5c4e Mon Sep 17 00:00:00 2001 From: Otto Date: Sun, 1 Feb 2026 19:23:33 +0000 Subject: [PATCH] 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. --- .../backend/backend/api/features/chat/service.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/autogpt_platform/backend/backend/api/features/chat/service.py b/autogpt_platform/backend/backend/api/features/chat/service.py index efeacbea53..ddc64d64b2 100644 --- a/autogpt_platform/backend/backend/api/features/chat/service.py +++ b/autogpt_platform/backend/backend/api/features/chat/service.py @@ -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 = [