From 481915587e60becaac7ab25bf4963348a882d1d1 Mon Sep 17 00:00:00 2001 From: Teknium Date: Tue, 24 Mar 2026 09:25:06 -0700 Subject: [PATCH] fix: update context pressure warnings and token estimates after compaction Reset context pressure warnings and update last_prompt_tokens and last_completion_tokens in the context compressor to prevent stale values from causing excessive warnings and re-triggering compression. This change ensures accurate pressure calculations following the compaction process. --- run_agent.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/run_agent.py b/run_agent.py index a09d898d6..08e2807b8 100644 --- a/run_agent.py +++ b/run_agent.py @@ -4609,9 +4609,18 @@ class AIAgent: except Exception as e: logger.debug("Session DB compression split failed: %s", e) - # Reset context pressure warnings — usage drops after compaction + # Reset context pressure warnings and token estimate — usage drops + # after compaction. Without this, the stale last_prompt_tokens from + # the previous API call causes the pressure calculation to stay at + # >1000% and spam warnings / re-trigger compression in a loop. self._context_50_warned = False self._context_70_warned = False + _compressed_est = ( + estimate_tokens_rough(new_system_prompt) + + estimate_messages_tokens_rough(compressed) + ) + self.context_compressor.last_prompt_tokens = _compressed_est + self.context_compressor.last_completion_tokens = 0 return compressed, new_system_prompt