From 188e9e1114f0dfdeb95f9b1592bc81777a8226fa Mon Sep 17 00:00:00 2001 From: jayeshthk Date: Thu, 10 Apr 2025 02:29:40 +0530 Subject: [PATCH] =?UTF-8?q?Optimize=20head=E2=80=91truncation=20loop=20in?= =?UTF-8?q?=20`summarize=5Freal`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- aider/history.py | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/aider/history.py b/aider/history.py index a4727b941..9836be0ca 100644 --- a/aider/history.py +++ b/aider/history.py @@ -63,37 +63,38 @@ class ChatSummary: if split_index <= min_split: return self.summarize_all(messages) + # Split head and tail head = messages[:split_index] tail = messages[split_index:] - sized = sized[:split_index] - head.reverse() - sized.reverse() + # Only size the head once + sized_head = sized[:split_index] + + # Precompute token limit (fallback to 4096 if undefined) + model_max_input_tokens = self.models[0].info.get("max_input_tokens") or 4096 + model_max_input_tokens -= 512 # reserve buffer for safety + keep = [] total = 0 - # These sometimes come set with value = None - model_max_input_tokens = self.models[0].info.get("max_input_tokens") or 4096 - model_max_input_tokens -= 512 - - for i in range(split_index): - total += sized[i][0] + # Iterate in original order, summing tokens until limit + for tokens, msg in sized_head: + total += tokens if total > model_max_input_tokens: break - keep.append(head[i]) - - keep.reverse() + keep.append(msg) + # No need to reverse lists back and forth summary = self.summarize_all(keep) - tail_tokens = sum(tokens for tokens, msg in sized[split_index:]) + # If the combined summary and tail still fits, return directly summary_tokens = self.token_count(summary) - - result = summary + tail + tail_tokens = sum(tokens for tokens, _ in sized[split_index:]) if summary_tokens + tail_tokens < self.max_tokens: - return result + return summary + tail - return self.summarize_real(result, depth + 1) + # Otherwise recurse with increased depth + return self.summarize_real(summary + tail, depth + 1) def summarize_all(self, messages): content = ""