The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(compression): let the next summary build on a fallback handoff
Adolaniume46d4c0ade67
diff --git a/agent/context_compressor.py b/agent/context_compressor.pyindex ffb99647e9c..0e7a6a9f938 100644--- a/agent/context_compressor.py+++ b/agent/context_compressor.py@@ -5304,11 +5304,16 @@ Write only the summary body. Do not include any preamble or prefix."""telemetry["failure_class"] = "summary_overload_degraded" if self._last_summary_overload_degraded else (telemetry.get("failure_class") or ("feasibility_skip" if feasibility_skip else "summary_generation_failed"))- return self._build_static_fallback_summary(+ summary = self._build_static_fallback_summary(turns_to_summarize,# A stale error from an earlier failure must not be embedded in a feasibility-skip fallback.reason=None if feasibility_skip else self._last_summary_error,)+ # The fallback row replaces the older handoff in the transcript, and the next compaction drops+ # handoff rows from its window as "already folded into _previous_summary". Publish it like a+ # real summary so its anchors reach the next summarizer instead of the stale in-memory one.+ self._previous_summary = self._strip_summary_prefix(summary)+ return summarydef _assemble_tail(self, messages: List[Dict[str, Any]], compress_end: int, tail_start: int, summary_indices: set,diff --git a/tests/agent/test_context_compressor_summary_continuity.py b/tests/agent/test_context_compressor_summary_continuity.pyindex 941a6c014fd..14e4c2d6a41 100644--- a/tests/agent/test_context_compressor_summary_continuity.py+++ b/tests/agent/test_context_compressor_summary_continuity.py@@ -459,3 +459,36 @@ def test_empty_post_handoff_window_noops_without_summary_call():assert compressor._last_compress_aborted is Falsetelemetry = compressor._last_compression_telemetry or {}assert telemetry.get("failure_class") == "empty_post_handoff_window"+++def _turns(tag: str, n: int = 6):+ return [+ {"role": "user" if i % 2 == 0 else "assistant", "content": f"{tag} turn {i}"}+ for i in range(n)+ ]+++def test_fallback_handoff_anchors_reach_the_next_summarizer():+ """A deterministic fallback replaces the older handoff in the transcript, and the next compaction drops+ handoff rows from its window as already folded. The next summarizer must therefore see the fallback's+ anchors, not only the stale in-memory summary from before the fallback."""+ compressor = _compressor()+ with patch("agent.context_compressor.call_llm", return_value=_response("FIRST healthy summary")):+ messages = compressor.compress(_messages_with_handoff("seed summary"))+ assert (compressor._previous_summary or "").endswith("FIRST healthy summary")++ messages = messages + [+ {"role": "user", "content": "FALLBACK-ANCHOR-ASK please fix the parser"},+ {"role": "assistant", "content": "looking at it"},+ ] + _turns("filler")+ with patch.object(compressor, "_generate_summary", lambda *a, **k: None):+ messages = compressor.compress(messages)+ assert compressor._last_summary_fallback_used is True+ assert any("FALLBACK-ANCHOR-ASK" in str(m.get("content")) for m in messages)++ messages = messages + _turns("later")+ with patch("agent.context_compressor.call_llm", return_value=_response("THIRD summary")) as mock_call:+ compressor.compress(messages)++ prompt = mock_call.call_args.kwargs["messages"][0]["content"]+ assert "FALLBACK-ANCHOR-ASK" in promptdiff --git a/website/docs/developer-guide/context-compression-and-caching.md b/website/docs/developer-guide/context-compression-and-caching.mdindex b7b78a618db..3199d9a11b9 100644--- a/website/docs/developer-guide/context-compression-and-caching.md+++ b/website/docs/developer-guide/context-compression-and-caching.md@@ -594,7 +594,8 @@ information across multiple compactions — items move from "In Progress" to "Donew progress is added, and obsolete information is removed.The `_previous_summary` field on the compressor instance stores the last summary-text for this purpose.+text for this purpose. A deterministic fallback summary is stored there too, since+it is the handoff the transcript now carries.## Before/After Example