Skip to content

Agent Zero AI framework

Fix blocking history compression edge cases

Alessandro6de7073bf9c0
diff --git a/extensions/python/message_loop_prompts_before/_90_organize_history_wait.py b/extensions/python/message_loop_prompts_before/_90_organize_history_wait.py
index 903981ad4..b2c925fe8 100644
--- a/extensions/python/message_loop_prompts_before/_90_organize_history_wait.py
+++ b/extensions/python/message_loop_prompts_before/_90_organize_history_wait.py
@@ -3,6 +3,8 @@ from agent import LoopData
from extensions.python.message_loop_end._10_organize_history import DATA_NAME_TASK
from helpers.defer import DeferredTask, THREAD_BACKGROUND
+MAX_SYNC_COMPRESSION_PASSES = 64
+
class OrganizeHistoryWait(Extension):
async def execute(self, loop_data: LoopData = LoopData(), **kwargs):
@@ -10,9 +12,13 @@ class OrganizeHistoryWait(Extension):
return
# sync action only required if the history is too large, otherwise leave it in background
+ passes = 0
while self.agent.history.is_over_limit():
+ passes += 1
+ before_tokens = self.agent.history.get_tokens()
+
# get task
- task: DeferredTask|None = self.agent.get_data(DATA_NAME_TASK)
+ task: DeferredTask | None = self.agent.get_data(DATA_NAME_TASK)
# Check if the task is already done
if task:
@@ -20,12 +26,39 @@ class OrganizeHistoryWait(Extension):
self.agent.context.log.set_progress("Compressing history...")
# Wait for the task to complete
- await task.result()
+ compressed = bool(await task.result())
# Clear the coroutine data after it's done
self.agent.set_data(DATA_NAME_TASK, None)
else:
# no task was running, start and wait
self.agent.context.log.set_progress("Compressing history...")
- await self.agent.history.compress()
+ compressed = await self.agent.history.compress()
+
+ after_tokens = self.agent.history.get_tokens()
+ if not compressed or after_tokens >= before_tokens:
+ self._log_compression_stalled(before_tokens, after_tokens)
+ break
+
+ if passes >= MAX_SYNC_COMPRESSION_PASSES:
+ self._log_compression_stalled(
+ before_tokens, after_tokens, max_passes=True
+ )
+ break
+
+ def _log_compression_stalled(
+ self, before_tokens: int, after_tokens: int, max_passes: bool = False
+ ) -> None:
+ if not self.agent:
+ return
+ detail = (
+ f"History compression stopped after {MAX_SYNC_COMPRESSION_PASSES} passes"
+ if max_passes
+ else "History compression could not reduce the prompt history further"
+ )
+ self.agent.context.log.log(
+ type="warning",
+ heading="History compression stalled",
+ content=f"{detail}. Tokens before: {before_tokens}; after: {after_tokens}.",
+ )
diff --git a/plugins/_chat_compaction/helpers/compactor.py b/plugins/_chat_compaction/helpers/compactor.py
index 011711582..19b2105c7 100644
--- a/plugins/_chat_compaction/helpers/compactor.py
+++ b/plugins/_chat_compaction/helpers/compactor.py
@@ -1,5 +1,6 @@
"""Core compaction logic for the compaction plugin."""
import os
+from collections import deque
from datetime import datetime
import models as models_module
@@ -15,6 +16,9 @@ from helpers.persist_chat import (
from helpers.state_monitor_integration import mark_dirty_all
MIN_COMPACTION_TOKENS = 1000
+COMPACTION_CHUNK_TARGET_RATIO = 0.9
+COMPACTION_CHUNK_VERIFY_RATIO = 0.98
+
from plugins._model_config.helpers.model_config import (
get_chat_model_config,
get_utility_model_config,
@@ -199,14 +203,11 @@ async def _compact_large_history(
agent, full_text: str, token_count: int, max_input_tokens: int, log_item, model
) -> str:
"""Handle large histories by splitting into chunks and summarizing iteratively."""
+ chunks = _split_text_for_compaction(agent, full_text, token_count, max_input_tokens)
log_item.update(
- content=f"History is large (~{token_count} tokens). Splitting into chunks...",
+ content=f"History is large (~{token_count} tokens). Splitting into {len(chunks)} chunks...",
)
- lines = full_text.split('\n')
- mid = len(lines) // 2
- chunks = ['\n'.join(lines[:mid]), '\n'.join(lines[mid:])]
-
summaries = []
for i, chunk in enumerate(chunks, 1):
log_item.update(content=f"Summarizing part {i}/{len(chunks)}...")
@@ -241,6 +242,91 @@ async def _compact_large_history(
return final_summary
+def _split_text_for_compaction(
+ agent, full_text: str, token_count: int, max_input_tokens: int
+) -> list[str]:
+ """Split large compaction input into prompt-safe chunks.
+
+ The previous line-midpoint split left a single-line payload as one empty
+ chunk plus one still-oversized chunk. This splitter derives a conservative
+ character target from the measured token density, then verifies each prompt
+ and keeps splitting any chunk that still exceeds the model input budget.
+ """
+ text = full_text or ""
+ if not text:
+ return []
+
+ prompt_overhead = _compaction_input_tokens(agent, "")
+ usable_tokens = max(max_input_tokens - prompt_overhead, 1)
+ target_tokens = max(int(usable_tokens * COMPACTION_CHUNK_TARGET_RATIO), 1)
+
+ if token_count <= target_tokens:
+ return [text]
+
+ chars_per_token = max(len(text) / max(token_count, 1), 0.01)
+ target_chars = max(int(target_tokens * chars_per_token), 1)
+ chunks = _split_text_by_chars(text, target_chars)
+
+ verified: list[str] = []
+ max_verified_tokens = max(int(max_input_tokens * COMPACTION_CHUNK_VERIFY_RATIO), 1)
+ pending = deque(chunk for chunk in chunks if chunk)
+
+ while pending:
+ chunk = pending.popleft()
+ if not chunk:
+ continue
+
+ if (
+ len(chunk) <= 1
+ or _compaction_input_tokens(agent, chunk) <= max_verified_tokens
+ ):
+ verified.append(chunk)
+ continue
+
+ split_chunks = _split_text_by_chars(chunk, max(len(chunk) // 2, 1))
+ if len(split_chunks) <= 1:
+ verified.append(chunk)
+ else:
+ pending.extendleft(reversed(split_chunks))
+
+ return verified
+
+
+def _compaction_input_tokens(agent, conversation: str) -> int:
+ system_prompt = agent.read_prompt("compact.sys.md")
+ user_prompt = agent.read_prompt("compact.msg.md", conversation=conversation)
+ return tokens.approximate_tokens(system_prompt) + tokens.approximate_tokens(
+ user_prompt
+ )
+
+
+def _split_text_by_chars(text: str, target_chars: int) -> list[str]:
+ if not text:
+ return []
+
+ target_chars = max(int(target_chars), 1)
+ chunks: list[str] = []
+ start = 0
+ length = len(text)
+
+ while start < length:
+ end = min(start + target_chars, length)
+ if end < length:
+ floor = start + max((end - start) // 2, 1)
+ split_at = text.rfind("\n", floor, end)
+ if split_at == -1:
+ split_at = text.rfind(" ", floor, end)
+ if split_at > start:
+ end = split_at + 1
+
+ chunk = text[start:end]
+ if chunk:
+ chunks.append(chunk)
+ start = end
+
+ return chunks
+
+
async def get_compaction_stats(context) -> dict:
"""
Get statistics about the current chat for the confirmation modal.
diff --git a/tests/test_chat_compaction.py b/tests/test_chat_compaction.py
new file mode 100644
index 000000000..39c4169d7
--- /dev/null
+++ b/tests/test_chat_compaction.py
@@ -0,0 +1,89 @@
+import sys
+from pathlib import Path
+
+import pytest
+
+PROJECT_ROOT = Path(__file__).resolve().parents[1]
+if str(PROJECT_ROOT) not in sys.path:
+ sys.path.insert(0, str(PROJECT_ROOT))
+
+from plugins._chat_compaction.helpers import compactor
+
+
+class _FakeAgent:
+ def read_prompt(self, name: str, **kwargs):
+ if name == "compact.sys.md":
+ return "system"
+ if name == "compact.msg.md":
+ return kwargs.get("conversation", "")
+ raise AssertionError(f"Unexpected prompt: {name}")
+
+
+class _FakeLog:
+ def __init__(self):
+ self.updates = []
+ self.streams = []
+
+ def update(self, **kwargs):
+ self.updates.append(kwargs)
+
+ def stream(self, **kwargs):
+ self.streams.append(kwargs)
+
+
+class _RecordingModel:
+ def __init__(self):
+ self.user_messages = []
+
+ async def unified_call(self, system_message, user_message, response_callback=None):
+ self.user_messages.append(user_message)
+ if response_callback:
+ await response_callback("done", "done")
+ return f"summary-{len(self.user_messages)}", None
+
+
+def test_compaction_splitter_wraps_single_line_85k_payload(monkeypatch):
+ monkeypatch.setattr(
+ compactor.tokens, "approximate_tokens", lambda text: len(text or "")
+ )
+
+ agent = _FakeAgent()
+ chunks = compactor._split_text_for_compaction(
+ agent,
+ "x" * 85_000,
+ token_count=85_000,
+ max_input_tokens=10_000,
+ )
+
+ assert len(chunks) > 2
+ assert all(chunks)
+ assert "".join(chunks) == "x" * 85_000
+ assert all(
+ compactor._compaction_input_tokens(agent, chunk) <= 10_000
+ for chunk in chunks
+ )
+
+
+async def test_large_compaction_does_not_send_unsplit_single_line_payload(monkeypatch):
+ monkeypatch.setattr(
+ compactor.tokens, "approximate_tokens", lambda text: len(text or "")
+ )
+
+ agent = _FakeAgent()
+ model = _RecordingModel()
+
+ summary = await compactor._compact_large_history(
+ agent,
+ "x" * 85_000,
+ token_count=85_000,
+ max_input_tokens=10_000,
+ log_item=_FakeLog(),
+ model=model,
+ )
+
+ chunk_messages = model.user_messages[:-1]
+ assert summary == f"summary-{len(model.user_messages)}"
+ assert len(chunk_messages) > 2
+ assert all(chunk_messages)
+ assert all(len(message) <= 10_000 for message in chunk_messages)
diff --git a/tests/test_history_compression_wait.py b/tests/test_history_compression_wait.py
new file mode 100644
index 000000000..ad6e81049
--- /dev/null
+++ b/tests/test_history_compression_wait.py
@@ -0,0 +1,96 @@
+import sys
+from pathlib import Path
+
+import pytest
+
+PROJECT_ROOT = Path(__file__).resolve().parents[1]
+if str(PROJECT_ROOT) not in sys.path:
+ sys.path.insert(0, str(PROJECT_ROOT))
+
+from extensions.python.message_loop_prompts_before._90_organize_history_wait import (
+ MAX_SYNC_COMPRESSION_PASSES,
+ OrganizeHistoryWait,
+)
+
+
+class _StalledHistory:
+ def __init__(self):
+ self.compress_calls = 0
+
+ def is_over_limit(self):
+ return self.compress_calls < 2
+
+ def get_tokens(self):
+ return 1234
+
+ async def compress(self):
+ self.compress_calls += 1
+ return False
+
+
+class _MaxPassHistory:
+ def __init__(self):
+ self.compress_calls = 0
+ self.tokens = 2000
+
+ def is_over_limit(self):
+ return True
+
+ def get_tokens(self):
+ return self.tokens
+
+ async def compress(self):
+ self.compress_calls += 1
+ self.tokens -= 1
+ return True
+
+
+class _FakeLog:
+ def __init__(self):
+ self.entries = []
+
+ def set_progress(self, *args, **kwargs):
+ pass
+
+ def log(self, **kwargs):
+ self.entries.append(kwargs)
+
+
+class _FakeAgent:
+ def __init__(self, history=None):
+ self.data = {}
+ self.history = history or _StalledHistory()
+ self.context = type("Context", (), {"log": _FakeLog()})()
+
+ def get_data(self, key):
+ return self.data.get(key)
+
+ def set_data(self, key, value):
+ self.data[key] = value
+
+
+async def test_history_wait_stops_when_compression_makes_no_progress():
+ agent = _FakeAgent()
+
+ await OrganizeHistoryWait(agent).execute()
+
+ assert agent.history.compress_calls == 1
+ assert agent.context.log.entries
+ assert agent.context.log.entries[-1]["heading"] == "History compression stalled"
+
+
+async def test_history_wait_stops_after_max_sync_compression_passes():
+ history = _MaxPassHistory()
+ agent = _FakeAgent(history)
+
+ await OrganizeHistoryWait(agent).execute()
+
+ assert history.compress_calls == MAX_SYNC_COMPRESSION_PASSES
+ assert agent.context.log.entries
+ assert agent.context.log.entries[-1]["heading"] == "History compression stalled"
+ assert (
+ f"stopped after {MAX_SYNC_COMPRESSION_PASSES} passes"
+ in agent.context.log.entries[-1]["content"]
+ )