Agent Zero AI framework
Make chat compaction resumable and secret-safe
Alessandro425cfc283b3c
diff --git a/plugins/_chat_compaction/AGENTS.md b/plugins/_chat_compaction/AGENTS.mdindex 7f318836c..739ed901a 100644--- a/plugins/_chat_compaction/AGENTS.md+++ b/plugins/_chat_compaction/AGENTS.md@@ -18,7 +18,10 @@- Preserve chat history integrity and persistence after compaction.- 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.- 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.- After replacing local history, clear the active Responses provider continuation while preserving stored response IDs for cleanup.- Do not discard original context data unless the compaction flow explicitly owns that behavior.diff --git a/plugins/_chat_compaction/helpers/compactor.py b/plugins/_chat_compaction/helpers/compactor.pyindex f0a56c225..d8e8b1a82 100644--- a/plugins/_chat_compaction/helpers/compactor.py+++ b/plugins/_chat_compaction/helpers/compactor.py@@ -146,6 +146,7 @@ async def run_compaction(agent.history = History(agent=agent)agent.history.add_message(ai=True, content=compacted_content)clear_responses_provider_state(agent)+ agent.data.pop(Agent.DATA_NAME_CTX_WINDOW, None)# Clear subordinate chainagent.data.pop(Agent.DATA_NAME_SUBORDINATE, None)diff --git a/plugins/_chat_compaction/prompts/compact.sys.md b/plugins/_chat_compaction/prompts/compact.sys.mdindex 49a2593be..62db6575c 100644--- a/plugins/_chat_compaction/prompts/compact.sys.md+++ b/plugins/_chat_compaction/prompts/compact.sys.md@@ -1,18 +1,34 @@-You are a conversation compactor. Produce the most concise summary possible while preserving critical information.+You are a conversation compactor. Preserve the minimum state another agent needs to resume the task correctly without rereading the original conversation.Rules:-- Extract only: key decisions, final outcomes, actionable facts, unresolved items-- Discard: intermediate reasoning, failed attempts, redundant exchanges, pleasantries-- Use terse bullet points, not prose-- Collapse related items into single lines-- Keep exact values: file paths, config values, code identifiers, credentials, URLs-- Preserve loaded skill names from skill_instructions metadata, but do not copy full skill bodies-- Omit anything that can be re-derived from context-- Group by topic, not chronology-- No meta-commentary about the summarization-- Target: 10-20% of original length--Output format:-- Markdown with short section headers-- Bullet lists, no paragraphs-- Code/paths in backticks inline, not fenced blocks unless multi-line+- Capture the latest active user request, including anything that superseded an earlier request.+- Preserve only explicit or clearly implied authorization and prohibitions; never broaden scope.+- Separate decisions from assumptions and mark unverified assumptions.+- Record completed work only with available evidence such as results, commands, tests, or artifact paths.+- Preserve exact file paths, URLs, config values, code identifiers, job IDs, context IDs, and verification status.+- Record pending workers or jobs with their IDs, state, and the next executable step.+- Record blockers and every relevant check that was not run.+- Preserve loaded skill names from `skill_instructions` metadata, but never copy skill bodies.+- Never include passwords, API keys, tokens, credentials, private keys, session secrets, or other secret values. Preserve only a secret's name, purpose, storage location, or reference alias when needed.+- Discard intermediate reasoning, redundant exchanges, pleasantries, and facts that can be safely re-derived.+- Use terse bullets, no prose or meta-commentary. Target 10-20% of the original length when possible.++Use exactly these sections in this order. Include `- None recorded.` for an empty section.++## Current objective and latest user request++## Authorized scope and prohibited actions++## Decisions and assumptions++## Completed work with evidence++## Modified files and artifacts++## Pending jobs and next executable step++## Blockers and checks not run++## Loaded skill names++## Secret referencesdiff --git a/tests/test_chat_compaction.py b/tests/test_chat_compaction.pyindex f9678169f..9bd678cd3 100644--- a/tests/test_chat_compaction.py+++ b/tests/test_chat_compaction.py@@ -72,6 +72,10 @@ class _CompactionAgent:def __init__(self):self.history = _CompactionHistory()self.data = {+ "ctx_window": {+ "text": "pre-compaction transcript with secret values",+ "tokens": 42,+ },"responses_state": {"response_id": "resp_current","previous_response_id": "resp_previous",@@ -86,6 +90,31 @@ class _CompactionAgent:self.data[key] = value+def test_compaction_prompt_is_resumable_task_state_without_secret_values():+ prompt = (+ PROJECT_ROOT / "plugins" / "_chat_compaction" / "prompts" / "compact.sys.md"+ ).read_text(encoding="utf-8")+ headings = [+ "## Current objective and latest user request",+ "## Authorized scope and prohibited actions",+ "## Decisions and assumptions",+ "## Completed work with evidence",+ "## Modified files and artifacts",+ "## Pending jobs and next executable step",+ "## Blockers and checks not run",+ "## Loaded skill names",+ "## Secret references",+ ]++ positions = [prompt.index(heading) for heading in headings]+ assert positions == sorted(positions)+ assert "Never include passwords, API keys, tokens, credentials" in prompt+ assert "Preserve only a secret's name, purpose, storage location" in prompt+ assert "Keep exact values: file paths, config values, code identifiers, credentials" not in prompt+ assert "next executable step" in prompt+ assert "job IDs" in prompt++def test_pre_compaction_backup_sanitizes_surrogate_text(tmp_path, monkeypatch):monkeypatch.setattr(compactor,@@ -193,3 +222,4 @@ async def test_manual_compaction_clears_active_responses_state(monkeypatch):assert "response_id" not in stateassert "previous_response_id" not in stateassert state["response_ids"] == ["resp_previous", "resp_current"]+ assert "ctx_window" not in agent.data