feat: tighten context budget - maxlen 15k, trim refactor with staged compress

This commit is contained in:
Jiaqing Liang authored and Liang Jiaqing committed 2026-05-13 23:02:52 +08:00
1 parent 853b0dd0d6
commit e4c39b16d3
2 files changed
+15 -14

No files matched your search

+2 -2
View File
@@ -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"
+13 -12
View File
@@ -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 = ""