Skip to content

The agent that grows with you

fix(agent): guard micro-compaction refusals

KoNit-K42545ab0b090
diff --git a/agent/context_compressor.py b/agent/context_compressor.py
index f1969b3a8bf..dd6456a249c 100644
--- a/agent/context_compressor.py
+++ b/agent/context_compressor.py
@@ -165,7 +165,9 @@ _TRUNCATED_SUMMARY_MARKER = "finish_reason=length"
# 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",
+ r"^\s*(?:(?:sorry|i(?:['’]m| am)\s+sorry|i\s+apologize|as\s+an\s+ai)"
+ r"\s*[,;:]?\s*(?:but\s+)?)?i\s+"
+ r"(?:can(?:not|['’]t)|won['’]t|will not|must decline|am unable to|am not able to)\b",
re.IGNORECASE,
)
diff --git a/agent/micro_compaction.py b/agent/micro_compaction.py
index 34ff76340a4..73849829ccc 100644
--- a/agent/micro_compaction.py
+++ b/agent/micro_compaction.py
@@ -148,12 +148,16 @@ class MicroCompactionMixin:
message = response.choices[0].message
content = message.get("content") if isinstance(message, dict) else getattr(message, "content", message)
content = (content if isinstance(content, str) else str(content) if content else "").strip()
+
+ from agent.agent_runtime_helpers import strip_think_blocks
+ content = strip_think_blocks(None, content).strip()
if not content:
logger.info("micro-summarization returned empty content")
return None
-
- from agent.agent_runtime_helpers import strip_think_blocks
- return strip_think_blocks(None, content).strip() or None
+ if _cc()._is_summary_refusal(content):
+ logger.warning("micro-summarization returned refusal content — discarding unusable summary")
+ return None
+ return content
def _needs_defrag(self) -> bool:
"""Return True when the rolling summary is large enough to defrag."""
@@ -203,6 +207,8 @@ class MicroCompactionMixin:
if not self._micro_compact_enabled:
return messages
+ cursor_before = self._micro_compact_cursor
+
# Cadence gate: each pass breaks prompt cache once. Counted per invocation so a no-op turn
# can't wedge it.
every_n = max(1, int(self._micro_compact_every_n_turns or 1))
@@ -247,6 +253,8 @@ class MicroCompactionMixin:
updated_summary = self._micro_summarize_one(exchange_text)
if updated_summary is None:
_outcome = self._record_micro_failure(exchange_start, exchange_end)
+ if _outcome != "exchange_skipped":
+ self._micro_compact_cursor = cursor_before
_telemetry(_outcome, messages, tokens_after=_tokens_before, exchange_tokens=_exchange_tokens)
return messages
diff --git a/tests/agent/test_compressor_truncated_summary_guard.py b/tests/agent/test_compressor_truncated_summary_guard.py
index bfe3dc93b5e..b74e9fa9302 100644
--- a/tests/agent/test_compressor_truncated_summary_guard.py
+++ b/tests/agent/test_compressor_truncated_summary_guard.py
@@ -65,6 +65,10 @@ class TestSummaryRefusalGuard:
[
"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.",
+ "I'm sorry, but I can't provide the requested summary.",
+ "I’m sorry, but I can’t provide the requested summary.",
+ "I apologize, but I cannot create a summary of this conversation.",
+ "As an AI, I cannot provide the requested context checkpoint.",
],
)
def test_rejects_refusal_body(self, content):
@@ -238,3 +242,29 @@ class TestMicroSummarizeTruncationGuard:
):
result = c._micro_summarize_one("user: hi\nassistant: hello")
assert result == "merged summary"
+
+ def test_refusal_leaves_exchange_and_cursor_unchanged(self):
+ with patch("agent.context_compressor.get_model_context_length", return_value=100000):
+ c = ContextCompressor(
+ model="test",
+ quiet_mode=True,
+ protect_first_n=1,
+ protect_last_n=2,
+ config_context_length=40960,
+ )
+ c._micro_compact_enabled = True
+ messages = _msgs()
+ cursor_before = c._micro_compact_cursor
+ refusal = "I’m sorry, but I can’t provide the requested summary."
+ response_content = f"<think>Check the applicable policy.</think>\n{refusal}"
+
+ with patch(
+ "agent.auxiliary_client.call_llm",
+ return_value=_mock_response(response_content, "stop"),
+ ):
+ result = c._micro_compact(list(messages))
+
+ assert result == messages
+ assert c._micro_compact_cursor == cursor_before
+ assert c._micro_compact_rolling_summary == ""
+ assert not any(refusal in str(message.get("content")) for message in result)