fix(context): rescale threshold_tokens to real cap in live usage snapshot

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.
This commit is contained in:
allenliang2022
2026-05-31 21:46:50 +08:00
committed by nesquena-hermes
parent 1263cf03cf
commit cba69cd415
+19 -3
View File
@@ -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