The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(agent): reject refusal-only context summaries
KoNit-Kf6f7b13f7cf4
diff --git a/agent/context_compressor.py b/agent/context_compressor.pyindex f337eb65d8a..f1969b3a8bf 100644--- a/agent/context_compressor.py+++ b/agent/context_compressor.py@@ -158,6 +158,27 @@ def _response_finish_reason(response: Any) -> str:# into every subsequent iterative-update prompt. (Ported from earendil-works/pi#7048 / commit 97fa14e39.)_TRUNCATED_SUMMARY_MARKER = "finish_reason=length"+# A provider can return a natural-language refusal with finish_reason="stop". It is+# non-empty, so the usual response validation accepts it, but it contains none of+# the checkpoint needed to safely replace the compacted turns. Keep this narrow:+# a real summary may mention a refusal in a recorded turn, while a refusal as the+# whole response begins with one of these phrases and refers to the requested+# summary/checkpoint.+_SUMMARY_REFUSAL_PREFIX_RE = re.compile(+ r"^\s*(?:sorry[,:]?\s+)?i\s+(?:can't|cannot|won't|will not|must decline|am unable to|am not able to)\b",+ re.IGNORECASE,+)+++def _is_summary_refusal(content: str) -> bool:+ """Return whether a complete response is a refusal instead of a summary."""+ normalized = " ".join(content.split())+ if not _SUMMARY_REFUSAL_PREFIX_RE.match(normalized):+ return False+ # Limit the search to the opener so a structured checkpoint that records a+ # historical refusal elsewhere is not rejected.+ return any(term in normalized[:400].casefold() for term in ("summary", "summarize", "checkpoint"))+def _is_summary_access_or_quota_error(exc: Exception) -> bool:"""Return True for non-retryable summary auth, permission, or quota errors."""@@ -655,7 +676,9 @@ def _classify_summary_failure(e: Exception) -> _SummaryFailureKind:# HTTP 200 with empty body from a degraded provider, plus the sibling "no usable response"# shapes from _validate_llm_response.empty_content=isinstance(e, RuntimeError) and any(- m in err for m in ("empty content", "llm returned none response", "llm returned invalid response")+ m in err for m in (+ "empty content", "refusal content", "llm returned none response", "llm returned invalid response",+ )),# Truncated summary: one main-model retry, then ABORT preserving the session.truncated=isinstance(e, RuntimeError) and _TRUNCATED_SUMMARY_MARKER in err,@@ -3583,6 +3606,11 @@ Summary generation was unavailable, so this is a best-effort deterministic fallb# error, rather than replacing real context with an empty summary.if not content.strip():raise RuntimeError(f"Context compression LLM returned empty content {where}")+ if _is_summary_refusal(content):+ # Treat a refusal as unusable content. This deliberately reuses the+ # established fallback/cooldown/abort path for an empty body, so it+ # can never be committed as `_previous_summary`.+ raise RuntimeError(f"Context compression LLM returned refusal content {where}")# A finish_reason of "length" means the summarizer hit its output token cap mid-generation: the text# present is PARTIAL. Persisting a partial summary as the compaction checkpoint silently truncates# the conversation's memory — the cut-off text replaces the real middle turns AND is fed back intodiff --git a/tests/agent/test_compressor_truncated_summary_guard.py b/tests/agent/test_compressor_truncated_summary_guard.pyindex cd7ae10ce48..bfe3dc93b5e 100644--- a/tests/agent/test_compressor_truncated_summary_guard.py+++ b/tests/agent/test_compressor_truncated_summary_guard.py@@ -23,6 +23,7 @@ import pytestfrom agent.context_compressor import (ContextCompressor,+ _is_summary_refusal,_response_finish_reason,)@@ -58,6 +59,22 @@ class TestResponseFinishReason:assert _response_finish_reason(None) == ""+class TestSummaryRefusalGuard:+ @pytest.mark.parametrize(+ "content",+ [+ "I can't produce this summary as requested. The instructions conflict with my operating rules.",+ "Sorry, I am unable to create a context checkpoint for this conversation.",+ ],+ )+ def test_rejects_refusal_body(self, content):+ assert _is_summary_refusal(content) is True++ def test_accepts_structured_summary_that_records_a_refusal(self):+ summary = "## Goal\nPreserve the user's task.\n\n## Completed Actions\n1. Recorded that a provider refused an earlier request."+ assert _is_summary_refusal(summary) is False++class TestGenerateSummaryTruncationGuard:def test_length_stop_is_rejected_and_aborts(self):"""A length-stopped summary must not become a checkpoint; with no@@ -157,6 +174,24 @@ class TestGenerateSummaryTruncationGuard:assert result is not Noneassert "complete summary" in result+ def test_refusal_body_is_rejected_and_never_becomes_previous_summary(self):+ """A stop-terminated refusal is not a usable compaction checkpoint."""+ with patch("agent.context_compressor.get_model_context_length", return_value=100000):+ c = ContextCompressor(+ model="test", quiet_mode=True,+ protect_first_n=2, protect_last_n=2,+ abort_on_summary_failure=False,+ )+ msgs = _msgs()+ refusal = "I can't produce this summary as requested. The instructions conflict with my operating rules."+ with patch("agent.context_compressor.call_llm", return_value=_mock_response(refusal, "stop")):+ result = c.compress(msgs, current_tokens=999999, force=True)++ assert result == msgs+ assert c._last_summary_empty_content_failure is True+ assert c._last_compress_aborted is True+ assert c._previous_summary is None or refusal not in (c._previous_summary or "")+def test_missing_finish_reason_still_succeeds(self):"""Providers that omit finish_reason entirely must not be rejected."""with patch("agent.context_compressor.get_model_context_length", return_value=100000):