From b92d2bc97d9c5e59bcb0307c0a7804b11d0fd296 Mon Sep 17 00:00:00 2001 From: Liang Jiaqing Date: Wed, 25 Mar 2026 08:37:46 +0800 Subject: [PATCH] fix: always compress history tags, adjust context trim thresholds --- ga.py | 2 +- llmcore.py | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/ga.py b/ga.py index b3aab51..8bff9ac 100644 --- a/ga.py +++ b/ga.py @@ -516,8 +516,8 @@ def get_global_memory(): script_dir = os.path.dirname(os.path.abspath(__file__)) with open(os.path.join(script_dir, 'memory/global_mem_insight.txt'), 'r', encoding='utf-8') as f: insight = f.read() with open(os.path.join(script_dir, 'assets/insight_fixed_structure.txt'), 'r', encoding='utf-8') as f: structure = f.read() - prompt += f"\n[Memory]\n" prompt += f'cwd = {os.path.abspath("./temp")} (用./引用)\n' + prompt += f"\n[Memory] (../memory)\n" prompt += structure + '\n../memory/global_mem_insight.txt:\n' prompt += insight + "\n" except FileNotFoundError: pass diff --git a/llmcore.py b/llmcore.py index 9b7cdca..3abc151 100644 --- a/llmcore.py +++ b/llmcore.py @@ -14,7 +14,7 @@ mykeys = _load_mykeys() proxy = mykeys.get("proxy", 'http://127.0.0.1:2082') proxies = {"http": proxy, "https": proxy} if proxy else None -def compress_history_tags(messages, keep_recent=12, max_len=1000): +def compress_history_tags(messages, keep_recent=10, max_len=1000): """Compress // tags in older messages to save tokens. Supports both prompt-style (ClaudeSession/LLMSession) and content-style (NativeClaudeSession) messages.""" compress_history_tags._cd = getattr(compress_history_tags, '_cd', 0) + 1 @@ -83,7 +83,7 @@ class ClaudeSession: self.raw_msgs, self.lock = [], threading.Lock() self.prompt_cache = cfg.get('prompt_cache', False) def _trim_messages(self, messages): - if not self.prompt_cache: compress_history_tags(messages) + compress_history_tags(messages) total = sum(len(m['prompt']) for m in messages) if total <= self.context_win * 3: return messages target, current, result = self.context_win * 3 * 0.6, 0, [] @@ -288,7 +288,7 @@ class LLMSession: return def make_messages(self, raw_list, omit_images=True): - if not self.prompt_cache: compress_history_tags(raw_list) + compress_history_tags(raw_list) messages = [] for i, msg in enumerate(raw_list): prompt = msg['prompt'] @@ -563,8 +563,8 @@ class NativeClaudeSession: self.history.append(msg) compress_history_tags(self.history) cost = sum(len(json.dumps(m, ensure_ascii=False)) for m in self.history) - if cost > self.context_win * 4: - target = self.context_win * 4 * 0.6 + if cost > self.context_win * 3: + target = self.context_win * 3 * 0.6 while len(self.history) > 2 and cost > target: self.history.pop(0); self.history.pop(0) cost = sum(len(json.dumps(m, ensure_ascii=False)) for m in self.history)