fix(compaction): cap post-tool attempts per turn

This commit is contained in:
Dom Bejar 2026-07-13 16:37:01 +00:00 committed by Teknium
parent 644b397fb2
commit fca883f06f
2 changed files with 36 additions and 1 deletions

View file

@ -5177,7 +5177,12 @@ def run_conversation(
messages, tools=agent.tools or None
)
if agent.compression_enabled and _compressor.should_compress(_real_tokens):
if (
agent.compression_enabled
and compression_attempts < 3
and _compressor.should_compress(_real_tokens)
):
compression_attempts += 1
agent._safe_print(" ⟳ compacting context…")
messages, active_system_prompt = agent._compress_context(
messages, system_message,

View file

@ -0,0 +1,30 @@
"""Regression guard for the post-tool automatic-compression attempt cap.
The pre-API and overflow compression paths share ``compression_attempts`` as a
three-pass per-turn backstop. The post-tool path must use the same counter;
otherwise a long tool loop can compact after every tool response for the
lifetime of the turn.
"""
from __future__ import annotations
import inspect
def _post_tool_compression_block() -> str:
from agent import conversation_loop
source = inspect.getsource(conversation_loop.run_conversation)
start = source.index("# Use real token counts from the API response")
end = source.index("# Save session log incrementally", start)
return source[start:end]
def test_post_tool_compression_uses_shared_per_turn_attempt_cap():
block = _post_tool_compression_block()
assert "compression_attempts < 3" in block, (
"post-tool compression must stop after the shared three attempts per turn"
)
assert "compression_attempts += 1" in block, (
"post-tool compression must consume one shared per-turn attempt"
)