Skip to content

The agent that grows with you

fix(compaction): name the emitted task heading in the update instruction

openclaw4ab7dec7562d
diff --git a/agent/context_compressor.py b/agent/context_compressor.py
index d2cf2a9d802..7504cc67c86 100644
--- a/agent/context_compressor.py
+++ b/agent/context_compressor.py
@@ -3531,7 +3531,7 @@ PREVIOUS SUMMARY:
NEW TURNS TO INCORPORATE:
{content_to_summarize}{_memory_section}
-Update the summary using this exact structure. PRESERVE all existing information that is still relevant. ADD new completed actions to the numbered list (continue numbering). Move items from "In Progress" to "Completed Actions" when done. Move answered questions to "Resolved Questions". Update "Active State" to reflect current state. Remove information only if it is clearly obsolete. CRITICAL: Update "## Active Task" to reflect the user's most recent unfulfilled input — this includes any question, decision request, or discussion turn that the assistant has not yet answered. Only write "None" if the last exchange was fully resolved.
+Update the summary using this exact structure. PRESERVE all existing information that is still relevant. ADD new completed actions to the numbered list (continue numbering). Move items from "In Progress" to "Completed Actions" when done. Move answered questions to "Resolved Questions". Update "Active State" to reflect current state. Remove information only if it is clearly obsolete. CRITICAL: Update "{HISTORICAL_TASK_HEADING}" to reflect the user's most recent unfulfilled input — this includes any question, decision request, or discussion turn that the assistant has not yet answered. Only write "None" if the last exchange was fully resolved.
{_template_sections}"""
else:
diff --git a/tests/agent/test_context_compressor_task_heading.py b/tests/agent/test_context_compressor_task_heading.py
new file mode 100644
index 00000000000..247d247716b
--- /dev/null
+++ b/tests/agent/test_context_compressor_task_heading.py
@@ -0,0 +1,40 @@
+"""The iterative-update instruction must name the heading the template emits."""
+
+from types import SimpleNamespace
+
+from agent.context_compressor import (
+ ContextCompressor,
+ HISTORICAL_TASK_HEADING,
+ _HISTORICAL_TASK_SECTION_RE,
+)
+
+
+def _iterative_prompt() -> str:
+ """The prompt built when a previous summary exists (the update path)."""
+ stub = SimpleNamespace(
+ tail_mode="lean",
+ _previous_summary="PREVIOUS SUMMARY BODY",
+ _bound_summary_input=lambda text: text,
+ )
+ stub._summary_template_sections = ContextCompressor._summary_template_sections
+ stub._build_summary_prompt = ContextCompressor._build_summary_prompt.__get__(stub)
+ return stub._build_summary_prompt("NEW TURNS", 2000, None, "", True)
+
+
+def test_update_instruction_names_the_emitted_heading():
+ prompt = _iterative_prompt()
+ assert f'Update "{HISTORICAL_TASK_HEADING}"' in prompt
+ assert '"## Active Task"' not in prompt
+
+
+def test_grounding_prepends_when_the_task_heading_differs():
+ """Why the names must agree: a foreign heading yields two task sections."""
+ body = "## Active Task\nUser asked: 'do X'\n\n## Goal\nthing\n"
+ assert _HISTORICAL_TASK_SECTION_RE.search(body) is None
+
+ grounded = ContextCompressor._ground_historical_task_snapshot.__func__(
+ ContextCompressor, body, [{"role": "user", "content": "do X"}]
+ )
+ headings = [line for line in grounded.splitlines() if line.startswith("## ")]
+ assert headings.count(HISTORICAL_TASK_HEADING) == 1
+ assert "## Active Task" in headings