From 39177b6e43d3a751993c5738a0cddd46bfa7de69 Mon Sep 17 00:00:00 2001 From: Otto Date: Sun, 1 Feb 2026 17:36:51 +0000 Subject: [PATCH] fix(copilot): use messages_dict in fallback context compaction The fallback path in _stream_chat_chunks was passing recent_messages (already sliced) instead of messages_dict (full conversation) to _ensure_tool_pairs_intact. This caused the function to fail to find assistant messages that exist in the original conversation but were outside the sliced window. Fix: Pass messages_dict and slice_start (relative to messages_dict) instead of recent_messages and reduced_slice_start. Fixes SECRT-1861 Related: SECRT-1839 --- .../backend/backend/api/features/chat/service.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/autogpt_platform/backend/backend/api/features/chat/service.py b/autogpt_platform/backend/backend/api/features/chat/service.py index 20216162b5..efeacbea53 100644 --- a/autogpt_platform/backend/backend/api/features/chat/service.py +++ b/autogpt_platform/backend/backend/api/features/chat/service.py @@ -1184,11 +1184,11 @@ async def _stream_chat_chunks( else recent_messages ) # Ensure tool pairs stay intact in the reduced slice - reduced_slice_start = max( - 0, len(recent_messages) - keep_count - ) + # 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. reduced_recent = _ensure_tool_pairs_intact( - reduced_recent, recent_messages, reduced_slice_start + reduced_recent, messages_dict, slice_start ) if has_system_prompt: messages = [