From cba69cd415498cbdd7ad0c64ec9de3066f4ffc3e Mon Sep 17 00:00:00 2001 From: allenliang2022 Date: Sun, 31 May 2026 21:46:50 +0800 Subject: [PATCH] fix(context): rescale threshold_tokens to real cap in live usage snapshot MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The default-only guard corrected context_length to the real per-model cap (e.g. 1M for claude-opus-4.7-1m) but left threshold_tokens pointing at the ContextCompressor's stale value (computed from the global 232K cap → 197.2k @ 85%). UI then showed 'auto-compress at 197.2k / 1M' which is misleading. Rescale threshold_tokens by the real/orig ratio so the displayed trigger reflects the actual window (e.g. ~850k @ 1M). NOTE: this only corrects the SSE display payload. The real auto-compress trigger lives inside ContextCompressor in hermes-agent (agent_init.py:1446 constructs it with the global cap). A full fix requires a parallel change upstream — tracked separately. --- api/streaming.py | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/api/streaming.py b/api/streaming.py index cbbd8b327..2a66b249e 100644 --- a/api/streaming.py +++ b/api/streaming.py @@ -4046,10 +4046,26 @@ def _run_agent_streaming( _real_ctx_cache[0] = _resolved_real # Apply the cached real cap when the guard determined one. if _real_ctx_cache[0]: + # Also rescale threshold_tokens by the same ratio so the + # auto-compress trigger reflects the real window, not + # the stale global cap (e.g. 197.2k @ 232K cap → ~850k + # @ 1M real cap). + _orig_cc_cl = getattr(_cc, 'context_length', 0) or 0 + _orig_thresh = getattr(_cc, 'threshold_tokens', 0) or 0 _cc_cl_u = _real_ctx_cache[0] - _usage['context_length'] = _cc_cl_u - _usage['threshold_tokens'] = getattr(_cc, 'threshold_tokens', 0) or 0 - _usage['last_prompt_tokens'] = getattr(_cc, 'last_prompt_tokens', 0) or 0 + if _orig_cc_cl > 0 and _orig_thresh > 0: + _scaled_thresh = int(_orig_thresh * _real_ctx_cache[0] / _orig_cc_cl) + _usage['context_length'] = _cc_cl_u + _usage['threshold_tokens'] = _scaled_thresh + _usage['last_prompt_tokens'] = getattr(_cc, 'last_prompt_tokens', 0) or 0 + else: + _usage['context_length'] = _cc_cl_u + _usage['threshold_tokens'] = _orig_thresh + _usage['last_prompt_tokens'] = getattr(_cc, 'last_prompt_tokens', 0) or 0 + else: + _usage['context_length'] = _cc_cl_u + _usage['threshold_tokens'] = getattr(_cc, 'threshold_tokens', 0) or 0 + _usage['last_prompt_tokens'] = getattr(_cc, 'last_prompt_tokens', 0) or 0 except Exception: pass