fix: always compress history tags, adjust context trim thresholds

This commit is contained in:
Liang Jiaqing
2026-03-25 08:37:46 +08:00
parent ddce7ef549
commit b92d2bc97d
2 changed files with 6 additions and 6 deletions

2
ga.py
View File

@@ -516,8 +516,8 @@ def get_global_memory():
script_dir = os.path.dirname(os.path.abspath(__file__)) 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, '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() 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'cwd = {os.path.abspath("./temp")} (用./引用)\n'
prompt += f"\n[Memory] (../memory)\n"
prompt += structure + '\n../memory/global_mem_insight.txt:\n' prompt += structure + '\n../memory/global_mem_insight.txt:\n'
prompt += insight + "\n" prompt += insight + "\n"
except FileNotFoundError: pass except FileNotFoundError: pass

View File

@@ -14,7 +14,7 @@ mykeys = _load_mykeys()
proxy = mykeys.get("proxy", 'http://127.0.0.1:2082') proxy = mykeys.get("proxy", 'http://127.0.0.1:2082')
proxies = {"http": proxy, "https": proxy} if proxy else None 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 <thinking>/<tool_use>/<tool_result> tags in older messages to save tokens. """Compress <thinking>/<tool_use>/<tool_result> tags in older messages to save tokens.
Supports both prompt-style (ClaudeSession/LLMSession) and content-style (NativeClaudeSession) messages.""" Supports both prompt-style (ClaudeSession/LLMSession) and content-style (NativeClaudeSession) messages."""
compress_history_tags._cd = getattr(compress_history_tags, '_cd', 0) + 1 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.raw_msgs, self.lock = [], threading.Lock()
self.prompt_cache = cfg.get('prompt_cache', False) self.prompt_cache = cfg.get('prompt_cache', False)
def _trim_messages(self, messages): 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) total = sum(len(m['prompt']) for m in messages)
if total <= self.context_win * 3: return messages if total <= self.context_win * 3: return messages
target, current, result = self.context_win * 3 * 0.6, 0, [] target, current, result = self.context_win * 3 * 0.6, 0, []
@@ -288,7 +288,7 @@ class LLMSession:
return return
def make_messages(self, raw_list, omit_images=True): 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 = [] messages = []
for i, msg in enumerate(raw_list): for i, msg in enumerate(raw_list):
prompt = msg['prompt'] prompt = msg['prompt']
@@ -563,8 +563,8 @@ class NativeClaudeSession:
self.history.append(msg) self.history.append(msg)
compress_history_tags(self.history) compress_history_tags(self.history)
cost = sum(len(json.dumps(m, ensure_ascii=False)) for m in self.history) cost = sum(len(json.dumps(m, ensure_ascii=False)) for m in self.history)
if cost > self.context_win * 4: if cost > self.context_win * 3:
target = self.context_win * 4 * 0.6 target = self.context_win * 3 * 0.6
while len(self.history) > 2 and cost > target: while len(self.history) > 2 and cost > target:
self.history.pop(0); self.history.pop(0) self.history.pop(0); self.history.pop(0)
cost = sum(len(json.dumps(m, ensure_ascii=False)) for m in self.history) cost = sum(len(json.dumps(m, ensure_ascii=False)) for m in self.history)