From e4c39b16d3bbda10aa7a4ad656183cfe51ce6126 Mon Sep 17 00:00:00 2001 From: Jiaqing Liang Date: Wed, 13 May 2026 15:23:10 +0800 Subject: [PATCH] feat: tighten context budget - maxlen 15k, trim refactor with staged compress --- ga.py | 4 ++-- llmcore.py | 25 +++++++++++++------------ 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/ga.py b/ga.py index f040cf08..c2e5681d 100644 --- a/ga.py +++ b/ga.py @@ -414,7 +414,7 @@ class GenericAgentHandler(BaseHandler): count=count, show_linenos=show_linenos) if show_linenos and not result.startswith("Error:"): result = '由于设置了show_linenos,以下返回信息为:(行号|)内容 。\n' + result if ' ... [TRUNCATED]' in result: result += '\n\n(某些行被截断,如需完整内容可改用 code_run 读取)' - maxlen = 20000 // args.get('_tool_num', 1) + maxlen = 15000 // args.get('_tool_num', 1) result = smart_format(result, max_str_len=maxlen, omit_str='\n\n[omitted long content]\n\n') next_prompt = self._get_anchor_prompt(skip=args.get('_index', 0) > 0) log_memory_access(path) @@ -528,7 +528,7 @@ class GenericAgentHandler(BaseHandler): flush(); parts.append(line); cnt = 0; last = '' else: cnt += 1; last = line flush() - return "\n".join(parts[-150:]) + return "\n".join(parts[-100:]) def _get_anchor_prompt(self, skip=False): if skip: return "\n" diff --git a/llmcore.py b/llmcore.py index bf239e7e..17f376ea 100644 --- a/llmcore.py +++ b/llmcore.py @@ -88,18 +88,19 @@ def safeprint(*argv): print = safeprint def trim_messages_history(history, context_win): + cap = context_win * 3 + target = int(cap * 0.6) + def cost(): return sum(len(json.dumps(m, ensure_ascii=False)) for m in history) compress_history_tags(history) - cost = sum(len(json.dumps(m, ensure_ascii=False)) for m in history) - print(f'[Debug] Current context: {cost} chars, {len(history)} messages.') - if cost > context_win * 3: - compress_history_tags(history, keep_recent=4, force=True) # trim breaks cache, so compress more btw - target = context_win * 3 * 0.6 - while len(history) > 5 and cost > target: - history.pop(0) - while history and history[0].get('role') != 'user': history.pop(0) - if history and history[0].get('role') == 'user': history[0] = _sanitize_leading_user_msg(history[0]) - cost = sum(len(json.dumps(m, ensure_ascii=False)) for m in history) - print(f'[Debug] Trimmed context, current: {cost} chars, {len(history)} messages.') + print(f'[Debug] Current context: {cost()} chars, {len(history)} messages.') + if cost() <= cap: return + compress_history_tags(history, keep_recent=4, force=True) + if cost() <= target: return + while len(history) > 9 and cost() > target: + history.pop(0) + while history and history[0].get('role') != 'user': history.pop(0) + if history and history[0].get('role') == 'user': history[0] = _sanitize_leading_user_msg(history[0]) + print(f'[Debug] Trimmed context, current: {cost()} chars, {len(history)} messages.') def auto_make_url(base, path): b, p = base.rstrip('/'), path.strip('/') @@ -510,7 +511,7 @@ class BaseSession: self.api_key = cfg['apikey'] self.api_base = cfg['apibase'].rstrip('/') self.model = cfg.get('model', '') - self.context_win = cfg.get('context_win', 28000) + self.context_win = cfg.get('context_win', 30000) self.history = [] self.lock = threading.Lock() self.system = ""