Skip to content

Agent Zero AI framework

Preserve compacted context in provider history

Alessandro52ec111d12af
diff --git a/plugins/_chat_compaction/AGENTS.md b/plugins/_chat_compaction/AGENTS.md
index 739ed901a..d0c985afb 100644
--- a/plugins/_chat_compaction/AGENTS.md
+++ b/plugins/_chat_compaction/AGENTS.md
@@ -19,6 +19,7 @@
- Backup JSON and transcript artifacts must remain UTF-8 writable when chat content contains malformed Unicode such as lone surrogates.
- Keep generated summaries bounded by configured model and token limits.
- Compacted summaries must be resumable task state: preserve the latest request, authorization boundaries, decisions, evidence, modified artifacts, pending jobs and their IDs, the next executable step, blockers, and checks not run.
+- Store the compacted summary as non-assistant history context so provider-history normalization cannot discard it as an orphaned assistant turn.
- Preserve loaded skill names from `skill_instructions` metadata without copying full skill bodies into compacted summaries.
- Preserve only secret references such as names, aliases, purposes, or storage locations; never preserve secret values.
- Clear the cached context window after replacing history so stale transcript content is not persisted as active resumable state; the cache rebuilds on the next model turn.
diff --git a/plugins/_chat_compaction/helpers/compactor.py b/plugins/_chat_compaction/helpers/compactor.py
index d8e8b1a82..b9285e4aa 100644
--- a/plugins/_chat_compaction/helpers/compactor.py
+++ b/plugins/_chat_compaction/helpers/compactor.py
@@ -86,7 +86,7 @@ async def run_compaction(
2. Estimates token count and checks against model context window
3. If needed, splits history and summarizes iteratively
4. Calls the LLM to generate a comprehensive summary
- 5. Replaces the history with a single AI message containing the summary
+ 5. Replaces the history with a single context message containing the summary
6. Resets the log and creates a response log item
7. Persists the changes
@@ -144,7 +144,8 @@ async def run_compaction(
compacted_content = f"## Context compacted\n\n{summary}{backup_note}"
agent.history = History(agent=agent)
- agent.history.add_message(ai=True, content=compacted_content)
+ # History summaries are context, not orphaned assistant turns.
+ agent.history.add_message(ai=False, content=compacted_content)
clear_responses_provider_state(agent)
agent.data.pop(Agent.DATA_NAME_CTX_WINDOW, None)
diff --git a/tests/test_chat_compaction.py b/tests/test_chat_compaction.py
index 9bd678cd3..87a7ad00a 100644
--- a/tests/test_chat_compaction.py
+++ b/tests/test_chat_compaction.py
@@ -3,6 +3,7 @@ from types import SimpleNamespace
from pathlib import Path
import pytest
+from langchain_core.messages import HumanMessage
PROJECT_ROOT = Path(__file__).resolve().parents[1]
if str(PROJECT_ROOT) not in sys.path:
@@ -189,7 +190,9 @@ async def test_large_compaction_does_not_send_unsplit_single_line_payload(monkey
@pytest.mark.asyncio
-async def test_manual_compaction_clears_active_responses_state(monkeypatch):
+async def test_manual_compaction_preserves_summary_and_clears_responses_state(
+ monkeypatch,
+):
async def fake_single_pass(*args, **kwargs):
return "summary"
@@ -218,6 +221,11 @@ async def test_manual_compaction_clears_active_responses_state(monkeypatch):
await compactor.run_compaction(context)
+ prompt_history = agent.history.current.output_langchain()
+ assert len(prompt_history) == 1
+ assert isinstance(prompt_history[0], HumanMessage)
+ assert "## Context compacted\n\nsummary" in prompt_history[0].content
+
state = agent.data["responses_state"]
assert "response_id" not in state
assert "previous_response_id" not in state